Download code/scripts/export_generator.py from luyu1021/turbot2va-ltx2: direct link, hf CLI and curl.
- Browser
- Download file 2.12 kB
-
https://huggingface.co/luyu1021/turbot2va-ltx2/resolve/main/code/scripts/export_generator.py
- Command line
-
hf download hf://luyu1021/turbot2va-ltx2/code/scripts/export_generator.py
-
curl -L -o export_generator.py https://huggingface.co/luyu1021/turbot2va-ltx2/resolve/main/code/scripts/export_generator.py
2.12 kB
| #!/usr/bin/env python3 | |
| """Export only the generator from DCP for the existing single-GPU .pth runner. | |
| Run with the pinned PyTorch 2.7 environment. This uses CPU RAM and no GPU; | |
| roughly 38 GB are allocated for a BF16 generator. Optimizers and critic are | |
| excluded; the result is for inference or weights-only initialization. | |
| """ | |
| from pathlib import Path | |
| import argparse | |
| import torch | |
| import torch.distributed.checkpoint as dcp | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('checkpoint', type=Path) | |
| parser.add_argument('output', type=Path) | |
| args = parser.parse_args() | |
| if args.output.exists(): | |
| parser.error('Output already exists.') | |
| reader = dcp.FileSystemReader(str(args.checkpoint / 'sharded')) | |
| metadata = reader.read_metadata() | |
| # DCP planner paths preserve the nested "generator" dictionary and the | |
| # model.* state dict keys. Do not strip "model." from the wrapper state. | |
| generator = {} | |
| for key, storage in metadata.state_dict_metadata.items(): | |
| if key.startswith('generator.') and hasattr(storage, 'size'): | |
| path = metadata.planner_data[key] | |
| generator[path[1]] = torch.empty(storage.size, dtype=storage.properties.dtype, device='cpu') | |
| if not generator: | |
| raise RuntimeError('No generator tensors found in DCP metadata.') | |
| state = {'generator': generator} | |
| dcp.load(state, storage_reader=reader) | |
| source = torch.load(args.checkpoint/'metadata.pth', map_location='cpu', weights_only=False) | |
| payload = {'generator': generator, 'completed_step': source.get('completed_step'), | |
| 'next_step': source.get('next_step'), 'source_checkpoint': str(args.checkpoint.resolve()), | |
| 'has_generator_optimizer': False, 'has_critic_optimizer': False} | |
| args.output.parent.mkdir(parents=True, exist_ok=True) | |
| tmp = args.output.with_name(args.output.name + '.partial') | |
| torch.save(payload, tmp) | |
| tmp.replace(args.output) | |
| print(f'Exported {len(generator)} tensors, {args.output.stat().st_size} bytes: {args.output}') | |
| if __name__ == '__main__': | |
| main() | |