def collect_callgrind(self, task_spec: common.TaskSpec, globals: Dict[str, Any], number: int, collect_baseline: bool) -> CallgrindStats: """Collect stats, and attach a reference run which can be used to filter interpreter overhead.""" self._validate() baseline_inclusive_stats = FunctionCounts((), inclusive=True) baseline_exclusive_stats = FunctionCounts((), inclusive=False) if collect_baseline: cache_key = (number, task_spec.num_threads) if cache_key not in self._baseline_cache: self._baseline_cache[cache_key] = self._invoke( common.TaskSpec(stmt="pass", setup="pass", num_threads=task_spec.num_threads), globals={}, number=number, ) baseline_inclusive_stats, baseline_exclusive_stats = \ self._baseline_cache[cache_key] stmt_inclusive_stats, stmt_exclusive_stats = self._invoke( task_spec, globals, number) return CallgrindStats( task_spec=task_spec, number_per_run=number, built_with_debug_symbols=self._build_type == "RelWithDebInfo", baseline_inclusive_stats=baseline_inclusive_stats, baseline_exclusive_stats=baseline_exclusive_stats, stmt_inclusive_stats=stmt_inclusive_stats, stmt_exclusive_stats=stmt_exclusive_stats, )
def __init__( self, stmt="pass", setup="pass", timer=timer, globals: Optional[dict] = None, label: Optional[str] = None, sub_label: Optional[str] = None, description: Optional[str] = None, env: Optional[str] = None, num_threads=1, ): if not isinstance(stmt, str): raise ValueError("Currently only a `str` stmt is supported.") # We copy `globals` to prevent mutations from leaking, (for instance, # `eval` adds the `__builtins__` key) and include `torch` if not # specified as a convenience feature. globals = dict(globals or {}) globals.setdefault("torch", torch) self._timer = self._timer_cls(stmt=stmt, setup=setup, timer=timer, globals=globals) self._task_spec = common.TaskSpec( stmt=stmt, setup=setup, label=label, sub_label=sub_label, description=description, env=env, num_threads=num_threads, )
def __init__( self, stmt: str = "pass", setup: str = "pass", timer: Callable[[], float] = timer, globals: Optional[Dict[str, Any]] = None, label: Optional[str] = None, sub_label: Optional[str] = None, description: Optional[str] = None, env: Optional[str] = None, num_threads: int = 1, ): if not isinstance(stmt, str): raise ValueError("Currently only a `str` stmt is supported.") # We copy `globals` to prevent mutations from leaking, (for instance, # `eval` adds the `__builtins__` key) and include `torch` if not # specified as a convenience feature. globals = dict(globals or {}) globals.setdefault("torch", torch) self._globals = globals # Convenience adjustment so that multi-line code snippets defined in # functions do not IndentationError inside timeit.Timer. The leading # newline removal is for the initial newline that appears when defining # block strings. For instance: # textwrap.dedent(""" # print("This is a stmt") # """) # produces '\nprint("This is a stmt")\n'. # # Stripping this down to 'print("This is a stmt")' doesn't change # what gets executed, but it makes __repr__'s nicer. stmt = textwrap.dedent(stmt) stmt = (stmt[1:] if stmt[0] == "\n" else stmt).rstrip() setup = textwrap.dedent(setup) setup = (setup[1:] if setup[0] == "\n" else setup).rstrip() self._timer = self._timer_cls( stmt=stmt, setup=setup, timer=timer, globals=valgrind_timer_interface.CopyIfCallgrind.unwrap_all( globals), ) self._task_spec = common.TaskSpec( stmt=stmt, setup=setup, label=label, sub_label=sub_label, description=description, env=env, num_threads=num_threads, )
def __init__( self, stmt: str = "pass", setup: str = "pass", timer: Callable[[], float] = timer, globals: Optional[Dict[str, Any]] = None, label: Optional[str] = None, sub_label: Optional[str] = None, description: Optional[str] = None, env: Optional[str] = None, num_threads: int = 1, language: Union[Language, str] = Language.PYTHON, ): if not isinstance(stmt, str): raise ValueError("Currently only a `str` stmt is supported.") # We copy `globals` to prevent mutations from leaking. # (For instance, `eval` adds the `__builtins__` key) self._globals = dict(globals or {}) if language in (Language.PYTHON, "py", "python"): # Include `torch` if not specified as a convenience feature. self._globals.setdefault("torch", torch) self._language: Language = Language.PYTHON elif language in (Language.CPP, "cpp", "c++"): assert self._timer_cls is timeit.Timer, "_timer_cls has already been swapped." self._timer_cls = CPPTimer setup = ("" if setup == "pass" else setup) self._language = Language.CPP else: raise ValueError(f"Invalid language `{language}`.") # Convenience adjustment so that multi-line code snippets defined in # functions do not IndentationError (Python) or look odd (C++). The # leading newline removal is for the initial newline that appears when # defining block strings. For instance: # textwrap.dedent(""" # print("This is a stmt") # """) # produces '\nprint("This is a stmt")\n'. # # Stripping this down to 'print("This is a stmt")' doesn't change # what gets executed, but it makes __repr__'s nicer. stmt = textwrap.dedent(stmt) stmt = (stmt[1:] if stmt and stmt[0] == "\n" else stmt).rstrip() setup = textwrap.dedent(setup) setup = (setup[1:] if setup and setup[0] == "\n" else setup).rstrip() self._timer = self._timer_cls( stmt=stmt, setup=setup, timer=timer, globals=valgrind_timer_interface.CopyIfCallgrind.unwrap_all(self._globals), ) self._task_spec = common.TaskSpec( stmt=stmt, setup=setup, label=label, sub_label=sub_label, description=description, env=env, num_threads=num_threads, )