|
import json |
|
import os |
|
import shutil |
|
import mmap |
|
import re |
|
|
|
def rename_tensors(): |
|
|
|
with open('model.safetensors.index.json', 'r') as f: |
|
data = json.load(f) |
|
|
|
|
|
safetensor_files = set(data['weight_map'].values()) |
|
|
|
|
|
for file in safetensor_files: |
|
new_file = file.replace('model-', 'model-renamed-') |
|
shutil.copy(file, new_file) |
|
|
|
|
|
with open(new_file, 'r+b') as f: |
|
mm = mmap.mmap(f.fileno(), 1024*1024) |
|
content = mm.read() |
|
|
|
content = content.replace(b'"llm.', b' "') |
|
mm.seek(0) |
|
mm.write(content) |
|
mm.close() |
|
|
|
|
|
new_weight_map = {} |
|
for key, value in data['weight_map'].items(): |
|
new_key = re.sub(r'^llm\.', '', key) |
|
new_value = value.replace('model-', 'model-renamed-') |
|
new_weight_map[new_key] = new_value |
|
|
|
data['weight_map'] = new_weight_map |
|
|
|
|
|
with open('model-renamed.safetensors.index.json', 'w') as f: |
|
json.dump(data, f, indent=2) |
|
|
|
print("处理完成。新的JSON文件已生成:model-renamed.safetensors.index.json") |
|
|
|
if __name__ == "__main__": |
|
rename_tensors() |