-
Notifications
You must be signed in to change notification settings - Fork 7
/
PickleLoader.cs
302 lines (276 loc) · 8.13 KB
/
PickleLoader.cs
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
using GGMLSharp;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
namespace ModelLoader
{
public class PickleLoader : IModelLoader
{
private ZipArchive zip;
private ReadOnlyCollection<ZipArchiveEntry> entries;
public List<Tensor> ReadTensorsInfoFromFile(string fileName)
{
List<Tensor> tensors = new List<Tensor>();
zip = ZipFile.OpenRead(fileName);
entries = zip.Entries;
ZipArchiveEntry headerEntry = entries.First(e => e.Name == "data.pkl");
byte[] headerBytes = new byte[headerEntry.Length];
// Header is always small enough to fit in memory, so we can read it all at once
using (Stream stream = headerEntry.Open())
{
stream.Read(headerBytes, 0, headerBytes.Length);
}
if (headerBytes[0] != 0x80 || headerBytes[1] != 0x02)
{
throw new ArgumentException("Not a valid pickle file");
}
int index = 1;
bool finished = false;
bool readStrides = false;
bool binPersid = false;
Tensor tensor = new Tensor() { FileName = fileName, Offset = { 0 } };
int deepth = 0;
Dictionary<int, string> BinPut = new Dictionary<int, string>();
while (index < headerBytes.Length && !finished)
{
byte opcode = headerBytes[index];
switch (opcode)
{
case (byte)'}': // EMPTY_DICT = b'}' # push empty dict
break;
case (byte)']': // EMPTY_LIST = b']' # push empty list
break;
// skip unused sections
case (byte)'h': // BINGET = b'h' # " " " " " " ; " " 1-byte arg
{
int id = headerBytes[index + 1];
BinPut.TryGetValue(id, out string precision);
if (precision != null)
{
if (precision.Contains("FloatStorage"))
{
tensor.Type = Structs.GGmlType.GGML_TYPE_F32;
}
else if (precision.Contains("HalfStorage"))
{
tensor.Type = Structs.GGmlType.GGML_TYPE_F16;
}
else if (precision.Contains("BFloat16Storage"))
{
tensor.Type = Structs.GGmlType.GGML_TYPE_BF16;
}
}
index++;
break;
}
case (byte)'q': // BINPUT = b'q' # " " " " " ; " " 1-byte arg
{
index++;
break;
}
case (byte)'Q': // BINPERSID = b'Q' # " " " ; " " " " stack
binPersid = true;
break;
case (byte)'r': // LONG_BINPUT = b'r' # " " " " " ; " " 4-byte arg
index += 4;
break;
case 0x95: // FRAME = b'\x95' # indicate the beginning of a new frame
index += 8;
break;
case 0x94: // MEMOIZE = b'\x94' # store top of the stack in memo
break;
case (byte)'(': // MARK = b'(' # push special markobject on stack
deepth++;
break;
case (byte)'K': // BININT1 = b'K' # push 1-byte unsigned int
{
int value = headerBytes[index + 1];
index++;
if (deepth > 1 && value != 0 && binPersid)
{
if (readStrides)
{
//tensor.Stride.Add((ulong)value);
tensor.Stride.Add((ulong)value);
}
else
{
tensor.Shape.Add(value);
}
}
}
break;
case (byte)'M': // BININT2 = b'M' # push 2-byte unsigned int
{
UInt16 value = BitConverter.ToUInt16(headerBytes, index + 1);
index += 2;
if (deepth > 1 && value != 0 && binPersid)
{
if (readStrides)
{
tensor.Stride.Add(value);
}
else
{
tensor.Shape.Add(value);
}
}
}
break;
case (byte)'J': // BININT = b'J' # push four-byte signed int
{
int value = BitConverter.ToInt32(headerBytes, index + 1);
//int value = headerBytes[index + 4] << 24 + headerBytes[index + 3] << 16 + headerBytes[index + 2] << 8 + headerBytes[index + 1];
index += 4;
if (deepth > 1 && value != 0 && binPersid)
{
if (readStrides)
{
tensor.Stride.Add((ulong)value);
}
else
{
tensor.Shape.Add(value);
}
}
}
break;
case (byte)'X': // BINUNICODE = b'X' # " " " ; counted UTF-8 string argument
{
int length = headerBytes[index + 1];
int start = index + 5;
byte module = headerBytes[index + 1];
string name = System.Text.Encoding.UTF8.GetString(headerBytes, start, length);
index = index + 4 + length;
if (deepth == 1)
{
tensor.Name = name;
}
else if (deepth == 3)
{
if ("cpu" != name && !name.Contains("cuda"))
{
tensor.DataNameInZipFile = name;
}
}
}
break;
case 0x8C: // SHORT_BINUNICODE = b'\x8c' # push short string; UTF-8 length < 256 bytes
{
}
break;
case (byte)'c': // GLOBAL = b'c' # push self.find_class(modname, name); 2 string args
{
int start = index + 1;
while (headerBytes[index + 1] != (byte)'q')
{
index++;
}
int length = index - start + 1;
string global = System.Text.Encoding.UTF8.GetString(headerBytes, start, length);
// precision is stored in the global variable
// next tensor will read the precision
// so we can set the Type here
BinPut.Add(headerBytes[index + 2], global);
if (global.Contains("FloatStorage"))
{
tensor.Type = Structs.GGmlType.GGML_TYPE_F32;
}
else if (global.Contains("HalfStorage"))
{
tensor.Type = Structs.GGmlType.GGML_TYPE_F16;
}
else if (global.Contains("BFloat16Storage"))
{
tensor.Type = Structs.GGmlType.GGML_TYPE_BF16;
}
break;
}
case 0x86: // TUPLE2 = b'\x86' # build 2-tuple from two topmost stack items
{
if (binPersid)
{
readStrides = true;
}
break;
}
case 0x85: // TUPLE1 = b'\x85' # build 1-tuple from stack top
if (binPersid)
{
readStrides = true;
}
break;
case (byte)'t': // TUPLE = b't' # build tuple from topmost stack items
deepth--;
if (binPersid)
{
readStrides = true;
}
break;
case (byte)'R': // REDUCE = b'R' # apply callable to argtuple, both on stack
if (deepth == 1)
{
if (tensor.Name.Contains("metadata"))
{
break;
}
if (string.IsNullOrEmpty(tensor.DataNameInZipFile))
{
tensor.DataNameInZipFile = tensors.Last().DataNameInZipFile;
tensor.Offset = new List<ulong> { (ulong)tensor.Shape[0] * Common.GetGGmlTypeSize(tensor.Type) };
tensor.Shape.RemoveAt(0);
//tensor.offset = tensors.Last().
}
tensors.Add(tensor);
tensor = new Tensor() { FileName = fileName, Offset = { 0 } };
readStrides = false;
binPersid = false;
}
break;
case (byte)'.': // STOP = b'.' # every pickle ends with STOP
finished = true;
break;
default:
break;
}
index++;
}
Tensor metaTensor = tensors.Find(x => x.Name.Contains("_metadata"));
if (metaTensor != null)
{
tensors.Remove(metaTensor);
}
return tensors;
}
public byte[] ReadByteFromFile(Tensor tensor)
{
if (entries is null)
{
throw new ArgumentNullException(nameof(entries));
}
ZipArchiveEntry dataEntry = entries.First(e => e.Name == tensor.DataNameInZipFile);
long i = 1;
foreach (var ne in tensor.Shape)
{
i *= ne;
}
ulong length = Common.GetGGmlTypeSize(tensor.Type) * (ulong)i;
byte[] data = new byte[dataEntry.Length];
using (Stream stream = dataEntry.Open())
{
stream.Read(data, 0, data.Length);
}
//data = data.Take(new Range((int)tensor.Offset[0], (int)(tensor.Offset[0] + length))).ToArray();
byte[] result = new byte[length];
for (int j = 0; j < (int)length; j++)
{
result[j] = data[j + (int)tensor.Offset[0]];
}
return result;
//return data;
}
}
}