Skip to content

Support PLaMo3 instruction-tuned models with YaRN - #1931

Merged
zcbenz merged 2 commits into
ml-explore:mainfrom
mitmul:codex/plamo3-610m-fin-speed
Oct 18, 2026
Merged

zcbenz merged 2 commits into
ml-explore:mainfrom
mitmul:codex/plamo3-610m-fin-speed

Conversation

@mitmul

@mitmul mitmul commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Requirements

  • I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: AI assisted with implementation and testing.

Model information

Supported checkpoints

Tested revision: a000b59209f1688322c0804009dc9f3fd67e62c1.

Verification

Test environment: Apple M1 Max, MLX 0.32.3, Python 3.13.15.

The CLI check used an 8-bit conversion with group size 64. To convert and run:

python -m mlx_lm convert \
  --hf-path pfnet/plamo-3-610m-fin-instruct \
  --mlx-path ./plamo3-fin-8bit \
  -q --q-bits 8 --q-group-size 64 \
  --trust-remote-code

python -m mlx_lm generate \
  --model ./plamo3-fin-8bit \
  --trust-remote-code \
  --temp 0 \
  --max-tokens 512 \
  --prompt 'PERとは何ですか?一文で答えて。'

Output (final answer excerpt; reasoning text omitted):

PER(Price Earnings Ratio、株価収益率)は、株価が1株当たり利益(EPS)の何倍かを示す指標です。計算式は「株価 ÷ 1株当たり利益」となります。

Reference output (if you have access to CUDA hardware)

No CUDA run was performed. Reference comparisons used PyTorch 2.9.0 on MPS with FP32 weights.

  • Compared three finance prompts, including all prefill positions and 32 subsequent decoding steps per prompt.
  • Also compared the final prefill position and eight decoding steps for a 4096-token input.
  • Next-token argmax matched at all compared positions. Maximum absolute logit error was below 2e-4.

These comparisons used unquantized FP32 weights; the 8-bit CLI output above is a separate generation check.

Full-model inference at the maximum 262144-token context length has not been tested.

@mitmul

mitmul commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Hi @zcbenz, thank you again for reviewing and merging the original PLaMo 3 support in #1234.
Would you be able to take a look at this follow-up when you have a chance? It extends the existing implementation to support instruction-tuned PLaMo3 checkpoints with YaRN, and has been ready for review since September 29. Reference comparison results are included in the PR description.
Please let me know if any changes or additional validation would help with the review. Thank you for your time!

@zcbenz
zcbenz force-pushed the codex/plamo3-610m-fin-speed branch from beceff8 to 7b0242a Compare October 18, 2026 23:21

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@zcbenz
zcbenz merged commit c39139d into ml-explore:main Oct 18, 2026
2 checks passed
@mitmul
mitmul deleted the codex/plamo3-610m-fin-speed branch October 19, 2026 00:06
inureyes pushed a commit to lablup/mlxcel that referenced this pull request Nov 27, 2026
…uncate (#3444)

Gemma 4: a transformers 5.15+ config writes the full-attention shapes as `per_layer_config` instead of `global_head_dim` / `num_global_key_value_heads`. The new `mlxcel_core::gemma4_per_layer::gemma4_global_attention_from_per_layer_config` ports upstream's rules and error texts (Neywa-Labs/mlx-vlm#2719), and `TextConfig::apply_per_layer_config` folds the result into the two global fields at every parse site: `ModelArgs::text_args`, the fallible `ModelArgs::validate_text_config` the text, VLM, Unified, stage and tensor-parallel loaders call right after parsing, DiffusionGemma, the tensor-parallel batching probe and the MTP drafter config. A full-attention layer takes the global KV-head count under `attention_k_eq_v` or a `per_layer_config`. The memory preflight resolves the block through the same function and keeps its previous figure when the block does not resolve. The tensor-parallel runtime restates the block after dividing the KV heads per rank.

Qwen 3.5: a `rope_parameters` block whose `type` or `rope_type` is `yarn` builds its table with `rope_utils::yarn_rope_setup` over the rotary dims, with `max_position_embeddings` as the default original context (Neywa-Labs/mlx-vlm#2649). The attention factor follows the transformers rules and multiplies only the rotary lanes of Q and K, on the standard, per-row verify, batched and MRoPE paths. An invalid YaRN block is a load error naming the field. Other users of the Qwen3-Next attention keep the plain rotation.

`rope_utils`: `RopeScalingSpec` reads YaRN `truncate` (JSON bool only), and `truncate: false` keeps the correction band's fractional bounds as ml-explore/mlx-lm#1931 does. Without it the table is bit-identical to the previous integer ramp. The module's `Used by:` header now names every caller.

Fixes #3215
inureyes added a commit to lablup/mlxcel that referenced this pull request Nov 28, 2026
…3513)

## Summary

- Adds PLaMo 3 (`plamo3`, `Plamo3ForCausalLM`): a dense sliding/full attention hybrid with packed `qkv_proj` and `gate_up_proj`, q/k offset norms before RoPE, and PLaMo 2's offset RMSNorms (helpers shared from `plamo2.rs`). Wired through detection, `ModelType`, `LoadedModel`, the config-backed loader, `mlxcel arch`, and model-owned state via `sliding_hybrid_lm` (native batched decode, snapshots, last-row logits).
- Full-attention layers follow the checkpoint's `modeling_plamo.py`: YaRN (factor 256 over 4096 on the 610m instruct, `truncate: false`, concentration `0.1 ln(factor) + 1`) resolved through `RopeScalingKind::resolve`, so the server's `--rope-scaling none` reproduces mlx-lm's rotation. Sliding layers use plain `rope_local_theta`, a `window_size` ring and mlx-lm's `window_size + 1` prefill mask.
- Tokenizer: every `CONTROL` row of `tokenizer.jsonl` becomes a special token (`<|plamo:msg|>` = 19, `<|plamo:tag|>` = 16), and for `Plamo3Tokenizer` only, its boundary splits around `<|plamo:...|>`, repeated-character and space runs are reproduced. PLaMo 2 encodings are unchanged.
- `scale_embedding: true`, non-null image fields and non-`normal` `linear_type` are refused by name. `image_proj_type` is not rejected because the public 610m checkpoint ships `"linear"` with every other image field null.
- Load-time hardening: every weight, including an untied `lm_head` (checked against `[vocab_size, hidden_size]`), is shape-validated against `config.json`, and the `break_around_*_threshold` values read from `tokenizer_config.json` are capped at 256 (shipped values are 4 and 2), since the repeated-character split pattern is quadratic for large thresholds.

The YaRN `truncate` toggle the issue asks for already existed on main (ml-explore/mlx-lm#1931 port); this PR adds the factor 256 / 4096 pin (`yarn_truncate_toggle_on_the_plamo3_geometry`: default equals `truncate=true` bit for bit, `truncate=false` differs).

## Verification (spark-102, GB10, CUDA; Metal not run)

- Unit tests (`plamo3`, `rope_utils`, `tokenizer::`, `detection`, coverage suites): pass. Covers pattern 8, the six offsets, q/k norm before RoPE, the YaRN table against the reference formula, the `window + 1` mask, ring wrap, all-full and all-sliding stacks, snapshot, batched decode, last-row logits. PLaMo 2 tokenizer tests pass against `models/plamo-2-1b`.
- Revert check: reverting q/k order, one offset, `truncate`, the mask band, CONTROL registration and the pre-tokenizer fails 8 tests.
- Tokenizer: 10 strings match `Plamo3Tokenizer` ids exactly, including `<|plamo:msg|>` -> `[19]`.
- `mlxcel generate --temp 0`: Japanese (111 tokens) and English (47 tokens) answers are coherent and stop at the turn end.
- `mlxcel-server` chat completion: `finish_reason: stop`, "Paris is the capital city of France."
- HF reference (transformers 5.24.1, f32 CPU, YaRN on), 256-token teacher-forced stream: top-1 agreement 250/256.
- mlx-lm 0.32.2 (CUDA), YaRN off: greedy ids match 59/64 (ja) and 38/64 (en); teacher-forced top-1 agrees 63/64 on both. Not a full 64-token match: each divergence is a one-ULP bf16 tie in mlx-lm's own logits (0.125 apart at ja step 59; at en step 38 mlx-lm's own decode and prefill pick different ids).
- Rebuilt at the final head: `mlxcel generate` (Japanese, 111 tokens, stops at turn end) and `mlxcel-server` chat completion (`finish_reason: stop`) re-checked.
- clippy (`--lib --tests --features cuda -D warnings`), fmt and the four contract checks pass. Workspace-wide tests not run here.

## Changes during review

- Security review: untied `lm_head` shape validation (`a_misshapen_untied_lm_head_is_a_load_error`) and the 256 cap on PLaMo 3 pre-tokenizer thresholds (`oversized_break_thresholds_are_rejected`).

## Open points

- Reasoning in `<|plamo:begin_think:plamo|>` markers stays in `content`; no PLaMo reasoning parser yet.
- With `skip_special_tokens`, PLaMo 2 now also drops its CONTROL rows (for example `<|plamo:op|>`) on decode; its encodings are unchanged.
- mlx-lm's convention (`window + 1` prefill band, `window` decode ring) makes a split prefill on a wrapped ring differ slightly from a one-shot prefill. The HF reference uses `window` throughout.

Closes #2374
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.

2 participants