1: # tuto for launching experiment
2: 
3: ## install env
4: 
5: First install the both conda (check readme for more details):
6: - Inference env `sglang47` (you can upgrade to lateset SGLang version) 
7: - Training env `unsloth_env`
8: 
9: 
10: Now fist let's set path variables
11: 
12: ## init variables
13: 
14: ```bash
15: export work_path="/home/flowers/work"
16: export soar_path="$work_path/SOAR"
17: export model_id_base="Qwen2.5-Coder-3B-Instruct" # model that you want to use here
18: export path_base_model="$work_path/hf/$model_id_base"
19: export path_embed_model="$work_path/hf/CodeRankEmbed"
20: export split="train" # "train" -> public test set  or "val" -> public test set 
21: 
22: # n_sample_task -> number of task sample per task to finetuned the model (should be 50 but for demo set to 5)
23: export n_sample_task=50
24: export model_id="${model_id_base}_n${n_sample_task}"
25: ```
26: 
27: ## LOOP start here 
28: 
29: Choose generation number:
30: 
31: ```bash
32: # generation number strat at 0 then 1,2,...
33: 
34: export gen="0"
35: 
36: if [ "$gen" -eq 0 ]; then
37:     # use base model for generation 0
38:     export path_model="$work_path/hf/$model_id_base" 
39:     
40: else
41:     # use finetuned model for other generation
42:     export path_model="$work_path/hf/trained/$model_id_base/gen-$gen/$model_id"
43: fi
44: ```
45: 
46: ### sampling intial solution
47: 
48: To sample the 3k initial solution (you can change -k 3000 to the number of sample you want) run this:
49: 
50: ```bash
51: conda deactivate
52: conda activate sglang47
53: 
54: # Sample phase (default fewshot example from train_solution.pkl but should be from train archive of previous generation)
55: 
56: python $soar_path/soar/inference/sample_phase.py \
57: --base_path $soar_path/ \
58: --path_model $path_model/ \
59: --path_save_res $soar_path/save_results/$model_id_base/gen-$gen/solution \
60: -k 3000 \
61: --model_len 30000 \
62: --gpu_mem 0.89 \
63: --n_gpu 1 \
64: --split $split \
65: --bs_inference 1 \
66: --seed 0 \
67: --max_tokens 4096 \
68: --path_fewshot $soar_path/soar/inference/train_solutions.pkl
69: ```
70: 
71: **Note:**
72: 
73: - **GPU Configuration**: You can increase the `n_gpu` parameter to match the number of GPUs available. This will speed up the process and allow you to use larger language models (LLMs) that require multiple GPUs. For example:
74:     - I Used 1 GPU for models up to 7B parameters.
75:     - I Used 2 GPUs for models up to 14B parameters.
76:     - I Used 4 GPUs for models larger than 14B parameters.
77: 
78: - **Resuming After Interruption**: Each phase automatically saves its progress. If interrupted (e.g., due to a crash or manual cancellation), you can safely rerun the phase. It will reload the last saved results and continue from where it left off.
79: 
80: Then do post-processing on those data (rm code with error, ...)
81: 
82: ```bash
83: # Process results sample phase
84: python $soar_path/soar/post_process/merge_filter.py \
85: --base_path $soar_path/ \
86: --path_folder $soar_path/save_results/$model_id_base/gen-$gen/ \
87: --max_data 3000 \
88: --split $split 
89: ```
90: 
91: 
92: ### Refine initial solution
93: 
94: Now we need to refine those initial solutions. To save time, you can run the 5 seeds on separate nodes (each seed generates 600 refined codes). This approach allows parallel processing and speeds up the refinement phase.
95: 
96: So execute this:
97: ```bash
98: for seed in {0..4}; do
99:     python $soar_path/soar/repair/rex_inference.py \
100:     --base_path $soar_path/ \
101:     --path_model $path_model/ \
102:     --path_archive $soar_path/save_results/$model_id_base/gen-$gen/solution/train_sol.pkl \
103:     --model_len 30000 \
104:     --total_budget 600 \
105:     --seed $seed \
106:     --split $split \
107:     --max_tokens 2048 \
108:     --n_completion 1 \
109:     --gpu_mem 0.87 \
110:     --n_gpu 1
111: done
112: ```
113: 
114: Or to get the results faster if you have multiple nodes, this:
115: 
116: ```bash
117: # run this for five different seed (0,1,2,3,4) in pa
118: export seed=0
119: 
120: python $soar_path/soar/repair/rex_inference.py \
121: --base_path $soar_path/ \
122: --path_model $path_model/ \
123: --path_archive $soar_path/save_results/$model_id_base/gen-$gen/solution/train_sol.pkl \
124: --model_len 30000 \
125: --total_budget 600 \
126: --seed $seed \
127: --split $split \
128: --max_tokens 2048 \
129: --n_completion 1 \
130: --gpu_mem 0.87 \
131: --n_gpu 1
132: ```
133: 
134: 
135: Then do post-processing on those data (rm code with error, ...)
136: 
137: ```bash
138: python $soar_path/soar/post_process/merge_filter.py \
139: --base_path $soar_path/ \
140: --path_folder $soar_path/save_results/$model_id_base/gen-$gen/ \
141: --max_data 3000 \
142: --split $split \
143: --use_repair_data
144: ```
145: 
146: Optional merge results and deduplication based on code in one results (for merging responses from all models in the paper)
147: ```bash
148: python $soar_path/soar/post_process/dedup.py \
149: --base_path $soar_path/ \
150: --path_embed_model $path_embed_model/ \
151: --path_save $soar_path/save_results/$model_id_base/gen-$gen/solution/train_sol_repair.pkl \
152: --path_0 $soar_path/save_results/$model_id_base/gen-$gen/solution/train_sol_repair.pkl 
153: ```
154: 
155: ### Sample data for training model (gen + 1)-th generation
156: 
157: For sampling data to improving model on sampling intial solution:
158: 
159: ```bash
160: # sample training data for sample training (use train_sol_repair.pkl in path_folder_solution as data)
161: python $soar_path/soar/post_process/process_sample_for_training.py \
162: --path_model $path_model/ \
163: --path_embed_model $path_embed_model/ \
164: --split $split \
165: --N_sample_task $n_sample_task \
166: --path_folder_solution $soar_path/save_results/$model_id_base/gen-$gen/solution/ \
167: --sampling_her greedy_div 
168: ```
169: 
170: 
171: For sampling data to improving model on refinement
172: 
173: ```bash
174: # sample training data for sample refinement (use every data starting with $split in path_folder_refinement as data)
175: python $soar_path/soar/post_process/process_repair_for_training.py \
176: --base_path $soar_path/ \
177: --path_embed_model $path_embed_model/ \
178: --split $split \
179: --N_sample_task $n_sample_task \
180: --path_folder_refinement $soar_path/save_results/$model_id_base/gen-$gen/refinement/
181: ```
182: 
183: 
184: ### Training the model
185: 
186: 
187: ```bash
188: # Train model (add --load_in_4bit for Q-LoRA)
189: 
190: conda deactivate
191: conda activate unsloth_env
192: python $soar_path/soar/training/train_unsloth.py \
193: --base_path $soar_path/ \
194: --model_len 30000 \
195: --path_base_model $path_base_model \
196: --path_archive $soar_path/save_results/$model_id_base/gen-$gen/solution/data4train-train-n$n_sample_task-greedy_div.pkl \
197: --path_archive_repair $soar_path/save_results/$model_id_base/gen-$gen/refinement/data4train-train-n$n_sample_task.pkl \
198: --path_save_model $work_path/hf/trained/$model_id_base/gen-$((gen + 1))/$model_id
199: ```
200: 
201: ## LOOP
202: 