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
55 56 57 58 59 60 61 62 63 64 65 | |
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/op.py
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 | |
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
67 68 69 70 71 72 73 74 75 76 77 | |
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
59 60 61 62 63 64 65 66 67 68 69 70 71 | |
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
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | |
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/mixin/op.py
22 23 24 25 26 27 28 29 30 31 32 | |
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
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |
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
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
tinygrad ops¤
linear_with_vars
¤
Creates the LINEAR UOp needed to realize these Tensor(s), with Variables.
Source code in tinygrad/tensor.py
395 396 397 398 399 400 401 402 | |
schedule_linear
¤
Creates the schedule needed to realize these Tensor(s).
Source code in tinygrad/tensor.py
404 405 406 407 408 | |
realize
¤
Triggers the computation needed to create these Tensor(s).
Source code in tinygrad/tensor.py
410 411 412 413 414 415 416 | |
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
418 419 420 421 422 423 424 425 | |
assign
¤
Source code in tinygrad/tensor.py
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
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
43 44 45 46 47 | |
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
530 531 532 533 534 535 536 537 | |
to
¤
Moves the tensor to the given device.
Source code in tinygrad/tensor.py
539 540 541 542 543 544 545 546 547 548 549 | |
to_
¤
Moves the tensor to the given device in place.
Source code in tinygrad/tensor.py
551 552 553 554 555 556 557 | |
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.UNSHARD, arg=(1,), src=(
UOp(Ops.SHRINK, arg=None, src=(
UOp(Ops.COPY, arg=('CPU', 'CPU'), src=(
UOp(Ops.RESHAPE, arg=None, src=(
UOp(Ops.BUFFER, arg=ParamArg(1343, dtypes.float, 8, device='CPU'), src=()),
UOp(Ops.STACK, arg=None, src=(
x5:=UOp(Ops.CONST, arg=2, src=()),
UOp(Ops.CONST, arg=4, src=()),)),)),)),
UOp(Ops.STACK, arg=None, src=(
UOp(Ops.CONST, arg=0, src=()),
UOp(Ops.MUL, arg=None, src=(
x10:=UOp(Ops.RANGE, arg=(-1, AxisType.DEVICE), src=(
x5,)),
x5,)),)),
UOp(Ops.STACK, arg=None, src=(
x5,
x5,)),)),
x10,))
Source code in tinygrad/tensor.py
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 | |
shard_
¤
Shards the tensor across the given devices in place.
Source code in tinygrad/tensor.py
577 578 579 580 581 | |
contiguous
¤
contiguous(**kwargs) -> Self
Returns a contiguous tensor.
Source code in tinygrad/mixin/elementwise.py
59 60 61 62 63 64 65 66 | |
contiguous_backward
¤
contiguous_backward() -> Self
Inserts a contiguous operation in the backward pass.
Source code in tinygrad/mixin/elementwise.py
68 69 70 71 72 | |
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/mixin/op.py
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
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
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 | |