Skip to content

Support Mistral Multimodal Embed for text and image embeddings - #3654

Merged
inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:feat/mistral-contrastive-3631
Dec 2, 2026
Merged

inureyes merged 4 commits into
lablup:mainfrom
Jaeyeong-CHOI:feat/mistral-contrastive-3631

Conversation

@Jaeyeong-CHOI

Copy link
Copy Markdown
Contributor

Closes #3631.

Adds the Mistral contrastive text and image towers, checkpoint detection and loading, and image preprocessing. Both towers return raw 1024-dimensional projections; the embedding engine applies normalization when requested. Pretokenized text must include EOS for pooling.

Validated on Spark1 CUDA with the BF16 (625384b8) and 4-bit (c7cbea8b) exports against mlx-vlm at c745aa70. Text-only, image-only and interleaved requests preserved input order. Across four captions and three images, minimum cosine to reference was 0.999959, and all three images retrieved their matching caption on both exports. Raw projection relative error was below 0.93% across the mixed batch. Both native routes matched the OpenAI route; a token row without EOS returned HTTP 400 with its input index.

Independent review identified normalization being applied inside the towers. The included HTTP regression check failed against that implementation and passed against both rebuilt exports after the correction.

@inureyes inureyes added status:review Under review type:enhancement New features, capabilities, or significant additions priority:low Low priority area:models Model architectures, weights, loading, metadata modelsize:small 4-bit checkpoint <= 10GB; fast iteration, safe for smoke tests modeltype:vlm Vision-language model arch:dense Dense transformer decoder labels Dec 1, 2026
Jaeyeong-CHOI and others added 4 commits December 2, 2026 09:12
…bution

Include top-level `text_vocab_size` in `resolve_vocab_size` so Mistral Multimodal Embed checkpoints enforce token-id bounds even without an HF tokenizer fallback, and add a unit test in `src/embeddings/limits_tests.rs`.

Add a unit test in `src/embeddings/engine_tests.rs` verifying `EmbeddingEngine::embed_tokens` rejects rows missing `required_eos_token()` with `InvalidRow` and the offending index.

Complete per-file third-party attribution URLs and `NOTICE` pointers in `src/models/mistral_contrastive.rs` and `src/vision/dense/preprocess.rs`.

Validated with 244 `mistral_contrastive`/`preprocess`/`embeddings` unit tests, `cargo fmt --check`, and a real Metal `mlxcel-server --embeddings` test on `nativ-community/Mistral-Multimodal-Embed-1B-4bit` (raw norms 224.3538 text / 27.8373 image_url, matching caption cosine 0.2210 vs unrelated 0.0158, missing-EOS HTTP 400).

Refs lablup#3631, lablup#3654
@inureyes
inureyes force-pushed the feat/mistral-contrastive-3631 branch from 7ab3e1f to 6655c57 Compare December 2, 2026 00:24
@inureyes

inureyes commented Dec 2, 2026

Copy link
Copy Markdown
Member

Implementation Hardening Summary

Audited PR #3654 against issue #3631 across the Mistral Multimodal Embed dual encoder, image preprocessing, embedding engine, and HTTP routes.

Findings & Fixes

  • Rebase onto main: Rebased onto origin/main and resolved the NOTICE conflict (aligning the Mistral Multimodal Embed heading underline).
  • Correctness / Bounds (MEDIUM): resolve_vocab_size in src/embeddings/limits.rs checked vocab_size and text_config.vocab_size but ignored top-level text_vocab_size, which Mistral Multimodal Embed uses. Added text_vocab_size fallback and a unit test in src/embeddings/limits_tests.rs.
  • Tests (MEDIUM): Added embed_tokens_rejects_rows_missing_required_eos in src/embeddings/engine_tests.rs to directly test EmbeddingEngine rejecting missing-EOS rows with InvalidRow and the row index.
  • Attribution (MEDIUM): Added full upstream GitHub URLs and NOTICE pointers to src/models/mistral_contrastive.rs and src/vision/dense/preprocess.rs.
  • Security & Performance: Verified resize_area_f32 overflow/zero-dimension guards, bf16-to-f16 load precision rules, normalization boundary, and // Used by: annotations.

Validation

  • cargo test --profile test-fast --lib -- mistral_contrastive preprocess embeddings: 244 passed, 0 failed (2 ignored).
  • cargo fmt --check: passed.
  • Real-model Metal check (nativ-community/Mistral-Multimodal-Embed-1B-4bit via mlxcel-server --embeddings, Apple M1 Ultra): scripts/verify_mistral_contrastive_normalization.py passed (4bit text raw norm 224.3538, image_url raw norm 27.8373, 74 mixed tokens), matching caption cosine 0.2210 vs unrelated 0.0158, and missing-EOS pretokenized input returned HTTP 400 (input[0] has no EOS token (id 2)).

@inureyes
inureyes merged commit 96f1c55 into lablup:main Dec 2, 2026
36 checks passed
inureyes added a commit that referenced this pull request Dec 2, 2026
Parallel `cargo test --features cuda --lib -- --test-threads=4` aborted in most runs with "invalid memory registered when calling <op>::eval_gpu". The cause is a race in MLX's debug-only CUDA allocation tracker: `move_to_unified_memory` and `move_to_device_memory` freed the old pointer before calling `swap_allocation`, so another thread could be handed that address and track it, and the late swap then moved the live entry away and its next `verify_graph` aborted. A new overlay of `mlx/backend/cuda/allocator.cpp` (base 5a48f9cc) retargets the tracker before the free. Release and test-fast builds define NDEBUG and never ran this code, which is why the merge gates did not see it.

MLX-evaluating lib tests now take `mlx_test_guard()` as their first statement, and `tests/mlx_test_guard_coverage.rs` gains a source scan that fails when a test reaches an eval or host readback before taking the guard. Helpers that took their own guard or the device lock were adjusted so nothing deadlocks. CI clippy now also lints examples (about one second of extra time).

Main broke several times while this was open, and the branch carries the fixes the verification needed: the diffusing_interval settings classification, LLaVA-OneVision in FAMILY_ORDER, two clippy lints, the WebUI string fixture and bundle, and guards on tests added by #3654, #3657 and #3678.

Validation: five consecutive 4-thread CUDA lib runs on GB10 passed (14080 passed, 0 failed each) where the base aborted; the guard scan, family_order, fmt, clippy with examples, the scripts/ci contract checks, the WebUI suite and contract check pass. OpenXLA feature compile fails with the same RUSTFLAGS quoting error as on main.

Closes #3660
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:vlm Vision-language model priority:low Low priority status:review Under review type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(models): add Mistral Multimodal Embed (mistral_contrastive)

2 participants