Skip to content

feat(models): add K2-Horizon (k2_horizon) dense support - #3243

Merged
inureyes merged 5 commits into
mainfrom
feature/2358-k2-horizon
Nov 22, 2026
Merged

inureyes merged 5 commits into
mainfrom
feature/2358-k2-horizon

Conversation

@inureyes

@inureyes inureyes commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Summary

  • k2_horizon (IFM/K2-Horizon-0.9B) now loads on the shared Llama3Model through K2HorizonConfig, a config-backed text family, so the CLI, mlxcel-server, the adapter path and mlxcel arch pick it up.
  • RoPE comes from rope_parameters through the shared impl_rope_lifting_deserialize! lift: YaRN (factor 16, beta_fast 128, beta_slow 4, original 8192) at base 1e6 on the 0.9B, base 1e7 without scaling on the 7B. to_llama3_args is a full struct literal.
  • validate() refuses, naming the field: layernorm_num_groups != 1 (grouped RMSNorm: 3.7B / 7B / 32B), query_key_norm, num_experts or mova_num_experts above 0, a non-null attention_gate_func, use_sliding_window, rope_head_dim != head_dim (partial rotary), a hidden_act other than silu, and a YaRN attention_factor that differs from the shared derivation 0.1 * ln(factor) + 1. Omitted keys take the HF configuration_k2_horizon.py defaults (query_key_norm: true, num_experts: 128, head_dim: 128), so leaving a key out is refused the same way as declaring it. Geometry is capped and heads * head_dim is checked to fit in i32. Tensor parallelism stays refused.
  • RopeScalingSpec gains an optional attention_factor field (read only by K2-Horizon) and yarn_get_mscale becomes pub(crate); no other family's RoPE changes.
  • The directory loader adds the chat template's turn terminator <|ifm|im_end|>, resolved by name from the tokenizer's added tokens, to the model's EOS ids. The sidecars declare only <|endoftext|>, so the marker used to leak into content.
  • No YaRN code changes: the shared mscale (1.27726) equals attention_factor, and the floor/ceil range matches truncate: true.

Real-checkpoint validation (models/k2-horizon-0.9b, bf16, M3 Ultra)

  • mlxcel generate -p "The capital of France is" -n 50 --temp 0: coherent ("... The capital of France is Paris. ..."), 182 tok/s. With a chat question and -n 200 it stops on the terminator after 68 tokens. Re-run after the final screening commit: still loads and generates.
  • mlxcel-server /v1/chat/completions, non-streaming and streaming: finish_reason: stop, 68 tokens, no <|ifm|im_end|> in the output, ending "The capital of France is Paris."
  • Teacher-forced logit trace against the vendored mlx-vlm k2_horizon (d998a620, bf16), 2 chunks of 12288 tokens, compare_logit_traces.py --decided 2:
arm positions top-1 disagreement decided (gap >= 2) perplexity vs ref 15.123
YaRN (this PR) 24576 2.73% 0 / 7279 -0.31%
YaRN, pos >= 8192 8192 2.94% 0 / 2354 -0.43%
rope_scaling dropped 24576 37.64% 700 / 7279 (9.62%) +89.1%
dropped, pos >= 8192 8192 43.21% 352 / 2354 (14.95%) +152.2%

YaRN-arm disagreements all sit where the reference's top-two gap is below 0.25 (mlxcel runs the weights in f16).

Known limitations

  • The <ifm|think> block (and the think_fast / think_faster effort variants) is not split into reasoning_content. Registering the pairs touches five shared marker tables, so it is left for a follow-up.
  • The adapter route (from_weights) does not add the turn terminator: the config-backed weight_builder hook receives only the weights and config.

Test plan

  • models::k2_horizon (14), models::rope_utils (33), models::detection_tests and coverage_tests (18) selectors; clippy -D warnings; fmt; the scripts/ci contract checks

Closes #2358

@inureyes inureyes added type:enhancement New features, capabilities, or significant additions status:review Under review priority:low Low priority area:models Model architectures, weights, loading, metadata modelsize:small 4-bit checkpoint <= 10GB; fast iteration, safe for smoke tests modeltype:text Text-only language model arch:dense Dense transformer decoder labels Nov 22, 2026
inureyes added a commit that referenced this pull request Nov 22, 2026
Bilingual (en/ko) report for the K2-Horizon dense support PR: the config translation onto the shared Llama decoder, the screened variants, the turn terminator, and the real-checkpoint logit-trace and CLI/server results.
@inureyes inureyes added status:done Completed and removed status:review Under review labels Nov 22, 2026
mlxcel exited with `Unsupported model type: k2_horizon` on IFM/K2-Horizon-0.9B. The 0.9B is a plain Llama-shaped decoder with YaRN RoPE, so it now runs on the shared Llama3Model through a K2HorizonConfig translation registered as a config-backed text family (CLI, server, adapter path, `mlxcel arch`).

K2HorizonConfig applies the shared impl_rope_lifting_deserialize! lift, so the transformers 5 rope_parameters block becomes rope_theta / rope_scaling (YaRN factor 16 at base 1e6 on the 0.9B, base 1e7 with no scaling on the 7B). to_llama3_args is a full struct literal so a new llama3::ModelArgs field fails to compile here. validate() refuses, naming the field, grouped RMSNorm (layernorm_num_groups != 1, the 3.7B / 7B / 32B), query_key_norm, num_experts / mova_num_experts > 0, a non-null attention_gate_func, use_sliding_window, and a rope_head_dim that differs from head_dim (partial rotary). Tensor parallelism stays refused for the family.

Unit tests pin both Hub configs through the lift and each refusal; detection and the rope_parameters lift registry cover the new type.

Refs #2358
K2-Horizon's config.json, generation_config.json and tokenizer_config.json declare only <|endoftext|> as EOS, while every assistant turn the chat template renders ends with <|ifm|im_end|>. mlxcel-server therefore decoded the marker into content before the model emitted EOS.

The K2-Horizon loader now resolves <|ifm|im_end|> by name from tokenizer.json added_tokens (falling back to tokenizer_config.json added_tokens_decoder) and appends it to the model's EOS ids, following the InternVL and LLM-jp-VL loaders. Shared EOS resolution for other families is unchanged. Unit tests cover the lookup, the merge, and the real checkpoint tokenizer when present.

Verified on models/k2-horizon-0.9b: server chat completions (streaming and not) and CLI generate stop after 68 tokens with finish_reason stop and no marker in the output. supported-models.md documents the remaining limitation: the <ifm|think> block is not split into reasoning_content.

Refs #2358
…m_experts

K2HorizonConfig defaulted an absent query_key_norm to false and an absent num_experts to 0, so a config that left either key out passed validate() and loaded on the dense Llama decoder. The HF configuration_k2_horizon.py defaults them to true and 128, so such a config describes a QK-norm or MoE model; the QK-norm case would decode wrongly without any error because the unused q_norm/k_norm weights are ignored.

The serde defaults now follow the HF config class, so an omitted key is refused by name like an explicit one. The published 0.9B config declares both keys and still loads. A unit test removes each key from the 0.9B fixture and expects the refusal.

Validated with the k2_horizon, rope_parameters_lift and coverage_tests selectors, clippy -D warnings, fmt and the scripts/ci contract checks.

Refs #2358
Cap num_attention_heads, num_key_value_heads, hidden_size, intermediate_size, vocab_size and max_position_embeddings, and check that heads * head_dim and kv_heads * head_dim fit in i32, so an untrusted config cannot wrap the decoder's i32 casts. The MAX_HEAD_DIM doc comment no longer claims it bounds that product alone.

Refuse a hidden_act other than silu, since the shared decoder is SwiGLU. An omitted head_dim now defaults to 128 as in HF configuration_k2_horizon.py (the 0.9B declares 64 and is unchanged). A YaRN attention_factor that differs from the shared derivation 0.1 * ln(factor) + 1 is refused; RopeScalingSpec gains an optional attention_factor field that no other family reads, and yarn_get_mscale becomes pub(crate).

The adapter route (from_weights) still does not add the turn terminator: the config-backed weight_builder hook receives only the weights and config, with no model directory or tokenizer.

Validation: models::k2_horizon (14 passed), models::rope_utils (33 passed), coverage_tests (18 passed), clippy -D warnings clean, and the 0.9B checkpoint still loads and generates.

Refs #2358
Bilingual (en/ko) report for the K2-Horizon dense support PR: the config translation onto the shared Llama decoder, the screened variants, the turn terminator, and the real-checkpoint logit-trace and CLI/server results.
@inureyes
inureyes force-pushed the feature/2358-k2-horizon branch from 2ba254d to 0eb9404 Compare November 22, 2026 19:46
@inureyes
inureyes marked this pull request as ready for review November 22, 2026 19:51
@inureyes
inureyes merged commit 0b47ba8 into main Nov 22, 2026
33 checks passed
@inureyes
inureyes deleted the feature/2358-k2-horizon branch November 22, 2026 19:51
@inureyes inureyes added this to the 0.9 milestone Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arch:dense Dense transformer decoder area:models Model architectures, weights, loading, metadata modelsize:small 4-bit checkpoint <= 10GB; fast iteration, safe for smoke tests modeltype:text Text-only language model priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(models): add K2-Horizon (k2_horizon) support

1 participant