microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
ocos/kernels/string_upper.cc
55lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | #include "string_upper.hpp" |
| 5 | #include <vector> |
| 6 | #include <cmath> |
| 7 | #include <algorithm> |
| 8 | |
| 9 | |
| 10 | KernelStringUpper::KernelStringUpper(OrtApi api) : BaseKernel(api) { |
| 11 | } |
| 12 | |
| 13 | void KernelStringUpper::Compute(OrtKernelContext* context) { |
| 14 | // Setup inputs |
| 15 | const OrtValue* input_X = ort_.KernelContext_GetInput(context, 0); |
| 16 | const std::string* X = ort_.GetTensorData<std::string>(input_X); |
| 17 | |
| 18 | // Setup output |
| 19 | OrtTensorDimensions dimensions(ort_, input_X); |
| 20 | OrtValue* output = ort_.KernelContext_GetOutput(context, 0, dimensions.data(), dimensions.size()); |
| 21 | std::string* out = ort_.GetTensorMutableData<std::string>(output); |
| 22 | |
| 23 | OrtTensorTypeAndShapeInfo* output_info = ort_.GetTensorTypeAndShape(output); |
| 24 | int64_t size = ort_.GetTensorShapeElementCount(output_info); |
| 25 | ort_.ReleaseTensorTypeAndShapeInfo(output_info); |
| 26 | |
| 27 | // Do computation |
| 28 | for (int64_t i = 0; i < size; i++) { |
| 29 | out[i] = X[i]; |
| 30 | std::transform(out[i].begin(), out[i].end(), out[i].begin(), ::toupper); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void* CustomOpStringUpper::CreateKernel(OrtApi api, const OrtKernelInfo* /* info */) { |
| 35 | return new KernelStringUpper(api); |
| 36 | }; |
| 37 | |
| 38 | const char* CustomOpStringUpper::GetName() const { return "StringUpper"; }; |
| 39 | |
| 40 | size_t CustomOpStringUpper::GetInputTypeCount() const { |
| 41 | return 1; |
| 42 | }; |
| 43 | |
| 44 | ONNXTensorElementDataType CustomOpStringUpper::GetInputType(size_t /*index*/) const { |
| 45 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING; |
| 46 | }; |
| 47 | |
| 48 | size_t CustomOpStringUpper::GetOutputTypeCount() const { |
| 49 | return 1; |
| 50 | }; |
| 51 | |
| 52 | ONNXTensorElementDataType CustomOpStringUpper::GetOutputType(size_t /*index*/) const { |
| 53 | return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING; |
| 54 | }; |