model: Add NVIDIA Canary-1B-v2 to Transformers#46825
Conversation
|
Thank you for your contribution π€! CI Security Gate β automatic approval blockedThis PR was not automatically approved for CI because the security gate failed. Possible reasons:
See the workflow run for the exact violations. A maintainer can review and manually approve CI if a finding is a false positive. |
|
Good day @ebezzam! Just bumping this up for whenever you find the time; thanks! |
ebezzam
left a comment
There was a problem hiding this comment.
hi @harshaljanjani thanks for the contribution! Here are some initial comments to start iterating. I didn't have time for an in-depth review, but hopefully these help to get things in the expected direction!
Main idea is to see how existing models define / implement the various parts, and trying to use modular as much as possible!
|
|
||
| def __init__( | ||
| self, | ||
| embed_dim: int, |
There was a problem hiding this comment.
I wonder if super_kwargs could be used here? I learned about this for here
There was a problem hiding this comment.
Thanks for the precedent link; done!
|
|
||
|
|
||
| @auto_docstring | ||
| class CanaryPreTrainedModel(PreTrainedModel): |
There was a problem hiding this comment.
(nit but good practice) could we do modular from an existing model? to save some lines
There was a problem hiding this comment.
Done as well.
| if encoder_outputs is None: | ||
| encoder_outputs = self.encoder( | ||
| input_features=input_features, | ||
| attention_mask=attention_mask, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| encoder_attention_mask = getattr(encoder_outputs, "attention_mask", None) |
There was a problem hiding this comment.
could we define and use get_audio_features like this for calling the encoder
There was a problem hiding this comment.
Thanks for the precedent, done.
| speech-to-text translation. | ||
| """ | ||
| ) | ||
| class CanaryForConditionalGeneration(CanaryPreTrainedModel, GenerationMixin): |
There was a problem hiding this comment.
can we do modular from WhisperForConditionalGeneration?
There was a problem hiding this comment.
IMO since WhisperForConditionalGeneration inherits from the custom WhisperGenerationMixin with tons of custom methods specific to Whisper (long-form chunking, timestamp extraction for Whisper-specific token IDs, etc., also bias = False), we'd be adding breakage here if we inherited from the class. Moonshine sets precedent as well by being standalone with the same WhisperModel derived shape. Happy to know if I'm missing something here though!
| init.copy_(module.positional_embeddings, module._build_table()) | ||
|
|
||
|
|
||
| class CanaryDecoder(CanaryPreTrainedModel): |
There was a problem hiding this comment.
TO CHECK: there isn't a similar module elsewhere in the lib? for example Whisper
There was a problem hiding this comment.
Deduped it with WhisperDecoder, thanks for the nudge!
| class CanaryModel(CanaryPreTrainedModel): | ||
| def __init__(self, config: CanaryConfig): | ||
| super().__init__(config) | ||
| self.encoder = AutoModel.from_config(config.encoder_config) | ||
| self.decoder = CanaryDecoder(config) | ||
| self.post_init() |
There was a problem hiding this comment.
could we do modular from WhisperModel?
| audio = make_list_of_audio(audio) | ||
| inputs = self.feature_extractor(audio, **output_kwargs["audio_kwargs"]) | ||
|
|
||
| prompt_tokens = self._build_prompt_tokens(source_lang, target_lang, pnc, timestamps) |
There was a problem hiding this comment.
can we use a chat template for specifying these prompts? See VibeVoice ASR or Qwen 3 ASR. And so we likely need to define an additional method called apply_transcription_request that will call apply_chat_template which then calls this __call__.
The purpose of this __call__ should be simply:
- apply feature extrator to audio and insert audio tokens
- tokenize text
- (optionally) prepare output labels
you will need to add this chat template to your checkpoint in the conversion script
There was a problem hiding this comment.
Done following the aforementioned lib patterns, happy to adjust if anything comes up!
There was a problem hiding this comment.
can we generate this via modular from an existing config?
|
|
||
| Canary reuses the [Fast Conformer](https://huggingface.co/papers/2305.05084) encoder from [Parakeet](./parakeet.md) (loaded through [`ParakeetEncoder`] / [`ParakeetEncoderConfig`]) and pairs it with a Transformer decoder that uses fixed sinusoidal positional embeddings, cross-attention to the encoder outputs and tied input/output embeddings. The task is selected through a decoder prompt prefix built by [`CanaryProcessor`] of the form `<|startofcontext|> <|startoftranscript|> <source_lang> <target_lang> <pnc|nopnc> <timestamp|notimestamp> ...`, where `source_lang == target_lang` selects transcription and otherwise selects translation. | ||
|
|
||
| The original implementation can be found in [NVIDIA NeMo](https://github.com/NVIDIA/NeMo). A Transformers-compatible checkpoint is available at [harshaljanjani/canary-1b-v2-hf](https://huggingface.co/harshaljanjani/canary-1b-v2-hf). |
There was a problem hiding this comment.
NOTE for later: we'll eventually want to transfer a checkpoint to NVIDIA's org
|
|
||
| The original implementation can be found in [NVIDIA NeMo](https://github.com/NVIDIA/NeMo). A Transformers-compatible checkpoint is available at [harshaljanjani/canary-1b-v2-hf](https://huggingface.co/harshaljanjani/canary-1b-v2-hf). | ||
|
|
||
| ## Usage |
There was a problem hiding this comment.
other example usage, we'd like to cover:
- batch
- training (simply showing forward/backward)
- torch compile
- model features such as timestamp, translation, diariarization
Check out existing models like AudioFlamingo3, VIbevoice ASR, Qwen3 ASR
There was a problem hiding this comment.
Done, fleshed out all the tasks with examples :)
|
[For maintainers] Suggested jobs to run (before merge) run-slow: auto, canary |
CI recapDashboard: View test results in Grafana |

What does this PR do?
β This PR adds Canary 1B v2 to Transformers!
References:
β Model Checkpoints
β GitHub Repository
β Transformers Converted Checkpoints
β Research Paper
β Testing Script
Rebased on top of @eustlb's #40756
cc: @ebezzam @Rocketknight1
Multilingual Transcription and Test Results
transformersversion:5.13.0.dev0Linux-6.8.0-1060-gcp-x86_64-with-glibc2.353.11.15huggingface_hubversion:1.20.1safetensorsversion:0.8.0accelerateversion:1.14.0not installed2.11.0+cu130 (CUDA)NVIDIA L413.0Before submitting