57
57
# Transform it to the desired format and use ``DataLoader`` to load each batch.
58
58
59
59
transform = T .Compose (
60
- [T .Resize (224 ), T .ToTensor (), T .Normalize ((0.5 , 0.5 , 0.5 ), (0.5 , 0.5 , 0.5 ))]
61
- )
62
- train_set = torchvision .datasets .CIFAR10 (
63
- root = "./data" , train = True , download = True , transform = transform
64
- )
60
+ [T .Resize (224 ),
61
+ T .ToTensor (),
62
+ T .Normalize ((0.5 , 0.5 , 0.5 ), (0.5 , 0.5 , 0.5 ))])
63
+ train_set = torchvision .datasets .CIFAR10 (root = './data' , train = True , download = True , transform = transform )
65
64
train_loader = torch .utils .data .DataLoader (train_set , batch_size = 32 , shuffle = True )
66
65
67
66
######################################################################
68
67
# Next, create Resnet model, loss function, and optimizer objects.
69
68
# To run on GPU, move model and loss to GPU device.
70
69
71
70
device = torch .device ("cuda:0" )
72
- model = torchvision .models .resnet18 (weights = " IMAGENET1K_V1" ).cuda (device )
71
+ model = torchvision .models .resnet18 (weights = ' IMAGENET1K_V1' ).cuda (device )
73
72
criterion = torch .nn .CrossEntropyLoss ().cuda (device )
74
73
optimizer = torch .optim .SGD (model .parameters (), lr = 0.001 , momentum = 0.9 )
75
74
model .train ()
78
77
######################################################################
79
78
# Define the training step for each batch of input data.
80
79
81
-
82
80
def train (data ):
83
81
inputs , labels = data [0 ].to (device = device ), data [1 ].to (device = device )
84
82
outputs = model (inputs )
@@ -122,11 +120,11 @@ def train(data):
122
120
# clicking a stack frame will navigate to the specific code line.
123
121
124
122
with torch .profiler .profile (
125
- schedule = torch .profiler .schedule (wait = 1 , warmup = 1 , active = 3 , repeat = 1 ),
126
- on_trace_ready = torch .profiler .tensorboard_trace_handler (" ./log/resnet18" ),
127
- record_shapes = True ,
128
- profile_memory = True ,
129
- with_stack = True ,
123
+ schedule = torch .profiler .schedule (wait = 1 , warmup = 1 , active = 3 , repeat = 1 ),
124
+ on_trace_ready = torch .profiler .tensorboard_trace_handler (' ./log/resnet18' ),
125
+ record_shapes = True ,
126
+ profile_memory = True ,
127
+ with_stack = True
130
128
) as prof :
131
129
for step , batch_data in enumerate (train_loader ):
132
130
prof .step () # Need to call this at each step to notify profiler of steps' boundary.
@@ -137,11 +135,10 @@ def train(data):
137
135
######################################################################
138
136
# Alternatively, the following non-context manager start/stop is supported as well.
139
137
prof = torch .profiler .profile (
140
- schedule = torch .profiler .schedule (wait = 1 , warmup = 1 , active = 3 , repeat = 1 ),
141
- on_trace_ready = torch .profiler .tensorboard_trace_handler ("./log/resnet18" ),
142
- record_shapes = True ,
143
- with_stack = True ,
144
- )
138
+ schedule = torch .profiler .schedule (wait = 1 , warmup = 1 , active = 3 , repeat = 1 ),
139
+ on_trace_ready = torch .profiler .tensorboard_trace_handler ('./log/resnet18' ),
140
+ record_shapes = True ,
141
+ with_stack = True )
145
142
prof .start ()
146
143
for step , batch_data in enumerate (train_loader ):
147
144
prof .step ()
@@ -359,7 +356,7 @@ def train(data):
359
356
# ``aten::empty`` to allocate memory. For example, ``aten::ones`` is implemented as ``aten::empty`` followed by an
360
357
# ``aten::fill_``. Solely display the operator name as ``aten::empty`` is of little help. It will be shown as
361
358
# ``aten::ones (aten::empty)`` in this special case. The "Allocation Time", "Release Time" and "Duration"
362
- # columns' data might be missing if the event occurs outside of the time range.
359
+ # columns' data might be missing if the event occurs outside of the time range.
363
360
#
364
361
# In the memory statistics table, the "Size Increase" column sums up all allocation size and minus all the memory
365
362
# release size, that is, the net increase of memory usage after this operator. The "Self Size Increase" column is
0 commit comments