Load a quantized checkpoint and pack for the target backend.
(
prequantized_dir: str,
max_seq_len: int = 4096,
backend: str = "cuda",
)
| 41 | |
| 42 | |
| 43 | def load_prequantized_model( |
| 44 | prequantized_dir: str, |
| 45 | max_seq_len: int = 4096, |
| 46 | backend: str = "cuda", |
| 47 | ) -> tuple[Gemma4_31B, Gemma4_31BConfig]: |
| 48 | """Load a quantized checkpoint and pack for the target backend.""" |
| 49 | config = Gemma4_31BConfig.from_hf_config( |
| 50 | os.path.join(prequantized_dir, "config.json") |
| 51 | ) |
| 52 | config.max_seq_len = max_seq_len |
| 53 | |
| 54 | print("Building model on meta device...") |
| 55 | with torch.device("meta"): |
| 56 | model = Gemma4_31B(config) |
| 57 | |
| 58 | safetensors_path = os.path.join(prequantized_dir, "model.safetensors") |
| 59 | print(f"Loading quantized checkpoint from {safetensors_path}...") |
| 60 | _pack_for_backend(model, safetensors_path, backend) |
| 61 | model.eval() |
| 62 | |
| 63 | print(f"Model: {config.num_hidden_layers} layers, hidden={config.hidden_size}") |
| 64 | return model, config |
| 65 | |
| 66 | |
| 67 | def load_and_quantize( |