Skip to content

Commit 5b0f400

Browse files
Flamefirefacebook-github-bot
authored andcommitted
Replace list(map(...)) constructs by list comprehensions (#46461)
Summary: As discussed in #46392 this makes the code more readable and possibly more performant. It also fixes a bug detected by this where the argument order of `map` was confused: Flamefire@030a249#diff-5bb26bd3a23ee3bb540aeadcc0385df2a4e48de39f87ed9ea76b21990738fe98L1537-R1537 Fixes #46392 Pull Request resolved: #46461 Reviewed By: ailzhang Differential Revision: D24367015 Pulled By: ezyang fbshipit-source-id: d55a67933cc22346b00544c9671f09982ad920e7
1 parent 3d421b3 commit 5b0f400

25 files changed

+63
-63
lines changed

.circleci/cimodel/data/simple/util/versions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def prefixed_parts(self):
99
with the prefix string.
1010
"""
1111
if self.parts:
12-
return [self.prefix + str(self.parts[0])] + list(map(str, self.parts[1:]))
12+
return [self.prefix + str(self.parts[0])] + [str(part) for part in self.parts[1:]]
1313
else:
1414
return [self.prefix]
1515

caffe2/python/fused_8bit_rowwise_conversion_ops_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def floats_to_bytes(floats):
3030
if isinstance(as_bytes[0], int):
3131
byte_matrix[i] = list(as_bytes)
3232
else:
33-
byte_matrix[i] = list(map(ord, as_bytes))
33+
byte_matrix[i] = [ord(i) for i in as_bytes]
3434
return byte_matrix
3535

3636

caffe2/python/operator_test/fused_nbit_rowwise_conversion_ops_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def int8_to_bytes(int8s):
4646
if isinstance(as_bytes[0], int):
4747
byte_matrix[i] = list(as_bytes)
4848
else:
49-
byte_matrix[i] = list(map(ord, as_bytes))
49+
byte_matrix[i] = [ord(i) for i in as_bytes]
5050
return byte_matrix
5151

5252

caffe2/python/operator_test/torch_integration_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def floats_to_bytes(floats):
9090
if isinstance(as_bytes[0], int):
9191
byte_matrix[i] = list(as_bytes)
9292
else:
93-
byte_matrix[i] = list(map(ord, as_bytes))
93+
byte_matrix[i] = [ord(i) for i in as_bytes]
9494
return byte_matrix
9595

9696

caffe2/python/rnn_cell.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _RectifyName(blob_reference_or_name):
4242
def _RectifyNames(blob_references_or_names):
4343
if blob_references_or_names is None:
4444
return None
45-
return list(map(_RectifyName, blob_references_or_names))
45+
return [_RectifyName(i) for i in blob_references_or_names]
4646

4747

4848
class RNNCell(object):
@@ -236,7 +236,7 @@ def get_state_names(self):
236236
'''
237237
Returns recurrent state names with self.name scoping applied
238238
'''
239-
return list(map(self.scope, self.get_state_names_override()))
239+
return [self.scope(name) for name in self.get_state_names_override()]
240240

241241
def get_state_names_override(self):
242242
'''

test/jit/test_list_dict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,11 +948,11 @@ def check_list(fn, li):
948948
check_list(min_intlist, int_list)
949949
check_list(max_intlist, int_list)
950950

951-
bool_li = list(map(lambda x: bool(x), int_list))
951+
bool_li = [bool(x) for x in int_list]
952952
check_list(min_boollist, bool_li)
953953
check_list(max_boollist, bool_li)
954954

955-
float_li = list(map(lambda x: float(x), int_list))
955+
float_li = [float(x) for x in int_list]
956956
check_list(min_floatlist, float_li)
957957
check_list(max_floatlist, float_li)
958958

test/onnx/test_pytorch_onnx_onnxruntime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def convert_to_onnx(model, input=None, opset_version=9, example_outputs=None,
5353
def run_ort(ort_sess, input):
5454
input_copy = copy.deepcopy(input)
5555
input, _ = torch.jit._flatten(input_copy)
56-
inputs = list(map(to_numpy, input))
56+
inputs = [to_numpy(inp) for inp in input]
5757

5858
ort_inputs = dict((ort_sess.get_inputs()[i].name, input) for i, input in enumerate(inputs))
5959
ort_outs = ort_sess.run(None, ort_inputs)
@@ -62,7 +62,7 @@ def run_ort(ort_sess, input):
6262

6363
def ort_compare_with_pytorch(ort_outs, output, rtol, atol):
6464
output, _ = torch.jit._flatten(output)
65-
outputs = list(map(to_numpy, output))
65+
outputs = [to_numpy(outp) for outp in output]
6666

6767
# compare onnxruntime and PyTorch results
6868
assert len(outputs) == len(ort_outs), "number of outputs differ"

test/onnx/verify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ def run(args):
386386
"it had a different set of parameters. Are you assigning Parameters\n"
387387
"in the forward() of your model definition?")
388388
with errs.addErrCtxt(initializer_order_hint):
389-
errs.requireEqual(list(map(lambda x: x.name, proto.graph.initializer)),
390-
list(map(lambda x: x.name, alt_proto.graph.initializer)),
389+
errs.requireEqual([x.name for x in proto.graph.initializer],
390+
[x.name for x in alt_proto.graph.initializer],
391391
msg="Parameters list differs")
392392

393393
# Now check if the embedded parameters are actually the same

test/test_cuda.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,9 +2959,9 @@ def test_reduce_add(self):
29592959
self.assertEqual(result.cpu(), x + y)
29602960

29612961
def _test_reduce_add_coalesced(self, tensors, buffer_size):
2962-
dup_tensors = [tensors, list(map(lambda t: t.cuda(1), tensors))]
2962+
dup_tensors = [tensors, [t.cuda(1) for t in tensors]]
29632963

2964-
r_tensors = list(map(comm.reduce_add, zip(*dup_tensors)))
2964+
r_tensors = [comm.reduce_add(t) for t in zip(*dup_tensors)]
29652965
for r, t in zip(r_tensors, tensors):
29662966
self.assertEqualTypeString(r, t)
29672967
self.assertEqual(r, t * 2)

test/test_jit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8030,7 +8030,7 @@ def contained_blocks(node):
80308030
return len(node.findAllNodes("prim::If")) * 2 + len(node.findAllNodes("prim::Loop"))
80318031
for node in ifs + loops:
80328032
outs = list(node.outputs())
8033-
out_name = list(map(lambda x: x.debugName(), outs))
8033+
out_name = [x.debugName() for x in outs]
80348034
if len(out_name) == 0:
80358035
continue
80368036
fc = FileCheck()

0 commit comments

Comments
 (0)