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/tensor.py
3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 |
|
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/tensor.py
4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 |
|
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/tensor.py
4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 |
|
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/tensor.py
4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 |
|
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/tensor.py
4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 |
|
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
305 306 307 308 309 310 311 312 313 314 315 316 |
|
item
¤
item() -> ConstType
Returns the value of this tensor as a standard Python number.
t = Tensor(42)
print(t.item())
42
Source code in tinygrad/tensor.py
318 319 320 321 322 323 324 325 326 327 328 |
|
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
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
numpy
¤
numpy() -> 'np.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
348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
tinygrad ops¤
schedule_with_vars
¤
Creates the schedule needed to realize these Tensor(s), with Variables.
Note
A Tensor can only be scheduled once.
Source code in tinygrad/tensor.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
schedule
¤
schedule(*lst: Tensor) -> list[ScheduleItem]
Creates the schedule needed to realize these Tensor(s).
Source code in tinygrad/tensor.py
261 262 263 264 265 |
|
realize
¤
Triggers the computation needed to create these Tensor(s).
Source code in tinygrad/tensor.py
267 268 269 270 |
|
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
272 273 274 275 276 277 278 279 |
|
assign
¤
assign(x) -> Tensor
Source code in tinygrad/tensor.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
|
detach
¤
detach() -> Tensor
Returns a new tensor with the same data as this tensor, but detached from the autograd graph.
Source code in tinygrad/tensor.py
296 297 298 299 300 |
|
clone
¤
clone() -> Tensor
Creates a clone of this tensor allocating a separate buffer for the data.
Source code in tinygrad/tensor.py
363 364 365 366 367 368 369 |
|
to
¤
Moves the tensor to the given device.
Source code in tinygrad/tensor.py
371 372 373 374 375 376 377 378 379 380 |
|
to_
¤
Moves the tensor to the given device in place.
Source code in tinygrad/tensor.py
382 383 384 385 386 387 388 |
|
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=((0, 2), (UOp(Ops.MUL, dtypes.int, arg=None, src=(
UOp(Ops.DEFINE_VAR, dtypes.int, arg=('_device_num', 0, 1), src=()),
UOp(Ops.CONST, dtypes.int, arg=2, src=()),)), UOp(Ops.ADD, dtypes.int, arg=None, src=(
UOp(Ops.MUL, dtypes.int, arg=None, src=(
UOp(Ops.DEFINE_VAR, dtypes.int, arg=('_device_num', 0, 1), src=()),
x2:=UOp(Ops.CONST, dtypes.int, arg=2, src=()),)),
x2,)))), src=(
UOp(Ops.COPY, dtypes.float, arg=None, src=(
UOp(Ops.RESHAPE, dtypes.float, arg=(2, 4), src=(
UOp(Ops.BUFFER, dtypes.float, arg=8, src=(
UOp(Ops.UNIQUE, dtypes.void, arg=1310, src=()),
UOp(Ops.DEVICE, dtypes.void, arg='CPU', src=()),)),)),
UOp(Ops.DEVICE, dtypes.void, arg=('CPU', 'CPU'), src=()),)),)),))
Source code in tinygrad/tensor.py
390 391 392 393 394 395 396 397 398 399 400 401 402 |
|
shard_
¤
Shards the tensor across the given devices in place.
Source code in tinygrad/tensor.py
404 405 406 407 408 |
|
contiguous
¤
contiguous() -> Tensor
Returns a contiguous tensor.
Source code in tinygrad/tensor.py
2866 2867 2868 2869 2870 |
|
contiguous_backward
¤
contiguous_backward() -> Tensor
Inserts a contiguous operation in the backward pass.
Source code in tinygrad/tensor.py
2881 2882 2883 2884 2885 |
|
Gradient¤
gradient
¤
gradient(
*targets: Tensor,
gradient: Tensor | None = None,
materialize_grads=False
) -> list[Tensor]
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
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 |
|
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], requires_grad=True)
t.sum().backward()
print(t.grad.numpy())
[1. 1. 1. 1.]
Source code in tinygrad/tensor.py
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 |
|