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 SparseSliceOp : public OpKernel {
     27  public:
     28   explicit SparseSliceOp(OpKernelConstruction* context) : OpKernel(context) {}
     29 
     30   void Compute(OpKernelContext* context) override {
     31     const Tensor& input_indices = context->input(0);
     32     const Tensor& input_values = context->input(1);
     33     const Tensor& input_shape = context->input(2);
     34     const Tensor& input_start = context->input(3);
     35     const Tensor& input_size = context->input(4);
     36 
     37     OP_REQUIRES(context, TensorShapeUtils::IsMatrix(input_indices.shape()),
     38                 errors::InvalidArgument(
     39                     "Input indices should be a matrix but received shape ",
     40                     input_indices.shape().DebugString()));
     41     OP_REQUIRES(context, TensorShapeUtils::IsVector(input_values.shape()),
     42                 errors::InvalidArgument(
     43                     "Input values should be a vector but received shape ",
     44                     input_indices.shape().DebugString()));
     45     OP_REQUIRES(context, TensorShapeUtils::IsVector(input_shape.shape()),
     46                 errors::InvalidArgument(
     47                     "Input shape should be a vector but received shape ",
     48                     input_shape.shape().DebugString()));
     49     OP_REQUIRES(context, TensorShapeUtils::IsVector(input_start.shape()),
     50                 errors::InvalidArgument(
     51                     "Input start should be a vector but received shape ",
     52                     input_start.shape().DebugString()));
     53     OP_REQUIRES(context, TensorShapeUtils::IsVector(input_size.shape()),
     54                 errors::InvalidArgument(
     55                     "Input size should be a vector but received shape ",
     56                     input_size.shape().DebugString()));
     57 
     58     const int input_dims = input_shape.NumElements();
     59     OP_REQUIRES(context, input_dims == input_start.NumElements(),
     60                 errors::InvalidArgument(
     61                     "Expected start to be a vector of length ", input_dims,
     62                     " but got length ", input_start.NumElements()));
     63 
     64     OP_REQUIRES(context, input_dims == input_size.NumElements(),
     65                 errors::InvalidArgument(
     66                     "Expected size to be a vector of length ", input_dims,
     67                     " but got length ", input_size.NumElements()));
     68 
     69     sparse::SparseTensor sparse_tensor(input_indices, input_values,
     70                                        TensorShape(input_shape.vec<int64>()));
     71 
     72     const gtl::ArraySlice<int64> start(input_start.flat<int64>().data(),
     73                                        input_dims);
     74     const gtl::ArraySlice<int64> size(input_size.flat<int64>().data(),
     75                                       input_dims);
     76 
     77     const sparse::SparseTensor output =
     78         sparse::SparseTensor::Slice<T>(sparse_tensor, start, size);
     79 
     80     context->set_output(0, output.indices());
     81     context->set_output(1, output.values());
     82 
     83     const TensorShape output_shape(output.shape());
     84 
     85     Tensor* shape = nullptr;
     86     OP_REQUIRES_OK(context,
     87                    context->allocate_output(2, {output_shape.dims()}, &shape));
     88     for (int dim = 0; dim < output_shape.dims(); ++dim) {
     89       shape->vec<int64>()(dim) = output_shape.dim_size(dim);
     90     }
     91   }
     92 
     93  private:
     94 };
     95 
     96 #define REGISTER_KERNELS(type)                                          \
     97   REGISTER_KERNEL_BUILDER(                                              \
     98       Name("SparseSlice").Device(DEVICE_CPU).TypeConstraint<type>("T"), \
     99       SparseSliceOp<type>)
    100 
    101 TF_CALL_ALL_TYPES(REGISTER_KERNELS);
    102 #undef REGISTER_KERNELS
    103 
    104 }  // namespace tensorflow
    105