using tuples in isinstance
MODIFY
tinygrad/helpers.py
tinygrad/helpers.py
@@ -3,7 +3,7 @@ import os, math, functools
def dedup(x): return list(dict.fromkeys(x)) # retains list order
def prod(x): return math.prod(x)
-def argfix(*x): return tuple() if len(x) == 0 else tuple(x[0]) if isinstance(x[0], tuple) or isinstance(x[0], list) else tuple(x)
+def argfix(*x): return tuple() if len(x) == 0 else tuple(x[0]) if isinstance(x[0], (tuple, list)) else tuple(x)
def argsort(x): return sorted(range(len(x)), key=x.__getitem__) # https://stackoverflow.com/questions/3382352/equivalent-of-numpy-argsort-in-basic-python
def all_same(items): return all(x == items[0] for x in items) if len(items) > 0 else True
def colored(st, color): return f"\u001b[{30+['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'].index(color)}m{st}\u001b[0m" # replace the termcolor library with one line

MODIFY
tinygrad/nn/optim.py
tinygrad/nn/optim.py
@@ -69,7 +69,7 @@ def get_parameters(obj):
parameters = []
if isinstance(obj, Tensor):
parameters.append(obj)
- elif isinstance(obj, list) or isinstance(obj, tuple):
+ elif isinstance(obj, (list, tuple)):
for x in obj:
parameters.extend(get_parameters(x))
elif hasattr(obj, '__dict__'):

MODIFY
tinygrad/shape/symbolic.py
tinygrad/shape/symbolic.py
@@ -23,7 +23,7 @@ class Node:
if isinstance(self, MulNode) and self.b%b == 0: return self.a*(self.b//b)
if isinstance(self, MulNode) and b%self.b == 0: return self.a//(b//self.b)
if isinstance(self, SumNode):
- factors, tmp_nofactor = partition(self.nodes, lambda x: (isinstance(x, MulNode) or isinstance(x, NumNode)) and x.b%b == 0)
+ factors, tmp_nofactor = partition(self.nodes, lambda x: (isinstance(x, (MulNode, NumNode))) and x.b%b == 0)
nofactor = []
# ugh, i doubt this is universally right
for x in tmp_nofactor:
@@ -33,7 +33,7 @@ class Node:
nofactor.append(Variable.num(modn(x.b, b)))
else:
nofactor.append(x)
- gcd = [math.gcd(x.b, b) if isinstance(x, MulNode) or isinstance(x, NumNode) else None for x in nofactor]
+ gcd = [math.gcd(x.b, b) if isinstance(x, (MulNode, NumNode)) else None for x in nofactor]
if len(factors) > 0:
# these don't have to be the same, just having a common factor
if len(gcd) > 0 and all_same(gcd) and gcd[0] is not None and gcd[0] > 1:
@@ -45,7 +45,7 @@ class Node:
def __mod__(self, b:int):
if b == 1: return NumNode(0)
if isinstance(self, SumNode):
- a = Variable.sum([(x if not isinstance(x, NumNode) else Variable.num(modn(x.b, b))) for x in self.nodes if not (isinstance(x, MulNode) or isinstance(x, NumNode)) or (x.b%b != 0)])
+ a = Variable.sum([(x if not isinstance(x, NumNode) else Variable.num(modn(x.b, b))) for x in self.nodes if not (isinstance(x, (MulNode, NumNode))) or (x.b%b != 0)])
else:
a = self
if a.min >= 0 and a.max < b: return a

using tuples in isinstance (#534)