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 | #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 | |
25 | namespace tensorflow { |
26 | |
27 | namespace functor { |
28 | |
29 | template <typename Device, typename T, typename Tindices, bool ADJ_A, |
30 | bool ADJ_B> |
31 | struct 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 | |
38 | template <typename MATRIX, bool ADJ> |
39 | class MaybeAdjoint; |
40 | |
41 | template <typename MATRIX> |
42 | class 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 | |
54 | template <typename T> |
55 | EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T MaybeConj(T v) { |
56 | return Eigen::numext::conj(v); |
57 | } |
58 | |
59 | template <typename MATRIX> |
60 | class 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 | |
72 | template <typename T> |
73 | struct SumType { |
74 | using type = T; |
75 | }; |
76 | |
77 | template <> |
78 | struct 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 | |