Movement
Movement (low level)¤
view
¤
view(shape, *args) -> Self
.view is an alias for .reshape.
Source code in tinygrad/mixin/movement.py
256 257 258 | |
reshape
¤
reshape(shape, *args) -> Self
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/mixin/movement.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | |
expand
¤
expand(shape, *args) -> Self
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/mixin/movement.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
permute
¤
permute(order, *args) -> Self
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/mixin/movement.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
flip
¤
flip(axis, *args) -> Self
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/mixin/movement.py
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
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/mixin/movement.py
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | |
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.
paddinglength 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). paddingmust 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
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 | |
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
tupleorlisthere also works.print(t[Tensor([2, 0, 1]), Tensor([1, 2, 3])].numpy())[9 2 7] -
NoneIndexing: 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
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 | |
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/mixin/__init__.py
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 | |
cat
¤
Concatenates self with other tensors 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/mixin/__init__.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
stack
¤
Concatenates self with other tensors 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/mixin/__init__.py
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 | |
repeat
¤
repeat(repeats, *args) -> Self
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/mixin/movement.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
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/mixin/movement.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
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/mixin/movement.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 | |
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/mixin/movement.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
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/mixin/movement.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | |
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/mixin/movement.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
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/mixin/movement.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
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/mixin/movement.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
transpose
¤
transpose(dim0=1, dim1=0) -> Self
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/mixin/movement.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
flatten
¤
flatten(start_dim=0, end_dim=-1) -> Self
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/mixin/movement.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
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/mixin/movement.py
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
diag
¤
diag() -> Self
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/mixin/movement.py
474 475 476 477 478 479 480 481 482 483 | |
diagonal
¤
Returns a view of the diagonal elements with respect to dim1 and dim2.
offset controls which diagonal: 0 is main, positive is above, negative is below.
t = Tensor.arange(9).reshape(3, 3)
print(t.numpy())
[[0 1 2]
[3 4 5]
[6 7 8]]
print(t.diagonal().numpy())
[0 4 8]
print(t.diagonal(offset=1).numpy())
[1 5]
Source code in tinygrad/mixin/movement.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
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/mixin/movement.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
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/mixin/movement.py
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |