microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0a9a3a9b2ff80e187f99c8266a89bc64ba44bc13

Branches

Tags

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

Clone

HTTPS

Download ZIP

operators/text/string_concat.cc

55lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4#include "string_concat.hpp"
5#include "string_tensor.h"
6#include <vector>
7#include <locale>
8#include <codecvt>
9#include <algorithm>
10
11KernelStringConcat::KernelStringConcat(const OrtApi& api, const OrtKernelInfo& info) : BaseKernel(api, info) {
12}
13
14void KernelStringConcat::Compute(OrtKernelContext* context) {
15 // Setup inputs
16 const OrtValue* left = ort_.KernelContext_GetInput(context, 0);
17 const OrtValue* right = ort_.KernelContext_GetInput(context, 1);
18 OrtTensorDimensions left_dim(ort_, left);
19 OrtTensorDimensions right_dim(ort_, right);
20
21 if (left_dim != right_dim) {
22 ORTX_CXX_API_THROW("Two input tensor should have the same dimension.", ORT_INVALID_ARGUMENT);
23 }
24
25 std::vector<std::string> left_value;
26 std::vector<std::string> right_value;
27 GetTensorMutableDataString(api_, ort_, context, left, left_value);
28 GetTensorMutableDataString(api_, ort_, context, right, right_value);
29
30 // reuse left_value as output to save memory
31 for (size_t i = 0; i < left_value.size(); i++) {
32 left_value[i].append(right_value[i]);
33 }
34
35 OrtValue* output = ort_.KernelContext_GetOutput(context, 0, left_dim.data(), left_dim.size());
36 FillTensorDataString(api_, ort_, context, left_value, output);
37}
38
39const char* CustomOpStringConcat::GetName() const { return "StringConcat"; };
40
41size_t CustomOpStringConcat::GetInputTypeCount() const {
42 return 2;
43};
44
45ONNXTensorElementDataType CustomOpStringConcat::GetInputType(size_t /*index*/) const {
46 return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING;
47};
48
49size_t CustomOpStringConcat::GetOutputTypeCount() const {
50 return 1;
51};
52
53ONNXTensorElementDataType CustomOpStringConcat::GetOutputType(size_t /*index*/) const {
54 return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING;
55};
56