From 677778c5721fca0c0cf18911527afd3ad0dd1086 Mon Sep 17 00:00:00 2001 From: "gerardo.dominguez.aldama" Date: Wed, 13 Aug 2025 12:40:00 -0700 Subject: [PATCH 1/8] Updated device-agnostic code on tutorials --- examples/tutorials/forced_alignment_tutorial.py | 8 +++++++- .../tutorials/speech_recognition_pipeline_tutorial.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/tutorials/forced_alignment_tutorial.py b/examples/tutorials/forced_alignment_tutorial.py index 7fa7c86dc3..15c7311c20 100644 --- a/examples/tutorials/forced_alignment_tutorial.py +++ b/examples/tutorials/forced_alignment_tutorial.py @@ -47,7 +47,13 @@ print(torchaudio.__version__) -device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +if torch.accelerator.is_available(): + acc = torch.accelerator.current_accelerator() + device: torch.device = torch.device(acc) + backend = torch.distributed.get_default_backend_for_device(device) +else: + device: torch.device = torch.device("cpu") + backend = "gloo" print(device) diff --git a/examples/tutorials/speech_recognition_pipeline_tutorial.py b/examples/tutorials/speech_recognition_pipeline_tutorial.py index 2c8dfc752b..64880ae8a3 100644 --- a/examples/tutorials/speech_recognition_pipeline_tutorial.py +++ b/examples/tutorials/speech_recognition_pipeline_tutorial.py @@ -42,7 +42,13 @@ print(torchaudio.__version__) torch.random.manual_seed(0) -device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +if torch.accelerator.is_available(): + acc = torch.accelerator.current_accelerator() + device: torch.device = torch.device(acc) + backend = torch.distributed.get_default_backend_for_device(device) +else: + device: torch.device = torch.device("cpu") + backend = "gloo" print(device) From 1a0b0d629644684380edb2045007676bcc4c14ef Mon Sep 17 00:00:00 2001 From: "gerardo.dominguez.aldama" Date: Wed, 13 Aug 2025 12:41:03 -0700 Subject: [PATCH 2/8] Rolled back changes --- examples/tutorials/forced_alignment_tutorial.py | 8 +------- .../tutorials/speech_recognition_pipeline_tutorial.py | 8 +------- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/examples/tutorials/forced_alignment_tutorial.py b/examples/tutorials/forced_alignment_tutorial.py index 15c7311c20..7fa7c86dc3 100644 --- a/examples/tutorials/forced_alignment_tutorial.py +++ b/examples/tutorials/forced_alignment_tutorial.py @@ -47,13 +47,7 @@ print(torchaudio.__version__) -if torch.accelerator.is_available(): - acc = torch.accelerator.current_accelerator() - device: torch.device = torch.device(acc) - backend = torch.distributed.get_default_backend_for_device(device) -else: - device: torch.device = torch.device("cpu") - backend = "gloo" +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(device) diff --git a/examples/tutorials/speech_recognition_pipeline_tutorial.py b/examples/tutorials/speech_recognition_pipeline_tutorial.py index 64880ae8a3..2c8dfc752b 100644 --- a/examples/tutorials/speech_recognition_pipeline_tutorial.py +++ b/examples/tutorials/speech_recognition_pipeline_tutorial.py @@ -42,13 +42,7 @@ print(torchaudio.__version__) torch.random.manual_seed(0) -if torch.accelerator.is_available(): - acc = torch.accelerator.current_accelerator() - device: torch.device = torch.device(acc) - backend = torch.distributed.get_default_backend_for_device(device) -else: - device: torch.device = torch.device("cpu") - backend = "gloo" +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(device) From ea34618eacf511b8378f0b0f0ab33ccbefc42500 Mon Sep 17 00:00:00 2001 From: "gerardo.dominguez.aldama" Date: Wed, 13 Aug 2025 12:43:26 -0700 Subject: [PATCH 3/8] Updated device-agnostic code on tutorials --- examples/tutorials/forced_alignment_tutorial.py | 8 +++++++- .../tutorials/speech_recognition_pipeline_tutorial.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/tutorials/forced_alignment_tutorial.py b/examples/tutorials/forced_alignment_tutorial.py index 7fa7c86dc3..15c7311c20 100644 --- a/examples/tutorials/forced_alignment_tutorial.py +++ b/examples/tutorials/forced_alignment_tutorial.py @@ -47,7 +47,13 @@ print(torchaudio.__version__) -device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +if torch.accelerator.is_available(): + acc = torch.accelerator.current_accelerator() + device: torch.device = torch.device(acc) + backend = torch.distributed.get_default_backend_for_device(device) +else: + device: torch.device = torch.device("cpu") + backend = "gloo" print(device) diff --git a/examples/tutorials/speech_recognition_pipeline_tutorial.py b/examples/tutorials/speech_recognition_pipeline_tutorial.py index 2c8dfc752b..64880ae8a3 100644 --- a/examples/tutorials/speech_recognition_pipeline_tutorial.py +++ b/examples/tutorials/speech_recognition_pipeline_tutorial.py @@ -42,7 +42,13 @@ print(torchaudio.__version__) torch.random.manual_seed(0) -device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +if torch.accelerator.is_available(): + acc = torch.accelerator.current_accelerator() + device: torch.device = torch.device(acc) + backend = torch.distributed.get_default_backend_for_device(device) +else: + device: torch.device = torch.device("cpu") + backend = "gloo" print(device) From 11ac5482904baccc5ff8ff402736e0fe2b07b529 Mon Sep 17 00:00:00 2001 From: "gerardo.dominguez.aldama" Date: Thu, 14 Aug 2025 09:01:51 -0700 Subject: [PATCH 4/8] Removing device: torch.device --- examples/tutorials/forced_alignment_tutorial.py | 4 ++-- examples/tutorials/speech_recognition_pipeline_tutorial.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/tutorials/forced_alignment_tutorial.py b/examples/tutorials/forced_alignment_tutorial.py index 15c7311c20..969af1fde5 100644 --- a/examples/tutorials/forced_alignment_tutorial.py +++ b/examples/tutorials/forced_alignment_tutorial.py @@ -49,10 +49,10 @@ if torch.accelerator.is_available(): acc = torch.accelerator.current_accelerator() - device: torch.device = torch.device(acc) + device = torch.device(acc) backend = torch.distributed.get_default_backend_for_device(device) else: - device: torch.device = torch.device("cpu") + device = torch.device("cpu") backend = "gloo" print(device) diff --git a/examples/tutorials/speech_recognition_pipeline_tutorial.py b/examples/tutorials/speech_recognition_pipeline_tutorial.py index 64880ae8a3..f5654ba7d4 100644 --- a/examples/tutorials/speech_recognition_pipeline_tutorial.py +++ b/examples/tutorials/speech_recognition_pipeline_tutorial.py @@ -44,10 +44,10 @@ torch.random.manual_seed(0) if torch.accelerator.is_available(): acc = torch.accelerator.current_accelerator() - device: torch.device = torch.device(acc) + device = torch.device(acc) backend = torch.distributed.get_default_backend_for_device(device) else: - device: torch.device = torch.device("cpu") + device = torch.device("cpu") backend = "gloo" print(device) From 1d23777cf93293d2efa29407111a8b8bb72b3796 Mon Sep 17 00:00:00 2001 From: gera-aldama <111396864+gera-aldama@users.noreply.github.com> Date: Mon, 1 Sep 2025 16:13:55 -0600 Subject: [PATCH 5/8] Refactored Audio Resampling Tutorial to work with accelerator API (#2) * Refactored Audio Resampling Tutorial to work with accelerator API --- .../tutorials/audio_resampling_tutorial.py | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/examples/tutorials/audio_resampling_tutorial.py b/examples/tutorials/audio_resampling_tutorial.py index adca5073c9..4071599d7f 100644 --- a/examples/tutorials/audio_resampling_tutorial.py +++ b/examples/tutorials/audio_resampling_tutorial.py @@ -16,6 +16,13 @@ print(torch.__version__) print(torchaudio.__version__) +if torch.accelerator.is_available(): + acc = torch.accelerator.current_accelerator() + device = torch.device(acc) +else: + device = torch.device("cpu") +print(f"Using device: {device}") + ###################################################################### # Preparation # ----------- @@ -41,7 +48,7 @@ def _get_log_freq(sample_rate, max_sweep_rate, offset): """ start, stop = math.log(offset), math.log(offset + max_sweep_rate // 2) - return torch.exp(torch.linspace(start, stop, sample_rate, dtype=torch.double)) - offset + return torch.exp(torch.linspace(start, stop, sample_rate, dtype=torch.double, device=device)) - offset def _get_inverse_log_freq(freq, sample_rate, offset): @@ -91,6 +98,7 @@ def plot_sweep( freq_y = [f for f in freq if f in y_ticks and 1000 <= f <= sample_rate // 2] figure, axis = plt.subplots(1, 1) + waveform = waveform.cpu() if torch.is_tensor(waveform) else waveform _, _, _, cax = axis.specgram(waveform[0].numpy(), Fs=sample_rate) plt.xticks(time, freq_x) plt.yticks(freq_y, freq_y) @@ -144,7 +152,7 @@ def plot_sweep( waveform = get_sine_sweep(sample_rate) plot_sweep(waveform, sample_rate, title="Original Waveform") -Audio(waveform.numpy()[0], rate=sample_rate) +Audio(waveform.cpu().numpy()[0], rate=sample_rate) ###################################################################### # @@ -157,11 +165,11 @@ def plot_sweep( # an explanation of how it happens, and why it looks like a reflection. resample_rate = 32000 -resampler = T.Resample(sample_rate, resample_rate, dtype=waveform.dtype) +resampler = T.Resample(sample_rate, resample_rate, dtype=waveform.dtype).to(device) resampled_waveform = resampler(waveform) plot_sweep(resampled_waveform, resample_rate, title="Resampled Waveform") -Audio(resampled_waveform.numpy()[0], rate=resample_rate) +Audio(resampled_waveform.cpu().numpy()[0], rate=resample_rate) ###################################################################### # Controling resampling quality with parameters @@ -182,13 +190,13 @@ def plot_sweep( sample_rate = 48000 resample_rate = 32000 -resampled_waveform = F.resample(waveform, sample_rate, resample_rate, lowpass_filter_width=6) +resampled_waveform = F.resample(waveform, sample_rate, resample_rate, lowpass_filter_width=6).to(device) plot_sweep(resampled_waveform, resample_rate, title="lowpass_filter_width=6") ###################################################################### # -resampled_waveform = F.resample(waveform, sample_rate, resample_rate, lowpass_filter_width=128) +resampled_waveform = F.resample(waveform, sample_rate, resample_rate, lowpass_filter_width=128).to(device) plot_sweep(resampled_waveform, resample_rate, title="lowpass_filter_width=128") ###################################################################### @@ -208,13 +216,13 @@ def plot_sweep( sample_rate = 48000 resample_rate = 32000 -resampled_waveform = F.resample(waveform, sample_rate, resample_rate, rolloff=0.99) +resampled_waveform = F.resample(waveform, sample_rate, resample_rate, rolloff=0.99).to(device) plot_sweep(resampled_waveform, resample_rate, title="rolloff=0.99") ###################################################################### # -resampled_waveform = F.resample(waveform, sample_rate, resample_rate, rolloff=0.8) +resampled_waveform = F.resample(waveform, sample_rate, resample_rate, rolloff=0.8).to(device) plot_sweep(resampled_waveform, resample_rate, title="rolloff=0.8") @@ -234,13 +242,13 @@ def plot_sweep( sample_rate = 48000 resample_rate = 32000 -resampled_waveform = F.resample(waveform, sample_rate, resample_rate, resampling_method="sinc_interp_hann") +resampled_waveform = F.resample(waveform, sample_rate, resample_rate, resampling_method="sinc_interp_hann").to(device) plot_sweep(resampled_waveform, resample_rate, title="Hann Window Default") ###################################################################### # -resampled_waveform = F.resample(waveform, sample_rate, resample_rate, resampling_method="sinc_interp_kaiser") +resampled_waveform = F.resample(waveform, sample_rate, resample_rate, resampling_method="sinc_interp_kaiser").to(device) plot_sweep(resampled_waveform, resample_rate, title="Kaiser Window Default") @@ -267,7 +275,7 @@ def plot_sweep( rolloff=0.9475937167399596, resampling_method="sinc_interp_kaiser", beta=14.769656459379492, -) +).to(device) plot_sweep(resampled_waveform, resample_rate, title="Kaiser Window Best (torchaudio)") ###################################################################### @@ -282,7 +290,7 @@ def plot_sweep( rolloff=0.85, resampling_method="sinc_interp_kaiser", beta=8.555504641634386, -) +).to(device) plot_sweep(resampled_waveform, resample_rate, title="Kaiser Window Fast (torchaudio)") ###################################################################### From 0a86fa4c03827f0e4d73100d8a83bd91dc6871e7 Mon Sep 17 00:00:00 2001 From: "gerardo.dominguez.aldama" Date: Mon, 1 Sep 2025 15:18:31 -0700 Subject: [PATCH 6/8] Rolled back changes --- examples/tutorials/forced_alignment_tutorial.py | 7 ------- .../tutorials/speech_recognition_pipeline_tutorial.py | 11 ----------- 2 files changed, 18 deletions(-) diff --git a/examples/tutorials/forced_alignment_tutorial.py b/examples/tutorials/forced_alignment_tutorial.py index 969af1fde5..962a84f961 100644 --- a/examples/tutorials/forced_alignment_tutorial.py +++ b/examples/tutorials/forced_alignment_tutorial.py @@ -47,13 +47,6 @@ print(torchaudio.__version__) -if torch.accelerator.is_available(): - acc = torch.accelerator.current_accelerator() - device = torch.device(acc) - backend = torch.distributed.get_default_backend_for_device(device) -else: - device = torch.device("cpu") - backend = "gloo" print(device) diff --git a/examples/tutorials/speech_recognition_pipeline_tutorial.py b/examples/tutorials/speech_recognition_pipeline_tutorial.py index f5654ba7d4..fb9edeacba 100644 --- a/examples/tutorials/speech_recognition_pipeline_tutorial.py +++ b/examples/tutorials/speech_recognition_pipeline_tutorial.py @@ -38,17 +38,6 @@ import torch import torchaudio -print(torch.__version__) -print(torchaudio.__version__) - -torch.random.manual_seed(0) -if torch.accelerator.is_available(): - acc = torch.accelerator.current_accelerator() - device = torch.device(acc) - backend = torch.distributed.get_default_backend_for_device(device) -else: - device = torch.device("cpu") - backend = "gloo" print(device) From f935fe50a60dcd04092012d3480820aae5b44f62 Mon Sep 17 00:00:00 2001 From: "gerardo.dominguez.aldama" Date: Mon, 1 Sep 2025 15:21:39 -0700 Subject: [PATCH 7/8] Rolled back changes --- examples/tutorials/forced_alignment_tutorial.py | 1 + examples/tutorials/speech_recognition_pipeline_tutorial.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/examples/tutorials/forced_alignment_tutorial.py b/examples/tutorials/forced_alignment_tutorial.py index 962a84f961..7fa7c86dc3 100644 --- a/examples/tutorials/forced_alignment_tutorial.py +++ b/examples/tutorials/forced_alignment_tutorial.py @@ -47,6 +47,7 @@ print(torchaudio.__version__) +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(device) diff --git a/examples/tutorials/speech_recognition_pipeline_tutorial.py b/examples/tutorials/speech_recognition_pipeline_tutorial.py index fb9edeacba..2c8dfc752b 100644 --- a/examples/tutorials/speech_recognition_pipeline_tutorial.py +++ b/examples/tutorials/speech_recognition_pipeline_tutorial.py @@ -38,6 +38,11 @@ import torch import torchaudio +print(torch.__version__) +print(torchaudio.__version__) + +torch.random.manual_seed(0) +device = torch.device("cuda" if torch.cuda.is_available() else "cpu") print(device) From 04eeaaa2faf4cb771e6eacb22df985ad6935d207 Mon Sep 17 00:00:00 2001 From: "gerardo.dominguez.aldama" Date: Mon, 1 Sep 2025 15:24:09 -0700 Subject: [PATCH 8/8] Rolled back changes --- examples/tutorials/forced_alignment_tutorial.py | 8 +++++++- .../tutorials/speech_recognition_pipeline_tutorial.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/examples/tutorials/forced_alignment_tutorial.py b/examples/tutorials/forced_alignment_tutorial.py index 7fa7c86dc3..969af1fde5 100644 --- a/examples/tutorials/forced_alignment_tutorial.py +++ b/examples/tutorials/forced_alignment_tutorial.py @@ -47,7 +47,13 @@ print(torchaudio.__version__) -device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +if torch.accelerator.is_available(): + acc = torch.accelerator.current_accelerator() + device = torch.device(acc) + backend = torch.distributed.get_default_backend_for_device(device) +else: + device = torch.device("cpu") + backend = "gloo" print(device) diff --git a/examples/tutorials/speech_recognition_pipeline_tutorial.py b/examples/tutorials/speech_recognition_pipeline_tutorial.py index 2c8dfc752b..f5654ba7d4 100644 --- a/examples/tutorials/speech_recognition_pipeline_tutorial.py +++ b/examples/tutorials/speech_recognition_pipeline_tutorial.py @@ -42,7 +42,13 @@ print(torchaudio.__version__) torch.random.manual_seed(0) -device = torch.device("cuda" if torch.cuda.is_available() else "cpu") +if torch.accelerator.is_available(): + acc = torch.accelerator.current_accelerator() + device = torch.device(acc) + backend = torch.distributed.get_default_backend_for_device(device) +else: + device = torch.device("cpu") + backend = "gloo" print(device)