Download colm_loader.py from edeneldith/COLM: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/edeneldith/COLM/resolve/main/colm_loader.py
- Command line
-
hf download hf://edeneldith/COLM/colm_loader.py
-
curl -L -o colm_loader.py https://huggingface.co/edeneldith/COLM/resolve/main/colm_loader.py
3.7 kB
| """Load the released native COLM weights, including its fixed complex mixers.""" | |
| import json | |
| import math | |
| from pathlib import Path | |
| from types import SimpleNamespace | |
| import numpy as np | |
| import torch | |
| from safetensors import safe_open | |
| from safetensors.torch import load_file | |
| from model import COLM | |
| def checkpoint_state(checkpoint_path): | |
| """Read the original .pt on CPU and remove only torch.compile's prefix.""" | |
| checkpoint = torch.load(checkpoint_path, map_location="cpu", weights_only=True) | |
| state = { | |
| name.removeprefix("_orig_mod."): tensor | |
| for name, tensor in checkpoint["model_state_dict"].items() | |
| } | |
| if len(state) != len(checkpoint["model_state_dict"]): | |
| raise ValueError("Duplicate state names after removing the compile prefix") | |
| return state | |
| def safetensors_state(directory): | |
| """Both files are required: learned parameters plus saved fixed mixers.""" | |
| directory = Path(directory) | |
| state = load_file(str(directory / "model.safetensors"), device="cpu") | |
| with safe_open(str(directory / "model.safetensors"), framework="pt", device="cpu") as archive: | |
| metadata = archive.metadata() or {} | |
| # The Hub counter omits shape=[] tensors. Store these as shape=[1], then | |
| # restore the original scalar shape before loading the unchanged model. | |
| for name in json.loads(metadata.get("scalar_parameter_names", "[]")): | |
| if state[name].shape != (1,): | |
| raise ValueError(f"Expected a stored length-one scalar: {name}") | |
| state[name] = state[name].reshape(()) | |
| with np.load(directory / "model_buffers.npz", allow_pickle=False) as archive: | |
| for name in archive.files: | |
| if name in state: | |
| raise ValueError(f"Duplicate parameter/buffer name: {name}") | |
| state[name] = torch.from_numpy(archive[name].copy()) | |
| return state | |
| def build_model(directory): | |
| """Construct the unchanged native architecture on CPU, without inference.""" | |
| directory = Path(directory) | |
| config = json.loads((directory / "colm_config.json").read_text(encoding="utf-8")) | |
| arch = config["architecture"] | |
| n_layer = arch["n_layer"] | |
| alpha = ([math.e / 10, math.pi / 2] if n_layer == 2 else | |
| [(i + math.pi) / n_layer for i in range(n_layer)]) | |
| cfg = SimpleNamespace( | |
| **arch, | |
| scanner_clamp=config["scanner"]["scanner_clamp"], | |
| coupling_alpha_init=alpha, | |
| ) | |
| # Explicit CPU allocation also covers callers with a different default device. | |
| # Every random initial parameter AND mixer is subsequently restored strictly. | |
| with torch.device("cpu"): | |
| return COLM(cfg) | |
| def load_colm(directory=".", *, weights_format="safetensors", device="cpu"): | |
| """Strictly restore COLM, then optionally move it to the caller's device. | |
| weights_format='pt' loads the original colm_best_Final.pt instead. | |
| No generation, compilation, CUDA probing or precision conversion is performed. | |
| """ | |
| directory = Path(directory) | |
| if weights_format == "safetensors": | |
| state = safetensors_state(directory) | |
| elif weights_format == "pt": | |
| state = checkpoint_state(directory / "colm_best_Final.pt") | |
| else: | |
| raise ValueError("weights_format must be 'safetensors' or 'pt'") | |
| model = build_model(directory) | |
| # load_state_dict can silently cast; reject a dtype/shape mismatch first. | |
| expected = model.state_dict() | |
| for name in expected.keys() & state.keys(): | |
| if expected[name].dtype != state[name].dtype or expected[name].shape != state[name].shape: | |
| raise ValueError(f"State dtype/shape mismatch: {name}") | |
| model.load_state_dict(state, strict=True) | |
| return model.eval().to(device=device) | |