Download drop_net_outputs.py from Tinghong-Ye22/script: direct link, hf CLI and curl.
- Browser
- Download file 2.45 kB
-
https://huggingface.co/Tinghong-Ye22/script/resolve/main/drop_net_outputs.py
- Command line
-
hf download hf://Tinghong-Ye22/script/drop_net_outputs.py
-
curl -L -o drop_net_outputs.py https://huggingface.co/Tinghong-Ye22/script/resolve/main/drop_net_outputs.py
2.45 kB
| #!/usr/bin/env python3 | |
| from concurrent.futures import FIRST_COMPLETED, ThreadPoolExecutor, wait | |
| from pathlib import Path | |
| import argparse | |
| import os | |
| import torch | |
| from tqdm import tqdm | |
| def load_pt(path: Path): | |
| try: | |
| return torch.load(path, map_location="cpu", weights_only=False) | |
| except TypeError: | |
| return torch.load(path, map_location="cpu") | |
| def iter_pt_files(root: Path): | |
| for p in root.rglob("*.pt"): | |
| if p.is_file(): | |
| yield p | |
| def process_one(path: Path, key: str): | |
| try: | |
| obj = load_pt(path) | |
| if isinstance(obj, dict) and key in obj: | |
| obj.pop(key, None) | |
| torch.save(obj, path) | |
| return 1, 0 | |
| return 0, 0 | |
| except Exception: | |
| return 0, 1 | |
| def main(): | |
| parser = argparse.ArgumentParser(description="Drop a key from all .pt files recursively.") | |
| parser.add_argument("root", nargs="?", default="/cpfs01/videogen/gvhmr_smpl") | |
| parser.add_argument("--key", default="net_outputs") | |
| parser.add_argument("--workers", type=int, default=min(16, os.cpu_count() or 1)) | |
| parser.add_argument("--prefetch", type=int, default=4) | |
| args = parser.parse_args() | |
| root = Path(args.root) | |
| files = iter_pt_files(root) | |
| workers = max(1, args.workers) | |
| inflight_cap = max(1, workers * max(1, args.prefetch)) | |
| scanned = 0 | |
| removed = 0 | |
| errors = 0 | |
| print(f"start: root={root}, workers={workers}", flush=True) | |
| with ThreadPoolExecutor(max_workers=workers) as pool, tqdm( | |
| desc="processing", unit="file", dynamic_ncols=True | |
| ) as pbar: | |
| inflight = set() | |
| for _ in range(inflight_cap): | |
| try: | |
| f = next(files) | |
| except StopIteration: | |
| break | |
| inflight.add(pool.submit(process_one, f, args.key)) | |
| while inflight: | |
| done, inflight = wait(inflight, return_when=FIRST_COMPLETED) | |
| for fut in done: | |
| r, e = fut.result() | |
| scanned += 1 | |
| removed += r | |
| errors += e | |
| pbar.update(len(done)) | |
| for _ in range(len(done)): | |
| try: | |
| f = next(files) | |
| except StopIteration: | |
| break | |
| inflight.add(pool.submit(process_one, f, args.key)) | |
| print(f"done: {scanned} files scanned, {removed} files updated, {errors} errors") | |
| if __name__ == "__main__": | |
| main() | |