Hub loading and artifact safety
Separate identity, integrity and authorization
A model ID names a repository; a branch like main can change. Pin an immutable reviewed commit and record a trusted manifest of SHA-256 digests for every required file: config, weight shards, indexes, tokenizer and relevant templates. A digest identifies bytes, not their safety or license. Obtain the expected digest through a separately trusted approval process; hashing the same untrusted download and accepting its own hash proves nothing about provenance. Here abc is a three-byte teaching fixture, not a model. Its published test digest matches; changing one byte causes rejection. Only lowercase hexadecimal digests are permitted by this local contract. Production verification should stream bounded files and load from the same verified immutable snapshot to avoid time-of-check/time-of-use problems.
import hashlibimport hmac
def verify_bytes(payload, expected): # Expected digest comes from a separately trusted manifest. if not isinstance(payload, bytes) or not isinstance(expected, str): return False if len(expected) != 64 or any(c not in "0123456789abcdef" for c in expected): return False actual = hashlib.sha256(payload).hexdigest() return hmac.compare_digest(actual, expected)
approved = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"print(verify_bytes(b"abc", approved))print(verify_bytes(b"abd", approved))print(verify_bytes(b"abc", "unknown"))Understand the loading interfaces
The Transformers library offers AutoTokenizer and task-appropriate model classes such as AutoModelForCausalLM; a pipeline wraps preprocessing, model execution and postprocessing for a task. Review the exact library version, architecture, model configuration and tokenizer compatibility. The documented from_pretrained pattern accepts a repository identifier or a local directory, and revision selects a repository revision. After a separately authorized acquisition and review, use an immutable verified local snapshot for both tokenizer and model, set local_files_only=True and trust_remote_code=False, and set use_safetensors=True for the supported model loader. These are documented options, not executed calls here; do not assume every class or version accepts identical parameters. A conceptual local flow is AutoTokenizer.from_pretrained(snapshot, local_files_only=True, trust_remote_code=False) followed by AutoModelForCausalLM.from_pretrained(snapshot, local_files_only=True, trust_remote_code=False, use_safetensors=True). Pin and audit dependencies; do not turn off safeguards to resolve a missing artifact.
Reject unreviewed loading plans
Safetensors stores tensors without general pickle object deserialization. It reduces one risk, but it does not prove that the weights, config, dependencies or parser are trustworthy. Avoid loading unreviewed pickle checkpoints or executing model scripts. In this example the gate applies a strict single-repository policy: model and tokenizer must share the exact approved 40-character lowercase commit identifier, format must be safetensors, custom remote code must be disabled and every required digest must be verified. Some legitimate projects use a separately versioned tokenizer; those require a different approved compatibility policy. The fixture uses invented identifiers, never retrieves files and cannot load a model. The first call passes the gate; revision drift, unknown format and remote code fail.
def loader_gate(model_revision, tokenizer_revision, approved_revision, file_format, remote_code, digests_ok): # A fictional single-repository policy, not a loader. return (isinstance(approved_revision, str) and len(approved_revision) == 40 and all(c in "0123456789abcdef" for c in approved_revision) and model_revision == tokenizer_revision == approved_revision and file_format == "safetensors" and remote_code is False and digests_ok is True)
revision = "a" * 40 # fictional identifier, not a real commitprint(loader_gate(revision, revision, revision, "safetensors", False, True))print(loader_gate(revision, "b" * 40, revision, "safetensors", False, True))print(loader_gate(revision, revision, revision, "pickle", False, True))print(loader_gate(revision, revision, revision, "safetensors", True, True))Local runtimes and isolated evaluation
llama.cpp uses GGUF for supported architectures. Its quantization tool converts suitable weights into lower-precision GGUF variants. Ollama documents importing a supported local GGUF using a Modelfile with FROM pointing at the file, then creating a local model. It also supports some Safetensors architectures; check current supported models and exact installed versions. A GGUF import is not itself a quantization step. Do not treat GGUF, Safetensors, or a filename extension as a security certificate. The strict example gate intentionally rejects GGUF; a separate reviewed runtime policy would be needed. No runtime is installed or invoked here. If authorized later, use an isolated low-privilege process, resource and time caps, no unexpected network access, reviewed data, output validation and a rollback plan. Avoid exposing a local inference endpoint publicly. Local operation does not guarantee privacy if logs or integrations leak data.
Practice: reject a changed snapshot
Use the project fixtures to combine reviewed metadata, a memory budget, the trusted digest, and the loader policy. Test an altered payload, a mismatched tokenizer revision, a mutable branch name, and a remote-code request. Explain why all files require integrity checks, why a successful hash cannot grant a license, and why passing the gate is not proof of actual model performance or security. Compare the current documentation in source-evidence.json. Next chapter: embedding model identity and similarity contracts.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Hub loading and artifact safety quiz
Apply model selection and artifact review contracts.
Why pin a reviewed commit instead of main?
What does a verified SHA-256 digest establish?
What is the policy for custom remote modeling code here?
Which format is designed for tensor storage without general pickle deserialization?
Does a GGUF import in Ollama automatically quantize a model?