Skip to content

Conversation

@Marshall-mk
Copy link

PR type

  • Bug Fix
  • 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).

Marshall-mk and others added 5 commits July 15, 2025 17:34
This commit fixes two critical bugs that would prevent tensor input functionality from working:

1. Missing tensor_idx initialization in StdTemplateInputs.__post_init__
   - Added tensor_idx = 0 initialization (line 122)
   - Without this, the code would fail when trying to access tensor_idx during template processing

2. Missing tensor list handling in StdTemplateInputs.__post_init__
   - Added check to convert tensors to list if not already one (lines 131-132)
   - This ensures consistency with how images, videos, and audios are handled

3. Missing tensor support in dataset preprocessor
   - Added 'tensors' to RowPreprocessor.standard_keys (line 24)
   - Added tensors_keys mapping for 'tensors' and 'tensor' (line 39)
   - Added 'tensors' to mm_type loop (line 40)
   - This is critical for dataset loading to work with tensor files

These fixes ensure that tensor inputs are properly initialized and handled throughout the template processing pipeline, matching the existing patterns for images, videos, and audios.
This commit adds:

1. Unit Tests (tests/test_align/test_template/test_tensor.py):
   - test_load_tensor: Tests basic tensor loading from .pt files
   - test_load_batch_tensor: Tests batch tensor loading (4D tensors)
   - test_tensor_to_images_single: Tests single tensor to PIL Image conversion
   - test_tensor_to_images_batch: Tests batch tensor to PIL Images conversion
   - test_tensor_to_images_grayscale: Tests grayscale tensor conversion
   - test_tensor_to_images_2d: Tests 2D tensor conversion
   - test_tensor_normalization: Tests proper value normalization [0,1] -> [0,255]
   - test_template_inputs_with_tensors: Tests StdTemplateInputs tensor support
   - test_infer_request_with_tensors: Tests InferRequest tensor support
   - test_mixed_media: Tests tensors with images, videos, audios
   - test_dataset_preprocessor_tensor_support: Tests dataset preprocessor
   - test_special_tokens: Tests tensor special tokens registration

   Total: 12 comprehensive test cases covering all tensor functionality

2. Documentation (docs/source_en/Customization/Tensor-support.md):
   - Complete guide to tensor input support
   - Supported tensor formats and shapes
   - Usage examples (basic, multiple tensors, mixed media)
   - Dataset format examples for all training modes
   - Command line usage
   - Creating tensor files (with medical imaging example)
   - Custom template implementation
   - Technical details and troubleshooting
   - Use cases: medical report generation, scientific data analysis

3. Documentation Updates (docs/source_en/Customization/Custom-dataset.md):
   - Added tensors to standard dataset keys documentation
   - Added <tensor> tag to multimodal section
   - Added tensor examples for pre-training and supervised fine-tuning
   - Added reference link to detailed tensor documentation

The tests follow the existing test patterns in ms-swift and cover:
- Tensor loading (single, batch)
- Tensor-to-image conversion (RGB, grayscale, 2D, 3D, 4D)
- Template inputs integration
- Dataset preprocessor integration
- Mixed multimodal usage

The documentation provides comprehensive guidance for users wanting to:
- Train models with tensor inputs
- Use tensors for medical imaging, scientific data, or custom features
- Integrate tensors with existing multimodal pipelines
@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 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 new tensors parameter in InferRequest and 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 👍 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 support for tensor inputs (.pt files), which is a great new feature for handling specialized data like medical scans. The implementation is thorough, covering data preprocessing, template handling, and tensor-to-image conversion logic. The addition of comprehensive documentation and a full suite of unit tests is excellent and greatly improves the quality of the contribution.

My review includes one suggestion for improving code maintainability by removing redundant imports. Overall, this is a high-quality pull request.

Comment on lines +1356 to +1357
from PIL import Image
import numpy as np
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The import from PIL import Image is redundant, as Image is already imported at the module level (line 20). Additionally, import numpy as np is duplicated in the _single_tensor_to_image method. To improve maintainability, you could remove the redundant Image import. For numpy, you could import it once at the top of the file and remove the local imports.

Comment on lines +1376 to +1377
from PIL import Image
import numpy as np
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The import from PIL import Image is redundant as Image is already imported at the module level (line 20). Additionally, import numpy as np is duplicated in the _tensor_to_images method. To improve maintainability, you could remove the redundant Image import. For numpy, you could import it once at the top of the file and remove the local imports.

@Jintao-Huang
Copy link
Collaborator

Hello. Thanks for your PR. Could you tell me what additional advantages using <tensor> has compared to using <image> ?

@Marshall-mk
Copy link
Author

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 or

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 directly is a better approach, as it avoids the unnecessary conversion and preserves the embeddings.

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.

3 participants