forked from tinygrad/tinygrad
remove redundant list comprehension from inside all. (#397)
remove explicit inherit from object.
This commit is contained in:
@@ -529,7 +529,7 @@ def bytes_to_unicode():
|
||||
cs = [chr(n) for n in cs]
|
||||
return dict(zip(bs, cs))
|
||||
|
||||
class ClipTokenizer(object):
|
||||
class ClipTokenizer:
|
||||
def __init__(self, bpe_path: str = default_bpe()):
|
||||
self.byte_encoder = bytes_to_unicode()
|
||||
merges = gzip.open(bpe_path).read().decode("utf-8").split('\n')
|
||||
|
||||
@@ -64,7 +64,7 @@ def strides_for_shape(shape:Tuple[int, ...]) -> Tuple[int, ...]:
|
||||
|
||||
@functools.lru_cache(maxsize=None)
|
||||
def view_from_shape(shape:Tuple[int, ...]) -> View:
|
||||
assert all([isinstance(x, int) for x in shape]) and len(shape) != 0
|
||||
assert all(isinstance(x, int) for x in shape) and len(shape) != 0
|
||||
return View(tuple(shape), strides_for_shape(shape))
|
||||
|
||||
class ShapeTracker:
|
||||
@@ -100,7 +100,7 @@ class ShapeTracker:
|
||||
self.views.append(view)
|
||||
|
||||
def reshape(self, *new_shape):
|
||||
assert all([isinstance(x, int) for x in new_shape])
|
||||
assert all(isinstance(x, int) for x in new_shape)
|
||||
assert prod(self.shape) == prod(new_shape), f"can't reshape {self.shape} -> {new_shape}"
|
||||
|
||||
# check if this is adding or removing 1s (only)
|
||||
@@ -117,7 +117,7 @@ class ShapeTracker:
|
||||
self.views.append(view)
|
||||
|
||||
def permute(self, *axis):
|
||||
assert all([isinstance(x, int) and x >= 0 and x < len(self.shape) for x in axis])
|
||||
assert all(isinstance(x, int) and x >= 0 and x < len(self.shape) for x in axis)
|
||||
assert len(set(axis)) == len(axis) and len(axis) == len(self.shape), f"can't permute {self.shape} with {axis}"
|
||||
self.views[-1] = View([self.shape[a] for a in axis], [self.strides[a] for a in axis], self.offset)
|
||||
|
||||
@@ -143,14 +143,14 @@ class ShapeTracker:
|
||||
self.views += [zeroview, View(self.shape, strides_for_shape(self.shape))]
|
||||
|
||||
def expand(self, *new_shape):
|
||||
assert all([isinstance(x, int) for x in new_shape])
|
||||
assert all([x == y or x == 1 for x,y in zip(self.shape, new_shape)]), f"can't expand {self.shape} into {new_shape}"
|
||||
assert all(isinstance(x, int) for x in new_shape)
|
||||
assert all(x == y or x == 1 for x,y in zip(self.shape, new_shape)), f"can't expand {self.shape} into {new_shape}"
|
||||
strides = [s if x == y else 0 for s,(x,y) in zip(self.strides, zip(self.shape, new_shape))]
|
||||
self.views[-1] = View(new_shape, strides, self.offset)
|
||||
|
||||
# TODO: combine with slice? this doesn't require a ZeroView, though slice shouldn't always either
|
||||
def stride(self, *mul):
|
||||
assert all([isinstance(x, int) for x in mul])
|
||||
assert all(isinstance(x, int) for x in mul)
|
||||
strides = [z*m for z,m in zip(self.strides, mul)]
|
||||
new_shape = [(s+(abs(m)-1))//abs(m) for s,m in zip(self.shape, mul)]
|
||||
offset = sum([(s-1)*z for s,z,m in zip(self.shape, self.strides, mul) if m < 0])
|
||||
|
||||
+2
-2
@@ -321,7 +321,7 @@ class Function:
|
||||
def __init__(self, device:str, *tensors:Tensor):
|
||||
self.device, self.parents = device, tensors
|
||||
self.needs_input_grad = [t.requires_grad for t in self.parents]
|
||||
self.requires_grad = True if any(self.needs_input_grad) else (None if any([x is None for x in self.needs_input_grad]) else False)
|
||||
self.requires_grad = True if any(self.needs_input_grad) else (None if any(x is None for x in self.needs_input_grad) else False)
|
||||
self.saved_tensors : List[Tensor] = []
|
||||
|
||||
def forward(self, *args, **kwargs): raise NotImplementedError(f"forward not implemented for {type(self)}")
|
||||
@@ -356,4 +356,4 @@ def register_op(name, fxn):
|
||||
setattr(Tensor, f"__i{name}__", lambda self,x: self.assign(fxn(self,x)))
|
||||
setattr(Tensor, f"__r{name}__", lambda self,x: fxn(x,self))
|
||||
for name in ['add', 'sub', 'mul', 'pow', 'matmul', 'truediv']:
|
||||
register_op(name, getattr(Tensor, name if name != 'truediv' else 'div'))
|
||||
register_op(name, getattr(Tensor, name if name != 'truediv' else 'div'))
|
||||
|
||||
Reference in New Issue
Block a user