⚠️ DO NOT MERGE: feat(transformers): Add native Transformers weights and usage

#27
README.md CHANGED
@@ -47,6 +47,7 @@ tags:
47
  - pytorch
48
  - NeMo
49
  - hf-asr-leaderboard
 
50
  model-index:
51
  - name: canary-1b-v2
52
  results:
@@ -1896,6 +1897,10 @@ pip install -U nemo_toolkit['asr']
1896
  ```
1897
  The model is available for use in the NeMo toolkit [6], and can be used as a pre-trained checkpoint for inference or for fine-tuning on another dataset.
1898
 
 
 
 
 
1899
  #### Automatically instantiate the model
1900
 
1901
  ```python
@@ -1955,6 +1960,110 @@ For translation task, please, refer to segment-level timestamps for getting intu
1955
  > **Note:** If timestamps are not required for your work, you can reduce memory usage by restoring only the `.nemo` file without the auxiliary CTC model. To do this, extract the `.nemo` file, remove any *timestamps_asr_model* files, then repackage it into a new `.nemo` file.
1956
 
1957
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1958
  ## <span style="color:#b37800;">Software Integration</span>
1959
 
1960
  **Runtime Engine(s):**
 
47
  - pytorch
48
  - NeMo
49
  - hf-asr-leaderboard
50
+ - Transformers
51
  model-index:
52
  - name: canary-1b-v2
53
  results:
 
1897
  ```
1898
  The model is available for use in the NeMo toolkit [6], and can be used as a pre-trained checkpoint for inference or for fine-tuning on another dataset.
1899
 
1900
+ You can also run Canary with [Transformers](https://github.com/huggingface/transformers) 🤗 (more below).
1901
+
1902
+ ### 1) NeMo usage
1903
+
1904
  #### Automatically instantiate the model
1905
 
1906
  ```python
 
1960
  > **Note:** If timestamps are not required for your work, you can reduce memory usage by restoring only the `.nemo` file without the auxiliary CTC model. To do this, extract the `.nemo` file, remove any *timestamps_asr_model* files, then repackage it into a new `.nemo` file.
1961
 
1962
 
1963
+ ### 2) [Transformers](https://github.com/huggingface/transformers) 🤗 usage
1964
+
1965
+
1966
+ Until Canary is part of an official Transformers release, you can use it by installing from source.
1967
+
1968
+ ```bash
1969
+ pip install git+https://github.com/huggingface/transformers
1970
+ ```
1971
+
1972
+ <details>
1973
+ <summary>➡️ Pipeline usage</summary>
1974
+
1975
+ ```python
1976
+ from transformers import pipeline
1977
+
1978
+ pipe = pipeline("automatic-speech-recognition", model="nvidia/canary-1b-v2")
1979
+ out = pipe("https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/bcn_weather.mp3")
1980
+ print(out)
1981
+ ```
1982
+ </details>
1983
+
1984
+ <details>
1985
+ <summary>➡️ Transcription</summary>
1986
+
1987
+ ```python
1988
+ from datasets import load_dataset, Audio
1989
+ from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq
1990
+
1991
+ model_id = "nvidia/canary-1b-v2"
1992
+ processor = AutoProcessor.from_pretrained(model_id)
1993
+ model = AutoModelForSpeechSeq2Seq.from_pretrained(model_id, device_map="auto")
1994
+
1995
+ ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
1996
+ ds = ds.cast_column("audio", Audio(sampling_rate=processor.feature_extractor.sampling_rate))
1997
+
1998
+ inputs = processor.apply_transcription_request(audio=ds[0]["audio"]["array"], source_language="en").to(model.device)
1999
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
2000
+ print(processor.decode(generated_ids, skip_special_tokens=True)[0])
2001
+ ```
2002
+ </details>
2003
+
2004
+ <details>
2005
+ <summary>➡️ Translation</summary>
2006
+
2007
+ ```python
2008
+ inputs = processor.apply_transcription_request(
2009
+ audio=ds[0]["audio"]["array"], source_language="en", target_language="de"
2010
+ ).to(model.device)
2011
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
2012
+ print(processor.decode(generated_ids, skip_special_tokens=True)[0])
2013
+ ```
2014
+ </details>
2015
+
2016
+ <details>
2017
+ <summary>➡️ Batch inference</summary>
2018
+
2019
+ ```python
2020
+ audios = [ds[0]["audio"]["array"], ds[1]["audio"]["array"]]
2021
+ inputs = processor.apply_transcription_request(
2022
+ audio=audios, source_language="en", target_language=["en", "de"]
2023
+ ).to(model.device)
2024
+ generated_ids = model.generate(**inputs, max_new_tokens=128)
2025
+ for text in processor.decode(generated_ids, skip_special_tokens=True):
2026
+ print(text)
2027
+ ```
2028
+ </details>
2029
+
2030
+ <details>
2031
+ <summary>➡️ Training</summary>
2032
+
2033
+ Put the target transcript in the assistant turn and pass `output_labels=True`. Padding positions are masked automatically.
2034
+
2035
+ ```python
2036
+ model.train()
2037
+ transcription = "mister Quilter is the apostle of the middle classes, and we are glad to welcome his gospel."
2038
+
2039
+ conversation = [
2040
+ [
2041
+ {
2042
+ "role": "user",
2043
+ "content": [
2044
+ {"type": "audio", "audio": ds[0]["audio"]["array"]},
2045
+ {"type": "text", "source_language": "en", "target_language": "en", "punctuation": True},
2046
+ ],
2047
+ },
2048
+ {"role": "assistant", "content": transcription},
2049
+ ]
2050
+ ]
2051
+
2052
+ inputs = processor.apply_chat_template(
2053
+ conversation,
2054
+ tokenize=True,
2055
+ return_dict=True,
2056
+ processor_kwargs={"output_labels": True},
2057
+ ).to(model.device)
2058
+
2059
+ outputs = model(**inputs)
2060
+ outputs.loss.backward()
2061
+ ```
2062
+ </details>
2063
+
2064
+ For more details about usage, please refer to the [Transformers' documentation](https://huggingface.co/docs/transformers/en/model_doc/canary).
2065
+
2066
+
2067
  ## <span style="color:#b37800;">Software Integration</span>
2068
 
2069
  **Runtime Engine(s):**
chat_template.jinja ADDED
@@ -0,0 +1 @@
 
 
1
+ {{- '<|startofcontext|><|startoftranscript|><|emo:undefined|>' -}}{%- for message in messages -%}{%- if message['role'] == 'user' -%}{%- for content in message['content'] if content['type'] == 'text' -%}{{- '<|' ~ content['source_language'] ~ '|>' -}}{{- '<|' ~ content['target_language'] ~ '|>' -}}{{- '<|pnc|>' if content['punctuation'] else '<|nopnc|>' -}}{{- '<|noitn|><|notimestamp|><|nodiarize|>' -}}{%- endfor -%}{%- elif message['role'] == 'assistant' -%}{{- message['content'] ~ '<|endoftext|>' -}}{%- endif -%}{%- endfor -%}
config.json ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "CanaryForConditionalGeneration"
4
+ ],
5
+ "bos_token_id": 4,
6
+ "decoder_config": {
7
+ "attention_bias": true,
8
+ "attention_dropout": 0.1,
9
+ "bos_token_id": 4,
10
+ "eos_token_id": 3,
11
+ "head_dim": 128,
12
+ "hidden_act": "relu",
13
+ "hidden_size": 1024,
14
+ "initializer_range": 0.02,
15
+ "intermediate_size": 4096,
16
+ "max_position_embeddings": 1024,
17
+ "model_type": "canary_decoder",
18
+ "num_attention_heads": 8,
19
+ "num_hidden_layers": 8,
20
+ "num_key_value_heads": 8,
21
+ "pad_token_id": 2,
22
+ "use_cache": true,
23
+ "vocab_size": 16384
24
+ },
25
+ "decoder_start_token_id": 7,
26
+ "dtype": "float32",
27
+ "encoder_config": {
28
+ "activation_dropout": 0.1,
29
+ "attention_bias": true,
30
+ "attention_dropout": 0.1,
31
+ "conv_kernel_size": 9,
32
+ "convolution_bias": true,
33
+ "dropout": 0.1,
34
+ "dropout_positions": 0.0,
35
+ "hidden_act": "silu",
36
+ "hidden_size": 1024,
37
+ "initializer_range": 0.02,
38
+ "intermediate_size": 4096,
39
+ "layerdrop": 0.1,
40
+ "max_position_embeddings": 5000,
41
+ "model_type": "parakeet_encoder",
42
+ "num_attention_heads": 8,
43
+ "num_hidden_layers": 32,
44
+ "num_key_value_heads": 8,
45
+ "num_mel_bins": 128,
46
+ "scale_input": false,
47
+ "subsampling_conv_channels": 256,
48
+ "subsampling_conv_kernel_size": 3,
49
+ "subsampling_conv_stride": 2,
50
+ "subsampling_factor": 8
51
+ },
52
+ "eos_token_id": 3,
53
+ "initializer_range": 0.02,
54
+ "is_encoder_decoder": true,
55
+ "model_type": "canary",
56
+ "pad_token_id": 2,
57
+ "tie_word_embeddings": true,
58
+ "transformers_version": "5.15.0.dev0",
59
+ "use_cache": true,
60
+ "vocab_size": 16384
61
+ }
generation_config.json ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 4,
4
+ "decoder_start_token_id": 7,
5
+ "eos_token_id": 3,
6
+ "output_attentions": false,
7
+ "output_hidden_states": false,
8
+ "pad_token_id": 2,
9
+ "transformers_version": "5.15.0.dev0",
10
+ "use_cache": true
11
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4f6d381f6a939b95e9f8516212148b701e9b3805d26462c4c3e9e34f3d89b6f8
3
+ size 3916173816
processor_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "feature_extractor": {
3
+ "feature_extractor_type": "ParakeetFeatureExtractor",
4
+ "feature_size": 128,
5
+ "hop_length": 160,
6
+ "n_fft": 512,
7
+ "padding_side": "right",
8
+ "padding_value": 0.0,
9
+ "preemphasis": 0.97,
10
+ "return_attention_mask": true,
11
+ "sampling_rate": 16000,
12
+ "win_length": 400
13
+ },
14
+ "processor_class": "CanaryProcessor"
15
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "backend": "tokenizers",
3
+ "bos_token": "<|startoftranscript|>",
4
+ "clean_up_tokenization_spaces": false,
5
+ "eos_token": "<|endoftext|>",
6
+ "model_max_length": 1000000000000000019884624838656,
7
+ "pad_token": "<pad>",
8
+ "processor_class": "CanaryProcessor",
9
+ "tokenizer_class": "TokenizersBackend",
10
+ "unk_token": "<unk>"
11
+ }