-
Notifications
You must be signed in to change notification settings - Fork 3
/
opslist.py
608 lines (470 loc) · 18.5 KB
/
opslist.py
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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
import numpy as np
class RWKVOnnxOps():
def __init__(self, layers, embed, opsVersion = 15, externalData = True, splitExternalData = False,fp32inout=True, quantized = False, *args, dtype=None, heads=32, **kwargs):
import onnx
self.n_layers = layers
self.n_embed = embed
print("embed ", embed)
dtype = onnx.TensorProto.FLOAT if dtype == np.float32 else onnx.TensorProto.FLOAT16 if dtype == np.float16 else onnx.TensorProto.BFLOAT16 if dtype == np.bfloat16 else onnx.TensorProto.FLOAT
nptype = np.float32 if dtype == onnx.TensorProto.FLOAT else np.float16 if dtype == onnx.TensorProto.FLOAT16 else np.float16 if dtype == onnx.TensorProto.BFLOAT16 else np.float32
self.nm = 0
exportname = f"RWKV_{layers}_{embed}_{'32' if dtype == onnx.TensorProto.FLOAT else '16'}_{opsVersion}.onnx"
externalname = f"RWKV_{layers}_{embed}_{'32' if dtype == onnx.TensorProto.FLOAT else '16'}_{opsVersion}"
# remove old files
import os
if os.path.exists(exportname):
os.remove(exportname)
if os.path.exists(externalname):
os.remove(externalname)
self.TensorList = []
self.NodeList = []
def initTensor(x, isfp32 = False, exname = ""):
npdtype = np.float32 if (isfp32 and fp32inout) else nptype
ddtype = onnx.TensorProto.FLOAT if (isfp32 and fp32inout) else dtype
name = f"PreTrainedTensor_{self.nm}"
self.nm += 1
if isinstance(x, list):
xx = np.array(x).astype(npdtype)
else:
xx = x.squeeze().float().cpu().numpy()
# convert to float32
xx = xx.astype(npdtype)
rrx = onnx.helper.make_tensor(
name,
ddtype,
xx.shape,
xx.tobytes(),
raw=True
)
if externalData:
if not splitExternalData:
exname = ""
onnx.external_data_helper.set_external_data(
rrx,
location=externalname+exname+".bin",
)
self.TensorList.append(rrx)
return name
def initIntTensor(x, exname = ""):
name = f"PreTrainedTensor_{self.nm}"
self.nm += 1
if isinstance(x, list):
xx = np.array(x).astype(np.int64)
else:
xx = x.squeeze().int().cpu().numpy()
# convert to float32
xx = xx.astype(np.int64)
rrx = onnx.helper.make_tensor(
name,
onnx.TensorProto.INT64,
xx.shape,
xx.tobytes(),
raw=True
)
# if externalData:
# if not splitExternalData:
# exname = ""
# onnx.external_data_helper.set_external_data(
# rrx,
# location=externalname+exname+".bin",
# )
self.TensorList.append(rrx)
return name
self.initTensor = initTensor
self.initIntTensor = initIntTensor
def sqrt(x):
name = f"sqrt_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Sqrt',
inputs=[x],
outputs=[name]
)
self.NodeList.append(node)
return name
def convertToFloat16(x):
if x == None:
return None
name = f"convertToFloat16_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Cast',
inputs=[x],
outputs=[name],
to=onnx.TensorProto.FLOAT16
)
self.NodeList.append(node)
return name
def convertToFloat32(x):
if x == None :
return None
name = f"convertToFloat32_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Cast',
inputs=[x],
outputs=[name],
to=onnx.TensorProto.FLOAT
)
self.NodeList.append(node)
return name
self.convertToFloat16 = convertToFloat16 if (dtype == onnx.TensorProto.FLOAT16 and fp32inout) else lambda x: x
self.convertToFloat32 = convertToFloat32 if (dtype == onnx.TensorProto.FLOAT16 and fp32inout) else lambda x: x
self.sqrt = sqrt
def mean(x, dim=None):
if dim == None:
dim = self.zeroInt
name = f"mean_{self.nm}_out"
self.nm += 1
if opsVersion == 18:
node = onnx.helper.make_node(
'ReduceMean',
inputs=[x,dim],
outputs=[name]
)
else:
node = onnx.helper.make_node(
'ReduceMean',
inputs=[x],
outputs=[name],
axes=dim,
keepdims=1
)
self.NodeList.append(node)
return name
self.mean = mean
def meanvarnorm(x, dim=None):
if dim == None:
dim = self.zeroInt
name = f"meanvarnorm_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'MeanVarianceNormalization',
inputs=[x],
outputs=[name],
axes=dim,
keepdims=1
)
self.NodeList.append(node)
return name
self.meanvarnorm = meanvarnorm
def relu(x):
name = f"relu_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Relu',
inputs=[x],
outputs=[name]
)
self.NodeList.append(node)
return name
self.relu = relu
def exp(x):
name = f"exp_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Exp',
inputs=[x],
outputs=[name]
)
self.NodeList.append(node)
return name
self.exp = exp
def stack(x, fp32 = False, exname = ""):
return [initTensor(r, fp32, exname) for r in x]
self.stack = stack
def matvec(x, y, outputfp32 = False):
name = f"matvec_{self.nm}_out"
oname = f"matvec_g_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'MatMul',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
if outputfp32:
return self.convertToFloat32(name)
return name
self.matvec = matvec
def prod(x):
name = f"prod_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'ReduceProd',
inputs=[x],
outputs=[name],
axes=[1],
keepdims=0
)
self.NodeList.append(node)
return name
self.prod = prod
def mul(x, y):
name = f"mul_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Mul',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
return name
self.multiply = mul
def squeeze(x):
name = f"squeeze_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Squeeze',
inputs=[x],
outputs=[name]
)
self.NodeList.append(node)
return name
def add(x, y):
name = f"add_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Add',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
return name
self.add = add
def sub(x, y):
name = f"sub_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Sub',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
return name
self.subtract = sub
self.one = initTensor([1.0]*embed)
self.margins = initTensor([0.00001]*embed, True)
self.margins16 = initTensor([0.00001]*embed)
self.margins32 = initTensor([0.00001]*(embed//heads))
self.margins3232 = initTensor([0.00001]*(embed//heads),True)
def lerpx(x, y, z):
return self.add(x, self.multiply(self.subtract(y, x), z))
self.lerp = lerpx
def minimum(x, y):
name = f"minimum_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Min',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
return name
self.minimum = minimum
# module def
self.module = object
def log(x):
name = f"log_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Log',
inputs=[x],
outputs=[name]
)
self.NodeList.append(node)
return name
self.log = log
# pytorch function defs
self.initfunc = lambda x: x
self.layerdef = lambda x: x
self.mainfunc = lambda x: x
def divide(x, y):
name = f"divide_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Div',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
return name
self.divide = divide
def layernorm17(x, w, b):
name = f"layernorm_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'LayerNormalization',
inputs=[x, w, b],
outputs=[name]
)
self.NodeList.append(node)
return name
# ort 15 does not support layernorm
def layernorm(x, w, b):
xee2 = self.subtract(x,self.mean(x))
x2 = self.add(self.sqrt(self.add(self.mean(self.multiply(xee2,xee2)), self.margins16)), self.margins16)
return self.add(self.multiply(w, self.divide(xee2, x2)), b)
self.layernorm = layernorm if opsVersion != 17 else layernorm17
def groupnorm(x, w, b):
x = self.reshape(x, self.premshape)
xee2 = self.subtract(x,self.mean(x,self.oneInt))
x2 = self.add(self.sqrt(self.add(self.mean(self.multiply(xee2,xee2),self.oneInt), self.margins32)), self.margins32)
return self.add(self.multiply(w, self.divide(xee2, x2)), b)
def groupnorm18(x, w, b):
name = f"groupnorm_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'GroupNormalization',
inputs=[x, w, b],
outputs=[name],
num_groups=heads
)
self.NodeList.append(node)
return name
self.groupnorm = groupnorm
def getIndex(x, y):
name = f"getIndex_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Gather',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
return squeeze(name)
self.stackEmbed = False
def neg(x):
name = f"neg_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Neg',
inputs=[x],
outputs=[name]
)
self.NodeList.append(node)
return name
self.neg = neg
def logistic(x):
name = f"logistic_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Sigmoid',
inputs=[x],
outputs=[name]
)
self.NodeList.append(node)
return name
self.logistical = logistic
def silu(x):
return self.multiply(x, logistic(x))
self.silu = silu
def reshape(x, y):
name = f"reshape_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Reshape',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
return name
self.reshape = reshape
self.kshape = initIntTensor([heads, embed//heads, 1])
self.vshape = initIntTensor([heads, 1, embed//heads])
self.rshape = initIntTensor([heads, 1, embed//heads])
self.normshape = initIntTensor([heads * embed//heads])
self.zeroInt = initIntTensor([0]) if opsVersion == 18 else [0]
self.oneInt = initIntTensor([1]) if opsVersion == 18 else [1]
self.eight = initTensor([8.0])
self.premshape = initIntTensor([heads, embed//heads])
def maximum(x, y):
name = f"maximum_{self.nm}_out"
self.nm += 1
node = onnx.helper.make_node(
'Max',
inputs=[x, y],
outputs=[name]
)
self.NodeList.append(node)
return name
self.maximum = maximum
self.getIndex = getIndex
# convert to float32
self.emptyState = np.array((([[0.00]*embed, [0.00]*embed]))*layers)
self.emptyState = np.array(self.emptyState)
# emptwkv state is n_layers,32,64,64
hs = embed//heads
self.emptyWkvState = np.array(([[[[0.0]*hs]*hs]*heads]*layers))
if dtype == onnx.TensorProto.FLOAT16 and not fp32inout:
self.emptyState = self.emptyState.astype(np.float16)
self.emptyWkvState = self.emptyWkvState.astype(np.float16)
# self.zero = initTensor([0.0]*embed)
def ppm(x):
import onnx
inputtensor = onnx.helper.make_tensor_value_info("input0",
onnx.TensorProto.INT32,
[1]), "input0"
emptyState = list(map(lambda x: (onnx.helper.make_tensor_value_info("instate"+str(x),
onnx.TensorProto.FLOAT if fp32inout else dtype,
[embed]), "instate"+str(x)), range((2)*layers)))
emptystate2 = list(map(lambda x: (onnx.helper.make_tensor_value_info("instatewkv"+str(x),
onnx.TensorProto.FLOAT if fp32inout else dtype,
[heads, hs, hs]), "instatewkv"+str(x)), range(layers)))
outs = x.forward(
inputtensor[1], list(map(lambda x: x[1], emptyState)), list(map(lambda x: x[1], emptystate2)))
print(self.TensorList.__len__())
print(self.NodeList.__len__())
print(outs)
logits = onnx.helper.make_tensor_value_info(outs[0],
onnx.TensorProto.FLOAT if fp32inout else dtype,
[65536])
state = list(map(lambda x: onnx.helper.make_tensor_value_info(x,
onnx.TensorProto.FLOAT if fp32inout else dtype,
[embed]), outs[1]))
state2 = list(map(lambda x: onnx.helper.make_tensor_value_info(x,
onnx.TensorProto.FLOAT if fp32inout else dtype,
[heads, hs, hs]), outs[2]))
# Create the graph (GraphProto)
graph_def = onnx.helper.make_graph(
nodes=self.NodeList, # The list of nodes in the graph.
name="RWKV",
# Graph input
inputs=[inputtensor[0], * \
list(map(lambda x:x[0], emptyState)), * \
list(map(lambda x:x[0], emptystate2))],
outputs=[logits, *state, *state2], # Graph output
initializer=self.TensorList, # initializer
# did not work, needs to be external
)
modelDef = onnx.helper.make_model(
graph_def, producer_name="rwkvstic",
)
modelDef.opset_import[0].version = opsVersion
print("Nearly save")
onnx.save(modelDef, exportname)
del modelDef
onnx.checker.check_model(exportname)
onnx.shape_inference.infer_shapes_path(exportname, check_type=True, strict_mode=True)
if quantized:
import onnx
from onnxruntime.quantization import quantize_dynamic, QuantType
model_fp32 = exportname
model_quant = "quantized_"+exportname
try:
quantized_model = quantize_dynamic(model_fp32, model_quant, per_channel=True, reduce_range=True, use_external_data_format=True)
import os
os.remove(model_fp32)
os.rename(model_quant, model_fp32)
os.remove(externalname+".bin")
except:
print("Quantization failed, chase this line and update the above code to use external data(if you are using a model more than 1b5)")
exit()
# run model
print("Model saved to: ", exportname, " and is ready to be run")
print("Data type: ", dtype)
print("Embedding size: ", embed)
print("Number of layers: ", layers)
print("external data: ", externalname)
exit()
self.postProcessModule = ppm