code: def export(
      func: Optional[object] = None,
      dir_name: str = "./log/",
      worker_name: Optional[str] = None,
      use_gzip: bool = False,
  ):
      """Exports current and aggregated traces for:
      - Chrome tensorboard
      - FlameGraph
      (and sets default parameters for log file folder/filenames).
      """
      import os
      import socket
      import time
  
      # Chrome export (default handler); inspired the log_file() function below.
      tensorboard_handler = profiler.tensorboard_trace_handler(
          dir_name=dir_name, worker_name=worker_name, use_gzip=use_gzip
      )
  
      def trace_handler(prof: profiler.profile):
          """trace_handler implementation."""
  
          def log_file(export_chrome: bool = False, info: str = ""):
              """Implementation of logging file."""
              nonlocal worker_name
              if not worker_name:
                  worker_name = "{}_{}".format(
                      socket.gethostname(), str(os.getpid())
                  )
              if export_chrome:
                  ext = "pt.trace.json"
              else:
                  ext = "txt"
              if info:
                  pattern = "{{}}.{{}}_{}.{{}}".format(info)
              else:
                  pattern = "{}.{}.{}"
              file_name = pattern.format(
                  worker_name, int(time.time() * 1000), ext
              )
              if use_gzip:
                  file_name = file_name + ".gz"
              return os.path.join(dir_name, file_name)
  
          def export_stacks(log_path: str, metric: str):
              """Implementation of export_stacks."""
              prof.export_stacks(log_file(), metric)
  
          def export_traces(aggregated_traces: bool = False):
              """Implementation of export_traces."""
              if not aggregated_traces:
                  # Chrome export (also checks for dir_name existing).
                  tensorboard_handler(prof)
  
              # FlameGraph exports.
              if prof.with_stack or aggregated_traces:
                  log_path = (
                      log_file(info="aggregated")
                      if aggregated_traces
                      else log_file()
                  )
                  export_stacks(
                      log_path=log_path, metric="self_cpu_time_total"
                  )
                  if prof.profiler is not None:
                      if prof.profiler.use_cuda:
                          export_stacks(
                              log_path=log_path,
                              metric="self_cuda_time_total",
                          )
  
          # export last logged trace - skip if events are empty (e.g., profiler created w/o any torch.nn call)
          if prof.events():
              export_traces()
  
      return set_profiler_attr(
          func=func, set_attr="on_trace_ready", handler=trace_handler
      )
  
file_path: speechbrain/speechbrain/utils/profiling.py
imports: import numpy
  from copy import deepcopy
  from torch import profiler
  from functools import wraps
  from typing import Any, Callable, Iterable, Optional
label_desc: Use image node_id 3 for calling a global function with example usage: export(func, dir_name, worker_name, use_gzip) and returns: set_profiler_attr, os