diff --git a/recipes_source/recipes/profiler_recipe.py b/recipes_source/recipes/profiler_recipe.py index 6402c74e77..602ab1a7a8 100644 --- a/recipes_source/recipes/profiler_recipe.py +++ b/recipes_source/recipes/profiler_recipe.py @@ -163,18 +163,20 @@ # Note the occurrence of ``aten::convolution`` twice with different input shapes. ###################################################################### -# Profiler can also be used to analyze performance of models executed on GPUs and XPUs: +# Profiler can also be used to analyze performance of models executed on GPUs: # Users could switch between cpu, cuda and xpu +activities = [ProfilerActivity.CPU] if torch.cuda.is_available(): device = 'cuda' + activities += [ProfilerActivity.CUDA] elif torch.xpu.is_available(): device = 'xpu' + activities += [ProfilerActivity.XPU] else: print('Neither CUDA nor XPU devices are available to demonstrate profiling on acceleration devices') import sys sys.exit(0) -activities = [ProfilerActivity.CPU, ProfilerActivity.CUDA, ProfilerActivity.XPU] sort_by_keyword = device + "_time_total" model = models.resnet18().to(device) @@ -308,9 +310,17 @@ # Profiling results can be outputted as a ``.json`` trace file: # Tracing CUDA or XPU kernels # Users could switch between cpu, cuda and xpu -device = 'cuda' - -activities = [ProfilerActivity.CPU, ProfilerActivity.CUDA, ProfilerActivity.XPU] +activities = [ProfilerActivity.CPU] +if torch.cuda.is_available(): + device = 'cuda' + activities += [ProfilerActivity.CUDA] +elif torch.xpu.is_available(): + device = 'xpu' + activities += [ProfilerActivity.XPU] +else: + print('Neither CUDA nor XPU devices are available to demonstrate profiling on acceleration devices') + import sys + sys.exit(0) model = models.resnet18().to(device) inputs = torch.randn(5, 3, 224, 224).to(device)