libinfinicore_infer.py 13.7 KB
Newer Older
PanZezhong's avatar
init  
PanZezhong committed
1
import ctypes
blkmjsian's avatar
blkmjsian committed
2
from ctypes import c_char, c_char_p, c_size_t, c_uint, c_int, c_float, c_void_p, POINTER
PanZezhong's avatar
init  
PanZezhong committed
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
import os


class DataType(ctypes.c_int):
    INFINI_DTYPE_INVALID = 0
    INFINI_DTYPE_BYTE = 1
    INFINI_DTYPE_BOOL = 2
    INFINI_DTYPE_I8 = 3
    INFINI_DTYPE_I16 = 4
    INFINI_DTYPE_I32 = 5
    INFINI_DTYPE_I64 = 6
    INFINI_DTYPE_U8 = 7
    INFINI_DTYPE_U16 = 8
    INFINI_DTYPE_U32 = 9
    INFINI_DTYPE_U64 = 10
    INFINI_DTYPE_F8 = 11
    INFINI_DTYPE_F16 = 12
    INFINI_DTYPE_F32 = 13
    INFINI_DTYPE_F64 = 14
    INFINI_DTYPE_C16 = 15
    INFINI_DTYPE_C32 = 16
    INFINI_DTYPE_C64 = 17
    INFINI_DTYPE_C128 = 18
    INFINI_DTYPE_BF16 = 19


class DeviceType(ctypes.c_int):
    DEVICE_TYPE_CPU = 0
PanZezhong's avatar
PanZezhong committed
31
    DEVICE_TYPE_NVIDIA = 1
PanZezhong's avatar
init  
PanZezhong committed
32
33
34
35
    DEVICE_TYPE_CAMBRICON = 2
    DEVICE_TYPE_ASCEND = 3
    DEVICE_TYPE_METAX = 4
    DEVICE_TYPE_MOORE = 5
zhangyue's avatar
zhangyue committed
36
    DEVICE_TYPE_ILUVATAR = 6
PanZezhong's avatar
init  
PanZezhong committed
37
38


39
class JiugeMetaCStruct(ctypes.Structure):
PanZezhong's avatar
init  
PanZezhong committed
40
41
    _fields_ = [
        ("dt_logits", DataType),
PanZezhong's avatar
PanZezhong committed
42
43
44
45
46
47
48
49
        ("nlayer", c_size_t),
        ("d", c_size_t),
        ("nh", c_size_t),
        ("nkvh", c_size_t),
        ("dh", c_size_t),
        ("di", c_size_t),
        ("dctx", c_size_t),
        ("dvoc", c_size_t),
PanZezhong's avatar
init  
PanZezhong committed
50
51
52
53
54
55
56
        ("epsilon", c_float),
        ("theta", c_float),
        ("end_token", c_uint),
    ]


# Define the JiugeWeights struct
57
class JiugeWeightsCStruct(ctypes.Structure):
PanZezhong's avatar
init  
PanZezhong committed
58
    _fields_ = [
PanZezhong's avatar
PanZezhong committed
59
        ("nlayer", c_size_t),
PanZezhong's avatar
PanZezhong committed
60
61
        ("dt_norm", DataType),
        ("dt_mat", DataType),
PanZezhong's avatar
PanZezhong committed
62
        ("transpose_linear_weights", c_int),
PanZezhong's avatar
init  
PanZezhong committed
63
64
65
66
67
68
69
70
71
72
73
74
75
        ("input_embd", c_void_p),
        ("output_norm", c_void_p),
        ("output_embd", c_void_p),
        ("attn_norm", POINTER(c_void_p)),
        ("attn_qkv", POINTER(c_void_p)),
        ("attn_qkv_b", POINTER(c_void_p)),
        ("attn_o", POINTER(c_void_p)),
        ("ffn_norm", POINTER(c_void_p)),
        ("ffn_gate_up", POINTER(c_void_p)),
        ("ffn_down", POINTER(c_void_p)),
    ]


76
class JiugeModelCSruct(ctypes.Structure):
PanZezhong's avatar
init  
PanZezhong committed
77
78
79
    pass


blkmjsian's avatar
blkmjsian committed
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
class DeepSeekV3MetaCStruct(ctypes.Structure):
    _fields_ = [
        # dtypes
        ("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),
        # sizes
        ("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),
        # routing / experts / vocab / ctx
        ("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),
        # misc
        ("epsilon", c_float),
        ("rope_theta", c_float),
        ("end_token", c_uint),
    ]


class DeepSeekV3WeightsCStruct(ctypes.Structure):
    pass


# void (*load_global_fn)(DeepSeekV3Weights*, void *cpu_ptr)
load_global_fn = ctypes.CFUNCTYPE(None, POINTER(DeepSeekV3WeightsCStruct), c_void_p)

# void (*load_layer_fn)(DeepSeekV3Weights*, void *cpu_ptr, size_t layer_id)
load_layer_fn = ctypes.CFUNCTYPE(
    None, POINTER(DeepSeekV3WeightsCStruct), c_void_p, c_size_t
)

# void (*load_layer_linear_fn)(DeepSeekV3Weights*, void *weight_ptr, void *scale_ptr, void *zero_ptr, size_t layer_id)
load_layer_linear_fn = ctypes.CFUNCTYPE(
    None, POINTER(DeepSeekV3WeightsCStruct), c_void_p, c_void_p, c_void_p, c_size_t
)

# void (*load_layer_mlp_fn)(DeepSeekV3Weights*, ... , size_t layer_id)
load_layer_mlp_fn = ctypes.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,
)

# void (*load_layer_expert_mlp_fn)(DeepSeekV3Weights*, ..., size_t layer_id, size_t expert_id)
load_layer_expert_mlp_fn = ctypes.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,
)
# -------------------------------------------------------------------
# Struct containing all weight loading functions
# -------------------------------------------------------------------


class DeepSeekV3WeightLoaderCStruct(ctypes.Structure):
    _fields_ = [
        # Global
        ("load_input_embd", load_global_fn),
        ("load_output_norm", load_global_fn),
        ("load_output_embd", load_global_fn),
        # Attention
        ("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),
        # MLP
        ("load_mlp_norm", load_layer_fn),
        # MLP dense part
        ("load_mlp_dense", load_layer_mlp_fn),
        # MLP sparse gating
        ("load_mlp_gate_weight", load_layer_fn),
        ("load_mlp_gate_bias", load_layer_fn),
        # Shared experts
        ("load_mlp_shared_experts", load_layer_mlp_fn),
        # Per-expert functions
        ("load_mlp_experts", load_layer_expert_mlp_fn),
    ]


class DeepSeekV3ModelCStruct(ctypes.Structure):
    pass


204
class KVCacheCStruct(ctypes.Structure):
PanZezhong's avatar
init  
PanZezhong committed
205
206
207
    pass


blkmjsian's avatar
blkmjsian committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
class DeepSeekV3CacheCStruct(ctypes.Structure):
    pass


class JiugeAWQMetaCStruct(ctypes.Structure):
    _fields_ = [
        ("dt_logits", DataType),
        ("dt_linear_w", DataType),
        ("dt_norm_w", DataType),
        ("nlayer", c_size_t),
        ("d", c_size_t),
        ("nh", c_size_t),
        ("nkvh", c_size_t),
        ("dh", c_size_t),
        ("di", c_size_t),
        ("dctx", c_size_t),
        ("dvoc", c_size_t),
        ("epsilon", c_float),
        ("theta", c_float),
        ("end_token", c_uint),
        ("nbit", c_size_t),
        ("quant_group_size", c_size_t),
        ("has_qkv_bias", c_char),
    ]


class ModelWeightsCStruct(ctypes.Structure):
    pass


class JiugeAWQModelCStruct(ctypes.Structure):
    pass  # opaque struct


PanZezhong's avatar
PanZezhong committed
242
def __open_library__():
PanZezhong's avatar
init  
PanZezhong committed
243
244
245
246
    lib_path = os.path.join(
        os.environ.get("INFINI_ROOT"), "lib", "libinfinicore_infer.so"
    )
    lib = ctypes.CDLL(lib_path)
blkmjsian's avatar
blkmjsian committed
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261

    lib.createKVCache.argtypes = [
        c_size_t,  # nlayers
        c_size_t,  # max_len
        c_size_t,  # nkvh_
        c_size_t,  # dk
        c_size_t,  # dv
        DataType,  # dtype
        DeviceType,  # device
        POINTER(c_int),  # dev_ids
        c_size_t,  # ndev
    ]
    lib.createKVCache.restype = POINTER(KVCacheCStruct)
    lib.dropKVCache.argtypes = [POINTER(KVCacheCStruct)]

262
    lib.createJiugeModel.restype = POINTER(JiugeModelCSruct)
PanZezhong's avatar
PanZezhong committed
263
    lib.createJiugeModel.argtypes = [
264
265
        POINTER(JiugeMetaCStruct),  # JiugeMeta const *
        POINTER(JiugeWeightsCStruct),  # JiugeWeights const *
PanZezhong's avatar
init  
PanZezhong committed
266
267
268
269
        DeviceType,  # DeviceType
        c_int,  # int ndev
        POINTER(c_int),  # int const *dev_ids
    ]
270
    lib.destroyJiugeModel.argtypes = [POINTER(JiugeModelCSruct)]
blkmjsian's avatar
blkmjsian committed
271
272
273

    lib.inferBatchJiuge.restype = None
    lib.inferBatchJiuge.argtypes = [
274
        POINTER(JiugeModelCSruct),  # struct JiugeModel const *
PanZezhong's avatar
init  
PanZezhong committed
275
276
277
278
279
        POINTER(c_uint),  # unsigned int const *tokens
        c_uint,  # unsigned int ntok
        POINTER(c_uint),  # unsigned int const *req_lens
        c_uint,  # unsigned int nreq
        POINTER(c_uint),  # unsigned int const *req_pos
280
        POINTER(POINTER(KVCacheCStruct)),  # struct KVCache **kv_caches
Pan Zezhong's avatar
Pan Zezhong committed
281
282
283
        POINTER(c_float),  # float temperature
        POINTER(c_uint),  # unsigned int topk
        POINTER(c_float),  # float topp
PanZezhong's avatar
init  
PanZezhong committed
284
285
        POINTER(c_uint),  # unsigned int *output
    ]
blkmjsian's avatar
blkmjsian committed
286
287
    lib.forwardBatchJiuge.restype = None
    lib.forwardBatchJiuge.argtypes = [
PanZezhong's avatar
PanZezhong committed
288
289
290
291
292
293
294
295
296
        POINTER(JiugeModelCSruct),  # struct JiugeModel const *
        POINTER(c_uint),  # unsigned int const *tokens
        c_uint,  # unsigned int ntok
        POINTER(c_uint),  # unsigned int const *req_lens
        c_uint,  # unsigned int nreq
        POINTER(c_uint),  # unsigned int const *req_pos
        POINTER(POINTER(KVCacheCStruct)),  # struct KVCache **kv_caches
        c_void_p,  # void *logits
    ]
PanZezhong's avatar
init  
PanZezhong committed
297

blkmjsian's avatar
blkmjsian committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    # createDeepSeekV3WeightLoader
    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)

    # destroyDeepSeekV3Model
    lib.destroyDeepSeekV3Model.argtypes = [POINTER(DeepSeekV3ModelCStruct)]
    lib.destroyDeepSeekV3Model.restype = None

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

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

    # inferBatchDeepSeekV3
    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),
    ]
    lib.inferBatchDeepSeekV3.restype = None

    # forwardBatchDeepSeekV3
    lib.forwardBatchDeepSeekV3.argtypes = [
        POINTER(DeepSeekV3ModelCStruct),
        POINTER(c_uint),
        c_uint,
        POINTER(c_uint),
        c_uint,
        POINTER(c_uint),
        POINTER(POINTER(DeepSeekV3CacheCStruct)),
        c_void_p,
    ]
    lib.forwardBatchDeepSeekV3.restype = None

    lib.createJiugeAWQWeights.restype = POINTER(ModelWeightsCStruct)
    lib.createJiugeAWQWeights.argtypes = [
        POINTER(JiugeAWQMetaCStruct),  # const JiugeAWQMeta*
        DeviceType,  # infiniDevice_t
        c_int,  # int ndev
        POINTER(c_int),  # const int* dev_ids
    ]

    # createJiugeAWQModel
    lib.createJiugeAWQModel.restype = POINTER(JiugeAWQModelCStruct)
    lib.createJiugeAWQModel.argtypes = [
        POINTER(JiugeAWQMetaCStruct),  # const JiugeAWQMeta*
        POINTER(ModelWeightsCStruct),  # const ModelWeights*
    ]

    # destroyJiugeAWQModel
    lib.destroyJiugeAWQModel.argtypes = [POINTER(JiugeAWQModelCStruct)]
    lib.destroyJiugeAWQModel.restype = None

    # inferBatchJiugeAWQ
    lib.inferBatchJiugeAWQ.argtypes = [
        POINTER(JiugeAWQModelCStruct),  # JiugeAWQModel*
        POINTER(c_uint),  # const uint32_t* tokens
        c_uint,  # uint32_t ntok
        POINTER(c_uint),  # const uint32_t* req_lens
        c_uint,  # uint32_t nreq
        POINTER(c_uint),  # const uint32_t* req_pos
        POINTER(POINTER(KVCacheCStruct)),  # struct KVCache** kv_caches
        POINTER(c_float),  # const float* temperature
        POINTER(c_uint),  # const uint32_t* topk
        POINTER(c_float),  # const float* topp
        POINTER(c_uint),  # uint32_t* output
    ]
    lib.inferBatchJiugeAWQ.restype = None

    # forwardBatchJiugeAWQ
    lib.forwardBatchJiugeAWQ.argtypes = [
        POINTER(JiugeAWQModelCStruct),  # JiugeAWQModel*
        POINTER(c_uint),  # const uint32_t* tokens
        c_uint,  # uint32_t ntok
        POINTER(c_uint),  # const uint32_t* req_lens
        c_uint,  # uint32_t nreq
        POINTER(c_uint),  # const uint32_t* req_pos
        POINTER(POINTER(KVCacheCStruct)),  # struct KVCache** kv_caches
        c_void_p,  # void* logits
    ]
    lib.forwardBatchJiugeAWQ.restype = None

    lib.loadModelWeight.argtypes = [
        POINTER(ModelWeightsCStruct),  # struct ModelWeights*
        c_char_p,  # const char* name
        c_void_p,  # void* data
    ]
    lib.loadModelWeight.restype = None

    # loadModelWeightDistributed
    lib.loadModelWeightDistributed.argtypes = [
        POINTER(ModelWeightsCStruct),  # struct ModelWeights*
        c_char_p,  # const char* name
        c_void_p,  # void* data
        POINTER(c_int),  # int* ranks
        c_int,  # int nrank
    ]
    lib.loadModelWeightDistributed.restype = None

PanZezhong's avatar
init  
PanZezhong committed
425
    return lib
PanZezhong's avatar
PanZezhong committed
426
427
428
429


LIB = __open_library__()

blkmjsian's avatar
blkmjsian committed
430
431
432
433
434
435
436
437
438

def load_model_weight(weights, name, data):
    LIB.loadModelWeight(weights, name.encode("utf-8"), data)


def load_model_weight_distributed(weights, name, data, ranks, nrank):
    LIB.loadModelWeightDistributed(weights, name.encode("utf-8"), data, ranks, nrank)


PanZezhong's avatar
PanZezhong committed
439
create_jiuge_model = LIB.createJiugeModel
PanZezhong's avatar
PanZezhong committed
440
destroy_jiuge_model = LIB.destroyJiugeModel
PanZezhong's avatar
PanZezhong committed
441
442
create_kv_cache = LIB.createKVCache
drop_kv_cache = LIB.dropKVCache
blkmjsian's avatar
blkmjsian committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
infer_batch_jiuge = LIB.inferBatchJiuge
forward_batch_jiuge = LIB.forwardBatchJiuge

create_jiuge_awq_weights = LIB.createJiugeAWQWeights
create_jiuge_awq_model = LIB.createJiugeAWQModel
destroy_jiuge_awq_model = LIB.destroyJiugeAWQModel
infer_batch_jiuge_awq = LIB.inferBatchJiugeAWQ
forward_batch_jiuge_awq = LIB.forwardBatchJiugeAWQ

create_deepseek_v3_model = LIB.createDeepSeekV3Model
destroy_deepseek_v3_model = LIB.destroyDeepSeekV3Model
create_deepseek_v3_weight_loader = LIB.createDeepSeekV3WeightLoader
create_deepseek_v3_weights = LIB.createDeepSeekV3Weights
create_deepseek_v3_cache = LIB.createDeepSeekV3Cache
drop_deepseek_v3_cache = LIB.dropDeepSeekV3Cache
infer_batch_deepseek_v3 = LIB.inferBatchDeepSeekV3