Upload folder using huggingface_hub
Browse files- fix.py +131 -0
- quant_strategy.json +0 -0
fix.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
修复 quant_strategy.json 中的列表结构问题
|
| 4 |
+
将 [{...}] 格式转换为 {...} 格式
|
| 5 |
+
|
| 6 |
+
用法:
|
| 7 |
+
python fix_quant_strategy.py quant_strategy.json
|
| 8 |
+
python fix_quant_strategy.py quant_strategy.json -o fixed_strategy.json
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
import json
|
| 12 |
+
import argparse
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def fix_strategy_structure(input_file, output_file=None):
|
| 17 |
+
"""
|
| 18 |
+
修复 strategy 文件中的列表结构
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
input_file: 输入的 quant_strategy.json 文件路径
|
| 22 |
+
output_file: 输出文件路径(如果为 None,则覆盖原文件)
|
| 23 |
+
"""
|
| 24 |
+
input_path = Path(input_file)
|
| 25 |
+
|
| 26 |
+
if not input_path.exists():
|
| 27 |
+
print(f"❌ 错误: 文件不存在 {input_path}")
|
| 28 |
+
return False
|
| 29 |
+
|
| 30 |
+
print(f"📖 读取文件: {input_path}")
|
| 31 |
+
|
| 32 |
+
# 读取原始 JSON
|
| 33 |
+
try:
|
| 34 |
+
with open(input_path, 'r', encoding='utf-8') as f:
|
| 35 |
+
data = json.load(f)
|
| 36 |
+
except json.JSONDecodeError as e:
|
| 37 |
+
print(f"❌ JSON 解析错误: {e}")
|
| 38 |
+
return False
|
| 39 |
+
|
| 40 |
+
if 'measurement' not in data:
|
| 41 |
+
print("❌ 错误: JSON 中没有 'measurement' 字段")
|
| 42 |
+
return False
|
| 43 |
+
|
| 44 |
+
measurement = data['measurement']
|
| 45 |
+
new_measurement = {}
|
| 46 |
+
fixed_count = 0
|
| 47 |
+
kept_count = 0
|
| 48 |
+
|
| 49 |
+
# 遍历所有层并修复结构
|
| 50 |
+
for key, value in measurement.items():
|
| 51 |
+
if isinstance(value, list):
|
| 52 |
+
if len(value) > 0:
|
| 53 |
+
# 从列表中取出字典
|
| 54 |
+
new_measurement[key] = value[0]
|
| 55 |
+
fixed_count += 1
|
| 56 |
+
print(f" ✅ 修复: {key} (list -> dict)")
|
| 57 |
+
else:
|
| 58 |
+
print(f" ⚠️ 警告: {key} 是空列表,跳过")
|
| 59 |
+
new_measurement[key] = value
|
| 60 |
+
elif isinstance(value, dict):
|
| 61 |
+
# 已经是字典,保持不变
|
| 62 |
+
new_measurement[key] = value
|
| 63 |
+
kept_count += 1
|
| 64 |
+
else:
|
| 65 |
+
print(f" ⚠️ 警告: {key} 的值类型未知 ({type(value)})")
|
| 66 |
+
new_measurement[key] = value
|
| 67 |
+
|
| 68 |
+
# 更新数据
|
| 69 |
+
data['measurement'] = new_measurement
|
| 70 |
+
|
| 71 |
+
# 确定输出文件路径
|
| 72 |
+
if output_file is None:
|
| 73 |
+
output_path = input_path
|
| 74 |
+
print(f"\n💾 覆盖原文件: {output_path}")
|
| 75 |
+
else:
|
| 76 |
+
output_path = Path(output_file)
|
| 77 |
+
print(f"\n💾 保存到: {output_path}")
|
| 78 |
+
|
| 79 |
+
# 写入修复后的 JSON
|
| 80 |
+
try:
|
| 81 |
+
with open(output_path, 'w', encoding='utf-8') as f:
|
| 82 |
+
json.dump(data, f, indent=2, ensure_ascii=False)
|
| 83 |
+
except Exception as e:
|
| 84 |
+
print(f"❌ 写入文件失败: {e}")
|
| 85 |
+
return False
|
| 86 |
+
|
| 87 |
+
# 打印统计信息
|
| 88 |
+
print(f"\n📊 统计:")
|
| 89 |
+
print(f" - 修复的层数: {fixed_count}")
|
| 90 |
+
print(f" - 保持不变: {kept_count}")
|
| 91 |
+
print(f" - 总层数: {len(new_measurement)}")
|
| 92 |
+
print(f"\n✅ 完成!")
|
| 93 |
+
|
| 94 |
+
return True
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def main():
|
| 98 |
+
parser = argparse.ArgumentParser(
|
| 99 |
+
description='修复 quant_strategy.json 中的列表结构问题',
|
| 100 |
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
| 101 |
+
epilog="""
|
| 102 |
+
示例:
|
| 103 |
+
# 直接覆盖原文件
|
| 104 |
+
python fix_quant_strategy.py quant_strategy.json
|
| 105 |
+
|
| 106 |
+
# 保存到新文件
|
| 107 |
+
python fix_quant_strategy.py quant_strategy.json -o fixed_strategy.json
|
| 108 |
+
"""
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
parser.add_argument(
|
| 112 |
+
'input_file',
|
| 113 |
+
help='输入的 quant_strategy.json 文件路径'
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
parser.add_argument(
|
| 117 |
+
'-o', '--output',
|
| 118 |
+
dest='output_file',
|
| 119 |
+
default=None,
|
| 120 |
+
help='输出文件路径(默认覆盖原文件)'
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
args = parser.parse_args()
|
| 124 |
+
|
| 125 |
+
success = fix_strategy_structure(args.input_file, args.output_file)
|
| 126 |
+
|
| 127 |
+
return 0 if success else 1
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
if __name__ == '__main__':
|
| 131 |
+
exit(main())
|
quant_strategy.json
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|