LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
distconv_matmul.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 #ifndef LBANN_LAYERS_MATH_DISTCONV_MATMUL
27 #define LBANN_LAYERS_MATH_DISTCONV_MATMUL
28 #include "distconv/base.hpp"
29 #include "distconv/tensor/tensor.hpp"
30 #include "distconv/tensor/tensor_mpi.hpp"
31 #include "lbann/utils/distconv.hpp"
32 
33 #ifdef LBANN_HAS_DISTCONV
34 namespace distconv {
35 template <typename Backend, typename DataType>
36 class MatMul
37 {
38  using LocalMPI = tensor::LocaleMPI;
39 
40 public:
41  MatMul(Backend& backend) : m_be(backend){};
42 
43  template <typename Allocator>
44  int forward(
45  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input_0,
46  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input_1,
47  tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& output,
48  const bool transpose_0,
49  const bool transpose_1);
50 
51  template <typename Allocator>
52  int backward(
53  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input_0,
54  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input_1,
55  const tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& output_grad,
56  tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input_grad_0,
57  tensor::Tensor<DataType, tensor::LocaleMPI, Allocator>& input_grad_1,
58  const bool transpose_0,
59  const bool transpose_1);
60 
61 protected:
62  Backend& m_be;
63 };
64 
65 template <typename DataType, typename locale, typename Allocator>
66 tensor::Shape get_matmul_local_tensor_shape(
67  const tensor::Tensor<DataType, locale, Allocator>& input_0,
68  const tensor::Tensor<DataType, locale, Allocator>& input_1,
69  bool transpose_1,
70  bool transpose_2)
71 {
72  // Use input dims to fill channel and mini-batch dimensions
73  auto output_local_shape = input_0.get_local_shape();
74 
75  auto inp_0_dims = input_0.get_local_shape();
76  auto inp_1_dims = input_1.get_local_shape();
77 
78  // Update the matrix dimensions according to transpose and input matrix shapes
79  output_local_shape[0] = transpose_2 ? inp_1_dims[1] : inp_1_dims[0];
80  output_local_shape[1] = transpose_1 ? inp_0_dims[0] : inp_0_dims[1];
81 
82  return output_local_shape;
83 }
84 
85 extern template class MatMul<::distconv::BackendDNNLib, float>;
86 extern template class MatMul<::distconv::BackendDNNLib, double>;
87 } // namespace distconv
88 
89 #endif // LBANN_HAS_DISTCONV
90 #endif // LBANN_LAYERS_MATH_DISTCONV_MATMUL
::distconv::tensor::LocaleMPI LocaleMPI
::distconv::tensor::Shape Shape