• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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 #include "tensorflow/compiler/tf2xla/kernels/tensor_list_utils.h"
17 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
18 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
19 #include "tensorflow/compiler/xla/client/xla_builder.h"
20 #include "tensorflow/core/lib/core/errors.h"
21 
22 namespace tensorflow {
23 namespace {
24 
25 class AddNOp : public XlaOpKernel {
26  public:
AddNOp(OpKernelConstruction * ctx)27   explicit AddNOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
28 
Compile(XlaOpKernelContext * ctx)29   void Compile(XlaOpKernelContext* ctx) override {
30     if (!ctx->ValidateInputsAreSameShape(this)) return;
31 
32     OP_REQUIRES(ctx, ctx->num_inputs() >= 1,
33                 errors::InvalidArgument("AddN requires at least one argument"));
34 
35     XlaExpression::Kind kind = ctx->InputExpression(0).kind();
36     xla::XlaOp sum;
37     switch (kind) {
38       case XlaExpression::Kind::kTensorList: {
39         OP_REQUIRES_OK(ctx, GetTensorListBuffer(ctx->Input(0), &sum));
40         TensorShape sum_shape;
41         OP_REQUIRES_OK(ctx,
42                        GetTensorListBufferShape(ctx->Input(0), &sum_shape));
43         for (int i = 1; i < ctx->num_inputs(); ++i) {
44           xla::XlaOp operand;
45           OP_REQUIRES_OK(ctx, GetTensorListBuffer(ctx->Input(i), &operand));
46           // Check that the shapes match.
47           TensorShape operand_shape;
48           OP_REQUIRES_OK(
49               ctx, GetTensorListBufferShape(ctx->Input(i), &operand_shape));
50           OP_REQUIRES(
51               ctx, sum_shape.dim_sizes() == operand_shape.dim_sizes(),
52               errors::InvalidArgument(
53                   "TensorList arguments to AddN must all have the same ",
54                   "shape.\n", "Expected: ", sum_shape.DebugString(), "\n",
55                   "Found: ", operand_shape.DebugString()));
56           sum = xla::Add(sum, operand);
57         }
58         xla::XlaOp push_index;
59         OP_REQUIRES_OK(ctx, GetTensorListPushIndex(ctx->Input(0), &push_index));
60         OP_REQUIRES_OK(ctx, BuildTensorList(sum, push_index, &sum));
61         break;
62       }
63       default:
64         sum = ctx->Input(0);
65         for (int i = 1; i < ctx->num_inputs(); ++i) {
66           sum = xla::Add(sum, ctx->Input(i));
67         }
68     }
69 
70     ctx->SetOutput(0, sum);
71   }
72 
73  private:
74   TF_DISALLOW_COPY_AND_ASSIGN(AddNOp);
75 };
76 
77 REGISTER_XLA_OP(Name("AddN").AllowVariantTypes(), AddNOp);
78 
79 }  // namespace
80 }  // namespace tensorflow
81