1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_KERNELS_SPARSE_TENSOR_DENSE_MATMUL_OP_H_
17#define TENSORFLOW_CORE_KERNELS_SPARSE_TENSOR_DENSE_MATMUL_OP_H_
18
19#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
20#include "tensorflow/core/framework/op_kernel.h"
21#include "tensorflow/core/framework/tensor_types.h"
22#include "tensorflow/core/framework/types.h"
23#include "tensorflow/core/lib/core/errors.h"
24
25namespace tensorflow {
26
27namespace functor {
28
29template <typename Device, typename T, typename Tindices, bool ADJ_A,
30 bool ADJ_B>
31struct SparseTensorDenseMatMulFunctor {
32 static EIGEN_ALWAYS_INLINE Status Compute(
33 OpKernelContext* ctx, typename TTypes<T>::Matrix out,
34 typename TTypes<Tindices>::ConstMatrix a_indices,
35 typename TTypes<T>::ConstVec a_values, typename TTypes<T>::ConstMatrix b);
36};
37
38template <typename MATRIX, bool ADJ>
39class MaybeAdjoint;
40
41template <typename MATRIX>
42class MaybeAdjoint<MATRIX, false> {
43 public:
44 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaybeAdjoint(MATRIX m) : m_(m) {}
45 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename MATRIX::Scalar operator()(
46 const typename MATRIX::Index i, const typename MATRIX::Index j) const {
47 return m_(i, j);
48 }
49
50 private:
51 const MATRIX m_;
52};
53
54template <typename T>
55EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T MaybeConj(T v) {
56 return Eigen::numext::conj(v);
57}
58
59template <typename MATRIX>
60class MaybeAdjoint<MATRIX, true> {
61 public:
62 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaybeAdjoint(MATRIX m) : m_(m) {}
63 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename MATRIX::Scalar operator()(
64 const typename MATRIX::Index i, const typename MATRIX::Index j) const {
65 return Eigen::numext::conj(m_(j, i));
66 }
67
68 private:
69 const MATRIX m_;
70};
71
72template <typename T>
73struct SumType {
74 using type = T;
75};
76
77template <>
78struct SumType<Eigen::half> {
79 using type = float; // Use fp32 accumulator for fp16 input values
80};
81
82} // end namespace functor
83} // end namespace tensorflow
84
85#endif // TENSORFLOW_CORE_KERNELS_SPARSE_TENSOR_DENSE_MATMUL_OP_H_
86