tensor
Module
Implements the Tensor
type.
Example:
from tensor import Tensor, TensorSpec, TensorShape
from utils.index import Index
from random import rand
let height = 256
let width = 256
let channels = 3
# Create the tensor of dimensions height, width, channels
# and fill with random values.
let image = rand[DType.float32](height, width, channels)
# Declare the grayscale image.
let spec = TensorSpec(DType.float32, height, width)
var gray_scale_image = Tensor[DType.float32](spec)
# Perform the RGB to grayscale transform.
for y in range(height):
for x in range(width):
let r = image[y,x,0]
let g = image[y,x,1]
let b = image[y,x,2]
= 0.299 * r + 0.587 * g + 0.114 * b
gray_scale_image[Index(y,x)]
print(gray_scale_image.shape().__str__())
Tensor
A tensor type which owns its underlying data and is parameterized on DType.
Parameters:
- dtype (
DType
): The underlying element type of the tensor.
Functions:
__init__
__init__(inout self: Self)
Default initializer for TensorShape.
__init__(inout self: Self, *dims: Int)
Allocates a tensor using the shape provided.
Args:
- dims (
*Int
): The tensor dimensions.
__init__(inout self: Self, owned shape: TensorShape)
Allocates a tensor using the shape provided.
Args:
- shape (
TensorShape
): The tensor shape.
__init__(inout self: Self, owned spec: TensorSpec)
Allocates a tensor using the spec provided.
Args:
- spec (
TensorSpec
): The tensor spec.
__init__(inout self: Self, owned ptr: DTypePointer[dtype], owned shape: TensorShape)
Initializes a Tensor from the pointer and shape provided. The caller relinquishes the ownership of the pointer being passed in.
Args:
- ptr (
DTypePointer[dtype]
): The data pointer. - shape (
TensorShape
): The tensor shapes.
__init__(inout self: Self, owned ptr: DTypePointer[dtype], owned spec: TensorSpec)
Initializes a Tensor from the pointer and shape provided. The caller relinquishes the ownership of the pointer being passed in.
Args:
- ptr (
DTypePointer[dtype]
): The data pointer. - spec (
TensorSpec
): The tensor spec.
__copyinit__
__copyinit__(inout self: Self, other: Self)
Creates a deep copy of an existing tensor.
Args:
- other (
Self
): The tensor to copy from.
__moveinit__
__moveinit__(inout self: Self, owned existing: Self)
Move initializer for the tensor.
Args:
- existing (
Self
): The tensor to move.
__del__
__del__(owned self: Self)
Delete the spec and release any owned memory.
__getitem__
__getitem__(self: Self, index: Int) -> SIMD[dtype, 1]
Gets the value at the specified index.
Args:
- index (
Int
): The index of the value to retrieve.
Returns:
The value at the specified indices.
__getitem__(self: Self, *indices: Int) -> SIMD[dtype, 1]
Gets the value at the specified indices.
Args:
- indices (
*Int
): The indices of the value to retrieve.
Returns:
The value at the specified indices.
__getitem__(self: Self, indices: VariadicList[Int]) -> SIMD[dtype, 1]
Gets the value at the specified indices.
Args:
- indices (
VariadicList[Int]
): The indices of the value to retrieve.
Returns:
The value at the specified indices.
__getitem__[len: Int](self: Self, indices: StaticIntTuple[len]) -> SIMD[dtype, 1]
Gets the SIMD value at the specified indices.
Parameters:
- len (
Int
): The length of the indecies.
Args:
- indices (
StaticIntTuple[len]
): The indices of the value to retrieve.
Returns:
The value at the specified indices.
__setitem__
__setitem__(inout self: Self, index: Int, val: SIMD[dtype, 1])
Sets the value at the specified index.
Args:
- index (
Int
): The index of the value to set. - val (
SIMD[dtype, 1]
): The value to store.
__setitem__(inout self: Self, indices: VariadicList[Int], val: SIMD[dtype, 1])
Sets the value at the specified indices.
Args:
- indices (
VariadicList[Int]
): The indices of the value to set. - val (
SIMD[dtype, 1]
): The value to store.
__setitem__[len: Int](inout self: Self, indices: StaticIntTuple[len], val: SIMD[dtype, 1])
Sets the value at the specified indices.
Parameters:
- len (
Int
): The length of the indecies.
Args:
- indices (
StaticIntTuple[len]
): The indices of the value to set. - val (
SIMD[dtype, 1]
): The value to store.
__eq__
__eq__(self: Self, other: Self) -> Bool
Returns True if the two tensors are the same and False otherwise.
Args:
- other (
Self
): The other Tensor to compare against.
Returns:
True if the two tensors are the same and False otherwise.
__ne__
__ne__(self: Self, other: Self) -> Bool
Returns True if the two tensors are not the same and False otherwise.
Args:
- other (
Self
): The other Tensor to compare against.
Returns:
True if the two tensors are the not the same and False otherwise.
data
data(self: Self) -> DTypePointer[dtype]
Gets the underlying Data pointer to the Tensor.
Returns:
The underlying data pointer of the tensor.
type
type(self: Self) -> DType
Gets the underlying DType of the tensor.
Returns:
The underlying DType of the tensor.
rank
rank(self: Self) -> Int
Gets the rank of the tensor.
Returns:
The rank of the tensor.
num_elements
num_elements(self: Self) -> Int
Gets the total number of elements in the tensor.
Returns:
The total number of elements in the tensor.
bytecount
bytecount(self: Self) -> Int
Gets the total bytecount of the tensor.
Returns:
The total bytecount of the tensor.
spec
spec(self: Self) -> TensorSpec
Gets the specification of the tensor.
Returns:
The underlying tensor spec of the tensor.
shape
shape(self: Self) -> TensorShape
Gets the shape of the tensor.
Returns:
The underlying tensor shape of the tensor.
dim
dim(self: Self, idx: Int) -> Int
Gets the dimension at the specified index.
Args:
- idx (
Int
): The dimension index.
Returns:
The dimension at the specified index.
simd_load
simd_load[simd_width: Int](self: Self, index: Int) -> SIMD[dtype, simd_width]
Gets the SIMD value at the specified index.
Parameters:
- simd_width (
Int
): The SIMD width of the vector.
Args:
- index (
Int
): The index of the value to retrieve.
Returns:
The SIMD value at the specified indices.
simd_load[simd_width: Int](self: Self, *indices: Int) -> SIMD[dtype, simd_width]
Gets the SIMD value at the specified indices.
Parameters:
- simd_width (
Int
): The SIMD width of the vector.
Args:
- indices (
*Int
): The indices of the value to retrieve.
Returns:
The SIMD value at the specified indices.
simd_load[simd_width: Int](self: Self, indices: VariadicList[Int]) -> SIMD[dtype, simd_width]
Gets the SIMD value at the specified indices.
Parameters:
- simd_width (
Int
): The SIMD width of the vector.
Args:
- indices (
VariadicList[Int]
): The indices of the value to retrieve.
Returns:
The SIMD value at the specified indices.
simd_load[simd_width: Int, len: Int](self: Self, indices: StaticIntTuple[len]) -> SIMD[dtype, simd_width]
Gets the SIMD value at the specified indices.
Parameters:
- simd_width (
Int
): The SIMD width of the vector. - len (
Int
): The length of the indecies.
Args:
- indices (
StaticIntTuple[len]
): The indices of the value to retrieve.
Returns:
The SIMD value at the specified indices.
simd_store
simd_store[simd_width: Int](inout self: Self, index: Int, val: SIMD[dtype, simd_width])
Sets the SIMD value at the specified index.
Parameters:
- simd_width (
Int
): The SIMD width of the vector.
Args:
- index (
Int
): The index of the value to set. - val (
SIMD[dtype, simd_width]
): The SIMD value to store.
simd_store[simd_width: Int](inout self: Self, indices: VariadicList[Int], val: SIMD[dtype, simd_width])
Sets the SIMD value at the specified indices.
Parameters:
- simd_width (
Int
): The SIMD width of the vector.
Args:
- indices (
VariadicList[Int]
): The indices of the value to set. - val (
SIMD[dtype, simd_width]
): The SIMD value to store.
simd_store[simd_width: Int, len: Int](inout self: Self, indices: StaticIntTuple[len], val: SIMD[dtype, simd_width])
Sets the SIMD value at the specified indices.
Parameters:
- simd_width (
Int
): The SIMD width of the vector. - len (
Int
): The length of the indecies.
Args:
- indices (
StaticIntTuple[len]
): The indices of the value to set. - val (
SIMD[dtype, simd_width]
): The SIMD value to store.