code: def test_profile_func(device):
      # import torch
      # from pytest import raises
      # from torch.optim import SGD
      # from speechbrain.core import Brain
      # from torch.autograd.profiler import record_function
      from speechbrain.utils.profiling import profile
  
      # from speechbrain.utils.profiling import events_diff
  
      """
      class SimpleBrain(Brain):
          def compute_forward(self, batch, stage):
              return self.modules.model(batch[0])
  
          def compute_objectives(self, predictions, batch, stage):
              return torch.nn.functional.l1_loss(predictions, batch[1])
  
      class SimpleBrainNittyGritty(Brain):
          def compute_forward(self, batch, stage):
              # example: one way of using torch.autograd.profiler.record_function
              with record_function("is this faster (?)"):
                  this = self.modules.model(batch[0])
              return this
  
          def compute_objectives(self, predictions, batch, stage):
              # example: one could also think of running comparative testing using record_function
              with record_function("or that (?)"):
                  that = torch.nn.functional.l1_loss(predictions, batch[1])
              return that
      """
  
      @profile
      def train(brain, train_set, valid_set):
          brain.fit(
              epoch_counter=range(10),
              train_set=train_set,
              valid_set=valid_set,
          )
  
      """
      model = torch.nn.Linear(in_features=10, out_features=10, device=device)
      inputs = torch.rand(10, 10, device=device)
      targets = torch.rand(10, 10, device=device)
      training_set = ([inputs, targets],)
      validation_set = ([inputs, targets],)
      simple_brain = SimpleBrain(
          {"model": model}, lambda x: SGD(x, 0.1), run_opts={"device": device}
      )
  
      prof_simple = train(simple_brain, training_set, validation_set)
      # print(prof_simple.key_averages().table(sort_by="cpu_time_total"))
      # assert len(prof_simple.events()) >= 2500  # 2832 with torch==1.10.1
      # assert len(prof_simple.key_averages()) >= 60  # 72 with torch==1.10.1
  
      simple_brain_nitty_gritty = SimpleBrainNittyGritty(
          {"model": model}, lambda x: SGD(x, 0.1), run_opts={"device": device}
      )
      prof_nitty_gritty = train(
          simple_brain_nitty_gritty, training_set, validation_set
      )
      # print(prof_nitty_gritty.key_averages().table(sort_by="cpu_time_total"))
      # assert len(prof_nitty_gritty.events()) >= 2500  # 3030 with torch==1.10.1
      # assert len(prof_nitty_gritty.key_averages()) >= 60  # 74 with torch==1.10.1
      """
  
      # The outputs of this diff are only for visualisation, ``simple_delta._build_tree()`` will throw an error.
      """
      simple_delta, nitty_gritty_delta = events_diff(
          prof_simple.key_averages(), prof_nitty_gritty.key_averages()
      )
      # assert len(simple_delta) >= 4  # == 6  # before; config dependent: 7
      # assert len(nitty_gritty_delta) >= 4  # == 8  # before
      # assert simple_delta.total_average().count == 582 #Switching off becuase sometimes it fails
      # assert nitty_gritty_delta.total_average().count == 780 #Switching off becuase sometimes it fails
      with raises(Exception) as err_tree:
          simple_delta._build_tree()  # as mentioned.
      assert err_tree.type == AttributeError
      with raises(Exception) as err_averages:
          simple_delta.key_averages()  # as mentioned.
      assert err_averages.type == AssertionError
      " ""Both classes have alike numbers of function calls (given the same input data and train function).
      Sparing where both have the same number of calls:
  
      print(simple_delta.table(sort_by="cpu_time_total"))
      ----------------  ------------  ------------  ------------  ------------  ------------  ------------
                  Name    Self CPU %      Self CPU   CPU total %     CPU total  CPU time avg    # of Calls
      ----------------  ------------  ------------  ------------  ------------  ------------  ------------
           aten::zeros        17.44%     240.000us        27.69%     381.000us       6.350us            60
           aten::empty        26.96%     371.000us        27.11%     373.000us       1.492us           250
          aten::detach        18.68%     257.000us        25.65%     353.000us       5.694us            62
            aten::add_        25.00%     344.000us        25.00%     344.000us       5.931us            58
                detach         6.98%      96.000us         9.45%     130.000us       2.097us            62
           aten::zero_         4.94%      68.000us         4.94%      68.000us       0.756us            90
      ----------------  ------------  ------------  ------------  ------------  ------------  ------------
      Self CPU time total: 1.376ms
  
      print(nitty_gritty_delta.table(sort_by="cpu_time_total"))
      ----------------------  ------------  ------------  ------------  ------------  ------------  ------------
                        Name    Self CPU %      Self CPU   CPU total %     CPU total  CPU time avg    # of Calls
      ----------------------  ------------  ------------  ------------  ------------  ------------  ------------
          is this faster (?)        29.70%       1.024ms        76.19%       2.627ms     131.350us            20
                 or that (?)        29.29%       1.010ms        67.00%       2.310ms     115.500us            20
                 aten::zeros         8.32%     287.000us        15.52%     535.000us       5.350us           100
                 aten::empty        14.01%     483.000us        14.07%     485.000us       1.470us           330
                  aten::add_         9.92%     342.000us         9.92%     342.000us       5.700us            60
                aten::detach         2.81%      97.000us         6.09%     210.000us       3.500us            60
                      detach         3.28%     113.000us         4.00%     138.000us       2.300us            60
                 aten::zero_         2.67%      92.000us         2.67%      92.000us       0.708us           130
      ----------------------  ------------  ------------  ------------  ------------  ------------  ------------
      Self CPU time total: 3.448ms
  
      Curiosity doesn't come for free ;-)
      """
  
label_desc: Use image node_id 2 for calling a global function with example usage: test_profile_func(device) without return types