forked from NetEase-FuXi/EET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eet2py.cpp
156 lines (140 loc) · 7.76 KB
/
eet2py.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <torch/extension.h>
#include "op/ffn.hpp"
#include "op/gated_ffn.hpp"
#include "op/gated_ffn_int8.hpp"
#include "op/embedding.hpp"
#include "op/layer_norm.hpp"
#include "op/multi_head_attention.hpp"
#include "op/cross_multi_head_attention.hpp"
#include "op/masked_multi_head_attention.hpp"
#include "op/baichuan_mmha.hpp"
#include "op/llama_mmha.hpp"
#include "cutlass_kernels/fpA_intB_gemm_wrapper.h"
#define STRINGIFY(x) #x
#define MACRO_STRINGIFY(x) STRINGIFY(x)
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m)
{
#ifdef VERSION_INFO
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
#else
m.attr("__version__") = "dev";
#endif
py::class_<eet::MetaDesc>(m, "MetaDesc")
.def(py::init<const py::object &, const int &, const int &, const int &, const int &, const int &, const int &,
const std::string &, const int &, const int &, const std::string &, const bool &, const float &, const bool &>(),
py::arg("dtype"),
py::arg("batch_size"),
py::arg("head_num"),
py::arg("hidden_units"),
py::arg("layer_num"),
py::arg("max_seq_len"),
py::arg("max_full_seq_len") = 1,
py::arg("activation_fn") = 'gelu',
py::arg("d_kv") = 0,
py::arg("d_ff") = 0,
py::arg("cuda_device") = "cuda:0",
py::arg("requires_grad") = false,
py::arg("layernorm_eps") = 1e-6,
py::arg("is_int8") = false);
py::class_<eet::op::MaskedMultiHeadAttention>(m, "MaskedMultiHeadAttention")
.def(py::init<eet::MetaDesc, const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &>())
.def("forward", &eet::op::MaskedMultiHeadAttention::forward, "MaskedMultiHeadAttention forward",
py::arg("input"),
py::arg("pre_padding_len"),
py::arg("reorder_state"),
py::arg("pre_layernorm"),
py::arg("add_residual"),
py::arg("first_pass"),
py::arg("relative_attention_bias") = torch::empty(0));
py::class_<eet::op::BaichuanMmha>(m, "BaichuanMmha")
.def(py::init<eet::MetaDesc, const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &>())
.def("forward", &eet::op::BaichuanMmha::forward, "BaichuanMmha forward",
py::arg("input"),
py::arg("pre_padding_len"),
py::arg("reorder_state"),
py::arg("pre_layernorm"),
py::arg("add_residual"),
py::arg("first_pass"),
py::arg("relative_attention_bias") = torch::empty(0));
py::class_<eet::op::LlamaMmha>(m, "LlamaMmha")
.def(py::init<eet::MetaDesc, const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &>())
.def("forward", &eet::op::LlamaMmha::forward, "LlamaMmha forward",
py::arg("input"),
py::arg("pre_padding_len"),
py::arg("reorder_state"),
py::arg("pre_layernorm"),
py::arg("add_residual"),
py::arg("first_pass"),
py::arg("relative_attention_bias") = torch::empty(0));
py::class_<eet::op::CrossMultiHeadAttention>(m, "CrossMultiHeadAttention")
.def(py::init<eet::MetaDesc, const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &>())
.def("forward", &eet::op::CrossMultiHeadAttention::forward, "CrossMultiHeadAttention forward");
py::class_<eet::op::MultiHeadAttention>(m, "MultiHeadAttention")
.def(py::init<eet::MetaDesc, const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &>())
.def("forward", &eet::op::MultiHeadAttention::forward, "MultiHeadAttention forward",
py::arg("input"),
py::arg("padding_mask"),
py::arg("pre_layernorm"),
py::arg("add_residual"),
py::arg("need_sequence_mask") = false,
py::arg("relative_attention_bias") = torch::empty(0));
py::class_<eet::op::FeedForwardNetwork>(m, "FeedForwardNetwork")
.def(py::init<eet::MetaDesc,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const std::string &>())
.def("forward", &eet::op::FeedForwardNetwork::forward, "FeedForwardNetwork forward");
py::class_<eet::op::GatedFeedForwardNetwork>(m, "GatedFeedForwardNetwork")
.def(py::init<eet::MetaDesc,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const std::string &>())
.def("forward", &eet::op::GatedFeedForwardNetwork::forward, "GatedFeedForwardNetwork forward");
py::class_<eet::op::GatedFeedForwardNetworkInt8>(m, "GatedFeedForwardNetworkInt8")
.def(py::init<eet::MetaDesc,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &,const std::string &>())
.def("forward", &eet::op::GatedFeedForwardNetworkInt8::forward, "GatedFeedForwardNetworkInt8 forward");
py::class_<eet::op::Embedding>(m, "Embedding")
.def(py::init<eet::MetaDesc, const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const torch::Tensor &,
const torch::Tensor &, const std::string &>())
.def("forward_fairseq", &eet::op::Embedding::forward_fairseq, "Embedding forward_fairseq")
.def("forward_transformers", &eet::op::Embedding::forward_transformers, "Embedding forward_transformers");
py::class_<eet::op::LayerNorm>(m, "LayerNorm")
.def(py::init<eet::MetaDesc, const torch::Tensor &, const torch::Tensor &>())
.def("layer_norm", &eet::op::LayerNorm::layer_norm, "layer_norm");
// .def("AddBiasLayerNorm", &eet::op::layer_norm::AddBiasLayerNorm, "AddBiasLayerNorm");
m.def("preprocess_weights", &preprocess_weights_cuda, "transform int8 weights for cutlass",
py::arg("origin_weight"),
py::arg("is_int4") = false);
m.def("quant_weights", &symmetric_quantize_last_axis_of_tensor, "quantize weight",
py::arg("origin_weight"),
py::arg("quant_type"),
py::arg("return_unprocessed_quantized_tensor") = false);
}