Add Hunyuan dense V1 (Hunyuan-MT-7B) support#4
Open
beshkenadze wants to merge 1 commit into
Open
Conversation
Owner
Author
📌 Upstreaming note (ml-explore)This PR bundles two layers for convenience in the fork, but upstream it must go as two separate PRs — the core RoPE addition lives in
Each should branch off |
0124bb3 to
264fe11
Compare
Tencent's Hunyuan dense V1 architecture (HunYuanDenseV1ForCausalLM), used by the translation models Hunyuan-MT-7B and Hy-MT2-7B. A Llama-family dense transformer, closest to the existing Qwen3 path: - Per-head QK RMSNorm, GQA, SwiGLU MLP, pre/post RMSNorm blocks. - Tied embeddings (sanitize drops the tied lm_head). - DynamicNTKAlphaRoPE: rescales the RoPE base once by alpha^(dim/(dim-2)) and reuses the existing freqs-based fast-RoPE path (no sequence-length-dependent resizing). Changes: - Libraries/MLXLMCommon/RoPEUtils.swift: DynamicNTKAlphaRoPE. - Libraries/MLXLLM/Models/Hunyuan.swift: HunyuanModel / HunyuanConfiguration (flat config decode; accepts head_dim or attention_head_dim; conditional qk-norm). - Libraries/MLXLLM/LLMModelFactory.swift: register hunyuan_v1_dense; presets hunyuan_mt_7b_4bit/8bit and hy_mt2_7b_4bit/8bit. - Tests/MLXLMTests/HunyuanTests.swift: config decode, attention_head_dim alias, sanitize, tiny forward, dynamic-RoPE alpha, presets. Validated end-to-end against the mlx-lm reference (byte-identical greedy) on locally converted 4-bit weights for both models.
264fe11 to
c7623b6
Compare
Owner
Author
✅ Upstreamed (correction to the note above)Submitted to ml-explore/mlx-swift-lm as two separate PRs — the split is per-feature (each branched off upstream
Both build + pass tests on the current upstream base. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds Tencent's Hunyuan dense V1 architecture (
hunyuan_v1_dense/HunYuanDenseV1ForCausalLM), used by Hunyuan-MT-7B (translation-tuned) and Hunyuan-7B-Instruct. Ported from mlx-lm'shunyuan_v1_dense.py.Architecture
A Llama-family dense transformer, closest to the existing Qwen3 path:
query_layernorm/key_layernorm), GQA, SwiGLU MLP, pre/post RMSNorm blocks.sanitizedrops the tiedlm_head).DynamicNTKAlphaRoPE— the one new piece: rescales the RoPE base once byalpha^(dim/(dim-2))and reuses the existing freqs-based fast-RoPE path (no sequence-length-dependent resizing). Mirrors mlx-lm.Changes
Libraries/MLXLMCommon/RoPEUtils.swift:DynamicNTKAlphaRoPE.Libraries/MLXLLM/Models/Hunyuan.swift:HunyuanModel/HunyuanConfiguration(flat config decode; accepts bothhead_dimandattention_head_dim; conditional qk-norm).Libraries/MLXLLM/LLMModelFactory.swift: registerhunyuan_v1_denseand thehunyuanalias (the older Instruct checkpoint reportsmodel_type: hunyuan, dense-equivalent withnum_experts=1); presetshunyuan_mt_7b_4bit/8bit+hunyuan_7b_instruct_4bit.Tests/MLXLMTests/HunyuanTests.swift: 5 unit tests (config decode, sanitize, tiny forward, dynamic-RoPE alpha, presets).scripts/:hunyuan_reference.sh(CUDA greedy capture) andhunyuan_convert_mlx.sh(MLX 4/8-bit conversion).Validation
5/5 unit tests pass; package builds.
End-to-end byte-identical parity against the mlx-lm reference on a locally-converted
Hunyuan-MT-7B-4bit, greedy:Prompt: Translate the following segment into Chinese, without additional explanation.\n\nIt's on the house.
hunyuan_v1_dense)HunyuanModelIdentical — validates config decode, weight loading, qk-norm, GQA, dynamic RoPE, tied embeddings, and the chat template via swift-jinja.
Notes
mlx-communitybuild of Hunyuan-MT-7B exists yet;scripts/hunyuan_convert_mlx.shproduces the 4/8-bit weights the presets point at (tokenizer.json ships with the source, so it loads in swift-transformers).mlx-community/Hunyuan-7B-Instruct-4bitships onlyhy.tiktoken(notokenizer.json) so it does not load in swift — Hunyuan-MT-7B is the intended target.