Movement
Movement (low level)¤
view
¤
.view
is an alias for .reshape
.
Source code in tinygrad/tensor.py
964 965 966 |
|
reshape
¤
reshape(shape, *args) -> Tensor
Returns a tensor with the same data as the original tensor but with a different shape.
shape
can be passed as a tuple or as separate arguments.
t = Tensor.arange(6)
print(t.reshape(2, 3).numpy())
[[0 1 2]
[3 4 5]]
Source code in tinygrad/tensor.py
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 |
|
expand
¤
expand(shape, *args) -> Tensor
Returns a tensor that is expanded to the shape that is specified. Expand can also increase the number of dimensions that a tensor has.
Passing a -1
or None
to a dimension means that its size will not be changed.
t = Tensor([1, 2, 3])
print(t.expand(4, -1).numpy())
[[1 2 3]
[1 2 3]
[1 2 3]
[1 2 3]]
Source code in tinygrad/tensor.py
985 986 987 988 989 990 991 992 993 994 995 996 997 998 |
|
permute
¤
permute(order, *args) -> Tensor
Returns a tensor that is a permutation of the original tensor.
The new tensor has the same data as the original tensor but with the dimensions permuted according to the order specified.
order
can be passed as a tuple or as separate arguments.
t = Tensor.empty(2, 3, 5)
print(t.shape)
(2, 3, 5)
print(t.permute(2, 0, 1).shape)
(5, 2, 3)
Source code in tinygrad/tensor.py
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 |
|
flip
¤
flip(axis, *args) -> Tensor
Returns a tensor that reverses the order of the original tensor along given axis
.
axis
can be passed as a tuple or as separate arguments.
t = Tensor.arange(6).reshape(2, 3)
print(t.numpy())
[[0 1 2]
[3 4 5]]
print(t.flip(0).numpy())
[[3 4 5]
[0 1 2]]
print(t.flip((0, 1)).numpy())
[[5 4 3]
[2 1 0]]
Source code in tinygrad/tensor.py
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 |
|
shrink
¤
Returns a tensor that shrinks the each axis based on input arg.
arg
must have the same length as self.ndim
.
For each axis, it can be None
, which means no shrink, or a tuple (start, end)
that works the same as Python slice.
t = Tensor.arange(9).reshape(3, 3)
print(t.numpy())
[[0 1 2]
[3 4 5]
[6 7 8]]
print(t.shrink(((None, (1, 3)))).numpy())
[[1 2]
[4 5]
[7 8]]
print(t.shrink((((0, 2), (0, 2)))).numpy())
[[0 1]
[3 4]]
Source code in tinygrad/tensor.py
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 |
|
pad
¤
pad(
padding: (
Sequence[sint] | Sequence[tuple[sint, sint] | None]
),
mode: str = "constant",
value: float = 0.0,
) -> Tensor
Returns a tensor with padding applied based on the input padding
.
padding
supports two padding structures:
-
Flat padding:
(padding_left, padding_right, padding_top, padding_bottom, ...)
- This structure matches PyTorch's pad.
padding
length must be even.
-
Group padding:
(..., (padding_top, padding_bottom), (padding_left, padding_right))
- This structure matches pad for JAX, NumPy, TensorFlow, and others.
- For each axis, padding can be
None
, meaning no padding, or a tuple(start, end)
. padding
must have the same length asself.ndim
.
Padding values can be negative, resulting in dimension shrinks that work similarly to Python negative slices.
Padding modes is selected with mode
which supports constant
, reflect
and replicate
.
t = Tensor.arange(9).reshape(1, 1, 3, 3)
print(t.numpy())
[[[[0 1 2]
[3 4 5]
[6 7 8]]]]
print(t.pad((1, 2, 0, -1)).numpy())
[[[[0 0 1 2 0 0]
[0 3 4 5 0 0]]]]
print(t.pad(((None, None, (0, -1), (1, 2)))).numpy())
[[[[0 0 1 2 0 0]
[0 3 4 5 0 0]]]]
print(t.pad((1, 2, 0, -1), value=-float('inf')).numpy())
[[[[-inf 0. 1. 2. -inf -inf]
[-inf 3. 4. 5. -inf -inf]]]]
Source code in tinygrad/tensor.py
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
|
Movement (high level)¤
__getitem__
¤
__getitem__(indices) -> Tensor
Retrieves a sub-tensor using indexing.
Supported Index Types: int | slice | Tensor | None | list | tuple | Ellipsis
Examples:
t = Tensor.arange(12).reshape(3, 4)
print(t.numpy())
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
-
Int Indexing: Select an element or sub-tensor using integers for each dimension.
print(t[1, 2].numpy())
6
-
Slice Indexing: Select a range of elements using slice notation (
start:end:stride
).print(t[0:2, ::2].numpy())
[[0 2] [4 6]]
-
Tensor Indexing: Use another tensor as indices for advanced indexing. Using
tuple
orlist
here also works.print(t[Tensor([2, 0, 1]), Tensor([1, 2, 3])].numpy())
[9 2 7]
-
None
Indexing: Add a new dimension to the tensor.print(t[:, None].shape)
(3, 1, 4)
Note
Out-of-bounds indexing results in a value of 0
.
t = Tensor([1, 2, 3])
print(t[Tensor([4, 3, 2])].numpy())
[0 0 3]
Source code in tinygrad/tensor.py
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 |
|
gather
¤
Gathers values along an axis specified by dim
.
t = Tensor([[1, 2], [3, 4]])
print(t.numpy())
[[1 2]
[3 4]]
print(t.gather(1, Tensor([[0, 0], [1, 0]])).numpy())
[[1 1]
[4 3]]
Source code in tinygrad/tensor.py
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 |
|
cat
¤
Concatenates self with other Tensor
in args
along an axis specified by dim
.
All tensors must have the same shape except in the concatenating dimension.
t0, t1, t2 = Tensor([[1, 2]]), Tensor([[3, 4]]), Tensor([[5, 6]])
print(t0.cat(t1, t2, dim=0).numpy())
[[1 2]
[3 4]
[5 6]]
print(t0.cat(t1, t2, dim=1).numpy())
[[1 2 3 4 5 6]]
Source code in tinygrad/tensor.py
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 |
|
stack
¤
Concatenates self with other Tensor
in args
along a new dimension specified by dim
.
t0, t1, t2 = Tensor([1, 2]), Tensor([3, 4]), Tensor([5, 6])
print(t0.stack(t1, t2, dim=0).numpy())
[[1 2]
[3 4]
[5 6]]
print(t0.stack(t1, t2, dim=1).numpy())
[[1 3 5]
[2 4 6]]
Source code in tinygrad/tensor.py
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 |
|
repeat
¤
repeat(repeats, *args) -> Tensor
Repeats tensor number of times along each dimension specified by repeats
.
repeats
can be passed as a tuple or as separate arguments.
t = Tensor([1, 2, 3])
print(t.repeat(4, 2).numpy())
[[1 2 3 1 2 3]
[1 2 3 1 2 3]
[1 2 3 1 2 3]
[1 2 3 1 2 3]]
print(t.repeat(4, 2, 1).shape)
(4, 2, 3)
Source code in tinygrad/tensor.py
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 |
|
repeat_interleave
¤
Repeats elements of a tensor.
t = Tensor([1, 2, 3])
print(t.repeat_interleave(2).numpy())
[1 1 2 2 3 3]
Source code in tinygrad/tensor.py
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 |
|
split
¤
Splits the tensor into chunks along the dimension specified by dim
.
If sizes
is an integer, it splits into equally sized chunks if possible, otherwise the last chunk will be smaller.
If sizes
is a list, it splits into len(sizes)
chunks with size in dim
according to size
.
t = Tensor.arange(10).reshape(5, 2)
print(t.numpy())
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
split = t.split(2)
print("\n".join([repr(x.numpy()) for x in split]))
array([[0, 1],
[2, 3]], dtype=int32)
array([[4, 5],
[6, 7]], dtype=int32)
array([[8, 9]], dtype=int32)
split = t.split([1, 4])
print("\n".join([repr(x.numpy()) for x in split]))
array([[0, 1]], dtype=int32)
array([[2, 3],
[4, 5],
[6, 7],
[8, 9]], dtype=int32)
Source code in tinygrad/tensor.py
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 |
|
chunk
¤
Splits the tensor into chunks
number of chunks along the dimension dim
.
If the tensor size along dim
is not divisible by chunks
, all returned chunks will be the same size except the last one.
The function may return fewer than the specified number of chunks.
chunked = Tensor.arange(11).chunk(6)
print("\n".join([repr(x.numpy()) for x in chunked]))
array([0, 1], dtype=int32)
array([2, 3], dtype=int32)
array([4, 5], dtype=int32)
array([6, 7], dtype=int32)
array([8, 9], dtype=int32)
array([10], dtype=int32)
chunked = Tensor.arange(12).chunk(6)
print("\n".join([repr(x.numpy()) for x in chunked]))
array([0, 1], dtype=int32)
array([2, 3], dtype=int32)
array([4, 5], dtype=int32)
array([6, 7], dtype=int32)
array([8, 9], dtype=int32)
array([10, 11], dtype=int32)
chunked = Tensor.arange(13).chunk(6)
print("\n".join([repr(x.numpy()) for x in chunked]))
array([0, 1, 2], dtype=int32)
array([3, 4, 5], dtype=int32)
array([6, 7, 8], dtype=int32)
array([ 9, 10, 11], dtype=int32)
array([12], dtype=int32)
Source code in tinygrad/tensor.py
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 |
|
unfold
¤
Unfolds the tensor along dimension dim
into overlapping windows.
Each window has length size
and begins every step
elements of self
.
Returns the input tensor with dimension dim
replaced by dims (n_windows, size)
where n_windows = (self.shape[dim] - size) // step + 1
.
unfolded = Tensor.arange(8).unfold(0,2,2)
print("\n".join([repr(x.numpy()) for x in unfolded]))
array([0, 1], dtype=int32)
array([2, 3], dtype=int32)
array([4, 5], dtype=int32)
array([6, 7], dtype=int32)
unfolded = Tensor.arange(27).reshape(3,3,3).unfold(-1,2,3)
print("\n".join([repr(x.numpy()) for x in unfolded]))
array([[[0, 1]],
[[3, 4]],
[[6, 7]]], dtype=int32)
array([[[ 9, 10]],
[[12, 13]],
[[15, 16]]], dtype=int32)
array([[[18, 19]],
[[21, 22]],
[[24, 25]]], dtype=int32)
Source code in tinygrad/tensor.py
1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 |
|
meshgrid
¤
Generates coordinate matrices from coordinate vectors. Input tensors can be scalars or 1D tensors.
indexing
determines how the output grids are aligned.
ij
indexing follows matrix-style indexing and xy
indexing follows Cartesian-style indexing.
x, y = Tensor([1, 2, 3]), Tensor([4, 5, 6])
grid_x, grid_y = x.meshgrid(y)
print(grid_x.numpy())
print(grid_y.numpy())
[[1 1 1]
[2 2 2]
[3 3 3]]
[[4 5 6]
[4 5 6]
[4 5 6]]
grid_x, grid_y = x.meshgrid(y, indexing="xy")
print(grid_x.numpy())
print(grid_y.numpy())
[[1 2 3]
[1 2 3]
[1 2 3]]
[[4 4 4]
[5 5 5]
[6 6 6]]
Source code in tinygrad/tensor.py
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 |
|
squeeze
¤
Returns a tensor with specified dimensions of input of size 1 removed.
If dim
is not specified, all dimensions with size 1 are removed.
t = Tensor.zeros(2, 1, 2, 1, 2)
print(t.squeeze().shape)
(2, 2, 2)
print(t.squeeze(0).shape)
(2, 1, 2, 1, 2)
print(t.squeeze(1).shape)
(2, 2, 1, 2)
Source code in tinygrad/tensor.py
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 |
|
unsqueeze
¤
Returns a tensor with a new dimension of size 1 inserted at the specified dim
.
t = Tensor([1, 2, 3, 4])
print(t.unsqueeze(0).numpy())
[[1 2 3 4]]
print(t.unsqueeze(1).numpy())
[[1]
[2]
[3]
[4]]
Source code in tinygrad/tensor.py
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 |
|
transpose
¤
transpose(dim0=1, dim1=0) -> Tensor
Returns a tensor that is a transposed version of the original tensor.
The given dimensions dim0
and dim1
are swapped.
t = Tensor.arange(6).reshape(2, 3)
print(t.numpy())
[[0 1 2]
[3 4 5]]
print(t.transpose(0, 1).numpy())
[[0 3]
[1 4]
[2 5]]
Source code in tinygrad/tensor.py
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 |
|
flatten
¤
flatten(start_dim=0, end_dim=-1) -> Tensor
Flattens the tensor by reshaping it into a one-dimensional tensor.
If start_dim
or end_dim
are passed, only dimensions starting with start_dim
and ending with end_dim
are flattened.
t = Tensor.arange(8).reshape(2, 2, 2)
print(t.flatten().numpy())
[0 1 2 3 4 5 6 7]
print(t.flatten(start_dim=1).numpy())
[[0 1 2 3]
[4 5 6 7]]
Source code in tinygrad/tensor.py
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 |
|
unflatten
¤
Unflattens dimension dim
of the tensor into multiple dimensions specified by sizes
. Tensor.flatten()
is the inverse of this function.
print(Tensor.ones(3, 4, 1).unflatten(1, (2, 2)).shape)
(3, 2, 2, 1)
print(Tensor.ones(3, 4, 1).unflatten(1, (-1, 2)).shape)
(3, 2, 2, 1)
print(Tensor.ones(5, 12, 3).unflatten(-2, (2, 2, 3, 1, 1)).shape)
(5, 2, 2, 3, 1, 1, 3)
Source code in tinygrad/tensor.py
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 |
|
diag
¤
diag() -> Tensor
Returns a 2-D square tensor with the elements of input as the main diagonal.
print(Tensor([1, 2, 3]).diag().numpy())
[[1 0 0]
[0 2 0]
[0 0 3]]
Source code in tinygrad/tensor.py
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 |
|
roll
¤
Rolls the tensor along specified dimension(s). The rolling operation is circular, meaning that elements that go beyond the edge are wrapped around to the beginning of the dimension.
t = Tensor.arange(4)
print(t.roll(shifts=1, dims=0).numpy())
[3 0 1 2]
print(t.roll(shifts=-1, dims=0).numpy())
[1 2 3 0]
Source code in tinygrad/tensor.py
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 |
|
rearrange
¤
Rearranges input according to formula
See: https://einops.rocks/api/rearrange/
x = Tensor([[1, 2], [3, 4]])
print(Tensor.rearrange(x, "batch channel -> (batch channel)").numpy())
[1 2 3 4]
Source code in tinygrad/tensor.py
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 |
|