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 // See docs in ../ops/array_ops.cc.
     17 #include "tensorflow/core/framework/op_kernel.h"
     18 #include "tensorflow/core/framework/register_types.h"
     19 #include "tensorflow/core/framework/tensor.h"
     20 #include "tensorflow/core/framework/tensor_shape.h"
     21 #include "tensorflow/core/framework/types.h"
     22 #include "tensorflow/core/lib/core/casts.h"
     23 
     24 namespace tensorflow {
     25 
     26 class BitcastOp : public OpKernel {
     27  public:
     28   explicit BitcastOp(OpKernelConstruction* context) : OpKernel(context) {
     29     OP_REQUIRES_OK(context, context->GetAttr("T", &input_data_type_));
     30     OP_REQUIRES_OK(context, context->GetAttr("type", &output_data_type_));
     31     in_size_ = DataTypeSize(input_data_type_);
     32     out_size_ = DataTypeSize(output_data_type_);
     33     int check_size =
     34         std::max(in_size_, out_size_) % std::min(in_size_, out_size_);
     35     OP_REQUIRES(
     36         context, check_size == 0,
     37         errors::InvalidArgument("cannot convert between datatype ",
     38                                 input_data_type_, " and ", output_data_type_));
     39   }
     40 
     41   void Compute(OpKernelContext* context) override {
     42     const Tensor& input_tensor = context->input(0);
     43 
     44     TensorShape adjusted_shape = input_tensor.shape();
     45     OP_REQUIRES(context,
     46                 in_size_ >= out_size_ ||
     47                     (input_tensor.dims() > 0 &&
     48                      input_tensor.dim_size(input_tensor.dims() - 1) ==
     49                          out_size_ / in_size_) ||
     50                     input_tensor.dim_size(input_tensor.dims()) == -1,
     51                 errors::InvalidArgument(
     52                     "Cannot bitcast from ", DataTypeString(input_data_type_),
     53                     " to ", DataTypeString(output_data_type_), ": shape ",
     54                     input_tensor.shape().DebugString()));
     55 
     56     if (out_size_ < in_size_) {
     57       adjusted_shape.AddDim(in_size_ / out_size_);
     58     } else if (out_size_ > in_size_) {
     59       adjusted_shape.RemoveDim(input_tensor.dims() - 1);
     60     }
     61     Tensor output_tensor;
     62 
     63     output_tensor.UnsafeCopyFromInternal(input_tensor, output_data_type_,
     64                                          adjusted_shape);
     65     context->set_output(0, output_tensor);
     66   }
     67 
     68   bool IsExpensive() override { return false; }
     69 
     70  private:
     71   DataType input_data_type_;
     72   DataType output_data_type_;
     73   int in_size_;
     74   int out_size_;
     75 };
     76 
     77 REGISTER_KERNEL_BUILDER(Name("Bitcast").Device(DEVICE_CPU), BitcastOp);
     78 
     79 #if GOOGLE_CUDA
     80 REGISTER_KERNEL_BUILDER(Name("Bitcast").Device(DEVICE_GPU), BitcastOp);
     81 #endif  // GOOGLE_CUDA
     82 
     83 }  // end namespace tensorflow
     84