Add Transformers weights, config and usage section

#1
by eustlb HF Staff - opened
Files changed (4) hide show
  1. README.md +120 -0
  2. config.json +54 -0
  3. model.safetensors +3 -0
  4. processor_config.json +31 -0
README.md CHANGED
@@ -185,6 +185,126 @@ python ${NEMO_ROOT}/examples/speaker_tasks/diarization/neural_diarizer/e2e_diari
185
 
186
  More details on the evaluation can be found in the [Diarization Evaluation](diarization_evaluation.md) subcard.
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  ### Integration with Streaming ASR
189
 
190
  Please refer to the [ASR Integration Guide](ASR_INTEGRATION_GUIDE.md) for the detailed instructions on integration of Nemotron Diarization with Streaming ASR.
 
185
 
186
  More details on the evaluation can be found in the [Diarization Evaluation](diarization_evaluation.md) subcard.
187
 
188
+ ### 🤗 Transformers usage
189
+
190
+ This model is supported natively in [🤗 Transformers](https://github.com/huggingface/transformers)!
191
+ Install from source:
192
+
193
+ ```bash
194
+ pip install git+https://github.com/huggingface/transformers
195
+ ```
196
+
197
+ For more details about usage, please refer to the [Transformers documentation](https://huggingface.co/docs/transformers/en/model_doc/nemotron3_diarization).
198
+
199
+ <details>
200
+ <summary>➡️ Offline diarization</summary>
201
+
202
+ ```python
203
+ import torch
204
+ from transformers import AutoModelForAudioFrameClassification, AutoProcessor
205
+ from transformers.audio_utils import load_audio
206
+
207
+ model_id = "nvidia/Nemotron-3-Diarization"
208
+ processor = AutoProcessor.from_pretrained(model_id)
209
+ model = AutoModelForAudioFrameClassification.from_pretrained(model_id, device_map="auto")
210
+
211
+ sampling_rate = processor.feature_extractor.sampling_rate
212
+ audio = load_audio(
213
+ "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/diarization_example.mp3",
214
+ sampling_rate=sampling_rate,
215
+ )
216
+ inputs = processor(audio, sampling_rate=sampling_rate).to(model.device, dtype=model.dtype)
217
+
218
+ with torch.inference_mode():
219
+ logits = model(**inputs).logits # (1, num_frames, 8), one frame every 10 ms
220
+
221
+ segments = processor.extract_speaker_dict(logits, inputs.attention_mask)[0]
222
+ for segment in segments:
223
+ print(f"speaker_{segment['Speaker']}: {segment['Start']:.2f}s - {segment['End']:.2f}s")
224
+ ```
225
+ </details>
226
+
227
+ <details>
228
+ <summary>➡️ Streaming diarization</summary>
229
+
230
+ Audio arrives chunk by chunk, and each forward takes one chunk: the processor cuts it for its `streaming_mode` and
231
+ adds `num_lookahead_frames`, the number of trailing look-ahead frames the model attends to but does not score, since
232
+ they open the next chunk. The forward returns the `speaker_cache` to pass to the next call. The last chunk of a
233
+ session is extracted with `is_last_audio_chunk=True`: it has no look-ahead, so every remaining frame is scored.
234
+
235
+
236
+ | `streaming_mode` | Latency¹ |
237
+ | ------------------------- | -------- |
238
+ | `"low_latency"` (default) | 1.04 s |
239
+ | `"very_low_latency"` | 0.64 s |
240
+ | `"ultra_low_latency"` | 0.32 s |
241
+
242
+
243
+ ¹ Audio to wait for before the model runs on a chunk: the chunk plus its look-ahead, excluding compute time.
244
+
245
+ ```python
246
+ import torch
247
+ from transformers import AutoModelForAudioFrameClassification, AutoProcessor
248
+ from transformers.audio_utils import load_audio
249
+
250
+ model_id = "nvidia/Nemotron-3-Diarization"
251
+ processor = AutoProcessor.from_pretrained(model_id)
252
+ model = AutoModelForAudioFrameClassification.from_pretrained(model_id, device_map="auto")
253
+ processor.set_streaming_mode("low_latency") # the default, can also be "very_low_latency" and "ultra_low_latency"
254
+ print(f"Streaming latency: {processor.streaming_latency_ms} ms")
255
+
256
+ sampling_rate = processor.feature_extractor.sampling_rate
257
+ audio = load_audio(
258
+ "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/diarization_example.mp3",
259
+ sampling_rate=sampling_rate,
260
+ )
261
+
262
+
263
+ def inputs_generator():
264
+ """Yields the processor outputs of each chunk."""
265
+ yield processor(
266
+ audio[: processor.num_samples_first_audio_chunk],
267
+ sampling_rate=sampling_rate,
268
+ is_streaming=True,
269
+ is_first_audio_chunk=True,
270
+ )
271
+
272
+ mel_frame_idx = processor.num_mel_frames_per_step
273
+ start_idx = processor.audio_chunk_start(mel_frame_idx)
274
+ while (end_idx := start_idx + processor.num_samples_per_audio_chunk) <= audio.shape[0]:
275
+ yield processor(
276
+ audio[start_idx:end_idx],
277
+ sampling_rate=sampling_rate,
278
+ is_streaming=True,
279
+ is_first_audio_chunk=False,
280
+ )
281
+ mel_frame_idx += processor.num_mel_frames_per_step
282
+ start_idx = processor.audio_chunk_start(mel_frame_idx)
283
+
284
+ # the audio ended: the frames left in the buffer are the last ones of the session
285
+ yield processor(
286
+ audio[start_idx:],
287
+ sampling_rate=sampling_rate,
288
+ is_streaming=True,
289
+ is_first_audio_chunk=False,
290
+ is_last_audio_chunk=True,
291
+ )
292
+
293
+
294
+ speaker_cache, logits = None, []
295
+ with torch.inference_mode():
296
+ for inputs in inputs_generator():
297
+ inputs = inputs.to(model.device, dtype=model.dtype)
298
+ # `inputs` carries `num_lookahead_frames` for every chunk but the last, `speaker_cache` links the chunks
299
+ outputs = model(**inputs, speaker_cache=speaker_cache)
300
+ logits.append(outputs.logits) # the chunk's frames, without its look-ahead
301
+ speaker_cache = outputs.speaker_cache
302
+
303
+ logits = torch.cat(logits, dim=1) # (1, num_frames, 8), one frame every 10 ms
304
+ segments = processor.extract_speaker_dict(logits)[0] # [{"Start": 0.0, "End": 15.43, "Speaker": 0}, ...]
305
+ ```
306
+ </details>
307
+
308
  ### Integration with Streaming ASR
309
 
310
  Please refer to the [ASR Integration Guide](ASR_INTEGRATION_GUIDE.md) for the detailed instructions on integration of Nemotron Diarization with Streaming ASR.
config.json ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Nemotron3DiarizationForAudioFrameClassification"
4
+ ],
5
+ "audio_config": {
6
+ "attention_dropout": 0.0,
7
+ "hidden_act": "gelu",
8
+ "hidden_size": 512,
9
+ "initializer_range": 0.02,
10
+ "intermediate_size": 2048,
11
+ "max_position_embeddings": 5000,
12
+ "model_type": "nemotron3_diarization_audio",
13
+ "num_attention_heads": 8,
14
+ "num_hidden_layers": 31,
15
+ "num_key_value_heads": 8,
16
+ "num_mel_bins": 128,
17
+ "partial_rotary_factor": 1.0,
18
+ "rope_parameters": {
19
+ "partial_rotary_factor": 1.0,
20
+ "rope_theta": 10000.0,
21
+ "rope_type": "default"
22
+ },
23
+ "subsampling_factor": 8
24
+ },
25
+ "chunk_length": 340,
26
+ "chunk_right_context": 40,
27
+ "dtype": "float32",
28
+ "fifo_length": 40,
29
+ "head_config": {
30
+ "audio_hidden_size": 512,
31
+ "hidden_size": 192,
32
+ "model_type": "",
33
+ "num_speakers": 8,
34
+ "subsampling_factor": 8
35
+ },
36
+ "initializer_range": 0.02,
37
+ "model_type": "nemotron3_diarization",
38
+ "speaker_cache_update_period": 300,
39
+ "streaming_config": {
40
+ "fifo_length": 264,
41
+ "latest_frames_score_boost": 0.05,
42
+ "min_positive_scores_rate": 0.5,
43
+ "model_type": "",
44
+ "num_speakers": 8,
45
+ "prediction_score_threshold": 0.25,
46
+ "speaker_cache_length": 264,
47
+ "speaker_cache_silence_frames_per_speaker": 1,
48
+ "speaker_cache_update_period": 222,
49
+ "strong_boost_rate": 0.75,
50
+ "subsampling_factor": 8,
51
+ "weak_boost_rate": 1.5
52
+ },
53
+ "transformers_version": "5.18.0.dev0"
54
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c074d86335b3b794f8fa5edc25594558f128bdb3914d27806a3a5a2e44963cb6
3
+ size 396954592
processor_config.json ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "feature_extractor": {
3
+ "feature_extractor_type": "NemotronAsrStreamingFeatureExtractor",
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": "Nemotron3DiarizationProcessor",
15
+ "streaming_mode": "low_latency",
16
+ "streaming_modes": {
17
+ "low_latency": [
18
+ 9,
19
+ 4
20
+ ],
21
+ "ultra_low_latency": [
22
+ 3,
23
+ 1
24
+ ],
25
+ "very_low_latency": [
26
+ 6,
27
+ 2
28
+ ]
29
+ },
30
+ "subsampling_factor": 8
31
+ }