Elffuss BraidHub

kikocis/auto-code-rover public

main · 7.4 KiBRawPermalink
1import json
2import os
3from os.path import join as pjoin
4
5from check_vanilla import get_resolved_tasks
6
7
8def get_intersec(list_one, list_two):
9 return list(set(list_one) & set(list_two))
10
11
12def get_tasks_with_traj(result_dir):
13 traj_files = [x for x in os.listdir(pjoin(result_dir)) if x.endswith(".traj")]
14 tasks_with_traj = [x.split(".")[0] for x in traj_files]
15 return tasks_with_traj
16
17
18def get_one_instance_cost(traj_file):
19 with open(traj_file) as f:
20 traj = json.load(f)
21 cost = float(traj["info"]["model_stats"]["instance_cost"])
22 in_token = int(traj["info"]["model_stats"]["tokens_sent"])
23 out_token = int(traj["info"]["model_stats"]["tokens_received"])
24 return cost, in_token + out_token
25
26
27def compute_avg_cost(result_dir, tasks):
28 """
29 result_dir should have .traj for all tasks in `tasks`.
30 """
31 traj_files = [f"{t}.traj" for t in tasks]
32 traj_files = [pjoin(result_dir, t) for t in traj_files]
33 total_cost = 0.0
34 total_tokens = 0
35 for f in traj_files:
36 cost, tokens = get_one_instance_cost(f)
37 total_cost += cost
38 total_tokens += tokens
39 avg_cost = total_cost / len(tasks)
40 avg_tokens = total_tokens / len(tasks)
41 return avg_cost, avg_tokens
42
43
44def main():
45 script_dir = os.path.dirname(os.path.realpath(__file__))
46 root_dir = os.path.dirname(script_dir)
47 result_dir = pjoin(root_dir, "results")
48
49 swe_agent_dir = pjoin(result_dir, "swe-agent-results")
50
51 # STEP (1) get lists of tasks with .traj files and find the overlap of them
52 # This is the final list of tasks in Figure 6
53
54 run_one = "cost_2_1"
55 run_two = "cost_2_2"
56 run_three = "cost_2_3"
57 run_one_dir = pjoin(swe_agent_dir, run_one)
58 run_two_dir = pjoin(swe_agent_dir, run_two)
59 run_three_dir = pjoin(swe_agent_dir, run_three)
60
61 run_one_tasks = get_tasks_with_traj(run_one_dir)
62 run_two_tasks = get_tasks_with_traj(run_two_dir)
63 run_three_tasks = get_tasks_with_traj(run_three_dir)
64
65 tasks_fig_6 = set(run_one_tasks) & set(run_two_tasks) & set(run_three_tasks)
66 tasks_fig_6 = list(tasks_fig_6)
67 print(f"All considered tasks: {len(tasks_fig_6)}")
68
69 print(
70 "\n================================= SWE-agent results over 274 instances that could be run by it: ================================="
71 )
72
73 # STEP (2) get resolved tasks for each run.
74 run_one_resolved = get_resolved_tasks(run_one_dir)
75 run_two_resolved = get_resolved_tasks(run_two_dir)
76 run_three_resolved = get_resolved_tasks(run_three_dir)
77 # Since only 274 overlapped are considered, do a filtering
78 run_one_resolved = [x for x in run_one_resolved if x in tasks_fig_6]
79 run_two_resolved = [x for x in run_two_resolved if x in tasks_fig_6]
80 run_three_resolved = [x for x in run_three_resolved if x in tasks_fig_6]
81
82 run_one_ratio = len(run_one_resolved) / len(tasks_fig_6)
83 run_two_ratio = len(run_two_resolved) / len(tasks_fig_6)
84 run_three_ratio = len(run_three_resolved) / len(tasks_fig_6)
85
86 print(f"{run_one} - Resolved: {run_one_ratio*100:.2f}% ({len(run_one_resolved)})")
87 print(f"{run_two} - Resolved: {run_two_ratio*100:.2f}% ({len(run_two_resolved)})")
88 print(
89 f"{run_three} - Resolved: {run_three_ratio*100:.2f}% ({len(run_three_resolved)})"
90 )
91
92 # STEP (3) compute average
93 average_num_resolved = (
94 len(run_one_resolved) + len(run_two_resolved) + len(run_three_resolved)
95 ) / 3
96 average_ratio = average_num_resolved / len(tasks_fig_6)
97 print(
98 f"Average resolved tasks: {average_ratio*100:.2f}% ({average_num_resolved:.1f})"
99 )
100
101 # STEP (4) get union of the resolved tasks
102 resolved_union = (
103 set(run_one_resolved) | set(run_two_resolved) | set(run_three_resolved)
104 )
105 resolved_union = list(resolved_union)
106 union_ratio = len(resolved_union) / len(tasks_fig_6)
107 print(f"Union of resolved tasks: {union_ratio*100:.2f}% ({len(resolved_union)})")
108
109 print(
110 "\n================================= ACR results over these 274 instances: ================================="
111 )
112
113 # STEP (5) Get resolved from ACR, and filter based on 274 tasks
114 # Following is basically repetition of what we computed above
115 acr_run_one_dir = pjoin(result_dir, "acr-run-1")
116 acr_run_two_dir = pjoin(result_dir, "acr-run-2")
117 acr_run_three_dir = pjoin(result_dir, "acr-run-3")
118 acr_run_one_resolved = get_resolved_tasks(acr_run_one_dir)
119 acr_run_two_resolved = get_resolved_tasks(acr_run_two_dir)
120 acr_run_three_resolved = get_resolved_tasks(acr_run_three_dir)
121 acr_run_one_resolved = [x for x in acr_run_one_resolved if x in tasks_fig_6]
122 acr_run_two_resolved = [x for x in acr_run_two_resolved if x in tasks_fig_6]
123 acr_run_three_resolved = [x for x in acr_run_three_resolved if x in tasks_fig_6]
124
125 acr_run_one_ratio = len(acr_run_one_resolved) / len(tasks_fig_6)
126 acr_run_two_ratio = len(acr_run_two_resolved) / len(tasks_fig_6)
127 acr_run_three_ratio = len(acr_run_three_resolved) / len(tasks_fig_6)
128
129 print(
130 f"ACR-1 - Resolved: {acr_run_one_ratio*100:.2f}% ({len(acr_run_one_resolved)})"
131 )
132 print(
133 f"ACR-2 - Resolved: {acr_run_two_ratio*100:.2f}% ({len(acr_run_two_resolved)})"
134 )
135 print(
136 f"ACR-3 - Resolved: {acr_run_three_ratio*100:.2f}% ({len(acr_run_three_resolved)})"
137 )
138
139 # STEP (6) compute average
140 acr_average_num_resolved = (
141 len(acr_run_one_resolved)
142 + len(acr_run_two_resolved)
143 + len(acr_run_three_resolved)
144 ) / 3
145 acr_average_ratio = acr_average_num_resolved / len(tasks_fig_6)
146 print(
147 f"ACR-Average resolved tasks: {acr_average_ratio*100:.2f}% ({acr_average_num_resolved:.1f})"
148 )
149
150 # STEP (7) get union of the resolved tasks
151 acr_resolved_union = (
152 set(acr_run_one_resolved)
153 | set(acr_run_two_resolved)
154 | set(acr_run_three_resolved)
155 )
156 acr_resolved_union = list(acr_resolved_union)
157 acr_union_ratio = len(acr_resolved_union) / len(tasks_fig_6)
158 print(
159 f"ACR-Union of resolved tasks: {acr_union_ratio*100:.2f}% ({len(acr_resolved_union)})"
160 )
161
162 print(
163 "\n================================= Venn for SWE-Agent-all and ACR-all ================================="
164 )
165 swe_agent_extra = set(resolved_union) - set(acr_resolved_union)
166 acr_extra = set(acr_resolved_union) - set(resolved_union)
167 insersec = get_intersec(resolved_union, acr_resolved_union)
168 print(f"SWE-Agent extra: {len(swe_agent_extra)}")
169 print(f"ACR extra: {len(acr_extra)}")
170 print(f"Intersection: {len(insersec)}")
171
172 print(
173 "\n================================= Compute cost and tokesn for SWE-agent ================================="
174 )
175 run_one_cost, run_one_tokens = compute_avg_cost(run_one_dir, tasks_fig_6)
176 run_two_cost, run_two_tokens = compute_avg_cost(run_two_dir, tasks_fig_6)
177 run_three_cost, run_three_tokens = compute_avg_cost(run_three_dir, tasks_fig_6)
178 print(f"Run-1: Cost: {run_one_cost:.6f}, Tokens: {run_one_tokens:.2f}")
179 print(f"Run-2: Cost: {run_two_cost:.6f}, Tokens: {run_two_tokens:.2f}")
180 print(f"Run-3: Cost: {run_three_cost:.6f}, Tokens: {run_three_tokens:.2f}")
181 avg_of_avg_cost = (run_one_cost + run_two_cost + run_three_cost) / 3
182 avg_of_avg_tokens = (run_one_tokens + run_two_tokens + run_three_tokens) / 3
183 print(f"Average of average cost: {avg_of_avg_cost:.6f}")
184 print(f"Average of average tokens: {avg_of_avg_tokens:.2f}")
185
186
187if __name__ == "__main__":
188
189 main()