Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "gtest/gtest.h"
#include "test/test_environment.h"
#include "test/util/include/asserts.h"
#include "core/graph/model.h"
#include "core/graph/op.h"
#include "core/graph/onnx_protobuf.h"
#include "core/framework/execution_providers.h"
#include "core/framework/op_kernel.h"
#include "core/framework/external_data_loader_manager.h"
#include "core/framework/session_state.h"
#include "core/providers/cpu/cpu_execution_provider.h"
#include "core/session/onnxruntime_cxx_api.h"
 
using namespace ONNX_NAMESPACE;
 
namespace onnxruntime {
namespace test {
 
ONNX_OPERATOR_SCHEMA(KernelInfoStringArrayAttrOp)
    .SetDoc("Test op for kernel info string-array attributes.")
    .Attr("strings_attr", "Repeated string attribute for kernel info API tests.",
          AttrType::AttributeProto_AttributeType_STRINGS, std::vector<std::string>{})
    .Output(0, "output_1", "docstr for output_1.", "tensor(int32)");
 
static void VerifyKernelInfoStringArrayAttribute(const std::vector<std::string>& attribute_values) {
  OrtThreadPoolParams to;
  auto tp = concurrency::CreateThreadPool(&onnxruntime::Env::Default(), to, concurrency::ThreadPoolType::INTRA_OP);
 
  onnxruntime::Model model("graph_kernel_info_string_attr", false, DefaultLoggingManager().DefaultLogger());
  auto& graph = model.MainGraph();
 
  ExecutionProviders execution_providers;
  auto tmp_cpu_execution_provider = std::make_unique<CPUExecutionProvider>(CPUExecutionProviderInfo(false));
  auto* cpu_execution_provider = tmp_cpu_execution_provider.get();
  ASSERT_STATUS_OK(execution_providers.Add(kCpuExecutionProvider, std::move(tmp_cpu_execution_provider)));
 
  DataTransferManager dtm;
  ExternalDataLoaderManager edlm;
  profiling::Profiler profiler;
 
  SessionOptions sess_options;
  sess_options.enable_mem_pattern = true;
  sess_options.execution_mode = ExecutionMode::ORT_SEQUENTIAL;
  sess_options.use_deterministic_compute = false;
  sess_options.enable_mem_reuse = true;
 
  SessionState session_state(graph, execution_providers, tp.get(), nullptr, dtm, edlm,
                             DefaultLoggingManager().DefaultLogger(), profiler, sess_options);
 
  std::vector<onnxruntime::NodeArg*> inputs;
  std::vector<onnxruntime::NodeArg*> outputs;
  TypeProto output_type;
  output_type.mutable_tensor_type()->set_elem_type(TensorProto_DataType_INT32);
  output_type.mutable_tensor_type()->mutable_shape()->add_dim()->set_dim_value(1);
  onnxruntime::NodeArg output_arg("node_1_out_1", &output_type);
  outputs.push_back(&output_arg);
 
  onnxruntime::Node& node = graph.AddNode("node_1", "KernelInfoStringArrayAttrOp", "node 1.", inputs, outputs);
  node.AddAttribute("strings_attr", gsl::make_span(attribute_values));
  ASSERT_STATUS_OK(graph.Resolve());
 
  auto kernel_def = KernelDefBuilder().SetName("KernelInfoStringArrayAttrOp").Provider(kCpuExecutionProvider).SinceVersion(1, 10).Build();
 
  OpKernelInfo kernel_info(node, *kernel_def, *cpu_execution_provider, session_state.GetConstantInitializedTensors(),
                           session_state.GetOrtValueNameIdxMap(), session_state.GetDataTransferMgr(), session_state.GetAllocators(),
                           session_state.GetSessionOptions().config_options);
 
  const OrtApi& ort_api = Ort::GetApi();
  OrtAllocator* allocator = nullptr;
  ASSERT_EQ(nullptr, ort_api.GetAllocatorWithDefaultOptions(&allocator));
 
  size_t size = 0;
  ASSERT_EQ(nullptr, ort_api.KernelInfoGetAttributeArray_string(reinterpret_cast<const OrtKernelInfo*>(&kernel_info), "strings_attr",
                                                                allocator, nullptr, &size));
  ASSERT_EQ(attribute_values.size(), size);
 
  char** out = nullptr;
  ASSERT_EQ(nullptr, ort_api.KernelInfoGetAttributeArray_string(reinterpret_cast<const OrtKernelInfo*>(&kernel_info), "strings_attr",
                                                                allocator, &out, &size));
  ASSERT_EQ(attribute_values.size(), size);
 
  if (attribute_values.empty()) {
    ASSERT_EQ(nullptr, out);
  } else {
    ASSERT_NE(nullptr, out);
    for (size_t i = 0; i < size; ++i) {
      EXPECT_STREQ(attribute_values[i].c_str(), out[i]);
      allocator->Free(allocator, out[i]);
    }
    allocator->Free(allocator, out);
  }
 
  Ort::ConstKernelInfo ort_kernel_info{reinterpret_cast<const OrtKernelInfo*>(&kernel_info)};
  EXPECT_EQ(attribute_values, ort_kernel_info.GetAttributes<std::string>("strings_attr"));
 
  OrtStatus* status = ort_api.KernelInfoGetAttributeArray_string(reinterpret_cast<const OrtKernelInfo*>(&kernel_info), "missing_attr",
                                                                 allocator, nullptr, &size);
  ASSERT_NE(nullptr, status);
  ort_api.ReleaseStatus(status);
}
 
TEST(KernelInfoTests, KernelInfoGetAttributeArrayString) {
  VerifyKernelInfoStringArrayAttribute({"alpha", "beta", "gamma"});
}
 
TEST(KernelInfoTests, KernelInfoGetAttributeArrayStringEmpty) {
  VerifyKernelInfoStringArrayAttribute({});
}
 
}  // namespace test
}  // namespace onnxruntime