Skip to content

DRAFT: Make embedding model switchable - #1752

Open
liopeer wants to merge 46 commits into
lightly-ai:mainfrom
liopeer:lionel-triton-studio-client
Open

DRAFT: Make embedding model switchable#1752
liopeer wants to merge 46 commits into
lightly-ai:mainfrom
liopeer:lionel-triton-studio-client

Conversation

@liopeer

@liopeer liopeer commented Jul 22, 2026

Copy link
Copy Markdown

What has changed and why?

NOTE: Not 100% finished, but would like early feedback, since this is my first PR here! Especially the tests still need some focus.

  • allows access to all exposed embedding models
  • includes external connections to Nvidia Triton-hosted embedding models (URL->embedding via gRPC)

How has it been tested?

  • unit test + local tests with Triton serving mobile clip

Did you update CHANGELOG.md?

  • Yes
  • Not needed (internal change)

Summary by CodeRabbit

  • New Features
    • Added configurable embedding backends (Torch or Triton) with selectable embedding model variants.
    • Added Triton-based text, image path, and image-crop embedding with batching and stricter output validation.
    • In-memory image embedding now checks the selected model supports in-memory inputs.
  • Documentation
    • Expanded embedding environment documentation, including Triton connection settings and request timing failure behavior.
    • Minor formatting cleanup in the YOLO26 evaluation tutorial.
  • Tests / Chores
    • Updated and added embedding manager and Triton embedding tests; improved optional Torch handling in the test suite.

@liopeer
liopeer requested a review from a team as a code owner July 22, 2026 19:08
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds selectable torch and Triton embedding backends, configurable model variants, chunked Triton inference, runtime PIL-image checks, optional dependency handling, and related tests and documentation.

Changes

Embedding backend integration

Layer / File(s) Summary
Embedding configuration and contracts
lightly_studio/pyproject.toml, lightly_studio/src/lightly_studio/dataset/env.py, lightly_studio/src/lightly_studio/dataset/embedding_generator.py, lightly_studio/.env.example
Adds Triton and mypy configuration, embedding environment variables, crop-coordinate documentation, and the PIL image generator protocol.
Selectable torch model generators
lightly_studio/src/lightly_studio/dataset/*embedding_generator.py
Adds model-name selection and validation for MobileCLIP and Perception Encoder generators.
Triton embedding generator
lightly_studio/src/lightly_studio/dataset/triton_mobileclip_embedding_generator.py, lightly_studio/tests/dataset/test_triton_mobileclip_embedding_generator.py
Adds Triton inference for text, images, and crops with batching, metadata parsing, output validation, hashing, and tests.
Embedding manager orchestration
lightly_studio/src/lightly_studio/dataset/embedding_manager.py, lightly_studio/tests/dataset/test_embedding_manager.py
Routes torch and Triton configurations, validates PIL-image support, restricts video loading, and tests model routing.
Repository support and documentation
lightly_studio/src/lightly_studio/api/..., lightly_studio/tests/dataset/*, lightly_studio/docs/docs/tutorials/*
Updates optional-import handling, skips torch-dependent tests when unavailable, removes import suppressions, and adjusts documentation formatting.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

Suggested reviewers: horatiualmasan

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 28.57% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: making the embedding model switchable.
Description check ✅ Passed The description matches the required template and includes the change summary, testing notes, and changelog status.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🧹 Nitpick comments (2)
lightly_studio/.env.example (1)

17-22: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Clarify LIGHTLY_STUDIO_EMBEDDINGS_MODEL_NAME semantics for the triton type.

Line 18 documents this variable via "# Embeddings. The torch backend supports MobileCLIP and Perception Encoder model names." but doesn't state what value is expected when LIGHTLY_STUDIO_EMBEDDINGS_MODEL_TYPE=triton. In embedding_manager.py, the same env var is forwarded verbatim as the Triton model name (TritonEmbeddingGenerator(url=..., model_name=model_name)), so with the default mobileclip_s0 a user switching to Triton without also overriding MODEL_NAME will send a Triton model name that likely doesn't match their actual deployed model repository name.

📝 Suggested doc clarification
-# LIGHTLY_STUDIO_EMBEDDINGS_MODEL_NAME=mobileclip_s0
+# For torch: name of a MobileCLIP or Perception Encoder variant.
+# For triton: must match the model name in your Triton model repository.
+# LIGHTLY_STUDIO_EMBEDDINGS_MODEL_NAME=mobileclip_s0
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lightly_studio/.env.example` around lines 17 - 22, Update the embedding
environment variable comments to state that LIGHTLY_STUDIO_EMBEDDINGS_MODEL_NAME
must match the deployed Triton model repository name when
LIGHTLY_STUDIO_EMBEDDINGS_MODEL_TYPE=triton, while retaining the existing torch
model-name guidance.
lightly_studio/src/lightly_studio/dataset/embedding_manager.py (1)

247-281: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Chunked embed_images logic verified correct; consider adding test coverage.

The chunk-relative kept_indices re-offsetting, the "raise only if no chunk succeeded" AllInputFilesFailedError handling, and the empty-embeddings fallback are all consistent. However, no test in test_embedding_manager.py exercises this chunking/failure path (only _load_torch_embedding_generator is covered by the new tests), despite this being flagged as high complexity.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@lightly_studio/src/lightly_studio/dataset/embedding_manager.py` around lines
247 - 281, Add tests in test_embedding_manager.py covering the chunked
embed_images flow: verify kept_indices are offset to global sample IDs across
chunks, successful chunks are stored when another raises
AllInputFilesFailedError, and the error is re-raised only when every chunk fails
with an empty-embeddings fallback. Use mocks for model.embed_images and
_store_embeddings.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lightly_studio/pyproject.toml`:
- Line 68: The Triton client should be optional rather than installed for every
user. In lightly_studio/pyproject.toml lines 68-68, move
tritonclient[grpc]>=2.41.1 from the main dependencies into a Triton
optional-dependencies/extras group; in
lightly_studio/src/lightly_studio/dataset/embedding_manager.py lines 606-618,
retain the existing try/except ImportError guard around the Triton image
embedding generator loader with no direct change required.

In `@lightly_studio/src/lightly_studio/dataset/embedding_manager.py`:
- Around line 600-626: Update _load_image_embedding_generator_from_env so a
failed TritonEmbeddingGenerator import logs the disabled message once and
returns None immediately. Prevent the Triton path from falling through to the
unsupported-model-type warning, while preserving the existing behavior for
successfully loaded Triton and torch generators.

In
`@lightly_studio/src/lightly_studio/dataset/triton_mobileclip_embedding_generator.py`:
- Line 42: Update the embedding generator’s Triton RPCs to pass the configured
finite client_timeout to both get_model_metadata() during initialization and
infer() during batch embedding, ensuring neither call can wait indefinitely
while preserving the existing request behavior.

---

Nitpick comments:
In `@lightly_studio/.env.example`:
- Around line 17-22: Update the embedding environment variable comments to state
that LIGHTLY_STUDIO_EMBEDDINGS_MODEL_NAME must match the deployed Triton model
repository name when LIGHTLY_STUDIO_EMBEDDINGS_MODEL_TYPE=triton, while
retaining the existing torch model-name guidance.

In `@lightly_studio/src/lightly_studio/dataset/embedding_manager.py`:
- Around line 247-281: Add tests in test_embedding_manager.py covering the
chunked embed_images flow: verify kept_indices are offset to global sample IDs
across chunks, successful chunks are stored when another raises
AllInputFilesFailedError, and the error is re-raised only when every chunk fails
with an empty-embeddings fallback. Use mocks for model.embed_images and
_store_embeddings.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 71447df4-a94f-429a-955f-f914ac402434

📥 Commits

Reviewing files that changed from the base of the PR and between c0b0993 and 1cf18be.

⛔ Files ignored due to path filters (3)
  • lightly_studio/uv.lock is excluded by !**/*.lock
  • tritonserver/server_trt/__pycache__/dali_pipeline.cpython-37.pyc is excluded by !**/*.pyc
  • tritonserver/server_trt/model_repository/mobileclip_s0/1/__pycache__/model.cpython-37.pyc is excluded by !**/*.pyc
📒 Files selected for processing (22)
  • .gitignore
  • lightly_studio/.env.example
  • lightly_studio/docs/docs/tutorials/yolo26-model-evaluation.md
  • lightly_studio/pyproject.toml
  • lightly_studio/src/lightly_studio/api/routes/api/enterprise.py
  • lightly_studio/src/lightly_studio/dataset/embedding_generator.py
  • lightly_studio/src/lightly_studio/dataset/embedding_manager.py
  • lightly_studio/src/lightly_studio/dataset/env.py
  • lightly_studio/src/lightly_studio/dataset/mobileclip_embedding_generator.py
  • lightly_studio/src/lightly_studio/dataset/perception_encoder_embedding_generator.py
  • lightly_studio/src/lightly_studio/dataset/triton_mobileclip_embedding_generator.py
  • lightly_studio/src/lightly_studio/examples/example_custom_embedding_model.py
  • lightly_studio/tests/api/routes/api/test_enterprise.py
  • lightly_studio/tests/dataset/test_embedding_manager.py
  • lightly_studio/tests/dataset/test_fsspec_lister.py
  • lightly_studio/tests/dataset/test_image_crop_embedding.py
  • lightly_studio/tests/dataset/test_image_embedding.py
  • lightly_studio/tests/dataset/test_mobileclip_embedding_generator.py
  • lightly_studio/tests/dataset/test_perception_encoder_embedding_generator.py
  • lightly_studio/tests/dataset/test_triton_mobileclip_embedding_generator.py
  • lightly_studio_view/src/lib/components/PlotPanel/SearchMarkerOverlay.ts
  • lightly_studio_view/src/lib/hooks/useEmbeddingProjection/useEmbeddingProjection.ts

Comment thread lightly_studio/pyproject.toml Outdated
Comment thread lightly_studio/src/lightly_studio/dataset/embedding_manager.py
@liopeer liopeer changed the title Make embedding model switchable DRAFT: Make embedding model switchable Jul 22, 2026
@liopeer

liopeer commented Jul 22, 2026

Copy link
Copy Markdown
Author

/review

@liopeer liopeer mentioned this pull request Jul 23, 2026
2 tasks
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