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 <algorithm> 19 #include <numeric> 20 #include <unordered_map> 21 #include <utility> 22 23 #include "tensorflow/core/framework/op_kernel.h" 24 #include "tensorflow/core/framework/register_types.h" 25 #include "tensorflow/core/framework/tensor.h" 26 #include "tensorflow/core/framework/tensor_util.h" 27 #include "tensorflow/core/framework/types.h" 28 #include "tensorflow/core/lib/gtl/inlined_vector.h" 29 #include "tensorflow/core/util/sparse/sparse_tensor.h" 30 31 namespace tensorflow { 32 33 template <typename T> 34 class SparseReorderOp : public OpKernel { 35 public: 36 explicit SparseReorderOp(OpKernelConstruction* context) : OpKernel(context) {} 37 38 void Compute(OpKernelContext* context) override { 39 const Tensor& input_ind = context->input(0); 40 OP_REQUIRES(context, TensorShapeUtils::IsMatrix(input_ind.shape()), 41 errors::InvalidArgument( 42 "Input indices should be a matrix but received shape ", 43 input_ind.shape().DebugString())); 44 45 const Tensor& input_val = context->input(1); 46 OP_REQUIRES(context, TensorShapeUtils::IsVector(input_val.shape()), 47 errors::InvalidArgument( 48 "Input values should be a vector but received shape ", 49 input_val.shape().DebugString())); 50 51 const Tensor& input_shape_in = context->input(2); 52 OP_REQUIRES(context, TensorShapeUtils::IsVector(input_shape_in.shape()), 53 errors::InvalidArgument( 54 "Input shape should be a vector but received shape ", 55 input_shape_in.shape().DebugString())); 56 57 const TensorShape input_shape(input_shape_in.vec<int64>()); 58 59 gtl::InlinedVector<int64, 8> std_order(input_shape.dims()); 60 std::iota(std_order.begin(), std_order.end(), 0); 61 62 // Check if the sparse tensor is already ordered correctly 63 sparse::SparseTensor input_sp(input_ind, input_val, input_shape, std_order); 64 65 if (input_sp.IndicesValid().ok()) { 66 context->set_output(0, input_sp.indices()); 67 context->set_output(1, input_sp.values()); 68 } else { 69 // Deep-copy the input Tensors, then reorder in-place 70 sparse::SparseTensor reordered_sp(tensor::DeepCopy(input_ind), 71 tensor::DeepCopy(input_val), 72 input_shape); 73 reordered_sp.Reorder<T>(std_order); 74 context->set_output(0, reordered_sp.indices()); 75 context->set_output(1, reordered_sp.values()); 76 } 77 } 78 }; 79 80 #define REGISTER_KERNELS(type) \ 81 REGISTER_KERNEL_BUILDER( \ 82 Name("SparseReorder").Device(DEVICE_CPU).TypeConstraint<type>("T"), \ 83 SparseReorderOp<type>) 84 85 TF_CALL_ALL_TYPES(REGISTER_KERNELS); 86 #undef REGISTER_KERNELS 87 } // namespace tensorflow 88