File size: 2,121 Bytes
76b7b25 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | #!/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()
|