| 145 | def __del__(self): all_tensors.pop(weakref.ref(self), None) |
| 146 | |
| 147 | def _apply_uop(self, fxn:Callable[..., UOp], *x:Tensor, extra_args=(), **kwargs) -> Tensor: |
| 148 | srcs = (self,)+x |
| 149 | new_uop: UOp = fxn(*[t.uop for t in srcs], *extra_args, **kwargs) |
| 150 | if TRACEMETA >= 1 and (metadata:=_METADATA.get()) is not None: all_metadata[new_uop] = (metadata,) |
| 151 | # directly create the Tensor |
| 152 | ret = Tensor.__new__(Tensor) |
| 153 | ret.uop, ret.grad, ret.is_param = new_uop, None, True |
| 154 | # add to all_tensors after construction succeeds |
| 155 | all_tensors[weakref.ref(ret)] = None |
| 156 | return ret |
| 157 | |
| 158 | # alu and const_like are used by the mixins |
| 159 | def alu(self, op: Ops, *src: Tensor) -> Tensor: return self._apply_uop(lambda *u: u[0].alu(op, *u[1:]), *src) |