llm.compat — Compatibility Layer¶
HuggingFace compatibility utilities for loading and publishing models.
The compat group (uv sync --extra compat) provides optional
dependencies (huggingface_hub, pillow, safetensors).
Overview¶
| Module | Purpose |
|---|---|
hf_loader |
Load HuggingFace checkpoints |
hf_publisher |
Publish models to HuggingFace Hub |
weight_mapping |
Map weight names between formats |
HF Loader¶
hf_loader
¶
HuggingFace Model Loader.
Provides from_pretrained functionality for loading HuggingFace models
into our DecoderModel format.
from_pretrained
¶
Load a pretrained model from HuggingFace format.
Supports loading from: - Local directory with config.json and model weights - HuggingFace Hub model ID (requires huggingface_hub)
When loading from the Hub, only *.json and *.safetensors are
downloaded. *.bin files are intentionally skipped because they are
pickled and can execute arbitrary code on load. Local *.bin files
are still accepted (you opted into that file by putting it on disk).
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
model_path
|
str | Path
|
Local path or HuggingFace model ID. |
必需 |
device
|
str | device
|
Device to load model on ("auto", "cuda", "cpu"). |
'auto'
|
dtype
|
dtype | None
|
Data type for model weights. |
None
|
trust_remote_code
|
bool
|
Whether to trust remote code (for HF Hub). |
False
|
返回:
| 类型 | 描述 |
|---|---|
DecoderModel
|
Loaded DecoderModel. |
源代码位于: src/llm/compat/hf_loader.py
list_supported_architectures
¶
List supported model architectures.
mistral is included (dense Mistral). mixtral is deliberately NOT:
it is block-sparse MoE, which :func:from_pretrained rejects with a clear
error rather than silently dropping every expert/router tensor (RIL
ISS-144).
源代码位于: src/llm/compat/hf_loader.py
HF Publisher¶
hf_publisher
¶
HuggingFace publish helpers — reverse of :mod:llm.compat.hf_loader.
Provides :func:save_pretrained and :func:push_to_hub so models
trained with this project can be shared on HuggingFace Hub in a
format that the existing :func:llm.compat.hf_loader.from_pretrained
can roundtrip-load (i.e. produces config.json +
model.safetensors in Llama-style naming).
Both helpers are soft-dependency-friendly:
safetensorsis required for save — install viapip install 'llm[compat]'.huggingface_hubis required for push — same install command.
Each raises a clear ImportError with the install hint when the
corresponding dependency is missing, mirroring the convention used
elsewhere in the project (flash_attn, huggingface_hub in the
loader, etc.).
The reverse weight mapping lives in :mod:llm.compat.weight_mapping
(see :func:llm.compat.weight_mapping.convert_our_weights).
save_pretrained
¶
Save a :class:DecoderModel in HuggingFace-compatible format.
Writes:
config.json— Llama-style config; loadable byLlamaConfig.model.safetensors— state dict in Llama-style naming. Roundtrip- loadable by :func:llm.compat.hf_loader.from_pretrained.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
model
|
DecoderModel
|
A trained :class: |
必需 |
save_directory
|
str | Path
|
Local directory to write into. Created if it doesn't exist. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
Path
|
The resolved |
引发:
| 类型 | 描述 |
|---|---|
ImportError
|
If |
源代码位于: src/llm/compat/hf_publisher.py
push_to_hub
¶
push_to_hub(model, repo_id, *, token=None, private=False, commit_message='Upload model', exist_ok=True, save_directory=None)
Save the model locally and push to a HuggingFace Hub repo.
Calls :func:save_pretrained to a staging directory (or to
save_directory if provided) and uploads via
huggingface_hub.upload_folder. The repo is created on first
push unless exist_ok=False.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
model
|
DecoderModel
|
A trained :class: |
必需 |
repo_id
|
str
|
HF Hub repo ID (e.g. |
必需 |
token
|
str | None
|
HF auth token. Falls back to |
None
|
private
|
bool
|
Whether to create the repo as private. |
False
|
commit_message
|
str
|
Git commit message on the Hub side. |
'Upload model'
|
exist_ok
|
bool
|
Don't raise if the repo already exists. |
True
|
save_directory
|
str | Path | None
|
Optional staging dir; defaults to a temporary directory under the system temp path. |
None
|
返回:
| 类型 | 描述 |
|---|---|
str
|
The HF Hub URL of the pushed repo (e.g. |
str
|
|
引发:
| 类型 | 描述 |
|---|---|
ImportError
|
If |
源代码位于: src/llm/compat/hf_publisher.py
Weight Mapping¶
weight_mapping
¶
Weight Mapping for HuggingFace Model Conversion.
Provides mappings from HuggingFace weight names to this project's naming convention. Supports Llama, Mistral, and Qwen architectures.
detect_architecture
¶
Detect model architecture from HuggingFace config.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
config
|
dict[str, Any]
|
HuggingFace model config dict. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
str
|
Architecture name (llama, mistral, qwen, qwen2, mixtral) or |
str
|
|
源代码位于: src/llm/compat/weight_mapping.py
get_weight_mapping
¶
Get weight name mapping for an architecture.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
architecture
|
str
|
Architecture name. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict[str, str]
|
Dictionary mapping HF names to our names. |
源代码位于: src/llm/compat/weight_mapping.py
expand_layer_mapping
¶
Expand layer-indexed mappings for all layers.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
mapping
|
dict[str, str]
|
Base mapping with {layer} placeholders. |
必需 |
num_layers
|
int
|
Number of transformer layers. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict[str, str]
|
Expanded mapping with concrete layer indices. |
源代码位于: src/llm/compat/weight_mapping.py
convert_hf_weights
¶
Convert HuggingFace state dict to our naming convention.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
hf_state_dict
|
dict[str, Any]
|
HuggingFace model state dict. |
必需 |
architecture
|
str
|
Model architecture. |
必需 |
num_layers
|
int
|
Number of transformer layers. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict[str, Any]
|
Converted state dict with our naming. |
源代码位于: src/llm/compat/weight_mapping.py
convert_gguf_weights
¶
Translate llama.cpp GGUF tensor names into our naming convention.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
gguf_state_dict
|
dict[str, Any]
|
GGUF tensors keyed by llama.cpp names ( |
必需 |
num_layers
|
int
|
Number of transformer layers (to expand the |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict[str, Any]
|
|
list[str]
|
(q/k/v still split as |
tuple[dict[str, Any], list[str]]
|
func: |
tuple[dict[str, Any], list[str]]
|
that had no mapping. A non-empty |
tuple[dict[str, Any], list[str]]
|
not a pure dense Llama-style GGUF (or carries extra tensors); the |
tuple[dict[str, Any], list[str]]
|
caller should refuse rather than silently drop them (RIL ISS-220 |
tuple[dict[str, Any], list[str]]
|
philosophy). |
源代码位于: src/llm/compat/weight_mapping.py
convert_our_weights
¶
convert_our_weights(our_state_dict, architecture, num_layers, *, num_heads=None, num_kv_heads=None, head_dim=None)
Convert our naming convention to HuggingFace state dict.
Inverse of :func:convert_hf_weights for the supported weight
names. Used by save_pretrained to publish models to
HuggingFace in a format the existing from_pretrained can
roundtrip-load.
Splits our combined qkv_proj projection into HF's separate
q_proj / k_proj / v_proj weights so the published
artifact is loadable by both our from_pretrained (which uses
the reverse concat) and HF's transformers library.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
our_state_dict
|
dict[str, Any]
|
Our model state dict (e.g. |
必需 |
architecture
|
str
|
Target HF architecture (must match the model). |
必需 |
num_layers
|
int
|
Number of transformer layers in the model. |
必需 |
num_heads
|
int | None
|
Total attention heads. Required when the model has
a combined |
None
|
num_kv_heads
|
int | None
|
Number of KV heads (for GQA/MQA). Defaults to
|
None
|
head_dim
|
int | None
|
Per-head dimension. Defaults to |
None
|
返回:
| 类型 | 描述 |
|---|---|
dict[str, Any]
|
Converted state dict with HuggingFace naming. |
源代码位于: src/llm/compat/weight_mapping.py
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 | |
convert_hf_to_combined_qkv
¶
convert_hf_to_combined_qkv(our_state_dict, num_layers, *, num_heads=None, num_kv_heads=None, head_dim=None)
Concatenate our separate q_proj / k_proj / v_proj
projections into the combined qkv_proj.
Used by :func:llm.compat.hf_loader.from_pretrained after
:func:convert_hf_weights has renamed HF Llama's separate
q/k/v projections to our naming. Our MHA stores Q/K/V in a
single qkv_proj Linear — this helper fuses the three
projections back together so load_state_dict finds the
expected key.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
our_state_dict
|
dict[str, Any]
|
Our-renamed state dict (output of
|
必需 |
num_layers
|
int
|
Number of transformer layers. |
必需 |
num_heads
|
int | None
|
Total attention heads. |
None
|
num_kv_heads
|
int | None
|
Number of KV heads (for GQA/MQA). Defaults to
|
None
|
head_dim
|
int | None
|
Per-head dimension. |
None
|
返回:
| 类型 | 描述 |
|---|---|
dict[str, Any]
|
State dict with combined |
源代码位于: src/llm/compat/weight_mapping.py
get_config_mapping
¶
Map HuggingFace config to our config format.
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
hf_config
|
dict[str, Any]
|
HuggingFace config dict. |
必需 |
返回:
| 类型 | 描述 |
|---|---|
dict[str, Any]
|
Our config dict. |
源代码位于: src/llm/compat/weight_mapping.py
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | |