Views
No views yet

1pip install --upgrade pip
2pip install --upgrade transformers accelerateNote: MF processes audio in 30-second windows with a 10-minute total cap per sample. Longer inputs are truncated.
1from transformers import AudioFlamingo3ForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = AudioFlamingo3ForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversation = [
8 {
9 "role": "user",
10 "content": [
11 {"type": "text", "text": "Describe this track in full detail - tell me the genre, tempo, and key, then dive into the instruments, production style, and overall mood it creates."},
12 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/MF-Skills/resolve/main/assets/song_1.mp3"},
13 ],
14 }
15]
16
17inputs = processor.apply_chat_template(
18 conversation,
19 tokenize=True,
20 add_generation_prompt=True,
21 return_dict=True,
22).to(model.device)
23
24outputs = model.generate(**inputs, max_new_tokens=1024)
25
26decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
27print(decoded_outputs)1from transformers import AudioFlamingo3ForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = AudioFlamingo3ForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6
7conversations = [
8 [
9 {
10 "role": "user",
11 "content": [
12 {
13 "type": "text",
14 "text": "Describe this track in full detail - tell me the genre, tempo, and key, then dive into the instruments, production style, and overall mood it creates."},
15 {
16 "type": "audio",
17 "path": "https://huggingface.co/datasets/nvidia/MF-Skills/resolve/main/assets/song_1.mp3",
18 },
19 ],
20 }
21 ],
22 [
23 {
24 "role": "user",
25 "content": [
26 {
27 "type": "text",
28 "text": "Write a rich caption that blends the technical details (genre, BPM, key, chords, mix) with how the song feels emotionally and dynamically as it unfolds.",
29 },
30 {
31 "type": "audio",
32 "path": "https://huggingface.co/datasets/nvidia/MF-Skills/resolve/main/assets/song_2.mp3"
33 },
34 ],
35 }
36 ],
37]
38
39inputs = processor.apply_chat_template(
40 conversations,
41 tokenize=True,
42 add_generation_prompt=True,
43 return_dict=True,
44).to(model.device)
45
46outputs = model.generate(**inputs, max_new_tokens=1024)
47
48decoded_outputs = processor.batch_decode(outputs[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)
49print(decoded_outputs)1# text-only
2conv = [{"role": "user", "content": [{"type": "text", "text": "What is the capital of France?"}]}]
3batch = processor.apply_chat_template(conv, tokenize=True, add_generation_prompt=True, return_dict=True).to(device)
4print(processor.batch_decode(model.generate(**batch)[:, batch["input_ids"].shape[1]:], skip_special_tokens=True)[0])
5
6# audio-only
7conv = [{"role": "user", "content": [{"type": "audio", "path": "https://.../sample.wav"}]}]
8batch = processor.apply_chat_template(conv, tokenize=True, add_generation_prompt=True, return_dict=True).to(device)
9print(processor.batch_decode(model.generate(**batch)[:, batch["input_ids"].shape[1]:], skip_special_tokens=True)[0])1from transformers import AudioFlamingo3ForConditionalGeneration, AutoProcessor
2
3model_id = "nvidia/music-flamingo-hf"
4processor = AutoProcessor.from_pretrained(model_id)
5model = AudioFlamingo3ForConditionalGeneration.from_pretrained(model_id, device_map="auto")
6model.train()
7
8conversation = [
9 [
10 {
11 "role": "user",
12 "content": [
13 {"type": "text", "text": "What's the key of this song?"},
14 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/MF-Skills/resolve/main/assets/song_1.mp3"},
15 ],
16 },
17 {
18 "role": "assistant",
19 "content": [{"type": "text", "text": "D major"}],
20 }
21 ],
22 [
23 {
24 "role": "user",
25 "content": [
26 {
27 "type": "text",
28 "text": "What's the bpm of this song?",
29 },
30 {"type": "audio", "path": "https://huggingface.co/datasets/nvidia/MF-Skills/resolve/main/assets/song_2.mp3"},
31 ],
32 },
33 {
34 "role": "assistant",
35 "content": [{"type": "text", "text": "87"}],
36 }
37
38 ]
39]
40
41inputs = processor.apply_chat_template(
42 conversation,
43 tokenize=True,
44 add_generation_prompt=True,
45 return_dict=True,
46 output_labels=True,
47).to(model.device)
48
49loss = model(**inputs).loss
50loss.backward()1generate_kwargs = {
2 "max_new_tokens": 256,
3 "do_sample": True,
4 "temperature": 0.7,
5 "top_p": 0.9,
6}
7out = model.generate(**batch, **generate_kwargs)torch.compile, install Flash-Attention and enable it at load time:pip install flash-attn --no-build-isolation1model = AudioFlamingo3ForConditionalGeneration.from_pretrained(
2 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="flash_attention_2"
3).to(device)torch.compile for significant speed-ups:1import torch
2torch.set_float32_matmul_precision("high")
3
4model.generation_config.cache_implementation = "static"
5model.generation_config.max_new_tokens = 256
6model.forward = torch.compile(model.forward, mode="reduce-overhead", fullgraph=True)torch.compileis not compatible with Flash Attention 2 at the same time.
1model = AudioFlamingo3ForConditionalGeneration.from_pretrained(
2 model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, attn_implementation="sdpa"
3).to(device)