1 | /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. |
2 | |
3 | Licensed under the Apache License, Version 2.0 (the "License"); |
4 | you may not use this file except in compliance with the License. |
5 | You may obtain a copy of the License at |
6 | |
7 | http://www.apache.org/licenses/LICENSE-2.0 |
8 | |
9 | Unless required by applicable law or agreed to in writing, software |
10 | distributed under the License is distributed on an "AS IS" BASIS, |
11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | See the License for the specific language governing permissions and |
13 | limitations under the License. |
14 | ==============================================================================*/ |
15 | |
16 | // See docs in ../ops/string_ops.cc. |
17 | |
18 | #include <string> |
19 | |
20 | #include "tensorflow/core/framework/kernel_def_builder.h" |
21 | #include "tensorflow/core/framework/op_kernel.h" |
22 | #include "tensorflow/core/framework/tensor.h" |
23 | #include "tensorflow/core/lib/core/errors.h" |
24 | #include "tensorflow/core/lib/core/status.h" |
25 | #include "tensorflow/core/lib/strings/str_util.h" |
26 | |
27 | namespace tensorflow { |
28 | |
29 | class StringJoinOp : public OpKernel { |
30 | public: |
31 | using OpKernel::OpKernel; |
32 | |
33 | explicit StringJoinOp(OpKernelConstruction* ctx) : OpKernel(ctx) { |
34 | OP_REQUIRES_OK(ctx, ctx->GetAttr("separator" , &separator_)); |
35 | } |
36 | |
37 | void Compute(OpKernelContext* context) override { |
38 | OpInputList input_list; |
39 | OP_REQUIRES_OK(context, context->input_list("inputs" , &input_list)); |
40 | TensorShape input_shape; |
41 | std::vector<bool> is_scalar; |
42 | std::vector<TTypes<tstring>::ConstFlat> inputs; |
43 | |
44 | for (const auto& input : input_list) { |
45 | inputs.push_back(input.flat<tstring>()); |
46 | is_scalar.push_back(TensorShapeUtils::IsScalar(input.shape())); |
47 | if (!TensorShapeUtils::IsScalar(input.shape())) { |
48 | if (TensorShapeUtils::IsScalar(input_shape)) { |
49 | input_shape = input.shape(); |
50 | } else { |
51 | OP_REQUIRES( |
52 | context, input_shape == input.shape(), |
53 | errors::InvalidArgument( |
54 | "Input shapes do not match: " , input_shape.DebugString(), |
55 | " vs. " , input.shape().DebugString())); |
56 | } |
57 | } |
58 | } |
59 | |
60 | Tensor* output_tensor = nullptr; |
61 | OP_REQUIRES_OK(context, context->allocate_output("output" , input_shape, |
62 | &output_tensor)); |
63 | auto output_flat = output_tensor->flat<tstring>(); |
64 | |
65 | std::vector<StringPiece> strings(input_list.size()); |
66 | for (size_t i = 0; i < input_shape.num_elements(); ++i) { |
67 | for (int j = 0; j < input_list.size(); ++j) { |
68 | strings[j] = (is_scalar[j]) ? inputs[j](0) : inputs[j](i); |
69 | } |
70 | output_flat(i) = absl::StrJoin(strings, separator_); |
71 | } |
72 | } |
73 | |
74 | private: |
75 | string separator_; |
76 | }; |
77 | |
78 | REGISTER_KERNEL_BUILDER(Name("StringJoin" ).Device(DEVICE_CPU), StringJoinOp); |
79 | |
80 | } // namespace tensorflow |
81 | |