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-specific reshape Op.
     17 
     18 #include "tensorflow/compiler/tf2xla/type_util.h"
     19 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
     20 #include "tensorflow/compiler/tf2xla/xla_op_kernel.h"
     21 #include "tensorflow/compiler/tf2xla/xla_op_registry.h"
     22 #include "tensorflow/compiler/xla/literal_util.h"
     23 #include "tensorflow/core/framework/op_kernel.h"
     24 #include "tensorflow/core/framework/register_types.h"
     25 #include "tensorflow/core/framework/tensor.h"
     26 
     27 namespace tensorflow {
     28 namespace {
     29 
     30 class ReshapeOp : public XlaOpKernel {
     31  public:
     32   explicit ReshapeOp(OpKernelConstruction* ctx) : XlaOpKernel(ctx) {}
     33 
     34   void Compile(XlaOpKernelContext* ctx) override {
     35     const TensorShape input_shape = ctx->InputShape(0);
     36     const TensorShape sizes_shape = ctx->InputShape(1);
     37     // Preliminary validation of sizes.
     38     OP_REQUIRES(ctx, IsLegacyVector(sizes_shape),
     39                 errors::InvalidArgument("sizes input must be 1-D, not shape ",
     40                                         sizes_shape.DebugString()));
     41     const int64 num_dims = sizes_shape.num_elements();
     42 
     43     xla::Literal literal;
     44     OP_REQUIRES_OK(ctx, ctx->ConstantInput(1, &literal));
     45 
     46     // Compute the output shape.  Determine product of specified
     47     // dimensions, and find the index of the unspecified one if there
     48     // is one.
     49     TensorShape shape;
     50     int64 product = 1;
     51     int unknown_index = -1;
     52     for (int d = 0; d < num_dims; ++d) {
     53       const int32 size = literal.Get<int>({d});
     54       if (size == -1) {
     55         OP_REQUIRES(
     56             ctx, unknown_index == -1,
     57             errors::InvalidArgument("only one input size may be -1, not both ",
     58                                     unknown_index, " and ", d));
     59         unknown_index = d;
     60         shape.AddDim(1);
     61       } else {
     62         OP_REQUIRES(ctx, size >= 0,
     63                     errors::InvalidArgument(
     64                         "size ", d, " must be non-negative, not ", size));
     65         shape.AddDim(size);
     66         product *= size;
     67       }
     68     }
     69     if (unknown_index != -1) {
     70       OP_REQUIRES(
     71           ctx, product > 0,
     72           errors::InvalidArgument("Reshape cannot infer the missing input size "
     73                                   "for an empty tensor unless all specified "
     74                                   "input sizes are non-zero"));
     75       const int64 missing = input_shape.num_elements() / product;
     76       OP_REQUIRES(
     77           ctx, product * missing == input_shape.num_elements(),
     78           errors::InvalidArgument(
     79               "Input to reshape is a tensor with ", input_shape.num_elements(),
     80               " values, but the requested shape requires a multiple of ",
     81               product));
     82       shape.set_dim(unknown_index, missing);
     83     }
     84     OP_REQUIRES(ctx, shape.num_elements() == input_shape.num_elements(),
     85                 errors::InvalidArgument("Input to reshape is a tensor with ",
     86                                         input_shape.num_elements(),
     87                                         " values, but the requested shape has ",
     88                                         shape.num_elements()));
     89 
     90     VLOG(1) << "Reshape " << input_shape.DebugString() << " "
     91             << shape.DebugString();
     92 
     93     ctx->SetOutput(0,
     94                    ctx->builder()->Reshape(ctx->Input(0), shape.dim_sizes()));
     95   }
     96 };
     97 
     98 REGISTER_XLA_OP(Name("Reshape").CompileTimeConstInput("shape"), ReshapeOp);
     99 
    100 }  // namespace
    101 }  // namespace tensorflow
    102