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 Unpack 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 UnpackOp : public XlaOpKernel {
     40  public:
     41   explicit UnpackOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {
     42     OP_REQUIRES_OK(ctx, ctx->GetAttr("axis", &axis_));
     43   }
     44 
     45   void Compile(XlaOpKernelContext* ctx) override {
     46     const int num = num_outputs();
     47     const TensorShape input_shape = ctx->InputShape(0);
     48 
     49     int axis = axis_;
     50     if (axis < 0) axis += input_shape.dims();
     51 
     52     OP_REQUIRES(ctx, 0 <= axis && axis < input_shape.dims(),
     53                 errors::InvalidArgument("axis = ", axis_, " not in [",
     54                                         -input_shape.dims(), ", ",
     55                                         input_shape.dims(), ")"));
     56 
     57     OP_REQUIRES(
     58         ctx, input_shape.dims() > 0 && input_shape.dim_size(axis) == num,
     59         errors::InvalidArgument("Input shape axis ", axis, " must equal ", num,
     60                                 ", got shape ", input_shape.DebugString()));
     61 
     62     auto output_shape = input_shape;
     63     output_shape.RemoveDim(axis);
     64 
     65     auto input = ctx->Input(0);
     66 
     67     std::vector<int64> start_indices(input_shape.dims(), 0);
     68     std::vector<int64> limit_indices(input_shape.dims());
     69     std::vector<int64> strides(input_shape.dims(), 1);
     70     for (int i = 0; i < input_shape.dims(); ++i) {
     71       limit_indices[i] = input_shape.dim_size(i);
     72     }
     73 
     74     for (int i = 0; i < num; ++i) {
     75       start_indices[axis] = i;
     76       limit_indices[axis] = i + 1;
     77       auto slice = ctx->builder()->Slice(input, start_indices, limit_indices,
     78                                          strides);
     79       // Reshape to drop the 'axis' dimension.
     80       auto result = ctx->builder()->Reshape(slice, output_shape.dim_sizes());
     81       ctx->SetOutput(i, result);
     82     }
     83   }
     84 
     85  private:
     86   int axis_;
     87 };
     88 
     89 REGISTER_XLA_OP(Name("Unpack"), UnpackOp);
     90 
     91 }  // namespace
     92 }  // namespace tensorflow
     93