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_NAMESPACEso 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 withONNX_APIcompile without modification.Namespace alias so that ONNX C++ code (and consumers such as onnxruntime) that refers to the literal
onnxnamespace — rather than theONNX_NAMESPACEmacro — resolves to the onnx-light namespace. The standard onnx package lives innamespace onnx; onnx-light usesonnx_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 fromonnx.-
namespace onnx_kernels
Typedefs
-
using DataType = TensorProto::DataType#
DataType — alias for
TensorProto::DataType.Provides a short, namespace-local name for the upstream ONNX
TensorProto::DataTypeenumeration so backend test code can writeDataType::INT64instead of fully qualifying every reference.
Functions
-
size_t ElementSize(int32_t dtype)#
Returns the size in bytes of one element of
dtype(aDataTypeinteger). Throwsstd::invalid_argumentfor 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; usePackedByteSizeinstead.
-
size_t PackedByteSize(int32_t dtype, int64_t element_count)#
Returns the storage size in bytes for
element_countelements ofdtype. Whole-byte dtypes returnelement_count * ElementSize(dtype); sub-byte packed dtypes pack 2 (INT4/UINT4) or 4 (INT2/UINT2) elements per byte andelement_countis rounded up to fill the trailing byte. Throwsstd::invalid_argumentfor unsupported types.
-
void FillValueInfo(const Tensor &tensor, ValueInfoProto &vi)#
Fills
viwith the type/shape information described bytensor.vi.nameis set totensor.name.
-
Tensor TensorFromProto(const TensorProto &tp)#
Converts a
TensorPrototo 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-endianraw_datafield.STRINGtensors are read fromstring_data.The resulting
Tensor::nameis set fromtp.name(); the shape is taken fromtp.dims().- Parameters:
tp – The source
TensorProto.- Throws:
std::invalid_argument – for unsupported
data_typevalues.- Returns:
A
Tensorwhose data matches the content oftp.
-
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 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. Throwsstd::invalid_argumentfor unsupported types.
-
template<typename T>
const T *As() const# Typed views over the underlying
databuffer. They throw if the requested type does not matchdata_type.The templated
As<T>()accessor is the generic version. The non-templateAsFloat/AsDouble/AsInt32/AsInt64are thin wrappers kept for source compatibility.
-
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
dataforBOOLelement type, stored one byte per element. The byte value is 0 for false and non-zero (canonically 1) for true.
-
uint8_t *AsBool()#
Public Members
-
int32_t data_type = 0#
Element data type stored as a
DataTypeinteger value.
-
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:
bytesand :cpp:func:size_bytesto access element bytes regardless of storage mode. Also empty whendata_typeisDataType::STRING; in that case the element values are stored instring_datainstead.
Public Static Functions
-
static inline Tensor MakeString(std::string n, std::vector<int64_t> s, std::vector<std::string> sd)#
Constructs a
STRINGtensor whose elements live instring_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)
Tensorthat references an external byte buffer without copying.The pointed-to buffer at
ptr[0 .. sz-1]MUST outlive thisTensor. Both the const and non-constAs<T>()overloads (andAsBool()) are available on borrowed tensors; the non-const overloads return aT *viaconst_cast. Callers must not write through that pointer if the underlying storage is immutable — doing so is undefined behaviour.
-
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 throwstd::invalid_argumentif any dimension inshapeis negative or ifvalues.size()does not matchprod(shape).The templated
From<T>factory is the generic version. The non-templateFromFloat/FromDouble/FromInt32/FromInt64are 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
BOOLtensor; element values are stored as one byte each (0 == false, non-zero == true). Provided as auint8_tvector so the usualstd::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
STRINGtensor whose elements are the provided UTF-8 strings (stored instring_data). Throwsstd::invalid_argumentif any dimension inshapeis negative or ifvalues.size()does not matchprod(shape).
-
Tensor() = default#
-
using DataType = TensorProto::DataType#
-
namespace onnx_kernels