@@ -76,11 +76,14 @@ def make_model(in_size, out_size, num_layers):
76
76
num_batches = 50
77
77
epochs = 3
78
78
79
+ device = 'cuda' if torch .cuda .is_available () else 'cpu'
80
+ torch .set_default_device (device )
81
+
79
82
# Creates data in default precision.
80
83
# The same data is used for both default and mixed precision trials below.
81
84
# You don't need to manually change inputs' ``dtype`` when enabling mixed precision.
82
- data = [torch .randn (batch_size , in_size , device = "cuda" ) for _ in range (num_batches )]
83
- targets = [torch .randn (batch_size , out_size , device = "cuda" ) for _ in range (num_batches )]
85
+ data = [torch .randn (batch_size , in_size ) for _ in range (num_batches )]
86
+ targets = [torch .randn (batch_size , out_size ) for _ in range (num_batches )]
84
87
85
88
loss_fn = torch .nn .MSELoss ().cuda ()
86
89
@@ -116,7 +119,7 @@ def make_model(in_size, out_size, num_layers):
116
119
for epoch in range (0 ): # 0 epochs, this section is for illustration only
117
120
for input , target in zip (data , targets ):
118
121
# Runs the forward pass under ``autocast``.
119
- with torch .autocast (device_type = 'cuda' , dtype = torch .float16 ):
122
+ with torch .autocast (device_type = device , dtype = torch .float16 ):
120
123
output = net (input )
121
124
# output is float16 because linear layers ``autocast`` to float16.
122
125
assert output .dtype is torch .float16
@@ -151,7 +154,7 @@ def make_model(in_size, out_size, num_layers):
151
154
152
155
for epoch in range (0 ): # 0 epochs, this section is for illustration only
153
156
for input , target in zip (data , targets ):
154
- with torch .autocast (device_type = 'cuda' , dtype = torch .float16 ):
157
+ with torch .autocast (device_type = device , dtype = torch .float16 ):
155
158
output = net (input )
156
159
loss = loss_fn (output , target )
157
160
@@ -184,7 +187,7 @@ def make_model(in_size, out_size, num_layers):
184
187
start_timer ()
185
188
for epoch in range (epochs ):
186
189
for input , target in zip (data , targets ):
187
- with torch .autocast (device_type = 'cuda' , dtype = torch .float16 , enabled = use_amp ):
190
+ with torch .autocast (device_type = device , dtype = torch .float16 , enabled = use_amp ):
188
191
output = net (input )
189
192
loss = loss_fn (output , target )
190
193
scaler .scale (loss ).backward ()
@@ -202,7 +205,7 @@ def make_model(in_size, out_size, num_layers):
202
205
203
206
for epoch in range (0 ): # 0 epochs, this section is for illustration only
204
207
for input , target in zip (data , targets ):
205
- with torch .autocast (device_type = 'cuda' , dtype = torch .float16 ):
208
+ with torch .autocast (device_type = device , dtype = torch .float16 ):
206
209
output = net (input )
207
210
loss = loss_fn (output , target )
208
211
scaler .scale (loss ).backward ()
0 commit comments