Skip to content

Conversation

@alien-0119
Copy link
Collaborator

@alien-0119 alien-0119 commented Oct 31, 2025

What does this PR do?

Adds # (feature)
Add Granite Speech model and fast ut.

Usage Example:

import mindspore as ms
from scipy.io import wavfile
from mindone.transformers import AutoProcessor, GraniteSpeechForConditionalGeneration
from huggingface_hub import hf_hub_download
import numpy as np

model_name = "ibm-granite/granite-speech-3.3-2b"
processor = AutoProcessor.from_pretrained(model_name)
tokenizer = processor.tokenizer
# load audio
audio_path = hf_hub_download(repo_id=model_name, filename="10226_10111_000000.wav")
sr, wav = wavfile.read(audio_path)
# normalization
wav = wav.astype(np.float32) / 32768.0
wav = wav[np.newaxis, :]
assert wav.shape[0] == 1 and sr == 16000  # mono, 16khz

# create text prompt
system_prompt = "Knowledge Cutoff Date: April 2024.\nToday's Date: April 9, 2025.\nYou are Granite, developed by IBM. You are a helpful AI assistant"
user_prompt = "<|audio|>can you transcribe the speech into a written format?"
chat = [dict(role="system", content=system_prompt),dict(role="user", content=user_prompt)]
prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
# run the processor+model
model_inputs = processor(prompt, wav, return_tensors="np")
model_inputs = {k: ms.tensor(v) for k, v in model_inputs.items()}
model = GraniteSpeechForConditionalGeneration.from_pretrained(model_name)
model_outputs = model.generate(**model_inputs, max_new_tokens=200, do_sample=False, num_beams=1)

# Transformers includes the input IDs in the response.
num_input_tokens = model_inputs["input_ids"].shape[-1]
new_tokens = ms.mint.unsqueeze(model_outputs[0, num_input_tokens:], dim=0)
output_text = tokenizer.batch_decode(new_tokens, add_special_tokens=False, skip_special_tokens=True)
print(f"STT output = {output_text[0].upper()}")
# STT output = AFTER HIS NAP TIMOTHY LEISURELY STRETCHED FIRST ONE GRAY VELVET FOOT
# THEN ANOTHER STROLLED INDOLENTLY TO HIS PLATE TURNING OVER THE FOOD CAREFULLY SELECTING CHOICE BITS NOSING OUT THAT WHICH HE SCORNED UPON THE CLEAN HEARTH

Performance:
Experiments were tested on Ascend Atlas 800T A2 machines with mindspore 2.7.0 pynative mode.

model precision weight load(s) s/step
ibm-granite/granite-speech-3.3-2b fp32 147.425 0.436
ibm-granite/granite-speech-3.3-2b fp16 168.786 0.458
ibm-granite/granite-speech-3.3-2b bf16 122.458 0.453

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@xxx

@alien-0119 alien-0119 requested a review from vigo999 as a code owner October 31, 2025 01:23
@gemini-code-assist
Copy link
Contributor

Summary of Changes

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

This pull request integrates the Granite Speech model (version 4.54.1) into the mindone.transformers library, significantly enhancing its capabilities for speech-to-text conditional generation. The changes encompass the full model architecture, featuring a Conformer-based audio encoder, a Q-Former-based projector, and seamless integration with an auto-regressive language model. Additionally, it incorporates advanced Parameter-Efficient Fine-Tuning (PEFT) methods such as LoRA, LoftQ, and DoRA, alongside necessary updates to the auto-configuration and auto-modeling systems to support both the new Granite Speech model and the BLIP-2 QFormer.

Highlights

  • New Model Integration: Introduced the Granite Speech model (v4.54.1) for conditional generation, expanding the library's speech-to-text capabilities.
  • PEFT Support: Added comprehensive Parameter-Efficient Fine-Tuning (PEFT) configurations, including LoRA, LoftQ, and DoRA, to enable efficient fine-tuning of models.
  • Auto-Configuration and Auto-Modeling: Updated the auto-configuration and auto-modeling systems to seamlessly support the new Granite Speech model and the BLIP-2 QFormer.
  • New Test Suite: Included a dedicated test suite for the Granite Speech model to ensure its functional correctness and precision across various data types.
Using Gemini Code Assist

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

Invoking Gemini

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

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

Customization

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

Limitations & Feedback

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

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

Footnotes

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

Copy link
Contributor

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

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for the Granite Speech model. The implementation is largely a port from the Hugging Face Transformers library. My review has identified a couple of logical errors in validation checks within the LoRA configuration and the audio feature merging logic. I have also suggested an enhancement to the test suite to include graph mode testing, which will improve the model's robustness on MindSpore. Apart from these points, the changes look good.

@alien-0119 alien-0119 force-pushed the granite_speech_master branch from 2198a1b to bb39fc0 Compare October 31, 2025 03:18
@alien-0119 alien-0119 added the new model add new model to mindone label Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

new model add new model to mindone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant