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 #define EIGEN_USE_THREADS
     17 
     18 #include <vector>
     19 #include "tensorflow/core/framework/op_kernel.h"
     20 #include "tensorflow/core/framework/register_types.h"
     21 #include "tensorflow/core/util/sparse/sparse_tensor.h"
     22 
     23 namespace tensorflow {
     24 
     25 template <typename T>
     26 class SparseSplitOp : public OpKernel {
     27  public:
     28   explicit SparseSplitOp(OpKernelConstruction* context) : OpKernel(context) {
     29     OP_REQUIRES_OK(context, context->GetAttr("num_split", &num_split_));
     30   }
     31 
     32   void Compute(OpKernelContext* context) override {
     33     const int64 split_dim = context->input(0).scalar<int64>()();
     34     const Tensor& input_indices = context->input(1);
     35     const Tensor& input_values = context->input(2);
     36     const Tensor& input_shape = context->input(3);
     37 
     38     OP_REQUIRES(context, TensorShapeUtils::IsMatrix(input_indices.shape()),
     39                 errors::InvalidArgument(
     40                     "Input indices should be a matrix but received shape ",
     41                     input_indices.shape().DebugString()));
     42     OP_REQUIRES(context, TensorShapeUtils::IsVector(input_values.shape()),
     43                 errors::InvalidArgument(
     44                     "Input values should be a vector but received shape ",
     45                     input_indices.shape().DebugString()));
     46     OP_REQUIRES(context, TensorShapeUtils::IsVector(input_shape.shape()),
     47                 errors::InvalidArgument(
     48                     "Input shape should be a vector but received shape ",
     49                     input_shape.shape().DebugString()));
     50 
     51     OP_REQUIRES(
     52         context,
     53         input_shape.dim_size(0) && split_dim < input_shape.vec<int64>().size(),
     54         errors::InvalidArgument(
     55             "Input split_dim should be between 0 and rank (",
     56             input_shape.vec<int64>().size(), "), got ", split_dim));
     57 
     58     OP_REQUIRES(
     59         context,
     60         num_split_ >= 1 && num_split_ <= input_shape.vec<int64>()(split_dim),
     61         errors::InvalidArgument("Input num_split should be between 1 "
     62                                 "and the splitting dimension size (",
     63                                 input_shape.vec<int64>()(split_dim), "), got ",
     64                                 num_split_));
     65 
     66     sparse::SparseTensor sparse_tensor(input_indices, input_values,
     67                                        TensorShape(input_shape.vec<int64>()));
     68     const std::vector<sparse::SparseTensor> outputs =
     69         sparse::SparseTensor::Split<T>(sparse_tensor, split_dim, num_split_);
     70 
     71     for (int slice_index = 0; slice_index < num_split_; ++slice_index) {
     72       context->set_output(slice_index, outputs[slice_index].indices());
     73       context->set_output(slice_index + num_split_,
     74                           outputs[slice_index].values());
     75       Tensor* shape = nullptr;
     76       OP_REQUIRES_OK(context, context->allocate_output(
     77                                   slice_index + 2 * num_split_,
     78                                   {outputs[slice_index].dims()}, &shape));
     79       auto output_shape = outputs[slice_index].shape();
     80       for (int dim = 0; dim < outputs[slice_index].dims(); ++dim) {
     81         shape->vec<int64>()(dim) = output_shape[dim];
     82       }
     83     }
     84   }
     85 
     86  private:
     87   int num_split_;
     88 };
     89 
     90 #define REGISTER_KERNELS(type)                                          \
     91   REGISTER_KERNEL_BUILDER(                                              \
     92       Name("SparseSplit").Device(DEVICE_CPU).TypeConstraint<type>("T"), \
     93       SparseSplitOp<type>)
     94 
     95 TF_CALL_ALL_TYPES(REGISTER_KERNELS);
     96 #undef REGISTER_KERNELS
     97 
     98 }  // namespace tensorflow
     99