Thanks to visit codestin.com
Credit goes to github.com

Skip to content

model: Add NVIDIA Canary-1B-v2 to Transformers#46825

Open
harshaljanjani wants to merge 8 commits into
huggingface:mainfrom
harshaljanjani:add-canary
Open

model: Add NVIDIA Canary-1B-v2 to Transformers#46825
harshaljanjani wants to merge 8 commits into
huggingface:mainfrom
harshaljanjani:add-canary

Conversation

@harshaljanjani

@harshaljanjani harshaljanjani commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

CI

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

  • transformers version: 5.13.0.dev0
  • Platform: Linux-6.8.0-1060-gcp-x86_64-with-glibc2.35
  • Python version: 3.11.15
  • huggingface_hub version: 1.20.1
  • safetensors version: 0.8.0
  • accelerate version: 1.14.0
  • DeepSpeed version: not installed
  • PyTorch version (accelerator?): 2.11.0+cu130 (CUDA)
  • GPU type: NVIDIA L4
  • CUDA version: 13.0
2 1

Before submitting

  • This PR adds a new model to Transformers.
  • Did you read the contributor guidelines, specifically the Pull Request section?
  • Did you make sure to update the documentation with your changes? Here are the documentation guidelines, and here are tips on formatting docstrings.
  • Did you add any necessary tests?

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution πŸ€—!

CI Security Gate β€” automatic approval blocked

This PR was not automatically approved for CI because the security gate failed.

Possible reasons:

  • The PR touches 50 or more files β€” only PRs with fewer than 50 changed files are automatically approved
  • A changed file is outside the allowed directories (src/, tests/, docs/, utils/), has a disallowed extension (only .py, .txt, .md permitted outside tests/ and docs/), or is not .md/.yml inside docs/
  • A new high-severity security issue was detected in the changed Python files (Bandit check)

See the workflow run for the exact violations.

A maintainer can review and manually approve CI if a finding is a false positive.

@harshaljanjani harshaljanjani marked this pull request as ready for review June 23, 2026 03:48
@harshaljanjani harshaljanjani marked this pull request as draft June 23, 2026 04:30
@harshaljanjani harshaljanjani marked this pull request as ready for review June 23, 2026 04:50
@harshaljanjani

Copy link
Copy Markdown
Contributor Author

Good day @eustlb @ebezzam, just a gentle ping in regard to model reviews!

@harshaljanjani

Copy link
Copy Markdown
Contributor Author

Good day @ebezzam! Just bumping this up for whenever you find the time; thanks!

@ebezzam ebezzam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if super_kwargs could be used here? I learned about this for here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the precedent link; done!



@auto_docstring
class CanaryPreTrainedModel(PreTrainedModel):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(nit but good practice) could we do modular from an existing model? to save some lines

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done as well.

Comment on lines +294 to +301
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could we define and use get_audio_features like this for calling the encoder

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the precedent, done.

speech-to-text translation.
"""
)
class CanaryForConditionalGeneration(CanaryPreTrainedModel, GenerationMixin):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we do modular from WhisperForConditionalGeneration?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

TO CHECK: there isn't a similar module elsewhere in the lib? for example Whisper

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deduped it with WhisperDecoder, thanks for the nudge!

Comment on lines +247 to +252
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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could we do modular from WhisperModel?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, done!

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done following the aforementioned lib patterns, happy to adjust if anything comes up!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can we generate this via modular from an existing config?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done :)

Comment thread docs/source/en/model_doc/canary.md Outdated

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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, fleshed out all the tasks with examples :)

@harshaljanjani

harshaljanjani commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for your time @ebezzam; left replies, addressed the review comments and verified that there are no regressions across the test suite or in generation. I suspect the failures are alluding to cache warmup and will be fixed with this review when we get to it?

image

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: auto, canary

@harshaljanjani harshaljanjani requested a review from ebezzam July 9, 2026 10:07
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29009401108:2
Result: failure | Jobs: 15 | Tests: 171,039 | Failures: 8 | Duration: 19h 53m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants