Skip to content

Commit 748bac8

Browse files
Skylion007pytorchmergebot
authored andcommitted
[BE]: Apply pyupgrade yield from and unit test alias upgrades (pytorch#94309)
Applies some more harmless pyupgrades. This one gets rid of deprecated aliases in unit_tests and more upgrades yield for loops into yield from generators which are more performance and propagates more information / exceptions from original generator. This is the modern recommended way of forwarding generators. Pull Request resolved: pytorch#94309 Approved by: https://github.com/albanD
1 parent 895d478 commit 748bac8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+308
-329
lines changed

caffe2/python/checkpoint_test.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,20 @@ def fetch_total(session):
7878
session, checkpoint = builder()
7979
job.compile(LocalSession)
8080
num_epochs = JobRunner(job, checkpoint).train(session)
81-
self.assertEquals(num_epochs, len(EXPECTED_TOTALS))
82-
self.assertEquals(fetch_total(session), EXPECTED_TOTALS[-1])
81+
self.assertEqual(num_epochs, len(EXPECTED_TOTALS))
82+
self.assertEqual(fetch_total(session), EXPECTED_TOTALS[-1])
8383

8484
for initial_epoch in range(1, num_epochs + 1):
8585
session, checkpoint = builder()
8686
JobRunner(
8787
job,
8888
checkpoint, resume_from_epoch=initial_epoch
8989
).train(session)
90-
self.assertEquals(fetch_total(session), EXPECTED_TOTALS[-1])
90+
self.assertEqual(fetch_total(session), EXPECTED_TOTALS[-1])
9191

9292
for epoch in range(1, num_epochs + 1):
9393
session.run(checkpoint.load(epoch))
94-
self.assertEquals(fetch_total(session),
94+
self.assertEqual(fetch_total(session),
9595
EXPECTED_TOTALS[epoch - 1])
9696

9797
def test_single_checkpoint(self):
@@ -141,7 +141,7 @@ def test_ckpt_name_and_load_model_from_ckpts(self):
141141
epoch = 5
142142
node_name = 'trainer_%d' % node_id
143143
expected_db_name = tmpdir + '/' + node_name + '.5'
144-
self.assertEquals(
144+
self.assertEqual(
145145
checkpoint.get_ckpt_db_name(node_name, epoch),
146146
expected_db_name)
147147
shutil.rmtree(tmpdir)
@@ -159,15 +159,15 @@ def test_ckpt_name_and_load_model_from_ckpts(self):
159159
job.compile(LocalSession)
160160
job_runner = JobRunner(job, checkpoint)
161161
num_epochs = job_runner.train(session)
162-
self.assertEquals(num_epochs, len(EXPECTED_TOTALS))
162+
self.assertEqual(num_epochs, len(EXPECTED_TOTALS))
163163

164164
# There are 17 global blobs after finishing up the job runner.
165165
# (only blobs on init_group are checkpointed)
166-
self.assertEquals(len(ws.blobs), 17)
166+
self.assertEqual(len(ws.blobs), 17)
167167

168168
ws = workspace.C.Workspace()
169169
session = LocalSession(ws)
170-
self.assertEquals(len(ws.blobs), 0)
170+
self.assertEqual(len(ws.blobs), 0)
171171
model_blob_names = ['trainer_1/task_2/GivenTensorInt64Fill:0',
172172
'trainer_2/task_2/GivenTensorInt64Fill:0']
173173
checkpoint = MultiNodeCheckpointManager(tmpdir, 'minidb')
@@ -190,7 +190,7 @@ def test_ckpt_name_and_load_model_from_ckpts(self):
190190
# Check that all the model blobs are loaded.
191191
for blob_name in model_blob_names:
192192
self.assertTrue(ws.has_blob(blob_name))
193-
self.assertEquals(
193+
self.assertEqual(
194194
ws.fetch_blob(blob_name),
195195
np.array([EXPECTED_TOTALS[epoch - 1]]))
196196
self.assertFalse(
@@ -227,7 +227,7 @@ def test_upload_checkpoint(self):
227227
job, checkpoint,
228228
upload_task_group_builder=local_upload_builder)
229229
num_epochs = job_runner.train(session)
230-
self.assertEquals(num_epochs, len(EXPECTED_TOTALS))
230+
self.assertEqual(num_epochs, len(EXPECTED_TOTALS))
231231

232232
# The uploaded files should exist now.
233233
for node_id in range(num_nodes):
@@ -260,7 +260,7 @@ def test_ckpt_save_failure(self):
260260
num_epochs = job_runner.train(session)
261261
# make sure all epochs are executed even though saving the checkpoint failed
262262
# Saving checkpoint failure should not cause job failure
263-
self.assertEquals(num_epochs, len(EXPECTED_TOTALS))
263+
self.assertEqual(num_epochs, len(EXPECTED_TOTALS))
264264

265265
def test_download_group_simple(self):
266266
"""
@@ -332,7 +332,7 @@ def fetch_total(session):
332332
checkpoint,
333333
resume_from_epoch=initial_epoch
334334
).train(session)
335-
self.assertEquals(fetch_total(session), EXPECTED_TOTALS[-1])
335+
self.assertEqual(fetch_total(session), EXPECTED_TOTALS[-1])
336336

337337
finally:
338338
shutil.rmtree(tmpdir)

caffe2/python/core_test.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,13 @@ def test_extract_simple(self):
459459
self.assertFalse("xx/data" in op.input)
460460

461461
# Note: image input should not be included
462-
self.assertEquals(ops[0].type, "Conv")
463-
self.assertEquals(ops[1].type, "FC")
464-
self.assertEquals(ops[2].type, "FC")
465-
self.assertEquals(len(ops), 3)
462+
self.assertEqual(ops[0].type, "Conv")
463+
self.assertEqual(ops[1].type, "FC")
464+
self.assertEqual(ops[2].type, "FC")
465+
self.assertEqual(len(ops), 3)
466466

467467
# test rename happened
468-
self.assertEquals(ops[0].input[0], "image")
468+
self.assertEqual(ops[0].input[0], "image")
469469

470470
# Check export blobs
471471
self.assertTrue("image" not in export_blobs)
@@ -474,7 +474,7 @@ def test_extract_simple(self):
474474

475475
# Check external inputs/outputs
476476
self.assertTrue("image" in predict_net.Proto().external_input)
477-
self.assertEquals(set(["pred"]), set(predict_net.Proto().external_output))
477+
self.assertEqual(set(["pred"]), set(predict_net.Proto().external_output))
478478
self.assertEqual(
479479
set(predict_net.Proto().external_input) -
480480
set([str(p) for p in model.params]), set(["image"])

caffe2/python/layer_parameter_sharing_test.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@ def test_layer_parameter_name(self):
2020
self.model.input_feature_schema.float_features,
2121
output_dims
2222
)
23-
self.assertEquals(self.model.layers[-1].w, 'global_scope/fc/w')
24-
self.assertEquals(fc1_output(), 'global_scope/fc/output')
23+
self.assertEqual(self.model.layers[-1].w, 'global_scope/fc/w')
24+
self.assertEqual(fc1_output(), 'global_scope/fc/output')
2525

2626
with scope.NameScope('nested_scope'):
2727
fc2_output = self.model.FC(
2828
fc1_output,
2929
output_dims
3030
)
31-
self.assertEquals(self.model.layers[-1].w,
31+
self.assertEqual(self.model.layers[-1].w,
3232
'global_scope/nested_scope/fc/w')
33-
self.assertEquals(fc2_output(),
33+
self.assertEqual(fc2_output(),
3434
'global_scope/nested_scope/fc/output')
3535

3636
fc3_output = self.model.FC(
3737
fc1_output,
3838
output_dims
3939
)
40-
self.assertEquals(self.model.layers[-1].w,
40+
self.assertEqual(self.model.layers[-1].w,
4141
'global_scope/nested_scope/fc_auto_0/w')
42-
self.assertEquals(fc3_output(),
42+
self.assertEqual(fc3_output(),
4343
'global_scope/nested_scope/fc_auto_0/output')
4444

4545
def test_layer_shared_parameter_name_different_namescopes(self):
@@ -51,19 +51,19 @@ def test_layer_shared_parameter_name_different_namescopes(self):
5151
self.model.input_feature_schema.float_features,
5252
output_dims
5353
)
54-
self.assertEquals(self.model.layers[-1].w,
54+
self.assertEqual(self.model.layers[-1].w,
5555
'global_scope/scope_0/fc/w')
56-
self.assertEquals(fc1_output(),
56+
self.assertEqual(fc1_output(),
5757
'global_scope/scope_0/fc/output')
5858

5959
with scope.NameScope('scope_1'):
6060
fc2_output = self.model.FC(
6161
self.model.input_feature_schema.float_features,
6262
output_dims
6363
)
64-
self.assertEquals(self.model.layers[-1].w,
64+
self.assertEqual(self.model.layers[-1].w,
6565
'global_scope/scope_0/fc/w')
66-
self.assertEquals(fc2_output(),
66+
self.assertEqual(fc2_output(),
6767
'global_scope/scope_1/fc/output')
6868

6969
def test_layer_shared_parameter_name_within_same_namescope(self):
@@ -74,14 +74,14 @@ def test_layer_shared_parameter_name_within_same_namescope(self):
7474
self.model.input_feature_schema.float_features,
7575
output_dims
7676
)
77-
self.assertEquals(self.model.layers[-1].w,
77+
self.assertEqual(self.model.layers[-1].w,
7878
'global_scope/fc/w')
7979

8080
self.model.FC(
8181
self.model.input_feature_schema.float_features,
8282
output_dims
8383
)
84-
self.assertEquals(self.model.layers[-1].w,
84+
self.assertEqual(self.model.layers[-1].w,
8585
'global_scope/fc/w')
8686

8787
def test_layer_shared_parameter_name_within_same_namescope_customized_name(self):
@@ -93,15 +93,15 @@ def test_layer_shared_parameter_name_within_same_namescope_customized_name(self)
9393
output_dims,
9494
name='shared_fc'
9595
)
96-
self.assertEquals(self.model.layers[-1].w,
96+
self.assertEqual(self.model.layers[-1].w,
9797
'global_scope/shared_fc/w')
9898

9999
self.model.FC(
100100
self.model.input_feature_schema.float_features,
101101
output_dims,
102102
name='new_fc'
103103
)
104-
self.assertEquals(self.model.layers[-1].w,
104+
self.assertEqual(self.model.layers[-1].w,
105105
'global_scope/shared_fc/w')
106106

107107
def test_layer_shared_parameter_name_different_shapes(self):
@@ -112,7 +112,7 @@ def test_layer_shared_parameter_name_different_shapes(self):
112112
self.model.input_feature_schema.float_features,
113113
output_dims
114114
)
115-
self.assertEquals(self.model.layers[-1].w,
115+
self.assertEqual(self.model.layers[-1].w,
116116
'global_scope/fc/w')
117117

118118
with self.assertRaisesRegex(ValueError, 'Got inconsistent shapes .*'):
@@ -145,7 +145,7 @@ def test_layer_duplicated_parameter_init(self):
145145
op_outputs.extend(op.output)
146146

147147
# only fill these parameter blobs once
148-
self.assertEquals(
148+
self.assertEqual(
149149
sorted(op_outputs),
150150
['global_scope/shared_fc/b', 'global_scope/shared_fc/w']
151151
)

caffe2/python/layers_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def testSparseLookupSumPoolingWithEviction(self):
424424
workspace.RunNetOnce(train_net.Proto())
425425
embedding_after_training = workspace.FetchBlob("sparse_lookup/w")
426426
# Verify row 0's value does not change after reset
427-
self.assertEquals(embedding_after_training.all(), embedding_after_init.all())
427+
self.assertEqual(embedding_after_training.all(), embedding_after_init.all())
428428

429429

430430
def testSparseLookupSumPooling(self):

caffe2/python/memonger_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ def test_memonger_mix_cpu_gpu(self):
263263
device_crossers = device_blobs[caffe2_pb2.CPU].intersection(
264264
device_blobs[workspace.GpuDeviceType]
265265
)
266-
self.assertEquals(device_crossers, set())
266+
self.assertEqual(device_crossers, set())
267267

268268
@given(input_dim=st.integers(min_value=4, max_value=4),
269269
output_dim=st.integers(min_value=4, max_value=4),

caffe2/python/modeling/parameter_sharing_test.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,56 @@ class ParameterSharingTest(unittest.TestCase):
1919
def test_parameter_sharing_default_scopes(self):
2020
# Test no sharing default scopes
2121
param_1 = parameter_sharing_context.get_parameter_name('w')
22-
self.assertEquals(param_1, 'w')
22+
self.assertEqual(param_1, 'w')
2323
with scope.NameScope('scope'):
2424
param_2 = parameter_sharing_context.get_parameter_name('w')
25-
self.assertEquals(param_2, 'scope/w')
25+
self.assertEqual(param_2, 'scope/w')
2626
with scope.NameScope('scope_2'):
2727
param_3 = parameter_sharing_context.get_parameter_name('w')
28-
self.assertEquals(param_3, 'scope/scope_2/w')
28+
self.assertEqual(param_3, 'scope/scope_2/w')
2929

3030
def test_parameter_sharing_nested_scopes(self):
3131
# Test parameter sharing
3232
with scope.NameScope('global_scope'):
3333
with ParameterSharing({'model_b': 'model_a'}):
3434
param_global = parameter_sharing_context.get_parameter_name('w')
35-
self.assertEquals(param_global, 'global_scope/w')
35+
self.assertEqual(param_global, 'global_scope/w')
3636
# This scope is overridden to match 'model_a'
3737
with scope.NameScope('model_b'):
3838
with ParameterSharing({'shared_scope': ''}):
3939
param_4 = parameter_sharing_context.get_parameter_name(
4040
'w')
41-
self.assertEquals(param_4, 'global_scope/model_a/w')
41+
self.assertEqual(param_4, 'global_scope/model_a/w')
4242
with scope.NameScope('shared_scope'):
4343
param_5 = parameter_sharing_context.\
4444
get_parameter_name('w')
45-
self.assertEquals(param_5, 'global_scope/model_a/w')
45+
self.assertEqual(param_5, 'global_scope/model_a/w')
4646
# This scope is supposed to have not sharing
4747
with scope.NameScope('model_c'):
4848
with ParameterSharing({'shared_scope': ''}):
4949
param_4 = parameter_sharing_context.get_parameter_name(
5050
'w')
51-
self.assertEquals(param_4, 'global_scope/model_c/w')
51+
self.assertEqual(param_4, 'global_scope/model_c/w')
5252
with scope.NameScope('shared_scope'):
5353
param_5 = parameter_sharing_context.\
5454
get_parameter_name('w')
55-
self.assertEquals(param_5, 'global_scope/model_c/w')
55+
self.assertEqual(param_5, 'global_scope/model_c/w')
5656

5757
def test_parameter_sharing_subscopes(self):
5858
# Sharing only one of the subscopes
5959
with ParameterSharing({'global_scope/b': 'global_scope/a'}):
6060
with scope.NameScope('global_scope'):
6161
param_6 = parameter_sharing_context.get_parameter_name('w')
62-
self.assertEquals(param_6, 'global_scope/w')
62+
self.assertEqual(param_6, 'global_scope/w')
6363
with scope.NameScope('a'):
6464
param_7 = parameter_sharing_context.get_parameter_name('w')
65-
self.assertEquals(param_7, 'global_scope/a/w')
65+
self.assertEqual(param_7, 'global_scope/a/w')
6666
with scope.NameScope('b'):
6767
param_8 = parameter_sharing_context.get_parameter_name('w')
68-
self.assertEquals(param_8, 'global_scope/a/w')
68+
self.assertEqual(param_8, 'global_scope/a/w')
6969
with scope.NameScope('c'):
7070
param_9 = parameter_sharing_context.get_parameter_name('w')
71-
self.assertEquals(param_9, 'global_scope/c/w')
71+
self.assertEqual(param_9, 'global_scope/c/w')
7272

7373
def test_create_param(self):
7474
model = model_helper.ModelHelper(name="test")

caffe2/python/net_builder_test.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_ops(self):
101101
]
102102
for b, expected in expected:
103103
actual = ws.blobs[str(b)].fetch()
104-
self.assertEquals(actual, expected)
104+
self.assertEqual(actual, expected)
105105

106106
def _expected_loop(self):
107107
total = 0
@@ -152,7 +152,7 @@ def test_net_multi_use(self):
152152
result = final_output(total)
153153
with LocalSession() as session:
154154
session.run(task)
155-
self.assertEquals(2, result.fetch())
155+
self.assertEqual(2, result.fetch())
156156

157157
def test_loops(self):
158158
with Task() as task:
@@ -162,7 +162,7 @@ def test_loops(self):
162162
expected = self._expected_loop()
163163
actual = [o.fetch() for o in out_actual]
164164
for e, a in zip(expected, actual):
165-
self.assertEquals(e, a)
165+
self.assertEqual(e, a)
166166

167167
def test_setup(self):
168168
with Task() as task:
@@ -184,9 +184,9 @@ def test_setup(self):
184184
o7_2 = final_output(seven_2)
185185
with LocalSession() as session:
186186
session.run(task)
187-
self.assertEquals(o6.fetch(), 6)
188-
self.assertEquals(o7_1.fetch(), 7)
189-
self.assertEquals(o7_2.fetch(), 7)
187+
self.assertEqual(o6.fetch(), 6)
188+
self.assertEqual(o7_1.fetch(), 7)
189+
self.assertEqual(o7_2.fetch(), 7)
190190

191191
def test_multi_instance_python_op(self):
192192
"""
@@ -203,8 +203,8 @@ def test_multi_instance_python_op(self):
203203
PythonOpStats.num_instances = 0
204204
PythonOpStats.num_calls = 0
205205
session.run(task)
206-
self.assertEquals(PythonOpStats.num_instances, 64)
207-
self.assertEquals(PythonOpStats.num_calls, 256)
206+
self.assertEqual(PythonOpStats.num_instances, 64)
207+
self.assertEqual(PythonOpStats.num_calls, 256)
208208

209209
def test_multi_instance(self):
210210
NUM_INSTANCES = 10
@@ -242,9 +242,9 @@ def test_multi_instance(self):
242242

243243
with LocalSession() as session:
244244
session.run(tg)
245-
self.assertEquals(total1.fetch(), NUM_INSTANCES * NUM_ITERS)
246-
self.assertEquals(total2.fetch(), NUM_INSTANCES * (NUM_ITERS ** 2))
247-
self.assertEquals(total3.fetch(), NUM_INSTANCES * (NUM_ITERS ** 2))
245+
self.assertEqual(total1.fetch(), NUM_INSTANCES * NUM_ITERS)
246+
self.assertEqual(total2.fetch(), NUM_INSTANCES * (NUM_ITERS ** 2))
247+
self.assertEqual(total3.fetch(), NUM_INSTANCES * (NUM_ITERS ** 2))
248248

249249
def test_if_net(self):
250250
with NetBuilder() as nb:
@@ -303,11 +303,11 @@ def test_if_net(self):
303303
y1_value = ws.blobs[str(y1)].fetch()
304304
y2_value = ws.blobs[str(y2)].fetch()
305305

306-
self.assertEquals(first_res_value, 1)
307-
self.assertEquals(second_res_value, 2)
308-
self.assertEquals(y0_value, 1000)
309-
self.assertEquals(y1_value, 101)
310-
self.assertEquals(y2_value, 108)
306+
self.assertEqual(first_res_value, 1)
307+
self.assertEqual(second_res_value, 2)
308+
self.assertEqual(y0_value, 1000)
309+
self.assertEqual(y1_value, 101)
310+
self.assertEqual(y2_value, 108)
311311
self.assertTrue(str(local_blob) not in ws.blobs)
312312

313313
def test_while_net(self):

0 commit comments

Comments
 (0)