simple_tensor.h#

Defines

ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(CPP_TYPE, ENUM_VALUE)#
namespace onnx_light

Alias that makes onnx-light headers compatible with code that references ONNX_LIGHT_NAMESPACE (the macro used in the standard onnx package).

Set to ONNX_LIGHT_NAMESPACE so both names resolve to the same namespace.

Symbol-visibility attribute for the public onnx-light C++ API.

Defined as empty because onnx-light does not require explicit __declspec(dllexport) or __attribute__((visibility("default"))) annotations — visibility is controlled at the shared-library level. The macro is provided so that vendored ONNX headers that decorate their declarations with ONNX_API compile without modification.

Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal onnx namespace — rather than the ONNX_NAMESPACE macro — resolves to the onnx-light namespace. The standard onnx package lives in namespace onnx; onnx-light uses onnx_light (via ONNX_LIGHT_NAMESPACE), so this alias keeps onnx-light a true drop-in. It is only introduced when the onnx-light namespace differs from onnx.

namespace onnx_kernels

Typedefs

using DataType = TensorProto::DataType#

DataType — alias for TensorProto::DataType.

Provides a short, namespace-local name for the upstream ONNX TensorProto::DataType enumeration so backend test code can write DataType::INT64 instead of fully qualifying every reference.

Functions

ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(float, DataType::FLOAT)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(double, DataType::DOUBLE)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(int16_t, DataType::INT16)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(int32_t, DataType::INT32)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(int64_t, DataType::INT64)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(int8_t, DataType::INT8)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(uint8_t, DataType::UINT8)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(uint16_t, DataType::UINT16)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(uint32_t, DataType::UINT32)#
ONNX_LIGHT_DECLARE_TENSOR_ELEMENT_TYPE(uint64_t, DataType::UINT64)#
size_t ElementSize(int32_t dtype)#

Returns the size in bytes of one element of dtype (a DataType integer). Throws std::invalid_argument for unsupported types. Sub-byte packed dtypes (INT4/UINT4/INT2/UINT2) are not supported by this helper because they do not have a whole-byte per-element size; use PackedByteSize instead.

size_t PackedByteSize(int32_t dtype, int64_t element_count)#

Returns the storage size in bytes for element_count elements of dtype. Whole-byte dtypes return element_count * ElementSize(dtype); sub-byte packed dtypes pack 2 (INT4/UINT4) or 4 (INT2/UINT2) elements per byte and element_count is rounded up to fill the trailing byte. Throws std::invalid_argument for unsupported types.

void FillValueInfo(const Tensor &tensor, ValueInfoProto &vi)#

Fills vi with the type/shape information described by tensor. vi.name is set to tensor.name.

Tensor TensorFromProto(const TensorProto &tp)#

Converts a TensorProto to a :cpp:class:Tensor.

Supports all numeric data types stored either in the typed repeated fields (float_data, int32_data, int64_data, double_data, uint64_data) or in the raw little-endian raw_data field. STRING tensors are read from string_data.

The resulting Tensor::name is set from tp.name(); the shape is taken from tp.dims().

Parameters:

tp – The source TensorProto.

Throws:

std::invalid_argument – for unsupported data_type values.

Returns:

A Tensor whose data matches the content of tp.

struct Tensor#
#include <simple_tensor.h>

Tensor — minimal runtime tensor used by backend test cases.

This struct is intentionally distinct from TensorProto: it carries no protobuf wire dependency, owns its bytes in row-major little-endian layout, and is meant to be consumed directly by a runtime exercising a single backend test node case.

Public Functions

Tensor() = default#
inline Tensor(std::string n, int32_t dt, std::vector<int64_t> s, std::vector<uint8_t> d)#
inline const uint8_t *bytes() const noexcept#

Returns a pointer to the raw element bytes. Works for both owned (data) and borrowed (non-owning view) tensors.

inline size_t size_bytes() const noexcept#

Returns the total number of raw element bytes. Works for both owned and borrowed tensors.

int64_t element_count() const#

Returns the product of all shape dimensions; 1 for an empty shape.

size_t element_size() const#

Returns the size in bytes of one element of data_type. Throws std::invalid_argument for unsupported types.

template<typename T>
const T *As() const#

Typed views over the underlying data buffer. They throw if the requested type does not match data_type.

The templated As<T>() accessor is the generic version. The non-template AsFloat/AsDouble/AsInt32/AsInt64 are thin wrappers kept for source compatibility.

template<typename T>
T *As()#
const float *AsFloat() const#
float *AsFloat()#
const double *AsDouble() const#
double *AsDouble()#
const int32_t *AsInt32() const#
int32_t *AsInt32()#
const int64_t *AsInt64() const#
int64_t *AsInt64()#
const int8_t *AsInt8() const#
int8_t *AsInt8()#
const uint8_t *AsUint8() const#
uint8_t *AsUint8()#
const int16_t *AsInt16() const#
int16_t *AsInt16()#
const uint16_t *AsUint16() const#
uint16_t *AsUint16()#
const uint32_t *AsUint32() const#
uint32_t *AsUint32()#
const uint64_t *AsUint64() const#
uint64_t *AsUint64()#
const uint8_t *AsBool() const#

Typed view over data for BOOL element type, stored one byte per element. The byte value is 0 for false and non-zero (canonically 1) for true.

uint8_t *AsBool()#
const std::vector<std::string> &AsStrings() const#

Typed view over the underlying string_data buffer. Throws std::invalid_argument if data_type is not DataType::STRING.

std::vector<std::string> &AsStrings()#

Public Members

std::string name#

Optional name of the tensor (input/output name in the test model).

int32_t data_type = 0#

Element data type stored as a DataType integer value.

std::vector<int64_t> shape#

Tensor shape; an empty shape denotes a scalar (element_count == 1).

std::vector<uint8_t> data#

Raw element bytes in row-major little-endian layout (owned storage).

Empty when the tensor uses a borrowed (non-owning) view — use :cpp:func:bytes and :cpp:func:size_bytes to access element bytes regardless of storage mode. Also empty when data_type is DataType::STRING; in that case the element values are stored in string_data instead.

std::vector<std::string> string_data#

String element values in row-major layout. Populated only when data_type is DataType::STRING; empty for all other element types.

Public Static Functions

static inline Tensor MakeString(std::string n, std::vector<int64_t> s, std::vector<std::string> sd)#

Constructs a STRING tensor whose elements live in string_data. Distinct from the bytes-based constructor so brace-enclosed { ... } initializer lists at call sites are unambiguous.

static Tensor Borrow(std::string name, int32_t dtype, std::vector<int64_t> shape, const uint8_t *ptr, size_t sz)#

Creates a non-owning (borrowed) Tensor that references an external byte buffer without copying.

The pointed-to buffer at ptr[0 .. sz-1] MUST outlive this Tensor. Both the const and non-const As<T>() overloads (and AsBool()) are available on borrowed tensors; the non-const overloads return a T * via const_cast. Callers must not write through that pointer if the underlying storage is immutable — doing so is undefined behaviour.

Parameters:
  • nameTensor name.

  • dtype – Element data type (a DataType integer value).

  • shapeTensor shape.

  • ptr – Pointer to the first byte of element data.

  • sz – Total byte count of the element buffer.

Returns:

A Tensor backed by the external buffer.

template<typename T>
static Tensor From(const std::string &name, const std::vector<int64_t> &shape, const std::vector<T> &values)#

Typed factories that construct a tensor of the given shape and copy the provided values into data. They throw std::invalid_argument if any dimension in shape is negative or if values.size() does not match prod(shape).

The templated From<T> factory is the generic version. The non-template FromFloat/FromDouble/FromInt32/FromInt64 are thin wrappers kept for source compatibility.

static Tensor FromFloat(const std::string &name, const std::vector<int64_t> &shape, const std::vector<float> &values)#
static Tensor FromDouble(const std::string &name, const std::vector<int64_t> &shape, const std::vector<double> &values)#
static Tensor FromInt32(const std::string &name, const std::vector<int64_t> &shape, const std::vector<int32_t> &values)#
static Tensor FromInt64(const std::string &name, const std::vector<int64_t> &shape, const std::vector<int64_t> &values)#
static Tensor FromInt8(const std::string &name, const std::vector<int64_t> &shape, const std::vector<int8_t> &values)#
static Tensor FromUint8(const std::string &name, const std::vector<int64_t> &shape, const std::vector<uint8_t> &values)#
static Tensor FromInt16(const std::string &name, const std::vector<int64_t> &shape, const std::vector<int16_t> &values)#
static Tensor FromUint16(const std::string &name, const std::vector<int64_t> &shape, const std::vector<uint16_t> &values)#
static Tensor FromUint32(const std::string &name, const std::vector<int64_t> &shape, const std::vector<uint32_t> &values)#
static Tensor FromUint64(const std::string &name, const std::vector<int64_t> &shape, const std::vector<uint64_t> &values)#
static Tensor FromBool(const std::string &name, const std::vector<int64_t> &shape, const std::vector<uint8_t> &values)#

Constructs a BOOL tensor; element values are stored as one byte each (0 == false, non-zero == true). Provided as a uint8_t vector so the usual std::vector<bool> packing pitfalls are avoided.

static Tensor FromStrings(const std::string &name, const std::vector<int64_t> &shape, const std::vector<std::string> &values)#

Constructs a STRING tensor whose elements are the provided UTF-8 strings (stored in string_data). Throws std::invalid_argument if any dimension in shape is negative or if values.size() does not match prod(shape).

Private Members

const uint8_t *borrow_ptr_ = nullptr#

Non-null only for borrowed (non-owning) tensors created via :cpp:func:Borrow. When set, element bytes are read from borrow_ptr_[0 .. borrow_size_-1] rather than from data.

size_t borrow_size_ = 0#