code: def test_manipulate_callgrind_stats(self):
      stats_no_data, stats_with_data = load_callgrind_artifacts()
  
      # Mock `torch.set_printoptions(linewidth=160)`
      wide_linewidth = benchmark_utils.FunctionCounts(
          stats_no_data.stats(inclusive=False)._data,
          False,
          _linewidth=160,
      )
  
      for l in repr(wide_linewidth).splitlines(keepends=False):
          self.assertLessEqual(len(l), 160)
  
      self.assertEqual(
          # `delta` is just a convenience method.
          stats_with_data.delta(stats_no_data)._data,
          (stats_with_data.stats() - stats_no_data.stats())._data,
      )
  
      deltas = stats_with_data.as_standardized().delta(
          stats_no_data.as_standardized()
      )
  
      def custom_transforms(fn: str):
          fn = re.sub(re.escape("/usr/include/c++/8/bits/"), "", fn)
          fn = re.sub(r"build/../", "", fn)
          fn = re.sub(".+" + re.escape("libsupc++"), "libsupc++", fn)
          return fn
  
      self.regularizeAndAssertExpectedInline(
          stats_no_data,
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.CallgrindStats object at 0xXXXXXXXXXXXX>
              y = torch.ones(())
                                         All          Noisy symbols removed
                  Instructions:      8869966                    8728096
                  Baseline:             6682                       5766
              1000 runs per measurement, 1 thread""",
      )
  
      self.regularizeAndAssertExpectedInline(
          stats_no_data.counts(),
          """8869966""",
      )
  
      self.regularizeAndAssertExpectedInline(
          stats_no_data.counts(denoise=True),
          """8728096""",
      )
  
      self.regularizeAndAssertExpectedInline(
          stats_no_data.stats(),
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.FunctionCounts object at 0xXXXXXXXXXXXX>
                408000  ???:__tls_get_addr [/usr/lib64/ld-2.28.so]
                388193  ???:_int_free [/usr/lib64/libc-2.28.so]
                274000  build/../torch/csrc/utils/python ... rch/torch/lib/libtorch_python.so]
                264000  build/../aten/src/ATen/record_fu ... ytorch/torch/lib/libtorch_cpu.so]
                192000  build/../c10/core/Device.h:c10:: ... epos/pytorch/torch/lib/libc10.so]
                169855  ???:_int_malloc [/usr/lib64/libc-2.28.so]
                154000  build/../c10/core/TensorOptions. ... ytorch/torch/lib/libtorch_cpu.so]
                148561  /tmp/build/80754af9/python_15996 ... da3/envs/throwaway/bin/python3.6]
                135000  ???:malloc [/usr/lib64/libc-2.28.so]
                   ...
                  2000  /usr/include/c++/8/ext/new_allocator.h:torch::PythonArgs::intlist(int)
                  2000  /usr/include/c++/8/bits/stl_vect ... *, _object*, _object*, _object**)
                  2000  /usr/include/c++/8/bits/stl_vect ... rningHandler::~PyWarningHandler()
                  2000  /usr/include/c++/8/bits/stl_vect ... ject*, _object*, _object**, bool)
                  2000  /usr/include/c++/8/bits/stl_algobase.h:torch::PythonArgs::intlist(int)
                  2000  /usr/include/c++/8/bits/shared_p ... ad_accumulator(at::Tensor const&)
                  2000  /usr/include/c++/8/bits/move.h:c ... te<c10::AutogradMetaInterface> >)
                  2000  /usr/include/c++/8/bits/atomic_b ... DispatchKey&&, caffe2::TypeMeta&)
                  2000  /usr/include/c++/8/array:at::Ten ... , at::Tensor&, c10::Scalar) const
  
              Total: 8869966""",
      )
  
      self.regularizeAndAssertExpectedInline(
          stats_no_data.stats(inclusive=True),
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.FunctionCounts object at 0xXXXXXXXXXXXX>
                8959166  ???:0x0000000000001050 [/usr/lib64/ld-2.28.so]
                8959166  ???:(below main) [/usr/lib64/libc-2.28.so]
                8959166  /tmp/build/80754af9/python_15996 ... a3/envs/throwaway/bin/python3.6]
                8959166  /tmp/build/80754af9/python_15996 ... a3/envs/throwaway/bin/python3.6]
                8959166  /tmp/build/80754af9/python_15996 ... a3/envs/throwaway/bin/python3.6]
                8959166  /tmp/build/80754af9/python_15996 ... a3/envs/throwaway/bin/python3.6]
                8959166  /tmp/build/80754af9/python_15996 ... a3/envs/throwaway/bin/python3.6]
                8959166  /tmp/build/80754af9/python_15996 ... a3/envs/throwaway/bin/python3.6]
                8959166  /tmp/build/80754af9/python_15996 ... a3/envs/throwaway/bin/python3.6]
                    ...
                  92821  /tmp/build/80754af9/python_15996 ... a3/envs/throwaway/bin/python3.6]
                  91000  build/../torch/csrc/tensor/pytho ... ch/torch/lib/libtorch_python.so]
                  91000  /data/users/test_user/repos/pyto ... nsors::get_default_scalar_type()
                  90090  ???:pthread_mutex_lock [/usr/lib64/libpthread-2.28.so]
                  90000  build/../c10/core/TensorImpl.h:c ... ch/torch/lib/libtorch_python.so]
                  90000  build/../aten/src/ATen/record_fu ... torch/torch/lib/libtorch_cpu.so]
                  90000  /data/users/test_user/repos/pyto ... uard(c10::optional<c10::Device>)
                  90000  /data/users/test_user/repos/pyto ... ersionCounter::~VersionCounter()
                  88000  /data/users/test_user/repos/pyto ... ratorKernel*, at::Tensor const&)""",
      )
  
      self.regularizeAndAssertExpectedInline(
          wide_linewidth,
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.FunctionCounts object at 0xXXXXXXXXXXXX>
                408000  ???:__tls_get_addr [/usr/lib64/ld-2.28.so]
                388193  ???:_int_free [/usr/lib64/libc-2.28.so]
                274000  build/../torch/csrc/utils/python_arg_parser.cpp:torch::FunctionSignature ...  bool) [/data/users/test_user/repos/pytorch/torch/lib/libtorch_python.so]
                264000  build/../aten/src/ATen/record_function.cpp:at::RecordFunction::RecordFun ... ordScope) [/data/users/test_user/repos/pytorch/torch/lib/libtorch_cpu.so]
                192000  build/../c10/core/Device.h:c10::Device::validate() [/data/users/test_user/repos/pytorch/torch/lib/libc10.so]
                169855  ???:_int_malloc [/usr/lib64/libc-2.28.so]
                154000  build/../c10/core/TensorOptions.h:c10::TensorOptions::merge_in(c10::Tens ... ns) const [/data/users/test_user/repos/pytorch/torch/lib/libtorch_cpu.so]
                148561  /tmp/build/80754af9/python_1599604603603/work/Python/ceval.c:_PyEval_EvalFrameDefault [/home/test_user/miniconda3/envs/throwaway/bin/python3.6]
                135000  ???:malloc [/usr/lib64/libc-2.28.so]
                   ...
                  2000  /usr/include/c++/8/ext/new_allocator.h:torch::PythonArgs::intlist(int)
                  2000  /usr/include/c++/8/bits/stl_vector.h:torch::PythonArgParser::raw_parse(_object*, _object*, _object*, _object**)
                  2000  /usr/include/c++/8/bits/stl_vector.h:torch::PyWarningHandler::~PyWarningHandler()
                  2000  /usr/include/c++/8/bits/stl_vector.h:torch::FunctionSignature::parse(_object*, _object*, _object*, _object**, bool)
                  2000  /usr/include/c++/8/bits/stl_algobase.h:torch::PythonArgs::intlist(int)
                  2000  /usr/include/c++/8/bits/shared_ptr_base.h:torch::autograd::impl::try_get_grad_accumulator(at::Tensor const&)
                  2000  /usr/include/c++/8/bits/move.h:c10::TensorImpl::set_autograd_meta(std::u ... AutogradMetaInterface, std::default_delete<c10::AutogradMetaInterface> >)
                  2000  /usr/include/c++/8/bits/atomic_base.h:at::Tensor at::detail::make_tensor ... t_null_type<c10::StorageImpl> >&&, c10::DispatchKey&&, caffe2::TypeMeta&)
                  2000  /usr/include/c++/8/array:at::Tensor& c10::Dispatcher::callWithDispatchKe ... , c10::Scalar)> const&, c10::DispatchKey, at::Tensor&, c10::Scalar) const
  
              Total: 8869966""",  # noqa: B950
      )
  
      self.regularizeAndAssertExpectedInline(
          stats_no_data.as_standardized().stats(),
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.FunctionCounts object at 0xXXXXXXXXXXXX>
                408000  ???:__tls_get_addr
                388193  ???:_int_free
                274000  build/../torch/csrc/utils/python ... ject*, _object*, _object**, bool)
                264000  build/../aten/src/ATen/record_fu ... ::RecordFunction(at::RecordScope)
                192000  build/../c10/core/Device.h:c10::Device::validate()
                169855  ???:_int_malloc
                154000  build/../c10/core/TensorOptions. ... erge_in(c10::TensorOptions) const
                148561  Python/ceval.c:_PyEval_EvalFrameDefault
                135000  ???:malloc
                   ...
                  2000  /usr/include/c++/8/ext/new_allocator.h:torch::PythonArgs::intlist(int)
                  2000  /usr/include/c++/8/bits/stl_vect ... *, _object*, _object*, _object**)
                  2000  /usr/include/c++/8/bits/stl_vect ... rningHandler::~PyWarningHandler()
                  2000  /usr/include/c++/8/bits/stl_vect ... ject*, _object*, _object**, bool)
                  2000  /usr/include/c++/8/bits/stl_algobase.h:torch::PythonArgs::intlist(int)
                  2000  /usr/include/c++/8/bits/shared_p ... ad_accumulator(at::Tensor const&)
                  2000  /usr/include/c++/8/bits/move.h:c ... te<c10::AutogradMetaInterface> >)
                  2000  /usr/include/c++/8/bits/atomic_b ... DispatchKey&&, caffe2::TypeMeta&)
                  2000  /usr/include/c++/8/array:at::Ten ... , at::Tensor&, c10::Scalar) const
  
              Total: 8869966""",
      )
  
      self.regularizeAndAssertExpectedInline(
          deltas,
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.FunctionCounts object at 0xXXXXXXXXXXXX>
                  85000  Objects/dictobject.c:lookdict_unicode
                  59089  ???:_int_free
                  43000  ???:malloc
                  25000  build/../torch/csrc/utils/python ... :torch::PythonArgs::intlist(int)
                  24000  ???:__tls_get_addr
                  23000  ???:free
                  21067  Objects/dictobject.c:lookdict_unicode_nodummy
                  20000  build/../torch/csrc/utils/python ... :torch::PythonArgs::intlist(int)
                  18000  Objects/longobject.c:PyLong_AsLongLongAndOverflow
                    ...
                   2000  /home/nwani/m3/conda-bld/compile ... del_op.cc:operator delete(void*)
                   1000  /usr/include/c++/8/bits/stl_vector.h:torch::PythonArgs::intlist(int)
                    193  ???:_int_malloc
                     75  ???:_int_memalign
                  -1000  build/../c10/util/SmallVector.h: ... _contiguous(c10::ArrayRef<long>)
                  -1000  build/../c10/util/SmallVector.h: ... nsor_restride(c10::MemoryFormat)
                  -1000  /usr/include/c++/8/bits/stl_vect ... es(_object*, _object*, _object*)
                  -8000  Python/ceval.c:_PyEval_EvalFrameDefault
                 -16000  Objects/tupleobject.c:PyTuple_New
  
              Total: 432917""",
      )
  
      self.regularizeAndAssertExpectedInline(len(deltas), """35""")
  
      self.regularizeAndAssertExpectedInline(
          deltas.transform(custom_transforms),
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.FunctionCounts object at 0xXXXXXXXXXXXX>
                  85000  Objects/dictobject.c:lookdict_unicode
                  59089  ???:_int_free
                  43000  ???:malloc
                  25000  torch/csrc/utils/python_numbers.h:torch::PythonArgs::intlist(int)
                  24000  ???:__tls_get_addr
                  23000  ???:free
                  21067  Objects/dictobject.c:lookdict_unicode_nodummy
                  20000  torch/csrc/utils/python_arg_parser.h:torch::PythonArgs::intlist(int)
                  18000  Objects/longobject.c:PyLong_AsLongLongAndOverflow
                    ...
                   2000  c10/util/SmallVector.h:c10::TensorImpl::compute_contiguous() const
                   1000  stl_vector.h:torch::PythonArgs::intlist(int)
                    193  ???:_int_malloc
                     75  ???:_int_memalign
                  -1000  stl_vector.h:torch::autograd::TH ... es(_object*, _object*, _object*)
                  -1000  c10/util/SmallVector.h:c10::Tens ... _contiguous(c10::ArrayRef<long>)
                  -1000  c10/util/SmallVector.h:c10::Tens ... nsor_restride(c10::MemoryFormat)
                  -8000  Python/ceval.c:_PyEval_EvalFrameDefault
                 -16000  Objects/tupleobject.c:PyTuple_New
  
              Total: 432917""",
      )
  
      self.regularizeAndAssertExpectedInline(
          deltas.filter(lambda fn: fn.startswith("???")),
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.FunctionCounts object at 0xXXXXXXXXXXXX>
                59089  ???:_int_free
                43000  ???:malloc
                24000  ???:__tls_get_addr
                23000  ???:free
                  193  ???:_int_malloc
                   75  ???:_int_memalign
  
              Total: 149357""",
      )
  
      self.regularizeAndAssertExpectedInline(
          deltas[:5],
          """\
              <torch.utils.benchmark.utils.valgrind_wrapper.timer_interface.FunctionCounts object at 0xXXXXXXXXXXXX>
                85000  Objects/dictobject.c:lookdict_unicode
                59089  ???:_int_free
                43000  ???:malloc
                25000  build/../torch/csrc/utils/python_ ... h:torch::PythonArgs::intlist(int)
                24000  ???:__tls_get_addr
  
              Total: 236089""",
      )
  
imports: import collections
  import json
  import os
  import re
  import textwrap
  import timeit
  from typing import Any, List, Tuple
  import unittest
  import torch
  import torch.utils.benchmark
  from torch.testing._internal.common_utils import TestCase, run_tests, IS_SANDCASTLE, IS_WINDOWS, slowTest, TEST_WITH_ASAN
  import expecttest
  import numpy
label_desc: Use image node_id 8 for calling the TestBenchmarkUtils obj's underlying member method code with example usage: obj.test_manipulate_callgrind_stats() and returns: fn