Files
wehub-resource-sync 5cbd3f29e3
Fuzz / Run fuzz harnesses (${{ github.event_name == 'schedule' && 'nightly' || 'smoke' }}) (push) Has been cancelled
Create Releases / call-mac (push) Has been cancelled
Create Releases / call-linux (push) Has been cancelled
Create Releases / call-sdist (push) Has been cancelled
Create Releases / call-win (push) Has been cancelled
Create Releases / call-pyodide (push) Has been cancelled
Windows_No_Exception_CI / build (x64, 3.10) (push) Has been cancelled
Check URLs / build (push) Has been cancelled
Create Releases / Attest CI build artifacts (push) Has been cancelled
Create Releases / Check for Publish release build to pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to test.pypi-weekly (push) Has been cancelled
Create Releases / Check for Publish release build to test.pypi (rc-candidates) (push) Has been cancelled
Create Releases / Publish release build to test.pypi (push) Has been cancelled
Create Releases / Check for Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish preview build to pypi-weekly (push) Has been cancelled
Create Releases / Publish release build to pypi (push) Has been cancelled
Create Releases / test source distribution (push) Has been cancelled
clang-tidy / clang-tidy (push) Has been cancelled
Lint / Validate SBOM (push) Has been cancelled
Lint / Enforce style (push) Has been cancelled
CI / Test windows-2022, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test windows-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=1, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=1, onnx_ml=1, autogen=1 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=0, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test macos-latest, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, External, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.10, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
CI / Test ubuntu-24.04, 3.14t, Internal, debug=0, unity_build=0, onnx_ml=1, autogen=0 (push) Has been cancelled
Pixi CI / Install and lint (ubuntu-24.04-arm) (push) Has been cancelled
Pixi CI / Install and lint (windows-2022) (push) Has been cancelled
Pixi CI / Xcode generator build (push) Has been cancelled
Pixi CI / Install and test (macos-latest, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, default) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, default) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, default) (push) Has been cancelled
Pixi CI / Install and test (macos-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-24.04-arm, oldies) (push) Has been cancelled
Pixi CI / Install and test (ubuntu-latest, oldies) (push) Has been cancelled
Pixi CI / Install and test (windows-2022, oldies) (push) Has been cancelled
CodeQL / Analyze (actions) (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
Generate and publish ONNX docs / build (push) Has been cancelled
Generate and publish ONNX docs / deploy (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:41:19 +08:00

237 lines
6.1 KiB
C++

// Copyright (c) ONNX Project Contributors
//
// SPDX-License-Identifier: Apache-2.0
// ATTENTION: The code in this file is highly EXPERIMENTAL.
// Adventurous users should note that the APIs will probably change.
#pragma once
#include <cmath>
#include <string>
#include <utility>
#include <vector>
#include "onnx/common/assertions.h"
#include "onnx/common/safe_math.h"
#include "onnx/onnx_pb.h"
namespace ONNX_NAMESPACE {
struct Tensor final {
private:
bool is_segment_{false};
int64_t segment_begin_{0};
int64_t segment_end_{0};
bool has_name_{false};
std::string name_;
int32_t elem_type_{ONNX_NAMESPACE::TensorProto_DataType_UNDEFINED};
std::vector<int64_t> sizes_;
std::vector<float> float_data_;
std::vector<double> double_data_;
std::vector<int32_t> int32_data_;
std::vector<int64_t> int64_data_;
std::vector<uint64_t> uint64_data_;
std::vector<std::string> string_data_;
bool is_raw_data_{false};
std::string raw_data_;
std::vector<std::pair<std::string, std::string>> external_data_;
ONNX_NAMESPACE::TensorProto_DataLocation data_location_{ONNX_NAMESPACE::TensorProto_DataLocation_DEFAULT};
public:
const std::vector<int64_t>& sizes() const {
return sizes_;
}
std::vector<int64_t>& sizes() {
return sizes_;
}
/// if tensor is a scalar, the sizes is empty, but the element number is actually 1.
/// size_from_dim() cannot handle this case, while elem_num() handles it correctly
int64_t elem_num() const {
return safe_dim_product(sizes_, [](const char* msg) { throw tensor_error(msg); });
}
int64_t size_from_dim(int dim) const {
if (dim < 0) {
dim += static_cast<int>(sizes_.size());
}
ONNX_ASSERT(dim >= 0 && static_cast<size_t>(dim) < sizes_.size())
return safe_dim_product(sizes_.begin() + dim, sizes_.end(), [](const char* msg) { throw tensor_error(msg); });
}
int32_t elem_type() const {
return elem_type_;
}
int32_t& elem_type() {
return elem_type_;
}
std::vector<std::string>& strings() {
return string_data_;
}
const std::vector<std::string>& strings() const {
return string_data_;
}
std::vector<float>& floats() {
return float_data_;
}
const std::vector<float>& floats() const {
return float_data_;
}
std::vector<double>& doubles() {
return double_data_;
}
const std::vector<double>& doubles() const {
return double_data_;
}
std::vector<int32_t>& int32s() {
return int32_data_;
}
const std::vector<int32_t>& int32s() const {
return int32_data_;
}
std::vector<int64_t>& int64s() {
return int64_data_;
}
const std::vector<int64_t>& int64s() const {
return int64_data_;
}
std::vector<uint64_t>& uint64s() {
return uint64_data_;
}
const std::vector<uint64_t>& uint64s() const {
return uint64_data_;
}
const std::string& raw() const {
return raw_data_;
}
void set_raw_data(std::string raw_data) {
is_raw_data_ = true;
raw_data_ = std::move(raw_data);
}
template <typename T>
T* data();
template <typename T>
const T* data() const;
bool is_segment() const {
return is_segment_;
}
int64_t segment_begin() const {
return segment_begin_;
}
int64_t segment_end() const {
return segment_end_;
}
void set_segment_begin_and_end(int64_t begin, int64_t end) {
is_segment_ = true;
segment_begin_ = begin;
segment_end_ = end;
}
bool hasName() const {
return has_name_;
}
const std::string& name() const {
return name_;
}
void setName(std::string name) {
has_name_ = true;
name_ = std::move(name);
}
bool is_raw_data() const {
return is_raw_data_;
}
const std::vector<std::pair<std::string, std::string>>& external_data() const {
return external_data_;
}
std::vector<std::pair<std::string, std::string>>& external_data() {
return external_data_;
}
bool has_data_location() const {
return data_location_ != ONNX_NAMESPACE::TensorProto_DataLocation_DEFAULT;
}
const ONNX_NAMESPACE::TensorProto_DataLocation& data_location() const {
return data_location_;
}
ONNX_NAMESPACE::TensorProto_DataLocation& data_location() {
return data_location_;
}
};
template <>
inline std::string* Tensor::data<std::string>() {
ONNX_ASSERTM(
!is_raw_data(),
"data type is string. string content is required to be stored in repeated bytes string_data field."
"raw_data type cannot be string.")
return string_data_.data();
}
template <>
inline const std::string* Tensor::data<std::string>() const {
ONNX_ASSERTM(
!is_raw_data(),
"data type is string. string content is required to be stored in repeated bytes string_data field."
"raw_data type cannot be string.")
return string_data_.data();
}
// NOLINTBEGIN(bugprone-macro-parentheses)
#define define_data(type, field) \
template <> \
inline type* Tensor::data<type>() { \
if (is_raw_data_) { \
return reinterpret_cast<type*>(raw_data_.data()); \
} else { \
return field.data(); \
} \
} \
\
template <> \
inline const type* Tensor::data<type>() const { \
if (is_raw_data_) { \
return reinterpret_cast<const type*>(raw_data_.data()); \
} else { \
return field.data(); \
} \
}
// NOLINTEND(bugprone-macro-parentheses)
define_data(float, float_data_)
define_data(double, double_data_)
define_data(int32_t, int32_data_)
define_data(int64_t, int64_data_)
define_data(uint64_t, uint64_data_)
#undef define_data
} // namespace ONNX_NAMESPACE