Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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 #ifndef TENSORFLOW_KERNELS_RESHAPE_OP_H_
     17 #define TENSORFLOW_KERNELS_RESHAPE_OP_H_
     18 
     19 #include <memory>
     20 #include "tensorflow/core/framework/op_kernel.h"
     21 #include "tensorflow/core/framework/register_types.h"
     22 #include "tensorflow/core/framework/tensor.h"
     23 #include "tensorflow/core/framework/tensor_shape.h"
     24 #include "tensorflow/core/framework/types.h"
     25 #include "tensorflow/core/lib/core/status.h"
     26 #include "tensorflow/core/platform/logging.h"
     27 
     28 namespace tensorflow {
     29 
     30 // Note that this op is subclassed for QuantizedReshapeOp.
     31 class ReshapeOp : public OpKernel {
     32  public:
     33   explicit ReshapeOp(OpKernelConstruction* context) : OpKernel(context) {}
     34 
     35   void Compute(OpKernelContext* context) override {
     36     const Tensor& input = context->input(0);
     37     const Tensor& sizes = context->input(1);
     38     // Preliminary validation of sizes.
     39     OP_REQUIRES(context, IsLegacyVector(sizes.shape()),
     40                 errors::InvalidArgument("sizes input must be 1-D, not ",
     41                                         sizes.shape().DebugString()));
     42 
     43     // Compute the output shape.  Determine product of specified
     44     // dimensions, and find the index of the unspecified one.
     45     TensorShape shape;
     46     int64 product = 1;
     47     int unknown_index = -1;
     48     switch (sizes.dtype()) {
     49       case DT_INT32:
     50         OP_REQUIRES_OK(context, ValidateSizes<int32>(sizes, &product,
     51                                                      &unknown_index, &shape));
     52         break;
     53       case DT_INT64:
     54         OP_REQUIRES_OK(context, ValidateSizes<int64>(sizes, &product,
     55                                                      &unknown_index, &shape));
     56         break;
     57       default:
     58         context->CtxFailure(errors::InvalidArgument(
     59             "desired shape must be a DT_INT32 or DT_INT64 vector, not a ",
     60             DataTypeString(sizes.dtype())));
     61         return;
     62     }
     63     if (unknown_index != -1) {
     64       OP_REQUIRES(
     65           context, product > 0,
     66           errors::InvalidArgument("Reshape cannot infer the missing input size "
     67                                   "for an empty tensor unless all specified "
     68                                   "input sizes are non-zero"));
     69       const int64 missing = input.NumElements() / product;
     70       OP_REQUIRES(
     71           context, product * missing == input.NumElements(),
     72           errors::InvalidArgument(
     73               "Input to reshape is a tensor with ", input.NumElements(),
     74               " values, but the requested shape requires a multiple of ",
     75               product));
     76       shape.set_dim(unknown_index, missing);
     77     }
     78     OP_REQUIRES(context, shape.num_elements() == input.NumElements(),
     79                 errors::InvalidArgument("Input to reshape is a tensor with ",
     80                                         input.NumElements(),
     81                                         " values, but the requested shape has ",
     82                                         shape.num_elements()));
     83 
     84     // Actually produce the reshaped output.
     85     Tensor output(input.dtype());
     86     CHECK(output.CopyFrom(input, shape));
     87     context->set_output(0, output);
     88   }
     89 
     90   bool IsExpensive() override { return false; }
     91 
     92  private:
     93   template <typename Tshape>
     94   Status ValidateSizes(const Tensor& sizes, int64* product, int* unknown_index,
     95                        TensorShape* shape) {
     96     *product = 1;
     97     *unknown_index = -1;
     98     const int64 num_dims = sizes.NumElements();
     99     auto Svec = sizes.flat<Tshape>();
    100     for (int d = 0; d < num_dims; ++d) {
    101       const Tshape size = Svec(d);
    102       if (size == -1) {
    103         if (*unknown_index != -1) {
    104           return errors::InvalidArgument(
    105               "Only one input size may be -1, not both ", *unknown_index,
    106               " and ", d);
    107         }
    108         *unknown_index = d;
    109         shape->AddDim(1);
    110       } else if (size < 0) {
    111         return errors::InvalidArgument("Size ", d,
    112                                        " must be non-negative, not ", size);
    113       } else {
    114         shape->AddDim(size);
    115         (*product) *= size;
    116       }
    117     }
    118     return Status::OK();
    119   }
    120 };
    121 
    122 }  // namespace tensorflow
    123 
    124 #endif  // TENSORFLOW_KERNELS_RESHAPE_OP_H_
    125