microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
base/ocos.cc
85lines · modeblame
c891e5d7Wenbing Li5 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. | |
4eaa5ac4Wenbing Li4 years ago | 3 | #include <sstream> |
c891e5d7Wenbing Li5 years ago | 4 | #include "ocos.h" |
3b0bd66eWenbing Li3 years ago | 5 | #include "narrow.h" |
c891e5d7Wenbing Li5 years ago | 6 | |
97ee9eb5Wenbing Li2 years ago | 7 | OrtxStatus::operator OrtStatus*() const noexcept { |
| 8 | if (IsOk()) { | |
| 9 | return nullptr; | |
| 10 | } | |
| 11 | | |
| 12 | OrtStatus* status = OrtW::CreateStatus(Message(), OrtErrorCode::ORT_RUNTIME_EXCEPTION); | |
| 13 | return status; | |
| 14 | } | |
| 15 | | |
5e44a7c3Scott McKay3 years ago | 16 | OrtErrorCode BaseKernel::GetErrorCodeAndRelease(OrtStatusPtr status) const noexcept { |
c891e5d7Wenbing Li5 years ago | 17 | if (status == nullptr) { |
| 18 | return ORT_OK; | |
| 19 | } | |
| 20 | auto error_code = api_.GetErrorCode(status); | |
| 21 | api_.ReleaseStatus(status); | |
| 22 | return error_code; | |
| 23 | } | |
| 24 | | |
5e44a7c3Scott McKay3 years ago | 25 | void BaseKernel::SetOutput(OrtKernelContext* ctx, size_t output_idx, const std::vector<int64_t>& dim, |
| 26 | const std::vector<int64_t>& data) { | |
| 27 | OrtValue* output = ort_.KernelContext_GetOutput(ctx, output_idx, dim.data(), dim.size()); | |
| 28 | int64_t* data_ptr = ort_.GetTensorMutableData<int64_t>(output); | |
| 29 | for (size_t i = 0; i < data.size(); i++) { | |
| 30 | data_ptr[i] = data[i]; | |
| 31 | } | |
aef5ef1eMojimi4 years ago | 32 | } |
| 33 | | |
c891e5d7Wenbing Li5 years ago | 34 | template <> |
5e44a7c3Scott McKay3 years ago | 35 | bool BaseKernel::TryToGetAttribute(const char* name, std::string& value) const noexcept { |
c891e5d7Wenbing Li5 years ago | 36 | size_t size = 0; |
5e44a7c3Scott McKay3 years ago | 37 | OrtStatus* status = api_.KernelInfoGetAttribute_string(&info_, name, nullptr, &size); |
c891e5d7Wenbing Li5 years ago | 38 | |
a11c8128Adrian Lizarraga3 years ago | 39 | // The status should be a nullptr when querying for the size. |
| 40 | if (status != nullptr) { | |
| 41 | api_.ReleaseStatus(status); | |
c891e5d7Wenbing Li5 years ago | 42 | return false; |
| 43 | } | |
| 44 | | |
| 45 | value.resize(size); | |
5e44a7c3Scott McKay3 years ago | 46 | status = api_.KernelInfoGetAttribute_string(&info_, name, &value[0], &size); |
c891e5d7Wenbing Li5 years ago | 47 | if (GetErrorCodeAndRelease(status) != ORT_OK) { |
| 48 | return false; | |
| 49 | } | |
| 50 | value.resize(size - 1); | |
| 51 | | |
| 52 | return true; | |
| 53 | } | |
| 54 | | |
| 55 | template <> | |
5e44a7c3Scott McKay3 years ago | 56 | bool BaseKernel::TryToGetAttribute(const char* name, int64_t& value) const noexcept { |
| 57 | return GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_int64(&info_, name, &value)) == ORT_OK; | |
c891e5d7Wenbing Li5 years ago | 58 | } |
| 59 | | |
| 60 | template <> | |
5e44a7c3Scott McKay3 years ago | 61 | bool BaseKernel::TryToGetAttribute(const char* name, float& value) const noexcept { |
| 62 | return GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_float(&info_, name, &value)) == ORT_OK; | |
aef5ef1eMojimi4 years ago | 63 | } |
| 64 | | |
3b0bd66eWenbing Li3 years ago | 65 | template <> |
| 66 | bool BaseKernel::TryToGetAttribute(const char* name, int& value) const noexcept { | |
| 67 | int64_t origin_value = 0; | |
| 68 | if (GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_int64(&info_, name, &origin_value)) != ORT_OK) { | |
| 69 | return false; | |
| 70 | } | |
| 71 | | |
| 72 | value = ort_extensions::narrow<int>(origin_value); | |
| 73 | return true; | |
| 74 | } | |
| 75 | | |
aef5ef1eMojimi4 years ago | 76 | template <> |
5e44a7c3Scott McKay3 years ago | 77 | bool BaseKernel::TryToGetAttribute(const char* name, bool& value) const noexcept { |
aef5ef1eMojimi4 years ago | 78 | int64_t origin_value = 0; |
5e44a7c3Scott McKay3 years ago | 79 | if (GetErrorCodeAndRelease(api_.KernelInfoGetAttribute_int64(&info_, name, &origin_value)) != ORT_OK) { |
aef5ef1eMojimi4 years ago | 80 | return false; |
| 81 | } | |
| 82 | | |
| 83 | value = origin_value == 1; | |
| 84 | return true; | |
| 85 | } |