ms-swift
ms-swift copied to clipboard
Add Tensor Input Support: Enable .pt file processing with <tensor> tags for latent representations
PR type
- [ ] Bug Fix
- [x] New Feature
- [ ] Document Updates
- [ ] More Models or Datasets Support
PR information
Write the detail information belongs to this PR.
Experiment results
Paste your experiment result here(if needed).
Summary of Changes
Hello @Marshall-mk, I'm Gemini Code Assist[^1]! 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 enhances the ms-swift framework by adding native support for PyTorch tensor files (.pt) as a new multimodal input. This feature allows for greater flexibility in handling specialized data, such as pre-processed medical scans, scientific data, or custom feature representations, by seamlessly integrating them into the existing multimodal processing pipeline. The change includes updates to the core processing logic, data structures, and comprehensive documentation to guide users on its implementation and usage.
Highlights
- Tensor Input Support: Introduced the ability to process PyTorch tensor files (.pt) as a new multimodal input type, alongside images, videos, and audios.
- Flexible Tagging and Loading: Users can now specify tensor inputs using
<tensor>tags within messages and provide file paths via a newtensorsparameter inInferRequestand dataset formats. - Automatic Tensor-to-Image Conversion: Tensors are automatically loaded and converted into PIL Images for model processing, supporting various shapes (2D, 3D, 4D) and channel formats (grayscale, RGB), with proper normalization.
- Comprehensive Documentation: Added a new dedicated documentation page (
Tensor-support.md) detailing the overview, supported formats, usage examples, dataset integration, creation of tensor files, technical details, use cases, and limitations of the tensor input feature. - Framework Integration and Testing: Integrated tensor support into the dataset preprocessor, template system (special tokens, keys, placeholders), and added extensive unit tests to ensure robust functionality.
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 :thumbsup: and :thumbsdown: 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.
[^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.
Hello. Thanks for your PR. Could you tell me what additional advantages using <tensor> has compared to using <image> ?
Hello. Thanks for your PR. Could you tell me what additional advantages using
<tensor>has compared to using<image>?
The key difference here is that you can't load tensors (.pt files) using the
You might be wondering why we’d want to load tensors at all. In my case, I work with medical images such as CT or MRI scans, which often contain multiple slices and have large dimensions. These images make training VLMs very compute-intensive. To efficiently process these images, we need to compress them using an encoder, save the resulting embeddings as tensors, and then use the VLMs available in ms-swift.
Now, a naive approach would be to convert the embeddings (the tensors) into videos (from .pt files to .mp4), but that introduces the need for additional normalizations and scaling, which actually leads to some disparity in the actual embeddings. This is why using