in utils/utils.py [0:0]
def process_all_keys(input_file):
with open(input_file, "r") as file:
first_line_str = file.readline().strip()
remaining_content = file.read().strip()
# 尝试修复和构建合法的 JSON 字符串
corrected_content = remaining_content.replace("}{", "},{").replace("]}{", "]},{")
# 构建 JSON 数组
json_str = f"[{corrected_content}]"
try:
data = json.loads(json_str)
processed_results = defaultdict(lambda: defaultdict(list))
for entry in data:
for key, measurements in entry.items():
for measure in measurements:
measure_key, measure_value = next(iter(measure.items()))
if "time_gpu" in measure_key:
processed_results[key]["time_gpu"].append(measure["time_gpu"])
else:
processed_results[key][measure_key] = measure_value
for key, values in processed_results.items():
if "time_gpu" in values:
gpu_times = values["time_gpu"]
min_time_gpu = min(gpu_times)
gpu_times_filtered = [t for t in gpu_times if t <= 3 * min_time_gpu]
values["time_gpu_max"] = max(gpu_times_filtered)
values["time_gpu_min"] = min_time_gpu
values["time_gpu_avg"] = sum(gpu_times_filtered) / len(
gpu_times_filtered
)
del values["time_gpu"]
with open(input_file, "w") as outfile:
outfile.write(first_line_str + "\n")
for key, values in processed_results.items():
outfile.write(f"{key}:\n")
for subkey, subvalue in values.items():
outfile.write(f" {subkey}: {subvalue}\n")
print(f"Compute-results save in:{input_file}")
except json.JSONDecodeError as e:
print(f"Failed to decode JSON: {e}")
print("Invalid JSON content:\n", corrected_content)