1 /* Copyright 2016 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/sparse_ops.cc. 17 18 #define EIGEN_USE_THREADS 19 20 #include <numeric> 21 22 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor" 23 #include "tensorflow/core/framework/op_kernel.h" 24 #include "tensorflow/core/framework/op_requires.h" 25 #include "tensorflow/core/framework/register_types.h" 26 #include "tensorflow/core/framework/tensor.h" 27 #include "tensorflow/core/framework/tensor_util.h" 28 #include "tensorflow/core/framework/types.h" 29 #include "tensorflow/core/util/sparse/sparse_tensor.h" 30 31 using tensorflow::gtl::ArraySlice; 32 using tensorflow::sparse::SparseTensor; 33 34 namespace tensorflow { 35 36 using CPUDevice = Eigen::ThreadPoolDevice; 37 38 template <typename Device, typename T> 39 class SparseSoftmaxOp : public OpKernel { 40 public: SparseSoftmaxOp(OpKernelConstruction * context)41 explicit SparseSoftmaxOp(OpKernelConstruction *context) : OpKernel(context) {} 42 Compute(OpKernelContext * context)43 void Compute(OpKernelContext *context) override { 44 const Tensor *indices_t, *values_t, *shape_t; 45 OP_REQUIRES_OK(context, context->input("sp_indices", &indices_t)); 46 OP_REQUIRES_OK(context, context->input("sp_values", &values_t)); 47 OP_REQUIRES_OK(context, context->input("sp_shape", &shape_t)); 48 49 // Validations. 50 OP_REQUIRES(context, TensorShapeUtils::IsMatrix(indices_t->shape()), 51 errors::InvalidArgument( 52 "Input sp_indices should be a matrix but received shape: ", 53 indices_t->shape().DebugString())); 54 OP_REQUIRES(context, 55 TensorShapeUtils::IsVector(values_t->shape()) && 56 TensorShapeUtils::IsVector(shape_t->shape()), 57 errors::InvalidArgument( 58 "Inputs sp_values and sp_shape should be vectors " 59 "but received shapes: ", 60 values_t->shape().DebugString(), " and ", 61 shape_t->shape().DebugString())); 62 OP_REQUIRES(context, shape_t->NumElements() >= 2, 63 errors::InvalidArgument( 64 "Input should have rank >= 2, but received shape: ", 65 shape_t->SummarizeValue(3))); 66 TensorShape shape; 67 OP_REQUIRES_OK(context, TensorShape::BuildTensorShape( 68 shape_t->flat<int64_t>(), &shape)); 69 70 const int64_t nnz = indices_t->dim_size(0); 71 const int rank = static_cast<int>(indices_t->dim_size(1)); 72 SparseTensor st; 73 OP_REQUIRES_OK( 74 context, SparseTensor::Create(tensor::DeepCopy(*indices_t), 75 tensor::DeepCopy(*values_t), shape, &st)); 76 77 Tensor *output_values = nullptr; 78 OP_REQUIRES_OK(context, context->allocate_output(0, TensorShape({nnz}), 79 &output_values)); 80 typename TTypes<T>::Flat output_flat = output_values->flat<T>(); 81 82 Tensor tmp_t; 83 OP_REQUIRES_OK(context, context->allocate_temp(DataTypeToEnum<T>::value, 84 TensorShape({}), &tmp_t)); 85 typename TTypes<T>::Scalar tmp_scalar = tmp_t.scalar<T>(); 86 87 gtl::InlinedVector<int64_t, 4> dims(rank); 88 std::iota(dims.begin(), dims.end(), 0); 89 // { 0, ..., rank-1 }. 90 const ArraySlice<int64_t> kReorderDims(dims); 91 // All but the last dim -- the class dimension to be max-reduced along. 92 const ArraySlice<int64_t> kGroupByDims = kReorderDims.subspan(0, rank - 1); 93 st.Reorder<T>(kReorderDims); 94 int count = 0; 95 96 // The SparseTensor has logical shape [..., b, c], where the 97 // innermost size-"c" dimension is the class dimension to be max-reduced. 98 // Therefore we group by the first (rank - 1) dimensions. 99 const Device &device = context->eigen_device<Device>(); 100 for (const auto &g : st.group(kGroupByDims)) { 101 const auto group_vals = g.values<T>(); 102 const int group_size = group_vals.size(); 103 104 // Shifts by max, exponentiates, then renormalizes. 105 tmp_scalar.device(context->eigen_device<Device>()) = group_vals.maximum(); 106 const T group_max = tmp_scalar(); 107 108 Eigen::Tensor<T, 1, Eigen::RowMajor> tmp(group_size); 109 tmp.device(device) = (group_vals - tmp.constant(group_max)).exp(); 110 111 tmp_scalar.device(device) = tmp.sum().inverse(); 112 tmp.device(device) = tmp * tmp.constant(tmp_scalar()); 113 114 // Assigns back to output[count, count + group_size). 115 Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor>> output_part( 116 output_flat.data() + count, group_size); 117 output_part.device(device) = tmp; 118 119 count += group_size; 120 } 121 } 122 }; 123 124 #define REGISTER_KERNEL(T) \ 125 REGISTER_KERNEL_BUILDER( \ 126 Name("SparseSoftmax").Device(DEVICE_CPU).TypeConstraint<T>("T"), \ 127 SparseSoftmaxOp<CPUDevice, T>) 128 129 REGISTER_KERNEL(float); 130 REGISTER_KERNEL(double); 131 #undef REGISTER_KERNEL 132 133 } // namespace tensorflow 134