MCPcopy Create free account
hub / github.com/pytorch/executorch / load_prequantized_model

Function load_prequantized_model

examples/models/gemma4_31b/export.py:43–64  ·  view source on GitHub ↗

Load a quantized checkpoint and pack for the target backend.

(
    prequantized_dir: str,
    max_seq_len: int = 4096,
    backend: str = "cuda",
)

Source from the content-addressed store, hash-verified

41
42
43def 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
67def load_and_quantize(

Callers 8

mainFunction · 0.90
test_generateMethod · 0.90
setUpMethod · 0.90
test_deterministicMethod · 0.90
test_export_to_pteMethod · 0.90
mainFunction · 0.70

Calls 4

Gemma4_31BClass · 0.90
_pack_for_backendFunction · 0.85
deviceMethod · 0.80
from_hf_configMethod · 0.45

Tested by 6

test_generateMethod · 0.72
setUpMethod · 0.72
test_deterministicMethod · 0.72
test_export_to_pteMethod · 0.72