| 95 | // ----------------------------------------------------------------------------------- |
| 96 | |
| 97 | void gemm ( |
| 98 | float beta, |
| 99 | tensor& dest, |
| 100 | float alpha, |
| 101 | const tensor& lhs, |
| 102 | bool trans_lhs, |
| 103 | const tensor& rhs, |
| 104 | bool trans_rhs, |
| 105 | operation_mode mode |
| 106 | ) |
| 107 | { |
| 108 | if (mode == operation_mode::CHANNEL_WISE) |
| 109 | { |
| 110 | // Recall that BLAS uses column major order so to deal with that we flip the |
| 111 | // order of the lhs and rhs arguments. |
| 112 | const auto transa = trans_lhs ? CUBLAS_OP_T : CUBLAS_OP_N; |
| 113 | const auto transb = trans_rhs ? CUBLAS_OP_T : CUBLAS_OP_N; |
| 114 | |
| 115 | const int dest_nr = dest.num_samples(); |
| 116 | const int dest_nc = dest.size() / dest_nr; |
| 117 | const int lhs_nr = lhs.num_samples(); |
| 118 | const int lhs_nc = lhs.size() / lhs_nr; |
| 119 | const int rhs_nr = rhs.num_samples(); |
| 120 | const int rhs_nc = rhs.size() / rhs_nr; |
| 121 | if (trans_lhs && trans_rhs) |
| 122 | { |
| 123 | DLIB_ASSERT(dest_nr == lhs_nc && |
| 124 | dest_nc == rhs_nr && |
| 125 | lhs_nr == rhs_nc) |
| 126 | } |
| 127 | else if (!trans_lhs && trans_rhs) |
| 128 | { |
| 129 | DLIB_ASSERT(dest_nr == lhs_nr && |
| 130 | dest_nc == rhs_nr && |
| 131 | lhs_nc == rhs_nc) |
| 132 | } |
| 133 | else if (trans_lhs && !trans_rhs) |
| 134 | { |
| 135 | DLIB_ASSERT(dest_nr == lhs_nc && |
| 136 | dest_nc == rhs_nc && |
| 137 | lhs_nr == rhs_nr) |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | DLIB_ASSERT(dest_nr == lhs_nr && |
| 142 | dest_nc == rhs_nc && |
| 143 | lhs_nc == rhs_nr) |
| 144 | } |
| 145 | |
| 146 | const int k = trans_rhs ? rhs_nc : rhs_nr; |
| 147 | CHECK_CUBLAS(cublasSgemm(context(), |
| 148 | transb, |
| 149 | transa, |
| 150 | dest_nc, dest_nr, k, |
| 151 | &alpha, |
| 152 | rhs.device(), rhs_nc, |
| 153 | lhs.device(), lhs_nc, |
| 154 | &beta, |