tinygrad shape.view
Note
You likely want the upstream tinygrad, not tinygrab. Tinygrab contains AI generated docstrings for a tinygrad snapshot. Upstream: https://tinygrad.org
- class tinygrad.shape.view.View(shape: Tuple[Node | int, ...], strides: Tuple[Node | int, ...], offset: Node | int, mask: Tuple[Tuple[Node | int, Node | int], ...] | None, contiguous: bool)[source]
Bases:
object
A data class representing a view of a multi-dimensional array.
- shape
The shape of the array.
- Type:
Tuple[sint, …]
- strides
The strides of the array.
- Type:
Tuple[sint, …]
- offset
The offset of the array.
- Type:
sint
- mask
An optional mask for the array.
- Type:
Optional[Tuple[Tuple[sint, sint], …]]
- contiguous
Whether or not the array is contiguous.
- Type:
bool
- contiguous: bool
- static create(shape: Tuple[Node | int, ...], strides: Tuple[Node | int, ...] | None = None, offset: Node | int = 0, mask: Tuple[Tuple[Node | int, Node | int], ...] | None = None)[source]
Create a new view.
- Parameters:
shape (Tuple[sint, ...]) – The shape of the array.
strides (Optional[Tuple[sint, ...]]) – The strides of the array. Default is None.
offset (sint) – The offset of the array. Default is 0.
mask (Optional[Tuple[Tuple[sint, sint], ...]]) – An optional mask for the array. Default is None.
- Returns:
A new view object with the given parameters.
- Return type:
- expand(new_shape: Tuple[Node | int, ...]) View [source]
Expand the view to a new shape.
- Parameters:
new_shape (Tuple[sint]) – The desired shape of the expanded view.
- Returns:
A new view with the given shape.
- Return type:
- pad(arg: Tuple[Tuple[int, int], ...]) View [source]
Pad the view with zeros.
- arg
The padding amount for each dimension.
- Type:
Tuple[Tuple[int, int], …]
- Returns:
The padded view.
- Return type:
- permute(axis: Tuple[int, ...]) View [source]
Permute the axes of the view according to a given axis order.
- Parameters:
axis (Tuple[int]) – The desired axis order for the permutation.
- Returns:
A new view with permuted axes.
- Return type:
- reshape(new_shape: Tuple[Node | int, ...]) View | None [source]
Reshapes the current view to a new shape. The elements in the view are rearranged without changing their order. Returns None if not possible due to mismatched size or non-contiguous data.
- Parameters:
self – Instance of the class.
new_shape – Tuple containing the desired shape of the reshaped view.
- Returns:
A new View object with the specified shape, or None if reshaping is not possible.
- shrink(arg: Tuple[Tuple[Node | int, Node | int], ...]) View [source]
Shrink the view to the specified dimensions.
- arg
The new shape of the view.
- Type:
Tuple[Tuple[sint, sint], …]
- Returns:
The resized view.
- Return type:
- stride(mul: Tuple[int, ...]) View [source]
Create a view with strides multiplied by the given factors.
- Parameters:
self (View) – The current view object.
mul (Tuple[int, ...]) – The tuple of factors to multiply strides by.
- Returns:
A new view with updated strides.
- Return type:
- assert all
Checks if all elements in the tuple ‘mul’ are non-zero integers. Raises an exception if not.
- Type:
isinstance(x, int) and x != 0 for x in mul
- strides = tuple
Creates a new tuple of strides by multiplying each element of ‘self.strides’ with the corresponding element from ‘mul’.
- Type:
[z * m for z, m in zip(self.strides, mul)]
- new_shape = tuple
Creates a new shape by calculating the quotient of each element in ‘self.shape’ and the absolute value of corresponding element from ‘mul’.
- Type:
[(s + (abs(m) - 1)) // abs(m) for s, m in zip(self.shape, mul)]
- offset = sum
Calculates the new offset by multiplying each pair of elements in ‘self.shape’ and ‘self.strides’, where ‘mul’ has a negative value.
- Type:
[((s - 1) * z) for s, z, m in zip(self.shape, self.strides, mul) if m < 0]
- mask = tuple
If ‘self.mask’ is not None, creates a new mask by performing calculations on elements from ‘self.mask’, ‘self.shape’, and ‘mul’. Else, sets mask to None.
- Type:
[((mx if m > 0 else s - my) + (abs(m) - 1)) // abs(m), ((my if m > 0 else s - mx) + (abs(m) - 1)) // abs(m)] for (mx, my), s, m in zip(self.mask, self.shape, mul)
- return View.create
Returns a new view with the calculated attributes.
- Type:
new_shape, strides, self.offset + offset, mask
- tinygrad.shape.view.filter_strides(shape: Tuple[int, ...], strides: Tuple[int, ...]) Tuple[int, ...] [source]
Filter strides based on shape.
This function takes a tuple of integers representing the shape and another tuple of integers representing the strides. It returns a new tuple of integers where each stride is either kept as it is or replaced with zero, depending on whether the corresponding element in the shape tuple is not equal to one.
- Parameters:
shape – Tuple[int, …] Shape of an array.
strides – Tuple[int, …] Strides of an array.
- Returns:
Tuple[int, …] Filtered strides.
- tinygrad.shape.view.strides_for_shape(shape: Tuple[int, ...]) Tuple[int, ...] [source]
Calculate strides for an array with a given shape.
This function takes a tuple of integers representing the shape of an array and calculates the corresponding strides. It starts with a list containing a single integer 1 if the shape is not empty, otherwise it’s an empty list. Then, for each dimension in reversed order (except the first one), it appends a new integer to the list: this new integer is the product of the current dimension and the last element in the list. Finally, it returns the filtered strides by calling filter_strides.
- Parameters:
shape – Tuple[int, …] Shape of an array for which to calculate strides.
- Returns:
Tuple[int, …] Strides of an array with given shape.