Download load_model.py from vanshnawander/assignment2-moe-shared: direct link, hf CLI and curl.
- Browser
- Download file 640 Bytes
-
https://huggingface.co/vanshnawander/assignment2-moe-shared/resolve/main/load_model.py
- Command line
-
hf download hf://vanshnawander/assignment2-moe-shared/load_model.py
-
curl -L -o load_model.py https://huggingface.co/vanshnawander/assignment2-moe-shared/resolve/main/load_model.py
640 Bytes
| """Load this repository's custom PyTorch causal language model.""" | |
| import json | |
| from pathlib import Path | |
| import torch | |
| from model import CausalLM | |
| from moe import TransformerConfig | |
| def load_model(directory, device="cpu"): | |
| """Load verified tensor weights and return a model in evaluation mode.""" | |
| directory = Path(directory) | |
| config = TransformerConfig(**json.loads((directory / "config.json").read_text())) | |
| model = CausalLM(config) | |
| weights = torch.load( | |
| directory / "model_state.pt", map_location="cpu", weights_only=True | |
| ) | |
| model.load_state_dict(weights, strict=True) | |
| return model.eval().to(device) | |