From f88f71d73a23cb028ce0eac96cc1cf5cd28afe42 Mon Sep 17 00:00:00 2001 From: Roelof van Dijk <3604013+roelofvandijk@users.noreply.github.com> Date: Thu, 27 Jun 2024 13:45:29 +0200 Subject: [PATCH] ruff: unnecessary-comprehension (#5174) * enable ruff C416 unnecessary-comprehension * already a list --- ruff.toml | 1 + test/external/external_benchmark_resnet.py | 2 +- test/external/external_model_benchmark.py | 2 +- test/external/external_test_onnx_backend.py | 2 +- test/test_linearizer.py | 2 +- test/test_multitensor.py | 4 ++-- tinygrad/codegen/linearizer.py | 4 ++-- tinygrad/helpers.py | 2 +- tinygrad/multi.py | 2 +- tinygrad/runtime/ops_nv.py | 4 ++-- tinygrad/tensor.py | 4 ++-- 11 files changed, 15 insertions(+), 14 deletions(-) diff --git a/ruff.toml b/ruff.toml index d2a221c6d2..6e7edfc4bb 100644 --- a/ruff.toml +++ b/ruff.toml @@ -23,6 +23,7 @@ lint.select = [ "W291", # trailing-whitespace "W293", # blank-line-with-whitespace "UP039", # unnecessary-class-parentheses + "C416", # unnecessary-comprehension ] line-length = 150 diff --git a/test/external/external_benchmark_resnet.py b/test/external/external_benchmark_resnet.py index 498f555cb1..813554b89e 100644 --- a/test/external/external_benchmark_resnet.py +++ b/test/external/external_benchmark_resnet.py @@ -75,7 +75,7 @@ class BenchmarkResnetTrain(unittest.TestCase): else: sched, _ = Tensor.schedule_with_vars(y, x.grad, *[t.grad for t in optim.params]) for _ in range(JITCNT): - run_schedule([si for si in sched]) + run_schedule(list(sched)) CNT = getenv("CNT", 5) best_tm = None diff --git a/test/external/external_model_benchmark.py b/test/external/external_model_benchmark.py index a7cc35b7a1..6b6c1b0299 100644 --- a/test/external/external_model_benchmark.py +++ b/test/external/external_model_benchmark.py @@ -118,7 +118,7 @@ def benchmark_model(m, devices, validate_outs=False): ort_sess = ort.InferenceSession(str(fn), ort_options, ["CPUExecutionProvider"]) onnx_out = ort_sess.run(output_names, np_inputs) - onnx_out = dict([*[(name,x) for name, x in zip(output_names, onnx_out)]]) + onnx_out = dict([*list(zip(output_names, onnx_out))]) assert_allclose(tinygrad_out, onnx_out, rtol=rtol, atol=atol) print(f"{m:16s}outputs validated on {device=} with rtol={rtol:.1e}, atol={atol:.1e}") diff --git a/test/external/external_test_onnx_backend.py b/test/external/external_test_onnx_backend.py index 47864d8e99..70fce70b8c 100644 --- a/test/external/external_test_onnx_backend.py +++ b/test/external/external_test_onnx_backend.py @@ -19,7 +19,7 @@ class TinygradModel(BackendRep): self.input_names = input_names def run(self, inputs: Any, **kwargs: Any) -> Tuple[Any, ...]: - real_inputs = {k:v for k,v in zip(self.input_names, inputs)} + real_inputs = dict(zip(self.input_names, inputs)) ret = self.fxn(real_inputs, debug=True) return tuple(x.numpy() if isinstance(x, Tensor) else [i.numpy() for i in x] if isinstance(x, list) else np.array(x) for x in ret.values()) diff --git a/test/test_linearizer.py b/test/test_linearizer.py index 130badb496..50e3d758f5 100644 --- a/test/test_linearizer.py +++ b/test/test_linearizer.py @@ -1058,7 +1058,7 @@ def _helper_linearizer_opt_ast(realized_ast:Tuple[LazyOp, ...], real_bufs:List[B for opt in opts: k.apply_opt(opt) if expected_color_size is not None: - assert (cs:=[(x,y) for x,y in zip(k.colors(), k.full_shape)]) == expected_color_size, f"expected={expected_color_size} got={cs}" + assert (cs:=list(zip(k.colors(), k.full_shape))) == expected_color_size, f"expected={expected_color_size} got={cs}" prg = get_prg(k) for buf in outbufs: buf.copyin(np.zeros((buf.size, ), dtype=_to_np_dtype(buf.dtype)).data) # Zero to check that all values are filled prg.exec(real_bufs) diff --git a/test/test_multitensor.py b/test/test_multitensor.py index e7f5ae7728..6553b501ae 100644 --- a/test/test_multitensor.py +++ b/test/test_multitensor.py @@ -829,9 +829,9 @@ class TestBatchNorm(unittest.TestCase): p.to_(devices) synced_out = synced_bn(x) - synced_si = [si for si in create_schedule(synced_out.lazydata.lbs)] + synced_si = list(create_schedule(synced_out.lazydata.lbs)) unsynced_out = unsynced_bn(x) - unsynced_si = [si for si in create_schedule(unsynced_out.lazydata.lbs)] + unsynced_si = list(create_schedule(unsynced_out.lazydata.lbs)) # TODO: test synced / unsynced batchnorm cross device kernel and copies assert synced_si diff --git a/tinygrad/codegen/linearizer.py b/tinygrad/codegen/linearizer.py index 807aeef3f1..8f6b0991f7 100644 --- a/tinygrad/codegen/linearizer.py +++ b/tinygrad/codegen/linearizer.py @@ -77,7 +77,7 @@ def expand_idxs(nodes:Sequence[Node]) -> Tuple[Union[Variable, NumNode], ...]: eidxs = [expand_idx(node) for node in nodes] return tuple([v if v not in eidxs[:j] else NumNode(0) for j, v in enumerate(eidxs)]) # take only first occurrence of expand variable def iter_idxs(idxs:Tuple[Union[Variable, NumNode], ...]) -> Iterator[Tuple[int,...]]: - yield from (x[::-1] for x in itertools.product(*[[x for x in range(v.min, v.max + 1)] for v in idxs[::-1]])) + yield from (x[::-1] for x in itertools.product(*[list(range(v.min, v.max + 1)) for v in idxs[::-1]])) def to_image_idx(base_shape:Tuple[int, ...], idxy:Node, valid:Node) -> Tuple[Tuple[Node, Node], Node]: idx, idy = (idxy // 4) % base_shape[1], (idxy // (4 * base_shape[1])) @@ -290,7 +290,7 @@ class Linearizer(Kernel): # cast initial accs wmmas = [UOp(UOps.CAST, (dt3:=tc.dtype_out.vec(wmma_sz[2])), tuple(accs[reduceop][x:x+wmma_sz[2]])) for x in range(0, len(accs[reduceop]), wmma_sz[2])] - for it in [x[::-1] for x in itertools.product(*[x for x in [range(sz) for _,sz in upcasts[0]][::-1]])]: + for it in [x[::-1] for x in itertools.product(*list([range(sz) for _,sz in upcasts[0]][::-1]))]: offs = [x*y for (x,y) in zip([sum([prod(x) for x in zip(it, [stride for stride,_ in y])]) for y in upcasts], wmma_sz)] ops = (UOp(UOps.CAST, tc.dtype_in.vec(wmma_sz[0]), tuple(locals_to_store[0][2][offs[0]:offs[0]+wmma_sz[0]])), UOp(UOps.CAST, tc.dtype_in.vec(wmma_sz[1]), tuple(locals_to_store[1][2][offs[1]:offs[1]+wmma_sz[1]])), diff --git a/tinygrad/helpers.py b/tinygrad/helpers.py index 02ea724f38..d8a6957e8f 100644 --- a/tinygrad/helpers.py +++ b/tinygrad/helpers.py @@ -58,7 +58,7 @@ def get_child(obj, key): def get_shape(x) -> Tuple[int, ...]: if not isinstance(x, (list, tuple)): return () subs = [get_shape(xi) for xi in x] - if not all_same([sub for sub in subs]): raise ValueError(f"inhomogeneous shape from {x}") + if not all_same(subs): raise ValueError(f"inhomogeneous shape from {x}") return (len(subs),) + (subs[0] if subs else ()) # returns the axes to create new_shape if new_shape can be created by combining axis from old_shape diff --git a/tinygrad/multi.py b/tinygrad/multi.py index d52c03ae5d..848cbe4bd8 100644 --- a/tinygrad/multi.py +++ b/tinygrad/multi.py @@ -54,7 +54,7 @@ class MultiLazyBuffer: self.lbs, self.axis, self.dtype, self.device, self.real = lbs, axis, lbs[0].dtype, tuple(x.device for x in lbs), real or [True]*len(lbs) if axis is not None: splits = list(itertools.accumulate([lb.shape[axis] for lb in lbs], initial=0)) - self.bounds = [(st,ed) for st,ed in zip(splits, splits[1:])] + self.bounds = list(zip(splits, splits[1:])) @property def shape(self): diff --git a/tinygrad/runtime/ops_nv.py b/tinygrad/runtime/ops_nv.py index 573fe931c9..980383d2e9 100644 --- a/tinygrad/runtime/ops_nv.py +++ b/tinygrad/runtime/ops_nv.py @@ -154,7 +154,7 @@ class HWComputeQueue(HWQueue): self.q += [nvmethod(1, nv_gpu.NVC6C0_OFFSET_OUT_UPPER, 2), *nvdata64(gpuaddr)] self.q += [nvmethod(1, nv_gpu.NVC6C0_LINE_LENGTH_IN, 2), len(data)*4, 0x1] self.q += [nvmethod(1, nv_gpu.NVC6C0_LAUNCH_DMA, 1), 0x41] - self.q += [nvmethod(1, nv_gpu.NVC6C0_LOAD_INLINE_DATA, len(data), typ=6)] + [x for x in data] + self.q += [nvmethod(1, nv_gpu.NVC6C0_LOAD_INLINE_DATA, len(data), typ=6)] + list(data) return self._mark_command_end() def exec(self, prg, kernargs, global_size=(1,1,1), local_size=(1,1,1), signal=None, signal_value=0): @@ -316,7 +316,7 @@ class NVProgram: # HACK: Save counts of args and vars to "unused" constbuffer for later extraction in mockgpu to pass into gpuocelot. if MOCKGPU: self.constbuffer_0[0:2] = [len(args), len(vals)] - kernargs = [arg_half for arg in args for arg_half in nvdata64_le(arg.base)] + [val for val in vals] + kernargs = [arg_half for arg in args for arg_half in nvdata64_le(arg.base)] + list(vals) sig_st, sig_en = (self.device._get_signal(), self.device._get_signal()) if PROFILE else (self.device.time_event_st, self.device.time_event_en) diff --git a/tinygrad/tensor.py b/tinygrad/tensor.py index 992c31388c..b2c643d027 100644 --- a/tinygrad/tensor.py +++ b/tinygrad/tensor.py @@ -1564,11 +1564,11 @@ class Tensor: formula = formula.replace(" ", "") inputs_str, output = formula.split("->") if "->" in formula else (formula, \ ''.join(c for c in sorted(formula) if formula.count(c) == 1 and c.isalpha())) - inputs = [x for x in inputs_str.split(',')] + inputs = inputs_str.split(',') assert len(xs) == len(inputs), f"number of inputs doesn't match number of operands in formula, expected {len(inputs)}, got {len(xs)}" # map the value of each letter in the formula - letter_val = sorted(merge_dicts([{letter:dim for letter, dim in zip(letters, tensor.shape)} for letters, tensor in zip(inputs, xs)]).items()) + letter_val = sorted(merge_dicts([dict(zip(letters, tensor.shape)) for letters, tensor in zip(inputs, xs)]).items()) xs_:List[Tensor] = [] lhs = [sorted(enumerate(s), key=lambda e:e[1]) for s in inputs]