microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
debug_i

Branches

Tags

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

Clone

HTTPS

Download ZIP

base/ocos.cc

85lines · modeblame

c891e5d7Wenbing Li5 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
4eaa5ac4Wenbing Li4 years ago3#include <sstream>
c891e5d7Wenbing Li5 years ago4#include "ocos.h"
3b0bd66eWenbing Li3 years ago5#include "narrow.h"
c891e5d7Wenbing Li5 years ago6
97ee9eb5Wenbing Li2 years ago7OrtxStatus::operator OrtStatus*() const noexcept {
8if (IsOk()) {
9return nullptr;
10}
11
12OrtStatus* status = OrtW::CreateStatus(Message(), OrtErrorCode::ORT_RUNTIME_EXCEPTION);
13return status;
14}
15
5e44a7c3Scott McKay3 years ago16OrtErrorCode BaseKernel::GetErrorCodeAndRelease(OrtStatusPtr status) const noexcept {
c891e5d7Wenbing Li5 years ago17if (status == nullptr) {
18return ORT_OK;
19}
20auto error_code = api_.GetErrorCode(status);
21api_.ReleaseStatus(status);
22return error_code;
23}
24
5e44a7c3Scott McKay3 years ago25void BaseKernel::SetOutput(OrtKernelContext* ctx, size_t output_idx, const std::vector<int64_t>& dim,
26const std::vector<int64_t>& data) {
27OrtValue* output = ort_.KernelContext_GetOutput(ctx, output_idx, dim.data(), dim.size());
28int64_t* data_ptr = ort_.GetTensorMutableData<int64_t>(output);
29for (size_t i = 0; i < data.size(); i++) {
30data_ptr[i] = data[i];
31}
aef5ef1eMojimi4 years ago32}
33
c891e5d7Wenbing Li5 years ago34template <>
5e44a7c3Scott McKay3 years ago35bool BaseKernel::TryToGetAttribute(const char* name, std::string& value) const noexcept {
c891e5d7Wenbing Li5 years ago36size_t size = 0;
5e44a7c3Scott McKay3 years ago37OrtStatus* status = api_.KernelInfoGetAttribute_string(&info_, name, nullptr, &size);
c891e5d7Wenbing Li5 years ago38
a11c8128Adrian Lizarraga3 years ago39// The status should be a nullptr when querying for the size.
40if (status != nullptr) {
41api_.ReleaseStatus(status);
c891e5d7Wenbing Li5 years ago42return false;
43}
44
45value.resize(size);
5e44a7c3Scott McKay3 years ago46status = api_.KernelInfoGetAttribute_string(&info_, name, &value[0], &size);
c891e5d7Wenbing Li5 years ago47if (GetErrorCodeAndRelease(status) != ORT_OK) {
48return false;
49}
50value.resize(size - 1);
51
52return true;
53}
54
55template <>
5e44a7c3Scott McKay3 years ago56bool BaseKernel::TryToGetAttribute(const char* name, int64_t& value) const noexcept {
57return GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_int64(&info_, name, &value)) == ORT_OK;
c891e5d7Wenbing Li5 years ago58}
59
60template <>
5e44a7c3Scott McKay3 years ago61bool BaseKernel::TryToGetAttribute(const char* name, float& value) const noexcept {
62return GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_float(&info_, name, &value)) == ORT_OK;
aef5ef1eMojimi4 years ago63}
64
3b0bd66eWenbing Li3 years ago65template <>
66bool BaseKernel::TryToGetAttribute(const char* name, int& value) const noexcept {
67int64_t origin_value = 0;
68if (GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_int64(&info_, name, &origin_value)) != ORT_OK) {
69return false;
70}
71
72value = ort_extensions::narrow<int>(origin_value);
73return true;
74}
75
aef5ef1eMojimi4 years ago76template <>
5e44a7c3Scott McKay3 years ago77bool BaseKernel::TryToGetAttribute(const char* name, bool& value) const noexcept {
aef5ef1eMojimi4 years ago78int64_t origin_value = 0;
5e44a7c3Scott McKay3 years ago79if (GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_int64(&info_, name, &origin_value)) != ORT_OK) {
aef5ef1eMojimi4 years ago80return false;
81}
82
83value = origin_value == 1;
84return true;
85}