deepseek_v3.py 5.56 KB
Newer Older
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from .base import BaseModel, DataType, DeviceType, KVCacheCStruct, register_model
from ctypes import (
    c_size_t,
    c_uint,
    c_int,
    c_float,
    c_void_p,
    POINTER,
    Structure,
    CFUNCTYPE,
)


class DeepSeekV3MetaCStruct(Structure):
    _fields_ = [
        ("dt_logits", DataType),
        ("dt_norm", DataType),
        ("dt_quant_weight", DataType),
        ("dt_quant_scale", DataType),
        ("dt_quant_zero", DataType),
        ("dt_gate_weight", DataType),
        ("dt_gate_bias", DataType),
        ("n_sparse_layer", c_size_t),
        ("n_dense_layer", c_size_t),
        ("d", c_size_t),
        ("nh", c_size_t),
        ("nkvh", c_size_t),
        ("d_rope", c_size_t),
        ("d_nope", c_size_t),
        ("r_q", c_size_t),
        ("r_kv", c_size_t),
        ("d_qk", c_size_t),
        ("d_v", c_size_t),
        ("routed_scale", c_float),
        ("nexperts", c_size_t),
        ("kexperts", c_size_t),
        ("di", c_size_t),
        ("di_moe", c_size_t),
        ("dctx", c_size_t),
        ("dvoc", c_size_t),
        ("epsilon", c_float),
        ("rope_theta", c_float),
        ("end_token", c_uint),
    ]


class DeepSeekV3WeightsCStruct(Structure):
    pass


class DeepSeekV3ModelCStruct(Structure):
    pass


class DeepSeekV3CacheCStruct(Structure):
    pass


load_global_fn = CFUNCTYPE(None, POINTER(DeepSeekV3WeightsCStruct), c_void_p)
load_layer_fn = CFUNCTYPE(None, POINTER(DeepSeekV3WeightsCStruct), c_void_p, c_size_t)
load_layer_linear_fn = CFUNCTYPE(
    None, POINTER(DeepSeekV3WeightsCStruct), c_void_p, c_void_p, c_void_p, c_size_t
)
load_layer_mlp_fn = CFUNCTYPE(
    None,
    POINTER(DeepSeekV3WeightsCStruct),
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_size_t,
)
load_layer_expert_mlp_fn = CFUNCTYPE(
    None,
    POINTER(DeepSeekV3WeightsCStruct),
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_void_p,
    c_size_t,
    c_size_t,
)


class DeepSeekV3WeightLoaderCStruct(Structure):
    _fields_ = [
        ("load_input_embd", load_global_fn),
        ("load_output_norm", load_global_fn),
        ("load_output_embd", load_global_fn),
        ("load_attn_norm", load_layer_fn),
        ("load_attn_q_a_proj", load_layer_linear_fn),
        ("load_attn_q_a_layernorm", load_layer_fn),
        ("load_attn_q_b_proj", load_layer_linear_fn),
        ("load_attn_kv_a_proj_with_mqa", load_layer_linear_fn),
        ("load_attn_kv_a_layernorm", load_layer_fn),
        ("load_attn_kv_b_proj", load_layer_linear_fn),
        ("load_attn_o_proj", load_layer_linear_fn),
        ("load_mlp_norm", load_layer_fn),
        ("load_mlp_dense", load_layer_mlp_fn),
        ("load_mlp_gate_weight", load_layer_fn),
        ("load_mlp_gate_bias", load_layer_fn),
        ("load_mlp_shared_experts", load_layer_mlp_fn),
        ("load_mlp_experts", load_layer_expert_mlp_fn),
    ]


@register_model
class DeepSeekV3Model(BaseModel):
    @classmethod
    def register_lib(cls, lib):
        """Register DeepSeekV3 model functions with the library"""
        lib.createDeepSeekV3WeightLoader.argtypes = []
        lib.createDeepSeekV3WeightLoader.restype = POINTER(
            DeepSeekV3WeightLoaderCStruct
        )

        lib.createDeepSeekV3Weights.argtypes = [
            POINTER(DeepSeekV3MetaCStruct),
            DeviceType,
            c_int,
            POINTER(c_int),
        ]
        lib.createDeepSeekV3Weights.restype = POINTER(DeepSeekV3WeightsCStruct)

        lib.createDeepSeekV3Model.argtypes = [
            POINTER(DeepSeekV3MetaCStruct),
            POINTER(DeepSeekV3WeightsCStruct),
        ]
        lib.createDeepSeekV3Model.restype = POINTER(DeepSeekV3ModelCStruct)

        lib.destroyDeepSeekV3Model.argtypes = [POINTER(DeepSeekV3ModelCStruct)]

        lib.createDeepSeekV3Cache.argtypes = [POINTER(DeepSeekV3ModelCStruct)]
        lib.createDeepSeekV3Cache.restype = POINTER(DeepSeekV3CacheCStruct)

        lib.dropDeepSeekV3Cache.argtypes = [
            POINTER(DeepSeekV3ModelCStruct),
            POINTER(DeepSeekV3CacheCStruct),
        ]

        lib.inferBatchDeepSeekV3.argtypes = [
            POINTER(DeepSeekV3ModelCStruct),
            POINTER(c_uint),
            c_uint,
            POINTER(c_uint),
            c_uint,
            POINTER(c_uint),
            POINTER(POINTER(DeepSeekV3CacheCStruct)),
            POINTER(c_float),
            POINTER(c_uint),
            POINTER(c_float),
            POINTER(c_uint),
        ]

    def create_weight_loader(self):
        return self.lib.createDeepSeekV3WeightLoader()

    def create_weights(self, meta, device_type, ndev, dev_ids):
        return self.lib.createDeepSeekV3Weights(meta, device_type, ndev, dev_ids)

    def create_model(self, meta, weights):
        return self.lib.createDeepSeekV3Model(meta, weights)

    def destroy_model(self, model):
        self.lib.destroyDeepSeekV3Model(model)

    def create_cache(self, model):
        return self.lib.createDeepSeekV3Cache(model)

    def drop_cache(self, model, cache):
        self.lib.dropDeepSeekV3Cache(model, cache)

    def infer_batch(
        self,
        model,
        tokens,
        ntok,
        req_lens,
        nreq,
        req_pos,
        caches,
        temperature,
        topk,
        topp,
        output,
    ):
        self.lib.inferBatchDeepSeekV3(
            model,
            tokens,
            ntok,
            req_lens,
            nreq,
            req_pos,
            caches,
            temperature,
            topk,
            topp,
            output,
        )