Consolidate utils::ToTensorProtoElementType, TypeToDataType, and data_types_internal::ToTensorDataType. (#9824)

This commit is contained in:
Edward Chen 2022-04-20 12:45:53 -07:00 committed by GitHub
parent 2cacd18d51
commit 4854a09340
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 109 additions and 227 deletions

View file

@ -14,6 +14,7 @@
#include "core/common/exceptions.h"
#include "core/framework/endian.h"
#include "core/framework/float16.h"
#include "core/framework/to_tensor_proto_element_type.h"
#if !defined(ORT_MINIMAL_BUILD)
#include "onnx/defs/schema.h"
#else
@ -229,67 +230,6 @@ namespace data_types_internal {
/// TensorType helpers
///
template <typename T>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType() {
return ONNX_NAMESPACE::TensorProto_DataType_UNDEFINED;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<float>() {
return ONNX_NAMESPACE::TensorProto_DataType_FLOAT;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<uint8_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT8;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<int8_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT8;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<uint16_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<int16_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<int32_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT32;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<int64_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT64;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<std::string>() {
return ONNX_NAMESPACE::TensorProto_DataType_STRING;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<bool>() {
return ONNX_NAMESPACE::TensorProto_DataType_BOOL;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<MLFloat16>() {
return ONNX_NAMESPACE::TensorProto_DataType_FLOAT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<double>() {
return ONNX_NAMESPACE::TensorProto_DataType_DOUBLE;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<uint32_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT32;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<uint64_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT64;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorDataType<BFloat16>() {
return ONNX_NAMESPACE::TensorProto_DataType_BFLOAT16;
}
/// Is a given type on the list of types?
/// Accepts a list of types and the first argument is the type
/// We are checking if it is listed among those that follow
@ -512,7 +452,7 @@ class TensorType : public TensorTypeBase {
private:
TensorType() {
using namespace data_types_internal;
TensorTypeHelper::Set(ToTensorDataType<elemT>(), MutableTypeProto());
TensorTypeHelper::Set(utils::ToTensorProtoElementType<elemT>(), MutableTypeProto());
}
};
@ -601,7 +541,7 @@ class SparseTensorType : public SparseTensorTypeBase {
private:
SparseTensorType() {
using namespace data_types_internal;
SparseTensorTypeHelper::Set(ToTensorDataType<elemT>(), MutableTypeProto());
SparseTensorTypeHelper::Set(utils::ToTensorProtoElementType<elemT>(), MutableTypeProto());
}
};
@ -799,7 +739,7 @@ class MapType : public NonTensorType<CPPType> {
private:
MapType() {
using namespace data_types_internal;
MapTypeHelper::Set(ToTensorDataType<typename CPPType::key_type>(),
MapTypeHelper::Set(utils::ToTensorProtoElementType<typename CPPType::key_type>(),
MapTypeHelper::GetValueType<typename CPPType::mapped_type>()->GetTypeProto(),
this->MutableTypeProto());
}
@ -990,7 +930,7 @@ class PrimitiveDataType : public PrimitiveDataTypeBase {
private:
PrimitiveDataType()
: PrimitiveDataTypeBase{sizeof(T),
data_types_internal::ToTensorDataType<T>()} {
utils::ToTensorProtoElementType<T>()} {
}
};

View file

@ -13,6 +13,7 @@
#include "boost/mp11.hpp"
#include "core/common/common.h"
#include "core/framework/to_tensor_proto_element_type.h"
#ifndef SHARED_PROVIDER
#include "core/common/type_list.h"
#include "core/framework/data_types.h"
@ -28,68 +29,6 @@
namespace onnxruntime {
namespace utils {
template <typename T>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType() {
return ONNX_NAMESPACE::TensorProto_DataType_UNDEFINED;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<float>() {
return ONNX_NAMESPACE::TensorProto_DataType_FLOAT;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<uint8_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT8;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<int8_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT8;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<uint16_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT16;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<int16_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT16;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<int32_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT32;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<int64_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT64;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<std::string>() {
return ONNX_NAMESPACE::TensorProto_DataType_STRING;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<bool>() {
return ONNX_NAMESPACE::TensorProto_DataType_BOOL;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<MLFloat16>() {
return ONNX_NAMESPACE::TensorProto_DataType_FLOAT16;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<double>() {
return ONNX_NAMESPACE::TensorProto_DataType_DOUBLE;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<uint32_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT32;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<uint64_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT64;
};
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<BFloat16>() {
return ONNX_NAMESPACE::TensorProto_DataType_BFLOAT16;
};
// The following primitives are strongly recommended for switching on tensor input datatypes for
// kernel implementations.
//

View file

@ -0,0 +1,79 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include <cstdint>
#include <string>
#ifndef SHARED_PROVIDER
#include "onnx/onnx_pb.h"
#endif
#include "core/framework/float16.h"
namespace onnxruntime {
namespace utils {
/** Gets the TensorProto_DataType corresponding to the template type `T`. */
template <typename T>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType() {
return ONNX_NAMESPACE::TensorProto_DataType_UNDEFINED;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<float>() {
return ONNX_NAMESPACE::TensorProto_DataType_FLOAT;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<uint8_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT8;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<int8_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT8;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<uint16_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<int16_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<int32_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT32;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<int64_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT64;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<std::string>() {
return ONNX_NAMESPACE::TensorProto_DataType_STRING;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<bool>() {
return ONNX_NAMESPACE::TensorProto_DataType_BOOL;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<MLFloat16>() {
return ONNX_NAMESPACE::TensorProto_DataType_FLOAT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<double>() {
return ONNX_NAMESPACE::TensorProto_DataType_DOUBLE;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<uint32_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT32;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<uint64_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT64;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType ToTensorProtoElementType<BFloat16>() {
return ONNX_NAMESPACE::TensorProto_DataType_BFLOAT16;
}
} // namespace utils
} // namespace onnxruntime

View file

@ -36,7 +36,7 @@ void CheckLayerNorm(bool compute_mean = true, bool compute_isd = true, std::vect
testCase.AddOutput("y");
testCase.AddOutput(compute_mean ? "mean" : "");
testCase.AddOutput(compute_isd ? "invstddev" : "");
testCase.AddAttribute("stash_type", data_types_internal::ToTensorDataType<U>());
testCase.AddAttribute("stash_type", utils::ToTensorProtoElementType<U>());
if (axis != -1)
testCase.AddAttribute("axis", axis);
if (RunTest)

View file

@ -5,7 +5,7 @@
#include <vector>
#include "core/framework/data_types.h"
#include "core/framework/to_tensor_proto_element_type.h"
#include "core/graph/model.h"
#include "core/providers/cpu/cpu_execution_provider.h"
#include "core/session/inference_session.h"
@ -87,7 +87,7 @@ struct FunctionTestCase {
template <typename T>
void AddInput(std::string input_name, std::vector<int64_t> shape, std::vector<T> data, std::vector<std::string> symshape = {}) {
auto arg_type = (symshape.size() > 0) ? TensorType(data_types_internal::ToTensorDataType<T>(), symshape) : TensorType(data_types_internal::ToTensorDataType<T>(), shape);
auto arg_type = (symshape.size() > 0) ? TensorType(utils::ToTensorProtoElementType<T>(), symshape) : TensorType(utils::ToTensorProtoElementType<T>(), shape);
input_args.emplace_back(input_name, &arg_type);
OrtValue ort_value;
@ -95,14 +95,14 @@ struct FunctionTestCase {
input_values.push_back(std::make_pair(input_name, ort_value));
input_value_map.insert(std::make_pair(input_name, ort_value));
}
void AddOpset(const char* opset_domain, int version) {
opsets[opset_domain] = version;
}
template <typename T, bool GenData = true>
void AddInput(std::string input_name, std::vector<int64_t> shape) {
auto arg_type = TensorType(data_types_internal::ToTensorDataType<T>(), shape);
auto arg_type = TensorType(utils::ToTensorProtoElementType<T>(), shape);
input_args.emplace_back(input_name, &arg_type);
if (GenData) {
@ -116,7 +116,7 @@ struct FunctionTestCase {
template <typename T>
void AddBoundedInput(const char* input_name, const std::vector<int64_t>& shape, T bound) {
auto arg_type = TensorType(data_types_internal::ToTensorDataType<T>(), shape);
auto arg_type = TensorType(utils::ToTensorProtoElementType<T>(), shape);
input_args.emplace_back(input_name, &arg_type);
std::vector<T> data = random<T>(shape);
for (size_t i = 0; i < data.size(); i++)

View file

@ -3,31 +3,32 @@
#pragma once
#include <variant>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <gsl/gsl>
#include "core/common/logging/logging.h"
#include "core/common/optional.h"
#include "core/framework/allocatormgr.h"
#include "core/framework/customregistry.h"
#include "core/framework/data_types.h"
#include "core/framework/execution_frame.h"
#include "core/framework/op_kernel.h"
#include "core/framework/prepacked_weights_container.h"
#include "core/framework/run_options.h"
#include "core/framework/session_options.h"
#include "core/framework/session_state.h"
#include "core/framework/tensor.h"
#include "core/framework/prepacked_weights_container.h"
#include "core/framework/TensorSeq.h"
#include "core/framework/to_tensor_proto_element_type.h"
#include "core/graph/graph_viewer.h"
#include "core/graph/model.h"
#include "core/framework/data_types.h"
#include "test/test_environment.h"
#include "test/framework/TestAllocatorManager.h"
#include "core/framework/TensorSeq.h"
#include "core/framework/session_options.h"
#include "core/providers/providers.h"
#include "test/util/include/asserts.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <gsl/gsl>
#include "core/util/math_cpuonly.h"
#include <variant>
#include "test/framework/TestAllocatorManager.h"
#include "test/test_environment.h"
#include "test/util/include/asserts.h"
namespace onnxruntime {
class InferenceSession;
@ -48,84 +49,10 @@ struct SeqTensors {
std::vector<Tensor<T>> tensors;
};
// Function templates to translate C++ types into ONNX_NAMESPACE::TensorProto_DataTypes
template <typename T>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType();
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<float>() {
return ONNX_NAMESPACE::TensorProto_DataType_FLOAT;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<double>() {
return ONNX_NAMESPACE::TensorProto_DataType_DOUBLE;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<int32_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT32;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<int64_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT64;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<bool>() {
return ONNX_NAMESPACE::TensorProto_DataType_BOOL;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<int8_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT8;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<int16_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_INT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<uint8_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT8;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<uint16_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<uint32_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT32;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<uint64_t>() {
return ONNX_NAMESPACE::TensorProto_DataType_UINT64;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<std::string>() {
return ONNX_NAMESPACE::TensorProto_DataType_STRING;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<MLFloat16>() {
return ONNX_NAMESPACE::TensorProto_DataType_FLOAT16;
}
template <>
constexpr ONNX_NAMESPACE::TensorProto_DataType TypeToDataType<BFloat16>() {
return ONNX_NAMESPACE::TensorProto_DataType_BFLOAT16;
}
template <typename T>
struct TTypeProto {
explicit TTypeProto(const std::vector<int64_t>* shape = nullptr) {
proto.mutable_tensor_type()->set_elem_type(TypeToDataType<T>());
proto.mutable_tensor_type()->set_elem_type(utils::ToTensorProtoElementType<T>());
if (shape) {
auto mutable_shape = proto.mutable_tensor_type()->mutable_shape();
for (auto i : *shape) {
@ -172,8 +99,8 @@ struct TSparseTensorProto {
template <typename TKey, typename TVal>
struct MTypeProto {
MTypeProto() {
proto.mutable_map_type()->set_key_type(TypeToDataType<TKey>());
proto.mutable_map_type()->mutable_value_type()->mutable_tensor_type()->set_elem_type(TypeToDataType<TVal>());
proto.mutable_map_type()->set_key_type(utils::ToTensorProtoElementType<TKey>());
proto.mutable_map_type()->mutable_value_type()->mutable_tensor_type()->set_elem_type(utils::ToTensorProtoElementType<TVal>());
proto.mutable_map_type()->mutable_value_type()->mutable_tensor_type()->mutable_shape()->clear_dim();
}
ONNX_NAMESPACE::TypeProto proto;
@ -192,8 +119,8 @@ template <typename TKey, typename TVal>
struct VectorOfMapTypeProto {
VectorOfMapTypeProto() {
auto* map_type = proto.mutable_sequence_type()->mutable_elem_type()->mutable_map_type();
map_type->set_key_type(TypeToDataType<TKey>());
map_type->mutable_value_type()->mutable_tensor_type()->set_elem_type(TypeToDataType<TVal>());
map_type->set_key_type(utils::ToTensorProtoElementType<TKey>());
map_type->mutable_value_type()->mutable_tensor_type()->set_elem_type(utils::ToTensorProtoElementType<TVal>());
map_type->mutable_value_type()->mutable_tensor_type()->mutable_shape()->clear_dim();
}
ONNX_NAMESPACE::TypeProto proto;
@ -249,9 +176,6 @@ struct CheckParams {
// 4. Call AddOutput with all expected outputs,
// Or call AddReferenceOutputs to compute reference outputs with the model
// 5. Call Run
// Not all tensor types and output types are added, if a new input type is used, add it to the TypeToDataType list
// above for new output types, add a new specialization for Check<> See current usage for an example, should be self
// explanatory
class OpTester {
public:
// Default to the first opset that ORT was available (7).