Linear.h 3.58 KB
Newer Older
Zhekai Zhang's avatar
Zhekai Zhang committed
1
2
3
4
5
6
#pragma once

#include "common.h"
#include "Tensor.h"
#include "Module.h"

muyangli's avatar
muyangli committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class GEMM_F16 : public Module {
public:
    GEMM_F16(int in_features, int out_features, bool use_bias, Tensor::ScalarType dtype, Device device);

    Tensor forward(Tensor x);

public:
    const int in_features;
    const int out_features;

public:
    Tensor weight;
    Tensor bias;
};

Zhekai Zhang's avatar
Zhekai Zhang committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class GEMV_AWQ : public Module {
public:
    GEMV_AWQ(int in_features, int out_features, bool use_bias, Tensor::ScalarType dtype, Device device);

    Tensor forward(Tensor x);

protected:
    virtual void loadParam(std::string key, Tensor &dst, Tensor src) override;

public:
    const int in_features;
    const int out_features;
    const int group_size;

    int lora_rank;
    float lora_scale;

muyangli's avatar
muyangli committed
39
    const Device device;
Muyang Li's avatar
Muyang Li committed
40

Zhekai Zhang's avatar
Zhekai Zhang committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
public:
    Tensor qweight;
    Tensor wscales;
    Tensor wzeros;
    Tensor bias;

    Tensor lora_down;
    Tensor lora_up;

    // std::shared_ptr<CUBLASWrapper> cublas;
};

class GEMM_W4A4 : public Module {
public:
    enum class FuseOptions {
        EMPTY = 0,
        GELU_QUANT,
muyangli's avatar
muyangli committed
58
        SILU,
Zhekai Zhang's avatar
Zhekai Zhang committed
59
60
61
62
63
64
    };
    struct QuantizedActivation {
        Tensor act;
        Tensor ascales;
        Tensor lora_act;
        bool is_unsigned = false;
muyangli's avatar
muyangli committed
65
        TensorShape actShape;
Zhekai Zhang's avatar
Zhekai Zhang committed
66
67
68
    };

public:
69
    GEMM_W4A4(int in_features, int out_features, bool bias, bool use_fp4, Tensor::ScalarType dtype, Device device);
muyangli's avatar
muyangli committed
70
71
72
    Tensor forward(Tensor x);
    Tensor forward_silu(Tensor x);
    std::variant<Tensor, QuantizedActivation> forward(Tensor x, FuseOptions fuse, GEMM_W4A4 *nextGEMM = nullptr);
Muyang Li's avatar
Muyang Li committed
73
74
75
76
77
78
79
80
81
82
83
84
    void forward(Tensor x,
                 Tensor out,
                 Tensor pool       = {},
                 Tensor norm_q     = {},
                 Tensor norm_k     = {},
                 Tensor rotary_emb = {},
                 Tensor out_q      = {},
                 Tensor out_k      = {},
                 Tensor out_v      = {},
                 int numTokens     = 0);
    std::variant<Tensor, QuantizedActivation>
    forward_quant(QuantizedActivation qact, FuseOptions fuse, GEMM_W4A4 *nextGEMM = nullptr);
muyangli's avatar
muyangli committed
85
    Tensor forward_quant(QuantizedActivation qact);
Zhekai Zhang's avatar
Zhekai Zhang committed
86
87

public:
muyangli's avatar
muyangli committed
88
    QuantizedActivation quantize(Tensor x, bool fuse_glu);
Zhekai Zhang's avatar
Zhekai Zhang committed
89
90
91
92

public:
    const int in_features;
    const int out_features;
muyangli's avatar
muyangli committed
93
94
    const int in_features_pad;
    const int out_features_pad;
95
    const bool use_fp4;
Muyang Li's avatar
Muyang Li committed
96

Zhekai Zhang's avatar
Zhekai Zhang committed
97
98
99
100
    int lora_rank;
    std::vector<float> lora_scales; // every 16 ranks share a scale

    const Tensor::ScalarType dtype;
muyangli's avatar
muyangli committed
101
    const Device device;
Zhekai Zhang's avatar
Zhekai Zhang committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115

protected:
    virtual void loadParam(std::string key, Tensor &dst, Tensor src) override;

public:
    Tensor qweight;
    Tensor wscales;
    Tensor bias;

    Tensor lora_down;
    Tensor lora_up;

    Tensor smooth;

116
117
118
    Tensor wtscale;
    Tensor wcscales;

Zhekai Zhang's avatar
Zhekai Zhang committed
119
120
121
    cublasHandle_t handle;
};

muyangli's avatar
muyangli committed
122
123
124
125
126
127
class GEMM_W8A8 : public Module {
public:
    struct QuantizedActivation {
        Tensor act;
        Tensor ascales;
    };
Muyang Li's avatar
Muyang Li committed
128

muyangli's avatar
muyangli committed
129
130
131
132
public:
    GEMM_W8A8(int in_features, int out_features, bool bias, Tensor::ScalarType dtype, Device device);

public:
Muyang Li's avatar
Muyang Li committed
133
    QuantizedActivation quantize(Tensor x, bool fuse_glu);
muyangli's avatar
muyangli committed
134
    Tensor forward_quant(QuantizedActivation qact);
Muyang Li's avatar
Muyang Li committed
135
136
137
    Tensor forward(Tensor x) {
        return forward_quant(quantize(x, false));
    }
muyangli's avatar
muyangli committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161

public:
    const int in_features;
    const int out_features;
    const Tensor::ScalarType dtype;

public:
    Tensor qweight;
    Tensor wscales;
    Tensor bias;
};

class DWCONV : public Module {
public:
    DWCONV(int in_features, bool bias, Tensor::ScalarType dtype, Device device);

    Tensor forward(Tensor x);

public:
    const int in_features;

public:
    Tensor weight;
    Tensor bias;
Muyang Li's avatar
Muyang Li committed
162
};