MCPcopy Create free account
hub / github.com/davisking/dlib / gemm

Function gemm

dlib/cuda/tensor_tools.cpp:242–346  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

240// ----------------------------------------------------------------------------------------
241
242 void gemm (
243 float beta,
244 tensor& dest,
245 float alpha,
246 const tensor& lhs,
247 bool trans_lhs,
248 const tensor& rhs,
249 bool trans_rhs,
250 operation_mode mode
251 )
252 {
253 IF_DLIB_USE_CUDA(
254 cuda::gemm(beta, dest, alpha, lhs, trans_lhs, rhs, trans_rhs, mode);
255 )
256
257 IF_DLIB_NOT_USE_CUDA(
258 if (mode == operation_mode::CHANNEL_WISE)
259 {
260 if (beta != 0)
261 {
262 if (trans_lhs && trans_rhs)
263 dest = alpha * trans(mat(lhs)) * trans(mat(rhs)) + beta * mat(dest);
264 else if (!trans_lhs && trans_rhs)
265 dest = alpha * mat(lhs) * trans(mat(rhs)) + beta * mat(dest);
266 else if (trans_lhs && !trans_rhs)
267 dest = alpha * trans(mat(lhs)) * mat(rhs) + beta * mat(dest);
268 else
269 dest = alpha * mat(lhs) * mat(rhs) + beta * mat(dest);
270 }
271 else
272 {
273 if (trans_lhs && trans_rhs)
274 dest = alpha * trans(mat(lhs)) * trans(mat(rhs));
275 else if (!trans_lhs && trans_rhs)
276 dest = alpha * mat(lhs) * trans(mat(rhs));
277 else if (trans_lhs && !trans_rhs)
278 dest = alpha * trans(mat(lhs)) * mat(rhs);
279 else
280 dest = alpha * mat(lhs) * mat(rhs);
281 }
282 }
283 else if (mode == operation_mode::PLANE_WISE)
284 {
285 auto is_matrix = [](const auto& tensor) {
286 return ((tensor.num_samples() * tensor.k() == 1 && tensor.nr() * tensor.nc() > 1) ||
287 (tensor.num_samples() * tensor.k() > 1 && tensor.nr() * tensor.nc() == 1));
288 };
289
290 long num_samples = std::min({ lhs.num_samples(), rhs.num_samples(), dest.num_samples() });
291 long num_channels = std::min({ lhs.k(), rhs.k(), dest.k() });
292 const bool lhs_is_matrix = is_matrix(lhs), rhs_is_matrix = is_matrix(rhs), dest_is_matrix = is_matrix(dest);
293
294 if (lhs_is_matrix && rhs_is_matrix && dest_is_matrix) {
295 num_samples = num_channels = 1;
296 }
297
298 long lhs_rows = (lhs_is_matrix && lhs.num_samples() > 1) ? lhs.num_samples() : lhs.nr();
299 long lhs_cols = (lhs_is_matrix && lhs.k() > 1) ? lhs.k() : lhs.nc();

Callers 9

perform_testMethod · 0.50
forwardMethod · 0.50
backwardMethod · 0.50
forwardMethod · 0.50
backwardMethod · 0.50
forwardMethod · 0.50
backwardMethod · 0.50

Calls 10

is_matrixClass · 0.85
matFunction · 0.70
alias_tensorClass · 0.70
ifFunction · 0.50
transFunction · 0.50
minFunction · 0.50
num_samplesMethod · 0.45
kMethod · 0.45
nrMethod · 0.45
ncMethod · 0.45

Tested by 1

perform_testMethod · 0.40