blas.h 3.19 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>

PanZezhong's avatar
PanZezhong committed
8
namespace op::gemm {
9

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

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

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

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

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

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

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

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

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

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

    size_t m, n, k, batch;

    bool is_transed = false;

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

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

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

108
109
        if ((layout == MatrixLayout::COL_MAJOR && c_matrix.col_stride == 1)
            || (layout == MatrixLayout::ROW_MAJOR && c_matrix.row_stride == 1)) {
PanZezhongQY's avatar
PanZezhongQY committed
110
111
112
113
114
115
116
117
118
119
120
121
            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;
    }
};
122

PanZezhong's avatar
PanZezhong committed
123
} // namespace op::gemm
PanZezhongQY's avatar
PanZezhongQY committed
124

125
#endif // __BLAS_H__