// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include #include "core/framework/data_types.h" #include "core/framework/execution_providers.h" #include "core/framework/op_kernel.h" #include "core/framework/session_state.h" #include "core/graph/graph_viewer.h" #include "core/graph/model.h" #include "core/graph/op.h" #include "core/providers/cpu/cpu_execution_provider.h" #include "core/session/inference_session.h" #include "gtest/gtest.h" #include "test/providers/provider_test_utils.h" #include "asserts.h" #include "test/unittest_util/framework_test_utils.h" using namespace ONNX_NAMESPACE; using namespace onnxruntime::common; const char* experimental_using_opaque = R"DOC( The operator constructs an instance of sparse one dimensional tensor represented by a SparseTensorSample type. It uses 3 supplied inputs each in a form of a single dimensional tensor. )DOC"; namespace onnxruntime { // We will use this class to implement Sparse Tensor and // register it as an Opaque type emulating some experimental type /** * @brief This class implements a SparseTensor as an example * of using custom experimental type outside of ONNXRuntime. * * @details The class captures the 3 necessary elements of a Sparse Tensor * values - a vector of non-zero sparse tensor values * indices - a vector of indices of non zero values * shape - a scalar tensor that indicates the size of a single dimension * It is assumed that all of the values for the tensors are int64 * we use tensor datatypes as effective memory managers. */ // This type is a result of the construct_sparse OpKernel. class SparseTensorSample final { public: SparseTensorSample() = default; ~SparseTensorSample() = default; SparseTensorSample(const SparseTensorSample&) = default; SparseTensorSample& operator=(const SparseTensorSample&) = default; SparseTensorSample(SparseTensorSample&&) = default; SparseTensorSample& operator=(SparseTensorSample&&) = default; const std::vector& Values() const { return values_; } const std::vector& Indicies() const { return indicies_; } int64_t Size() const { return size_; } std::vector& Values() { return values_; } std::vector& Indicies() { return indicies_; } int64_t& Size() { return size_; } private: std::vector values_; std::vector indicies_; int64_t size_; // The value of a single dimension }; // We will then register this class as an Opaque type as if created and used by // a 3rd party for experiments. extern const char kTestDomain[] = "ai.onnx"; extern const char kSparseTensorName[] = "SparseTensorSample"; ORT_REGISTER_OPAQUE_TYPE(SparseTensorSample, kTestDomain, kSparseTensorName); class OpaqueTypeTests : public testing::Test { public: static void SetUpTestCase() { MLDataType mltype = DataTypeImpl::GetType(); DataTypeImpl::RegisterDataType(mltype); } }; /** * @brief This class represents an operator kernel which takes as input 3 tensors * * The OpKernel takes 3 tensors as input named as follows: * - sparse_values - Tensor * - sparse_indicies - Tensor * - sparse_shape - Tensor * * Output - TestSparseTensorType - Opaque type */ class ConstructSparseTensor final : public OpKernel { public: ConstructSparseTensor(const OpKernelInfo& info) : OpKernel{info} {} Status Compute(OpKernelContext* ctx) const override { ORT_ENFORCE(ctx->InputCount() == 3, "Expecting 3 inputs"); const Tensor& values_tensor = *ctx->Input(0); const Tensor& indicies_tensor = *ctx->Input(1); const Tensor& shape_tensor = *ctx->Input(2); // Shapes of values and indicies should be the same since they refer to the same // values const TensorShape& val_shape = values_tensor.Shape(); const TensorShape& ind_shape = indicies_tensor.Shape(); ORT_ENFORCE(val_shape.NumDimensions() == 1, "Expecting vectors"); ORT_ENFORCE(val_shape.NumDimensions() == ind_shape.NumDimensions()); // Copy data. With some effort we could hold shallow copies of the input Tensors // but I will leave this for now. SparseTensorSample* output_sparse_tensor = ctx->Output(0); ORT_ENFORCE(output_sparse_tensor != nullptr); output_sparse_tensor->Values().assign(values_tensor.Data(), values_tensor.Data() + val_shape[0]); output_sparse_tensor->Indicies().assign(indicies_tensor.Data(), indicies_tensor.Data() + ind_shape[0]); output_sparse_tensor->Size() = *shape_tensor.Data(); return Status::OK(); } }; /** * @brief This class represents an operator kernel that fetches and returns * sparse tensor shape from an Opaque type * * * Output - Scalar Tensor */ class FetchSparseTensorShape final : public OpKernel { public: FetchSparseTensorShape(const OpKernelInfo& info) : OpKernel{info} {} Status Compute(OpKernelContext* ctx) const override { ORT_ENFORCE(ctx->InputCount() == 1, "Expecting a single SparseTensorSample input"); const SparseTensorSample* sparse_input = ctx->Input(0); // Always a single dimension of 1 bc we are storing a single number const int64_t dims[1] = {1}; TensorShape output_shape(dims, 1); Tensor* sparse_shape = ctx->Output(0, output_shape); int64_t* shape_data = sparse_shape->MutableData(); ORT_ENFORCE(shape_data != nullptr); *shape_data = sparse_input->Size(); return Status::OK(); } }; namespace test { KernelDefBuilder ConstructSparseTensorDef() { KernelDefBuilder def; def.SetName("ConstructSparseTensor") .SetDomain(onnxruntime::kMLDomain) .SinceVersion(8) .Provider(onnxruntime::kCpuExecutionProvider) .TypeConstraint("T1", DataTypeImpl::GetTensorType()) .TypeConstraint("T2", DataTypeImpl::GetTensorType()) .TypeConstraint("T3", DataTypeImpl::GetTensorType()) .TypeConstraint("T", DataTypeImpl::GetType()); return def; } KernelDefBuilder ConstructFetchSparseShape() { KernelDefBuilder def; def.SetName("FetchSparseTensorShape") .SetDomain(onnxruntime::kMLDomain) .SinceVersion(8) .Provider(onnxruntime::kCpuExecutionProvider) .TypeConstraint("T1", DataTypeImpl::GetType()) .TypeConstraint("T", DataTypeImpl::GetTensorType()); return def; } ONNX_NAMESPACE::OpSchema GetConstructSparseTensorSchema() { ONNX_NAMESPACE::OpSchema schema("ConstructSparseTensor", __FILE__, __LINE__); schema.SetDoc(experimental_using_opaque) .SetDomain(onnxruntime::kMLDomain) .Input( 0, "sparse_values", "Single dimensional Tensor that holds all non-zero values", "T1", OpSchema::Single) .Input( 1, "sparse_indicies", "Single dimensional tensor that holds indicies of non-zero values", "T2", OpSchema::Single) .Input( 2, "sparse_shape", "Single dimensional tensor that holds sparse tensor shape", "T3", OpSchema::Single) .Output( 0, "sparse_rep", "SparseTensorSample opaque object", "T", OpSchema::Single) .TypeConstraint( "T1", {"tensor(int64)"}, "Only int64 is allowed") .TypeConstraint( "T2", {"tensor(int64)"}, "Only int64 is allowed") .TypeConstraint( "T3", {"tensor(int64)"}, "Only int64 is allowed") .TypeConstraint( "T", {"opaque(ai.onnx,SparseTensorSample)"}, "Opaque object"); schema.SinceVersion(8); return schema; } ONNX_NAMESPACE::OpSchema GetFetchSparseShapeSchema() { ONNX_NAMESPACE::OpSchema schema("FetchSparseTensorShape", __FILE__, __LINE__); schema.SetDoc(experimental_using_opaque) .SetDomain(onnxruntime::kMLDomain) .Input( 0, "sparse_rep", "Opaque SparseTensorSample", "T1", OpSchema::Single) .Output( 0, "sparse_tensor_shape", "Single dimensional tensor that holds sparse tensor shape", "T", OpSchema::Single) .TypeConstraint( "T1", {"opaque(ai.onnx,SparseTensorSample)"}, "Only int64 is allowed") .TypeConstraint( "T", {"tensor(int64)"}, "Only int64 is allowed"); schema.SinceVersion(8); return schema; } TEST_F(OpaqueTypeTests, RunModel) { SessionOptions so; so.session_logid = "SparseTensorTest"; so.session_log_verbosity_level = 1; // Both the session and the model need custom registries // so we construct it here before the model std::shared_ptr registry = std::make_shared(); InferenceSession session_object{so, GetEnvironment()}; ASSERT_STATUS_OK(session_object.RegisterCustomRegistry(registry)); auto ops_schema = GetConstructSparseTensorSchema(); auto shape_schema = GetFetchSparseShapeSchema(); std::vector schemas = {ops_schema, shape_schema}; ASSERT_STATUS_OK(registry->RegisterOpSet(schemas, onnxruntime::kMLDomain, 8, 9)); // Register our kernels here auto ctor_def = ConstructSparseTensorDef(); ASSERT_STATUS_OK(registry->RegisterCustomKernel( ctor_def, [](FuncManager&, const OpKernelInfo& info, std::unique_ptr& out) { out = std::make_unique(info); return Status::OK(); })); auto shape_def = ConstructFetchSparseShape(); ASSERT_STATUS_OK(registry->RegisterCustomKernel( shape_def, [](FuncManager&, const OpKernelInfo& info, std::unique_ptr& out) { out = std::make_unique(info); return Status::OK(); })); IOnnxRuntimeOpSchemaRegistryList custom_schema_registries_ = {registry->GetOpschemaRegistry()}; std::unordered_map domain_to_version = {{onnxruntime::kMLDomain, 8}}; Model model("SparseTensorTest", false, ModelMetaData(), PathString(), custom_schema_registries_, domain_to_version, {}, DefaultLoggingManager().DefaultLogger()); auto& graph = model.MainGraph(); std::vector inputs; std::vector outputs; TypeProto input_tensor_proto(*DataTypeImpl::GetTensorType()->GetTypeProto()); { // Sparse tensor will contain total 5 elements but only 2 of them a non-zero TypeProto input_values(input_tensor_proto); input_values.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(2); auto& sparse_values_arg = graph.GetOrCreateNodeArg("sparse_values", &input_values); inputs.push_back(&sparse_values_arg); TypeProto input_indicies(input_tensor_proto); input_indicies.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(2); auto& sparse_indicies_arg = graph.GetOrCreateNodeArg("sparse_indicies", &input_indicies); inputs.push_back(&sparse_indicies_arg); // Shape tensor will contain only one value TypeProto input_shape(input_tensor_proto); input_shape.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1); auto& sparse_shape_arg = graph.GetOrCreateNodeArg("sparse_shape", &input_shape); inputs.push_back(&sparse_shape_arg); // Output is our custom data type TypeProto output_sparse_tensor(*DataTypeImpl::GetType()->GetTypeProto()); auto& output_sparse_tensor_arg = graph.GetOrCreateNodeArg("sparse_rep", &output_sparse_tensor); outputs.push_back(&output_sparse_tensor_arg); auto& node = graph.AddNode("ConstructSparseTensor", "ConstructSparseTensor", "Create a sparse tensor representation", inputs, outputs, nullptr, onnxruntime::kMLDomain); node.SetExecutionProviderType(onnxruntime::kCpuExecutionProvider); } { // We start the input from previous node output inputs = std::move(outputs); outputs.clear(); TypeProto output_shape(input_tensor_proto); output_shape.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1); auto& output_shape_arg = graph.GetOrCreateNodeArg("sparse_tensor_shape", &output_shape); outputs.push_back(&output_shape_arg); auto& node = graph.AddNode("FetchSparseTensorShape", "FetchSparseTensorShape", "Fetch shape from sparse tensor", inputs, outputs, nullptr, onnxruntime::kMLDomain); node.SetExecutionProviderType(onnxruntime::kCpuExecutionProvider); } ASSERT_STATUS_OK(graph.Resolve()); // Get a proto and load from it std::string serialized_model; auto model_proto = model.ToProto(); EXPECT_TRUE(model_proto.SerializeToString(&serialized_model)); std::stringstream sstr(serialized_model); ASSERT_STATUS_OK(session_object.Load(sstr)); ASSERT_STATUS_OK(session_object.Initialize()); RunOptions run_options; // Prepare inputs/outputs std::vector val_dims = {2}; std::vector values = {1, 2}; // prepare inputs OrtValue ml_values; CreateMLValue(TestCPUExecutionProvider()->CreatePreferredAllocators()[0], val_dims, values, &ml_values); std::vector ind_dims = {2}; std::vector indicies = {1, 4}; OrtValue ml_indicies; CreateMLValue(TestCPUExecutionProvider()->CreatePreferredAllocators()[0], ind_dims, indicies, &ml_indicies); std::vector shape_dims = {1}; std::vector shape = {5}; OrtValue ml_shape; CreateMLValue(TestCPUExecutionProvider()->CreatePreferredAllocators()[0], shape_dims, shape, &ml_shape); NameMLValMap feeds; feeds.insert(std::make_pair("sparse_values", ml_values)); feeds.insert(std::make_pair("sparse_indicies", ml_indicies)); feeds.insert(std::make_pair("sparse_shape", ml_shape)); // Output std::vector output_shape_dims = {1}; std::vector output_shape = {0}; std::vector output_names; output_names.push_back("sparse_tensor_shape"); std::vector fetches; ASSERT_STATUS_OK(session_object.Run(run_options, feeds, output_names, &fetches)); ASSERT_EQ(1u, fetches.size()); auto& rtensor = fetches.front().Get(); // Should get the original shape back in the form of a tensor EXPECT_EQ(1u, rtensor.Shape().NumDimensions()); EXPECT_EQ(5, *rtensor.Data()); } } // namespace test } // namespace onnxruntime