For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
PointerStorage
struct PointerStorage[*, element_width: Int = Int(1)]
Implements TensorOps backed by a raw UnsafePointer.
PointerStorage is the default storage policy for TileTensor. Its
StorageType handle is a plain UnsafePointer, and every operation is
expressed directly in terms of the underlying pointer.
Parameters
- element_width (
Int): Number of scalar elements per logical element. A value of1(the default) is a non-vectorized tensor; larger values describe a vectorized view whose logical elements are SIMD vectors.
Implemented traits
AnyType,
ImplicitlyDeletable,
TensorOps,
TensorStorage
comptime members
element_size
comptime element_size = element_width
Number of scalar elements per logical element (alias of element_width).
OffsetResultType
comptime OffsetResultType[offset_types: TypeList[values]] = PointerStorage[element_width=element_width]
The storage type produced by offsetting with a given coordinate.
Offsetting never changes the storage policy, so this is Self.
Parameters
- offset_types (
TypeList[values]): The coordinate element types of the applied offset.
StorageType
comptime StorageType[mut: Bool, //, dtype: DType, origin: Origin[mut=mut], address_space: AddressSpace] = UnsafePointer[SIMD[dtype, element_width], origin, address_space=address_space]
A raw UnsafePointer to Scalar[dtype] borrowing the storage.
Parameters
- mut (
Bool): The mutability of the borrowed storage, inferred fromorigin. - dtype (
DType): The element data type of the borrowed storage. - origin (
Origin[mut=mut]): The origin tracking the lifetime of the borrowed storage. - address_space (
AddressSpace): The address space the borrowed storage resides in.
Methods
write_type_name_to
static def write_type_name_to(mut writer: T)
Writes the storage type name representation to the writer.
Args:
- writer (
T): TheWriterto output to.
unsafe_cast
static def unsafe_cast[to_mut: Bool, //, to_dtype: DType, to_origin: Origin[mut=to_mut], to_address_space: AddressSpace](storage: UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]) -> UnsafePointer[SIMD[to_dtype, element_width], to_origin, address_space=to_address_space]
Reinterprets a storage handle with new type parameters.
Parameters:
- to_mut (
Bool): The mutability of the origin. - to_dtype (
DType): The element data type to reinterpret the storage as. - to_origin (
Origin[mut=to_mut]): The origin to reinterpret the storage as. - to_address_space (
AddressSpace): The address space to reinterpret the storage as.
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]): The storage to reinterpret.
Returns:
UnsafePointer[SIMD[to_dtype, element_width], to_origin, address_space=to_address_space]: A handle referring to the same storage, viewed with the new type
parameters.
load
static def load[dtype: DType, //, width: SIMDSize, alignment: Int, invariant: Bool = False, non_temporal: Bool = False](storage: UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]) -> SIMD[dtype, width]
Loads a SIMD value from the storage.
Parameters:
- dtype (
DType): The element data type of the storage. - width (
SIMDSize): The number of elements to load. - alignment (
Int): The alignment guarantee for the load. - invariant (
Bool): If True, the compiler may assume the memory won't be modified during the kernel, enabling load hoisting and caching. - non_temporal (
Bool): If True, indicates the data will not be reused soon, allowing the hardware to bypass caches (e.g., streaming loads).
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]): The storage to load from.
Returns:
SIMD[dtype, width]: The loaded SIMD value.
static def load[dtype: DType, //, width: SIMDSize, alignment: Int, invariant: Bool = False, non_temporal: Bool = False](storage: UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space], offset: T) -> SIMD[dtype, width]
Loads a SIMD value at a scalar-element offset from the storage.
Parameters:
- dtype (
DType): The element data type of the storage. - width (
SIMDSize): The number of elements to load. - alignment (
Int): The alignment guarantee for the load. - invariant (
Bool): If True, the compiler may assume the memory won't be modified during the kernel, enabling load hoisting and caching. - non_temporal (
Bool): If True, indicates the data will not be reused soon, allowing the hardware to bypass caches (e.g., streaming loads).
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]): The storage to load from. - offset (
T): The scalar-element offset to load at.
Returns:
SIMD[dtype, width]: The loaded SIMD value.
store
static def store[dtype: DType, alignment: Int, *, non_temporal: Bool = False](storage: UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space], value: SIMD[dtype])
Stores a SIMD value into the storage.
Parameters:
- dtype (
DType): The element data type of the storage. - alignment (
Int): The alignment guarantee for the store. - non_temporal (
Bool): If True, indicates the data will not be reused soon, allowing the hardware to bypass caches (e.g., streaming stores).
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]): The storage to store into. - value (
SIMD[dtype]): TheSIMDvalue to store.
static def store[dtype: DType, alignment: Int, *, non_temporal: Bool = False](storage: UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space], offset: T, value: SIMD[dtype])
Stores a SIMD value at a scalar-element offset in the storage.
Parameters:
- dtype (
DType): The element data type of the storage. - alignment (
Int): The alignment guarantee for the store. - non_temporal (
Bool): If True, indicates the data will not be reused soon, allowing the hardware to bypass caches (e.g., streaming stores).
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]): The storage to store into. - offset (
T): The scalar-element offset to store at. - value (
SIMD[dtype]): TheSIMDvalue to store.
offset
static def offset[offset_mut: Bool, offset_types: TypeList[offset_types.values], //, offset_dtype: DType, offset_origin: Origin[mut=offset_mut], offset_address_space: AddressSpace](var storage: UnsafePointer[SIMD[offset_dtype, element_width], offset_origin, address_space=offset_address_space], var offset_coord: Coord[offset_types]) -> UnsafePointer[SIMD[offset_dtype, element_width], offset_origin, address_space=offset_address_space]
Returns a storage handle offset by a number of scalar elements.
The returned handle refers to the same externally owned storage,
advanced by the scalar-element offset in offset_coord. The offset is
measured in scalar elements (not logical SIMD elements) so that it
matches the scalar-unit offsets produced by a tensor's layout and
consumed by load/store; for a vectorized storage
(element_width > 1) advancing the raw SIMD-typed handle directly would
over-advance by element_width.
Parameters:
- offset_mut (
Bool): The mutability of the storage, inferred fromoffset_origin. - offset_types (
TypeList[offset_types.values]): The coordinate element types ofoffset_coord. - offset_dtype (
DType): The element data type of the storage. - offset_origin (
Origin[mut=offset_mut]): The origin tracking the lifetime of the storage. - offset_address_space (
AddressSpace): The address space the storage resides in.
Args:
- storage (
UnsafePointer[SIMD[offset_dtype, element_width], offset_origin, address_space=offset_address_space]): The storage to offset from. - offset_coord (
Coord[offset_types]): A rank-1 coordinate holding the number of scalar elements to advance the handle by.
Returns:
UnsafePointer[SIMD[offset_dtype, element_width], offset_origin, address_space=offset_address_space]: A handle of the same type starting the given number of scalar
elements into the referenced storage.
distance
static def distance[dtype: DType, address_space: AddressSpace, //](storage: UnsafePointer[SIMD[dtype, element_width], address_space=address_space], other: UnsafePointer[SIMD[dtype, element_width], address_space=address_space]) -> Int
Returns the scalar-element distance from other to storage.
Parameters:
- dtype (
DType): The storages'DType. - address_space (
AddressSpace): The storages'AddressSpace.
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=address_space]): The storage to measure the distance to. - other (
UnsafePointer[SIMD[dtype, element_width], address_space=address_space]): The storage to measure the distance from.
Returns:
Int: The number of scalar elements separating the two handles. The
value is positive when storage is ahead of other and negative
when it precedes other.
copy_from
static def copy_from[SelfLayoutType: TensorLayout, self_origin: MutOrigin, self_address_space: AddressSpace, OtherLayoutType: TensorLayout, other_mut: Bool, other_origin: Origin[mut=other_mut], other_address_space: AddressSpace, //, dst_dtype: DType, src_dtype: DType, OtherStorage: TensorStorage](storage: Tuple[UnsafePointer[SIMD[dst_dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType], other: Tuple[OtherStorage.StorageType[other_mut, origin_of(other_origin), src_dtype, other_origin, other_address_space], OtherLayoutType])
Copies the elements of other into storage, in place.
Loads each logical element from other through its (possibly
different) storage policy and stores it into storage through
PointerStorage, casting to the destination dtype. Delegates to the
shared _copy_from loop.
- Both operands must have statically known shapes with matching total element count.
- Both operands must have the same logical element size.
- Source and destination dtypes may differ; each logical element is cast to the destination dtype.
Parameters:
- SelfLayoutType (
TensorLayout): The layout type of the destination storage. - self_origin (
MutOrigin): The origin of the destination storage. - self_address_space (
AddressSpace): The address space of the destination storage. - OtherLayoutType (
TensorLayout): The layout type of the source storage. - other_mut (
Bool): The mutability of the source storage. - other_origin (
Origin[mut=other_mut]): The origin of the source storage. - other_address_space (
AddressSpace): The address space of the source storage. - dst_dtype (
DType): The element data type of the destination storage. - src_dtype (
DType): The element data type of the source storage. - OtherStorage (
TensorStorage): The storage policy of the source. May differ fromSelfas long as the two policies are copy-compatible (same logical element size).
Args:
- storage (
Tuple[UnsafePointer[SIMD[dst_dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType]): A tuple of the destination storage (modified in place) and its layout. - other (
Tuple[OtherStorage.StorageType[other_mut, origin_of(other_origin), src_dtype, other_origin, other_address_space], OtherLayoutType]): A tuple of the source storage and its layout.
add
static def add[SelfLayoutType: TensorLayout, self_origin: MutOrigin, self_address_space: AddressSpace, OtherLayoutType: TensorLayout, other_mut: Bool, other_origin: Origin[mut=other_mut], other_address_space: AddressSpace, //, dtype: DType](storage: Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType], other: Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType])
Adds other into storage elementwise, in place.
Parameters:
- SelfLayoutType (
TensorLayout): The layout type of the destination storage. - self_origin (
MutOrigin): The origin of the destination storage. - self_address_space (
AddressSpace): The address space of the destination storage. - OtherLayoutType (
TensorLayout): The layout type of the right-hand storage operand. - other_mut (
Bool): The mutability of the right-hand storage operand. - other_origin (
Origin[mut=other_mut]): The origin of the right-hand storage operand. - other_address_space (
AddressSpace): The address space of the right-hand storage operand. - dtype (
DType): The element data type of both storages.
Args:
- storage (
Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType]): A tuple of the destination storage (modified in place) and its layout. - other (
Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType]): A tuple of the right-hand storage operand and its layout.
mul
static def mul[SelfLayoutType: TensorLayout, self_origin: MutOrigin, self_address_space: AddressSpace, OtherLayoutType: TensorLayout, other_mut: Bool, other_origin: Origin[mut=other_mut], other_address_space: AddressSpace, //, dtype: DType](storage: Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType], other: Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType])
Multiplies storage by other elementwise, in place.
Parameters:
- SelfLayoutType (
TensorLayout): The layout type of the destination storage. - self_origin (
MutOrigin): The origin of the destination storage. - self_address_space (
AddressSpace): The address space of the destination storage. - OtherLayoutType (
TensorLayout): The layout type of the right-hand storage operand. - other_mut (
Bool): The mutability of the right-hand storage operand. - other_origin (
Origin[mut=other_mut]): The origin of the right-hand storage operand. - other_address_space (
AddressSpace): The address space of the right-hand storage operand. - dtype (
DType): The element data type of both storages.
Args:
- storage (
Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType]): A tuple of the destination storage (modified in place) and its layout. - other (
Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType]): A tuple of the right-hand storage operand and its layout.
sub
static def sub[SelfLayoutType: TensorLayout, self_origin: MutOrigin, self_address_space: AddressSpace, OtherLayoutType: TensorLayout, other_mut: Bool, other_origin: Origin[mut=other_mut], other_address_space: AddressSpace, //, dtype: DType](storage: Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType], other: Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType])
Subtracts other from storage elementwise, in place.
Parameters:
- SelfLayoutType (
TensorLayout): The layout type of the destination storage. - self_origin (
MutOrigin): The origin of the destination storage. - self_address_space (
AddressSpace): The address space of the destination storage. - OtherLayoutType (
TensorLayout): The layout type of the right-hand storage operand. - other_mut (
Bool): The mutability of the right-hand storage operand. - other_origin (
Origin[mut=other_mut]): The origin of the right-hand storage operand. - other_address_space (
AddressSpace): The address space of the right-hand storage operand. - dtype (
DType): The element data type of both storages.
Args:
- storage (
Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType]): A tuple of the destination storage (modified in place) and its layout. - other (
Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType]): A tuple of the right-hand storage operand and its layout.
floordiv
static def floordiv[SelfLayoutType: TensorLayout, self_origin: MutOrigin, self_address_space: AddressSpace, OtherLayoutType: TensorLayout, other_mut: Bool, other_origin: Origin[mut=other_mut], other_address_space: AddressSpace, //, dtype: DType](storage: Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType], other: Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType])
Floor-divides storage by other elementwise, in place.
Parameters:
- SelfLayoutType (
TensorLayout): The layout type of the destination storage. - self_origin (
MutOrigin): The origin of the destination storage. - self_address_space (
AddressSpace): The address space of the destination storage. - OtherLayoutType (
TensorLayout): The layout type of the right-hand storage operand. - other_mut (
Bool): The mutability of the right-hand storage operand. - other_origin (
Origin[mut=other_mut]): The origin of the right-hand storage operand. - other_address_space (
AddressSpace): The address space of the right-hand storage operand. - dtype (
DType): The element data type of both storages.
Args:
- storage (
Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType]): A tuple of the destination storage (modified in place) and its layout. - other (
Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType]): A tuple of the right-hand storage operand and its layout.
truediv
static def truediv[SelfLayoutType: TensorLayout, self_origin: MutOrigin, self_address_space: AddressSpace, OtherLayoutType: TensorLayout, other_mut: Bool, other_origin: Origin[mut=other_mut], other_address_space: AddressSpace, //, dtype: DType](storage: Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType], other: Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType])
True-divides storage by other elementwise, in place.
Parameters:
- SelfLayoutType (
TensorLayout): The layout type of the destination storage. - self_origin (
MutOrigin): The origin of the destination storage. - self_address_space (
AddressSpace): The address space of the destination storage. - OtherLayoutType (
TensorLayout): The layout type of the right-hand storage operand. - other_mut (
Bool): The mutability of the right-hand storage operand. - other_origin (
Origin[mut=other_mut]): The origin of the right-hand storage operand. - other_address_space (
AddressSpace): The address space of the right-hand storage operand. - dtype (
DType): The element data type of both storages.
Args:
- storage (
Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType]): A tuple of the destination storage (modified in place) and its layout. - other (
Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType]): A tuple of the right-hand storage operand and its layout.
min
static def min[SelfLayoutType: TensorLayout, self_origin: MutOrigin, self_address_space: AddressSpace, OtherLayoutType: TensorLayout, other_mut: Bool, other_origin: Origin[mut=other_mut], other_address_space: AddressSpace, //, dtype: DType](storage: Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType], other: Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType])
Takes the elementwise minimum of storage and other, in place.
Parameters:
- SelfLayoutType (
TensorLayout): The layout type of the destination storage. - self_origin (
MutOrigin): The origin of the destination storage. - self_address_space (
AddressSpace): The address space of the destination storage. - OtherLayoutType (
TensorLayout): The layout type of the right-hand storage operand. - other_mut (
Bool): The mutability of the right-hand storage operand. - other_origin (
Origin[mut=other_mut]): The origin of the right-hand storage operand. - other_address_space (
AddressSpace): The address space of the right-hand storage operand. - dtype (
DType): The element data type of both storages.
Args:
- storage (
Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType]): A tuple of the destination storage (modified in place) and its layout. - other (
Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType]): A tuple of the right-hand storage operand and its layout.
max
static def max[SelfLayoutType: TensorLayout, self_origin: MutOrigin, self_address_space: AddressSpace, OtherLayoutType: TensorLayout, other_mut: Bool, other_origin: Origin[mut=other_mut], other_address_space: AddressSpace, //, dtype: DType](storage: Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType], other: Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType])
Takes the elementwise maximum of storage and other, in place.
Parameters:
- SelfLayoutType (
TensorLayout): The layout type of the destination storage. - self_origin (
MutOrigin): The origin of the destination storage. - self_address_space (
AddressSpace): The address space of the destination storage. - OtherLayoutType (
TensorLayout): The layout type of the right-hand storage operand. - other_mut (
Bool): The mutability of the right-hand storage operand. - other_origin (
Origin[mut=other_mut]): The origin of the right-hand storage operand. - other_address_space (
AddressSpace): The address space of the right-hand storage operand. - dtype (
DType): The element data type of both storages.
Args:
- storage (
Tuple[UnsafePointer[SIMD[dtype, element_width], self_origin, address_space=self_address_space], SelfLayoutType]): A tuple of the destination storage (modified in place) and its layout. - other (
Tuple[UnsafePointer[SIMD[dtype, element_width], other_origin, address_space=other_address_space], OtherLayoutType]): A tuple of the right-hand storage operand and its layout.
abs
static def abs[dtype: DType, //](storage: UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space], layout: T)
Takes the elementwise absolute value of storage, in place.
For unsigned dtypes this is the identity.
Parameters:
- dtype (
DType): The element data type of the storage.
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]): The storage to modify in place. - layout (
T): The layout describing the storage's elements.
recip
static def recip[dtype: DType, //](storage: UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space], layout: T)
Replaces each element of storage with its reciprocal, in place.
Elements equal to zero produce infinity, following IEEE 754 division semantics.
Parameters:
- dtype (
DType): The element data type of the storage. Must be a floating-point type.
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]): The storage to modify in place. - layout (
T): The layout describing the storage's elements.
exp
static def exp[dtype: DType, //, scale: Scalar[dtype]](storage: UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space], layout: T)
Replaces each element x of storage with exp(scale * x), in place.
The scale factor is applied before exponentiation so that scaled
exponentials (for example softmax logit scaling) fuse into a single
pass over the elements. Pass a scale of 1 for a plain exponential.
Parameters:
- dtype (
DType): The element data type of the storage. Must be a floating-point type. - scale (
Scalar[dtype]): The compile-time factor each element is multiplied by before exponentiation.
Args:
- storage (
UnsafePointer[SIMD[dtype, element_width], address_space=storage.address_space]): The storage to modify in place. - layout (
T): The layout describing the storage's elements.