Implementation:Onnx Onnx Tensor Class
| Knowledge Sources | |
|---|---|
| Domains | Data Structures, Tensor Representation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for representing multi-dimensional typed tensor data in ONNX's internal representation provided by the ONNX library.
Description
The onnx/common/tensor.h header defines the Tensor struct, which is the in-memory counterpart to the TensorProto protobuf message. It provides efficient access to tensor data for shape inference, optimization, and execution phases within the ONNX library. The API is marked as "highly EXPERIMENTAL" and may change.
The Tensor struct stores the following data:
- sizes_ (vector<int64_t>) - the dimensions of the tensor
- elem_type_ (int32_t) - the element data type from TensorProto_DataType enum
- Type-specific data vectors: float_data_, double_data_, int32_data_, int64_data_, uint64_data_, string_data_
- raw_data_ (string) - raw byte storage alternative
- external_data_ - key-value pairs for external data references
- data_location_ - indicates whether data is inline or external
- Optional segment information (segment_begin_, segment_end_) for partial tensor storage
- Optional name (name_)
Key methods include:
- sizes() - returns the dimension vector (const and mutable)
- elem_num() - computes total element count (handles scalar tensors correctly)
- size_from_dim(dim) - computes element count from a given dimension onward
- elem_type() - returns the element data type (const and mutable)
- Type-specific accessors: floats(), doubles(), int32s(), int64s(), uint64s(), strings()
- Templated data<T>() - returns a typed pointer to the data, transparently handling both typed vectors and raw data storage
- set_raw_data() - sets raw byte data
- is_raw_data() - checks if data is stored as raw bytes
- Segment and external data accessors
Template specializations of data<T>() are provided for float, double, int32_t, int64_t, uint64_t, and std::string, with the string specialization asserting that raw_data is not used (strings must be stored in string_data).
Usage
Use the Tensor class when working with constant tensor values in ONNX's internal IR, such as during shape inference or graph optimization. Access data through the type-specific accessors (floats(), int64s(), etc.) or the templated data<T>() method. When constructing tensors, set the element type, dimensions, and data through the appropriate mutators.
Code Reference
Source Location
- Repository: Onnx_Onnx
- File: onnx/common/tensor.h
Signature
namespace ONNX_NAMESPACE {
struct Tensor final {
const std::vector<int64_t>& sizes() const;
std::vector<int64_t>& sizes();
int64_t elem_num() const;
int64_t size_from_dim(int dim) const;
int32_t elem_type() const;
int32_t& elem_type();
std::vector<float>& floats();
const std::vector<float>& floats() const;
std::vector<double>& doubles();
const std::vector<double>& doubles() const;
std::vector<int32_t>& int32s();
const std::vector<int32_t>& int32s() const;
std::vector<int64_t>& int64s();
const std::vector<int64_t>& int64s() const;
std::vector<uint64_t>& uint64s();
const std::vector<uint64_t>& uint64s() const;
std::vector<std::string>& strings();
const std::vector<std::string>& strings() const;
const std::string& raw() const;
void set_raw_data(std::string raw_data);
bool is_raw_data() const;
template <typename T> T* data();
template <typename T> const T* data() const;
bool hasName() const;
const std::string& name() const;
void setName(std::string name);
bool is_segment() const;
int64_t segment_begin() const;
int64_t segment_end() const;
void set_segment_begin_and_end(int64_t begin, int64_t end);
const std::vector<std::pair<std::string, std::string>>& external_data() const;
std::vector<std::pair<std::string, std::string>>& external_data();
bool has_data_location() const;
const TensorProto_DataLocation& data_location() const;
TensorProto_DataLocation& data_location();
};
} // namespace ONNX_NAMESPACE
Import
#include "onnx/common/tensor.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| elem_type | int32_t | Yes | The tensor element data type (from TensorProto_DataType enum) |
| sizes | std::vector<int64_t> | Yes | The dimensions of the tensor |
| data | type-specific vectors or raw_data | Yes | The actual tensor data in one of the typed vectors or as raw bytes |
| name | std::string | No | Optional tensor name |
| segment | int64_t begin, int64_t end | No | Optional segment range for partial tensors |
Outputs
| Name | Type | Description |
|---|---|---|
| sizes() | const std::vector<int64_t>& | The tensor dimensions |
| elem_num() | int64_t | Total number of elements (product of all dimensions, 1 for scalars) |
| data<T>() | T* or const T* | Typed pointer to tensor data (from typed vector or raw_data) |
| elem_type() | int32_t | The element data type enum value |
Usage Examples
#include "onnx/common/tensor.h"
using namespace ONNX_NAMESPACE;
// Create and populate a float tensor
Tensor tensor;
tensor.elem_type() = TensorProto_DataType_FLOAT;
tensor.sizes() = {2, 3};
tensor.floats() = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f};
// Access tensor properties
int64_t total = tensor.elem_num(); // 6
int64_t from_dim1 = tensor.size_from_dim(1); // 3
// Access data via typed accessors
const std::vector<float>& data = tensor.floats();
// Access data via templated pointer
const float* ptr = tensor.data<float>();
for (int64_t i = 0; i < tensor.elem_num(); ++i) {
std::cout << ptr[i] << " ";
}
// Set a name
tensor.setName("weights");
if (tensor.hasName()) {
std::cout << tensor.name() << std::endl; // "weights"
}
// Work with raw data
Tensor raw_tensor;
raw_tensor.elem_type() = TensorProto_DataType_FLOAT;
raw_tensor.sizes() = {4};
raw_tensor.set_raw_data(raw_bytes);
const float* raw_ptr = raw_tensor.data<float>();