def test_collect_callgrind(self): with self.assertRaisesRegex( ValueError, r"`collect_callgrind` requires that globals be wrapped " r"in `CopyIfCallgrind` so that serialization is explicit."): benchmark_utils.Timer("pass", globals={ "x": 1 }).collect_callgrind(collect_baseline=False) with self.assertRaisesRegex( # Subprocess raises AttributeError (from pickle), # _ValgrindWrapper re-raises as generic OSError. OSError, "AttributeError: Can't get attribute 'MyModule'"): benchmark_utils.Timer("model(1)", globals={ "model": benchmark_utils.CopyIfCallgrind( MyModule()) }).collect_callgrind(collect_baseline=False) @torch.jit.script def add_one(x): return x + 1 timer = benchmark_utils.Timer( "y = add_one(x) + k", setup="x = torch.ones((1,))", globals={ "add_one": benchmark_utils.CopyIfCallgrind(add_one), "k": benchmark_utils.CopyIfCallgrind(5), "model": benchmark_utils.CopyIfCallgrind(MyModule(), setup=f"""\ import sys sys.path.append({repr(os.path.split(os.path.abspath(__file__))[0])}) from test_benchmark_utils import MyModule """) }) stats = timer.collect_callgrind(number=1000) counts = stats.counts(denoise=False) self.assertIsInstance(counts, int) self.assertGreater(counts, 0) stats = timer.collect_callgrind(number=1000, repeats=10) assert isinstance(stats, tuple) # Check that the repeats are at least somewhat repeatable. counts = collections.Counter([s.counts(denoise=True) for s in stats]) self.assertGreater( max(counts.values), 1, f"Every instruction count total was unique: {counts}") from torch.utils.benchmark.utils.valgrind_wrapper.timer_interface import wrapper_singleton self.assertIsNone(wrapper_singleton()._bindings_module, "JIT'd bindings are only for back testing.")
def test_collect_callgrind(self): with self.assertRaisesRegex( ValueError, r"`collect_callgrind` requires that globals be wrapped " r"in `CopyIfCallgrind` so that serialization is explicit." ): benchmark_utils.Timer( "pass", globals={"x": 1} ).collect_callgrind(collect_baseline=False) with self.assertRaisesRegex( # Subprocess raises AttributeError (from pickle), # _ValgrindWrapper re-raises as generic OSError. OSError, "AttributeError: Can't get attribute 'MyModule'" ): benchmark_utils.Timer( "model(1)", globals={"model": benchmark_utils.CopyIfCallgrind(MyModule())} ).collect_callgrind(collect_baseline=False) @torch.jit.script def add_one(x): return x + 1 timer = benchmark_utils.Timer( "y = add_one(x) + k", setup="x = torch.ones((1,))", globals={ "add_one": benchmark_utils.CopyIfCallgrind(add_one), "k": benchmark_utils.CopyIfCallgrind(5), "model": benchmark_utils.CopyIfCallgrind( MyModule(), setup=f"""\ import sys sys.path.append({repr(os.path.split(os.path.abspath(__file__))[0])}) from test_benchmark_utils import MyModule """ ) } ) # Don't collect baseline to speed up unit test by ~30 seconds. stats = timer.collect_callgrind(number=1000, collect_baseline=False) counts = stats.counts(denoise=False) self.assertIsInstance(counts, int) self.assertGreater(counts, 0) from torch.utils.benchmark.utils.valgrind_wrapper.timer_interface import wrapper_singleton self.assertIsNone( wrapper_singleton()._bindings_module, "JIT'd bindings are only for back testing." )
def test_collect_callgrind(self): with self.assertRaisesRegex( ValueError, r"`collect_callgrind` requires that globals be wrapped " r"in `CopyIfCallgrind` so that serialization is explicit."): benchmark_utils.Timer("pass", globals={ "x": 1 }).collect_callgrind(collect_baseline=False) with self.assertRaisesRegex( # Subprocess raises AttributeError (from pickle), # _ValgrindWrapper re-raises as generic OSError. OSError, "AttributeError: Can't get attribute 'MyModule'"): benchmark_utils.Timer("model(1)", globals={ "model": benchmark_utils.CopyIfCallgrind( MyModule()) }).collect_callgrind(collect_baseline=False) @torch.jit.script def add_one(x): return x + 1 timer = benchmark_utils.Timer( "y = add_one(x) + k", setup="x = torch.ones((1,))", globals={ "add_one": benchmark_utils.CopyIfCallgrind(add_one), "k": benchmark_utils.CopyIfCallgrind(5), "model": benchmark_utils.CopyIfCallgrind(MyModule(), setup=f"""\ import sys sys.path.append({repr(os.path.split(os.path.abspath(__file__))[0])}) from test_benchmark_utils import MyModule """) }) stats = timer.collect_callgrind(number=1000) counts = stats.counts(denoise=False) self.assertIsInstance(counts, int) self.assertGreater(counts, 0) # There is some jitter with the allocator, so we use a simpler task to # test reproducibility. timer = benchmark_utils.Timer( "x += 1", setup="x = torch.ones((1,))", ) stats = timer.collect_callgrind(number=1000, repeats=20) assert isinstance(stats, tuple) # Check that the repeats are at least somewhat repeatable. (within 10 instructions per iter) counts = collections.Counter( [s.counts(denoise=True) // 10_000 * 10_000 for s in stats])