kkknight commited on
Commit
b0f44b2
·
verified ·
1 Parent(s): dbf1aa4

Upload 22 files

Browse files
.gitattributes CHANGED
@@ -36,3 +36,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
36
  Amazon/train/Industrial_and_Scientific_5_2016-10-2018-11.csv filter=lfs diff=lfs merge=lfs -text
37
  Amazon/train/Office_Products_5_2016-10-2018-11.csv filter=lfs diff=lfs merge=lfs -text
38
  Industrial_ckpt/tokenizer.json filter=lfs diff=lfs merge=lfs -text
 
 
36
  Amazon/train/Industrial_and_Scientific_5_2016-10-2018-11.csv filter=lfs diff=lfs merge=lfs -text
37
  Amazon/train/Office_Products_5_2016-10-2018-11.csv filter=lfs diff=lfs merge=lfs -text
38
  Industrial_ckpt/tokenizer.json filter=lfs diff=lfs merge=lfs -text
39
+ Office_ckpt/tokenizer.json filter=lfs diff=lfs merge=lfs -text
Office_ckpt/added_tokens.json ADDED
@@ -0,0 +1,624 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "</tool_call>": 151658,
3
+ "<a_0>": 151665,
4
+ "<a_101>": 151666,
5
+ "<a_102>": 151667,
6
+ "<a_106>": 151668,
7
+ "<a_116>": 151669,
8
+ "<a_117>": 151670,
9
+ "<a_118>": 151671,
10
+ "<a_11>": 151672,
11
+ "<a_120>": 151673,
12
+ "<a_122>": 151674,
13
+ "<a_123>": 151675,
14
+ "<a_127>": 151676,
15
+ "<a_130>": 151677,
16
+ "<a_131>": 151678,
17
+ "<a_133>": 151679,
18
+ "<a_135>": 151680,
19
+ "<a_137>": 151681,
20
+ "<a_142>": 151682,
21
+ "<a_143>": 151683,
22
+ "<a_145>": 151684,
23
+ "<a_14>": 151685,
24
+ "<a_160>": 151686,
25
+ "<a_162>": 151687,
26
+ "<a_166>": 151688,
27
+ "<a_170>": 151689,
28
+ "<a_177>": 151690,
29
+ "<a_178>": 151691,
30
+ "<a_180>": 151692,
31
+ "<a_183>": 151693,
32
+ "<a_184>": 151694,
33
+ "<a_186>": 151695,
34
+ "<a_187>": 151696,
35
+ "<a_188>": 151697,
36
+ "<a_18>": 151698,
37
+ "<a_191>": 151699,
38
+ "<a_192>": 151700,
39
+ "<a_195>": 151701,
40
+ "<a_197>": 151702,
41
+ "<a_1>": 151703,
42
+ "<a_201>": 151704,
43
+ "<a_202>": 151705,
44
+ "<a_203>": 151706,
45
+ "<a_204>": 151707,
46
+ "<a_205>": 151708,
47
+ "<a_20>": 151709,
48
+ "<a_212>": 151710,
49
+ "<a_216>": 151711,
50
+ "<a_221>": 151712,
51
+ "<a_224>": 151713,
52
+ "<a_225>": 151714,
53
+ "<a_230>": 151715,
54
+ "<a_232>": 151716,
55
+ "<a_235>": 151717,
56
+ "<a_238>": 151718,
57
+ "<a_23>": 151719,
58
+ "<a_242>": 151720,
59
+ "<a_248>": 151721,
60
+ "<a_250>": 151722,
61
+ "<a_252>": 151723,
62
+ "<a_253>": 151724,
63
+ "<a_255>": 151725,
64
+ "<a_28>": 151726,
65
+ "<a_31>": 151727,
66
+ "<a_34>": 151728,
67
+ "<a_39>": 151729,
68
+ "<a_3>": 151730,
69
+ "<a_41>": 151731,
70
+ "<a_45>": 151732,
71
+ "<a_46>": 151733,
72
+ "<a_47>": 151734,
73
+ "<a_53>": 151735,
74
+ "<a_55>": 151736,
75
+ "<a_60>": 151737,
76
+ "<a_66>": 151738,
77
+ "<a_67>": 151739,
78
+ "<a_74>": 151740,
79
+ "<a_76>": 151741,
80
+ "<a_77>": 151742,
81
+ "<a_78>": 151743,
82
+ "<a_7>": 151744,
83
+ "<a_81>": 151745,
84
+ "<a_84>": 151746,
85
+ "<a_87>": 151747,
86
+ "<a_91>": 151748,
87
+ "<a_93>": 151749,
88
+ "<a_94>": 151750,
89
+ "<a_96>": 151751,
90
+ "<a_99>": 151752,
91
+ "<b_0>": 151753,
92
+ "<b_100>": 151754,
93
+ "<b_101>": 151755,
94
+ "<b_102>": 151756,
95
+ "<b_103>": 151757,
96
+ "<b_104>": 151758,
97
+ "<b_105>": 151759,
98
+ "<b_106>": 151760,
99
+ "<b_107>": 151761,
100
+ "<b_108>": 151762,
101
+ "<b_109>": 151763,
102
+ "<b_10>": 151764,
103
+ "<b_110>": 151765,
104
+ "<b_111>": 151766,
105
+ "<b_112>": 151767,
106
+ "<b_113>": 151768,
107
+ "<b_114>": 151769,
108
+ "<b_115>": 151770,
109
+ "<b_116>": 151771,
110
+ "<b_117>": 151772,
111
+ "<b_118>": 151773,
112
+ "<b_119>": 151774,
113
+ "<b_11>": 151775,
114
+ "<b_120>": 151776,
115
+ "<b_121>": 151777,
116
+ "<b_122>": 151778,
117
+ "<b_123>": 151779,
118
+ "<b_124>": 151780,
119
+ "<b_125>": 151781,
120
+ "<b_126>": 151782,
121
+ "<b_127>": 151783,
122
+ "<b_128>": 151784,
123
+ "<b_129>": 151785,
124
+ "<b_12>": 151786,
125
+ "<b_130>": 151787,
126
+ "<b_131>": 151788,
127
+ "<b_132>": 151789,
128
+ "<b_133>": 151790,
129
+ "<b_134>": 151791,
130
+ "<b_135>": 151792,
131
+ "<b_136>": 151793,
132
+ "<b_137>": 151794,
133
+ "<b_138>": 151795,
134
+ "<b_139>": 151796,
135
+ "<b_13>": 151797,
136
+ "<b_140>": 151798,
137
+ "<b_141>": 151799,
138
+ "<b_142>": 151800,
139
+ "<b_143>": 151801,
140
+ "<b_144>": 151802,
141
+ "<b_145>": 151803,
142
+ "<b_146>": 151804,
143
+ "<b_147>": 151805,
144
+ "<b_148>": 151806,
145
+ "<b_149>": 151807,
146
+ "<b_14>": 151808,
147
+ "<b_150>": 151809,
148
+ "<b_151>": 151810,
149
+ "<b_152>": 151811,
150
+ "<b_153>": 151812,
151
+ "<b_154>": 151813,
152
+ "<b_155>": 151814,
153
+ "<b_156>": 151815,
154
+ "<b_157>": 151816,
155
+ "<b_158>": 151817,
156
+ "<b_159>": 151818,
157
+ "<b_15>": 151819,
158
+ "<b_160>": 151820,
159
+ "<b_161>": 151821,
160
+ "<b_162>": 151822,
161
+ "<b_163>": 151823,
162
+ "<b_164>": 151824,
163
+ "<b_165>": 151825,
164
+ "<b_166>": 151826,
165
+ "<b_167>": 151827,
166
+ "<b_168>": 151828,
167
+ "<b_169>": 151829,
168
+ "<b_16>": 151830,
169
+ "<b_170>": 151831,
170
+ "<b_171>": 151832,
171
+ "<b_172>": 151833,
172
+ "<b_173>": 151834,
173
+ "<b_174>": 151835,
174
+ "<b_175>": 151836,
175
+ "<b_176>": 151837,
176
+ "<b_177>": 151838,
177
+ "<b_178>": 151839,
178
+ "<b_179>": 151840,
179
+ "<b_17>": 151841,
180
+ "<b_180>": 151842,
181
+ "<b_181>": 151843,
182
+ "<b_182>": 151844,
183
+ "<b_183>": 151845,
184
+ "<b_184>": 151846,
185
+ "<b_185>": 151847,
186
+ "<b_186>": 151848,
187
+ "<b_187>": 151849,
188
+ "<b_188>": 151850,
189
+ "<b_189>": 151851,
190
+ "<b_18>": 151852,
191
+ "<b_190>": 151853,
192
+ "<b_191>": 151854,
193
+ "<b_192>": 151855,
194
+ "<b_193>": 151856,
195
+ "<b_194>": 151857,
196
+ "<b_195>": 151858,
197
+ "<b_196>": 151859,
198
+ "<b_197>": 151860,
199
+ "<b_198>": 151861,
200
+ "<b_199>": 151862,
201
+ "<b_19>": 151863,
202
+ "<b_1>": 151864,
203
+ "<b_200>": 151865,
204
+ "<b_201>": 151866,
205
+ "<b_202>": 151867,
206
+ "<b_203>": 151868,
207
+ "<b_204>": 151869,
208
+ "<b_205>": 151870,
209
+ "<b_206>": 151871,
210
+ "<b_207>": 151872,
211
+ "<b_208>": 151873,
212
+ "<b_209>": 151874,
213
+ "<b_20>": 151875,
214
+ "<b_210>": 151876,
215
+ "<b_211>": 151877,
216
+ "<b_212>": 151878,
217
+ "<b_213>": 151879,
218
+ "<b_214>": 151880,
219
+ "<b_215>": 151881,
220
+ "<b_216>": 151882,
221
+ "<b_217>": 151883,
222
+ "<b_218>": 151884,
223
+ "<b_219>": 151885,
224
+ "<b_21>": 151886,
225
+ "<b_220>": 151887,
226
+ "<b_221>": 151888,
227
+ "<b_222>": 151889,
228
+ "<b_223>": 151890,
229
+ "<b_224>": 151891,
230
+ "<b_225>": 151892,
231
+ "<b_226>": 151893,
232
+ "<b_227>": 151894,
233
+ "<b_228>": 151895,
234
+ "<b_229>": 151896,
235
+ "<b_22>": 151897,
236
+ "<b_230>": 151898,
237
+ "<b_231>": 151899,
238
+ "<b_232>": 151900,
239
+ "<b_233>": 151901,
240
+ "<b_234>": 151902,
241
+ "<b_235>": 151903,
242
+ "<b_236>": 151904,
243
+ "<b_237>": 151905,
244
+ "<b_238>": 151906,
245
+ "<b_239>": 151907,
246
+ "<b_23>": 151908,
247
+ "<b_240>": 151909,
248
+ "<b_241>": 151910,
249
+ "<b_242>": 151911,
250
+ "<b_243>": 151912,
251
+ "<b_244>": 151913,
252
+ "<b_245>": 151914,
253
+ "<b_246>": 151915,
254
+ "<b_247>": 151916,
255
+ "<b_248>": 151917,
256
+ "<b_249>": 151918,
257
+ "<b_24>": 151919,
258
+ "<b_250>": 151920,
259
+ "<b_251>": 151921,
260
+ "<b_252>": 151922,
261
+ "<b_253>": 151923,
262
+ "<b_254>": 151924,
263
+ "<b_255>": 151925,
264
+ "<b_25>": 151926,
265
+ "<b_26>": 151927,
266
+ "<b_27>": 151928,
267
+ "<b_28>": 151929,
268
+ "<b_29>": 151930,
269
+ "<b_2>": 151931,
270
+ "<b_30>": 151932,
271
+ "<b_31>": 151933,
272
+ "<b_32>": 151934,
273
+ "<b_33>": 151935,
274
+ "<b_34>": 151936,
275
+ "<b_35>": 151937,
276
+ "<b_36>": 151938,
277
+ "<b_37>": 151939,
278
+ "<b_38>": 151940,
279
+ "<b_39>": 151941,
280
+ "<b_3>": 151942,
281
+ "<b_40>": 151943,
282
+ "<b_41>": 151944,
283
+ "<b_42>": 151945,
284
+ "<b_43>": 151946,
285
+ "<b_44>": 151947,
286
+ "<b_45>": 151948,
287
+ "<b_46>": 151949,
288
+ "<b_47>": 151950,
289
+ "<b_48>": 151951,
290
+ "<b_49>": 151952,
291
+ "<b_4>": 151953,
292
+ "<b_50>": 151954,
293
+ "<b_51>": 151955,
294
+ "<b_52>": 151956,
295
+ "<b_53>": 151957,
296
+ "<b_54>": 151958,
297
+ "<b_55>": 151959,
298
+ "<b_56>": 151960,
299
+ "<b_57>": 151961,
300
+ "<b_58>": 151962,
301
+ "<b_59>": 151963,
302
+ "<b_5>": 151964,
303
+ "<b_60>": 151965,
304
+ "<b_61>": 151966,
305
+ "<b_62>": 151967,
306
+ "<b_63>": 151968,
307
+ "<b_64>": 151969,
308
+ "<b_65>": 151970,
309
+ "<b_66>": 151971,
310
+ "<b_67>": 151972,
311
+ "<b_68>": 151973,
312
+ "<b_69>": 151974,
313
+ "<b_6>": 151975,
314
+ "<b_70>": 151976,
315
+ "<b_71>": 151977,
316
+ "<b_72>": 151978,
317
+ "<b_73>": 151979,
318
+ "<b_74>": 151980,
319
+ "<b_75>": 151981,
320
+ "<b_76>": 151982,
321
+ "<b_77>": 151983,
322
+ "<b_78>": 151984,
323
+ "<b_79>": 151985,
324
+ "<b_7>": 151986,
325
+ "<b_80>": 151987,
326
+ "<b_81>": 151988,
327
+ "<b_82>": 151989,
328
+ "<b_83>": 151990,
329
+ "<b_84>": 151991,
330
+ "<b_85>": 151992,
331
+ "<b_86>": 151993,
332
+ "<b_87>": 151994,
333
+ "<b_88>": 151995,
334
+ "<b_89>": 151996,
335
+ "<b_8>": 151997,
336
+ "<b_90>": 151998,
337
+ "<b_91>": 151999,
338
+ "<b_92>": 152000,
339
+ "<b_93>": 152001,
340
+ "<b_94>": 152002,
341
+ "<b_95>": 152003,
342
+ "<b_96>": 152004,
343
+ "<b_97>": 152005,
344
+ "<b_98>": 152006,
345
+ "<b_99>": 152007,
346
+ "<b_9>": 152008,
347
+ "<c_0>": 152009,
348
+ "<c_100>": 152010,
349
+ "<c_101>": 152011,
350
+ "<c_102>": 152012,
351
+ "<c_103>": 152013,
352
+ "<c_104>": 152014,
353
+ "<c_105>": 152015,
354
+ "<c_106>": 152016,
355
+ "<c_107>": 152017,
356
+ "<c_108>": 152018,
357
+ "<c_109>": 152019,
358
+ "<c_10>": 152020,
359
+ "<c_110>": 152021,
360
+ "<c_111>": 152022,
361
+ "<c_112>": 152023,
362
+ "<c_113>": 152024,
363
+ "<c_114>": 152025,
364
+ "<c_115>": 152026,
365
+ "<c_116>": 152027,
366
+ "<c_117>": 152028,
367
+ "<c_118>": 152029,
368
+ "<c_119>": 152030,
369
+ "<c_11>": 152031,
370
+ "<c_120>": 152032,
371
+ "<c_121>": 152033,
372
+ "<c_122>": 152034,
373
+ "<c_123>": 152035,
374
+ "<c_124>": 152036,
375
+ "<c_125>": 152037,
376
+ "<c_126>": 152038,
377
+ "<c_127>": 152039,
378
+ "<c_128>": 152040,
379
+ "<c_129>": 152041,
380
+ "<c_12>": 152042,
381
+ "<c_130>": 152043,
382
+ "<c_131>": 152044,
383
+ "<c_132>": 152045,
384
+ "<c_133>": 152046,
385
+ "<c_134>": 152047,
386
+ "<c_135>": 152048,
387
+ "<c_136>": 152049,
388
+ "<c_137>": 152050,
389
+ "<c_138>": 152051,
390
+ "<c_139>": 152052,
391
+ "<c_13>": 152053,
392
+ "<c_140>": 152054,
393
+ "<c_141>": 152055,
394
+ "<c_142>": 152056,
395
+ "<c_143>": 152057,
396
+ "<c_144>": 152058,
397
+ "<c_145>": 152059,
398
+ "<c_146>": 152060,
399
+ "<c_147>": 152061,
400
+ "<c_148>": 152062,
401
+ "<c_149>": 152063,
402
+ "<c_14>": 152064,
403
+ "<c_150>": 152065,
404
+ "<c_151>": 152066,
405
+ "<c_152>": 152067,
406
+ "<c_153>": 152068,
407
+ "<c_154>": 152069,
408
+ "<c_155>": 152070,
409
+ "<c_156>": 152071,
410
+ "<c_157>": 152072,
411
+ "<c_158>": 152073,
412
+ "<c_159>": 152074,
413
+ "<c_15>": 152075,
414
+ "<c_160>": 152076,
415
+ "<c_161>": 152077,
416
+ "<c_162>": 152078,
417
+ "<c_163>": 152079,
418
+ "<c_164>": 152080,
419
+ "<c_165>": 152081,
420
+ "<c_166>": 152082,
421
+ "<c_167>": 152083,
422
+ "<c_168>": 152084,
423
+ "<c_169>": 152085,
424
+ "<c_16>": 152086,
425
+ "<c_170>": 152087,
426
+ "<c_171>": 152088,
427
+ "<c_172>": 152089,
428
+ "<c_173>": 152090,
429
+ "<c_174>": 152091,
430
+ "<c_175>": 152092,
431
+ "<c_176>": 152093,
432
+ "<c_177>": 152094,
433
+ "<c_178>": 152095,
434
+ "<c_179>": 152096,
435
+ "<c_17>": 152097,
436
+ "<c_180>": 152098,
437
+ "<c_181>": 152099,
438
+ "<c_182>": 152100,
439
+ "<c_183>": 152101,
440
+ "<c_184>": 152102,
441
+ "<c_185>": 152103,
442
+ "<c_186>": 152104,
443
+ "<c_187>": 152105,
444
+ "<c_188>": 152106,
445
+ "<c_189>": 152107,
446
+ "<c_18>": 152108,
447
+ "<c_190>": 152109,
448
+ "<c_191>": 152110,
449
+ "<c_192>": 152111,
450
+ "<c_193>": 152112,
451
+ "<c_194>": 152113,
452
+ "<c_195>": 152114,
453
+ "<c_196>": 152115,
454
+ "<c_197>": 152116,
455
+ "<c_198>": 152117,
456
+ "<c_199>": 152118,
457
+ "<c_19>": 152119,
458
+ "<c_1>": 152120,
459
+ "<c_200>": 152121,
460
+ "<c_201>": 152122,
461
+ "<c_202>": 152123,
462
+ "<c_203>": 152124,
463
+ "<c_204>": 152125,
464
+ "<c_205>": 152126,
465
+ "<c_206>": 152127,
466
+ "<c_207>": 152128,
467
+ "<c_208>": 152129,
468
+ "<c_209>": 152130,
469
+ "<c_20>": 152131,
470
+ "<c_210>": 152132,
471
+ "<c_211>": 152133,
472
+ "<c_212>": 152134,
473
+ "<c_213>": 152135,
474
+ "<c_214>": 152136,
475
+ "<c_215>": 152137,
476
+ "<c_216>": 152138,
477
+ "<c_217>": 152139,
478
+ "<c_218>": 152140,
479
+ "<c_219>": 152141,
480
+ "<c_21>": 152142,
481
+ "<c_220>": 152143,
482
+ "<c_221>": 152144,
483
+ "<c_222>": 152145,
484
+ "<c_223>": 152146,
485
+ "<c_224>": 152147,
486
+ "<c_225>": 152148,
487
+ "<c_226>": 152149,
488
+ "<c_227>": 152150,
489
+ "<c_228>": 152151,
490
+ "<c_229>": 152152,
491
+ "<c_22>": 152153,
492
+ "<c_230>": 152154,
493
+ "<c_231>": 152155,
494
+ "<c_232>": 152156,
495
+ "<c_233>": 152157,
496
+ "<c_234>": 152158,
497
+ "<c_235>": 152159,
498
+ "<c_236>": 152160,
499
+ "<c_237>": 152161,
500
+ "<c_238>": 152162,
501
+ "<c_239>": 152163,
502
+ "<c_23>": 152164,
503
+ "<c_240>": 152165,
504
+ "<c_241>": 152166,
505
+ "<c_242>": 152167,
506
+ "<c_243>": 152168,
507
+ "<c_244>": 152169,
508
+ "<c_245>": 152170,
509
+ "<c_246>": 152171,
510
+ "<c_247>": 152172,
511
+ "<c_248>": 152173,
512
+ "<c_249>": 152174,
513
+ "<c_24>": 152175,
514
+ "<c_250>": 152176,
515
+ "<c_251>": 152177,
516
+ "<c_252>": 152178,
517
+ "<c_253>": 152179,
518
+ "<c_254>": 152180,
519
+ "<c_255>": 152181,
520
+ "<c_25>": 152182,
521
+ "<c_26>": 152183,
522
+ "<c_27>": 152184,
523
+ "<c_28>": 152185,
524
+ "<c_29>": 152186,
525
+ "<c_2>": 152187,
526
+ "<c_30>": 152188,
527
+ "<c_31>": 152189,
528
+ "<c_32>": 152190,
529
+ "<c_33>": 152191,
530
+ "<c_34>": 152192,
531
+ "<c_35>": 152193,
532
+ "<c_36>": 152194,
533
+ "<c_37>": 152195,
534
+ "<c_38>": 152196,
535
+ "<c_39>": 152197,
536
+ "<c_3>": 152198,
537
+ "<c_40>": 152199,
538
+ "<c_41>": 152200,
539
+ "<c_42>": 152201,
540
+ "<c_43>": 152202,
541
+ "<c_44>": 152203,
542
+ "<c_45>": 152204,
543
+ "<c_46>": 152205,
544
+ "<c_47>": 152206,
545
+ "<c_48>": 152207,
546
+ "<c_49>": 152208,
547
+ "<c_4>": 152209,
548
+ "<c_50>": 152210,
549
+ "<c_51>": 152211,
550
+ "<c_52>": 152212,
551
+ "<c_53>": 152213,
552
+ "<c_54>": 152214,
553
+ "<c_55>": 152215,
554
+ "<c_56>": 152216,
555
+ "<c_57>": 152217,
556
+ "<c_58>": 152218,
557
+ "<c_59>": 152219,
558
+ "<c_5>": 152220,
559
+ "<c_60>": 152221,
560
+ "<c_61>": 152222,
561
+ "<c_62>": 152223,
562
+ "<c_63>": 152224,
563
+ "<c_64>": 152225,
564
+ "<c_65>": 152226,
565
+ "<c_66>": 152227,
566
+ "<c_67>": 152228,
567
+ "<c_68>": 152229,
568
+ "<c_69>": 152230,
569
+ "<c_6>": 152231,
570
+ "<c_70>": 152232,
571
+ "<c_71>": 152233,
572
+ "<c_72>": 152234,
573
+ "<c_73>": 152235,
574
+ "<c_74>": 152236,
575
+ "<c_75>": 152237,
576
+ "<c_76>": 152238,
577
+ "<c_77>": 152239,
578
+ "<c_78>": 152240,
579
+ "<c_79>": 152241,
580
+ "<c_7>": 152242,
581
+ "<c_80>": 152243,
582
+ "<c_81>": 152244,
583
+ "<c_82>": 152245,
584
+ "<c_83>": 152246,
585
+ "<c_84>": 152247,
586
+ "<c_85>": 152248,
587
+ "<c_86>": 152249,
588
+ "<c_87>": 152250,
589
+ "<c_88>": 152251,
590
+ "<c_89>": 152252,
591
+ "<c_8>": 152253,
592
+ "<c_90>": 152254,
593
+ "<c_91>": 152255,
594
+ "<c_92>": 152256,
595
+ "<c_93>": 152257,
596
+ "<c_94>": 152258,
597
+ "<c_95>": 152259,
598
+ "<c_96>": 152260,
599
+ "<c_97>": 152261,
600
+ "<c_98>": 152262,
601
+ "<c_99>": 152263,
602
+ "<c_9>": 152264,
603
+ "<tool_call>": 151657,
604
+ "<|box_end|>": 151649,
605
+ "<|box_start|>": 151648,
606
+ "<|endoftext|>": 151643,
607
+ "<|file_sep|>": 151664,
608
+ "<|fim_middle|>": 151660,
609
+ "<|fim_pad|>": 151662,
610
+ "<|fim_prefix|>": 151659,
611
+ "<|fim_suffix|>": 151661,
612
+ "<|im_end|>": 151645,
613
+ "<|im_start|>": 151644,
614
+ "<|image_pad|>": 151655,
615
+ "<|object_ref_end|>": 151647,
616
+ "<|object_ref_start|>": 151646,
617
+ "<|quad_end|>": 151651,
618
+ "<|quad_start|>": 151650,
619
+ "<|repo_name|>": 151663,
620
+ "<|video_pad|>": 151656,
621
+ "<|vision_end|>": 151653,
622
+ "<|vision_pad|>": 151654,
623
+ "<|vision_start|>": 151652
624
+ }
Office_ckpt/chat_template.jinja ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if tools %}
2
+ {{- '<|im_start|>system\n' }}
3
+ {%- if messages[0]['role'] == 'system' %}
4
+ {{- messages[0]['content'] }}
5
+ {%- else %}
6
+ {{- 'You are Qwen, created by Alibaba Cloud. You are a helpful assistant.' }}
7
+ {%- endif %}
8
+ {{- "\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
9
+ {%- for tool in tools %}
10
+ {{- "\n" }}
11
+ {{- tool | tojson }}
12
+ {%- endfor %}
13
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
14
+ {%- else %}
15
+ {%- if messages[0]['role'] == 'system' %}
16
+ {{- '<|im_start|>system\n' + messages[0]['content'] + '<|im_end|>\n' }}
17
+ {%- else %}
18
+ {{- '<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n' }}
19
+ {%- endif %}
20
+ {%- endif %}
21
+ {%- for message in messages %}
22
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) or (message.role == "assistant" and not message.tool_calls) %}
23
+ {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
24
+ {%- elif message.role == "assistant" %}
25
+ {{- '<|im_start|>' + message.role }}
26
+ {%- if message.content %}
27
+ {{- '\n' + message.content }}
28
+ {%- endif %}
29
+ {%- for tool_call in message.tool_calls %}
30
+ {%- if tool_call.function is defined %}
31
+ {%- set tool_call = tool_call.function %}
32
+ {%- endif %}
33
+ {{- '\n<tool_call>\n{"name": "' }}
34
+ {{- tool_call.name }}
35
+ {{- '", "arguments": ' }}
36
+ {{- tool_call.arguments | tojson }}
37
+ {{- '}\n</tool_call>' }}
38
+ {%- endfor %}
39
+ {{- '<|im_end|>\n' }}
40
+ {%- elif message.role == "tool" %}
41
+ {%- if (loop.index0 == 0) or (messages[loop.index0 - 1].role != "tool") %}
42
+ {{- '<|im_start|>user' }}
43
+ {%- endif %}
44
+ {{- '\n<tool_response>\n' }}
45
+ {{- message.content }}
46
+ {{- '\n</tool_response>' }}
47
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
48
+ {{- '<|im_end|>\n' }}
49
+ {%- endif %}
50
+ {%- endif %}
51
+ {%- endfor %}
52
+ {%- if add_generation_prompt %}
53
+ {{- '<|im_start|>assistant\n' }}
54
+ {%- endif %}
Office_ckpt/config.json ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Qwen2ForCausalLM"
4
+ ],
5
+ "attention_dropout": 0.0,
6
+ "bos_token_id": 151643,
7
+ "eos_token_id": 151645,
8
+ "hidden_act": "silu",
9
+ "hidden_size": 1536,
10
+ "initializer_range": 0.02,
11
+ "intermediate_size": 8960,
12
+ "layer_types": [
13
+ "full_attention",
14
+ "full_attention",
15
+ "full_attention",
16
+ "full_attention",
17
+ "full_attention",
18
+ "full_attention",
19
+ "full_attention",
20
+ "full_attention",
21
+ "full_attention",
22
+ "full_attention",
23
+ "full_attention",
24
+ "full_attention",
25
+ "full_attention",
26
+ "full_attention",
27
+ "full_attention",
28
+ "full_attention",
29
+ "full_attention",
30
+ "full_attention",
31
+ "full_attention",
32
+ "full_attention",
33
+ "full_attention",
34
+ "full_attention",
35
+ "full_attention",
36
+ "full_attention",
37
+ "full_attention",
38
+ "full_attention",
39
+ "full_attention",
40
+ "full_attention"
41
+ ],
42
+ "max_position_embeddings": 32768,
43
+ "max_window_layers": 21,
44
+ "model_type": "qwen2",
45
+ "num_attention_heads": 12,
46
+ "num_hidden_layers": 28,
47
+ "num_key_value_heads": 2,
48
+ "rms_norm_eps": 1e-06,
49
+ "rope_scaling": null,
50
+ "rope_theta": 1000000.0,
51
+ "sliding_window": null,
52
+ "tie_word_embeddings": true,
53
+ "torch_dtype": "bfloat16",
54
+ "transformers_version": "4.55.4",
55
+ "use_cache": null,
56
+ "use_sliding_window": false,
57
+ "vocab_size": 152265
58
+ }
Office_ckpt/generation_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 151643,
4
+ "eos_token_id": 151645,
5
+ "transformers_version": "4.55.4"
6
+ }
Office_ckpt/latest.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ global_step1038
Office_ckpt/merges.txt ADDED
The diff for this file is too large to render. See raw diff
 
Office_ckpt/rng_state_0.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:36d2a2034ebb05cb71c510897f2795b31164e50f17b270bc25d2be3ad9a17b22
3
+ size 15984
Office_ckpt/rng_state_1.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e3c6a047e69957a4fcef4efdb6800b809ec2139e77ee22d4fcfa89f2cd323ecd
3
+ size 15920
Office_ckpt/rng_state_2.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ccd8154731e6030b615ae4037f15896288ad29f8b09e158fb8c62ca6f907b9a
3
+ size 15920
Office_ckpt/rng_state_3.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b6f89fccab8305e6fcb9c2f091f7bfd4540f0cdfeb328fd939b82b833352c8cf
3
+ size 15984
Office_ckpt/rng_state_4.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b22090cf19a126293a342f5a96142916c9fd77589ffe907ab9db4ee04994b96b
3
+ size 15984
Office_ckpt/rng_state_5.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:d3b75c906af53e2002f47853b7021f6c107cf9d9c95a63e992594be66db3cf2b
3
+ size 15984
Office_ckpt/rng_state_6.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8e6508a4ffa116a655533ee330827208aac9391f37a8d25df4ae3803fbc11122
3
+ size 15984
Office_ckpt/rng_state_7.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:12740e5b2c1d46dbf0ca69e3f7d898ace8ffa7c77093bf7db2d8141c26a09286
3
+ size 15920
Office_ckpt/scheduler.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5f73111d858ccd50a2c47017ecebd12fecd5ef13eed7f9930c60873e2f5b84b2
3
+ size 1064
Office_ckpt/special_tokens_map.json ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "additional_special_tokens": [
3
+ "<|im_start|>",
4
+ "<|im_end|>",
5
+ "<|object_ref_start|>",
6
+ "<|object_ref_end|>",
7
+ "<|box_start|>",
8
+ "<|box_end|>",
9
+ "<|quad_start|>",
10
+ "<|quad_end|>",
11
+ "<|vision_start|>",
12
+ "<|vision_end|>",
13
+ "<|vision_pad|>",
14
+ "<|image_pad|>",
15
+ "<|video_pad|>"
16
+ ],
17
+ "eos_token": {
18
+ "content": "<|im_end|>",
19
+ "lstrip": false,
20
+ "normalized": false,
21
+ "rstrip": false,
22
+ "single_word": false
23
+ },
24
+ "pad_token": "<|im_end|>"
25
+ }
Office_ckpt/tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:727f175c5ff824b7cfa45c8577e293b356b255b215a504834c4451298bd5c50a
3
+ size 11532202
Office_ckpt/tokenizer_config.json ADDED
The diff for this file is too large to render. See raw diff
 
Office_ckpt/trainer_state.json ADDED
The diff for this file is too large to render. See raw diff
 
Office_ckpt/training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:29ca1599f8dd75c9f347a26477d6bf3c4afdc0ad9dabb85ee1e6e8e951f3f33e
3
+ size 7544
Office_ckpt/vocab.json ADDED
The diff for this file is too large to render. See raw diff
 
Office_ckpt/zero_to_fp32.py ADDED
@@ -0,0 +1,760 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright (c) Microsoft Corporation.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ # DeepSpeed Team
7
+
8
+ # This script extracts fp32 consolidated weights from a zero 1, 2 and 3 DeepSpeed checkpoints. It gets
9
+ # copied into the top level checkpoint dir, so the user can easily do the conversion at any point in
10
+ # the future. Once extracted, the weights don't require DeepSpeed and can be used in any
11
+ # application.
12
+ #
13
+ # example:
14
+ # python zero_to_fp32.py . output_dir/
15
+ # or
16
+ # python zero_to_fp32.py . output_dir/ --safe_serialization
17
+
18
+ import argparse
19
+ import torch
20
+ import glob
21
+ import math
22
+ import os
23
+ import re
24
+ import gc
25
+ import json
26
+ import numpy as np
27
+ from tqdm import tqdm
28
+ from collections import OrderedDict
29
+ from dataclasses import dataclass
30
+
31
+ # while this script doesn't use deepspeed to recover data, since the checkpoints are pickled with
32
+ # DeepSpeed data structures it has to be available in the current python environment.
33
+ from deepspeed.utils import logger
34
+ from deepspeed.checkpoint.constants import (DS_VERSION, OPTIMIZER_STATE_DICT, SINGLE_PARTITION_OF_FP32_GROUPS,
35
+ FP32_FLAT_GROUPS, ZERO_STAGE, PARTITION_COUNT, PARAM_SHAPES, BUFFER_NAMES,
36
+ FROZEN_PARAM_SHAPES, FROZEN_PARAM_FRAGMENTS)
37
+
38
+
39
+ @dataclass
40
+ class zero_model_state:
41
+ buffers: dict()
42
+ param_shapes: dict()
43
+ shared_params: list
44
+ ds_version: int
45
+ frozen_param_shapes: dict()
46
+ frozen_param_fragments: dict()
47
+
48
+
49
+ debug = 0
50
+
51
+ # load to cpu
52
+ device = torch.device('cpu')
53
+
54
+
55
+ def atoi(text):
56
+ return int(text) if text.isdigit() else text
57
+
58
+
59
+ def natural_keys(text):
60
+ '''
61
+ alist.sort(key=natural_keys) sorts in human order
62
+ http://nedbatchelder.com/blog/200712/human_sorting.html
63
+ (See Toothy's implementation in the comments)
64
+ '''
65
+ return [atoi(c) for c in re.split(r'(\d+)', text)]
66
+
67
+
68
+ def get_model_state_file(checkpoint_dir, zero_stage):
69
+ if not os.path.isdir(checkpoint_dir):
70
+ raise FileNotFoundError(f"Directory '{checkpoint_dir}' doesn't exist")
71
+
72
+ # there should be only one file
73
+ if zero_stage <= 2:
74
+ file = os.path.join(checkpoint_dir, "mp_rank_00_model_states.pt")
75
+ elif zero_stage == 3:
76
+ file = os.path.join(checkpoint_dir, "zero_pp_rank_0_mp_rank_00_model_states.pt")
77
+
78
+ if not os.path.exists(file):
79
+ raise FileNotFoundError(f"can't find model states file at '{file}'")
80
+
81
+ return file
82
+
83
+
84
+ def get_checkpoint_files(checkpoint_dir, glob_pattern):
85
+ # XXX: need to test that this simple glob rule works for multi-node setup too
86
+ ckpt_files = sorted(glob.glob(os.path.join(checkpoint_dir, glob_pattern)), key=natural_keys)
87
+
88
+ if len(ckpt_files) == 0:
89
+ raise FileNotFoundError(f"can't find {glob_pattern} files in directory '{checkpoint_dir}'")
90
+
91
+ return ckpt_files
92
+
93
+
94
+ def get_optim_files(checkpoint_dir):
95
+ return get_checkpoint_files(checkpoint_dir, "*_optim_states.pt")
96
+
97
+
98
+ def get_model_state_files(checkpoint_dir):
99
+ return get_checkpoint_files(checkpoint_dir, "*_model_states.pt")
100
+
101
+
102
+ def parse_model_states(files):
103
+ zero_model_states = []
104
+ for file in files:
105
+ state_dict = torch.load(file, map_location=device, weights_only=False)
106
+
107
+ if BUFFER_NAMES not in state_dict:
108
+ raise ValueError(f"{file} is not a model state checkpoint")
109
+ buffer_names = state_dict[BUFFER_NAMES]
110
+ if debug:
111
+ print("Found buffers:", buffer_names)
112
+
113
+ # recover just the buffers while restoring them to fp32 if they were saved in fp16
114
+ buffers = {k: v.float() for k, v in state_dict["module"].items() if k in buffer_names}
115
+ param_shapes = state_dict[PARAM_SHAPES]
116
+
117
+ # collect parameters that are included in param_shapes
118
+ param_names = []
119
+ for s in param_shapes:
120
+ for name in s.keys():
121
+ param_names.append(name)
122
+
123
+ # update with frozen parameters
124
+ frozen_param_shapes = state_dict.get(FROZEN_PARAM_SHAPES, None)
125
+ if frozen_param_shapes is not None:
126
+ if debug:
127
+ print(f"Found frozen_param_shapes: {frozen_param_shapes}")
128
+ param_names += list(frozen_param_shapes.keys())
129
+
130
+ # handle shared params
131
+ shared_params = [[k, v] for k, v in state_dict["shared_params"].items()]
132
+
133
+ ds_version = state_dict.get(DS_VERSION, None)
134
+
135
+ frozen_param_fragments = state_dict.get(FROZEN_PARAM_FRAGMENTS, None)
136
+
137
+ z_model_state = zero_model_state(buffers=buffers,
138
+ param_shapes=param_shapes,
139
+ shared_params=shared_params,
140
+ ds_version=ds_version,
141
+ frozen_param_shapes=frozen_param_shapes,
142
+ frozen_param_fragments=frozen_param_fragments)
143
+ zero_model_states.append(z_model_state)
144
+
145
+ return zero_model_states
146
+
147
+
148
+ def parse_optim_states(files, ds_checkpoint_dir):
149
+ total_files = len(files)
150
+ state_dicts = []
151
+ for f in tqdm(files, desc='Loading checkpoint shards'):
152
+ state_dict = torch.load(f, map_location=device, mmap=True, weights_only=False)
153
+ # immediately discard the potentially huge 2 optimizer states as we only care for fp32 master weights
154
+ # and also handle the case where it was already removed by another helper script
155
+ state_dict["optimizer_state_dict"].pop("optimizer_state_dict", None)
156
+ state_dicts.append(state_dict)
157
+
158
+ if not ZERO_STAGE in state_dicts[0][OPTIMIZER_STATE_DICT]:
159
+ raise ValueError(f"{files[0]} is not a zero checkpoint")
160
+ zero_stage = state_dicts[0][OPTIMIZER_STATE_DICT][ZERO_STAGE]
161
+ world_size = state_dicts[0][OPTIMIZER_STATE_DICT][PARTITION_COUNT]
162
+
163
+ # For ZeRO-2 each param group can have different partition_count as data parallelism for expert
164
+ # parameters can be different from data parallelism for non-expert parameters. So we can just
165
+ # use the max of the partition_count to get the dp world_size.
166
+
167
+ if type(world_size) is list:
168
+ world_size = max(world_size)
169
+
170
+ if world_size != total_files:
171
+ raise ValueError(
172
+ f"Expected {world_size} of '*_optim_states.pt' under '{ds_checkpoint_dir}' but found {total_files} files. "
173
+ "Possibly due to an overwrite of an old checkpoint, or a checkpoint didn't get saved by one or more processes."
174
+ )
175
+
176
+ # the groups are named differently in each stage
177
+ if zero_stage <= 2:
178
+ fp32_groups_key = SINGLE_PARTITION_OF_FP32_GROUPS
179
+ elif zero_stage == 3:
180
+ fp32_groups_key = FP32_FLAT_GROUPS
181
+ else:
182
+ raise ValueError(f"unknown zero stage {zero_stage}")
183
+
184
+ fp32_flat_groups = [state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key] for i in range(len(state_dicts))]
185
+ return zero_stage, world_size, fp32_flat_groups
186
+
187
+
188
+ def _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir, exclude_frozen_parameters):
189
+ """
190
+ Returns fp32 state_dict reconstructed from ds checkpoint
191
+
192
+ Args:
193
+ - ``ds_checkpoint_dir``: path to the deepspeed checkpoint folder (where the optimizer files are)
194
+
195
+ """
196
+ print(f"Processing zero checkpoint '{ds_checkpoint_dir}'")
197
+
198
+ optim_files = get_optim_files(ds_checkpoint_dir)
199
+ zero_stage, world_size, fp32_flat_groups = parse_optim_states(optim_files, ds_checkpoint_dir)
200
+ print(f"Detected checkpoint of type zero stage {zero_stage}, world_size: {world_size}")
201
+
202
+ model_files = get_model_state_files(ds_checkpoint_dir)
203
+
204
+ zero_model_states = parse_model_states(model_files)
205
+ print(f'Parsing checkpoint created by deepspeed=={zero_model_states[0].ds_version}')
206
+
207
+ if zero_stage <= 2:
208
+ return _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states,
209
+ exclude_frozen_parameters)
210
+ elif zero_stage == 3:
211
+ return _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states,
212
+ exclude_frozen_parameters)
213
+
214
+
215
+ def _zero2_merge_frozen_params(state_dict, zero_model_states):
216
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
217
+ return
218
+
219
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
220
+ frozen_param_fragments = zero_model_states[0].frozen_param_fragments
221
+
222
+ if debug:
223
+ num_elem = sum(s.numel() for s in frozen_param_shapes.values())
224
+ print(f'rank 0: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
225
+
226
+ wanted_params = len(frozen_param_shapes)
227
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
228
+ avail_numel = sum([p.numel() for p in frozen_param_fragments.values()])
229
+ print(f'Frozen params: Have {avail_numel} numels to process.')
230
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
231
+
232
+ total_params = 0
233
+ total_numel = 0
234
+ for name, shape in frozen_param_shapes.items():
235
+ total_params += 1
236
+ unpartitioned_numel = shape.numel()
237
+ total_numel += unpartitioned_numel
238
+
239
+ state_dict[name] = frozen_param_fragments[name]
240
+
241
+ if debug:
242
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
243
+
244
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
245
+
246
+
247
+ def _has_callable(obj, fn):
248
+ attr = getattr(obj, fn, None)
249
+ return callable(attr)
250
+
251
+
252
+ def _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
253
+ param_shapes = zero_model_states[0].param_shapes
254
+
255
+ # Reconstruction protocol:
256
+ #
257
+ # XXX: document this
258
+
259
+ if debug:
260
+ for i in range(world_size):
261
+ for j in range(len(fp32_flat_groups[0])):
262
+ print(f"{FP32_FLAT_GROUPS}[{i}][{j}].shape={fp32_flat_groups[i][j].shape}")
263
+
264
+ # XXX: memory usage doubles here (zero2)
265
+ num_param_groups = len(fp32_flat_groups[0])
266
+ merged_single_partition_of_fp32_groups = []
267
+ for i in range(num_param_groups):
268
+ merged_partitions = [sd[i] for sd in fp32_flat_groups]
269
+ full_single_fp32_vector = torch.cat(merged_partitions, 0)
270
+ merged_single_partition_of_fp32_groups.append(full_single_fp32_vector)
271
+ avail_numel = sum(
272
+ [full_single_fp32_vector.numel() for full_single_fp32_vector in merged_single_partition_of_fp32_groups])
273
+
274
+ if debug:
275
+ wanted_params = sum([len(shapes) for shapes in param_shapes])
276
+ wanted_numel = sum([sum(shape.numel() for shape in shapes.values()) for shapes in param_shapes])
277
+ # not asserting if there is a mismatch due to possible padding
278
+ print(f"Have {avail_numel} numels to process.")
279
+ print(f"Need {wanted_numel} numels in {wanted_params} params.")
280
+
281
+ # params
282
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
283
+ # out-of-core computing solution
284
+ total_numel = 0
285
+ total_params = 0
286
+ for shapes, full_single_fp32_vector in zip(param_shapes, merged_single_partition_of_fp32_groups):
287
+ offset = 0
288
+ avail_numel = full_single_fp32_vector.numel()
289
+ for name, shape in shapes.items():
290
+
291
+ unpartitioned_numel = shape.numel() if _has_callable(shape, 'numel') else math.prod(shape)
292
+ total_numel += unpartitioned_numel
293
+ total_params += 1
294
+
295
+ if debug:
296
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
297
+ state_dict[name] = full_single_fp32_vector.narrow(0, offset, unpartitioned_numel).view(shape)
298
+ offset += unpartitioned_numel
299
+
300
+ # Z2 started to align to 2*world_size to improve nccl performance. Therefore both offset and
301
+ # avail_numel can differ by anywhere between 0..2*world_size. Due to two unrelated complex
302
+ # paddings performed in the code it's almost impossible to predict the exact numbers w/o the
303
+ # live optimizer object, so we are checking that the numbers are within the right range
304
+ align_to = 2 * world_size
305
+
306
+ def zero2_align(x):
307
+ return align_to * math.ceil(x / align_to)
308
+
309
+ if debug:
310
+ print(f"original offset={offset}, avail_numel={avail_numel}")
311
+
312
+ offset = zero2_align(offset)
313
+ avail_numel = zero2_align(avail_numel)
314
+
315
+ if debug:
316
+ print(f"aligned offset={offset}, avail_numel={avail_numel}")
317
+
318
+ # Sanity check
319
+ if offset != avail_numel:
320
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
321
+
322
+ print(f"Reconstructed fp32 state dict with {total_params} params {total_numel} elements")
323
+
324
+
325
+ def _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states,
326
+ exclude_frozen_parameters):
327
+ state_dict = OrderedDict()
328
+
329
+ # buffers
330
+ buffers = zero_model_states[0].buffers
331
+ state_dict.update(buffers)
332
+ if debug:
333
+ print(f"added {len(buffers)} buffers")
334
+
335
+ if not exclude_frozen_parameters:
336
+ _zero2_merge_frozen_params(state_dict, zero_model_states)
337
+
338
+ _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
339
+
340
+ # recover shared parameters
341
+ for pair in zero_model_states[0].shared_params:
342
+ if pair[1] in state_dict:
343
+ state_dict[pair[0]] = state_dict[pair[1]]
344
+
345
+ return state_dict
346
+
347
+
348
+ def zero3_partitioned_param_info(unpartitioned_numel, world_size):
349
+ remainder = unpartitioned_numel % world_size
350
+ padding_numel = (world_size - remainder) if remainder else 0
351
+ partitioned_numel = math.ceil(unpartitioned_numel / world_size)
352
+ return partitioned_numel, padding_numel
353
+
354
+
355
+ def _zero3_merge_frozen_params(state_dict, world_size, zero_model_states):
356
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
357
+ return
358
+
359
+ if debug:
360
+ for i in range(world_size):
361
+ num_elem = sum(s.numel() for s in zero_model_states[i].frozen_param_fragments.values())
362
+ print(f'rank {i}: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
363
+
364
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
365
+ wanted_params = len(frozen_param_shapes)
366
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
367
+ avail_numel = sum([p.numel() for p in zero_model_states[0].frozen_param_fragments.values()]) * world_size
368
+ print(f'Frozen params: Have {avail_numel} numels to process.')
369
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
370
+
371
+ total_params = 0
372
+ total_numel = 0
373
+ for name, shape in zero_model_states[0].frozen_param_shapes.items():
374
+ total_params += 1
375
+ unpartitioned_numel = shape.numel()
376
+ total_numel += unpartitioned_numel
377
+
378
+ param_frags = tuple(model_state.frozen_param_fragments[name] for model_state in zero_model_states)
379
+ state_dict[name] = torch.cat(param_frags, 0).narrow(0, 0, unpartitioned_numel).view(shape)
380
+
381
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
382
+
383
+ if debug:
384
+ print(
385
+ f"Frozen params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
386
+ )
387
+
388
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
389
+
390
+
391
+ class GatheredTensor:
392
+ """
393
+ A pseudo tensor that collects partitioned weights.
394
+ It is more memory efficient when there are multiple groups.
395
+ """
396
+
397
+ def __init__(self, flat_groups, flat_groups_offset, offset, partitioned_numel, shape):
398
+ self.flat_groups = flat_groups
399
+ self.flat_groups_offset = flat_groups_offset
400
+ self.offset = offset
401
+ self.partitioned_numel = partitioned_numel
402
+ self.shape = shape
403
+ self.dtype = self.flat_groups[0][0].dtype
404
+
405
+ def contiguous(self):
406
+ """
407
+ Merge partitioned weights from flat_groups into a single tensor.
408
+ """
409
+ end_idx = self.offset + self.partitioned_numel
410
+ world_size = len(self.flat_groups)
411
+ pad_flat_param_chunks = []
412
+
413
+ for rank_i in range(world_size):
414
+ # for each rank, we need to collect weights from related group/groups
415
+ flat_groups_at_rank_i = self.flat_groups[rank_i]
416
+ start_group_id = None
417
+ end_group_id = None
418
+ for group_id in range(len(self.flat_groups_offset)):
419
+ if self.flat_groups_offset[group_id] <= self.offset < self.flat_groups_offset[group_id + 1]:
420
+ start_group_id = group_id
421
+ if self.flat_groups_offset[group_id] < end_idx <= self.flat_groups_offset[group_id + 1]:
422
+ end_group_id = group_id
423
+ break
424
+ # collect weights from related group/groups
425
+ for group_id in range(start_group_id, end_group_id + 1):
426
+ flat_tensor = flat_groups_at_rank_i[group_id]
427
+ start_offset = self.offset - self.flat_groups_offset[group_id]
428
+ end_offset = min(end_idx, self.flat_groups_offset[group_id + 1]) - self.flat_groups_offset[group_id]
429
+ pad_flat_param_chunks.append(flat_tensor[start_offset:end_offset])
430
+
431
+ # collect weights from all ranks
432
+ pad_flat_param = torch.cat(pad_flat_param_chunks, dim=0)
433
+ param = pad_flat_param[:self.shape.numel()].view(self.shape).contiguous()
434
+ return param
435
+
436
+
437
+ def _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
438
+ param_shapes = zero_model_states[0].param_shapes
439
+ avail_numel = sum([flat_group.numel() for flat_group in fp32_flat_groups[0]]) * world_size
440
+
441
+ # Reconstruction protocol: For zero3 we need to zip the partitions together at boundary of each
442
+ # param, re-consolidating each param, while dealing with padding if any
443
+
444
+ # merge list of dicts, preserving order
445
+ param_shapes = {k: v for d in param_shapes for k, v in d.items()}
446
+
447
+ if debug:
448
+ for i in range(world_size):
449
+ print(f"{FP32_FLAT_GROUPS}[{i}].shape={fp32_flat_groups[i].shape}")
450
+
451
+ wanted_params = len(param_shapes)
452
+ wanted_numel = sum(shape.numel() for shape in param_shapes.values())
453
+ # not asserting if there is a mismatch due to possible padding
454
+ avail_numel = fp32_flat_groups[0].numel() * world_size
455
+ print(f"Trainable params: Have {avail_numel} numels to process.")
456
+ print(f"Trainable params: Need {wanted_numel} numels in {wanted_params} params.")
457
+
458
+ # params
459
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
460
+ # out-of-core computing solution
461
+ offset = 0
462
+ total_numel = 0
463
+ total_params = 0
464
+ flat_groups_offset = [0] + list(np.cumsum([flat_tensor.numel() for flat_tensor in fp32_flat_groups[0]]))
465
+ for name, shape in tqdm(param_shapes.items(), desc='Gathering sharded weights'):
466
+ unpartitioned_numel = shape.numel()
467
+ total_numel += unpartitioned_numel
468
+ total_params += 1
469
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
470
+
471
+ if debug:
472
+ print(
473
+ f"Trainable params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
474
+ )
475
+
476
+ # memory efficient tensor
477
+ tensor = GatheredTensor(fp32_flat_groups, flat_groups_offset, offset, partitioned_numel, shape)
478
+ state_dict[name] = tensor
479
+ offset += partitioned_numel
480
+
481
+ offset *= world_size
482
+
483
+ # Sanity check
484
+ if offset != avail_numel:
485
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
486
+
487
+ print(f"Reconstructed Trainable fp32 state dict with {total_params} params {total_numel} elements")
488
+
489
+
490
+ def _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states,
491
+ exclude_frozen_parameters):
492
+ state_dict = OrderedDict()
493
+
494
+ # buffers
495
+ buffers = zero_model_states[0].buffers
496
+ state_dict.update(buffers)
497
+ if debug:
498
+ print(f"added {len(buffers)} buffers")
499
+
500
+ if not exclude_frozen_parameters:
501
+ _zero3_merge_frozen_params(state_dict, world_size, zero_model_states)
502
+
503
+ _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
504
+
505
+ # recover shared parameters
506
+ for pair in zero_model_states[0].shared_params:
507
+ if pair[1] in state_dict:
508
+ state_dict[pair[0]] = state_dict[pair[1]]
509
+
510
+ return state_dict
511
+
512
+
513
+ def to_torch_tensor(state_dict, return_empty_tensor=False):
514
+ """
515
+ Convert state_dict of GatheredTensor to torch tensor
516
+ """
517
+ torch_state_dict = {}
518
+ converted_tensors = {}
519
+ for name, tensor in state_dict.items():
520
+ tensor_id = id(tensor)
521
+ if tensor_id in converted_tensors: # shared tensors
522
+ shared_tensor = torch_state_dict[converted_tensors[tensor_id]]
523
+ torch_state_dict[name] = shared_tensor
524
+ else:
525
+ converted_tensors[tensor_id] = name
526
+ if return_empty_tensor:
527
+ torch_state_dict[name] = torch.empty(tensor.shape, dtype=tensor.dtype)
528
+ else:
529
+ torch_state_dict[name] = tensor.contiguous()
530
+ return torch_state_dict
531
+
532
+
533
+ def get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir,
534
+ tag=None,
535
+ exclude_frozen_parameters=False,
536
+ lazy_mode=False):
537
+ """
538
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated state_dict that can be loaded with
539
+ ``load_state_dict()`` and used for training without DeepSpeed or shared with others, for example
540
+ via a model hub.
541
+
542
+ Args:
543
+ - ``checkpoint_dir``: path to the desired checkpoint folder
544
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in 'latest' file. e.g., ``global_step14``
545
+ - ``exclude_frozen_parameters``: exclude frozen parameters
546
+ - ``lazy_mode``: get state_dict in lazy mode. It returns a dict of pesduo tensor instead of torch tensor, which is more memory efficient.
547
+ Convert the pesduo tensor to torch tensor by ``.contiguous()``
548
+
549
+ Returns:
550
+ - pytorch ``state_dict``
551
+
552
+ A typical usage might be ::
553
+
554
+ from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint
555
+ # do the training and checkpoint saving
556
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # already on cpu
557
+ model = model.cpu() # move to cpu
558
+ model.load_state_dict(state_dict)
559
+ # submit to model hub or save the model to share with others
560
+
561
+ In this example the ``model`` will no longer be usable in the deepspeed context of the same
562
+ application. i.e. you will need to re-initialize the deepspeed engine, since
563
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
564
+
565
+ If you want it all done for you, use ``load_state_dict_from_zero_checkpoint`` instead.
566
+
567
+ Note: the above usage may not work if your application doesn't have sufficient free CPU memory.
568
+ You may need to use the offline approach using the ``zero_to_fp32.py`` script that is saved with
569
+ the checkpoint. Or you can load state_dict in lazy mode ::
570
+
571
+ from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint
572
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, lazy_mode=True) # not on cpu
573
+ for name, lazy_tensor in state_dict.item():
574
+ tensor = lazy_tensor.contiguous() # to cpu
575
+ print(name, tensor)
576
+ # del tensor to release memory if it no longer in use
577
+ """
578
+ if tag is None:
579
+ latest_path = os.path.join(checkpoint_dir, 'latest')
580
+ if os.path.isfile(latest_path):
581
+ with open(latest_path, 'r') as fd:
582
+ tag = fd.read().strip()
583
+ else:
584
+ raise ValueError(f"Unable to find 'latest' file at {latest_path}")
585
+
586
+ ds_checkpoint_dir = os.path.join(checkpoint_dir, tag)
587
+
588
+ if not os.path.isdir(ds_checkpoint_dir):
589
+ raise FileNotFoundError(f"Directory '{ds_checkpoint_dir}' doesn't exist")
590
+
591
+ state_dict = _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir, exclude_frozen_parameters)
592
+ if lazy_mode:
593
+ return state_dict
594
+ else:
595
+ return to_torch_tensor(state_dict)
596
+
597
+
598
+ def convert_zero_checkpoint_to_fp32_state_dict(checkpoint_dir,
599
+ output_dir,
600
+ max_shard_size="5GB",
601
+ safe_serialization=False,
602
+ tag=None,
603
+ exclude_frozen_parameters=False):
604
+ """
605
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict`` file that can be
606
+ loaded with ``torch.load(file)`` + ``load_state_dict()`` and used for training without DeepSpeed.
607
+
608
+ Args:
609
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
610
+ - ``output_dir``: directory to the pytorch fp32 state_dict output files
611
+ - ``max_shard_size``: the maximum size for a checkpoint before being sharded, default value is 5GB
612
+ - ``safe_serialization``: whether to save the model using `safetensors` or the traditional PyTorch way (that uses `pickle`).
613
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in the file named ``latest`` in the checkpoint folder, e.g., ``global_step14``
614
+ - ``exclude_frozen_parameters``: exclude frozen parameters
615
+ """
616
+
617
+ # Dependency pre-check
618
+ if safe_serialization:
619
+ try:
620
+ from safetensors.torch import save_file
621
+ except ImportError:
622
+ print('If you want to use `safe_serialization`, please `pip install safetensors`')
623
+ raise
624
+ if max_shard_size is not None:
625
+ try:
626
+ from huggingface_hub import split_torch_state_dict_into_shards
627
+ except ImportError:
628
+ print('If you want to use `max_shard_size`, please `pip install huggingface_hub`')
629
+ raise
630
+
631
+ # Convert zero checkpoint to state_dict
632
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir,
633
+ tag,
634
+ exclude_frozen_parameters,
635
+ lazy_mode=True)
636
+
637
+ # Shard the model if it is too big.
638
+ weights_name = "model.safetensors" if safe_serialization else "pytorch_model.bin"
639
+ if max_shard_size is not None:
640
+ filename_pattern = weights_name.replace(".bin", "{suffix}.bin").replace(".safetensors", "{suffix}.safetensors")
641
+ # an memory-efficient approach for sharding
642
+ empty_state_dict = to_torch_tensor(state_dict, return_empty_tensor=True)
643
+ state_dict_split = split_torch_state_dict_into_shards(empty_state_dict,
644
+ filename_pattern=filename_pattern,
645
+ max_shard_size=max_shard_size)
646
+ else:
647
+ from collections import namedtuple
648
+ StateDictSplit = namedtuple("StateDictSplit", ["is_sharded", "filename_to_tensors"])
649
+ state_dict_split = StateDictSplit(is_sharded=False,
650
+ filename_to_tensors={weights_name: list(state_dict.keys())})
651
+
652
+ # Save the model by shard
653
+ os.makedirs(output_dir, exist_ok=True)
654
+ filename_to_tensors = state_dict_split.filename_to_tensors.items()
655
+ for shard_file, tensors in tqdm(filename_to_tensors, desc="Saving checkpoint shards"):
656
+ shard_state_dict = {tensor_name: state_dict[tensor_name] for tensor_name in tensors}
657
+ shard_state_dict = to_torch_tensor(shard_state_dict)
658
+ output_path = os.path.join(output_dir, shard_file)
659
+ if safe_serialization:
660
+ save_file(shard_state_dict, output_path, metadata={"format": "pt"})
661
+ else:
662
+ torch.save(shard_state_dict, output_path)
663
+ # release the memory of current shard
664
+ for tensor_name in list(shard_state_dict.keys()):
665
+ del state_dict[tensor_name]
666
+ del shard_state_dict[tensor_name]
667
+ del shard_state_dict
668
+ gc.collect()
669
+
670
+ # Save index if sharded
671
+ if state_dict_split.is_sharded:
672
+ index = {
673
+ "metadata": state_dict_split.metadata,
674
+ "weight_map": state_dict_split.tensor_to_filename,
675
+ }
676
+ save_index_file = "model.safetensors.index.json" if safe_serialization else "pytorch_model.bin.index.json"
677
+ save_index_file = os.path.join(output_dir, save_index_file)
678
+ with open(save_index_file, "w", encoding="utf-8") as f:
679
+ content = json.dumps(index, indent=2, sort_keys=True) + "\n"
680
+ f.write(content)
681
+
682
+
683
+ def load_state_dict_from_zero_checkpoint(model, checkpoint_dir, tag=None):
684
+ """
685
+ 1. Put the provided model to cpu
686
+ 2. Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict``
687
+ 3. Load it into the provided model
688
+
689
+ Args:
690
+ - ``model``: the model object to update
691
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
692
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in the file named ``latest`` in the checkpoint folder, e.g., ``global_step14``
693
+
694
+ Returns:
695
+ - ``model`: modified model
696
+
697
+ Make sure you have plenty of CPU memory available before you call this function. If you don't
698
+ have enough use the ``zero_to_fp32.py`` utility to do the conversion. You will find it
699
+ conveniently placed for you in the checkpoint folder.
700
+
701
+ A typical usage might be ::
702
+
703
+ from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint
704
+ model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir)
705
+ # submit to model hub or save the model to share with others
706
+
707
+ Note, that once this was run, the ``model`` will no longer be usable in the deepspeed context
708
+ of the same application. i.e. you will need to re-initialize the deepspeed engine, since
709
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
710
+
711
+ """
712
+ logger.info(f"Extracting fp32 weights")
713
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
714
+
715
+ logger.info(f"Overwriting model with fp32 weights")
716
+ model = model.cpu()
717
+ model.load_state_dict(state_dict, strict=False)
718
+
719
+ return model
720
+
721
+
722
+ if __name__ == "__main__":
723
+ parser = argparse.ArgumentParser()
724
+ parser.add_argument("checkpoint_dir",
725
+ type=str,
726
+ help="path to the desired checkpoint folder, e.g., path/checkpoint-12")
727
+ parser.add_argument("output_dir",
728
+ type=str,
729
+ help="directory to the pytorch fp32 state_dict output files"
730
+ "(e.g. path/checkpoint-12-output/)")
731
+ parser.add_argument(
732
+ "--max_shard_size",
733
+ type=str,
734
+ default="5GB",
735
+ help="The maximum size for a checkpoint before being sharded. Checkpoints shard will then be each of size"
736
+ "lower than this size. If expressed as a string, needs to be digits followed by a unit (like `5MB`"
737
+ "We default it to 5GB in order for models to be able to run easily on free-tier google colab instances"
738
+ "without CPU OOM issues.")
739
+ parser.add_argument(
740
+ "--safe_serialization",
741
+ default=False,
742
+ action='store_true',
743
+ help="Whether to save the model using `safetensors` or the traditional PyTorch way (that uses `pickle`).")
744
+ parser.add_argument("-t",
745
+ "--tag",
746
+ type=str,
747
+ default=None,
748
+ help="checkpoint tag used as a unique identifier for checkpoint. e.g., global_step1")
749
+ parser.add_argument("--exclude_frozen_parameters", action='store_true', help="exclude frozen parameters")
750
+ parser.add_argument("-d", "--debug", action='store_true', help="enable debug")
751
+ args = parser.parse_args()
752
+
753
+ debug = args.debug
754
+
755
+ convert_zero_checkpoint_to_fp32_state_dict(args.checkpoint_dir,
756
+ args.output_dir,
757
+ max_shard_size=args.max_shard_size,
758
+ safe_serialization=args.safe_serialization,
759
+ tag=args.tag,
760
+ exclude_frozen_parameters=args.exclude_frozen_parameters)