class_docstr: An utility API to create function_hook callbacks to profile the execution time of each function.
  Passing ``ext_name`` and ``device_id``, you can define which device time you want to profile.
  If `ext_name` = "cuda" or "cudnn", then cudaEvent will be used to measure the execution time.
  For more information about cudaEvent, see the CUDA `document <https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__EVENT.html>`_.
  If `ext_name`="cpu" , then wall-clock-time on host will be used.
  
  Example:
  
  .. code-block:: python
  
      ext_name = "cpu"
      device_id = "0"
  
      from nnabla.ext_utils import get_extension_context
      ctx = get_extension_context(ext_name, device_id=device_id)
      nn.set_default_context(ctx)
  
      y = model(...)
  
      from nnabla.utils.inspection import TimeProfiler
      tp = TimeProfiler(ext_name=ext_name, device_id=device_id)
  
      for i in range(max_iter):
          # All results of executions under "forward" scope are registered as "forward" execution.
          with tp.scope("forward"):
              y.forward(function_pre_hook=tp.pre_hook, function_post_hook=tp.post_hook)
  
          # All results of executions under "backward" scope are registered as "backward" execution.
          with tp.scope("backward") as tp:
              y.backward(function_pre_hook=tp.pre_hook, function_post_hook=tp.post_hook)
  
          # All results are evaluated by passing scopes to .calc_elapsed_time().
          # Be sure to call calc_elapsed_time at each iteration, otherwise nothing is measured.
          tp.calc_elapsed_time(["forward", "backward", "summary"])
  
      # To output results on stdout, call instance as a function.
      tp()
  
      # To write out as csv file, call .to_csv().
      tp.to_csv(output_file_name)
class_docstr_tok: ["An","utility","API","to","create","function_hook","callbacks","to","profile","the","execution","time","of","each","function",".","Passing","``","ext_name","``","and","``","device_id","``",",","you","can","define","which","device","time","you","want","to","profile",".","If","`","ext_name","`","=","``","cuda","''","or","``","cudnn","''",",","then","cudaEvent","will","be","used","to","measure","the","execution","time",".","For","more","information","about","cudaEvent",",","see","the","CUDA","`","document","<","https",":","\/\/docs.nvidia.com\/cuda\/cuda-runtime-api\/group__CUDART__EVENT.html",">","`","_",".","If","`","ext_name","`","=","''","cpu","''",",","then","wall-clock-time","on","host","will","be","used",".","Example",":","..","code-block",":",":","python","ext_name","=","``","cpu","''","device_id","=","``","0","''","from","nnabla.ext_utils","import","get_extension_context","ctx","=","get_extension_context","(","ext_name",",","device_id=device_id",")","nn.set_default_context","(","ctx",")","y","=","model","(","...",")","from","nnabla.utils.inspection","import","TimeProfiler","tp","=","TimeProfiler","(","ext_name=ext_name",",","device_id=device_id",")","for","i","in","range","(","max_iter",")",":","#","All","results","of","executions","under","``","forward","''","scope","are","registered","as","``","forward","''","execution",".","with","tp.scope","(","``","forward","''",")",":","y.forward","(","function_pre_hook=tp.pre_hook",",","function_post_hook=tp.post_hook",")","#","All","results","of","executions","under","``","backward","''","scope","are","registered","as","``","backward","''","execution",".","with","tp.scope","(","``","backward","''",")","as","tp",":","y.backward","(","function_pre_hook=tp.pre_hook",",","function_post_hook=tp.post_hook",")","#","All","results","are","evaluated","by","passing","scopes","to",".calc_elapsed_time","(",")",".","#","Be","sure","to","call","calc_elapsed_time","at","each","iteration",",","otherwise","nothing","is","measured",".","tp.calc_elapsed_time","(","[","``","forward","''",",","``","backward","''",",","``","summary","''","]",")","#","To","output","results","on","stdout",",","call","instance","as","a","function",".","tp","(",")","#","To","write","out","as","csv","file",",","call",".to_csv","(",")",".","tp.to_csv","(","output_file_name",")"]
docstr: Args:
       ext_name (str): backend extension name (e.g. cpu, cuda, or cudnn)
       device_id (str): device id
docstr_tok: ["Args",":","ext_name","(","str",")",":","backend","extension","name","(","e.g",".","cpu",",","cuda",",","or","cudnn",")","device_id","(","str",")",":","device","id"]
code: def __init__(self, ext_name, device_id):
      """
      Args:
           ext_name (str): backend extension name (e.g. cpu, cuda, or cudnn)
           device_id (str): device id
      """
      if ext_name == "cpu":
          self.profiler = import_module(
              "nnabla.utils.inspection"
          ).CpuTimerCallback
  
      elif ext_name in ["cuda", "cudnn"]:
          self.profiler = import_extension_module(
              "cuda.utils.inspection"
          ).CudaEventTimerCallback
  
      else:
          # Unsupported extension.
          raise NotImplementedError(
              "Profiler for the extension '{}' is not implemented.".format(
                  ext_name
              )
          )
  
      self.ext_name = ext_name
      self.device_id = device_id
      self._scope_name = ""
      self.profilers = {}
      self.create_new_profiler("summary")
  
imports: from __future__ import absolute_import
  import csv
  import os
  import time
  from collections import OrderedDict
  from contextlib import contextmanager
  from importlib import import_module
  import numpy
  from nnabla.ext_utils import import_extension_module
  from .base import FunctionHookCallbackBase
label_desc: Use image node_id 1 to create a new TimeProfiler object from inherited base classes: FunctionHookCallbackBase with example: obj = TimeProfiler(ext_name, device_id)