Properties
Basic¤
ndim
property
¤
ndim: int
Returns the number of dimensions in the tensor.
t = Tensor([[1, 2], [3, 4]])
print(t.ndim)
2
numel
¤
numel() -> sint
Returns the total number of elements in the tensor.
t = Tensor([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(t.numel())
8
Source code in tinygrad/mixin/movement.py
38 39 40 41 42 43 44 45 46 47 | |
element_size
¤
element_size() -> int
Returns the size in bytes of an individual element in the tensor.
t = Tensor([5], dtype=dtypes.int16)
print(t.element_size())
2
Source code in tinygrad/mixin/dtype.py
12 13 14 15 16 17 18 19 20 21 | |
nbytes
¤
nbytes() -> int
Returns the total number of bytes of all elements in the tensor.
t = Tensor([8, 9], dtype=dtypes.float)
print(t.nbytes())
8
Source code in tinygrad/mixin/__init__.py
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 | |
is_floating_point
¤
is_floating_point() -> bool
Returns True if the tensor contains floating point types, i.e. is one of dtypes.float64, dtypes.float32,
dtypes.float16, dtypes.bfloat16.
t = Tensor([8, 9], dtype=dtypes.float32)
print(t.is_floating_point())
True
Source code in tinygrad/mixin/dtype.py
23 24 25 26 27 28 29 30 31 32 33 | |
size
¤
Returns the size of the tensor. If dim is specified, return the length along dimension dim. Otherwise return the shape of the tensor.
t = Tensor([[4, 5, 6], [7, 8, 9]])
print(t.size())
(2, 3)
print(t.size(dim=1))
3
Source code in tinygrad/mixin/movement.py
49 50 51 52 53 54 55 56 57 58 59 60 61 | |
Data Access¤
data
¤
data() -> memoryview
Returns the data of this tensor as a memoryview.
t = Tensor([1, 2, 3, 4])
print(np.frombuffer(t.data(), dtype=np.int32))
[1 2 3 4]
Source code in tinygrad/tensor.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
item
¤
item() -> PyConst
Returns the value of this tensor as a standard Python number.
t = Tensor(42)
print(t.item())
42
Source code in tinygrad/tensor.py
310 311 312 313 314 315 316 317 318 319 320 | |
tolist
¤
Returns the value of this tensor as a nested list. Returns single value for const tensor.
t = Tensor([1, 2, 3, 4])
print(t.tolist())
[1, 2, 3, 4]
t = Tensor(5)
print(t.tolist())
5
Source code in tinygrad/tensor.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
numpy
¤
numpy() -> 'numpy.ndarray'
Returns the value of this tensor as a numpy.ndarray.
t = Tensor([1, 2, 3, 4])
print(repr(t.numpy()))
array([1, 2, 3, 4], dtype=int32)
Source code in tinygrad/tensor.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 | |
tinygrad ops¤
linear_with_vars
¤
Creates the LINEAR UOp needed to realize these Tensor(s), with Variables.
Source code in tinygrad/tensor.py
228 229 230 231 232 | |
schedule_linear
¤
Creates the schedule needed to realize these Tensor(s).
Source code in tinygrad/tensor.py
234 235 236 237 238 | |
realize
¤
Triggers the computation needed to create these Tensor(s).
Source code in tinygrad/tensor.py
240 241 242 243 244 245 | |
replace
¤
Replaces the data of this tensor with the data of another tensor. Only the shape of the tensors must match.
Source code in tinygrad/tensor.py
247 248 249 250 251 252 253 254 | |
assign
¤
Source code in tinygrad/tensor.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
detach
¤
detach() -> Self
Returns a new tensor with the same data as this tensor, but detached from the autograd graph.
Source code in tinygrad/mixin/elementwise.py
28 29 30 31 32 | |
clone
¤
Creates a clone of this tensor allocating a separate buffer for the data.
If device is specified, the clone is placed on that device.
Source code in tinygrad/tensor.py
356 357 358 359 360 361 362 363 | |
to
¤
Moves the tensor to the given device.
Source code in tinygrad/tensor.py
365 366 367 368 369 370 371 372 373 | |
to_
¤
Moves the tensor to the given device in place.
Source code in tinygrad/tensor.py
375 376 377 378 379 380 381 | |
shard
¤
Shards the tensor across the given devices. Optionally specify which axis to shard on.
t = Tensor.empty(2, 4)
print(t.shard((t.device, t.device), axis=1).uop)
UOp(Ops.MULTI, dtypes.float, arg=1, src=(
UOp(Ops.SHRINK, dtypes.float, arg=None, src=(
UOp(Ops.COPY, dtypes.float, arg=None, src=(
UOp(Ops.RESHAPE, dtypes.float, arg=None, src=(
UOp(Ops.BUFFER, dtypes.float, arg=8, src=(
UOp(Ops.UNIQUE, dtypes.void, arg=1199, src=()),
UOp(Ops.DEVICE, dtypes.void, arg='CPU', src=()),)),
UOp(Ops.STACK, dtypes.weakint.vec(2), arg=None, src=(
x7:=UOp(Ops.CONST, dtypes.weakint, arg=2, src=()),
UOp(Ops.CONST, dtypes.weakint, arg=4, src=()),)),)),
UOp(Ops.DEVICE, dtypes.void, arg=('CPU', 'CPU'), src=()),)),
UOp(Ops.STACK, dtypes.weakint.vec(2), arg=None, src=(
UOp(Ops.CONST, dtypes.weakint, arg=0, src=()),
x12:=UOp(Ops.MUL, dtypes.weakint, arg=None, src=(
UOp(Ops.DEFINE_VAR, dtypes.weakint, arg=('_device_num', 0, 1), src=()),
x7,)),)),
UOp(Ops.STACK, dtypes.weakint.vec(2), arg=None, src=(
x7,
UOp(Ops.ADD, dtypes.weakint, arg=None, src=(
x12,
x7,)),)),)),))
Source code in tinygrad/tensor.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | |
shard_
¤
Shards the tensor across the given devices in place.
Source code in tinygrad/tensor.py
399 400 401 402 403 | |
contiguous
¤
contiguous(*args, **kwargs) -> Tensor
Returns a contiguous tensor.
Source code in tinygrad/tensor.py
1248 1249 1250 1251 1252 | |
contiguous_backward
¤
contiguous_backward() -> Self
Inserts a contiguous operation in the backward pass.
Source code in tinygrad/mixin/elementwise.py
46 47 48 49 50 | |
Gradient¤
gradient
¤
Computes the gradient of the targets with respect to self.
x = Tensor.eye(3)
y = Tensor([[2.0,0,-2.0]])
z = y.matmul(x).sum()
dx, dy = z.gradient(x, y)
print(dx.tolist()) # dz/dx
print(dy.tolist()) # dz/dy
[[2.0, 2.0, 2.0], [0.0, 0.0, 0.0], [-2.0, -2.0, -2.0]]
[[1.0, 1.0, 1.0]]
Source code in tinygrad/tensor.py
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 | |
backward
¤
Propagates the gradient of a tensor backwards through the computation graph. If the 'gradient' argument is not provided, the tensor must be a scalar, and the gradient is implicitly set to 1.0.
t = Tensor([1.0, 2.0, 3.0, 4.0])
t.sum().backward()
print(t.grad.numpy())
[1. 1. 1. 1.]
Source code in tinygrad/tensor.py
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 | |