forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_profiler_tree.py
1098 lines (1025 loc) · 45.3 KB
/
test_profiler_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Owner(s): ["oncall: profiler"]
import functools
import os
import re
import textwrap
import traceback
import unittest
import expecttest
import torch
from torch._C._profiler import _ExtraFields_PyCall, _ExtraFields_PyCCall
from torch.testing._internal.common_utils import (
IS_ARM64,
IS_WINDOWS,
run_tests,
skipIfTorchDynamo,
TEST_WITH_CROSSREF,
TestCase,
)
from torch.utils._pytree import tree_map
# These functions can vary from based on platform and build (e.g. with CUDA)
# and generally distract from rather than adding to the test.
PRUNE_ALL = 1
KEEP_ELLIPSES = 2
KEEP_NAME_AND_ELLIPSES = 3
PRUNE_FUNCTIONS = {
"torch/utils/_pytree.py(...): tree_map": KEEP_NAME_AND_ELLIPSES,
"torch/profiler/profiler.py(...): start": KEEP_ELLIPSES,
"torch/profiler/profiler.py(...): stop_trace": KEEP_ELLIPSES,
"torch/profiler/profiler.py(...): _transit_action": KEEP_ELLIPSES,
"<built-in method __exit__ of torch._C.DisableTorchFunctionSubclass object at 0xXXXXXXXXXXXX>": PRUNE_ALL,
"cudaStreamIsCapturing": PRUNE_ALL,
# These show up only on CUDA, prune them so the CUDA and CPU expected results can be the same
"cudaGetDeviceCount": PRUNE_ALL,
"cudaGetDeviceProperties_v2": PRUNE_ALL,
}
# ROCTracer is currently not producing events that profiler can extract. We
# should bring it up to parity with CUPTI Kineto / profiler integration, but in
# the mean time there is still utility in running tests but not checking that
# the values match expected value.
# 1) We will still catch runtime errors and assert failures
# 2) We can diff the output to see how far we are from parity
#
# TODO: We also fail to capture events for Windows on some platforms.
ALLOW_CUDA_FAILURE = (torch.version.hip is not None) or IS_WINDOWS
class TorchFunctionTensor(torch.Tensor):
@classmethod
def __torch_function__(cls, func, types, args=(), kwargs=None):
return super().__torch_function__(func, types, args, kwargs)
class TorchDispatchTensor(torch.Tensor):
@staticmethod
def __new__(cls, elem):
t = torch.Tensor._make_subclass(cls, elem, elem.requires_grad)
t.elem = elem
return t
@classmethod
def __torch_dispatch__(cls, func, types, args=(), kwargs=None):
def unwrap(x):
return x.elem if isinstance(x, TorchDispatchTensor) else x
def wrap(x):
return TorchDispatchTensor(x) if isinstance(x, torch.Tensor) else x
args = tree_map(unwrap, args)
kwargs = tree_map(unwrap, kwargs or {})
return tree_map(wrap, func(*args, **kwargs))
class ProfilerTree:
@staticmethod
def test(f):
"""Mark unit test that will be using ProfilerTree to test traces.
This decorator serves two purposes. First, it provides a method name
that `format` can use to tell where the test runner (which is
environment specific) ends and the unit test begins. Second, it runs
the test with replicates and allows `assertTreesMatch` to adjust
based on which replicate is running.
"""
@functools.wraps(f)
def begin_unit_test_marker(self, replicates=3):
try:
for i in range(replicates):
self.tree_replicate = i
out = f(self)
if self.tree_replicate is None:
break
return out
finally:
delattr(self, "tree_replicate")
return begin_unit_test_marker
@classmethod
def format(cls, profiler, indent: int = 0):
def flatten(nodes, depth=0, out=None):
if out is None:
out = []
for node in nodes:
cls.validate_node(node)
name = cls.fmt_name(node.name)
prune_level = PRUNE_FUNCTIONS.get(name.strip(), None)
if prune_level is None:
out.append((depth, name))
flatten(node.children, depth + 1, out)
elif prune_level == KEEP_NAME_AND_ELLIPSES:
out.append((depth, name))
if node.children:
out.append((depth + 1, "..."))
elif prune_level == KEEP_ELLIPSES:
out.append((depth, "..."))
else:
assert prune_level == PRUNE_ALL
return out
flat_nodes = flatten(profiler.kineto_results.experimental_event_tree())
# Profiler inserts a `cudaDeviceSynchronize` at the end of profiling.
# and may also insert 'Context Sync' CUDA synchronization event.
if flat_nodes and flat_nodes[-2][1] == "cudaDeviceSynchronize":
flat_nodes = flat_nodes[:-2]
if flat_nodes and flat_nodes[-1][1] == "cudaDeviceSynchronize":
flat_nodes = flat_nodes[:-1]
# Profiler inserts a `hipDeviceSynchronize` at the end of profiling.
if flat_nodes and flat_nodes[-1][1] == "hipDeviceSynchronize":
flat_nodes = flat_nodes[:-1]
min_depth = min(
[d + 1 for d, name in flat_nodes if "begin_unit_test_marker" in name] or [0]
)
return textwrap.indent(
"\n".join(
[
f"{' ' * (d - min_depth)}{name.rstrip()}"
for d, name in flat_nodes
if d >= min_depth
]
),
" " * indent,
)
@staticmethod
def fmt_name(name: str) -> str:
match = re.match(r"^(.*)\.py\(([0-9]+)\): (.*)$", name)
if match:
filename, _, fn = match.groups()
# This test can appear as `test/profiler/test_profiler_tree.py`
# depending on where it is run from.
test_file = os.path.splitext(os.path.split(__file__)[1])[0]
if filename.endswith(test_file):
filename = test_file
# We test against a string literal, so all paths have to look like POSIX paths.
filename = filename.replace(os.sep, "/")
# We don't want to have to update this test every time PyTorch changes.
# At some point we should test some line numbers, but for now it's
# too brittle.
lineno = "..."
return f"{filename}.py({lineno}): {fn}"
for kernel_pattern in (
"void at::native::elementwise_kernel",
"void at::native::reduce_kernel",
"void at::native::vectorized_elementwise_kernel",
"void at::native::unrolled_elementwise_kernel",
r"void [a-zA-Z0-9]+_kernel", # Nvidia kernels.
):
name = re.sub(
rf"{kernel_pattern}<.+>\(.+\)$",
f"{kernel_pattern.replace('[a-zA-Z0-9]+', '...')}<...>(...)",
name,
)
return re.sub("object at 0x[0-9a-fA-F]+>", "object at 0xXXXXXXXXXXXX>", name)
@classmethod
def validate_node(cls, node):
extra_fields = node.extra_fields
if isinstance(extra_fields, (_ExtraFields_PyCall, _ExtraFields_PyCCall)):
# Check that the lineage established by the profiler matches the
# caller recorded by the Python tracer.
parent = node.parent
while parent is not None:
if isinstance(parent.extra_fields, _ExtraFields_PyCall):
break
parent = parent.parent
def to_string(frame_state):
return f"{frame_state.file_name}(...): {frame_state.function_name}"
if parent:
parent_name = to_string(parent.extra_fields.callsite)
caller_name = to_string(extra_fields.caller)
assert parent_name == caller_name, f"{parent_name} vs. {caller_name}"
@unittest.skipIf(IS_ARM64, "Not working on ARM")
class TestProfilerTree(TestCase):
def assertTreesMatch(self, actual: str, expected: str, allow_failure: bool = False):
# Warning: Here be dragons
# Different platforms will have subtly different behavior for Python
# tracing. Observed differences include:
# 1) Windows symbolicates names differently from posix
# 2) The profile callback for c_call does not fire for Tensor.__pow__
# on certain platforms. This is not caused by the function tracer,
# but by cPython itself.
#
# The purpose of these unit tests is to ensure that the profiler is
# doing reasonable things. When these platform dependent variations occur
# simply coerce them into a platform independent form. If you made a
# change in the codebase which changes the trace produced, simply use
# EXPECTTEST_ACCEPT=1 to update the tests to reflect the new structure.
# expecttest will not show the diff view if `len(actual) < len(expected)`
if not expecttest.ACCEPT:
actual = actual.ljust(len(expected))
self.maxDiff = None
replicate = getattr(self, "tree_replicate", None)
self.assertIsNotNone(
replicate, "Please annotate test with `@ProfilerTree.test`"
)
# The profiler should produce deterministic results and should return
# to a clean state after each run. As a result, only the first
# replicate is allowed to update `expected`. If subsequent runs do not
# match it is a bug in the profiler.
if replicate:
self.assertEqual(actual, expected)
else:
try:
self.assertExpectedInline(actual, expected, skip=1)
except AssertionError as e:
if allow_failure:
self.tree_replicate = None
msg = traceback.format_exception_only(type(e), e)[0]
print(msg.split("AssertionError:")[-1])
else:
raise
# TODO: Add logic for CUDA version of test
@ProfilerTree.test
@unittest.skipIf(torch.cuda.is_available(), "Test not working for CUDA")
def test_profiler_experimental_tree(self):
t1, t2 = torch.ones(1, requires_grad=True), torch.ones(1, requires_grad=True)
with torch.profiler.profile() as p:
z = torch.add(t1, t2)
y = torch.ones(1)
loss = (y - z) ** 2
loss.backward()
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
aten::add
aten::ones
aten::empty
aten::fill_
aten::sub
aten::pow
aten::result_type
aten::to
aten::ones_like
aten::empty_like
aten::empty_strided
aten::fill_
autograd::engine::evaluate_function: PowBackward0
PowBackward0
aten::pow
aten::result_type
aten::to
aten::copy_
aten::mul
aten::mul
aten::to
aten::_to_copy
aten::empty_strided
aten::copy_
aten::mul
autograd::engine::evaluate_function: SubBackward0
SubBackward0
aten::neg
autograd::engine::evaluate_function: AddBackward0
AddBackward0
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::new_empty_strided
aten::empty_strided
aten::copy_
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::detach
detach""",
)
# TODO: Add logic for CUDA version of test
@ProfilerTree.test
@unittest.skipIf(torch.cuda.is_available(), "Test not working for CUDA")
def test_profiler_experimental_tree_with_record_function(self):
with torch.profiler.profile() as p:
with torch.autograd.profiler.record_function("Top level Annotation"):
with torch.autograd.profiler.record_function("First Annotation"):
x = torch.ones((1,), requires_grad=True)
# Check that we correctly handle the case when a user
# annotation does not call `__exit__`.
_ = torch.autograd.profiler.record_function(
"Second Annotation"
).__enter__()
y = x + 1
with torch.autograd.profiler.record_function("Third Annotation"):
y.backward()
# NB: The `aten::zeros` before the record function annotations are due to
# `at::cpp_custom_type_hack`. When we switch to `torch::CustomClassHolder`
# they will disappear.
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
Top level Annotation
First Annotation
aten::ones
aten::empty
aten::fill_
Second Annotation
aten::add
aten::to
aten::_to_copy
aten::empty_strided
aten::copy_
Third Annotation
aten::ones_like
aten::empty_like
aten::empty_strided
aten::fill_
autograd::engine::evaluate_function: AddBackward0
AddBackward0
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::new_empty_strided
aten::empty_strided
aten::copy_""",
)
# TODO: Add logic for CUDA version of test
@ProfilerTree.test
@unittest.skipIf(torch.cuda.is_available(), "Test not working for CUDA")
def test_profiler_experimental_tree_with_memory(self):
t1, t2 = torch.ones(1, requires_grad=True), torch.ones(1, requires_grad=True)
with torch.profiler.profile(profile_memory=True) as p:
z = torch.add(t1, t2)
y = torch.ones(1)
loss = (y - z) ** 2
loss.backward()
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
aten::add
[memory]
aten::ones
aten::empty
[memory]
aten::fill_
aten::sub
[memory]
aten::pow
aten::result_type
aten::to
[memory]
aten::ones_like
aten::empty_like
aten::empty_strided
[memory]
aten::fill_
autograd::engine::evaluate_function: PowBackward0
PowBackward0
aten::pow
aten::result_type
aten::to
[memory]
aten::copy_
aten::mul
[memory]
aten::mul
aten::to
aten::_to_copy
aten::empty_strided
[memory]
aten::copy_
[memory]
[memory]
[memory]
aten::mul
[memory]
[memory]
[memory]
[memory]
autograd::engine::evaluate_function: SubBackward0
SubBackward0
aten::neg
[memory]
[memory]
autograd::engine::evaluate_function: AddBackward0
AddBackward0
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::new_empty_strided
aten::empty_strided
[memory]
aten::copy_
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::detach
detach
[memory]""",
)
@unittest.skipIf(
TEST_WITH_CROSSREF, "crossref intercepts calls and changes the callsite."
)
@ProfilerTree.test
def test_profiler_experimental_tree_with_memory_and_stack(self):
t1, t2 = torch.ones(1, requires_grad=True), torch.ones(1, requires_grad=True)
with torch.profiler.profile(with_stack=True, profile_memory=True) as p:
z = torch.add(t1, t2)
y = torch.ones(1)
loss = torch.pow(y - z, 2)
loss.backward()
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
test_profiler_tree.py(...): test_profiler_experimental_tree_with_memory_and_stack
torch/profiler/profiler.py(...): __enter__
...
<built-in method add of type object at 0xXXXXXXXXXXXX>
aten::add
[memory]
<built-in method ones of type object at 0xXXXXXXXXXXXX>
aten::ones
aten::empty
[memory]
aten::fill_
aten::sub
[memory]
<built-in method pow of type object at 0xXXXXXXXXXXXX>
aten::pow
aten::result_type
aten::to
[memory]
torch/_tensor.py(...): backward
<built-in function _has_torch_function_unary>
torch/autograd/__init__.py(...): backward
<built-in method _are_functorch_transforms_active of PyCapsule object at 0xXXXXXXXXXXXX>
<built-in function isinstance>
<built-in function isinstance>
<built-in function len>
torch/autograd/__init__.py(...): _tensor_or_tensors_to_tuple
torch/autograd/__init__.py(...): _make_grads
<built-in function isinstance>
<built-in method numel of Tensor object at 0xXXXXXXXXXXXX>
<built-in method ones_like of type object at 0xXXXXXXXXXXXX>
aten::ones_like
aten::empty_like
aten::empty_strided
[memory]
aten::fill_
<built-in method append of list object at 0xXXXXXXXXXXXX>
torch/autograd/graph.py(...): _engine_run_backward
logging/__init__.py(...): getEffectiveLevel
<built-in method run_backward of torch._C._EngineBase object at 0xXXXXXXXXXXXX>
autograd::engine::evaluate_function: PowBackward0
PowBackward0
aten::pow
aten::result_type
aten::to
[memory]
aten::copy_
aten::mul
[memory]
aten::mul
aten::to
aten::_to_copy
aten::empty_strided
[memory]
aten::copy_
[memory]
[memory]
[memory]
aten::mul
[memory]
[memory]
[memory]
[memory]
autograd::engine::evaluate_function: SubBackward0
SubBackward0
aten::neg
[memory]
[memory]
autograd::engine::evaluate_function: AddBackward0
AddBackward0
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::new_empty_strided
aten::empty_strided
[memory]
aten::copy_
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::detach
detach
[memory]
torch/profiler/profiler.py(...): __exit__
torch/profiler/profiler.py(...): stop
...""",
)
@skipIfTorchDynamo("too slow")
@unittest.skipIf(
TEST_WITH_CROSSREF, "crossref intercepts calls and changes the callsite."
)
@ProfilerTree.test
def test_profiler_experimental_tree_with_stack_and_modules(self):
class MyModule(torch.nn.Module):
def __init__(self):
super().__init__()
self.layers = [
torch.nn.ReLU(),
torch.nn.Linear(1, 1),
torch.nn.ReLU(),
]
def forward(self, x: torch.Tensor) -> torch.Tensor:
for l in self.layers:
x = l(x)
return x
model = MyModule()
with torch.profiler.profile(with_stack=True) as p:
for _ in range(2):
model(torch.ones((1,)))
self.maxDiff = None
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
test_profiler_tree.py(...): test_profiler_experimental_tree_with_stack_and_modules
torch/profiler/profiler.py(...): __enter__
...
<built-in method ones of type object at 0xXXXXXXXXXXXX>
aten::ones
aten::empty
aten::fill_
nn.Module: MyModule_0
torch/nn/modules/module.py(...): _call_impl
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
test_profiler_tree.py(...): forward
nn.Module: ReLU_0
torch/nn/modules/module.py(...): _call_impl
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
torch/nn/modules/activation.py(...): forward
torch/nn/functional.py(...): relu
<built-in function _has_torch_function_unary>
<built-in method relu of type object at 0xXXXXXXXXXXXX>
aten::relu
aten::clamp_min
nn.Module: Linear_0
torch/nn/modules/module.py(...): _call_impl
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
torch/nn/modules/linear.py(...): forward
torch/nn/modules/module.py(...): __getattr__
torch/nn/modules/module.py(...): __getattr__
<built-in function linear>
aten::linear
aten::reshape
aten::view
aten::t
aten::transpose
aten::as_strided
aten::addmm
aten::expand
aten::as_strided
aten::copy_
aten::resolve_conj
aten::resolve_conj
aten::resolve_conj
aten::view
nn.Module: ReLU_1
torch/nn/modules/module.py(...): _call_impl
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
torch/nn/modules/activation.py(...): forward
torch/nn/functional.py(...): relu
<built-in function _has_torch_function_unary>
<built-in method relu of type object at 0xXXXXXXXXXXXX>
aten::relu
aten::clamp_min
<built-in method ones of type object at 0xXXXXXXXXXXXX>
aten::ones
aten::empty
aten::fill_
nn.Module: MyModule_0
torch/nn/modules/module.py(...): _call_impl
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
test_profiler_tree.py(...): forward
nn.Module: ReLU_0
torch/nn/modules/module.py(...): _call_impl
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
torch/nn/modules/activation.py(...): forward
torch/nn/functional.py(...): relu
<built-in function _has_torch_function_unary>
<built-in method relu of type object at 0xXXXXXXXXXXXX>
aten::relu
aten::clamp_min
nn.Module: Linear_0
torch/nn/modules/module.py(...): _call_impl
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
torch/nn/modules/linear.py(...): forward
torch/nn/modules/module.py(...): __getattr__
torch/nn/modules/module.py(...): __getattr__
<built-in function linear>
aten::linear
aten::reshape
aten::view
aten::t
aten::transpose
aten::as_strided
aten::addmm
aten::expand
aten::as_strided
aten::copy_
aten::resolve_conj
aten::resolve_conj
aten::resolve_conj
aten::view
nn.Module: ReLU_1
torch/nn/modules/module.py(...): _call_impl
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
torch/nn/modules/activation.py(...): forward
torch/nn/functional.py(...): relu
<built-in function _has_torch_function_unary>
<built-in method relu of type object at 0xXXXXXXXXXXXX>
aten::relu
aten::clamp_min
torch/profiler/profiler.py(...): __exit__
torch/profiler/profiler.py(...): stop
...""",
)
@unittest.skipIf(
TEST_WITH_CROSSREF, "crossref intercepts calls and changes the callsite."
)
@ProfilerTree.test
def test_profiler_experimental_tree_with_stack_and_torch_function(self):
x = TorchFunctionTensor(torch.ones((1,)))
y = torch.ones((1,))
# There's some lazy initialization in __torch_function__. If we don't
# run this the first run won't match the replicates.
torch.add(x, y)
with torch.profiler.profile(with_stack=True) as p:
torch.add(x, y)
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
test_profiler_tree.py(...): test_profiler_experimental_tree_with_stack_and_torch_function
torch/profiler/profiler.py(...): __enter__
...
<built-in method add of type object at 0xXXXXXXXXXXXX>
test_profiler_tree.py(...): __torch_function__
torch/_tensor.py(...): __torch_function__
<built-in function all>
torch/_tensor.py(...): <genexpr>
<built-in function issubclass>
torch/_tensor.py(...): <genexpr>
<built-in method add of type object at 0xXXXXXXXXXXXX>
aten::add
torch/_tensor.py(...): _convert
<built-in function isinstance>
<built-in function isinstance>
<built-in method as_subclass of Tensor object at 0xXXXXXXXXXXXX>
aten::alias
<built-in function isinstance>
torch/profiler/profiler.py(...): __exit__
torch/profiler/profiler.py(...): stop
...""",
)
@unittest.skipIf(
TEST_WITH_CROSSREF, "crossref intercepts calls and changes the callsite."
)
@ProfilerTree.test
def test_profiler_experimental_tree_with_stack_and_torch_dispatch(self):
x = TorchDispatchTensor(torch.ones((1,)))
y = torch.ones((1,))
with torch.profiler.profile(with_stack=True) as p:
x + y
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
test_profiler_tree.py(...): test_profiler_experimental_tree_with_stack_and_torch_dispatch
torch/profiler/profiler.py(...): __enter__
...
aten::add
test_profiler_tree.py(...): __torch_dispatch__
torch/utils/_pytree.py(...): tree_map
...
torch/utils/_pytree.py(...): tree_map
...
torch/_ops.py(...): __call__
<built-in method of PyCapsule object at 0xXXXXXXXXXXXX>
aten::add
torch/utils/_pytree.py(...): tree_map
...
torch/profiler/profiler.py(...): __exit__
torch/profiler/profiler.py(...): stop
...""",
)
@unittest.skip("https://github.com/pytorch/pytorch/issues/83606")
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is required")
@ProfilerTree.test
def test_profiler_experimental_tree_cuda(self):
with torch.profiler.profile(profile_memory=True) as p:
weight = torch.ones(1, device="cuda", requires_grad=True)
x = torch.ones(1, device="cuda")
y = torch.add(weight, x)
loss = torch.pow(y, 2)
loss.backward()
torch.optim.SGD([weight], lr=0.01, momentum=0.9).step()
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
aten::ones
aten::empty
[memory]
aten::fill_
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
aten::ones
aten::empty
[memory]
aten::fill_
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
aten::add
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
aten::pow
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
aten::result_type
aten::to
[memory]
aten::ones_like
aten::empty_like
aten::empty_strided
[memory]
aten::fill_
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
autograd::engine::evaluate_function: PowBackward0
PowBackward0
aten::pow
aten::result_type
aten::to
[memory]
aten::copy_
cudaMemcpyAsync
Memcpy DtoD (Device -> Device)
aten::mul
[memory]
aten::mul
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
[memory]
aten::mul
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
[memory]
[memory]
autograd::engine::evaluate_function: AddBackward0
AddBackward0
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::detach
detach
[memory]
aten::zeros
aten::zeros
aten::empty
[memory]
aten::zero_
Optimizer.step#SGD.step
aten::empty
[memory]
[memory]
[memory]
aten::clone
aten::empty_strided
[memory]
aten::copy_
cudaMemcpyAsync
Memcpy DtoD (Device -> Device)
aten::detach
detach
aten::add_
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]""", # noqa: B950
allow_failure=ALLOW_CUDA_FAILURE,
)
@unittest.skip("https://github.com/pytorch/pytorch/issues/83606")
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is required")
@ProfilerTree.test
def test_profiler_experimental_tree_cuda_with_stream(self):
streams = [torch.cuda.Stream() for _ in range(3)]
results = []
with torch.profiler.profile(profile_memory=True) as p:
x = torch.ones((4, 4), device="cuda")
for stream in streams:
with torch.cuda.stream(stream):
results.append(torch.tanh(x) - x)
del results
for s in streams:
torch.cuda.current_stream().wait_stream(s)
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
aten::ones
aten::empty
[memory]
aten::fill_
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
aten::tanh
cudaMalloc
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
aten::sub
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
[memory]
aten::tanh
cudaMalloc
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
aten::sub
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
[memory]
aten::tanh
cudaMalloc
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
aten::sub
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
[memory]""",
allow_failure=ALLOW_CUDA_FAILURE,
)
@unittest.skip("https://github.com/pytorch/pytorch/issues/83606")
@unittest.skipIf(
TEST_WITH_CROSSREF, "crossref intercepts calls and changes the callsite."
)
@unittest.skipIf(not torch.cuda.is_available(), "CUDA is required")
@ProfilerTree.test
def test_profiler_experimental_tree_cuda_detailed(self):
model = torch.nn.modules.Linear(1, 1, device="cuda")
model.train()
opt = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
def step():
x = torch.ones((1, 1), device="cuda")
loss = model(x)
loss.backward()
opt.step()
# Warmup
for _ in range(3):
step()
with torch.profiler.profile(profile_memory=True, with_stack=True) as p:
step()
self.assertTreesMatch(
ProfilerTree.format(p.profiler, 12),
"""\
test_profiler_tree.py(...): test_profiler_experimental_tree_cuda_detailed
torch/profiler/profiler.py(...): __enter__
...
test_profiler_tree.py(...): step
<built-in method ones of type object at 0xXXXXXXXXXXXX>
aten::ones
aten::empty
[memory]
aten::fill_
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
nn.Module: Linear_0
<built-in method _get_tracing_state of PyCapsule object at 0xXXXXXXXXXXXX>
torch/nn/modules/linear.py(...): forward
torch/nn/modules/module.py(...): __getattr__
torch/nn/modules/module.py(...): __getattr__
<built-in function linear>
aten::linear
aten::t
aten::transpose
aten::as_strided
aten::addmm
cudaMemcpyAsync
Memcpy DtoD (Device -> Device)
cudaLaunchKernel
void ..._kernel<...>(...)
[memory]
aten::expand
aten::as_strided
torch/_tensor.py(...): backward
<built-in function _has_torch_function_unary>
torch/autograd/__init__.py(...): backward
<built-in function isinstance>
<built-in function isinstance>
<built-in function len>
torch/autograd/__init__.py(...): _tensor_or_tensors_to_tuple
torch/autograd/__init__.py(...): _make_grads
<built-in function isinstance>
<built-in method numel of Tensor object at 0xXXXXXXXXXXXX>
<built-in method ones_like of type object at 0xXXXXXXXXXXXX>
aten::ones_like
aten::empty_like
aten::empty_strided
[memory]
aten::fill_
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
<built-in method append of list object at 0xXXXXXXXXXXXX>
<built-in method run_backward of torch._C._EngineBase object at 0xXXXXXXXXXXXX>
autograd::engine::evaluate_function: AddmmBackward0
AddmmBackward0
aten::t
aten::transpose
aten::as_strided
aten::mm
cudaLaunchKernel
void ..._kernel<...>(...)
[memory]
aten::t
aten::transpose
aten::as_strided
aten::sum
aten::sum
cudaLaunchKernel
void at::native::reduce_kernel<...>(...)
[memory]
aten::view
aten::view
autograd::engine::evaluate_function: torch::autograd::AccumulateGrad
torch::autograd::AccumulateGrad
aten::add_
cudaLaunchKernel
void at::native::vectorized_elementwise_kernel<...>(...)
[memory]
autograd::engine::evaluate_function: TBackward0
TBackward0