Skip to content

Creation

Creation (basic)¤

empty classmethod ¤

empty(
    *shape,
    device: str | tuple[str, ...] | None = None,
    dtype: DTypeLike | None = None
) -> Self

Creates an empty tensor with the given shape.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor.

t = Tensor.empty(2, 3)
print(t.shape)
(2, 3)
Source code in tinygrad/mixin/creation.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
@classmethod
def empty(cls, *shape, device:str|tuple[str, ...]|None=None, dtype:DTypeLike|None=None) -> Self:
  """
  Creates an empty tensor with the given shape.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  t = Tensor.empty(2, 3)
  print(t.shape)
  ```
  """
  from tinygrad.uop.ops import UOp, to_max_shape
  from tinygrad.device import canonicalize_device
  dt = to_dtype(dtype) if dtype is not None else dtypes.default_float
  new_shape = argfix(*shape)
  max_shape = to_max_shape(new_shape)
  u = UOp.new_buffer(canonicalize_device(device), prod(max_shape), dt).reshape(max_shape).shrink_to(new_shape)
  return cls._wrap_uop(u)

zeros classmethod ¤

zeros(*shape, **kwargs) -> Self

Creates a tensor with the given shape, filled with zeros.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

print(Tensor.zeros(2, 3).numpy())
[[0. 0. 0.]
 [0. 0. 0.]]
print(Tensor.zeros(2, 3, dtype=dtypes.int32).numpy())
[[0 0 0]
 [0 0 0]]

Source code in tinygrad/mixin/creation.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@classmethod
def zeros(cls, *shape, **kwargs) -> Self:
  """
  Creates a tensor with the given shape, filled with zeros.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.zeros(2, 3).numpy())
  ```
  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.zeros(2, 3, dtype=dtypes.int32).numpy())
  ```
  """
  return cls.full(argfix(*shape), 0.0, **kwargs)

ones classmethod ¤

ones(*shape, **kwargs) -> Self

Creates a tensor with the given shape, filled with ones.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

print(Tensor.ones(2, 3).numpy())
[[1. 1. 1.]
 [1. 1. 1.]]
print(Tensor.ones(2, 3, dtype=dtypes.int32).numpy())
[[1 1 1]
 [1 1 1]]

Source code in tinygrad/mixin/creation.py
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
@classmethod
def ones(cls, *shape, **kwargs) -> Self:
  """
  Creates a tensor with the given shape, filled with ones.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.ones(2, 3).numpy())
  ```
  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.ones(2, 3, dtype=dtypes.int32).numpy())
  ```
  """
  return cls.full(argfix(*shape), 1.0, **kwargs)

full classmethod ¤

full(
    shape: tuple[sint, ...],
    fill_value: ConstType | UOp,
    dtype: DTypeLike | None = None,
    device: str | tuple[str, ...] | None = None,
    buffer=True,
) -> Self

Creates a tensor with the given shape, filled with the given value.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Pass buffer=False to get a broadcast const value instead of a materialized buffer.

print(Tensor.full((2, 3), 42).numpy())
[[42 42 42]
 [42 42 42]]
print(Tensor.full((2, 3), False).numpy())
[[False False False]
 [False False False]]

Source code in tinygrad/mixin/creation.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
@classmethod
def full(cls, shape:'tuple[sint, ...]', fill_value:'ConstType|UOp', dtype:DTypeLike|None=None,
         device:str|tuple[str, ...]|None=None, buffer=True) -> Self:
  """
  Creates a tensor with the given shape, filled with the given value.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Pass `buffer=False` to get a broadcast const value instead of a materialized buffer.

  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.full((2, 3), 42).numpy())
  ```
  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.full((2, 3), False).numpy())
  ```
  """
  # TODO: enable this check
  # if not buffer: assert device is None, "buffer=False does not support device specification"
  from tinygrad.uop.ops import UOp
  new_shape = argfix(shape)
  dt = to_dtype(dtype) if dtype is not None else None
  val = cls.const(dt or (fill_value.dtype if isinstance(fill_value, UOp) else dtypes.from_py(fill_value)), fill_value)
  val = val.reshape((1,)*len(new_shape)).expand(new_shape)
  return val.clone(device=device) if buffer else val

arange classmethod ¤

arange(
    start, stop=None, step=1, dtype: DTypeLike | None = None
) -> Self

Returns a 1-D tensor of size ceil((stop - start) / step) with values from [start, stop), with spacing between values given by step.

If stop is not specified, values are generated from [0, start) with the given step.

If stop is specified, values are generated from [start, stop) with the given step.

print(Tensor.arange(5).numpy())
[0 1 2 3 4]
print(Tensor.arange(5, 10).numpy())
[5 6 7 8 9]
print(Tensor.arange(5, 10, 2).numpy())
[5 7 9]
print(Tensor.arange(5.5, 10, 2).numpy())
[5.5 7.5 9.5]

Source code in tinygrad/mixin/__init__.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
@classmethod
def arange(cls, start, stop=None, step=1, dtype:DTypeLike|None=None) -> Self:
  """
  Returns a 1-D tensor of size `ceil((stop - start) / step)` with values from `[start, stop)`, with spacing between values given by `step`.

  If `stop` is not specified, values are generated from `[0, start)` with the given `step`.

  If `stop` is specified, values are generated from `[start, stop)` with the given `step`.

  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.arange(5).numpy())
  ```
  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.arange(5, 10).numpy())
  ```
  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.arange(5, 10, 2).numpy())
  ```
  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.arange(5.5, 10, 2).numpy())
  ```
  """
  if stop is None: stop, start = start, 0
  if dtype is None: dtype = dtypes.default_float if any(isinstance(x, float) for x in (start, stop, step)) else dtypes.default_int
  lo, hi = (start, stop-step) if step > 0 else (stop-step, start)
  if lo < (dt:=to_dtype(dtype)).min or dt.max < hi: raise OverflowError(f"arange [{start}, {stop}) is not representable in dtype {dtype}")
  # NOTE: this matches numpy, torch raises RuntimeError if stop-start and step have different signs
  if (output_len:=ceildiv(stop-start, step)) <= 0: return cls.full((0,), 0, dtype=dtype, buffer=False)
  return (cls.full((output_len,), step, dtype=dtype, buffer=False)._cumalu(0, Ops.ADD) + (start - step)).cast(dtype)

linspace classmethod ¤

linspace(
    start: int | float,
    stop: int | float,
    steps: int,
    dtype: DTypeLike | None = None,
) -> Self

Returns a 1-D tensor of steps evenly spaced values from start to stop, inclusive.

print(Tensor.linspace(0, 10, 5).numpy())
[ 0.   2.5  5.   7.5 10. ]
print(Tensor.linspace(-1, 1, 5).numpy())
[-1.  -0.5  0.   0.5  1. ]

Source code in tinygrad/mixin/__init__.py
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
@classmethod
def linspace(cls, start:int|float, stop:int|float, steps:int, dtype:DTypeLike|None=None) -> Self:
  """
  Returns a 1-D tensor of `steps` evenly spaced values from `start` to `stop`, inclusive.

  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.linspace(0, 10, 5).numpy())
  ```
  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.linspace(-1, 1, 5).numpy())
  ```
  """
  if steps < 0: raise ValueError("number of steps must be non-negative")
  if (dtype := to_dtype(dtype or dtypes.default_float)) == dtypes.bool: raise ValueError("linspace with bool dtype is not supported")
  if steps == 1: return cls.full((1,), start, dtype=dtype, buffer=False)
  return (start + cls.arange(steps, dtype=dtypes.default_float) * ((stop - start) / (steps - 1))).cast(dtype)

eye classmethod ¤

eye(
    n: int,
    m: int | None = None,
    dtype: DTypeLike | None = None,
) -> Self

Returns a 2-D tensor with n rows and m columns, with ones on the diagonal and zeros elsewhere.

print(Tensor.eye(3).numpy())
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
print(Tensor.eye(2, 4).numpy())
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]]
Source code in tinygrad/mixin/__init__.py
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
@classmethod
def eye(cls, n:int, m:int|None=None, dtype:DTypeLike|None=None) -> Self:
  """
  Returns a 2-D tensor with `n` rows and `m` columns, with ones on the diagonal and zeros elsewhere.

  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.eye(3).numpy())
  ```

  ```python exec="true" source="above" session="tensor" result="python"
  print(Tensor.eye(2, 4).numpy())
  ```
  """
  m_ = n if m is None else m
  if n < 0 or m_ < 0: raise ValueError(f"cannot have negative {n=}, {m_=}")
  out_dtype = to_dtype(dtype) if dtype is not None else dtypes.default_float
  return cls.arange(n).unsqueeze(-1).eq(cls.arange(m_)).cast(out_dtype)

full_like ¤

full_like(
    fill_value: ConstType,
    dtype: DTypeLike | None = None,
    device: str | tuple[str, ...] | None = None,
    buffer=True,
) -> Self

Creates a tensor with the same shape as self, filled with the given value. If dtype is not specified, the dtype of self is used.

You can pass in the device keyword argument to control device of the tensor. Pass buffer=False to get a broadcast const value instead of a materialized buffer.

t = Tensor.ones(2, 3)
print(Tensor.full_like(t, 42).numpy())
[[42. 42. 42.]
 [42. 42. 42.]]
Source code in tinygrad/mixin/creation.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
def full_like(self, fill_value:ConstType, dtype:DTypeLike|None=None, device:str|tuple[str, ...]|None=None, buffer=True) -> Self:
  """
  Creates a tensor with the same shape as `self`, filled with the given value.
  If `dtype` is not specified, the dtype of `self` is used.

  You can pass in the `device` keyword argument to control device of the tensor.
  Pass `buffer=False` to get a broadcast const value instead of a materialized buffer.

  ```python exec="true" source="above" session="tensor" result="python"
  t = Tensor.ones(2, 3)
  print(Tensor.full_like(t, 42).numpy())
  ```
  """
  if isinstance(self.device, tuple):
    if device is not None: raise RuntimeError("cannot specify `device` on `*_like` of a multi device tensor")
    return self._multi_like(lambda shape, dev: type(self).full(shape, fill_value, dtype=dtype or self.dtype, device=dev, buffer=buffer))
  return type(self).full(self.shape, fill_value, dtype=dtype or self.dtype, device=self.device if device is None else device, buffer=buffer)

zeros_like ¤

zeros_like(**kwargs) -> Self

Creates a tensor with the same shape as self, filled with zeros.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor.

t = Tensor.ones(2, 3)
print(Tensor.zeros_like(t).numpy())
[[0. 0. 0.]
 [0. 0. 0.]]
Source code in tinygrad/mixin/creation.py
120
121
122
123
124
125
126
127
128
129
130
131
def zeros_like(self, **kwargs) -> Self:
  """
  Creates a tensor with the same shape as `self`, filled with zeros.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  t = Tensor.ones(2, 3)
  print(Tensor.zeros_like(t).numpy())
  ```
  """
  return self.full_like(0, **kwargs)

ones_like ¤

ones_like(**kwargs) -> Self

Creates a tensor with the same shape as self, filled with ones.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor.

t = Tensor.zeros(2, 3)
print(Tensor.ones_like(t).numpy())
[[1. 1. 1.]
 [1. 1. 1.]]
Source code in tinygrad/mixin/creation.py
150
151
152
153
154
155
156
157
158
159
160
161
def ones_like(self, **kwargs) -> Self:
  """
  Creates a tensor with the same shape as `self`, filled with ones.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  t = Tensor.zeros(2, 3)
  print(Tensor.ones_like(t).numpy())
  ```
  """
  return self.full_like(1, **kwargs)

Creation (external)¤

from_blob staticmethod ¤

from_blob(
    ptr: int, shape: tuple[int, ...], **kwargs
) -> Tensor

Exposes the pointer as a Tensor without taking ownership of the original data. The pointer must remain valid for the entire lifetime of the created Tensor.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Source code in tinygrad/tensor.py
354
355
356
357
358
359
360
361
362
363
364
365
366
@staticmethod
def from_blob(ptr:int, shape:tuple[int, ...], **kwargs) -> Tensor:
  """
  Exposes the pointer as a Tensor without taking ownership of the original data.
  The pointer must remain valid for the entire lifetime of the created Tensor.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.
  """
  r = Tensor.empty(*shape, **kwargs)
  assert isinstance(r.device, str)
  cast(Buffer, r.uop.buffer).allocate(external_ptr=ptr)
  return r

from_url staticmethod ¤

from_url(
    url: str, gunzip: bool = False, **kwargs
) -> Tensor

Creates a Tensor from a URL.

This is the preferred way to access Internet resources. It currently returns a DISK Tensor, but in the future it may return an HTTP Tensor. This also will soon become lazy (when possible) and not print progress without DEBUG.

The gunzip flag will gzip extract the resource and return an extracted Tensor.

Source code in tinygrad/tensor.py
368
369
370
371
372
373
374
375
376
377
378
379
@staticmethod
def from_url(url:str, gunzip:bool=False, **kwargs) -> Tensor:
  """
  Creates a Tensor from a URL.

  This is the preferred way to access Internet resources.
  It currently returns a DISK Tensor, but in the future it may return an HTTP Tensor.
  This also will soon become lazy (when possible) and not print progress without DEBUG.

  The `gunzip` flag will gzip extract the resource and return an extracted Tensor.
  """
  return Tensor(fetch(url, gunzip=gunzip), **kwargs)

Creation (random)¤

manual_seed staticmethod ¤

manual_seed(seed=0) -> None

Sets the seed for random operations.

Tensor.manual_seed(42)
print(Tensor.rand(5).numpy())
print(Tensor.rand(5).numpy())
[0.381  0.0098 0.1128 0.1177 0.5054]
[0.8984 0.9686 0.5969 0.9117 0.9869]
Tensor.manual_seed(42)  # reset to the same seed
print(Tensor.rand(5).numpy())
print(Tensor.rand(5).numpy())
[0.381  0.0098 0.1128 0.1177 0.5054]
[0.8984 0.9686 0.5969 0.9117 0.9869]

Source code in tinygrad/tensor.py
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
@staticmethod
def manual_seed(seed=0) -> None:
  """
  Sets the seed for random operations.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.rand(5).numpy())
  print(Tensor.rand(5).numpy())
  ```
  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)  # reset to the same seed
  print(Tensor.rand(5).numpy())
  print(Tensor.rand(5).numpy())
  ```
  """
  Tensor._seed, Tensor._device_seeds, Tensor._device_rng_counters = seed, {}, {}

rand classmethod ¤

rand(
    *shape,
    device: str | None = None,
    dtype: DTypeLike | None = None,
    contiguous: bool = True
) -> Self

Creates a tensor with the given shape, filled with random values from a uniform distribution over the interval [0, 1).

You can pass in dtype and device keyword arguments to control the data type and device of the tensor.

Tensor.manual_seed(42)
t = Tensor.rand(2, 3)
print(t.numpy())
[[0.381  0.0098 0.1128]
 [0.1177 0.5054 0.3721]]
Source code in tinygrad/mixin/rand.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@classmethod
def rand(cls, *shape, device:str|None=None, dtype:DTypeLike|None=None, contiguous:bool=True) -> Self:
  """
  Creates a tensor with the given shape, filled with random values from a uniform distribution over the interval `[0, 1)`.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  t = Tensor.rand(2, 3)
  print(t.numpy())
  ```
  """
  dt = to_dtype(dtype or dtypes.default_float)
  if not dtypes.is_float(dt): raise ValueError(f"rand only supports float dtypes, got {dt}")
  if not all_int(shape:=argfix(*shape)) or not all(s >= 0 for s in shape): raise ValueError(f"invalid input {shape=}")
  if device is not None and not isinstance(device, str): raise ValueError(f"rand only supports single device, got {device=}")
  device = cast(str, canonicalize_device(device))
  key, counter = cls._next_counter(device, ceildiv(prod(shape) * dt.itemsize, 4))
  return cls._rand(key, counter, shape, dt, contiguous=contiguous)

rand_like ¤

rand_like(**kwargs) -> Self

Creates a tensor with the same shape and sharding as self, filled with random values from a uniform distribution over the interval [0, 1).

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

t = Tensor.ones(2, 3)
print(Tensor.rand_like(t).numpy())
[[0.2103 0.611  0.1345]
 [0.0131 0.368  0.9245]]
Source code in tinygrad/mixin/rand.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
def rand_like(self, **kwargs) -> Self:
  """
  Creates a tensor with the same shape and sharding as `self`, filled with random values from a uniform distribution over the interval `[0, 1)`.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  t = Tensor.ones(2, 3)
  print(Tensor.rand_like(t).numpy())
  ```
  """
  if isinstance(self.device, tuple):
    if kwargs.pop("device", None) is not None: raise RuntimeError("cannot specify `device` on `*_like` of a multi device tensor")
    dtype = kwargs.pop("dtype", self.dtype)
    return self._multi_like(lambda shape, dev: type(self).rand(*shape, dtype=dtype, device=dev, **kwargs))
  return type(self).rand(*self.shape, device=kwargs.pop("device", self.device), dtype=kwargs.pop("dtype", self.dtype), **kwargs)

randn classmethod ¤

randn(
    *shape, dtype: DTypeLike | None = None, **kwargs
) -> Self

Creates a tensor with the given shape, filled with random values from a normal distribution with mean 0 and standard deviation 1. If dtype is not specified, the default type is used.

You can pass in the device keyword argument to control device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Tensor.manual_seed(42)
print(Tensor.randn(2, 3).numpy())
[[ 1.9576 -0.1859  1.6404]
 [-0.7647 -0.8695 -0.4379]]
Source code in tinygrad/mixin/rand.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@classmethod
def randn(cls, *shape, dtype:DTypeLike|None=None, **kwargs) -> Self:
  """
  Creates a tensor with the given shape, filled with random values from a normal distribution with mean `0` and standard deviation `1`.
  If `dtype` is not specified, the default type is used.

  You can pass in the `device` keyword argument to control device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.randn(2, 3).numpy())
  ```
  """
  return cls.empty(*shape, **kwargs).randn_like(dtype=dtype)  # type: ignore[attr-defined]

randn_like ¤

randn_like(
    dtype: DTypeLike | None = None, **kwargs
) -> Self

Creates a tensor with the same shape and sharding as self, filled with random values from a normal distribution with mean 0 and variance 1.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

t = Tensor.ones(2, 3)
print(Tensor.randn_like(t).numpy())
[[-0.7382  1.5164 -0.3065]
 [-0.7862  0.5411 -0.4394]]
Source code in tinygrad/mixin/rand.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def randn_like(self, dtype:DTypeLike|None=None, **kwargs) -> Self:
  """
  Creates a tensor with the same shape and sharding as `self`, filled with random values from a normal distribution with mean 0 and variance 1.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  t = Tensor.ones(2, 3)
  print(Tensor.randn_like(t).numpy())
  ```
  """
  src = self.stack(self).rand_like(**{**kwargs, "dtype": dtypes.float32})
  # https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
  return src[0].mul(2*math.pi).cos().mul((1 - src[1]).log().mul(-2).sqrt()).cast(to_dtype(dtype or self.dtype))

randint classmethod ¤

randint(
    *shape, low=0, high=10, dtype=int32, **kwargs
) -> Self

Creates a tensor with the given shape, filled with random integer values generated uniformly from the interval [low, high). Requires low < high. If dtype is not specified, the default type is used.

You can pass in the device keyword argument to control device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Tensor.manual_seed(42)
print(Tensor.randint(2, 3, low=5, high=10).numpy())
[[6 5 5]
 [5 7 6]]
Source code in tinygrad/mixin/rand.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@classmethod
def randint(cls, *shape, low=0, high=10, dtype=dtypes.int32, **kwargs) -> Self:
  """
  Creates a tensor with the given shape, filled with random integer values generated uniformly from the interval `[low, high)`.
  Requires `low < high`. If `dtype` is not specified, the default type is used.

  You can pass in the `device` keyword argument to control device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.randint(2, 3, low=5, high=10).numpy())
  ```
  """
  if not all_int([low, high]): raise TypeError(f"{low=} and {high=} must be integers")
  if not dtypes.is_int(dtype := to_dtype(dtype)): raise TypeError(f"{dtype=} must be int")
  if low >= high: raise ValueError(f"Tensor.randint requires low < high, got {low=}, {high=}")
  return cls.uniform(*shape, low=low, high=high, dtype=dtype, **kwargs)

randperm classmethod ¤

randperm(
    n: int, device=None, dtype=int32, **kwargs
) -> Self

Returns a tensor with a random permutation of integers from 0 to n-1.

Tensor.manual_seed(42)
print(Tensor.randperm(6).numpy())
[1 2 3 5 0 4]
Source code in tinygrad/mixin/rand.py
238
239
240
241
242
243
244
245
246
247
248
@classmethod
def randperm(cls, n:int, device=None, dtype=dtypes.int32, **kwargs) -> Self:
  """
  Returns a tensor with a random permutation of integers from `0` to `n-1`.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.randperm(6).numpy())
  ```
  """
  return cls.rand(n, device=device, **kwargs).argsort().cast(dtype)

normal classmethod ¤

normal(*shape, mean=0.0, std=1.0, **kwargs) -> Self

Creates a tensor with the given shape, filled with random values from a normal distribution with the given mean and standard deviation std. Requires std >= 0.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Tensor.manual_seed(42)
print(Tensor.normal(2, 3, mean=10, std=2).numpy())
[[13.9153  9.6281 13.2808]
 [ 8.4707  8.261   9.1242]]
Source code in tinygrad/mixin/rand.py
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
@classmethod
def normal(cls, *shape, mean=0.0, std=1.0, **kwargs) -> Self:
  """
  Creates a tensor with the given shape, filled with random values from a normal distribution with the given `mean` and standard deviation `std`.
  Requires `std >= 0`.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.normal(2, 3, mean=10, std=2).numpy())
  ```
  """
  if std < 0: raise ValueError(f"Tensor.normal requires std >= 0, got {std=}")
  return std * cls.randn(*shape, **kwargs) + mean

uniform classmethod ¤

uniform(
    *shape,
    low=0.0,
    high=1.0,
    dtype: DTypeLike | None = None,
    **kwargs
) -> Self

Creates a tensor with the given shape, filled with random values from a uniform distribution over the interval [low, high). Requires low < high.

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Tensor.manual_seed(42)
print(Tensor.uniform(2, 3, low=2, high=10).numpy())
[[5.0483 2.0782 2.9024]
 [2.9416 6.0429 4.9769]]
Source code in tinygrad/mixin/rand.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
@classmethod
def uniform(cls, *shape, low=0.0, high=1.0, dtype:DTypeLike|None=None, **kwargs) -> Self:
  """
  Creates a tensor with the given shape, filled with random values from a uniform distribution over the interval `[low, high)`.
  Requires `low < high`.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.uniform(2, 3, low=2, high=10).numpy())
  ```
  """
  if not all_int(shape:=argfix(*shape)) or not all(s >= 0 for s in shape): raise ValueError(f"invalid input {shape=}")
  if low >= high: raise ValueError(f"Tensor.uniform requires low < high, got {low=}, {high=}")
  return ((high-low) * cls.rand(*shape, **kwargs)).cast(dtype or dtypes.default_float) + low

scaled_uniform classmethod ¤

scaled_uniform(*shape, **kwargs) -> Self

Creates a tensor with the given shape, filled with random values from a uniform distribution over the interval [-prod(shape)**-0.5, prod(shape)**-0.5).

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Tensor.manual_seed(42)
print(Tensor.scaled_uniform(2, 3).numpy())
[[-0.0971 -0.4003 -0.3161]
 [-0.3121  0.0044 -0.1044]]
Source code in tinygrad/mixin/rand.py
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
@classmethod
def scaled_uniform(cls, *shape, **kwargs) -> Self:
  """
  Creates a tensor with the given shape, filled with random values from a uniform distribution
  over the interval `[-prod(shape)**-0.5, prod(shape)**-0.5)`.

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.scaled_uniform(2, 3).numpy())
  ```
  """
  return cls.uniform(*shape, low=-1.0, high=1.0, **kwargs).mul(prod(argfix(*shape))**-0.5)

glorot_uniform classmethod ¤

glorot_uniform(*shape, **kwargs) -> Self

https://www.tensorflow.org/api_docs/python/tf/keras/initializers/GlorotUniform

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Tensor.manual_seed(42)
print(Tensor.glorot_uniform(2, 3).numpy())
[[-0.2606 -1.074  -0.8483]
 [-0.8376  0.0117 -0.2802]]
Source code in tinygrad/mixin/rand.py
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
@classmethod
def glorot_uniform(cls, *shape, **kwargs) -> Self:
  """
  <https://www.tensorflow.org/api_docs/python/tf/keras/initializers/GlorotUniform>

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.glorot_uniform(2, 3).numpy())
  ```
  """
  bound = (6 / (argfix(*shape)[0]+prod(argfix(*shape)[1:]))) ** 0.5
  return cls.uniform(*shape, low=-bound, high=bound, **kwargs)

kaiming_uniform classmethod ¤

kaiming_uniform(*shape, a: float = 0.01, **kwargs) -> Self

https://pytorch.org/docs/stable/_modules/torch/nn/init.html#kaiming_uniform_

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Tensor.manual_seed(42)
print(Tensor.kaiming_uniform(2, 3).numpy())
[[-0.3364 -1.3865 -1.0951]
 [-1.0813  0.0152 -0.3617]]
Source code in tinygrad/mixin/rand.py
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
@classmethod
def kaiming_uniform(cls, *shape, a:float = 0.01, **kwargs) -> Self:
  """
  <https://pytorch.org/docs/stable/_modules/torch/nn/init.html#kaiming_uniform_>

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.kaiming_uniform(2, 3).numpy())
  ```
  """
  bound = (6 / (1 + a ** 2) / prod(argfix(*shape)[1:])) ** 0.5
  return cls.uniform(*shape, low=-bound, high=bound, **kwargs)

kaiming_normal classmethod ¤

kaiming_normal(*shape, a: float = 0.01, **kwargs) -> Self

https://pytorch.org/docs/stable/_modules/torch/nn/init.html#kaiming_normal_

You can pass in dtype and device keyword arguments to control the data type and device of the tensor. Additionally, all other keyword arguments are passed to the constructor of the tensor.

Tensor.manual_seed(42)
print(Tensor.kaiming_normal(2, 3).numpy())
[[ 1.5983 -0.1518  1.3393]
 [-0.6243 -0.7099 -0.3575]]
Source code in tinygrad/mixin/rand.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
@classmethod
def kaiming_normal(cls, *shape, a:float = 0.01, **kwargs) -> Self:
  """
  <https://pytorch.org/docs/stable/_modules/torch/nn/init.html#kaiming_normal_>

  You can pass in `dtype` and `device` keyword arguments to control the data type and device of the tensor.
  Additionally, all other keyword arguments are passed to the constructor of the tensor.

  ```python exec="true" source="above" session="tensor" result="python"
  Tensor.manual_seed(42)
  print(Tensor.kaiming_normal(2, 3).numpy())
  ```
  """
  std = (2 / (1 + a ** 2) / prod(argfix(*shape)[1:])) ** 0.5
  return cls.normal(*shape, mean=0.0, std=std, **kwargs)