microsoft/onnxruntime-extensions

Public

mirrored fromhttps://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
6c3b496e3f05b9a552cb57a5593da68d0b3f9f6f

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

operators/ocos.cc

79lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3#include <sstream>
4#include "ocos.h"
5
6
7bool BaseKernel::HasAttribute(const char* name) const {
8 if (info_ == nullptr) {
9 ORT_CXX_API_THROW("Kernel was incorrectly initialized, pointer info_ cannot be null.", ORT_INVALID_ARGUMENT);
10 }
11 size_t size;
12 std::string out;
13 // Crashes here.
14 OrtStatus* status = api_.KernelInfoGetAttribute_string(info_, name, nullptr, &size);
15 auto r = api_.GetErrorCode(status);
16 bool has = (r == ORT_INVALID_ARGUMENT) || (r == ORT_OK);
17 if (has) {
18 api_.ReleaseStatus(status);
19 return has;
20 }
21 const char* error = api_.GetErrorMessage(status);
22 if (strstr(error, "No attribute") == error) {
23 api_.ReleaseStatus(status);
24 return false;
25 }
26 api_.ReleaseStatus(status);
27 return true;
28}
29
30OrtErrorCode BaseKernel::GetErrorCodeAndRelease(OrtStatusPtr status) {
31 if (status == nullptr) {
32 return ORT_OK;
33 }
34 auto error_code = api_.GetErrorCode(status);
35 api_.ReleaseStatus(status);
36 return error_code;
37}
38
39template <>
40bool BaseKernel::TryToGetAttribute(const char* name, std::string& value) {
41 if (info_ == nullptr) {
42 ORT_CXX_API_THROW("Kernel was incorrectly initialized, pointer info_ cannot be null.", ORT_INVALID_ARGUMENT);
43 }
44
45 size_t size = 0;
46 OrtStatus* status = api_.KernelInfoGetAttribute_string(info_, name, nullptr, &size);
47
48 // The status should be ORT_INVALID_ARGUMENT because the size is insufficient to hold the string
49 if (GetErrorCodeAndRelease(status) != ORT_INVALID_ARGUMENT) {
50 return false;
51 }
52
53 value.resize(size);
54 status = api_.KernelInfoGetAttribute_string(info_, name, &value[0], &size);
55 if (GetErrorCodeAndRelease(status) != ORT_OK) {
56 return false;
57 }
58 value.resize(size - 1);
59
60 return true;
61}
62
63template <>
64bool BaseKernel::TryToGetAttribute(const char* name, int64_t& value) {
65 if (info_ == nullptr) {
66 ORT_CXX_API_THROW("Kernel was incorrectly initialized, pointer info_ cannot be null.", ORT_INVALID_ARGUMENT);
67 }
68
69 return GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_int64(info_, name, &value)) == ORT_OK;
70}
71
72template <>
73bool BaseKernel::TryToGetAttribute(const char* name, float& value) {
74 if (info_ == nullptr) {
75 ORT_CXX_API_THROW("Kernel was incorrectly initialized, pointer info_ cannot be null.", ORT_INVALID_ARGUMENT);
76 }
77
78 return GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_float(info_, name, &value)) == ORT_OK;
79}