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/parse_ops.cc.
     17 
     18 #include <algorithm>
     19 #include "tensorflow/core/framework/op_kernel.h"
     20 #include "tensorflow/core/framework/tensor.h"
     21 #include "tensorflow/core/framework/tensor_shape.h"
     22 #include "tensorflow/core/framework/types.h"
     23 #include "tensorflow/core/lib/core/errors.h"
     24 #include "tensorflow/core/platform/cpu_info.h"
     25 
     26 namespace tensorflow {
     27 
     28 template <typename T>
     29 class DecodeRawOp : public OpKernel {
     30  public:
     31   explicit DecodeRawOp(OpKernelConstruction* context) : OpKernel(context) {
     32     OP_REQUIRES_OK(context, context->GetAttr("little_endian", &little_endian_));
     33     OP_REQUIRES_OK(context, context->GetAttr("out_type", &out_type_));
     34   }
     35 
     36   void Compute(OpKernelContext* context) override {
     37     const auto& input = context->input(0);
     38     int64 str_size = -1;
     39     auto flat_in = input.flat<string>();
     40     for (int64 i = 0; i < flat_in.size(); ++i) {
     41       const string& in_str = flat_in(i);
     42       if (str_size == -1) {
     43         str_size = in_str.size();
     44       } else {
     45         OP_REQUIRES(context, str_size == in_str.size(),
     46                     errors::InvalidArgument(
     47                         "DecodeRaw requires input strings to all be the same "
     48                         "size, but element ",
     49                         i, " has size ", str_size, " != ", in_str.size()));
     50       }
     51     }
     52     TensorShape out_shape = input.shape();
     53     if (str_size == -1 || str_size == 0) {  // Empty input
     54       out_shape.AddDim(0);
     55       Tensor* output_tensor = nullptr;
     56       OP_REQUIRES_OK(context, context->allocate_output("output", out_shape,
     57                                                        &output_tensor));
     58       return;
     59     }
     60     OP_REQUIRES(
     61         context, str_size % sizeof(T) == 0,
     62         errors::InvalidArgument("Input to DecodeRaw has length ", str_size,
     63                                 " that is not a multiple of ", sizeof(T),
     64                                 ", the size of ", DataTypeString(out_type_)));
     65     const int64 added_dim = str_size / sizeof(T);
     66     out_shape.AddDim(added_dim);
     67     Tensor* output_tensor = nullptr;
     68     OP_REQUIRES_OK(
     69         context, context->allocate_output("output", out_shape, &output_tensor));
     70     auto out = output_tensor->flat_inner_dims<T>();
     71     DCHECK_EQ(flat_in.size(), out.dimensions()[0]);
     72     T* out_data = out.data();
     73     if (port::kLittleEndian == little_endian_ || sizeof(T) == 1) {
     74       for (int64 i = 0; i < flat_in.size(); ++i) {
     75         const T* in_data = reinterpret_cast<const T*>(flat_in(i).data());
     76         memcpy(out_data, in_data, str_size);
     77         out_data += added_dim;
     78       }
     79     } else {
     80       for (int64 i = 0; i < flat_in.size(); ++i) {
     81         const char* in_data_bytes =
     82             reinterpret_cast<const char*>(flat_in(i).data());
     83         char* out_data_bytes = reinterpret_cast<char*>(out_data);
     84         const char* p = in_data_bytes;
     85         char* q = out_data_bytes;
     86         for (; p < in_data_bytes + str_size; p += sizeof(T), q += sizeof(T)) {
     87           std::reverse_copy(p, p + sizeof(T), q);
     88         }
     89         out_data += added_dim;
     90       }
     91     }
     92   }
     93 
     94  private:
     95   bool little_endian_;
     96   DataType out_type_;
     97 };
     98 
     99 #define REGISTER(type)                                                       \
    100   REGISTER_KERNEL_BUILDER(                                                   \
    101       Name("DecodeRaw").Device(DEVICE_CPU).TypeConstraint<type>("out_type"), \
    102       DecodeRawOp<type>)
    103 
    104 REGISTER(Eigen::half);
    105 REGISTER(float);
    106 REGISTER(double);
    107 REGISTER(int32);
    108 REGISTER(uint16);
    109 REGISTER(uint8);
    110 REGISTER(int16);
    111 REGISTER(int8);
    112 REGISTER(int64);
    113 
    114 #undef REGISTER
    115 
    116 }  // namespace tensorflow
    117