MulOpGpu.cu 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "hl_base.h"
#include "MulOp.h"
#include "paddle/math/Matrix.h"
#include "paddle/math/SparseMatrix.h"

namespace paddle {
/**
 * out = scale_t * out + scale_ab * (a * b)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
 * out : output matrix, M * N
 */
template <>
void MulOp<DEVICE_TYPE_GPU>(GpuMatrix& out,
                            const GpuMatrix& a,
                            const GpuMatrix& b,
                            real scale_ab,
                            real scale_t) {
  CHECK(!out.isTransposed()) << "Not supported";

  if (!a.isTransposed() && !b.isTransposed()) {
    /// a : M * K, b: K * N
    CHECK_EQ(out.width_, b.width_);
    CHECK_EQ(out.height_, a.height_);
    CHECK_EQ(a.width_, b.height_);
  } else if (a.isTransposed() && !b.isTransposed()) {
    /// a : K * M, b : K * N
    CHECK_EQ(out.width_, b.width_);
    CHECK_EQ(out.height_, a.width_);
    CHECK_EQ(a.height_, b.height_);
  } else if (!a.isTransposed() && b.isTransposed()) {
    /// a: M * K, b : N * K
    CHECK_EQ(out.width_, b.height_);
    CHECK_EQ(out.height_, a.height_);
    CHECK_EQ(a.width_, b.width_);
  } else {
    LOG(FATAL) << "Is not supported";
  }

  real* a_data = a.data_;
  real* b_data = b.data_;
  real* out_data = out.data_;
  int dim_m = out.getHeight();
  int dim_n = out.getWidth();
  int dim_k = !a.isTransposed() ? a.width_ : a.height_;
  int lda = a.getStride();
  int ldb = b.getStride();
  int ldc = out.getStride();
  hl_trans_op_t trans_a = !a.isTransposed() ? HPPL_OP_N : HPPL_OP_T;
  hl_trans_op_t trans_b = !b.isTransposed() ? HPPL_OP_N : HPPL_OP_T;

  hl_matrix_mul(a_data,
                trans_a,
                b_data,
                trans_b,
                out_data,
                dim_m,
                dim_n,
                dim_k,
                scale_ab,
                scale_t,
                lda,
                ldb,
                ldc);
}

/**
 * out = scale_t * out + scale_ab * (a * b)
 * out : M * N
82 83 84 85 86 87 88 89 90 91 92 93
 */
template <>
void MulOp<DEVICE_TYPE_GPU>(GpuMatrix& out,
                            const GpuSparseMatrix& a,
                            const GpuMatrix& b,
                            real scale_ab,
                            real scale_t) {
  CHECK(out.isContiguous());
  CHECK(b.isContiguous());
  CHECK(b.useGpu_ == true) << "Matrix type are not equal";
  CHECK(!out.trans_ && !b.trans_) << "not supported";
  if (!a.trans_) {
94
    /// a: M * K,  b: K * N
95
    CHECK(out.width_ == b.width_ && out.height_ == a.height_
96
        && a.width_ == b.height_) << "Matrix dimensions are not equal";
97
  } else {
98
    /// a: K * M, transpose,  b: K * N
99
    CHECK(out.width_ == b.width_ && out.height_ == a.width_
100
        && a.height_ == b.height_) << "Matrix dimensions are not equal";
101
  }
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  hl_trans_op_t a_trans = a.trans_ ? HPPL_OP_T : HPPL_OP_N;
  hl_sparse_matrix_s a_data = a.sMatrix_.get();
  real* b_data = b.data_;
  real* out_data = out.data_;
  hl_matrix_csr_mul_dense(a_data,
                          a_trans,
                          b_data,
                          HPPL_OP_N,
                          out_data,
                          out.height_,
                          out.width_,
                          b.height_,
                          scale_ab,
                          scale_t);
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/**
 * out = scale_t * out + scale_ab * (a * b)
 * out : M * N
 */
template <>
void MulOp<DEVICE_TYPE_GPU>(GpuMatrix& out,
                            const GpuMatrix& a,
                            const GpuSparseMatrix& b,
                            real scale_ab,
                            real scale_t) {
  CHECK(out.isContiguous());
  CHECK(a.isContiguous());
  CHECK(a.useGpu_ == true) << "Matrix type are not equal";

  hl_sparse_matrix_s b_data = b.sMatrix_.get();
  real* a_data = a.data_;
  real* out_data = out.data_;
  hl_trans_op_t trans_b = b.trans_ ? HPPL_OP_T : HPPL_OP_N;
  if (!b.trans_) {
    /// a : M * K, b : K * N
    CHECK(out.width_ == b.width_ &&
          out.height_ == a.height_ && a.width_ == b.height_)
        << "Matrix dimensions are not equal";
  } else {
    /// a : M * K, b : N * K, transpose
    CHECK(out.width_ == b.height_ &&
          out.height_ == a.height_ && a.width_ == b.width_)
        << "Matrix dimensions are not equal";
  }
  if (b.format_ == SPARSE_CSC) {
    hl_matrix_dense_mul_csc(a_data,
                            HPPL_OP_N,
                            b_data,
                            trans_b,
                            out_data,
                            out.height_,
                            out.width_,
                            a.width_,
                            scale_ab,
                            scale_t);
  } else {
    hl_matrix_dense_mul_csr(a_data,
                            HPPL_OP_N,
                            b_data,
                            trans_b,
                            out_data,
                            out.height_,
                            out.width_,
                            a.width_,
                            scale_ab,
                            scale_t);
  }
}

173
}  // namespace paddle