fix(core): validate scales and biases rows in QuantizedEmbedding loading

オープン 初心者向け
#3,568 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
82/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust

調査の方向性

src/lib/mlxcel-core/src/layers.rs の QuantizedEmbedding::from_weights_with_mode から始め、その検証を UnifiedLinear::from_weights_with_mode の検証と比較します。src/lib/mlxcel-core/src/layers_scales_axes_tests.rs に、短すぎる Scales と Biases のケースを追加します。UnifiedEmbedding::from_weights の経路と、有効な affine_plane(64) のケースも含めます。「Used by」のドキュメントコメントを更新し、issue に記載されている3つの検証コマンドを実行します。

索引モデルが issue の本文から書いたものです。

説明

area:core priority:high status:ready type:security

Problem

QuantizedEmbedding::from_weights_with_mode (src/lib/mlxcel-core/src/layers.rs:375-436) accepts a scales or biases plane with fewer rows than weight. Safetensors files are untrusted input, and with a short plane the lookup reads past the buffer: quantized_embedding (src/lib/mlxcel-core/cpp/mlx_cxx_bridge.cpp:4023-4028) runs take(scales, ids, 0) and take(biases, ids, 0) with token ids bounded only by the weight's row count, and the tied head (as_linear) indexes scales by output row in qmv. The loader only calls reconcile_quantization_layout_logged, which looks at the last axis alone.

Current Behavior

UnifiedLinear::from_weights_with_mode (layers.rs:2912) already calls check_scales_leading_axes (issue #3457), so quantized linears are covered. The embedding loader was missed. PR #3542 patched this only for Lille, in src/models/lille_130m_shapes.rs:109-116 (check_packing). Other families get the check only if they call validate_embedding_table (BailingMoe, Gpt2, GptBigCode, GptNeoX, Mamba, Mamba2, Florence2). Every other UnifiedEmbedding::from_weights caller is unprotected.

Proposed Solution

In QuantizedEmbedding::from_weights_with_mode, call check_scales_leading_axes(prefix, &w_shape, &s_shape, b_shape.as_deref())? before reconciliation, the same way UnifiedLinear does. That covers from_weights and UnifiedEmbedding::from_weights too. Keep Lille's check_packing, which also validates the column count.

Out of scope: QuantizedWeight::new (layers.rs:106) is infallible and its callers build planes by hand; file a follow-up if an audit finds an unchecked caller.

Acceptance Criteria

  • New failing test in src/lib/mlxcel-core/src/layers_scales_axes_tests.rs: QuantizedEmbedding::from_weights_with_mode(&affine_plane(32), "p", 64, 4, "affine") returns an Err that names p.scales, [32, 1] and [64, 8]. Add the same check through UnifiedEmbedding::from_weights, plus a case where only biases is short. affine_plane(64) still loads
  • The existing Lille and validate_embedding_table tests still pass
  • Doc comment on check_scales_leading_axes lists QuantizedEmbedding::from_weights_with_mode under "Used by"

Verification

cargo test -p mlxcel-core --lib layers::scales_axes_tests
cargo test --lib lille_130m
cargo clippy --all-targets -- -D warnings
主要言語
Rust
スター
505
フォーク
81
平均マージ
14時間 2分
マージ済み PR(30日)
448

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

lablup/mlxcel のほかの issue

lablup/mlxcel の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。