Download code/multi_vlm2vec_train/cot_gen/merge_shards.py from WaitXian/liqi: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/WaitXian/liqi/resolve/main/code/multi_vlm2vec_train/cot_gen/merge_shards.py
- Command line
-
hf download hf://WaitXian/liqi/code/multi_vlm2vec_train/cot_gen/merge_shards.py
-
curl -L -o merge_shards.py https://huggingface.co/WaitXian/liqi/resolve/main/code/multi_vlm2vec_train/cot_gen/merge_shards.py
3.25 kB
| """ | |
| Merge shard outputs from distributed CoT generation into a single JSON file. | |
| Usage: | |
| # 合并指定前缀的分片(如 bianji_train_cot) | |
| python cot_gen/merge_shards.py --prefix bianji_train_cot --input_dir /data/oss_bucket_0/liqi/dataset/embedding_pf/cot_output2/ --format json | |
| # 指定输入目录 | |
| python cot_gen/merge_shards.py --prefix dapei_train_cot --input_dir /path/to/shards | |
| # 输出为jsonl格式 | |
| python cot_gen/merge_shards.py --prefix bianji_train_cot --format jsonl | |
| """ | |
| import os | |
| import re | |
| import json | |
| import argparse | |
| from config import OUTPUT_DIR | |
| def find_shard_files(input_dir: str, prefix: str) -> list[str]: | |
| """Find all shard files matching the prefix pattern (excluding failed files).""" | |
| pattern = re.compile(rf"^{re.escape(prefix)}_\d+_\d+\.jsonl$") | |
| shard_files = [] | |
| for fname in os.listdir(input_dir): | |
| if pattern.match(fname): | |
| shard_files.append(os.path.join(input_dir, fname)) | |
| shard_files.sort(key=lambda f: int(re.search(r"_(\d+)_\d+\.jsonl$", f).group(1))) | |
| return shard_files | |
| def merge(args): | |
| input_dir = args.input_dir or OUTPUT_DIR | |
| prefix = args.prefix | |
| shard_files = find_shard_files(input_dir, prefix) | |
| if not shard_files: | |
| print(f"No shard files found with prefix '{prefix}' in {input_dir}") | |
| return | |
| print(f"Found {len(shard_files)} shard files:") | |
| for f in shard_files: | |
| print(f" {os.path.basename(f)}") | |
| all_results = [] | |
| seen_keys = set() | |
| duplicates = 0 | |
| for shard_path in shard_files: | |
| with open(shard_path, "r", encoding="utf-8") as f: | |
| for line in f: | |
| if not line.strip(): | |
| continue | |
| item = json.loads(line) | |
| key = (item["rn"], item["prompt_index"]) | |
| if key in seen_keys: | |
| duplicates += 1 | |
| continue | |
| seen_keys.add(key) | |
| all_results.append(item) | |
| if args.format == "json": | |
| merged_path = os.path.join(input_dir, f"{prefix}.json") | |
| with open(merged_path, "w", encoding="utf-8") as f: | |
| json.dump(all_results, f, ensure_ascii=False, indent=2) | |
| else: | |
| merged_path = os.path.join(input_dir, f"{prefix}.jsonl") | |
| with open(merged_path, "w", encoding="utf-8") as f: | |
| for item in all_results: | |
| f.write(json.dumps(item, ensure_ascii=False) + "\n") | |
| print(f"\nMerged {len(all_results)} records -> {merged_path}") | |
| if duplicates > 0: | |
| print(f"Skipped {duplicates} duplicate records") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Merge CoT shard outputs by prefix") | |
| parser.add_argument("--prefix", type=str, required=True, | |
| help="Shard file prefix (e.g. bianji_train_cot, dapei_train_cot)") | |
| parser.add_argument("--input_dir", type=str, default=None, | |
| help="Directory containing shard files (default: OUTPUT_DIR from config)") | |
| parser.add_argument("--format", choices=["json", "jsonl"], default="json", | |
| help="Output format: json (single array) or jsonl (one per line)") | |
| args = parser.parse_args() | |
| merge(args) | |