blas.h 3.18 KB
Newer Older
PanZezhongQY's avatar
PanZezhongQY committed
1
2
3
#ifndef __BLAS_H__
#define __BLAS_H__

PanZezhong's avatar
PanZezhong committed
4
#include "../../operator.h"
PanZezhong's avatar
PanZezhong committed
5
#include "../../tensor.h"
PanZezhongQY's avatar
PanZezhongQY committed
6
7
#include <algorithm>

8
9
namespace matmul {
struct BlasMatrix {
PanZezhongQY's avatar
PanZezhongQY committed
10
11
    size_t ndim;
    size_t batch;
12
    ptrdiff_t stride;
PanZezhongQY's avatar
PanZezhongQY committed
13
14
    size_t rows;
    size_t cols;
15
16
    ptrdiff_t row_stride;
    ptrdiff_t col_stride;
PanZezhongQY's avatar
PanZezhongQY committed
17

18
    BlasMatrix() = default;
PanZezhongQY's avatar
PanZezhongQY committed
19

PanZezhong's avatar
PanZezhong committed
20
    BlasMatrix(infiniopTensorDescriptor_t layout, infiniStatus_t *status) {
PanZezhong's avatar
PanZezhong committed
21
        if (layout->ndim() == 2) {
22
23
24
            ndim = 2;
            batch = 1;
            stride = 0;
PanZezhong's avatar
PanZezhong committed
25
26
27
28
29
            rows = layout->dim(0);
            cols = layout->dim(1);
            row_stride = layout->stride(0);
            col_stride = layout->stride(1);
        } else if (layout->ndim() == 3) {
30
            ndim = 3;
PanZezhong's avatar
PanZezhong committed
31
32
33
34
35
36
            batch = layout->dim(0);
            stride = batch == 1 ? 0 : layout->stride(0);
            rows = layout->dim(1);
            cols = layout->dim(2);
            row_stride = layout->stride(1);
            col_stride = layout->stride(2);
PanZezhongQY's avatar
PanZezhongQY committed
37
        } else {
PanZezhong's avatar
PanZezhong committed
38
            *status = INFINI_STATUS_BAD_TENSOR_SHAPE;
PanZezhongQY's avatar
PanZezhongQY committed
39
40
41
            return;
        }

42
        if (row_stride != 1 && col_stride != 1) {
PanZezhong's avatar
PanZezhong committed
43
            *status = INFINI_STATUS_BAD_TENSOR_STRIDES;
PanZezhongQY's avatar
PanZezhongQY committed
44
45
46
            return;
        }

PanZezhong's avatar
PanZezhong committed
47
        *status = INFINI_STATUS_SUCCESS;
PanZezhongQY's avatar
PanZezhongQY committed
48
49
    }

50
    bool match_batch(size_t _batch) const {
51
        return batch == _batch || batch == 1;
PanZezhongQY's avatar
PanZezhongQY committed
52
53
54
55
56
57
58
    }

    void transpose() {
        std::swap(rows, cols);
        std::swap(row_stride, col_stride);
    }

59
    ptrdiff_t ld() const {
60
        return row_stride == 1 ? col_stride : row_stride;
PanZezhongQY's avatar
PanZezhongQY committed
61
    }
62
63
};

64
enum class MatrixLayout : char {
65
66
67
    COL_MAJOR,
    ROW_MAJOR,
};
PanZezhongQY's avatar
PanZezhongQY committed
68
69
70
71
72
73
74
75
76
77

struct MatmulInfo {
    BlasMatrix a_matrix;
    BlasMatrix b_matrix;
    BlasMatrix c_matrix;

    size_t m, n, k, batch;

    bool is_transed = false;

78
79
80
    MatmulInfo(infiniopTensorDescriptor_t c_desc,
               infiniopTensorDescriptor_t a_desc,
               infiniopTensorDescriptor_t b_desc,
PanZezhong's avatar
PanZezhong committed
81
               infiniStatus_t *status,
82
               MatrixLayout layout) {
PanZezhongQY's avatar
PanZezhongQY committed
83
        a_matrix = BlasMatrix(a_desc, status);
PanZezhong's avatar
PanZezhong committed
84
        if (*status != INFINI_STATUS_SUCCESS) {
PanZezhongQY's avatar
PanZezhongQY committed
85
86
87
            return;
        }
        b_matrix = BlasMatrix(b_desc, status);
PanZezhong's avatar
PanZezhong committed
88
        if (*status != INFINI_STATUS_SUCCESS) {
PanZezhongQY's avatar
PanZezhongQY committed
89
90
91
            return;
        }
        c_matrix = BlasMatrix(c_desc, status);
PanZezhong's avatar
PanZezhong committed
92
        if (*status != INFINI_STATUS_SUCCESS) {
PanZezhongQY's avatar
PanZezhongQY committed
93
94
95
            return;
        }

96
        if (c_matrix.rows != a_matrix.rows || c_matrix.cols != b_matrix.cols || a_matrix.cols != b_matrix.rows) {
PanZezhong's avatar
PanZezhong committed
97
            *status = INFINI_STATUS_BAD_TENSOR_SHAPE;
PanZezhongQY's avatar
PanZezhongQY committed
98
99
100
101
102
            return;
        }

        batch = c_matrix.batch;
        if (!a_matrix.match_batch(batch) || !b_matrix.match_batch(batch)) {
PanZezhong's avatar
PanZezhong committed
103
            *status = INFINI_STATUS_BAD_TENSOR_SHAPE;
PanZezhongQY's avatar
PanZezhongQY committed
104
105
106
            return;
        }

107
108
        if ((layout == MatrixLayout::COL_MAJOR && c_matrix.col_stride == 1)
            || (layout == MatrixLayout::ROW_MAJOR && c_matrix.row_stride == 1)) {
PanZezhongQY's avatar
PanZezhongQY committed
109
110
111
112
113
114
115
116
117
118
119
120
            c_matrix.transpose();
            b_matrix.transpose();
            a_matrix.transpose();
            std::swap(a_matrix, b_matrix);
            is_transed = true;
        }

        m = c_matrix.rows;
        n = c_matrix.cols;
        k = a_matrix.cols;
    }
};
121
} // namespace matmul
PanZezhongQY's avatar
PanZezhongQY committed
122

123
#endif // __BLAS_H__