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¶
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¶
Default constructor. Creates an empty NDArray.
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.
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.
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).
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 usingstd::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¶
Returns a pointer to the underlying data.
Returns
A pointer to the array's data.
shape¶
Returns the shape (dimensions) of the array.
Returns
A reference to the vector representing the shape.
ndim¶
Returns the number of dimensions of the array.
Returns
The number of dimensions.
size¶
Returns the total number of elements in the array.
Returns
The total number of elements.
begin¶
Returns an iterator to the beginning of the data.
Returns
A pointer to the first element.
Returns a constant iterator to the beginning of the data.
Returns
A pointer to the first element.
end¶
Returns an iterator to the end of the data.
Returns
A pointer past the last element.
Returns a constant iterator to the end of the data.
Returns
A pointer past the last element.
operator[]¶
Accesses an element of the array.
Parameters
idx— The index of the element.
Returns
A reference to the element at the specified index.
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.