콘텐츠로 이동

NDArray

#include <qbruntime/ndarray.h>

template <typename T>
class mobilint::NDArray

A class representing an N-dimensional array (NDArray).

The class automatically manages memory when it owns the data. If the NDArray is created from a raw pointer without taking ownership, it simply acts as a view of the existing data, and no memory management is performed by the NDArray.

NDArray is the recommended way for providing input and receiving output to/from model inferences. By using NDArray, unnecessary copies inside the qbruntime are avoided, which helps improve performance during model inference operations.

Template parameters

  • T — The type of the elements in the array.

Types

Member Description
value_type

value_type

using mobilint::NDArray<T>::value_type = T

Member functions

Member Description
NDArray ×5 Default constructor. Creates an empty NDArray.
data Returns a pointer to the underlying data.
shape Returns the shape (dimensions) of the array.
ndim Returns the number of dimensions of the array.
size Returns the total number of elements in the array.
begin ×2 Returns an iterator to the beginning of the data.
end ×2 Returns an iterator to the end of the data.
operator[] ×2 Accesses an element of the array.

NDArray

NDArray()=default

Default constructor. Creates an empty NDArray.

NDArray(const std::vector<int64_t> &shape, StatusCode &sc)

Constructs an NDArray with the given shape, allocating memory for elements.

This constructor allocates memory for the array and takes ownership of the allocated data. The memory is automatically managed by the NDArray instance.

Parameters

  • shape [in] — The shape (dimensions) of the array.
  • sc [out] — Status code indicating success or failure of memory allocation.
NDArray(T *ptr, const std::vector<int64_t> &shape)

Constructs an NDArray from a raw pointer without taking ownership.

This constructor creates an NDArray that acts as a view of the provided data. It does not manage the memory, and the memory is not owned by the NDArray instance. The user is responsible for ensuring that the data remains valid for the lifetime of the NDArray.

Parameters

  • ptr [in] — A pointer to the data.
  • shape [in] — The shape (dimensions) of the array.
NDArray(T *ptr, const std::vector<int64_t> &shape, bool owner)

Constructs an NDArray from a raw pointer without taking ownership.

Deprecated

This constructor is deprecated. Use NDArray(T*, const std::vector<int64_t>&) instead.

Parameters

  • ptr [in] — A pointer to the data.
  • shape [in] — The shape (dimensions) of the array.
  • owner — Ignored parameter (was intended to indicate ownership).
NDArray(std::shared_ptr<void> data, const std::vector<int64_t> &shape)

Constructs an NDArray from a shared memory block with shared ownership.

This constructor allows the NDArray to share ownership of an existing memory buffer, preventing unnecessary copies. The data parameter should be a std::shared_ptr<void> pointing to a valid memory block.

Example usage:

size_t bytesize = sizeof(float) * 100; // Allocate memory for 100 floats
void* raw_ptr = std::malloc(bytesize);
if (!raw_ptr) {
    // Handle memory allocation failure
}
auto buffer = std::shared_ptr<void>(raw_ptr, std::free);
NDArray<float> array(buffer, {10, 10}); // Create a 10x10 array

Parameters

  • data [in] — A shared pointer to the memory buffer. For example, it can be initialized using std::shared_ptr<void>(std::malloc(bytesize), std::free), but ensure that the allocation succeeds before passing it.
  • shape [in] — The shape (dimensions) of the array.

data

T * data() const

Returns a pointer to the underlying data.

Returns

A pointer to the array's data.

shape

const std::vector<int64_t> & shape() const

Returns the shape (dimensions) of the array.

Returns

A reference to the vector representing the shape.

ndim

std::size_t ndim() const

Returns the number of dimensions of the array.

Returns

The number of dimensions.

size

int64_t size() const

Returns the total number of elements in the array.

Returns

The total number of elements.

begin

T * begin()

Returns an iterator to the beginning of the data.

Returns

A pointer to the first element.

const T * begin() const

Returns a constant iterator to the beginning of the data.

Returns

A pointer to the first element.

end

T * end()

Returns an iterator to the end of the data.

Returns

A pointer past the last element.

const T * end() const

Returns a constant iterator to the end of the data.

Returns

A pointer past the last element.

operator[]

T & operator[](size_t idx)

Accesses an element of the array.

Parameters

  • idx — The index of the element.

Returns

A reference to the element at the specified index.

T operator[](size_t idx) const

Accesses an element of the array (const version).

Parameters

  • idx — The index of the element.

Returns

A copy of the element at the specified index.