ruff: unnecessary-comprehension (#5174)

* enable ruff C416 unnecessary-comprehension

* already a list
This commit is contained in:
Roelof van Dijk
2024-06-27 07:45:29 -04:00
committed by GitHub
parent f1c7944c44
commit f88f71d73a
11 changed files with 15 additions and 14 deletions
+1
View File
@@ -23,6 +23,7 @@ lint.select = [
"W291", # trailing-whitespace
"W293", # blank-line-with-whitespace
"UP039", # unnecessary-class-parentheses
"C416", # unnecessary-comprehension
]
line-length = 150
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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}")
+1 -1
View File
@@ -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())
+1 -1
View File
@@ -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)
+2 -2
View File
@@ -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
+2 -2
View File
@@ -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]])),
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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):
+2 -2
View File
@@ -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)
+2 -2
View File
@@ -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]