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

Đang mở Phù hợp với người mới
#3,568 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
82/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
backend, machine-learning

Hướng nghiên cứu

Bắt đầu tại src/lib/mlxcel-core/src/layers.rs, ở QuantizedEmbedding::from_weights_with_mode, rồi so sánh phần xác thực của nó với UnifiedLinear::from_weights_with_mode. Thêm các trường hợp Scales và Biases quá ngắn vào src/lib/mlxcel-core/src/layers_scales_axes_tests.rs, bao gồm đường đi UnifiedEmbedding::from_weights và trường hợp hợp lệ affine_plane(64). Cập nhật chú thích tài liệu “Used by”, rồi chạy ba lệnh xác minh được liệt kê trong issue.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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
Ngôn ngữ chính
Rust
Star
505
Fork
81
Merge trung bình
14 giờ 2 phút
Pull request đã merge (30 ngày)
448

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của lablup/mlxcel

Tất cả issue của lablup/mlxcel

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.