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 #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM
17
18 #define EIGEN_USE_GPU
19
20 #include "tensorflow/core/framework/bounds_check.h"
21 #include "tensorflow/core/framework/register_types.h"
22 #include "tensorflow/core/kernels/sparse_tensor_dense_matmul_op.h"
23 #include "tensorflow/core/util/gpu_kernel_helper.h"
24
25 namespace tensorflow {
26
27 typedef Eigen::GpuDevice GPUDevice;
28
29 template <typename T, typename Tindices, bool ADJ_A, bool ADJ_B>
SparseTensorDenseMatMulKernel(int nnz,int m,int b_rows,int b_cols,int p,const Tindices * __restrict__ a_indices,const T * __restrict__ a_values,const T * __restrict__ b,T * __restrict__ out)30 __global__ void SparseTensorDenseMatMulKernel(
31 int nnz, int m, int b_rows, int b_cols, int p,
32 const Tindices* __restrict__ a_indices, const T* __restrict__ a_values,
33 const T* __restrict__ b, T* __restrict__ out) {
34 // out_{ij} = sum_k {a_ik b_kj}
35 // out = A * B', out_{ij} = sum_k {a_ik (b')_kj}; b'_{kj} = b_{jk}
36 const int n = (ADJ_B) ? b_cols : b_rows;
37 GPU_1D_KERNEL_LOOP(index, nnz * p) {
38 const int a_ix = index / p;
39 const int j = index % p;
40 const int i = ldg(a_indices + 2 * a_ix + ((ADJ_A) ? 1 : 0));
41 const int k = ldg(a_indices + 2 * a_ix + ((ADJ_A) ? 0 : 1));
42 if (!FastBoundsCheck(i, m)) {
43 continue; // Nowhere to signal an error :(
44 }
45 // out[i, j]
46 T* out_location = out + i * p + j;
47 if (!FastBoundsCheck(k, n)) {
48 GpuAtomicAdd(out_location, std::numeric_limits<T>::quiet_NaN());
49 continue;
50 }
51
52 // a_value == (ADJ_A) ? a[k, i] : a[i, k]
53 const T a_value = ldg(a_values + a_ix);
54
55 // b_value == (ADJ_B) ? b[j, k] : b[k, j]
56 const T b_value = ldg(b + ((ADJ_B) ? j * b_cols + k : k * b_cols + j));
57 GpuAtomicAdd(out_location, a_value * b_value);
58 }
59 }
60
61 namespace functor {
62
63 template <typename T, typename Tindices, bool ADJ_A, bool ADJ_B>
64 struct SparseTensorDenseMatMulFunctor<GPUDevice, T, Tindices, ADJ_A, ADJ_B> {
65 static EIGEN_ALWAYS_INLINE Status
Computetensorflow::functor::SparseTensorDenseMatMulFunctor66 Compute(const GPUDevice& d, typename TTypes<T>::Matrix out,
67 typename TTypes<Tindices>::ConstMatrix a_indices,
68 typename TTypes<T>::ConstVec a_values,
69 typename TTypes<T>::ConstMatrix b) {
70 out.device(d) = out.constant(T(0));
71 int nnz = a_values.size();
72 // out = A * B, A is [m x n] and B is [n x p], out is [m x p]
73 int m = out.dimension(0);
74 int p = out.dimension(1);
75 int b_rows = b.dimension(0);
76 int b_cols = b.dimension(1);
77
78 // TODO(ebrevdo): Should this be alpha * nnz instead of
79 // out.size()? Perhaps p * nnz ?
80 GpuLaunchConfig config = GetGpuLaunchConfig(p * nnz, d);
81
82 TF_CHECK_OK(GpuLaunchKernel(
83 SparseTensorDenseMatMulKernel<T, Tindices, ADJ_A, ADJ_B>,
84 config.block_count, config.thread_per_block, 0, d.stream(), nnz, m,
85 b_rows, b_cols, p, a_indices.data(), a_values.data(), b.data(),
86 out.data()));
87
88 return Status::OK();
89 }
90 };
91
92 } // namespace functor
93
94 #define DEFINE(T, Tindices) \
95 template struct functor::SparseTensorDenseMatMulFunctor< \
96 GPUDevice, T, Tindices, false, false>; \
97 template struct functor::SparseTensorDenseMatMulFunctor< \
98 GPUDevice, T, Tindices, false, true>; \
99 template struct functor::SparseTensorDenseMatMulFunctor< \
100 GPUDevice, T, Tindices, true, false>; \
101 template struct functor::SparseTensorDenseMatMulFunctor< \
102 GPUDevice, T, Tindices, true, true>;
103
104 DEFINE(float, int32);
105 DEFINE(float, int64);
106 #undef DEFINE
107
108 } // end namespace tensorflow
109
110 #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM
111