MatMul
Scenario Description
Execute two input tensors a and b and the parameter structure M, perform multiplication operations on two matrices, and store the calculation results in the output tensor. For example:
1 2 3 4 5 6 | a: [1,2, 3,4] b: [5,6, 7,8] output: [19,22, 43,50] |
Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <cmath> #include <random> #include <cstdint> #include <iostream> #include "ktfop.h" int main() { using namespace ktfop; MatMulParams<float> params; params.order = CblasRowMajor; // Matrix storage order (row-major or column-major order). params.transA = CblasNoTrans; // Matrix A is transposed or not. params.transB = CblasNoTrans; // Matrix B is transposed or not. params.m = 2; // Number of rows in matrix A. params.n = 2; // Number of columns in matrix B. params.k = 2; // Number of columns in matrix A or number of rows in matrix B. params.alpha = 1.0f; // alpha parameter. params.lda = 2; // Storage stride of matrix A. params.ldb = 2; // Storage stride of matrix B. params.beta = 0.0f; // beta parameter. params.ldc = 2; // Storage stride of matrix C. float a[4] = {1, 2, 3, 4}; // Initialize the matrices involved in the calculation. float b[4] = {5, 6, 7, 8}; float c[4] = {0, 0, 0, 0}; // Matrix that stores the calculation results. int ret = -1; ret = Matmul(a, b, c, params); // Call the Matmul operator and store the results in the c array. std::cout << "c: ["; for (int i = 0; i < 4; ++i) { std::cout << c[i]; if (i < 3) { std::cout << ", "; } } std::cout << "]" << std::endl; ret = Matmul(static_cast<float *>(nullptr), b, c, params); // Enter a null pointer and print the log "ERROR Parameter verification failed for the MatMul Op." ret = Matmul(a, b, static_cast<float *>(nullptr), params); // Enter a null pointer and print the log "ERROR Parameter verification failed for the MatMul Op." } |
Parent topic: Examples