-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description <!-- Describe your changes. --> ### Motivation and Context Closes #17595. --------- Signed-off-by: Aditya Goel <[email protected]>
- Loading branch information
1 parent
f68dfcd
commit 4694edc
Showing
9 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "string_concat.h" | ||
#include "core/providers/cpu/math/element_wise_ops.h" | ||
#include "core/common/common.h" | ||
|
||
namespace onnxruntime { | ||
ONNX_CPU_OPERATOR_KERNEL(StringConcat, 20, | ||
KernelDefBuilder().TypeConstraint("T", DataTypeImpl::GetTensorType<std::string>()), | ||
StringConcat); | ||
|
||
Status StringConcat::Compute(OpKernelContext* context) const { | ||
ProcessBroadcastSpanFuncs broadcast_funcs{[](BroadcastHelper& broadcast_helper) { | ||
auto x = broadcast_helper.ScalarInput0<std::string>(); | ||
auto y = broadcast_helper.SpanInput1<std::string>(); | ||
auto y_iter = y.begin(); | ||
auto output_iter = broadcast_helper.OutputSpan<std::string>().begin(); | ||
const auto x_size = x.length(); | ||
while (y_iter != y.end()) { | ||
output_iter->reserve(x_size + y_iter->length()); | ||
output_iter->append(x); | ||
output_iter->append(*y_iter); | ||
y_iter++; | ||
output_iter++; | ||
} | ||
}, | ||
[](BroadcastHelper& broadcast_helper) { | ||
auto x = broadcast_helper.SpanInput0<std::string>(); | ||
auto x_iter = x.begin(); | ||
auto y = broadcast_helper.ScalarInput1<std::string>(); | ||
auto output_iter = broadcast_helper.OutputSpan<std::string>().begin(); | ||
const auto y_size = y.length(); | ||
while (x_iter != x.end()) { | ||
output_iter->reserve(y_size + x_iter->length()); | ||
output_iter->append(*x_iter); | ||
output_iter->append(y); | ||
x_iter++; | ||
output_iter++; | ||
} | ||
}, | ||
[](BroadcastHelper& broadcast_helper) { | ||
auto x_iter = broadcast_helper.SpanInput0<std::string>().begin(); | ||
auto y_iter = broadcast_helper.SpanInput1<std::string>().begin(); | ||
auto output = broadcast_helper.OutputSpan<std::string>(); | ||
auto output_iter = output.begin(); | ||
while (output_iter != output.end()) { | ||
output_iter->reserve(x_iter->length() + y_iter->length()); | ||
output_iter->append(*x_iter); | ||
output_iter->append(*y_iter); | ||
x_iter++; | ||
y_iter++; | ||
output_iter++; | ||
} | ||
}}; | ||
UntypedBroadcastTwo(*context, broadcast_funcs); | ||
return Status::OK(); | ||
} | ||
|
||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
|
||
#include "core/framework/op_kernel.h" | ||
|
||
namespace onnxruntime { | ||
|
||
class StringConcat final : public OpKernel { | ||
public: | ||
StringConcat(const OpKernelInfo& info) : OpKernel(info) {} | ||
|
||
Status Compute(OpKernelContext* context) const override; | ||
}; | ||
|
||
} // namespace onnxruntime |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include "gtest/gtest.h" | ||
#include "test/providers/provider_test_utils.h" | ||
|
||
namespace onnxruntime { | ||
namespace test { | ||
|
||
static void RunTest(const std::vector<int64_t>& dims, const std::vector<std::string>& input1, | ||
const std::vector<std::string>& input2, const std::vector<std::string>& output) { | ||
OpTester test("StringConcat", 20, onnxruntime::kOnnxDomain); | ||
test.AddInput<std::string>("X", dims, input1); | ||
test.AddInput<std::string>("Y", dims, input2); | ||
test.AddOutput<std::string>("Z", dims, output); | ||
test.Run(); | ||
} | ||
|
||
TEST(StringConcat, BasicConcatenation) { | ||
RunTest({1, 2}, {"Hello", "World"}, {"Hello", "World"}, {"HelloHello", "WorldWorld"}); | ||
} | ||
|
||
TEST(StringConcat, TwoDimensionalConcatenation) { | ||
RunTest({2, 2}, {"Hello", "World", "ONNX", "onnxruntime"}, {"Hello", "World", "ONNX", "onnxruntime"}, | ||
{"HelloHello", "WorldWorld", "ONNXONNX", "onnxruntimeonnxruntime"}); | ||
} | ||
|
||
TEST(StringConcat, LeftToRightBroadcastingConcatenation) { | ||
OpTester test("StringConcat", 20, onnxruntime::kOnnxDomain); | ||
test.AddInput<std::string>("X", {2, 2}, {"Hello", "World", "ONNX", "onnxruntime"}); | ||
test.AddInput<std::string>("Y", {1}, {"!"}); | ||
test.AddOutput<std::string>("Z", {2, 2}, {"Hello!", "World!", "ONNX!", "onnxruntime!"}); | ||
test.Run(); | ||
} | ||
|
||
TEST(StringConcat, RightToLeftBroadcastingConcatenation) { | ||
OpTester test("StringConcat", 20, onnxruntime::kOnnxDomain); | ||
test.AddInput<std::string>("X", {1}, {"!"}); | ||
test.AddInput<std::string>("Y", {2, 2}, {"Hello", "World", "ONNX", "onnxruntime"}); | ||
test.AddOutput<std::string>("Z", {2, 2}, {"!Hello", "!World", "!ONNX", "!onnxruntime"}); | ||
test.Run(); | ||
} | ||
|
||
TEST(StringConcat, BidirectionalBroadcastingConcatenation) { | ||
OpTester test("StringConcat", 20, onnxruntime::kOnnxDomain); | ||
test.AddInput<std::string>("X", {2, 1, 3}, {"a", "b", "c", "d", "e", "f"}); | ||
test.AddInput<std::string>("Y", {1, 4, 3}, {"a", "b", "c", "d", "e", "f", "g", "h", "i", "k", "l", "m"}); | ||
test.AddOutput<std::string>("Z", {2, 4, 3}, | ||
{ | ||
"aa", | ||
"bb", | ||
"cc", | ||
"ad", | ||
"be", | ||
"cf", | ||
"ag", | ||
"bh", | ||
"ci", | ||
"ak", | ||
"bl", | ||
"cm", | ||
"da", | ||
"eb", | ||
"fc", | ||
"dd", | ||
"ee", | ||
"ff", | ||
"dg", | ||
"eh", | ||
"fi", | ||
"dk", | ||
"el", | ||
"fm", | ||
}); | ||
test.Run(); | ||
} | ||
|
||
} // namespace test | ||
} // namespace onnxruntime |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters