-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProgram.cs
320 lines (260 loc) · 11 KB
/
Program.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using GGMLSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Magika
{
internal class Program
{
static string[] Labels ={
"ai", "apk", "appleplist", "asm", "asp",
"batch", "bmp", "bzip", "c", "cab",
"cat", "chm", "coff", "crx", "cs",
"css", "csv", "deb", "dex", "dmg",
"doc", "docx", "elf", "emf", "eml",
"epub", "flac", "gif", "go", "gzip",
"hlp", "html", "ico", "ini", "internetshortcut",
"iso", "jar", "java", "javabytecode", "javascript",
"jpeg", "json", "latex", "lisp", "lnk",
"m3u", "macho", "makefile", "markdown", "mht",
"mp3", "mp4", "mscompress", "msi", "mum",
"odex", "odp", "ods", "odt", "ogg",
"outlook", "pcap", "pdf", "pebin", "pem",
"perl", "php", "png", "postscript", "powershell",
"ppt", "pptx", "python", "pythonbytecode", "rar",
"rdf", "rpm", "rst", "rtf", "ruby",
"rust", "scala", "sevenzip", "shell", "smali",
"sql", "squashfs", "svg", "swf", "symlinktext",
"tar", "tga", "tiff", "torrent", "ttf",
"txt", "unknown", "vba", "wav", "webm",
"webp", "winregistry", "wmf", "xar", "xls",
"xlsb", "xlsx", "xml", "xpi", "xz",
"yaml", "zip", "zlibstream"
};
private class Hparams
{
public int blockSize = 4096;
public int begSize = 512;
public int midSize = 512;
public int endSize = 512;
public int minFileSizeForDl = 16;
public int labelCount = 113;
public float normEps = 0.001f;
public int paddingToken = 256;
};
private class MagikaModel
{
~MagikaModel()
{
backendBuffer.Free();
backend.Free();
context.Free();
}
public Hparams hparams = new Hparams();
public SafeGGmlTensor denseWeight;
public SafeGGmlTensor denseBias;
public SafeGGmlTensor layerNormGamma;
public SafeGGmlTensor layerNormBeta;
public SafeGGmlTensor dense1Weight;
public SafeGGmlTensor dense1Bias;
public SafeGGmlTensor dense2Weight;
public SafeGGmlTensor dense2Bias;
public SafeGGmlTensor layerNorm1Gamma;
public SafeGGmlTensor layerNorm1Beta;
public SafeGGmlTensor targetLabelWeight;
public SafeGGmlTensor targetLabelBias;
public SafeGGmlBackend backend = SafeGGmlBackend.CpuInit();
public SafeGGmlBackendBuffer backendBuffer;
public SafeGGmlContext context = new SafeGGmlContext(IntPtr.Zero);
};
private static SafeGGmlTensor CheckedGetTensor(SafeGGmlContext ctx, string name)
{
SafeGGmlTensor tensor = ctx.GetTensor(name);
if (tensor.IsInvalid)
{
throw new ArgumentNullException($"tensor {name} not found");
}
return tensor;
}
private static MagikaModel LoadModel(string fname)
{
MagikaModel model = new MagikaModel();
SafeGGufContext ggufContext = SafeGGufContext.InitFromFile(@"./Assets/magika.gguf", model.context, true);
model.backend = SafeGGmlBackend.CpuInit(); // init device 0
if (!ggufContext.IsHeaderMagicMatch)
{
throw new FileLoadException("gguf_init_from_file failed");
}
model.backendBuffer = model.context.BackendAllocContextTensors(model.backend);
if (model.backendBuffer.IsInvalid)
{
ggufContext.Free();
throw new Exception("ggml_backend_alloc_ctx_tensors failed");
}
try
{
model.denseWeight = CheckedGetTensor(model.context, "dense/kernel:0");
model.denseBias = CheckedGetTensor(model.context, "dense/bias:0");
model.layerNormGamma = CheckedGetTensor(model.context, "layer_normalization/gamma:0");
model.layerNormBeta = CheckedGetTensor(model.context, "layer_normalization/beta:0");
model.dense1Weight = CheckedGetTensor(model.context, "dense_1/kernel:0");
model.dense1Bias = CheckedGetTensor(model.context, "dense_1/bias:0");
model.dense2Weight = CheckedGetTensor(model.context, "dense_2/kernel:0");
model.dense2Bias = CheckedGetTensor(model.context, "dense_2/bias:0");
model.layerNorm1Gamma = CheckedGetTensor(model.context, "layer_normalization_1/gamma:0");
model.layerNorm1Beta = CheckedGetTensor(model.context, "layer_normalization_1/beta:0");
model.targetLabelWeight = CheckedGetTensor(model.context, "target_label/kernel:0");
model.targetLabelBias = CheckedGetTensor(model.context, "target_label/bias:0");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
ggufContext.Free();
return null;
}
using (FileStream fs = new FileStream(fname, FileMode.Open, FileAccess.Read))
{
for (ulong i = 0; i < ggufContext.TensorsCount; i++)
{
string name = ggufContext.GetTensorName((int)i);
SafeGGmlTensor tensor = model.context.GetTensor(name);
ulong offs = ggufContext.GetDataOffset() + ggufContext.GetTensorOffset((int)i);
byte[] buf = new byte[(long)tensor.ElementsSize * tensor.ElementsCount];
fs.Seek((long)offs, SeekOrigin.Begin);
int bytesRead = fs.Read(buf, 0, buf.Length);
tensor.SetBackend(buf);
}
}
//ggufContext.Free();
return model;
}
private static SafeGGmlGraph MagikaGraph(MagikaModel model)
{
Hparams hparams = model.hparams;
SafeGGmlContext ggmlContext = new SafeGGmlContext(IntPtr.Zero, NoAllocateMemory: true);
SafeGGmlGraph graph = ggmlContext.NewGraph();
SafeGGmlTensor input = ggmlContext.NewTensor3d(Structs.GGmlType.GGML_TYPE_F32, 257, 1536, 1); // one-hot
input.Name = "input";
input.SetInput();
SafeGGmlTensor cur;
// dense
cur = ggmlContext.MulMat(model.denseWeight, input);
cur = ggmlContext.Add(cur, model.denseBias); // [128, 1536, n_files]
cur = ggmlContext.Gelu(cur);
// reshape
cur = ggmlContext.Reshape3d(cur, 512, 384, 1); // [384, 512, n_files]
cur = ggmlContext.Cont(ggmlContext.Transpose(cur));
// layer normalization
cur = ggmlContext.Norm(cur, hparams.normEps);
cur = ggmlContext.Mul(cur, model.layerNormGamma); // [384, 512, n_files]
cur = ggmlContext.Add(cur, model.layerNormBeta); // [384, 512, n_files]
// dense_1
cur = ggmlContext.Cont(ggmlContext.Transpose(cur));
cur = ggmlContext.MulMat(model.dense1Weight, cur);
cur = ggmlContext.Add(cur, model.dense1Bias); // [256, 384, n_files]
cur = ggmlContext.Gelu(cur);
// dense_2
cur = ggmlContext.MulMat(model.dense2Weight, cur);
cur = ggmlContext.Add(cur, model.dense2Bias); // [256, 384, n_files]
cur = ggmlContext.Gelu(cur);
// global_max_pooling1d
cur = ggmlContext.Cont(ggmlContext.Transpose(cur)); // [384, 256, n_files]
cur = ggmlContext.Pool1d(cur, Structs.GGmlOpPool.GGML_OP_POOL_MAX, 384, 384, 0); // [1, 256, n_files]
cur = ggmlContext.Reshape2d(cur, 256, 1); // [256, n_files]
// layer normalization 1
cur = ggmlContext.Norm(cur, hparams.normEps);
cur = ggmlContext.Mul(cur, model.layerNorm1Gamma); // [256, n_files]
cur = ggmlContext.Add(cur, model.layerNorm1Beta); // [256, n_files]
// target_label
cur = ggmlContext.MulMat(model.targetLabelWeight, cur);
cur = ggmlContext.Add(cur, model.targetLabelBias); // [labelCount, n_files]
cur = ggmlContext.SoftMax(cur); // [labelCount, n_files]
cur.Name = "targetLabelProbs";
cur.SetOutput();
graph.BuildForwardExpend(cur);
return graph;
}
private static float[] Eval(MagikaModel model, string fname)
{
Hparams hparams = model.hparams;
SafeGGmlGraphAllocr alloc = new SafeGGmlGraphAllocr(model.backend.GetDefaultBufferType());
SafeGGmlGraph graph = MagikaGraph(model);
if (!graph.GraphAllocate(alloc))
{
throw new Exception("ggml_gallocr_alloc_graph() failed");
}
SafeGGmlTensor input = graph.GetTensor("input");
var buf = new List<int>(Enumerable.Repeat(hparams.paddingToken, 1536));
using (FileStream fileStream = new FileStream(fname, FileMode.Open, FileAccess.Read))
{
var fsize = fileStream.Length;
long size = Math.Max(Math.Max(hparams.midSize, hparams.endSize), hparams.begSize);
byte[] read_buf = new byte[size];
// Read beg
int bytesToRead = fileStream.Read(read_buf, 0, hparams.begSize);
for (int j = 0; j < bytesToRead; j++)
{
buf[j] = read_buf[j];
}
// Read mid
var midOffs = Math.Max(0, (int)(fsize - hparams.midSize) / 2);
fileStream.Seek(midOffs, SeekOrigin.Begin);
bytesToRead = fileStream.Read(read_buf, 0, hparams.midSize);
for (int j = 0; j < bytesToRead; j++)
{
// pad at both ends
int mid_idx = hparams.begSize + (hparams.midSize / 2) - bytesToRead / 2 + j;
buf[mid_idx] = read_buf[j];
}
// Read end
var endOffs = Math.Max(0, fsize - hparams.endSize);
fileStream.Seek(endOffs, SeekOrigin.Begin);
bytesToRead = fileStream.Read(read_buf, 0, hparams.endSize);
for (int j = 0; j < bytesToRead; j++)
{
// pad at the beginning
int end_idx = hparams.begSize + hparams.midSize + hparams.endSize - bytesToRead + j;
buf[end_idx] = read_buf[j];
}
}
var inpBytes = hparams.begSize + hparams.midSize + hparams.endSize;
var oneHot = new float[257 * inpBytes];
for (int j = 0; j < inpBytes; j++)
{
oneHot[257 * j + buf[j]] = 1.0f;
}
input.SetBackend(oneHot);
if (graph.BackendCompute(model.backend) != Structs.GGmlStatus.GGML_STATUS_SUCCESS)
{
throw new Exception("ggml_backend_graph_compute() failed");
}
SafeGGmlTensor targetLabelProbs = graph.GetTensor("targetLabelProbs");
byte[] bytes = targetLabelProbs.GetBackend();
float[] probs = DataConverter.ConvertToFloats(bytes);
return probs;
}
struct result
{
public string label;
public float score;
}
static void Main(string[] args)
{
MagikaModel model = LoadModel(@".\Assets\magika.gguf");
Console.WriteLine("Loaded model");
float[] result = Eval(model, @".\Assets\test");
List<result> results = new List<result>();
for (int i = 0; i < result.Length; i++)
{
results.Add(new result { label = Labels[i], score = result[i] });
}
results.Sort((a, b) => b.score.CompareTo(a.score));
for (int i = 0; i < 5; i++)
{
Console.WriteLine("{0}: {1}", results[i].label, results[i].score);
}
Console.ReadKey();
}
}
}