(x:Tensor)
| 1128 | |
| 1129 | # ***** Indexing Ops ***** |
| 1130 | def NonZero(x:Tensor): |
| 1131 | mask = (x!=0).flatten() |
| 1132 | flat_idx = Tensor.arange(mask.numel(), dtype=dtypes.int64, device=x.device).masked_select(mask) |
| 1133 | if flat_idx.ndim == 0: flat_idx = flat_idx.reshape(1) |
| 1134 | if x.ndim == 0: |
| 1135 | return Tensor.zeros((0, flat_idx.shape[0]), dtype=dtypes.int64, device=x.device) |
| 1136 | strides = [prod(int(s) for s in x.shape[i+1:]) if i+1 < x.ndim else 1 for i in range(x.ndim)] |
| 1137 | coords = [((flat_idx // stride) % int(dim)) for stride, dim in zip(strides, x.shape)] |
| 1138 | return Tensor.stack(*coords, dim=0) |
| 1139 | |
| 1140 | def ArrayFeatureExtractor(x:Tensor, indices:Tensor): return x[..., indices] |
| 1141 |
nothing calls this directly
no test coverage detected
searching dependent graphs…