microsoft/onnxruntime-extensions

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2ef88b0bda3208844b2bee2ea682bc76a4085c63

Branches

Tags

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

Clone

HTTPS

Download ZIP

ocos/kernels/string_regex_replace.cc

73lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4#include "string_regex_replace.hpp"
5#include <vector>
6#include <cmath>
7#include <algorithm>
8#include "re2/re2.h"
9
10KernelStringRegexReplace::KernelStringRegexReplace(OrtApi api) : BaseKernel(api) {
11}
12
13void KernelStringRegexReplace::Compute(OrtKernelContext* context) {
14 // Setup inputs
15 const OrtValue* input = ort_.KernelContext_GetInput(context, 0);
16 const std::string* str_input = ort_.GetTensorData<std::string>(input);
17 const OrtValue* pattern = ort_.KernelContext_GetInput(context, 1);
18 const std::string* str_pattern = ort_.GetTensorData<std::string>(pattern);
19 const OrtValue* rewrite = ort_.KernelContext_GetInput(context, 2);
20 const std::string* str_rewrite = ort_.GetTensorData<std::string>(rewrite);
21
22 // Verifications
23 OrtTensorDimensions pattern_dimensions(ort_, pattern);
24 OrtTensorDimensions rewrite_dimensions(ort_, rewrite);
25 if (pattern_dimensions.size() != 1 || pattern_dimensions[0] != 1)
26 throw std::runtime_error(MakeString(
27 "pattern (second input) must contain only one element. It has ",
28 pattern_dimensions.size(), " dimensions."));
29 if (rewrite_dimensions.size() != 1 || rewrite_dimensions[0] != 1)
30 throw std::runtime_error(MakeString(
31 "rewrite (third input) must contain only one element. It has ",
32 rewrite_dimensions.size(), " dimensions."));
33
34 // Setup output
35 OrtTensorDimensions dimensions(ort_, input);
36 OrtValue* output = ort_.KernelContext_GetOutput(context, 0, dimensions.data(), dimensions.size());
37 std::string* out = ort_.GetTensorMutableData<std::string>(output);
38
39 OrtTensorTypeAndShapeInfo* output_info = ort_.GetTensorTypeAndShape(output);
40 int64_t size = ort_.GetTensorShapeElementCount(output_info);
41 ort_.ReleaseTensorTypeAndShapeInfo(output_info);
42
43 re2::StringPiece piece(*str_rewrite);
44 re2::RE2 reg(*str_pattern);
45
46 // Do computation
47 for (int64_t i = 0; i < size; i++) {
48 out[i] = str_input[i];
49 re2::RE2::Replace(out + i, reg, piece);
50 }
51}
52
53void* CustomOpStringRegexReplace::CreateKernel(OrtApi api, const OrtKernelInfo* /* info */) {
54 return new KernelStringRegexReplace(api);
55};
56
57const char* CustomOpStringRegexReplace::GetName() const { return "StringRegexReplace"; };
58
59size_t CustomOpStringRegexReplace::GetInputTypeCount() const {
60 return 3;
61};
62
63ONNXTensorElementDataType CustomOpStringRegexReplace::GetInputType(size_t /*index*/) const {
64 return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING;
65};
66
67size_t CustomOpStringRegexReplace::GetOutputTypeCount() const {
68 return 1;
69};
70
71ONNXTensorElementDataType CustomOpStringRegexReplace::GetOutputType(size_t /*index*/) const {
72 return ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING;
73};
74