Skip to main content

Mojo struct

ImageData

struct ImageData[LayoutType: TensorLayout, //, dtype: DType, static_image_layout: Image2DLayout, origin: MutOrigin]

Utility class that generalizes conv2d data and filter tensor with a given data layout.

Fields​

  • ​data (TileTensor[dtype, LayoutType, origin]):
  • ​dynamic_image_layout (Image2DLayout):

Implemented traits​

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDestructible, Movable, RegisterPassable, TrivialRegisterPassable

Methods​

__init__​

__init__(data: TileTensor[dtype, LayoutType, origin], _layout: Image2DLayout) -> Self

Construct of an image data instance with dynamic layout param.

Args:

__init__(data: TileTensor[dtype, LayoutType, origin]) -> Self

__getitem__​

__getitem__(self, n: Int, c: Int, h: Int, w: Int) -> Scalar[dtype]

Reads the underlying data buffer based on the tensor index and under- lying data layout.

Args:

  • ​n (Int): Index on the batch dimension.
  • ​c (Int): Index on the channel dimension.
  • ​h (Int): Index on the height dimension.
  • ​w (Int): Index on the width dimension.

Returns:

Scalar[dtype]: The value stored at the given index position.

__setitem__​

__setitem__(self, n: Int, c: Int, h: Int, w: Int, value: Scalar[dtype])

Writes the underlying data buffer based on the tensor index and under- lying data layout.

Args:

  • ​n (Int): Index on the batch dimension.
  • ​c (Int): Index on the channel dimension.
  • ​h (Int): Index on the height dimension.
  • ​w (Int): Index on the width dimension.
  • ​value (Scalar[dtype]): The value to store at the given index position.

to_static_layout​

to_static_layout[new_static_image_layout: Image2DLayout](self) -> ImageData[dtype, new_static_image_layout, origin]

Conversion utility from a fully dynamic data structure, e.g. from c shim to one with compile-time known data layout.

Returns:

ImageData[dtype, new_static_image_layout, origin]: The image data with static data layout.

get_image_layout​

get_image_layout(self) -> Image2DLayout

The getter function of the underlying data layout, resolving from either statically or dynamically provided information.

Returns:

Image2DLayout: The resolved data layout tag for this image instance.

get_flat_index​

get_flat_index(self, n: Int, c: Int, h: Int, w: Int) -> Int

Converts the dimension index to the flat index of the underlying data based on the tensor layout.

Args:

  • ​n (Int): Index on the batch dimension.
  • ​c (Int): Index on the channel dimension.
  • ​h (Int): Index on the height dimension.
  • ​w (Int): Index on the width dimension.

Returns:

Int: An integer containing the index based on the underlying data layout.

get_tuple_index​

get_tuple_index(self, idx: Int) -> IndexList[4]

Converts the flat index to the dimension index of the underlying data based on the tensor layout.

Args:

  • ​idx (Int): Flat index.

Returns:

IndexList[4]: A IndexList containing the index in NCHW order.

num_elements​

num_elements(self) -> Int

Returns:

Int