Skip to content

Feat/native gguf llama - #327

Open
amodhyh wants to merge 9 commits into
FlashML-org:mainfrom
amodhyh:feat/native-gguf-llama
Open

Feat/native gguf llama#327
amodhyh wants to merge 9 commits into
FlashML-org:mainfrom
amodhyh:feat/native-gguf-llama

Conversation

@amodhyh

@amodhyh amodhyh commented Sep 1, 2026

Copy link
Copy Markdown

This pull request adds comprehensive support for loading and running Llama 4 (and Llama) models in GGUF format within the FreeToken codebase. The changes introduce GGUF-specific model configuration and weight loading for Llama, update tokenizer handling for new special tokens, and extend the model registry and architecture mappings to recognize and instantiate Llama GGUF models. Additionally, it updates the Llama model implementation to support new routing logic for MoE and shared expert layers.

Llama GGUF Model Support

  • Added a new gguf.py adapter in freetoken.models.llama for parsing GGUF configs and iterating weights, including support for MoE and shared expert routing. Also includes a conversion function to adapt Llama models to GGUF format.
  • Updated the Llama model (model.py) to support GGUF models: conditional initialization and conversion, new MoE/shared expert routing logic in decoder layers, and correct handling of GGUF-specific weight formats. [1] [2] [3] [4]

Tokenizer and Special Token Handling

  • Updated the GGUF tokenizer to recognize Llama 4 architectures and handle <|eot|> and <|eot_id|> as end-of-turn tokens, ensuring correct EOS token selection and stop id computation. [1] [2] [3]

Configuration and Registry Updates

  • Extended the GGUF architecture-to-registry mapping to include both llama and llama4, mapping them to the correct GGUF model class.
  • Registered the new LlamaGGUFForCausalLM in the model registry, specifying the GGUF-specific config and weight loading functions.
  • Exposed the GGUF config and weight iterators in the Llama module's __all__ for import convenience.

References #34. This PR introduces the LLaMA implementation for the overarching feature request.

Adds parse_gguf_config to map GGUF metadata to ModelConfig, iter_gguf_weights to stream and fuse QKV/GateUp tensors, and convert_llama_to_gguf to swap dense PyTorch layers with custom GGUF GPU kernels.
- Clamp RoPE max_position to 8192 to prevent massive pre-allocation OOMs
- Enable MoE offload routing by setting `moe_enabled` based on expert count
- Filter MoE expert tensors in `iter_gguf_weights` using include_moe_experts flags
- Implement shared expert mapping and fused gate/up aggregation
- Prevent 720 MiB GPU OOMs during dense pass by skipping expert allocations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant