first commit

This commit is contained in:
xxl 2025-02-28 11:09:24 +08:00
parent 705bf4d1a9
commit 6f4d0dbe31
51 changed files with 212616 additions and 2 deletions

9
CODE_OF_CONDUCT.md Normal file
View File

@ -0,0 +1,9 @@
# Microsoft Open Source Code of Conduct
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
Resources:
- [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/)
- [Microsoft Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/)
- Contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with questions or concerns

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) Microsoft Corporation.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE

645
README.md
View File

@ -1,3 +1,644 @@
# Phi-4-multimodal-instruct
---
license: mit
license_link: https://huggingface.co/microsoft/Phi-4-multimodal-instruct/resolve/main/LICENSE
language:
- multilingual
- ar
- zh
- cs
- da
- nl
- en
- fi
- fr
- de
- he
- hu
- it
- ja
- ko
- no
- pl
- pt
- ru
- es
- sv
- th
- tr
- uk
tags:
- nlp
- code
- audio
- automatic-speech-recognition
- speech-summarization
- speech-translation
- visual-question-answering
- phi-4-multimodal
- phi
- phi-4-mini
widget:
- example_title: Librispeech sample 1
src: https://cdn-media.huggingface.co/speech_samples/sample1.flac
- example_title: Librispeech sample 2
src: https://cdn-media.huggingface.co/speech_samples/sample2.flac
- messages:
- role: user
content: Can you provide ways to eat combinations of bananas and dragonfruits?
library_name: transformers
---
Phi-4-multimodal-instruct
## Model Summary
Phi-4-multimodal-instruct is a lightweight open multimodal foundation
model that leverages the language, vision, and speech research
and datasets used for Phi-3.5 and 4.0 models. The model processes text,
image, and audio inputs, generating text outputs, and comes with
128K token context length. The model underwent an enhancement process,
incorporating both supervised fine-tuning, direct preference
optimization and RLHF (Reinforcement Learning from Human Feedback)
to support precise instruction adherence and safety measures.
The languages that each modal supports are the following:
- Text: Arabic, Chinese, Czech, Danish, Dutch, English, Finnish,
French, German, Hebrew, Hungarian, Italian, Japanese, Korean, Norwegian,
Polish, Portuguese, Russian, Spanish, Swedish, Thai, Turkish, Ukrainian
- Vision: English
- Audio: English, Chinese, German, French, Italian, Japanese, Spanish, Portuguese
📰 [Phi-4-multimodal Microsoft Blog](https://aka.ms/phi4-feb2025) <br>
📖 [Phi-4-multimodal Technical Report](https://aka.ms/phi-4-multimodal/techreport) <br>
🏡 [Phi Portal](https://aka.ms/phi-4-multimodal/azure) <br>
👩‍🍳 [Phi Cookbook](https://github.com/microsoft/PhiCookBook) <br>
🖥️ Try It on [Azure](https://aka.ms/phi-4-multimodal/azure), [Nvidia Playgroud](https://aka.ms/phi-4-multimodal/nvidia) <br>
📱Huggingface Spaces
[Thoughts Organizer](https://huggingface.co/spaces/microsoft/ThoughtsOrganizer),
[Stories Come Alive](https://huggingface.co/spaces/microsoft/StoriesComeAlive),
[Phine Speech Translator](https://huggingface.co/spaces/microsoft/PhineSpeechTranslator) <br>
**Phi-4**: [[multimodal-instruct](https://huggingface.co/microsoft/Phi-4-multimodal-instruct) | [onnx](https://huggingface.co/microsoft/Phi-4-multimodal-instruct-onnx)]; [mini-instruct](https://huggingface.co/microsoft/Phi-4-mini-instruct);
Watch as Phi-4 Multimodal analyzes spoken language to help plan a trip to Seattle, demonstrating its advanced audio processing and recommendation capabilities.
<div style="width: 800px; height: 400px; margin: 0 auto;">
<video autoplay muted loop controls playsinline style="width: 100%; height: 100%; object-fit: contain;">
<source src="https://phi4releasestorage.blob.core.windows.net/demo/Phi-4-multimodal_SeattleTrip.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
See how Phi-4 Multimodal tackles complex mathematical problems through visual inputs, demonstrating its ability to process and solve equations presented in images.
<div style="width: 800px; height: 400px; margin: 0 auto;">
<video autoplay muted loop controls playsinline style="width: 100%; height: 100%; object-fit: contain;">
<source src="https://phi4releasestorage.blob.core.windows.net/demo/Phi-4-multimodal_Math.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
Explore how Phi-4 Mini functions as an intelligent agent, showcasing its reasoning and task execution abilities in complex scenarios.
<div style="width: 800px; height: 400px; margin: 0 auto;">
<video autoplay muted loop controls playsinline style="width: 100%; height: 100%; object-fit: contain;">
<source src="https://phi4releasestorage.blob.core.windows.net/demo/Phi-4-mini_Agents.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
## Intended Uses
### Primary Use Cases
The model is intended for broad multilingual and multimodal commercial and research use . The model provides uses for general purpose AI systems and applications which require
1) Memory/compute constrained environments
2) Latency bound scenarios
3) Strong reasoning (especially math and logic)
4) Function and tool calling
5) General image understanding
6) Optical character recognition
7) Chart and table understanding
8) Multiple image comparison
9) Multi-image or video clip summarization
10) Speech recognition
11) Speech translation
12) Speech QA
13) Speech summarization
14) Audio understanding
The model is designed to accelerate research on language and multimodal models, for use as a building block for generative AI powered features.
### Use Case Considerations
The model is not specifically designed or evaluated for all downstream purposes. Developers should consider common limitations of language models and multimodal models, as well as performance difference across languages, as they select use cases, and evaluate and mitigate for accuracy, safety, and fairness before using within a specific downstream use case, particularly for high-risk scenarios.
Developers should be aware of and adhere to applicable laws or regulations (including but not limited to privacy, trade compliance laws, etc.) that are relevant to their use case.
***Nothing contained in this Model Card should be interpreted as or deemed a restriction or modification to the license the model is released under.***
## Release Notes
This release of Phi-4-multimodal-instruct is based on valuable user feedback from the Phi-3 series. Previously, users could use a speech recognition model to talk to the Mini and Vision models. To achieve this, users needed to use a pipeline of two models: one model to transcribe the audio to text, and another model for the language or vision tasks. This pipeline means that the core model was not provided the full breadth of input information e.g. cannot directly observe multiple speakers, background noises, jointly align speech, vision, language information at the same time on the same representation space.
With Phi-4-multimodal-instruct, a single new open model has been trained across text, vision, and audio, meaning that all inputs and outputs are processed by the same neural network. The model employed new architecture, larger vocabulary for efficiency, multilingual, and multimodal support, and better post-training techniques were used for instruction following and function calling, as well as additional data leading to substantial gains on key multimodal capabilities.
It is anticipated that Phi-4-multimodal-instruct will greatly benefit app developers and various use cases. The enthusiastic support for the Phi-4 series is greatly appreciated. Feedback on Phi-4 is welcomed and crucial to the model's evolution and improvement. Thank you for being part of this journey!
## Model Quality
To understand the capabilities, Phi-4-multimodal-instruct was compared with a set of models over a variety of benchmarks using an internal benchmark platform (See Appendix A for benchmark methodology). Users can refer to the Phi-4-Mini-Instruct model card for details of language benchmarks. At the high-level overview of the model quality on representative speech and vision benchmarks:
### Speech
The Phi-4-multimodal-instruct was observed as
- Having strong automatic speech recognition (ASR) and speech translation (ST) performance, surpassing expert ASR model WhisperV3 and ST models SeamlessM4T-v2-Large.
- Ranking number 1 on the Huggingface OpenASR leaderboard with word error rate 6.14% in comparison with the current best model 6.5% as of Jan 17, 2025.
- Being the first open-sourced model that can perform speech summarization, and the performance is close to GPT4o.
- Having a gap with close models, e.g. Gemini-1.5-Flash and GPT-4o-realtime-preview, on speech QA task. Work is being undertaken to improve this capability in the next iterations.
#### Speech Recognition (lower is better)
The performance of Phi-4-multimodal-instruct on the aggregated benchmark datasets:
![alt text](./figures/speech_recognition.png)
The performance of Phi-4-multimodal-instruct on different languages, averaging the WERs of CommonVoice and FLEURS:
![alt text](./figures/speech_recog_by_lang.png)
#### Speech Translation (higher is better)
Translating from German, Spanish, French, Italian, Japanese, Portugues, Chinese to English:
![alt text](./figures/speech_translate.png)
Translating from English to German, Spanish, French, Italian, Japanese, Portugues, Chinese. Noted that WhiperV3 does not support this capability:
![alt text](./figures/speech_translate_2.png)
#### Speech Summarization (higher is better)
![alt text](./figures/speech_summarization.png)
#### Speech QA
MT bench scores are scaled by 10x to match the score range of MMMLU:
![alt text](./figures/speech_qa.png)
#### Audio Understanding
AIR bench scores are scaled by 10x to match the score range of MMAU:
![alt text](./figures/audio_understand.png)
### Vision
#### Vision-Speech tasks
Phi-4-multimodal-instruct is capable of processing both image and audio together, the following table shows the model quality when the input query for vision content is synthetic speech on chart/table understanding and document reasoning tasks. Compared to other existing state-of-the-art omni models that can enable audio and visual signal as input, Phi-4-multimodal-instruct achieves much stronger performance on multiple benchmarks.
| Benchmarks | Phi-4-multimodal-instruct | InternOmni-7B | Gemini-2.0-Flash-Lite-prv-02-05 | Gemini-2.0-Flash | Gemini-1.5-Pro |
|-----------------------|--------------------------|---------------|--------------------------------|-----------------|----------------|
| s_AI2D | **68.9** | 53.9 | 62.0 | **69.4** | 67.7 |
| s_ChartQA | **69.0** | 56.1 | 35.5 | 51.3 | 46.9 |
| s_DocVQA | **87.3** | 79.9 | 76.0 | 80.3 | 78.2 |
| s_InfoVQA | **63.7** | 60.3 | 59.4 | 63.6 | **66.1** |
| **Average** | **72.2** | **62.6** | **58.2** | **66.2** | **64.7** |
### Vision tasks
To understand the vision capabilities, Phi-4-multimodal-instruct was compared with a set of models over a variety of zero-shot benchmarks using an internal benchmark platform. At the high-level overview of the model quality on representative benchmarks:
| Dataset | Phi-4-multimodal-ins | Phi-3.5-vision-ins | Qwen 2.5-VL-3B-ins | Intern VL 2.5-4B | Qwen 2.5-VL-7B-ins | Intern VL 2.5-8B | Gemini 2.0-Flash Lite-preview-0205 | Gemini2.0-Flash | Claude-3.5-Sonnet-2024-10-22 | Gpt-4o-2024-11-20 |
|----------------------------------|---------------------|-------------------|-------------------|-----------------|-------------------|-----------------|--------------------------------|-----------------|----------------------------|------------------|
| **Popular aggregated benchmark** | | | | | | | | | | |
| MMMU | **55.1** | 43.0 | 47.0 | 48.3 | 51.8 | 50.6 | 54.1 | **64.7** | 55.8 | 61.7 |
| MMBench (dev-en) | **86.7** | 81.9 | 84.3 | 86.8 | 87.8 | 88.2 | 85.0 | **90.0** | 86.7 | 89.0 |
| MMMU-Pro (std/vision) | **38.5** | 21.8 | 29.9 | 32.4 | 36.9 | 34.4 | 45.1 | **54.4** | 54.3 | 53.0 |
| **Visual science reasoning** | | | | | | | | | | |
| ScienceQA Visual (img-test) | **97.5** | 91.3 | 79.4 | 96.2 | 87.7 | **97.3** | 85.0 | 88.3 | 81.2 | 88.2 |
| **Visual math reasoning** | | | | | | | | | | |
| MathVista (testmini) | **62.4** | 43.9 | 60.8 | 51.2 | **67.8** | 56.7 | 57.6 | 47.2 | 56.9 | 56.1 |
| InterGPS | **48.6** | 36.3 | 48.3 | 53.7 | 52.7 | 54.1 | 57.9 | **65.4** | 47.1 | 49.1 |
| **Chart & table reasoning** | | | | | | | | | | |
| AI2D | **82.3** | 78.1 | 78.4 | 80.0 | 82.6 | 83.0 | 77.6 | 82.1 | 70.6 | **83.8** |
| ChartQA | **81.4** | 81.8 | 80.0 | 79.1 | **85.0** | 81.0 | 73.0 | 79.0 | 78.4 | 75.1 |
| DocVQA | **93.2** | 69.3 | 93.9 | 91.6 | **95.7** | 93.0 | 91.2 | 92.1 | 95.2 | 90.9 |
| InfoVQA | **72.7** | 36.6 | 77.1 | 72.1 | **82.6** | 77.6 | 73.0 | 77.8 | 74.3 | 71.9 |
| **Document Intelligence** | | | | | | | | | | |
| TextVQA (val) | **75.6** | 72.0 | 76.8 | 70.9 | **77.7** | 74.8 | 72.9 | 74.4 | 58.6 | 73.1 |
| OCR Bench | **84.4** | 63.8 | 82.2 | 71.6 | **87.7** | 74.8 | 75.7 | 81.0 | 77.0 | 77.7 |
| **Object visual presence verification** | | | | | | | | | | |
| POPE | **85.6** | 86.1 | 87.9 | 89.4 | 87.5 | **89.1** | 87.5 | 88.0 | 82.6 | 86.5 |
| **Multi-image perception** | | | | | | | | | | |
| BLINK | **61.3** | 57.0 | 48.1 | 51.2 | 55.3 | 52.5 | 59.3 | **64.0** | 56.9 | 62.4 |
| Video MME 16 frames | **55.0** | 50.8 | 56.5 | 57.3 | 58.2 | 58.7 | 58.8 | 65.5 | 60.2 | **68.2** |
| **Average** | **72.0** | **60.9** | **68.7** | **68.8** | **73.1** | **71.1** | **70.2** | **74.3** | **69.1** | **72.4** |
![alt text](./figures/vision_radar.png)
#### Visual Perception
Below are the comparison results on existing multi-image tasks. On average, Phi-4-multimodal-instruct outperforms competitor models of the same size and competitive with much bigger models on multi-frame capabilities.
BLINK is an aggregated benchmark with 14 visual tasks that humans can solve very quickly but are still hard for current multimodal LLMs.
| Dataset | Phi-4-multimodal-instruct | Qwen2.5-VL-3B-Instruct | InternVL 2.5-4B | Qwen2.5-VL-7B-Instruct | InternVL 2.5-8B | Gemini-2.0-Flash-Lite-prv-02-05 | Gemini-2.0-Flash | Claude-3.5-Sonnet-2024-10-22 | Gpt-4o-2024-11-20 |
|----------------------------|--------------------------|----------------------|-----------------|----------------------|-----------------|--------------------------------|-----------------|----------------------------|------------------|
| Art Style | **86.3** | 58.1 | 59.8 | 65.0 | 65.0 | 76.9 | 76.9 | 68.4 | 73.5 |
| Counting | **60.0** | 67.5 | 60.0 | 66.7 | **71.7** | 45.8 | 69.2 | 60.8 | 65.0 |
| Forensic Detection | **90.2** | 34.8 | 22.0 | 43.9 | 37.9 | 31.8 | 74.2 | 63.6 | 71.2 |
| Functional Correspondence | **30.0** | 20.0 | 26.9 | 22.3 | 27.7 | 48.5 | **53.1** | 34.6 | 42.3 |
| IQ Test | **22.7** | 25.3 | 28.7 | 28.7 | 28.7 | 28.0 | **30.7** | 20.7 | 25.3 |
| Jigsaw | **68.7** | 52.0 | **71.3** | 69.3 | 53.3 | 62.7 | 69.3 | 61.3 | 68.7 |
| Multi-View Reasoning | **76.7** | 44.4 | 44.4 | 54.1 | 45.1 | 55.6 | 41.4 | 54.9 | 54.1 |
| Object Localization | **52.5** | 55.7 | 53.3 | 55.7 | 58.2 | 63.9 | **67.2** | 58.2 | 65.6 |
| Relative Depth | **69.4** | 68.5 | 68.5 | 80.6 | 76.6 | **81.5** | 72.6 | 66.1 | 73.4 |
| Relative Reflectance | **26.9** | **38.8** | **38.8** | 32.8 | **38.8** | 33.6 | 34.3 | 38.1 | 38.1 |
| Semantic Correspondence | **52.5** | 32.4 | 33.8 | 28.8 | 24.5 | **56.1** | 55.4 | 43.9 | 47.5 |
| Spatial Relation | **72.7** | 80.4 | 86.0 | **88.8** | 86.7 | 74.1 | 79.0 | 74.8 | 83.2 |
| Visual Correspondence | **67.4** | 28.5 | 39.5 | 50.0 | 44.2 | 84.9 | **91.3** | 72.7 | 82.6 |
| Visual Similarity | **86.7** | 67.4 | 88.1 | 87.4 | 85.2 | **87.4** | 80.7 | 79.3 | 83.0 |
| **Overall** | **61.6** | **48.1** | **51.2** | **55.3** | **52.5** | **59.3** | **64.0** | **56.9** | **62.4** |
![alt text](./figures/multi_image.png)
## Usage
### Requirements
Phi-4 family has been integrated in the `4.48.2` version of `transformers`. The current `transformers` version can be verified with: `pip list | grep transformers`.
Examples of required packages:
```
flash_attn==2.7.4.post1
torch==2.6.0
transformers==4.48.2
accelerate==1.3.0
soundfile==0.13.1
pillow==11.1.0
scipy==1.15.2
torchvision==0.21.0
backoff==2.2.1
peft==0.13.2
```
Phi-4-multimodal-instruct is also available in [Azure AI Studio](https://aka.ms/phi-4-multimodal/azure)
### Tokenizer
Phi-4-multimodal-instruct supports a vocabulary size of up to `200064` tokens. The [tokenizer files](https://huggingface.co/microsoft/Phi-4-multimodal-instruct/blob/main/added_tokens.json) already provide placeholder tokens that can be used for downstream fine-tuning, but they can also be extended up to the model's vocabulary size.
### Input Formats
Given the nature of the training data, the Phi-4-multimodal-instruct model is best suited for prompts using the chat format as follows:
#### Text chat format
This format is used for general conversation and instructions:
`
<|system|>You are a helpful assistant.<|end|><|user|>How to explain Internet for a medieval knight?<|end|><|assistant|>
`
#### Tool-enabled function-calling format
This format is used when the user wants the model to provide function calls based on
the given tools. The user should provide the available tools in the system prompt,
wrapped by <|tool|> and <|/tool|> tokens. The tools should be specified in JSON format,
using a JSON dump structure. Example:
`
<|system|>You are a helpful assistant with some tools.<|tool|>[{"name": "get_weather_updates", "description": "Fetches weather updates for a given city using the RapidAPI Weather API.", "parameters": {"city": {"description": "The name of the city for which to retrieve weather information.", "type": "str", "default": "London"}}}]<|/tool|><|end|><|user|>What is the weather like in Paris today?<|end|><|assistant|>
`
#### Vision-Language Format
This format is used for conversation with image:
`
<|user|><|image_1|>Describe the image in detail.<|end|><|assistant|>
`
For multiple images, the user needs to insert multiple image placeholders in the prompt as below:
`
<|user|><|image_1|><|image_2|><|image_3|>Summarize the content of the images.<|end|><|assistant|>
`
#### Speech-Language Format
This format is used for various speech and audio tasks:
`
<|user|><|audio_1|>{task prompt}<|end|><|assistant|>
`
The task prompt can vary for different task.
Automatic Speech Recognition:
`
<|user|><|audio_1|>Transcribe the audio clip into text.<|end|><|assistant|>
`
Automatic Speech Translation:
`
<|user|><|audio_1|>Translate the audio to {lang}.<|end|><|assistant|>
`
Automatic Speech Translation with chain-of-thoughts:
`
<|user|><|audio_1|>Transcribe the audio to text, and then translate the audio to {lang}. Use <sep> as a separator between the original transcript and the translation.<|end|><|assistant|>
`
Spoken-query Question Answering:
`
<|user|><|audio_1|><|end|><|assistant|>
`
#### Vision-Speech Format
This format is used for conversation with image and audio.
The audio may contain query related to the image:
`
<|user|><|image_1|><|audio_1|><|end|><|assistant|>
`
For multiple images, the user needs to insert multiple image placeholders in the prompt as below:
`
<|user|><|image_1|><|image_2|><|image_3|><|audio_1|><|end|><|assistant|>
`
**Vision**
- Any common RGB/gray image format (e.g., (".jpg", ".jpeg", ".png", ".ppm", ".bmp", ".pgm", ".tif", ".tiff", ".webp")) can be supported.
- Resolution depends on the GPU memory size. Higher resolution and more images will produce more tokens, thus using more GPU memory. During training, 64 crops can be supported.
If it is a square image, the resolution would be around (8*448 by 8*448). For multiple-images, at most 64 frames can be supported, but with more frames as input, the resolution of each frame needs to be reduced to fit in the memory.
**Audio**
- Any audio format that can be loaded by soundfile package should be supported.
- To keep the satisfactory performance, maximum audio length is suggested to be 40s. For summarization tasks, the maximum audio length is suggested to 30 mins.
### Loading the model locally
After obtaining the Phi-4-multimodal-instruct model checkpoints, users can use this sample code for inference.
```python
import requests
import torch
import os
import io
from PIL import Image
import soundfile as sf
from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
from urllib.request import urlopen
# Define model path
model_path = "microsoft/Phi-4-multimodal-instruct"
# Load model and processor
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_path,
device_map="cuda",
torch_dtype="auto",
trust_remote_code=True,
attn_implementation='flash_attention_2',
).cuda()
# Load generation config
generation_config = GenerationConfig.from_pretrained(model_path)
# Define prompt structure
user_prompt = '<|user|>'
assistant_prompt = '<|assistant|>'
prompt_suffix = '<|end|>'
# Part 1: Image Processing
print("\n--- IMAGE PROCESSING ---")
image_url = 'https://www.ilankelman.org/stopsigns/australia.jpg'
prompt = f'{user_prompt}<|image_1|>What is shown in this image?{prompt_suffix}{assistant_prompt}'
print(f'>>> Prompt\n{prompt}')
# Download and open image
image = Image.open(requests.get(image_url, stream=True).raw)
inputs = processor(text=prompt, images=image, return_tensors='pt').to('cuda:0')
# Generate response
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')
# Part 2: Audio Processing
print("\n--- AUDIO PROCESSING ---")
audio_url = "https://upload.wikimedia.org/wikipedia/commons/b/b0/Barbara_Sahakian_BBC_Radio4_The_Life_Scientific_29_May_2012_b01j5j24.flac"
speech_prompt = "Transcribe the audio to text, and then translate the audio to French. Use <sep> as a separator between the original transcript and the translation."
prompt = f'{user_prompt}<|audio_1|>{speech_prompt}{prompt_suffix}{assistant_prompt}'
print(f'>>> Prompt\n{prompt}')
# Downlowd and open audio file
audio, samplerate = sf.read(io.BytesIO(urlopen(audio_url).read()))
# Process with the model
inputs = processor(text=prompt, audios=[(audio, samplerate)], return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1]:]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')
```
## Responsible AI Considerations
Like other language models, the Phi family of models can potentially behave in ways that are unfair, unreliable, or offensive. Some of the limiting behaviors to be aware of include:
+ Quality of Service: The Phi models are trained primarily on English language content across text, speech, and visual inputs, with some additional multilingual coverage. Performance may vary significantly across different modalities and languages:
+ Text: Languages other than English will experience reduced performance, with varying levels of degradation across different non-English languages. English language varieties with less representation in the training data may perform worse than standard American English.
+ Speech: Speech recognition and processing shows similar language-based performance patterns, with optimal performance for standard American English accents and pronunciations. Other English accents, dialects, and non-English languages may experience lower recognition accuracy and response quality. Background noise, audio quality, and speaking speed can further impact performance.
+ Vision: Visual processing capabilities may be influenced by cultural and geographical biases in the training data. The model may show reduced performance when analyzing images containing text in non-English languages or visual elements more commonly found in non-Western contexts. Image quality, lighting conditions, and composition can also affect processing accuracy.
+ Multilingual performance and safety gaps: We believe it is important to make language models more widely available across different languages, but the Phi 4 models still exhibit challenges common across multilingual releases. As with any deployment of LLMs, developers will be better positioned to test for performance or safety gaps for their linguistic and cultural context and customize the model with additional fine-tuning and appropriate safeguards.
+ Representation of Harms & Perpetuation of Stereotypes: These models can over- or under-represent groups of people, erase representation of some groups, or reinforce demeaning or negative stereotypes. Despite safety post-training, these limitations may still be present due to differing levels of representation of different groups, cultural contexts, or prevalence of examples of negative stereotypes in training data that reflect real-world patterns and societal biases.
+ Inappropriate or Offensive Content: These models may produce other types of inappropriate or offensive content, which may make it inappropriate to deploy for sensitive contexts without additional mitigations that are specific to the case.
+ Information Reliability: Language models can generate nonsensical content or fabricate content that might sound reasonable but is inaccurate or outdated.
+ Limited Scope for Code: The majority of Phi 4 training data is based in Python and uses common packages such as "typing, math, random, collections, datetime, itertools". If the model generates Python scripts that utilize other packages or scripts in other languages, it is strongly recommended that users manually verify all API uses.
+ Long Conversation: Phi 4 models, like other models, can in some cases generate responses that are repetitive, unhelpful, or inconsistent in very long chat sessions in both English and non-English languages. Developers are encouraged to place appropriate mitigations, like limiting conversation turns to account for the possible conversational drift.
+ Inference of Sensitive Attributes: The Phi 4 models can sometimes attempt to infer sensitive attributes (such as personality characteristics, country of origin, gender, etc...) from the users voices when specifically asked to do so. Phi 4-multimodal-instruct is not designed or intended to be used as a biometric categorization system to categorize individuals based on their biometric data to deduce or infer their race, political opinions, trade union membership, religious or philosophical beliefs, sex life, or sexual orientation. This behavior can be easily and efficiently mitigated at the application level by a system message.
Developers should apply responsible AI best practices, including mapping, measuring, and mitigating risks associated with their specific use case and cultural, linguistic context. Phi 4 family of models are general purpose models. As developers plan to deploy these models for specific use cases, they are encouraged to fine-tune the models for their use case and leverage the models as part of broader AI systems with language-specific safeguards in place. Important areas for consideration include:
+ Allocation: Models may not be suitable for scenarios that could have consequential impact on legal status or the allocation of resources or life opportunities (ex: housing, employment, credit, etc.) without further assessments and additional debiasing techniques.
+ High-Risk Scenarios: Developers should assess the suitability of using models in high-risk scenarios where unfair, unreliable or offensive outputs might be extremely costly or lead to harm. This includes providing advice in sensitive or expert domains where accuracy and reliability are critical (ex: legal or health advice). Additional safeguards should be implemented at the application level according to the deployment context.
+ Misinformation: Models may produce inaccurate information. Developers should follow transparency best practices and inform end-users they are interacting with an AI system. At the application level, developers can build feedback mechanisms and pipelines to ground responses in use-case specific, contextual information, a technique known as Retrieval Augmented Generation (RAG).
+ Generation of Harmful Content: Developers should assess outputs for their context and use available safety classifiers or custom solutions appropriate for their use case.
+ Misuse: Other forms of misuse such as fraud, spam, or malware production may be possible, and developers should ensure that their applications do not violate applicable laws and regulations.
## Training
### Model
+ **Architecture:** Phi-4-multimodal-instruct has 5.6B parameters and is a multimodal transformer model. The model has the pretrained Phi-4-Mini-Instruct as the backbone language model, and the advanced encoders and adapters of vision and speech.<br>
+ **Inputs:** Text, image, and audio. It is best suited for prompts using the chat format.<br>
+ **Context length:** 128K tokens<br>
+ **GPUs:** 512 A100-80G<br>
+ **Training time:** 28 days<br>
+ **Training data:** 5T tokens, 2.3M speech hours, and 1.1T image-text tokens<br>
+ **Outputs:** Generated text in response to the input<br>
+ **Dates:** Trained between December 2024 and January 2025<br>
+ **Status:** This is a static model trained on offline datasets with the cutoff date of June 2024 for publicly available data.<br>
+ **Supported languages:**
+ Text: Arabic, Chinese, Czech, Danish, Dutch, English, Finnish, French, German, Hebrew, Hungarian, Italian, Japanese, Korean, Norwegian, Polish, Portuguese, Russian, Spanish, Swedish, Thai, Turkish, Ukrainian<br>
+ Vision: English<br>
+ Audio: English, Chinese, German, French, Italian, Japanese, Spanish, Portuguese<br>
+ **Release date:** February 2025<br>
### Training Datasets
Phi-4-multimodal-instruct's training data includes a wide variety of sources, totaling 5 trillion text tokens, and is a combination of
1) publicly available documents filtered for quality, selected high-quality educational data, and code
2) newly created synthetic, “textbook-like” data for the purpose of teaching math, coding, common sense reasoning, general knowledge of the world (e.g., science, daily activities, theory of mind, etc.)
3) high quality human labeled data in chat format
4) selected high-quality image-text interleave data
5) synthetic and publicly available image, multi-image, and video data
6) anonymized in-house speech-text pair data with strong/weak transcriptions
7) selected high-quality publicly available and anonymized in-house speech data with task-specific supervisions
8) selected synthetic speech data
9) synthetic vision-speech data.
Focus was placed on the quality of data that could potentially improve the reasoning ability for the model, and the publicly available documents were filtered to contain a preferred level of knowledge. As an example, the result of a game in premier league on a particular day might be good training data for large foundation models, but such information was removed for the Phi-4-multimodal-instruct to leave more model capacity for reasoning for the model's small size. The data collection process involved sourcing information from publicly available documents, with a focus on filtering out undesirable documents and images. To safeguard privacy, image and text data sources were filtered to remove or scrub potentially personal data from the training data.
The decontamination process involved normalizing and tokenizing the dataset, then generating and comparing n-grams between the target dataset and benchmark datasets. Samples with matching n-grams above a threshold were flagged as contaminated and removed from the dataset. A detailed contamination report was generated, summarizing the matched text, matching ratio, and filtered results for further analysis.
### Fine-tuning
A basic example of supervised fine-tuning (SFT) for [speech](https://huggingface.co/microsoft/Phi-4-multimodal-instruct/resolve/main/sample_finetune_speech.py) and [vision](https://huggingface.co/microsoft/Phi-4-multimodal-instruct/resolve/main/sample_finetune_vision.py) is provided respectively.
## Safety
The Phi-4 family of models has adopted a robust safety post-training approach. This approach leverages a variety of both open-source and in-house generated datasets. The overall technique employed for safety alignment is a combination of SFT (Supervised Fine-Tuning), DPO (Direct Preference Optimization), and RLHF (Reinforcement Learning from Human Feedback) approaches by utilizing human-labeled and synthetic English-language datasets, including publicly available datasets focusing on helpfulness and harmlessness, as well as various questions and answers targeted to multiple safety categories. For non-English languages, existing datasets were extended via machine translation. Speech Safety datasets were generated by running Text Safety datasets through Azure TTS (Text-To-Speech) Service, for both English and non-English languages. Vision (text & images) Safety datasets were created to cover harm categories identified both in public and internal multi-modal RAI datasets.
### Safety Evaluation and Red-Teaming
Various evaluation techniques including red teaming, adversarial conversation simulations, and multilingual safety evaluation benchmark datasets were leveraged to evaluate Phi-4 models' propensity to produce undesirable outputs across multiple languages and risk categories. Several approaches were used to compensate for the limitations of one approach alone. Findings across the various evaluation methods indicate that safety post-training that was done as detailed in the [Phi 3 Safety Post-Training paper](https://arxiv.org/abs/2407.13833) had a positive impact across multiple languages and risk categories as observed by refusal rates (refusal to output undesirable outputs) and robustness to jailbreak techniques. Details on prior red team evaluations across Phi models can be found in the [Phi 3 Safety Post-Training paper](https://arxiv.org/abs/2407.13833). For this release, the red teaming effort focused on the newest Audio input modality and on the following safety areas: harmful content, self-injury risks, and exploits. The model was found to be more susceptible to providing undesirable outputs when attacked with context manipulation or persuasive techniques. These findings applied to all languages, with the persuasive techniques mostly affecting French and Italian. This highlights the need for industry-wide investment in the development of high-quality safety evaluation datasets across multiple languages, including low resource languages, and risk areas that account for cultural nuances where those languages are spoken.
### Vision Safety Evaluation
To assess model safety in scenarios involving both text and images, Microsoft's Azure AI Evaluation SDK was utilized. This tool facilitates the simulation of single-turn conversations with the target model by providing prompt text and images designed to incite harmful responses. The target model's responses are subsequently evaluated by a capable model across multiple harm categories, including violence, sexual content, self-harm, hateful and unfair content, with each response scored based on the severity of the harm identified. The evaluation results were compared with those of Phi-3.5-Vision and open-source models of comparable size. In addition, we ran both an internal and the public RTVLM and VLGuard multi-modal (text & vision) RAI benchmarks, once again comparing scores with Phi-3.5-Vision and open-source models of comparable size. However, the model may be susceptible to language-specific attack prompts and cultural context.
### Audio Safety Evaluation
In addition to extensive red teaming, the Safety of the model was assessed through three distinct evaluations. First, as performed with Text and Vision inputs, Microsoft's Azure AI Evaluation SDK was leveraged to detect the presence of harmful content in the model's responses to Speech prompts. Second, [Microsoft's Speech Fairness evaluation](https://speech.microsoft.com/portal/responsibleai/assess) was run to verify that Speech-To-Text transcription worked well across a variety of demographics. Third, we proposed and evaluated a mitigation approach via a system message to help prevent the model from inferring sensitive attributes (such as gender, sexual orientation, profession, medical condition, etc...) from the voice of a user.
## Software
* [PyTorch](https://github.com/pytorch/pytorch)
* [Transformers](https://github.com/huggingface/transformers)
* [Flash-Attention](https://github.com/HazyResearch/flash-attention)
* [Accelerate](https://huggingface.co/docs/transformers/main/en/accelerate)
* [soundfile](https://github.com/bastibe/python-soundfile)
* [pillow](https://github.com/python-pillow/Pillow)
## Hardware
Note that by default, the Phi-4-multimodal-instruct model uses flash attention, which requires certain types of GPU hardware to run. We have tested on the following GPU types:
* NVIDIA A100
* NVIDIA A6000
* NVIDIA H100
If you want to run the model on:
* NVIDIA V100 or earlier generation GPUs: call AutoModelForCausalLM.from_pretrained() with attn_implementation="eager"
## License
The model is licensed under the [MIT license](./LICENSE).
## Trademarks
This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft trademarks or logos is subject to and must follow[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks). Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. Any use of third-party trademarks or logos are subject to those third-party's policies.
## Appendix A: Benchmark Methodology
We include a brief word on methodology here - and in particular, how we think about optimizing prompts.
In an ideal world, we would never change any prompts in our benchmarks to ensure it is always an apples-to-apples comparison when comparing different models. Indeed, this is our default approach, and is the case in the vast majority of models we have run to date.
There are, however, some exceptions to this. In some cases, we see a model that performs worse than expected on a given eval due to a failure to respect the output format. For example:
+ A model may refuse to answer questions (for no apparent reason), or in coding tasks models may prefix their response with “Sure, I can help with that. …” which may break the parser. In such cases, we have opted to try different system messages (e.g. “You must always respond to a question” or “Get to the point!”).
+ Some models, we observed that few shots actually hurt model performance. In this case we did allow running the benchmarks with 0-shots for all cases.
+ We have tools to convert between chat and completions APIs. When converting a chat prompt to a completion prompt, some models have different keywords e.g. Human vs User. In these cases, we do allow for model-specific mappings for chat to completion prompts.
However, we do not:
+ Pick different few-shot examples. Few shots will always be the same when comparing different models.
+ Change prompt format: e.g. if it is an A/B/C/D multiple choice, we do not tweak this to 1/2/3/4 multiple choice.
### Vision Benchmark Settings
The goal of the benchmark setup is to measure the performance of the LMM when a regular user utilizes these models for a task involving visual input. To this end, we selected 9 popular and publicly available single-frame datasets and 3 multi-frame benchmarks that cover a wide range of challenging topics and tasks (e.g., mathematics, OCR tasks, charts-and-plots understanding, etc.) as well as a set of high-quality models.
Our benchmarking setup utilizes zero-shot prompts and all the prompt content are the same for every model. We only formatted the prompt content to satisfy the model's prompt API. This ensures that our evaluation is fair across the set of models we tested. Many benchmarks necessitate models to choose their responses from a presented list of options. Therefore, we've included a directive in the prompt's conclusion, guiding all models to pick the option letter that corresponds to the answer they deem correct.
In terms of the visual input, we use the images from the benchmarks as they come from the original datasets. We converted these images to base-64 using a JPEG encoding for models that require this format (e.g., GPTV, Claude Sonnet 3.5, Gemini 1.5 Pro/Flash). For other models (e.g., Llava Interleave, and InternVL2 4B and 8B), we used their Huggingface interface and passed in PIL images or a JPEG image stored locally. We did not scale or pre-process images in any other way.
Lastly, we used the same code to extract answers and evaluate them using the same code for every considered model. This ensures that we are fair in assessing the quality of their answers.
### Speech Benchmark Settings
The objective of this benchmarking setup is to assess the performance of models in speech and audio understanding tasks as utilized by regular users. To accomplish this, we selected several state-of-the-art open-sourced and closed-sourced models and performed evaluations across a variety of public and in-house benchmarks. These benchmarks encompass diverse and challenging topics, including Automatic Speech Recognition (ASR), Automatic Speech Translation (AST), Spoken Query Question Answering (SQQA), Audio Understanding (AU), and Speech Summarization.
The results are derived from evaluations conducted on identical test data without any further clarifications. All results were obtained without sampling during inference. For an accurate comparison, we employed consistent prompts for models across different tasks, except for certain model APIs (e.g., GPT-4o), which may refuse to respond to specific prompts for some tasks.
In conclusion, we used uniform code to extract answers and evaluate them for all considered models. This approach ensured fairness by assessing the quality of their responses.
### Benchmark datasets
The model was evaluated across a breadth of public and internal benchmarks to understand it's capabilities under multiple tasks and conditions. While most evaluations use English, multilingual benchmark was incorporated to cover performance in select languages. More specifically,
+ Vision:
+ Popular aggregated benchmark:
+ MMMU and MMMU-Pro: massive multi-discipline tasks at college-level subject knowledge and deliberate reasoning.
+ MMBench: large-scale benchmark to evaluate perception and reasoning capabilities.
+ Visual reasoning:
+ ScienceQA: multimodal visual question answering on science.
+ MathVista: visual math reasoning.
+ InterGPS: Visual 2D geometry reasoning.
+ Chart reasoning:
+ ChartQA: visual and logical reasoning on charts.
+ AI2D: diagram understanding.
+ Document Intelligence:
+ TextVQA: read and reason about text in images to answer questions about them.
+ InfoVQA: read and reason about high-resolution infographics images with arbitrary aspect ratios.
+ DocVQA: read and reason about document images with dense texts and handwritten texts.
+ OCRBench: test OCR and QA capability on diverse text related images.
+ Vision speech multimodal understanding:
+ s_AI2D: diagram understanding with speech as the question format.
+ s_ChartQA: visual and logical reasoning on charts with speech as the question format.
+ s_InfoVQA: read and reason about high-resolution infographics images with speech as the question format.
+ s_DocVQA: read and reason about document images with dense texts and handwritten texts with speech as the question format.
+ RAI & Security Benchmarks:
+ VLGuardExt: VLGuard is a vision-language instruction following public dataset for model safety to address safety on deception
discrimination, privacy and risky behavior (advice, sexual, violence, political). This was extended to a few internal categories such as child safety and election critical information.
+ RTVLM: Public benchmark for red-teaming vision-language model on model truthfulness, privacy, safety, and fairness.
+ GPTV-RAI: In-house benchmark for GPT-4V released from Azure AI, measuring harmfulness (ex. sexual, violent, hate and self-harm), privacy, jailbreak, misinformation.
+ Speech:
+ CommonVoice v15 is an open-source, multilingual speech dataset developed by Mozilla. It includes over 33,000 hours of speech data in 133 languages, contributed and validated by volunteers worldwide.The evaluations were conducted in the eight supported languages.
+ The OpenASR Leaderboard on Hugging Face is designed for benchmarking and evaluating the robustness of ASR models on English. The datasets in the leaderboard cover diverse speech domains including reading speech, conversations, meetings, and so on.
+ CoVoST2 is a multilingual speech-to-text translation dataset derived from Mozilla's Common Voice project. It is one of the largest open datasets available for speech translation, providing support for both X-to-English (X→En) and English-to-X (En→X) translation tasks. The directions with supported languages were evaluated on the test sets.
+ FLEURS is a multilingual speech dataset designed for evaluating speech recognition and speech-to-text translation models across a wide range of languages. The test sets for speech recognition and translation tasks were evaluated with the eight supported languages.
+ MT Bench (Multi-turn Benchmark) is specifically designed to evaluate the conversational and instruction-following abilities of AI models in multi-turn question-answering (QA) scenarios. To support spoken questions, the text is synthesized into speech.
+ MMMLU (Multilingual Massive Multitask Language Understanding) is an extensive benchmark designed to evaluate the general knowledge and reasoning capabilities of AI models across a wide array of subjects. To support spoken questions, the text is synthesized into its speech counterpart. The model was evaluated on the eight supported languages for this test set.
+ AIR-Bench Chat (Audio Instruction and Response Benchmark) is a comprehensive evaluation framework designed to test the capabilities of large audio language models (LALMs). It includes both foundation and chat benchmarks. The chat benchmark is selected for its open-ended question answering for audio capability.
+ MMAU (Massive Multi-Task Audio Understanding) is a comprehensive dataset designed to evaluate the capabilities of multi-modal models in audio-based understanding and reasoning tasks. The test sets are in the form of multiple-choices QA, covering the categories of music, sound, and speech.
+ Golden3 is a real-world meeting dataset, containing 108 meeting recordings with corresponding transcripts, averaging 6 minutes each. It is recorded across 30 conference rooms, featuring 4-8 attendees. The dataset is primarily in English, covering a wide range of topics. GPT4 is employed to generate summarization instructions that ask to summarize partial or the entire conversation or control the output style/length/structure.
+ AMI (Augmented Multi-Party Interaction) is a comprehensive collection of meeting recordings, encompassing approximately 100 hours of data. The test split contains 20 meeting recordings with an average duration of 32 minutes. The model was tested on the close-talking version of audio. GPT4 is employed to generate summarization instructions that ask to summarize partial or the entire conversation or control the output style/length/structure.
+ Safety and RAI:
+ Single-turn trustworthiness evaluation:
+ DecodingTrust: DecodingTrust is a collection of trustworthiness benchmarks in eight different perspectives
+ XSTest: XSTest is an exaggerated safety evaluation
+ Toxigen: Toxigen is adversarial and hate speech detection
+ Red Team:
+ Responses to prompts provided by AI Red Team at Microsoft

41
SECURITY.md Normal file
View File

@ -0,0 +1,41 @@
<!-- BEGIN MICROSOFT SECURITY.MD V0.0.9 BLOCK -->
## Security
Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/Microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet) and [Xamarin](https://github.com/xamarin).
If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/security.md/definition), please report it to us as described below.
## Reporting Security Issues
**Please do not report security vulnerabilities through public GitHub issues.**
Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/security.md/msrc/create-report).
If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/security.md/msrc/pgp).
You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://www.microsoft.com/msrc).
Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
* Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
* Full paths of source file(s) related to the manifestation of the issue
* The location of the affected source code (tag/branch/commit or direct URL)
* Any special configuration required to reproduce the issue
* Step-by-step instructions to reproduce the issue
* Proof-of-concept or exploit code (if possible)
* Impact of the issue, including how an attacker might exploit the issue
This information will help us triage your report more quickly.
If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/security.md/msrc/bounty) page for more details about our active programs.
## Preferred Languages
We prefer all communications to be in English.
## Policy
Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/security.md/cvd).
<!-- END MICROSOFT SECURITY.MD BLOCK -->

25
SUPPORT.md Normal file
View File

@ -0,0 +1,25 @@
# TODO: The maintainer of this repo has not yet edited this file
**REPO OWNER**: Do you want Customer Service & Support (CSS) support for this product/project?
- **No CSS support:** Fill out this template with information about how to file issues and get help.
- **Yes CSS support:** Fill out an intake form at [aka.ms/onboardsupport](https://aka.ms/onboardsupport). CSS will work with/help you to determine next steps.
- **Not sure?** Fill out an intake as though the answer were "Yes". CSS will help you decide.
*Then remove this first heading from this SUPPORT.MD file before publishing your repo.*
# Support
## How to file issues and get help
This project uses GitHub Issues to track bugs and feature requests. Please search the existing
issues before filing new issues to avoid duplicates. For new issues, file your bug or
feature request as a new Issue.
For help and questions about using this project, please **REPO MAINTAINER: INSERT INSTRUCTIONS HERE
FOR HOW TO ENGAGE REPO OWNERS OR COMMUNITY FOR HELP. COULD BE A STACK OVERFLOW TAG OR OTHER
CHANNEL. WHERE WILL YOU HELP PEOPLE?**.
## Microsoft Support Policy
Support for this **PROJECT or PRODUCT** is limited to the resources listed above.

12
added_tokens.json Normal file
View File

@ -0,0 +1,12 @@
{
"<|/tool_call|>": 200026,
"<|/tool|>": 200024,
"<|assistant|>": 200019,
"<|end|>": 200020,
"<|system|>": 200022,
"<|tag|>": 200028,
"<|tool_call|>": 200025,
"<|tool_response|>": 200027,
"<|tool|>": 200023,
"<|user|>": 200021
}

221
config.json Normal file
View File

@ -0,0 +1,221 @@
{
"_name_or_path": "Phi-4-multimodal-instruct",
"architectures": [
"Phi4MMForCausalLM"
],
"attention_bias": false,
"attention_dropout": 0.0,
"audio_processor": {
"config": {
"activation": "swish",
"activation_checkpointing": {
"interval": 1,
"module": "transformer",
"offload": false
},
"attention_dim": 1024,
"attention_heads": 16,
"batch_norm": false,
"bias_in_glu": true,
"causal": true,
"chunk_size": -1,
"cnn_layer_norm": true,
"conv_activation": "swish",
"conv_glu_type": "swish",
"depthwise_multiplier": 1,
"depthwise_seperable_out_channel": 1024,
"dropout_rate": 0.0,
"encoder_embedding_config": {
"input_size": 80
},
"ext_pw_kernel_size": 1,
"ext_pw_out_channel": 1024,
"input_layer": "nemo_conv",
"input_size": 80,
"kernel_size": 3,
"left_chunk": 18,
"linear_units": 1536,
"nemo_conv_settings": {
"conv_channels": 1024
},
"num_blocks": 24,
"relative_attention_bias_args": {
"t5_bias_max_distance": 500,
"type": "t5"
},
"time_reduction": 8
},
"name": "cascades"
},
"auto_map": {
"AutoConfig": "configuration_phi4mm.Phi4MMConfig",
"AutoModelForCausalLM": "modeling_phi4mm.Phi4MMForCausalLM",
"AutoTokenizer": "Xenova/gpt-4o"
},
"bos_token_id": 199999,
"embd_layer": {
"audio_embd_layer": {
"compression_rate": 8,
"downsample_rate": 1,
"embedding_cls": "audio",
"enable_gradient_checkpointing": true,
"projection_cls": "mlp",
"use_conv_downsample": false,
"use_qformer": false
},
"embedding_cls": "image_audio",
"image_embd_layer": {
"crop_size": 448,
"embedding_cls": "tune_image",
"enable_gradient_checkpointing": true,
"hd_transform_order": "sub_glb",
"image_token_compression_cls": "avg_pool_2d",
"projection_cls": "mlp",
"use_hd_transform": true,
"with_learnable_separator": true
}
},
"embd_pdrop": 0.0,
"eos_token_id": 199999,
"full_attn_mod": 1,
"hidden_act": "silu",
"hidden_size": 3072,
"initializer_range": 0.02,
"intermediate_size": 8192,
"interpolate_factor": 1,
"lm_head_bias": false,
"vision_lora": {
"dp": 0.0,
"layer": "layers.*((self_attn\\.(qkv_proj|o_proj))|(mlp\\.(gate_up|down)_proj))",
"lora_alpha": 512,
"r": 256
},
"speech_lora": {
"dp": 0.01,
"layer": "((layers.*self_attn\\.(qkv|o)_proj)|(layers.*mlp\\.(gate_up|down)_proj))",
"lora_alpha": 640,
"r": 320
},
"max_position_embeddings": 131072,
"mlp_bias": false,
"model_type": "phi4mm",
"num_attention_heads": 24,
"num_hidden_layers": 32,
"num_key_value_heads": 8,
"original_max_position_embeddings": 4096,
"pad_token_id": 199999,
"partial_rotary_factor": 0.75,
"resid_pdrop": 0.0,
"rms_norm_eps": 1e-05,
"rope_scaling": {
"long_factor": [
1,
1.118320672,
1.250641126,
1.398617824,
1.564103225,
1.74916897,
1.956131817,
2.187582649,
2.446418898,
2.735880826,
3.059592084,
3.421605075,
3.826451687,
4.279200023,
4.785517845,
5.351743533,
5.984965424,
6.693110555,
7.485043894,
8.370679318,
9.36110372,
10.4687158,
11.70738129,
13.09260651,
14.64173252,
16.37415215,
18.31155283,
20.47818807,
22.90118105,
25.61086418,
28.64115884,
32.03,
32.1,
32.13,
32.23,
32.6,
32.61,
32.64,
32.66,
32.7,
32.71,
32.93,
32.97,
33.28,
33.49,
33.5,
44.16,
47.77
],
"short_factor": [
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0
],
"type": "longrope"
},
"rope_theta": 10000.0,
"sliding_window": 262144,
"tie_word_embeddings": true,
"torch_dtype": "bfloat16",
"transformers_version": "4.46.1",
"use_cache": true,
"vocab_size": 200064,
"_attn_implementation": "flash_attention_2"
}

235
configuration_phi4mm.py Normal file
View File

@ -0,0 +1,235 @@
# coding=utf-8
# Copyright 2024 Microsoft and the HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Phi-4-MM model configuration"""
from transformers.configuration_utils import PretrainedConfig
from transformers.utils import logging
logger = logging.get_logger(__name__)
class Phi4MMConfig(PretrainedConfig):
r"""
This is the configuration class to store the configuration of a [`Phi4MMModel`]. It is used to instantiate a Phi-4-MM
model according to the specified arguments, defining the model architecture.
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
documentation from [`PretrainedConfig`] for more information.
Args:
vocab_size (`int`, *optional*, defaults to 200064):
Vocabulary size of the Phi-4-MM model. Defines the number of different tokens that can be represented by the
`inputs_ids` passed when calling [`Phi4MMModel`].
hidden_size (`int`, *optional*, defaults to 3072):
Dimension of the hidden representations.
intermediate_size (`int`, *optional*, defaults to 8192):
Dimension of the MLP representations.
num_hidden_layers (`int`, *optional*, defaults to 32):
Number of hidden layers in the Transformer decoder.
num_attention_heads (`int`, *optional*, defaults to 32):
Number of attention heads for each attention layer in the Transformer decoder.
num_key_value_heads (`int`, *optional*):
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
`num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
`num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
by meanpooling all the original heads within that group. For more details checkout [this
paper](https://arxiv.org/pdf/2305.13245.pdf). If it is not specified, will default to
`num_attention_heads`.
resid_pdrop (`float`, *optional*, defaults to 0.0):
Dropout probability for mlp outputs.
embd_pdrop (`int`, *optional*, defaults to 0.0):
The dropout ratio for the embeddings.
attention_dropout (`float`, *optional*, defaults to 0.0):
The dropout ratio after computing the attention scores.
hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
The non-linear activation function (function or string) in the decoder.
max_position_embeddings (`int`, *optional*, defaults to 4096):
The maximum sequence length that this model might ever be used with.
original_max_position_embeddings (`int`, *optional*, defaults to 4096):
The maximum sequence length that this model was trained with. This is used to determine the size of the
original RoPE embeddings when using long scaling.
initializer_range (`float`, *optional*, defaults to 0.02):
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
rms_norm_eps (`float`, *optional*, defaults to 1e-05):
The epsilon value used for the RMSNorm.
use_cache (`bool`, *optional*, defaults to `True`):
Whether or not the model should return the last key/values attentions (not used by all models). Only
relevant if `config.is_decoder=True`. Whether to tie weight embeddings or not.
tie_word_embeddings (`bool`, *optional*, defaults to `False`):
Whether to tie weight embeddings
rope_theta (`float`, *optional*, defaults to 10000.0):
The base period of the RoPE embeddings.
rope_scaling (`dict`, *optional*):
The scaling strategy for the RoPE embeddings. If `None`, no scaling is applied. If a dictionary, it must
contain the following keys: `type`, `short_factor` and `long_factor`. The `type` must be `longrope` and
the `short_factor` and `long_factor` must be lists of numbers with the same length as the hidden size
divided by the number of attention heads divided by 2.
partial_rotary_factor (`float`, *optional*, defaults to 0.5):
Percentage of the query and keys which will have rotary embedding.
bos_token_id (`int`, *optional*, defaults to 199999):
The id of the "beginning-of-sequence" token.
eos_token_id (`int`, *optional*, defaults to 199999):
The id of the "end-of-sequence" token.
pad_token_id (`int`, *optional*, defaults to 199999):
The id of the padding token.
sliding_window (`int`, *optional*):
Sliding window attention window size. If `None`, no sliding window is applied.
Example:
```python
>>> from transformers import Phi4MMModel, Phi4MMConfig
>>> # Initializing a Phi-4-MM style configuration
>>> configuration = Phi4MMConfig.from_pretrained("TBA")
>>> # Initializing a model from the configuration
>>> model = Phi4MMModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
```"""
model_type = "phi4mm"
keys_to_ignore_at_inference = ["past_key_values"]
def __init__(
self,
vocab_size=200064,
hidden_size=3072,
intermediate_size=8192,
num_hidden_layers=32,
num_attention_heads=32,
num_key_value_heads=None,
resid_pdrop=0.0,
embd_pdrop=0.0,
attention_dropout=0.0,
hidden_act="silu",
max_position_embeddings=4096,
original_max_position_embeddings=4096,
initializer_range=0.02,
rms_norm_eps=1e-5,
use_cache=True,
tie_word_embeddings=False,
rope_theta=10000.0,
rope_scaling=None,
partial_rotary_factor=1,
bos_token_id=199999,
eos_token_id=199999,
pad_token_id=199999,
sliding_window=None,
embd_layer: str = "default",
img_processor=None,
audio_processor=None,
vision_lora=None,
speech_lora=None,
**kwargs,
):
self.embd_layer = embd_layer
self.img_processor = img_processor
self.audio_processor = audio_processor
self.vision_lora = vision_lora
self.speech_lora = speech_lora
self.vocab_size = vocab_size
self.hidden_size = hidden_size
self.intermediate_size = intermediate_size
self.num_hidden_layers = num_hidden_layers
self.num_attention_heads = num_attention_heads
if num_key_value_heads is None:
num_key_value_heads = num_attention_heads
self.num_key_value_heads = num_key_value_heads
self.resid_pdrop = resid_pdrop
self.embd_pdrop = embd_pdrop
self.attention_dropout = attention_dropout
self.hidden_act = hidden_act
self.max_position_embeddings = max_position_embeddings
self.original_max_position_embeddings = original_max_position_embeddings
self.initializer_range = initializer_range
self.rms_norm_eps = rms_norm_eps
self.use_cache = use_cache
self.rope_theta = rope_theta
self.rope_scaling = rope_scaling
self.partial_rotary_factor = partial_rotary_factor
self._rope_scaling_adjustment()
self._rope_scaling_validation()
self.sliding_window = sliding_window
super().__init__(
bos_token_id=bos_token_id,
eos_token_id=eos_token_id,
pad_token_id=pad_token_id,
tie_word_embeddings=tie_word_embeddings,
**kwargs,
)
def _rope_scaling_adjustment(self):
"""
Adjust the `type` of the `rope_scaling` configuration for backward compatibility.
"""
if self.rope_scaling is None:
return
rope_scaling_type = self.rope_scaling.get("type", None)
# For backward compatibility if previous version used "su" or "yarn"
if rope_scaling_type is not None and rope_scaling_type in ["su", "yarn"]:
self.rope_scaling["type"] = "longrope"
def _rope_scaling_validation(self):
"""
Validate the `rope_scaling` configuration.
"""
if self.rope_scaling is None:
return
if not isinstance(self.rope_scaling, dict) or len(self.rope_scaling) != 3:
raise ValueError(
"`rope_scaling` must be a dictionary with three fields, `type`, `short_factor` and `long_factor`, "
f"got {self.rope_scaling}"
)
rope_scaling_type = self.rope_scaling.get("type", None)
rope_scaling_short_factor = self.rope_scaling.get("short_factor", None)
rope_scaling_long_factor = self.rope_scaling.get("long_factor", None)
if rope_scaling_type is None or rope_scaling_type not in ["longrope"]:
raise ValueError(f"`rope_scaling`'s type field must be one of ['longrope'], got {rope_scaling_type}")
if not (
isinstance(rope_scaling_short_factor, list)
and all(isinstance(x, (int, float)) for x in rope_scaling_short_factor)
):
raise ValueError(
f"`rope_scaling`'s short_factor field must be a list of numbers, got {rope_scaling_short_factor}"
)
rotary_ndims = int(self.hidden_size // self.num_attention_heads * self.partial_rotary_factor)
if not len(rope_scaling_short_factor) == rotary_ndims // 2:
raise ValueError(
f"`rope_scaling`'s short_factor field must have length {rotary_ndims // 2}, got {len(rope_scaling_short_factor)}"
)
if not (
isinstance(rope_scaling_long_factor, list)
and all(isinstance(x, (int, float)) for x in rope_scaling_long_factor)
):
raise ValueError(
f"`rope_scaling`'s long_factor field must be a list of numbers, got {rope_scaling_long_factor}"
)
if not len(rope_scaling_long_factor) == rotary_ndims // 2:
raise ValueError(
f"`rope_scaling`'s long_factor field must have length {rotary_ndims // 2}, got {len(rope_scaling_long_factor)}"
)

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

BIN
figures/multi_image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 KiB

BIN
figures/speech_qa.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 89 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 62 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

BIN
figures/vision_radar.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB

11
generation_config.json Normal file
View File

@ -0,0 +1,11 @@
{
"_from_model_config": true,
"bos_token_id": 199999,
"eos_token_id": [
200020,
199999
],
"pad_token_id": 199999,
"transformers_version": "4.46.1",
"use_cache": true
}

199743
merges.txt Normal file

File diff suppressed because it is too large Load Diff

BIN
model-00003-of-00003.safetensors (Stored with Git LFS) Normal file

Binary file not shown.

2054
model.safetensors.index.json Normal file

File diff suppressed because it is too large Load Diff

2407
modeling_phi4mm.py Normal file

File diff suppressed because it is too large Load Diff

BIN
phi_4_mm.tech_report.02252025.pdf (Stored with Git LFS) Normal file

Binary file not shown.

14
preprocessor_config.json Normal file
View File

@ -0,0 +1,14 @@
{
"auto_map": {
"AutoProcessor": "processing_phi4mm.Phi4MMProcessor",
"AutoImageProcessor": "processing_phi4mm.Phi4MMImageProcessor",
"AutoFeatureExtractor": "processing_phi4mm.Phi4MMAudioFeatureExtractor"
},
"image_processor_type": "Phi4MMImageProcessor",
"processor_class": "Phi4MMProcessor",
"feature_extractor_type": "Phi4MMAudioFeatureExtractor",
"audio_compression_rate": 8,
"audio_downsample_rate": 1,
"audio_feat_stride": 1,
"dynamic_hd": 36
}

733
processing_phi4mm.py Normal file
View File

@ -0,0 +1,733 @@
# Copyright 2024 Microsoft and the HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Processor class for Phi4MM
"""
import re
from typing import List, Optional, Tuple, Union
import math
from enum import Enum
import numpy as np
import scipy
import torch
import torchvision
from transformers import AutoFeatureExtractor, AutoImageProcessor
from transformers.feature_extraction_sequence_utils import SequenceFeatureExtractor
from transformers.image_processing_utils import BaseImageProcessor, BatchFeature
from transformers.image_utils import (
ImageInput,
make_list_of_images,
valid_images,
)
from transformers.processing_utils import ProcessorMixin
from transformers.tokenization_utils_base import PaddingStrategy, TextInput, TruncationStrategy
from transformers.utils import TensorType, logging
from torch.nn.utils.rnn import pad_sequence
logger = logging.get_logger(__name__)
# Special tokens
_COMPATIBLE_IMAGE_SPECIAL_TOKEN_PATTERN = r'<\|image_\d+\|>' # For backward compatibility
_COMPATIBLE_AUDIO_SPECIAL_TOKEN_PATTERN = r'<\|audio_\d+\|>' # For backward compatibility
_IMAGE_SPECIAL_TOKEN = '<|endoftext10|>'
_AUDIO_SPECIAL_TOKEN = '<|endoftext11|>'
_IMAGE_SPECIAL_TOKEN_ID = 200010 # '<|endoftext10|>', or we can better name it (in `tokenizer_config.json`)
_AUDIO_SPECIAL_TOKEN_ID = 200011 # '<|endoftext11|>'
class InputMode(Enum):
LANGUAGE = 0
VISION = 1
SPEECH = 2
VISION_SPEECH = 3
class Phi4MMImageProcessor(BaseImageProcessor):
r"""
Constructs a Phi4MM image processor.
"""
model_input_names = ["input_image_embeds", "image_sizes", "image_attention_mask"]
def __init__(
self,
dynamic_hd,
**kwargs,
) -> None:
super().__init__(**kwargs)
self.dynamic_hd = dynamic_hd
def find_closest_aspect_ratio(self, aspect_ratio, target_ratios, width, height, image_size):
best_ratio_diff = float('inf')
best_ratio = (1, 1)
area = width * height
for ratio in target_ratios:
target_aspect_ratio = ratio[0] / ratio[1]
ratio_diff = abs(aspect_ratio - target_aspect_ratio)
if ratio_diff < best_ratio_diff:
best_ratio_diff = ratio_diff
best_ratio = ratio
elif ratio_diff == best_ratio_diff:
if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
best_ratio = ratio
return best_ratio
def dynamic_preprocess(self, image, min_num=1, max_num=12, image_size=384, mask_size=27, use_thumbnail=True):
orig_width, orig_height = image.size
w_crop_num = math.ceil(orig_width/float(image_size))
h_crop_num = math.ceil(orig_height/float(image_size))
if w_crop_num * h_crop_num > max_num:
aspect_ratio = orig_width / orig_height
# calculate the existing image aspect ratio
target_ratios = set(
(i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
i * j <= max_num and i * j >= min_num)
target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
# find the closest aspect ratio to the target
target_aspect_ratio = self.find_closest_aspect_ratio(
aspect_ratio, target_ratios, orig_width, orig_height, image_size)
# calculate the target width and height
target_width = image_size * target_aspect_ratio[0]
target_height = image_size * target_aspect_ratio[1]
else:
target_width = image_size * w_crop_num
target_height = image_size * h_crop_num
target_aspect_ratio = (w_crop_num, h_crop_num)
# Calculate the ratio
ratio_width = target_width / orig_width
ratio_height = target_height / orig_height
if ratio_width < ratio_height:
new_size = (target_width, int(orig_height * ratio_width))
padding_width = 0
padding_height = target_height - int(orig_height * ratio_width)
else:
new_size = (int(orig_width * ratio_height), target_height)
padding_width = target_width - int(orig_width * ratio_height)
padding_height = 0
attention_mask = torch.ones((int(mask_size*target_aspect_ratio[1]), int(mask_size*target_aspect_ratio[0])))
if padding_width >= 14:
attention_mask[:, -math.floor(padding_width/14):] = 0
if padding_height >= 14:
attention_mask[-math.floor(padding_height/14):,:] = 0
assert attention_mask.sum() > 0
if min(new_size[1], target_height) < 10 or min(new_size[0], target_width) < 10:
raise ValueError(f'the aspect ratio is very extreme {new_size}')
image = torchvision.transforms.functional.resize(image, [new_size[1], new_size[0]],)
resized_img = torchvision.transforms.functional.pad(image, [0, 0, padding_width, padding_height], fill=[255,255,255])
return resized_img, attention_mask
def pad_to_max_num_crops(self, images, max_crops=5):
"""
images: B x 3 x H x W, B<=max_crops
"""
B, _, H, W = images.shape
if B < max_crops:
pad = torch.zeros(max_crops - B, 3, H, W, dtype=images.dtype, device=images.device)
images = torch.cat([images, pad], dim=0)
return images
def pad_mask_to_max_num_crops(self, masks, max_crops=5):
B, H, W = masks.shape
if B < max_crops:
pad = torch.ones(max_crops - B, H, W, dtype=masks.dtype, device=masks.device)
masks = torch.cat([masks, pad], dim=0)
return masks
def preprocess(
self,
images: ImageInput,
return_tensors: Optional[Union[str, TensorType]] = None,
):
"""
Args:
images (`ImageInput`):
Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If
passing in images with pixel values between 0 and 1, set `do_rescale=False`.
return_tensors (`str` or `TensorType`, *optional*):
The type of tensors to return. Can be one of:
- Unset: Return a list of `np.ndarray`.
- `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
- `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
- `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
- `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
"""
images = make_list_of_images(images)
if not valid_images(images):
raise ValueError(
"Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
"torch.Tensor, tf.Tensor or jax.ndarray."
)
# Basic settings.
img_processor = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(
(0.5, 0.5, 0.5),
(0.5, 0.5, 0.5)
),
])
dyhd_base_resolution = 448
# Dynamic HD
base_resolution = dyhd_base_resolution
images = [image.convert('RGB') for image in images]
# cover 384 and 448 resolution
mask_resolution = base_resolution // 14
elems, image_attention_masks = [], []
for im in images:
elem, attention_mask = self.dynamic_preprocess(im, max_num=self.dynamic_hd, image_size=base_resolution, mask_size=mask_resolution)
elems.append(elem)
image_attention_masks.append(attention_mask)
hd_images = [img_processor(im) for im in elems]
global_image = [torch.nn.functional.interpolate(im.unsqueeze(0).float(), size=(base_resolution, base_resolution), mode='bicubic',).to(im.dtype) for im in hd_images]
shapes = [[im.size(1), im.size(2)] for im in hd_images]
mask_shapes = [[mask.size(0), mask.size(1)] for mask in image_attention_masks]
global_attention_mask = [torch.ones((1, mask_resolution, mask_resolution)) for _ in hd_images]
hd_images_reshape = [im.reshape(1, 3,
h//base_resolution,
base_resolution,
w//base_resolution,
base_resolution
).permute(0,2,4,1,3,5).reshape(-1, 3, base_resolution, base_resolution).contiguous() for im, (h, w) in zip(hd_images, shapes)]
attention_masks_reshape = [mask.reshape(1,
h//mask_resolution,
mask_resolution,
w//mask_resolution,
mask_resolution
).permute(0,1,3,2,4).reshape(-1, mask_resolution, mask_resolution).contiguous() for mask, (h, w) in zip(image_attention_masks, mask_shapes)]
downsample_attention_masks = [mask[:,0::2,0::2].reshape(1,
h//mask_resolution,
w//mask_resolution,
mask_resolution//2+mask_resolution%2,
mask_resolution//2+mask_resolution%2
).permute(0,1,3,2,4) for mask, (h,w) in zip(attention_masks_reshape, mask_shapes)]
downsample_attention_masks = [mask.reshape(mask.size(1)*mask.size(2), mask.size(3)*mask.size(4))for mask in downsample_attention_masks]
num_img_tokens = [256 + 1 + int(mask.sum().item()) + int(mask[:,0].sum().item()) + 16 for mask in downsample_attention_masks]
hd_images_reshape = [torch.cat([_global_image] + [_im], dim=0) for _global_image, _im in zip(global_image, hd_images_reshape)]
hd_masks_reshape = [torch.cat([_global_mask] + [_mask], dim=0) for _global_mask, _mask in zip(global_attention_mask, attention_masks_reshape)]
max_crops = max([img.size(0) for img in hd_images_reshape])
image_transformed = [self.pad_to_max_num_crops(im, max_crops) for im in hd_images_reshape]
image_transformed = torch.stack(image_transformed, dim=0)
mask_transformed = [self.pad_mask_to_max_num_crops(mask, max_crops) for mask in hd_masks_reshape]
mask_transformed = torch.stack(mask_transformed, dim=0)
returned_input_image_embeds = image_transformed
returned_image_sizes = torch.tensor(shapes, dtype=torch.long)
returned_image_attention_mask = mask_transformed
returned_num_img_tokens = num_img_tokens
data = {
"input_image_embeds": returned_input_image_embeds,
"image_sizes": returned_image_sizes,
"image_attention_mask": returned_image_attention_mask,
"num_img_tokens": returned_num_img_tokens,
}
return BatchFeature(data=data, tensor_type=return_tensors)
AudioInput = Tuple[Union[np.ndarray, torch.Tensor], int]
AudioInputs = List[AudioInput]
def speechlib_mel(sample_rate, n_fft, n_mels, fmin=None, fmax=None):
"""Create a Mel filter-bank the same as SpeechLib FbankFC.
Args:
sample_rate (int): Sample rate in Hz. number > 0 [scalar]
n_fft (int): FFT size. int > 0 [scalar]
n_mel (int): Mel filter size. int > 0 [scalar]
fmin (float): lowest frequency (in Hz). If None use 0.0.
float >= 0 [scalar]
fmax: highest frequency (in Hz). If None use sample_rate / 2.
float >= 0 [scalar]
Returns
out (numpy.ndarray): Mel transform matrix
[shape=(n_mels, 1 + n_fft/2)]
"""
bank_width = int(n_fft // 2 + 1)
if fmax is None:
fmax = sample_rate / 2
if fmin is None:
fmin = 0
assert fmin >= 0, "fmin cannot be negtive"
assert fmin < fmax <= sample_rate / 2, "fmax must be between (fmin, samplerate / 2]"
def mel(f):
return 1127.0 * np.log(1.0 + f / 700.0)
def bin2mel(fft_bin):
return 1127.0 * np.log(1.0 + fft_bin * sample_rate / (n_fft * 700.0))
def f2bin(f):
return int((f * n_fft / sample_rate) + 0.5)
# Spec 1: FFT bin range [f2bin(fmin) + 1, f2bin(fmax) - 1]
klo = f2bin(fmin) + 1
khi = f2bin(fmax)
khi = max(khi, klo)
# Spec 2: SpeechLib uses trianges in Mel space
mlo = mel(fmin)
mhi = mel(fmax)
m_centers = np.linspace(mlo, mhi, n_mels + 2)
ms = (mhi - mlo) / (n_mels + 1)
matrix = np.zeros((n_mels, bank_width), dtype=np.float32)
for m in range(0, n_mels):
left = m_centers[m]
center = m_centers[m + 1]
right = m_centers[m + 2]
for fft_bin in range(klo, khi):
mbin = bin2mel(fft_bin)
if left < mbin < right:
matrix[m, fft_bin] = 1.0 - abs(center - mbin) / ms
return matrix
class Phi4MMAudioFeatureExtractor(SequenceFeatureExtractor):
model_input_names = ["input_audio_embeds", "audio_embed_sizes", "audio_attention_mask"]
def __init__(self, audio_compression_rate, audio_downsample_rate, audio_feat_stride, **kwargs):
feature_size = 80
sampling_rate = 16000
padding_value = 0.0
super().__init__(feature_size, sampling_rate, padding_value, **kwargs)
self.compression_rate = audio_compression_rate
self.qformer_compression_rate = audio_downsample_rate
self.feat_stride = audio_feat_stride
self._eightk_method = "fillzero"
self._mel = speechlib_mel(16000, 512, 80, fmin=None, fmax=7690).T
self._hamming400 = np.hamming(400) # for 16k audio
self._hamming200 = np.hamming(200) # for 8k audio
def duration_to_frames(self, duration):
"""duration in s, estimated frames"""
frame_rate = 10
num_frames = duration * 1000 // frame_rate
return num_frames
def __call__(
self,
audios: List[AudioInput],
return_tensors: Optional[Union[str, TensorType]] = None,
):
# Ref: https://github.com/huggingface/transformers/blob/v4.47.0/src/transformers/models/audio_spectrogram_transformer/feature_extraction_audio_spectrogram_transformer.py#L161
returned_input_audio_embeds = []
returned_audio_embed_sizes = []
audio_frames_list = []
for audio_data, sample_rate in audios:
audio_embeds = self._extract_features(audio_data, sample_rate)
audio_frames = len(audio_embeds) * self.feat_stride
audio_embed_size = self._compute_audio_embed_size(audio_frames)
returned_input_audio_embeds.append(torch.tensor(audio_embeds))
returned_audio_embed_sizes.append(torch.tensor(audio_embed_size).long())
audio_frames_list.append(audio_frames)
returned_input_audio_embeds = pad_sequence(
returned_input_audio_embeds, batch_first=True
)
returned_audio_embed_sizes = torch.stack(returned_audio_embed_sizes, dim=0)
audio_frames = torch.tensor(audio_frames_list)
returned_audio_attention_mask = torch.arange(0, audio_frames.max()).unsqueeze(0) < audio_frames.unsqueeze(1) if len(audios) > 1 else None
data = {
"input_audio_embeds": returned_input_audio_embeds,
"audio_embed_sizes": returned_audio_embed_sizes,
}
if returned_audio_attention_mask is not None:
data["audio_attention_mask"] = returned_audio_attention_mask
return BatchFeature(data=data, tensor_type=return_tensors)
def _extract_spectrogram(self, wav, fs):
"""Extract spectrogram features from waveform.
Args:
wav (1D array): waveform of the input
fs (int): sampling rate of the waveform, 16000 or 8000.
If fs=8000, the waveform will be resampled to 16000Hz.
Output:
log_fbank (2D array): a TxD matrix of log Mel filterbank features.
D=80, and T is the number of frames.
"""
if wav.ndim > 1:
wav = np.squeeze(wav)
# by default, we extract the mean if stereo
if len(wav.shape) == 2:
wav = wav.mean(1)
# Resample to 16000 or 8000 if needed
if fs > 16000:
wav = scipy.signal.resample_poly(wav, 1, fs // 16000)
fs = 16000
elif 8000 < fs < 16000:
wav = scipy.signal.resample_poly(wav, 1, fs // 8000)
fs = 8000
elif fs < 8000:
raise RuntimeError(f"Unsupported sample rate {fs}")
if fs == 8000:
if self._eightk_method == "resample":
# Input audio is 8 kHz. Convert to 16 kHz before feature
# extraction
wav = scipy.signal.resample_poly(wav, 2, 1)
fs = 16000
# Do nothing here for fillzero method
elif fs != 16000:
# Input audio is not a supported sample rate.
raise RuntimeError(f"Input data using an unsupported sample rate: {fs}")
preemphasis = 0.97
if fs == 8000:
n_fft = 256
win_length = 200
hop_length = 80
fft_window = self._hamming200
elif fs == 16000:
n_fft = 512
win_length = 400
hop_length = 160
fft_window = self._hamming400
# Spec 1: SpeechLib cut remaining sample insufficient for a hop
n_batch = (wav.shape[0] - win_length) // hop_length + 1
# Here we don't use stride_tricks since the input array may not satisfy
# memory layout requirement and we need writeable output
# Here we only use list of views before copy to desination
# so it is more efficient than broadcasting
y_frames = np.array(
[wav[_stride : _stride + win_length] for _stride in range(0, hop_length * n_batch, hop_length)],
dtype=np.float32,
)
# Spec 2: SpeechLib applies preemphasis within each batch
y_frames_prev = np.roll(y_frames, 1, axis=1)
y_frames_prev[:, 0] = y_frames_prev[:, 1]
y_frames = (y_frames - preemphasis * y_frames_prev) * 32768
S = np.fft.rfft(fft_window * y_frames, n=n_fft, axis=1).astype(np.complex64)
if fs == 8000:
# Need to pad the output to look like 16 kHz data but with zeros in
# the 4 to 8 kHz bins.
frames, bins = S.shape
padarray = np.zeros((frames, bins))
S = np.concatenate((S[:, 0:-1], padarray), axis=1) # Nyquist bin gets set to zero
spec = np.abs(S).astype(np.float32)
return spec
def _extract_features(self, wav, fs):
"""Extract log filterbank features from waveform.
Args:
wav (1D array): waveform of the input
fs (int): sampling rate of the waveform, 16000 or 8000.
If fs=8000, the waveform will be resampled to 16000Hz.
Output:
log_fbank (2D array): a TxD matrix of log Mel filterbank features.
D=80, and T is the number of frames.
"""
spec = self._extract_spectrogram(wav, fs)
spec_power = spec**2
fbank_power = np.clip(spec_power.dot(self._mel), 1.0, None)
log_fbank = np.log(fbank_power).astype(np.float32)
return log_fbank
def _compute_audio_embed_size(self, audio_frames):
integer = audio_frames // self.compression_rate
remainder = audio_frames % self.compression_rate
result = integer if remainder == 0 else integer + 1
integer = result // self.qformer_compression_rate
remainder = result % self.qformer_compression_rate
result = integer if remainder == 0 else integer + 1 # qformer compression
return result
class Phi4MMProcessor(ProcessorMixin):
r"""
Constructs a Phi4MM processor which raps an image processor, a audio processor, and a GPT tokenizer into a single processor.
[`Phi4MMProcessor`] offers all the functionalities of [`Phi4MMImageProcessor`] and [`GPT2Tokenizer`]. See the
[`~Phi4MMProcessor.__call__`] and [`~Phi4MMProcessor.decode`] for more information.
Args:
image_processor ([`Phi4MMImageProcessor`], *optional*):
The image processor is a required input.
tokenizer ([`GPT2Tokenizer`], *optional*):
The tokenizer is a required input.
"""
attributes = ["image_processor", "audio_processor", "tokenizer"]
tokenizer_class = "GPT2TokenizerFast"
image_processor_class = "AutoImageProcessor" # Phi4MMImageProcessor will be registered later
audio_processor_class = "AutoFeatureExtractor" # Phi4MMAudioFeatureExtractor will be registered later
def __init__(self, image_processor, audio_processor, tokenizer):
self.image_processor = image_processor
self.audio_processor = audio_processor
self.tokenizer = tokenizer
def __call__(
self,
text: Union[TextInput, List[TextInput]],
images: Optional[ImageInput] = None,
audios: Optional[AudioInputs] = None,
padding: Union[bool, str, PaddingStrategy] = False,
truncation: Optional[Union[bool, str, TruncationStrategy]] = None,
max_length=None,
return_tensors: Optional[Union[str, TensorType]] = TensorType.PYTORCH,
) -> BatchFeature:
"""
Main method to prepare for the model one or several sequences(s) and image(s). This method forards the `text`
and `kwargs` arguments to GPT2Tokenizer's [`~GPT2Tokenizer.__call__`] if `text` is not `None` to encode
the text. To prepare the image(s), this method forwards the `images` and `kwrags` arguments to
Phi4MMImageProcessor's [`~Phi4MMImageProcessor.__call__`] if `images` is not `None`. Please refer to the doctsring
of the above two methods for more information.
Args:
text (`str`, `List[str]`, `List[List[str]]`):
The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
(pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
`is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
tensor. Both channels-first and channels-last formats are supported.
padding (`bool`, `str` or [`~utils.PaddingStrategy`], *optional*, defaults to `False`):
Select a strategy to pad the returned sequences (according to the model's padding side and padding
index) among:
- `True` or `'longest'`: Pad to the longest sequence in the batch (or no padding if only a single
sequence if provided).
- `'max_length'`: Pad to a maximum length specified with the argument `max_length` or to the maximum
acceptable input length for the model if that argument is not provided.
- `False` or `'do_not_pad'` (default): No padding (i.e., can output a batch with sequences of different
lengths).
max_length (`int`, *optional*):
Maximum length of the returned list and optionally padding length (see above).
truncation (`bool`, *optional*):
Activates truncation to cut input sequences longer than `max_length` to `max_length`.
return_tensors (`str` or [`~utils.TensorType`], *optional*):
If set, will return tensors of a particular framework. Acceptable values are:
- `'tf'`: Return TensorFlow `tf.constant` objects.
- `'pt'`: Return PyTorch `torch.Tensor` objects.
- `'np'`: Return NumPy `np.ndarray` objects.
- `'jax'`: Return JAX `jnp.ndarray` objects.
Returns:
[`BatchFeature`]: A [`BatchFeature`] with the following fields:
- **input_ids** -- List of token ids to be fed to a model.
- **input_image_embeds** -- Pixel values to be fed to a model.
- **image_sizes** -- List of tuples specifying the size of each image in `input_image_embeds`.
- **image_attention_mask** -- List of attention masks for each image in `input_image_embeds`.
- **input_audio_embeds** -- Audio embeddings to be fed to a model.
- **audio_embed_sizes** -- List of integers specifying the size of each audio in `input_audio_embeds`.
- **attention_mask** -- List of indices specifying which tokens should be attended to by the model.
"""
image_inputs = self.image_processor(images, return_tensors=return_tensors) if images is not None else {}
audio_inputs = self.audio_processor(audios, return_tensors=return_tensors) if audios is not None else {}
inputs = self._convert_images_audios_text_to_inputs(
image_inputs,
audio_inputs,
text,
padding=padding,
truncation=truncation,
max_length=max_length,
return_tensors=return_tensors,
)
# idenfity the input mode
if len(image_inputs) > 0 and len(audio_inputs) > 0:
input_mode = InputMode.VISION_SPEECH
elif len(image_inputs) > 0:
input_mode = InputMode.VISION
elif len(audio_inputs) > 0:
input_mode = InputMode.SPEECH
else:
input_mode = InputMode.LANGUAGE
inputs["input_mode"] = torch.tensor([input_mode.value], dtype=torch.long)
return inputs
@property
def special_image_token_id(self):
return self.tokenizer.convert_tokens_to_ids(self.special_image_token)
def get_special_image_token_id(self):
return self.tokenizer.convert_tokens_to_ids(self.special_image_token)
@property
def chat_template(self):
return self.tokenizer.chat_template
def _convert_images_audios_text_to_inputs(
self, images, audios, text, padding=False, truncation=None, max_length=None, return_tensors=None
):
# prepare image id to image input ids
if len(images) > 0:
input_image_embeds = images["input_image_embeds"]
image_sizes = images["image_sizes"]
image_attention_mask = images["image_attention_mask"]
num_img_tokens = images['num_img_tokens']
else:
input_image_embeds = torch.tensor([])
image_sizes = torch.tensor([])
image_attention_mask = torch.tensor([])
num_img_tokens = []
# prepare audio id to audio input ids
if len(audios) > 0:
input_audio_embeds = audios["input_audio_embeds"]
audio_embed_sizes = audios["audio_embed_sizes"]
audio_attention_mask = audios.get("audio_attention_mask", None)
else:
input_audio_embeds = torch.tensor([])
audio_embed_sizes = torch.tensor([])
audio_attention_mask = None
# Replace certain special tokens for compatibility
# Ref: https://stackoverflow.com/questions/11475885/python-replace-regex
if isinstance(text, str):
text = [text]
assert isinstance(text, list)
processed_text = [re.sub(_COMPATIBLE_IMAGE_SPECIAL_TOKEN_PATTERN, _IMAGE_SPECIAL_TOKEN, t) for t in text]
processed_text = [re.sub(_COMPATIBLE_AUDIO_SPECIAL_TOKEN_PATTERN, _AUDIO_SPECIAL_TOKEN, t) for t in processed_text]
input_ids_list = [self.tokenizer(t).input_ids for t in processed_text]
img_cnt, audio_cnt = 0, 0 # only needed for later assertion
image_token_count_iter = iter(num_img_tokens)
audio_embed_size_iter = iter(audio_embed_sizes.tolist())
new_input_ids_list = []
for input_ids in input_ids_list:
i = 0
while i < len(input_ids):
token_id = input_ids[i]
if token_id == _AUDIO_SPECIAL_TOKEN_ID:
token_count = next(audio_embed_size_iter)
audio_cnt += 1
elif token_id == _IMAGE_SPECIAL_TOKEN_ID:
token_count = next(image_token_count_iter)
img_cnt += 1
else:
i += 1
continue
tokens = [token_id] * token_count
input_ids = input_ids[:i] + tokens + input_ids[i + 1:]
i += token_count
input_ids = torch.tensor(input_ids, dtype=torch.long)
new_input_ids_list.append(input_ids)
lengths = torch.tensor([len(input_ids) for input_ids in new_input_ids_list])
max_len = lengths.max()
input_ids = input_ids.new_full((len(new_input_ids_list), max_len), self.tokenizer.pad_token_id)
# batched inference requires left padding
for i in range(len(new_input_ids_list)):
input_ids[i, max_len - len(new_input_ids_list[i]):] = new_input_ids_list[i]
# If the below assertion fails, it might be that input pure-text
# messages contain image/audio special tokens literally
# (<|endoftext10|>, <|endoftext11|>).
assert (
img_cnt == len(num_img_tokens)
), (
f"Number of image tokens in prompt_token_ids ({img_cnt}) "
f"does not match number of images ({len(num_img_tokens)})"
)
assert (
audio_cnt == len(audio_embed_sizes)
), (
f"Number of audio tokens in prompt_token_ids ({audio_cnt}) "
f"does not match number of audios ({len(audio_embed_sizes)})"
)
# prepare attention mask
seq_range = torch.arange(max_len - 1, -1, -1)
attention_mask = seq_range.unsqueeze(0) < lengths.unsqueeze(1)
# prepare batch feature
data = {
"input_ids": input_ids,
"input_image_embeds": input_image_embeds,
"image_sizes": image_sizes,
"image_attention_mask": image_attention_mask,
"input_audio_embeds": input_audio_embeds,
"audio_embed_sizes": audio_embed_sizes,
"audio_attention_mask": audio_attention_mask,
"attention_mask": attention_mask,
}
return BatchFeature(
data=data
)
# Copied from transformers.models.clip.processing_clip.CLIPProcessor.batch_decode with CLIP->Llama
def batch_decode(self, *args, **kwargs):
"""
This method forwards all its arguments to GPT2Tokenizer's [`~PreTrainedTokenizer.batch_decode`]. Please
refer to the docstring of this method for more information.
"""
return self.tokenizer.batch_decode(*args, **kwargs)
# Copied from transformers.models.clip.processing_clip.CLIPProcessor.decode with CLIP->Llama
def decode(self, *args, **kwargs):
"""
This method forwards all its arguments to GPT2Tokenizer's [`~PreTrainedTokenizer.decode`]. Please refer to
the docstring of this method for more information.
"""
return self.tokenizer.decode(*args, **kwargs)
@property
# Copied from transformers.models.clip.processing_clip.CLIPProcessor.model_input_names
def model_input_names(self):
tokenizer_input_names = self.tokenizer.model_input_names
image_processor_input_names = self.image_processor.model_input_names
audio_processor_input_names = self.audio_processor.model_input_names
return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names + audio_processor_input_names))
AutoImageProcessor.register("Phi4MMImageProcessor", Phi4MMImageProcessor)
AutoFeatureExtractor.register("Phi4MMAudioFeatureExtractor", Phi4MMAudioFeatureExtractor)

6
processor_config.json Normal file
View File

@ -0,0 +1,6 @@
{
"auto_map": {
"AutoProcessor": "processing_phi4mm.Phi4MMProcessor"
},
"processor_class": "Phi4MMProcessor"
}

478
sample_finetune_speech.py Normal file
View File

@ -0,0 +1,478 @@
"""
finetune Phi-4-multimodal-instruct on an speech task
scipy==1.15.1
peft==0.13.2
backoff==2.2.1
transformers==4.46.1
accelerate==1.3.0
"""
import argparse
import json
import os
from pathlib import Path
import torch
import sacrebleu
from accelerate import Accelerator
from accelerate.utils import gather_object
from datasets import load_dataset
from torch.utils.data import Dataset
from tqdm import tqdm
from transformers import (
AutoModelForCausalLM,
AutoProcessor,
BatchFeature,
Trainer,
TrainingArguments,
StoppingCriteria,
StoppingCriteriaList,
)
INSTSRUCTION = {
"en_zh-CN": "Translate the audio to Mandarin.",
"en_id": "Translate the audio to Indonesian.",
"en_sl": "Translate the audio to Slovenian.",
}
TOKENIZER = {
"en_zh-CN": "zh",
"en_ja": "ja-mecab",
}
ANSWER_SUFFIX = "<|end|><|endoftext|>"
_IGNORE_INDEX = -100
_TRAIN_SIZE = 50000
_EVAL_SIZE = 200
class MultipleTokenBatchStoppingCriteria(StoppingCriteria):
"""Stopping criteria capable of receiving multiple stop-tokens and handling batched inputs."""
def __init__(self, stop_tokens: torch.LongTensor, batch_size: int = 1) -> None:
"""Initialize the multiple token batch stopping criteria.
Args:
stop_tokens: Stop-tokens.
batch_size: Batch size.
"""
self.stop_tokens = stop_tokens
self.max_stop_tokens = stop_tokens.shape[-1]
self.stop_tokens_idx = torch.zeros(batch_size, dtype=torch.long, device=stop_tokens.device)
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
# Only gather the maximum number of inputs compatible with stop tokens
# and checks whether generated inputs are equal to `stop_tokens`
generated_inputs = torch.eq(input_ids[:, -self.max_stop_tokens :].unsqueeze(1), self.stop_tokens)
equal_generated_inputs = torch.all(generated_inputs, dim=2)
# Mark the position where a stop token has been produced for each input in the batch,
# but only if the corresponding entry is not already set
sequence_idx = torch.any(equal_generated_inputs, dim=1)
sequence_set_mask = self.stop_tokens_idx == 0
self.stop_tokens_idx[sequence_idx & sequence_set_mask] = input_ids.shape[-1]
return torch.all(self.stop_tokens_idx)
class CoVoSTDataset(Dataset):
def __init__(self, processor, data_dir, split,
lang="en_zh-CN", rank=0, world_size=1):
self.data = load_dataset("facebook/covost2",
lang,
data_dir=data_dir,
split=split,
trust_remote_code=True
)
self.training = "train" in split
self.processor = processor
self.instruction = INSTSRUCTION[lang]
if world_size > 1:
self.data = self.data.shard(world_size, rank)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
"""
{'client_id': '0013037a1d45cc33460806cc3f8ecee9d536c45639ba4cbbf1564f1c051f53ff3c9f89ef2f1bf04badf55b3a2e7654c086f903681a7b6299616cff6f67598eff',
'file': '{data_dir}/clips/common_voice_en_699711.mp3',
'audio': {'path': '{data_dir}/clips/common_voice_en_699711.mp3',
'array': array([-1.28056854e-09, -1.74622983e-09, -1.16415322e-10, ...,
3.92560651e-10, 6.62794264e-10, -3.89536581e-09]),
'sampling_rate': 16000},
'sentence': '"She\'ll be all right."',
'translation': '她会没事的。',
'id': 'common_voice_en_699711'}
"""
data = self.data[idx]
user_message = {
'role': 'user',
'content': '<|audio_1|>\n' + self.instruction,
}
prompt = self.processor.tokenizer.apply_chat_template(
[user_message], tokenize=False, add_generation_prompt=True
)
inputs = self.processor(text=prompt, audios=[(data["audio"]["array"], data["audio"]["sampling_rate"])], return_tensors='pt')
answer = f"{data['translation']}{ANSWER_SUFFIX}"
answer_ids = self.processor.tokenizer(answer, return_tensors='pt').input_ids
if self.training:
input_ids = torch.cat([inputs.input_ids, answer_ids], dim=1)
labels = torch.full_like(input_ids, _IGNORE_INDEX)
labels[:, -answer_ids.shape[1] :] = answer_ids
else:
input_ids = inputs.input_ids
labels = answer_ids
return {
'input_ids': input_ids,
'labels': labels,
'input_audio_embeds': inputs.input_audio_embeds,
'audio_embed_sizes': inputs.audio_embed_sizes,
}
def pad_sequence(sequences, padding_side='right', padding_value=0):
"""
Pad a list of sequences to the same length.
sequences: list of tensors in [seq_len, *] shape
"""
assert padding_side in ['right', 'left']
max_size = sequences[0].size()
trailing_dims = max_size[1:]
max_len = max(len(seq) for seq in sequences)
batch_size = len(sequences)
output = sequences[0].new_full((batch_size, max_len) + trailing_dims, padding_value)
for i, seq in enumerate(sequences):
length = seq.size(0)
if padding_side == 'right':
output.data[i, :length] = seq
else:
output.data[i, -length:] = seq
return output
def cat_with_pad(tensors, dim, padding_value=0):
"""
cat along dim, while pad to max for all other dims
"""
ndim = tensors[0].dim()
assert all(
t.dim() == ndim for t in tensors[1:]
), 'All tensors must have the same number of dimensions'
out_size = [max(t.shape[i] for t in tensors) for i in range(ndim)]
out_size[dim] = sum(t.shape[dim] for t in tensors)
output = tensors[0].new_full(out_size, padding_value)
index = 0
for t in tensors:
# Create a slice list where every dimension except dim is full slice
slices = [slice(0, t.shape[d]) for d in range(ndim)]
# Update only the concat dimension slice
slices[dim] = slice(index, index + t.shape[dim])
output[slices] = t
index += t.shape[dim]
return output
def covost_collate_fn(batch):
input_ids_list = []
labels_list = []
input_audio_embeds_list = []
audio_embed_sizes_list = []
audio_attention_mask_list = []
for inputs in batch:
input_ids_list.append(inputs['input_ids'][0])
labels_list.append(inputs['labels'][0])
input_audio_embeds_list.append(inputs['input_audio_embeds'])
audio_embed_sizes_list.append(inputs['audio_embed_sizes'])
audio_attention_mask_list.append(
inputs['input_audio_embeds'].new_full((inputs['input_audio_embeds'].size(1),), True, dtype=torch.bool)
)
try:
input_ids = pad_sequence(input_ids_list, padding_side='left', padding_value=0)
labels = pad_sequence(labels_list, padding_side='left', padding_value=0)
audio_attention_mask = (
pad_sequence(audio_attention_mask_list, padding_side='right', padding_value=False)
if len(audio_attention_mask_list) > 1
else None
)
except Exception as e:
print(e)
print(input_ids_list)
print(labels_list)
raise
attention_mask = (input_ids != 0).long()
input_audio_embeds = cat_with_pad(input_audio_embeds_list, dim=0)
audio_embed_sizes = torch.cat(audio_embed_sizes_list)
return BatchFeature(
{
'input_ids': input_ids,
'labels': labels,
'attention_mask': attention_mask,
'input_audio_embeds': input_audio_embeds,
'audio_embed_sizes': audio_embed_sizes,
'audio_attention_mask': audio_attention_mask,
'input_mode': 2, # speech mode
}
)
def create_model(model_name_or_path, use_flash_attention=False):
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
torch_dtype=torch.bfloat16 if use_flash_attention else torch.float32,
_attn_implementation='flash_attention_2' if use_flash_attention else 'sdpa',
trust_remote_code=True,
).to('cuda')
return model
@torch.no_grad()
def evaluate(
model, processor, eval_dataset, save_path=None, disable_tqdm=False, eval_batch_size=1
):
rank = int(os.environ.get('RANK', 0))
local_rank = int(os.environ.get('LOCAL_RANK', 0))
model.eval()
all_generated_texts = []
all_labels = []
eval_dataloader = torch.utils.data.DataLoader(
eval_dataset,
batch_size=eval_batch_size,
collate_fn=covost_collate_fn,
shuffle=False,
drop_last=False,
num_workers=8,
prefetch_factor=2,
pin_memory=True,
)
stop_tokens = ["<|end|>", processor.tokenizer.eos_token]
stop_tokens_ids = processor.tokenizer(stop_tokens, add_special_tokens=False, padding="longest", return_tensors="pt")["input_ids"]
stop_tokens_ids = stop_tokens_ids.to(f'cuda:{local_rank}')
for inputs in tqdm(
eval_dataloader, disable=(rank != 0) or disable_tqdm, desc='running eval'
):
stopping_criteria=StoppingCriteriaList([MultipleTokenBatchStoppingCriteria(stop_tokens_ids, batch_size=inputs.input_ids.size(0))])
inputs = inputs.to(f'cuda:{local_rank}')
generated_ids = model.generate(
**inputs, eos_token_id=processor.tokenizer.eos_token_id, max_new_tokens=64,
stopping_criteria=stopping_criteria,
)
stop_tokens_idx = stopping_criteria[0].stop_tokens_idx.reshape(inputs.input_ids.size(0), -1)[:, 0]
stop_tokens_idx = torch.where(
stop_tokens_idx > 0,
stop_tokens_idx - stop_tokens_ids.shape[-1],
generated_ids.shape[-1],
)
generated_text = [
processor.decode(_pred_ids[inputs["input_ids"].shape[1] : _stop_tokens_idx], skip_special_tokens=True, clean_up_tokenization_spaces=False)
for _pred_ids, _stop_tokens_idx in zip(generated_ids, stop_tokens_idx)
]
all_generated_texts.extend(generated_text)
labels = [processor.decode(_label_ids[_label_ids != 0]).rstrip(ANSWER_SUFFIX) for _label_ids in inputs["labels"]]
all_labels.extend(labels)
all_generated_texts = gather_object(all_generated_texts)
all_labels = gather_object(all_labels)
if rank == 0:
assert len(all_generated_texts) == len(all_labels)
bleu = sacrebleu.corpus_bleu(all_generated_texts, [all_labels])
print(bleu)
if save_path:
with open(save_path, 'w') as f:
save_dict = {
'all_generated_texts': all_generated_texts,
'all_labels': all_labels,
'score': bleu.score,
}
json.dump(save_dict, f)
return bleu.score
return None
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--model_name_or_path',
type=str,
default='microsoft/Phi-4-multimodal-instruct',
help='Model name or path to load from',
)
parser.add_argument(
"--common_voice_dir",
type=str,
default="CommonVoice/EN",
help="Unzipped Common Voice Audio dataset directory, refer to https://commonvoice.mozilla.org/en/datasets, version 4.0",
)
parser.add_argument(
"--lang",
type=str,
default="en_sl",
help="Language pair for translation.",
)
parser.add_argument('--use_flash_attention', action='store_true', help='Use Flash Attention')
parser.add_argument('--output_dir', type=str, default='./output/', help='Output directory')
parser.add_argument('--batch_size', type=int, default=128, help='Batch size')
parser.add_argument(
'--batch_size_per_gpu',
type=int,
default=32,
help='Batch size per GPU (adjust this to fit in GPU memory)',
)
parser.add_argument(
'--num_train_epochs', type=int, default=1, help='Number of training epochs'
)
parser.add_argument('--learning_rate', type=float, default=4.0e-5, help='Learning rate')
parser.add_argument('--wd', type=float, default=0.01, help='Weight decay')
parser.add_argument('--no-tqdm', dest='tqdm', action='store_false', help='Disable tqdm')
args = parser.parse_args()
accelerator = Accelerator()
with accelerator.local_main_process_first():
processor = AutoProcessor.from_pretrained(
args.model_name_or_path,
trust_remote_code=True,
)
model = create_model(
args.model_name_or_path,
use_flash_attention=args.use_flash_attention,
)
model.set_lora_adapter('speech')
rank = int(os.environ.get('RANK', 0))
world_size = int(os.environ.get('WORLD_SIZE', 1))
eval_dataset = CoVoSTDataset(processor,
data_dir=args.common_voice_dir,
split=f'test[:{_EVAL_SIZE}]',
lang=args.lang,
rank=rank,
world_size=world_size)
train_dataset = CoVoSTDataset(processor,
data_dir=args.common_voice_dir,
split=f'train[:{_TRAIN_SIZE}]',
lang=args.lang)
num_gpus = accelerator.num_processes
print(f'training on {num_gpus} GPUs')
assert (
args.batch_size % (num_gpus * args.batch_size_per_gpu) == 0
), 'Batch size must be divisible by the number of GPUs'
gradient_accumulation_steps = args.batch_size // (num_gpus * args.batch_size_per_gpu)
if args.use_flash_attention:
fp16 = False
bf16 = True
else:
fp16 = True
bf16 = False
# hard coded training args
training_args = TrainingArguments(
num_train_epochs=args.num_train_epochs,
per_device_train_batch_size=args.batch_size_per_gpu,
gradient_checkpointing=True,
gradient_checkpointing_kwargs={'use_reentrant': False},
gradient_accumulation_steps=gradient_accumulation_steps,
optim='adamw_torch',
adam_beta1=0.9,
adam_beta2=0.95,
adam_epsilon=1e-7,
learning_rate=args.learning_rate,
weight_decay=args.wd,
max_grad_norm=1.0,
lr_scheduler_type='linear',
warmup_steps=50,
logging_steps=10,
output_dir=args.output_dir,
save_strategy='no',
save_total_limit=10,
save_only_model=True,
bf16=bf16,
fp16=fp16,
remove_unused_columns=False,
report_to='none',
deepspeed=None,
disable_tqdm=not args.tqdm,
dataloader_num_workers=4,
ddp_find_unused_parameters=True, # for unused SigLIP layers
)
# eval before fine-tuning
out_path = Path(training_args.output_dir)
out_path.mkdir(parents=True, exist_ok=True)
score = evaluate(
model,
processor,
eval_dataset,
save_path=out_path / 'eval_before.json',
disable_tqdm=not args.tqdm,
eval_batch_size=args.batch_size_per_gpu,
)
if accelerator.is_main_process:
print(f'BLEU Score before finetuning: {score}')
trainer = Trainer(
model=model,
args=training_args,
data_collator=covost_collate_fn,
train_dataset=train_dataset,
)
trainer.train()
trainer.save_model()
if accelerator.is_main_process:
processor.save_pretrained(training_args.output_dir)
accelerator.wait_for_everyone()
# eval after fine-tuning (load saved checkpoint)
# first try to clear GPU memory
del model
del trainer
__import__('gc').collect()
torch.cuda.empty_cache()
# reload the model for inference
model = AutoModelForCausalLM.from_pretrained(
training_args.output_dir,
torch_dtype=torch.bfloat16 if args.use_flash_attention else torch.float32,
trust_remote_code=True,
_attn_implementation='flash_attention_2' if args.use_flash_attention else 'sdpa',
).to('cuda')
score = evaluate(
model,
processor,
eval_dataset,
save_path=out_path / 'eval_after.json',
disable_tqdm=not args.tqdm,
eval_batch_size=args.batch_size_per_gpu,
)
if accelerator.is_main_process:
print(f'BLEU Score after finetuning: {score}')
if __name__ == '__main__':
main()

556
sample_finetune_vision.py Normal file
View File

@ -0,0 +1,556 @@
"""
finetune Phi-4-multimodal-instruct on an image task
scipy==1.15.1
peft==0.13.2
backoff==2.2.1
transformers==4.47.0
accelerate==1.3.0
"""
import argparse
import json
import os
import tempfile
import zipfile
from pathlib import Path
import torch
from accelerate import Accelerator
from accelerate.utils import gather_object
from datasets import load_dataset
from huggingface_hub import hf_hub_download
from PIL import Image
from torch.utils.data import Dataset
from tqdm import tqdm
from transformers import (
AutoModelForCausalLM,
AutoProcessor,
BatchFeature,
Trainer,
TrainingArguments,
)
DEFAULT_INSTSRUCTION = "Answer with the option's letter from the given choices directly."
_IGNORE_INDEX = -100
_TRAIN_SIZE = 8000
_EVAL_SIZE = 500
_MAX_TRAINING_LENGTH = 8192
class PmcVqaTrainDataset(Dataset):
def __init__(self, processor, data_size, instruction=DEFAULT_INSTSRUCTION):
# Download the file
file_path = hf_hub_download(
repo_id='xmcmic/PMC-VQA', # repository name
filename='images_2.zip', # file to download
repo_type='dataset', # specify it's a dataset repo
)
# file_path will be the local path where the file was downloaded
print(f'File downloaded to: {file_path}')
# unzip to temp folder
self.image_folder = Path(tempfile.mkdtemp())
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall(self.image_folder)
data_files = {
'train': 'https://huggingface.co/datasets/xmcmic/PMC-VQA/resolve/main/train_2.csv',
}
split = 'train' if data_size is None else f'train[:{data_size}]'
self.annotations = load_dataset('xmcmic/PMC-VQA', data_files=data_files, split=split)
self.processor = processor
self.instruction = instruction
def __len__(self):
return len(self.annotations)
def __getitem__(self, idx):
"""
{'index': 35,
'Figure_path': 'PMC8253797_Fig4_11.jpg',
'Caption': 'A slightly altered cell . (c-c‴) A highly altered cell as seen from 4 different angles . Note mitochondria/mitochondrial networks (green), Golgi complexes (red), cell nuclei (light blue) and the cell outline (yellow).',
'Question': ' What color is used to label the Golgi complexes in the image?',
'Choice A': ' A: Green ',
'Choice B': ' B: Red ',
'Choice C': ' C: Light blue ',
'Choice D': ' D: Yellow',
'Answer': 'B',
'split': 'train'}
"""
annotation = self.annotations[idx]
image = Image.open(self.image_folder / 'figures' / annotation['Figure_path'])
question = annotation['Question']
choices = [annotation[f'Choice {chr(ord("A") + i)}'] for i in range(4)]
user_message = {
'role': 'user',
'content': '<|image_1|>' + '\n'.join([question] + choices + [self.instruction]),
}
prompt = self.processor.tokenizer.apply_chat_template(
[user_message], tokenize=False, add_generation_prompt=True
)
answer = f'{annotation["Answer"]}<|end|><|endoftext|>'
inputs = self.processor(prompt, images=[image], return_tensors='pt')
answer_ids = self.processor.tokenizer(answer, return_tensors='pt').input_ids
input_ids = torch.cat([inputs.input_ids, answer_ids], dim=1)
labels = torch.full_like(input_ids, _IGNORE_INDEX)
labels[:, -answer_ids.shape[1] :] = answer_ids
if input_ids.size(1) > _MAX_TRAINING_LENGTH:
input_ids = input_ids[:, :_MAX_TRAINING_LENGTH]
labels = labels[:, :_MAX_TRAINING_LENGTH]
if torch.all(labels == _IGNORE_INDEX).item():
# workaround to make sure loss compute won't fail
labels[:, -1] = self.processor.tokenizer.eos_token_id
return {
'input_ids': input_ids,
'labels': labels,
'input_image_embeds': inputs.input_image_embeds,
'image_attention_mask': inputs.image_attention_mask,
'image_sizes': inputs.image_sizes,
}
def __del__(self):
__import__('shutil').rmtree(self.image_folder)
class PmcVqaEvalDataset(Dataset):
def __init__(
self, processor, data_size, instruction=DEFAULT_INSTSRUCTION, rank=0, world_size=1
):
# Download the file
file_path = hf_hub_download(
repo_id='xmcmic/PMC-VQA', # repository name
filename='images_2.zip', # file to download
repo_type='dataset', # specify it's a dataset repo
)
# file_path will be the local path where the file was downloaded
print(f'File downloaded to: {file_path}')
# unzip to temp folder
self.image_folder = Path(tempfile.mkdtemp())
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall(self.image_folder)
data_files = {
'test': 'https://huggingface.co/datasets/xmcmic/PMC-VQA/resolve/main/test_2.csv',
}
split = 'test' if data_size is None else f'test[:{data_size}]'
self.annotations = load_dataset(
'xmcmic/PMC-VQA', data_files=data_files, split=split
).shard(num_shards=world_size, index=rank)
self.processor = processor
self.instruction = instruction
def __len__(self):
return len(self.annotations)
def __getitem__(self, idx):
"""
{'index': 62,
'Figure_path': 'PMC8253867_Fig2_41.jpg',
'Caption': 'CT pulmonary angiogram reveals encasement and displacement of the left anterior descending coronary artery ( blue arrows ).',
'Question': ' What is the name of the artery encased and displaced in the image? ',
'Choice A': ' A: Right Coronary Artery ',
'Choice B': ' B: Left Anterior Descending Coronary Artery ',
'Choice C': ' C: Circumflex Coronary Artery ',
'Choice D': ' D: Superior Mesenteric Artery ',
'Answer': 'B',
'split': 'test'}
"""
annotation = self.annotations[idx]
image = Image.open(self.image_folder / 'figures' / annotation['Figure_path'])
question = annotation['Question']
choices = [annotation[f'Choice {chr(ord("A") + i)}'] for i in range(4)]
user_message = {
'role': 'user',
'content': '<|image_1|>' + '\n'.join([question] + choices + [self.instruction]),
}
prompt = self.processor.tokenizer.apply_chat_template(
[user_message], tokenize=False, add_generation_prompt=True
)
answer = annotation['Answer']
inputs = self.processor(prompt, images=[image], return_tensors='pt')
unique_id = f'{annotation["index"]:010d}'
return {
'id': unique_id,
'input_ids': inputs.input_ids,
'input_image_embeds': inputs.input_image_embeds,
'image_attention_mask': inputs.image_attention_mask,
'image_sizes': inputs.image_sizes,
'answer': answer,
}
def __del__(self):
__import__('shutil').rmtree(self.image_folder)
def pad_sequence(sequences, padding_side='right', padding_value=0):
"""
Pad a list of sequences to the same length.
sequences: list of tensors in [seq_len, *] shape
"""
assert padding_side in ['right', 'left']
max_size = sequences[0].size()
trailing_dims = max_size[1:]
max_len = max(len(seq) for seq in sequences)
batch_size = len(sequences)
output = sequences[0].new_full((batch_size, max_len) + trailing_dims, padding_value)
for i, seq in enumerate(sequences):
length = seq.size(0)
if padding_side == 'right':
output.data[i, :length] = seq
else:
output.data[i, -length:] = seq
return output
def cat_with_pad(tensors, dim, padding_value=0):
"""
cat along dim, while pad to max for all other dims
"""
ndim = tensors[0].dim()
assert all(
t.dim() == ndim for t in tensors[1:]
), 'All tensors must have the same number of dimensions'
out_size = [max(t.shape[i] for t in tensors) for i in range(ndim)]
out_size[dim] = sum(t.shape[dim] for t in tensors)
output = tensors[0].new_full(out_size, padding_value)
index = 0
for t in tensors:
# Create a slice list where every dimension except dim is full slice
slices = [slice(0, t.shape[d]) for d in range(ndim)]
# Update only the concat dimension slice
slices[dim] = slice(index, index + t.shape[dim])
output[slices] = t
index += t.shape[dim]
return output
def pmc_vqa_collate_fn(batch):
input_ids_list = []
labels_list = []
input_image_embeds_list = []
image_attention_mask_list = []
image_sizes_list = []
for inputs in batch:
input_ids_list.append(inputs['input_ids'][0])
labels_list.append(inputs['labels'][0])
input_image_embeds_list.append(inputs['input_image_embeds'])
image_attention_mask_list.append(inputs['image_attention_mask'])
image_sizes_list.append(inputs['image_sizes'])
input_ids = pad_sequence(input_ids_list, padding_side='right', padding_value=0)
labels = pad_sequence(labels_list, padding_side='right', padding_value=0)
attention_mask = (input_ids != 0).long()
input_image_embeds = cat_with_pad(input_image_embeds_list, dim=0)
image_attention_mask = cat_with_pad(image_attention_mask_list, dim=0)
image_sizes = torch.cat(image_sizes_list)
return BatchFeature(
{
'input_ids': input_ids,
'labels': labels,
'attention_mask': attention_mask,
'input_image_embeds': input_image_embeds,
'image_attention_mask': image_attention_mask,
'image_sizes': image_sizes,
'input_mode': 1, # vision mode
}
)
def pmc_vqa_eval_collate_fn(batch):
input_ids_list = []
input_image_embeds_list = []
image_attention_mask_list = []
image_sizes_list = []
all_unique_ids = []
all_answers = []
for inputs in batch:
input_ids_list.append(inputs['input_ids'][0])
input_image_embeds_list.append(inputs['input_image_embeds'])
image_attention_mask_list.append(inputs['image_attention_mask'])
image_sizes_list.append(inputs['image_sizes'])
all_unique_ids.append(inputs['id'])
all_answers.append(inputs['answer'])
input_ids = pad_sequence(input_ids_list, padding_side='left', padding_value=0)
attention_mask = (input_ids != 0).long()
input_image_embeds = cat_with_pad(input_image_embeds_list, dim=0)
image_attention_mask = cat_with_pad(image_attention_mask_list, dim=0)
image_sizes = torch.cat(image_sizes_list)
return (
all_unique_ids,
all_answers,
BatchFeature(
{
'input_ids': input_ids,
'attention_mask': attention_mask,
'input_image_embeds': input_image_embeds,
'image_attention_mask': image_attention_mask,
'image_sizes': image_sizes,
'input_mode': 1, # vision mode
}
),
)
def create_model(model_name_or_path, use_flash_attention=False):
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
torch_dtype=torch.bfloat16 if use_flash_attention else torch.float32,
_attn_implementation='flash_attention_2' if use_flash_attention else 'sdpa',
trust_remote_code=True,
).to('cuda')
# remove parameters irrelevant to vision tasks
del model.model.embed_tokens_extend.audio_embed # remove audio encoder
for layer in model.model.layers:
# remove audio lora
del layer.mlp.down_proj.lora_A.speech
del layer.mlp.down_proj.lora_B.speech
del layer.mlp.gate_up_proj.lora_A.speech
del layer.mlp.gate_up_proj.lora_B.speech
del layer.self_attn.o_proj.lora_A.speech
del layer.self_attn.o_proj.lora_B.speech
del layer.self_attn.qkv_proj.lora_A.speech
del layer.self_attn.qkv_proj.lora_B.speech
# TODO remove unused vision layers?
return model
@torch.no_grad()
def evaluate(
model, processor, eval_dataset, save_path=None, disable_tqdm=False, eval_batch_size=1
):
rank = int(os.environ.get('RANK', 0))
local_rank = int(os.environ.get('LOCAL_RANK', 0))
model.eval()
all_answers = []
all_generated_texts = []
eval_dataloader = torch.utils.data.DataLoader(
eval_dataset,
batch_size=eval_batch_size,
collate_fn=pmc_vqa_eval_collate_fn,
shuffle=False,
drop_last=False,
num_workers=4,
prefetch_factor=2,
pin_memory=True,
)
for ids, answers, inputs in tqdm(
eval_dataloader, disable=(rank != 0) or disable_tqdm, desc='running eval'
):
all_answers.extend({'id': i, 'answer': a.strip().lower()} for i, a in zip(ids, answers))
inputs = inputs.to(f'cuda:{local_rank}')
generated_ids = model.generate(
**inputs, eos_token_id=processor.tokenizer.eos_token_id, max_new_tokens=64
)
input_len = inputs.input_ids.size(1)
generated_texts = processor.batch_decode(
generated_ids[:, input_len:],
skip_special_tokens=True,
clean_up_tokenization_spaces=False,
)
all_generated_texts.extend(
{'id': i, 'generated_text': g.strip().lower()} for i, g in zip(ids, generated_texts)
)
# gather outputs from all ranks
all_answers = gather_object(all_answers)
all_generated_texts = gather_object(all_generated_texts)
if rank == 0:
assert len(all_answers) == len(all_generated_texts)
acc = sum(
a['answer'] == g['generated_text'] for a, g in zip(all_answers, all_generated_texts)
) / len(all_answers)
if save_path:
with open(save_path, 'w') as f:
save_dict = {
'answers_unique': all_answers,
'generated_texts_unique': all_generated_texts,
'accuracy': acc,
}
json.dump(save_dict, f)
return acc
return None
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
'--model_name_or_path',
type=str,
default='microsoft/Phi-4-multimodal-instruct',
help='Model name or path to load from',
)
parser.add_argument('--use_flash_attention', action='store_true', help='Use Flash Attention')
parser.add_argument('--output_dir', type=str, default='./output/', help='Output directory')
parser.add_argument('--batch_size', type=int, default=16, help='Batch size')
parser.add_argument(
'--batch_size_per_gpu',
type=int,
default=1,
help='Batch size per GPU (adjust this to fit in GPU memory)',
)
parser.add_argument(
'--dynamic_hd',
type=int,
default=36,
help='Number of maximum image crops',
)
parser.add_argument(
'--num_train_epochs', type=int, default=1, help='Number of training epochs'
)
parser.add_argument('--learning_rate', type=float, default=4.0e-5, help='Learning rate')
parser.add_argument('--wd', type=float, default=0.01, help='Weight decay')
parser.add_argument('--no_tqdm', dest='tqdm', action='store_false', help='Disable tqdm')
parser.add_argument('--full_run', action='store_true', help='Run the full training and eval')
args = parser.parse_args()
accelerator = Accelerator()
with accelerator.local_main_process_first():
processor = AutoProcessor.from_pretrained(
args.model_name_or_path,
trust_remote_code=True,
dynamic_hd=args.dynamic_hd,
)
model = create_model(
args.model_name_or_path,
use_flash_attention=args.use_flash_attention,
)
# tune vision encoder and lora
model.set_lora_adapter('vision')
for param in model.model.embed_tokens_extend.image_embed.parameters():
param.requires_grad = True
rank = int(os.environ.get('RANK', 0))
world_size = int(os.environ.get('WORLD_SIZE', 1))
train_dataset = PmcVqaTrainDataset(processor, data_size=None if args.full_run else _TRAIN_SIZE)
eval_dataset = PmcVqaEvalDataset(
processor,
data_size=None if args.full_run else _EVAL_SIZE,
rank=rank,
world_size=world_size,
)
num_gpus = accelerator.num_processes
print(f'training on {num_gpus} GPUs')
assert (
args.batch_size % (num_gpus * args.batch_size_per_gpu) == 0
), 'Batch size must be divisible by the number of GPUs'
gradient_accumulation_steps = args.batch_size // (num_gpus * args.batch_size_per_gpu)
if args.use_flash_attention:
fp16 = False
bf16 = True
else:
fp16 = True
bf16 = False
# hard coded training args
training_args = TrainingArguments(
num_train_epochs=args.num_train_epochs,
per_device_train_batch_size=args.batch_size_per_gpu,
gradient_checkpointing=True,
gradient_checkpointing_kwargs={'use_reentrant': False},
gradient_accumulation_steps=gradient_accumulation_steps,
optim='adamw_torch',
adam_beta1=0.9,
adam_beta2=0.95,
adam_epsilon=1e-7,
learning_rate=args.learning_rate,
weight_decay=args.wd,
max_grad_norm=1.0,
lr_scheduler_type='linear',
warmup_steps=50,
logging_steps=10,
output_dir=args.output_dir,
save_strategy='no',
save_total_limit=10,
save_only_model=True,
bf16=bf16,
fp16=fp16,
remove_unused_columns=False,
report_to='none',
deepspeed=None,
disable_tqdm=not args.tqdm,
dataloader_num_workers=4,
ddp_find_unused_parameters=True, # for unused SigLIP layers
)
# eval before fine-tuning
out_path = Path(training_args.output_dir)
out_path.mkdir(parents=True, exist_ok=True)
acc = evaluate(
model,
processor,
eval_dataset,
save_path=out_path / 'eval_before.json',
disable_tqdm=not args.tqdm,
eval_batch_size=args.batch_size_per_gpu,
)
if accelerator.is_main_process:
print(f'Accuracy before finetuning: {acc}')
trainer = Trainer(
model=model,
args=training_args,
data_collator=pmc_vqa_collate_fn,
train_dataset=train_dataset,
)
trainer.train()
trainer.save_model()
accelerator.wait_for_everyone()
# eval after fine-tuning (load saved checkpoint)
# first try to clear GPU memory
del model
del trainer
__import__('gc').collect()
torch.cuda.empty_cache()
# reload the model for inference
model = AutoModelForCausalLM.from_pretrained(
training_args.output_dir,
torch_dtype=torch.bfloat16 if args.use_flash_attention else torch.float32,
trust_remote_code=True,
_attn_implementation='flash_attention_2' if args.use_flash_attention else 'sdpa',
).to('cuda')
acc = evaluate(
model,
processor,
eval_dataset,
save_path=out_path / 'eval_after.json',
disable_tqdm=not args.tqdm,
eval_batch_size=args.batch_size_per_gpu,
)
if accelerator.is_main_process:
print(f'Accuracy after finetuning: {acc}')
if __name__ == '__main__':
main()

243
sample_inference_phi4mm.py Normal file
View File

@ -0,0 +1,243 @@
import os
import requests
import torch
from PIL import Image
import soundfile
from transformers import AutoModelForCausalLM, AutoProcessor, GenerationConfig
model_path = './'
kwargs = {}
kwargs['torch_dtype'] = torch.bfloat16
processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
print(processor.tokenizer)
model = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=True,
torch_dtype='auto',
_attn_implementation='flash_attention_2',
).cuda()
print("model.config._attn_implementation:", model.config._attn_implementation)
generation_config = GenerationConfig.from_pretrained(model_path, 'generation_config.json')
user_prompt = '<|user|>'
assistant_prompt = '<|assistant|>'
prompt_suffix = '<|end|>'
#################################################### text-only ####################################################
prompt = f'{user_prompt}what is the answer for 1+1? Explain it.{prompt_suffix}{assistant_prompt}'
print(f'>>> Prompt\n{prompt}')
inputs = processor(prompt, images=None, return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')
#################################################### vision (single-turn) ####################################################
# single-image prompt
prompt = f'{user_prompt}<|image_1|>What is shown in this image?{prompt_suffix}{assistant_prompt}'
url = 'https://www.ilankelman.org/stopsigns/australia.jpg'
print(f'>>> Prompt\n{prompt}')
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=prompt, images=image, return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')
#################################################### vision (multi-turn) ####################################################
# chat template
chat = [
{'role': 'user', 'content': f'<|image_1|>What is shown in this image?'},
{
'role': 'assistant',
'content': "The image depicts a street scene with a prominent red stop sign in the foreground. The background showcases a building with traditional Chinese architecture, characterized by its red roof and ornate decorations. There are also several statues of lions, which are common in Chinese culture, positioned in front of the building. The street is lined with various shops and businesses, and there's a car passing by.",
},
{'role': 'user', 'content': 'What is so special about this image'},
]
url = 'https://www.ilankelman.org/stopsigns/australia.jpg'
image = Image.open(requests.get(url, stream=True).raw)
prompt = processor.tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
# need to remove last <|endoftext|> if it is there, which is used for training, not inference. For training, make sure to add <|endoftext|> in the end.
if prompt.endswith('<|endoftext|>'):
prompt = prompt.rstrip('<|endoftext|>')
print(f'>>> Prompt\n{prompt}')
inputs = processor(prompt, [image], return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')
########################### vision (multi-frame) ################################
images = []
placeholder = ''
for i in range(1, 5):
url = f'https://image.slidesharecdn.com/azureintroduction-191206101932/75/Introduction-to-Microsoft-Azure-Cloud-{i}-2048.jpg'
images.append(Image.open(requests.get(url, stream=True).raw))
placeholder += f'<|image_{i}|>'
messages = [
{'role': 'user', 'content': placeholder + 'Summarize the deck of slides.'},
]
prompt = processor.tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
print(f'>>> Prompt\n{prompt}')
inputs = processor(prompt, images, return_tensors='pt').to('cuda:0')
generation_args = {
'max_new_tokens': 1000,
'temperature': 0.0,
'do_sample': False,
}
generate_ids = model.generate(
**inputs, **generation_args, generation_config=generation_config,
)
# remove input tokens
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(response)
# NOTE: Please prepare the audio file 'examples/what_is_the_traffic_sign_in_the_image.wav'
# and audio file 'examples/what_is_shown_in_this_image.wav' before running the following code
# Basically you can record your own voice for the question "What is the traffic sign in the image?" in "examples/what_is_the_traffic_sign_in_the_image.wav".
# And you can record your own voice for the question "What is shown in this image?" in "examples/what_is_shown_in_this_image.wav".
AUDIO_FILE_1 = 'examples/what_is_the_traffic_sign_in_the_image.wav'
AUDIO_FILE_2 = 'examples/what_is_shown_in_this_image.wav'
if not os.path.exists(AUDIO_FILE_1):
raise FileNotFoundError(f'Please prepare the audio file {AUDIO_FILE_1} before running the following code.')
########################## vision-speech ################################
prompt = f'{user_prompt}<|image_1|><|audio_1|>{prompt_suffix}{assistant_prompt}'
url = 'https://www.ilankelman.org/stopsigns/australia.jpg'
print(f'>>> Prompt\n{prompt}')
image = Image.open(requests.get(url, stream=True).raw)
audio = soundfile.read(AUDIO_FILE_1)
inputs = processor(text=prompt, images=[image], audios=[audio], return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')
########################## speech only ################################
speech_prompt = "Based on the attached audio, generate a comprehensive text transcription of the spoken content."
prompt = f'{user_prompt}<|audio_1|>{speech_prompt}{prompt_suffix}{assistant_prompt}'
print(f'>>> Prompt\n{prompt}')
audio = soundfile.read(AUDIO_FILE_1)
inputs = processor(text=prompt, audios=[audio], return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')
if not os.path.exists(AUDIO_FILE_2):
raise FileNotFoundError(f'Please prepare the audio file {AUDIO_FILE_2} before running the following code.')
########################### speech only (multi-turn) ################################
audio_1 = soundfile.read(AUDIO_FILE_2)
audio_2 = soundfile.read(AUDIO_FILE_1)
chat = [
{'role': 'user', 'content': f'<|audio_1|>Based on the attached audio, generate a comprehensive text transcription of the spoken content.'},
{
'role': 'assistant',
'content': "What is shown in this image.",
},
{'role': 'user', 'content': f'<|audio_2|>Based on the attached audio, generate a comprehensive text transcription of the spoken content.'},
]
prompt = processor.tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
# need to remove last <|endoftext|> if it is there, which is used for training, not inference. For training, make sure to add <|endoftext|> in the end.
if prompt.endswith('<|endoftext|>'):
prompt = prompt.rstrip('<|endoftext|>')
print(f'>>> Prompt\n{prompt}')
inputs = processor(text=prompt, audios=[audio_1, audio_2], return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')
#################################################### vision-speech (multi-turn) ####################################################
# chat template
audio_1 = soundfile.read(AUDIO_FILE_2)
audio_2 = soundfile.read(AUDIO_FILE_1)
chat = [
{'role': 'user', 'content': f'<|image_1|><|audio_1|>'},
{
'role': 'assistant',
'content': "The image depicts a street scene with a prominent red stop sign in the foreground. The background showcases a building with traditional Chinese architecture, characterized by its red roof and ornate decorations. There are also several statues of lions, which are common in Chinese culture, positioned in front of the building. The street is lined with various shops and businesses, and there's a car passing by.",
},
{'role': 'user', 'content': f'<|audio_2|>'},
]
url = 'https://www.ilankelman.org/stopsigns/australia.jpg'
image = Image.open(requests.get(url, stream=True).raw)
prompt = processor.tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
# need to remove last <|endoftext|> if it is there, which is used for training, not inference. For training, make sure to add <|endoftext|> in the end.
if prompt.endswith('<|endoftext|>'):
prompt = prompt.rstrip('<|endoftext|>')
print(f'>>> Prompt\n{prompt}')
inputs = processor(text=prompt, images=[image], audios=[audio_1, audio_2], return_tensors='pt').to('cuda:0')
generate_ids = model.generate(
**inputs,
max_new_tokens=1000,
generation_config=generation_config,
)
generate_ids = generate_ids[:, inputs['input_ids'].shape[1] :]
response = processor.batch_decode(
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)[0]
print(f'>>> Response\n{response}')

24
special_tokens_map.json Normal file
View File

@ -0,0 +1,24 @@
{
"bos_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"pad_token": "<|endoftext|>",
"unk_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
}

View File

@ -0,0 +1,23 @@
{
"auto_mapping": null,
"base_model_name_or_path": "TBA",
"bias": "none",
"fan_in_fan_out": false,
"inference_mode": true,
"init_lora_weights": true,
"layers_pattern": null,
"layers_to_transform": null,
"lora_alpha": 640,
"lora_dropout": 0.01,
"modules_to_save": [],
"peft_type": "LORA",
"r": 320,
"revision": null,
"target_modules": [
"qkv_proj",
"o_proj",
"gate_up_proj",
"down_proj"
],
"task_type": "CAUSAL_LM"
}

BIN
speech-lora/adapter_model.safetensors (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,12 @@
{
"<|/tool_call|>": 200026,
"<|/tool|>": 200024,
"<|assistant|>": 200019,
"<|end|>": 200020,
"<|system|>": 200022,
"<|tag|>": 200028,
"<|tool_call|>": 200025,
"<|tool_response|>": 200027,
"<|tool|>": 200023,
"<|user|>": 200021
}

View File

@ -0,0 +1,24 @@
{
"bos_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"pad_token": "<|endoftext|>",
"unk_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
}

BIN
speech-lora/tokenizer.json (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,125 @@
{
"add_prefix_space": false,
"added_tokens_decoder": {
"200010": {
"content": "<|endoftext10|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200011": {
"content": "<|endoftext11|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"199999": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200018": {
"content": "<|endofprompt|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200019": {
"content": "<|assistant|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200020": {
"content": "<|end|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200021": {
"content": "<|user|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200022": {
"content": "<|system|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200023": {
"content": "<|tool|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200024": {
"content": "<|/tool|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200025": {
"content": "<|tool_call|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200026": {
"content": "<|/tool_call|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200027": {
"content": "<|tool_response|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200028": {
"content": "<|tag|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
}
},
"bos_token": "<|endoftext|>",
"chat_template": "{% for message in messages %}{% if message['role'] == 'system' and 'tools' in message and message['tools'] is not none %}{{ '<|' + message['role'] + '|>' + message['content'] + '<|tool|>' + message['tools'] + '<|/tool|>' + '<|end|>' }}{% else %}{{ '<|' + message['role'] + '|>' + message['content'] + '<|end|>' }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>' }}{% else %}{{ eos_token }}{% endif %}",
"clean_up_tokenization_spaces": false,
"eos_token": "<|endoftext|>",
"model_max_length": 128000,
"pad_token": "<|endoftext|>",
"tokenizer_class": "GPT2TokenizerFast",
"unk_token": "<|endoftext|>"
}

1
speech-lora/vocab.json Normal file

File diff suppressed because one or more lines are too long

2906
speech_conformer_encoder.py Normal file

File diff suppressed because it is too large Load Diff

BIN
tokenizer.json (Stored with Git LFS) Normal file

Binary file not shown.

125
tokenizer_config.json Normal file
View File

@ -0,0 +1,125 @@
{
"add_prefix_space": false,
"added_tokens_decoder": {
"200010": {
"content": "<|endoftext10|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200011": {
"content": "<|endoftext11|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"199999": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200018": {
"content": "<|endofprompt|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200019": {
"content": "<|assistant|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200020": {
"content": "<|end|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200021": {
"content": "<|user|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200022": {
"content": "<|system|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200023": {
"content": "<|tool|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200024": {
"content": "<|/tool|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200025": {
"content": "<|tool_call|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200026": {
"content": "<|/tool_call|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200027": {
"content": "<|tool_response|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200028": {
"content": "<|tag|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
}
},
"bos_token": "<|endoftext|>",
"chat_template": "{% for message in messages %}{% if message['role'] == 'system' and 'tools' in message and message['tools'] is not none %}{{ '<|' + message['role'] + '|>' + message['content'] + '<|tool|>' + message['tools'] + '<|/tool|>' + '<|end|>' }}{% else %}{{ '<|' + message['role'] + '|>' + message['content'] + '<|end|>' }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>' }}{% else %}{{ eos_token }}{% endif %}",
"clean_up_tokenization_spaces": false,
"eos_token": "<|endoftext|>",
"model_max_length": 131072,
"pad_token": "<|endoftext|>",
"tokenizer_class": "GPT2TokenizerFast",
"unk_token": "<|endoftext|>"
}

View File

@ -0,0 +1,23 @@
{
"auto_mapping": null,
"base_model_name_or_path": "TBA",
"bias": "none",
"fan_in_fan_out": false,
"inference_mode": true,
"init_lora_weights": true,
"layers_pattern": null,
"layers_to_transform": null,
"lora_alpha": 512,
"lora_dropout": 0.0,
"modules_to_save": [],
"peft_type": "LORA",
"r": 256,
"revision": null,
"target_modules": [
"qkv_proj",
"o_proj",
"gate_up_proj",
"down_proj"
],
"task_type": "CAUSAL_LM"
}

BIN
vision-lora/adapter_model.safetensors (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,12 @@
{
"<|/tool_call|>": 200026,
"<|/tool|>": 200024,
"<|assistant|>": 200019,
"<|end|>": 200020,
"<|system|>": 200022,
"<|tag|>": 200028,
"<|tool_call|>": 200025,
"<|tool_response|>": 200027,
"<|tool|>": 200023,
"<|user|>": 200021
}

View File

@ -0,0 +1,24 @@
{
"bos_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"eos_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
},
"pad_token": "<|endoftext|>",
"unk_token": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false
}
}

BIN
vision-lora/tokenizer.json (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,125 @@
{
"add_prefix_space": false,
"added_tokens_decoder": {
"200010": {
"content": "<|endoftext10|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200011": {
"content": "<|endoftext11|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"199999": {
"content": "<|endoftext|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200018": {
"content": "<|endofprompt|>",
"lstrip": false,
"normalized": false,
"rstrip": false,
"single_word": false,
"special": true
},
"200019": {
"content": "<|assistant|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200020": {
"content": "<|end|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200021": {
"content": "<|user|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200022": {
"content": "<|system|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
},
"200023": {
"content": "<|tool|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200024": {
"content": "<|/tool|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200025": {
"content": "<|tool_call|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200026": {
"content": "<|/tool_call|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200027": {
"content": "<|tool_response|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": false
},
"200028": {
"content": "<|tag|>",
"lstrip": false,
"normalized": false,
"rstrip": true,
"single_word": false,
"special": true
}
},
"bos_token": "<|endoftext|>",
"chat_template": "{% for message in messages %}{% if message['role'] == 'system' and 'tools' in message and message['tools'] is not none %}{{ '<|' + message['role'] + '|>' + message['content'] + '<|tool|>' + message['tools'] + '<|/tool|>' + '<|end|>' }}{% else %}{{ '<|' + message['role'] + '|>' + message['content'] + '<|end|>' }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>' }}{% else %}{{ eos_token }}{% endif %}",
"clean_up_tokenization_spaces": false,
"eos_token": "<|endoftext|>",
"model_max_length": 128000,
"pad_token": "<|endoftext|>",
"tokenizer_class": "GPT2TokenizerFast",
"unk_token": "<|endoftext|>"
}

1
vision-lora/vocab.json Normal file

File diff suppressed because one or more lines are too long

1717
vision_siglip_navit.py Normal file

File diff suppressed because it is too large Load Diff

1
vocab.json Normal file

File diff suppressed because one or more lines are too long