Home | History | Annotate | Download | only in kernels
      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 // XLA Pack operator.
     17 
     18 #include <limits>
     19 #include <vector>
     20 
     21 #include "tensorflow/compiler/tf2xla/type_util.h"
     22 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
     23 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
     24 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
     25 #include "tensorflow/compiler/xla/literal_util.h"
     26 #include "tensorflow/core/framework/op_kernel.h"
     27 #include "tensorflow/core/framework/register_types.h"
     28 #include "tensorflow/core/framework/tensor.h"
     29 #include "tensorflow/core/framework/tensor_types.h"
     30 #include "tensorflow/core/framework/types.h"
     31 #include "tensorflow/core/kernels/bounds_check.h"
     32 #include "tensorflow/core/kernels/concat_lib.h"
     33 #include "tensorflow/core/lib/core/status.h"
     34 #include "tensorflow/core/platform/types.h"
     35 
     36 namespace tensorflow {
     37 namespace {
     38 
     39 class PackOp : public XlaOpKernel {
     40  public:
     41   explicit PackOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
     42     OP_REQUIRES_OK(ctx, ctx->GetAttr("axis", &axis_));
     43   }
     44 
     45   void Compile(XlaOpKernelContext* ctx) override {
     46     std::vector<xla::ComputationDataHandle> values;
     47     std::vector<TensorShape> shapes;
     48     OP_REQUIRES_OK(ctx, ctx->InputList("values", &values, &shapes));
     49     const int num = values.size();
     50 
     51     OP_REQUIRES(ctx, num >= 0,
     52                 errors::InvalidArgument("Pack requires >= 1 arguments"));
     53 
     54     // Verify that all input shapes match
     55     for (int i = 1; i < num; i++) {
     56       OP_REQUIRES(ctx, shapes[0].IsSameSize(shapes[i]),
     57                   errors::InvalidArgument(
     58                       "Shapes of all inputs must match: values[0].shape = ",
     59                       shapes[0].DebugString(), " != values[", i, "].shape = ",
     60                       shapes[i].DebugString()));
     61     }
     62 
     63     int expanded_num_dims = shapes[0].dims() + 1;
     64     int axis = axis_;
     65     if (axis < 0) axis += expanded_num_dims;
     66 
     67     OP_REQUIRES(ctx, 0 <= axis && axis < expanded_num_dims,
     68                 errors::InvalidArgument("axis = ", axis_, " not in [",
     69                                         -expanded_num_dims, ", ",
     70                                         expanded_num_dims, ")"));
     71 
     72     std::vector<xla::ComputationDataHandle> reshaped_inputs(num);
     73 
     74     TensorShape child_shape(shapes[0]);
     75     child_shape.InsertDim(axis, 1);
     76 
     77     for (int i = 0; i < num; ++i) {
     78       // Reshape the inputs to have an extra dimension of size 1.
     79       reshaped_inputs[i] =
     80           ctx->builder()->Reshape(values[i], child_shape.dim_sizes());
     81     }
     82 
     83     ctx->SetOutput(0, ctx->builder()->ConcatInDim(reshaped_inputs, axis));
     84   }
     85 
     86  private:
     87   int axis_;
     88 };
     89 
     90 REGISTER_XLA_OP(Name("Pack"), PackOp);
     91 
     92 }  // namespace
     93 }  // namespace tensorflow
     94