Skip to content

Conversation

@Marshall-mk
Copy link

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

Add Tensor Input Support for Latent Representations

Motivation

This PR adds support for loading and processing PyTorch tensor files (.pt format) using <tensor> tags, similar to how <image> and <video> tags work in MS-Swift. This enables training VLMs with pre-computed embeddings and latent representations from encoder models.

Key Use Case: Latent-VLM Architecture

This feature was developed for my latent-vlm project, which uses pre-encoded latent representations instead of raw images/videos. Benefits include:

  1. Resource Efficiency: Process pre-computed embeddings from encoder models (e.g., VAE, vision encoders) instead of raw high-dimensional data
  2. Dimensionality Reduction: Use compressed latent representations, significantly reducing memory footprint and computational costs
  3. Medical Imaging Applications: Store and process medical scans as normalized tensors while maintaining patient privacy (my use case)

Instead of storing and processing raw images/videos repeatedly during training, you can:

  • Pre-encode data once using your encoder model
  • Save as .pt tensor files
  • Train VLM directly on latent representations using <tensor> tags
  • Reduce GPU memory usage and training time significantly

Core Idea

Extend MS-Swift's multimodal support to handle tensor files the same way it handles images and videos:

Before (Images/Videos only):

{"messages": [...], "images": ["image.jpg"], "videos": ["video.mp4"]}

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Marshall-mk, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly extends MS-Swift's multimodal capabilities by introducing native support for PyTorch tensor files. This feature allows users to leverage pre-computed latent representations or specialized data formats, such as medical scans, directly within their VLM training and inference workflows. By treating tensors as another media type, the system can now process these inputs efficiently, converting them into images for existing model architectures. The changes are accompanied by thorough documentation and practical examples to guide users in adopting this new functionality.

Highlights

  • Tensor Input Support: Introduced the ability to load and process PyTorch tensor files (.pt format) using new <tensor> tags, similar to existing <image> and <video> tags.
  • Enhanced Multimodal Capabilities: MS-Swift now supports training Vision-Language Models (VLMs) with pre-computed embeddings and latent representations, offering benefits in resource efficiency and dimensionality reduction.
  • Comprehensive Documentation: Added a new dedicated documentation page (Tensor-support.md) detailing the overview, supported tensor formats, usage examples, dataset integration, technical specifics, and limitations of the tensor input feature.
  • Example Scripts: Provided two new example scripts (tensor_example.py and tensor_template_example.py) to demonstrate basic tensor usage, integration within messages, mixed media scenarios, and custom template creation for tensor-to-image conversion.
  • Core System Integration: Updated the dataset preprocessor, template system (including special tokens and placeholders), and input request handling to seamlessly recognize and process tensor data, converting them to PIL Images for model consumption.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a significant and well-implemented feature for supporting PyTorch tensors as inputs, which is particularly useful for VLM training with pre-computed embeddings. The changes are comprehensive, covering core logic, extensive documentation, helpful examples, and thorough tests. The code is well-structured and follows the existing patterns in the codebase. I've identified one area for improvement in the tensor normalization logic to enhance its robustness against out-of-range values, which could prevent silent data corruption and improve the user experience. Overall, this is an excellent contribution to the project.

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.

1 participant