This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtension.cs
308 lines (271 loc) · 9.46 KB
/
Extension.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
using Phi.Module;
using Phi.Pipeline;
using System.Text;
using TorchSharp;
using static TorchSharp.torch;
public static class Extension
{
public static long GetSizeInBytes(this nn.Module model)
{
var state_dict = model.state_dict();
long size = 0;
foreach (var (_, value) in state_dict)
{
size += value.numel() * value.element_size();
}
return size;
}
public static Dictionary<string, long> GetSizeForEachDynamicLayerInBytes(this nn.Module model)
{
var state_dict = model.named_children();
if (state_dict.Count() == 0)
{
return new();
}
else
{
var dict = new Dictionary<string, long>();
foreach (var (key, value) in state_dict)
{
if (value is IDynamicLoadModule)
{
dict[key] = value.GetSizeInBytes();
}
else
{
var subDict = value.GetSizeForEachDynamicLayerInBytes();
foreach (var (subKey, subValue) in subDict)
{
dict[key + "." + subKey] = subValue;
}
}
}
return dict;
}
}
public static T ToDynamicLoadingModel<T>(
this T model,
Dictionary<string, string> deviceMap,
string targetDevice)
where T : nn.Module
{
if (deviceMap.Count == 0)
{
model.to(new Device(targetDevice));
return model;
}
//var dynamicModules = model.named_modules().Where(x => x.module is IDynamicLoadModule).Select(x => x.name).ToList();
// for each module in the model, update device if it is IDyanmicLoadModule
foreach(var (key, value) in model.named_children())
{
if (value is IDynamicLoadModule dynamicModule)
{
var device = deviceMap[key];
if (device != targetDevice)
{
dynamicModule.LoadToDeviceFunc = (nn.Module module) =>
{
module.to(new Device(targetDevice));
};
dynamicModule.UnloadFromDeviceFunc = (nn.Module module) =>
{
module.to(new Device(device));
};
}
value.to(new Device(device));
}
else
{
var childrenDeviceMap = deviceMap.Where(x => x.Key.StartsWith($"{key}.")).ToDictionary(x => x.Key.Substring($"{key}.".Length), x => x.Value);
value.ToDynamicLoadingModel(childrenDeviceMap, targetDevice);
}
//if (value is IDynamicLoadModule module)
//{
// value.to(new Device(deviceMap[key]));
// if (deviceMap[key] != targetDevice)
// {
// module.LoadToDeviceFunc = (nn.Module module) =>
// {
// module.to(new Device(targetDevice));
// };
// module.UnloadFromDeviceFunc = (nn.Module module) =>
// {
// module.to(new Device(deviceMap[key]));
// };
// }
//}
//else
//{
// // check if the module is any sub module of IDynamicLoadModule
// if (dynamicModules.Any(x => key.StartsWith(x)))
// {
// continue;
// }
// value.to(targetDevice);
//}
}
return model;
}
/// <summary>
/// Infer the device map for each layer in the model.
/// The device map is a dictionary where the key is the device id (e.g. "cuda:0") and the value is the memory size in bytes of the device.
/// When infering the device map, each layer in the model will be placed on the device in the order of the devices list.
/// </summary>
/// <param name="model"></param>
/// <param name="devices">a list of device ids (e.g. ["cuda:0", "cpu", "disk"])</param>
/// <param name="deviceSizeMapInByte">a map where the key is the device id (e.g. "cuda:0") and the value is the memory size in bytes of the device</param>
/// <returns></returns>
public static Dictionary<string, string> InferDeviceMapForEachLayer(
this nn.Module model,
string[] devices,
Dictionary<string, long> deviceSizeMapInByte)
{
var layerSizeMap = model.GetSizeForEachDynamicLayerInBytes();
var sizeToRemainOnEachDevice = 2 * layerSizeMap.Max(x => x.Value);
var deviceMap = new Dictionary<string, string>();
foreach(var device in devices)
{
long size = deviceSizeMapInByte[device];
var remainingLayerSizeMap = layerSizeMap.Where(x => !deviceMap.ContainsKey(x.Key)).ToDictionary(x => x.Key, x => x.Value);
// larger layer fit first
foreach (var (key, value) in remainingLayerSizeMap.OrderByDescending(x => x.Value))
{
if (size >= value)
{
deviceMap[key] = device;
size -= value;
}
if (size < sizeToRemainOnEachDevice)
{
break;
}
}
}
return deviceMap;
}
public static string Generate(
this CasualLMPipeline pipeline,
string prompt,
int maxLen = 128,
float temperature = 0.7f,
float topP = 0.9f,
string[]? stopSequences = null,
string device = "cpu",
bool bos = true,
bool eos = false,
bool echo = false)
{
using var __ = NewDisposeScope();
var inputIds = pipeline.Tokenizer.Encode(prompt, bos, eos);
var inputTensor = torch.tensor(inputIds.ToArray(), dtype: ScalarType.Int64, device: device).unsqueeze(0);
var attentionMask = torch.ones_like(inputTensor);
var stopTokenIds = stopSequences == null ? [[ pipeline.Tokenizer.EosId ]] : stopSequences.Select(x => pipeline.Tokenizer.Encode(x, false, false)).ToArray();
(var token, var _) = pipeline.Generate(inputTensor, attentionMask, temperature: temperature, maxLen: maxLen, topP: topP, stopTokenSequence: stopTokenIds, echo: echo);
var tokenIds = token[0].to_type(ScalarType.Int32).data<int>().ToArray();
var output = pipeline.Tokenizer.Decode(tokenIds);
return output;
}
public static string Peek(this Tensor tensor, string id, int n = 10)
{
var device = tensor.device;
var dtype = tensor.dtype;
// if type is fp16, convert to fp32
if (tensor.dtype == ScalarType.Float16)
{
tensor = tensor.to_type(ScalarType.Float32);
}
tensor = tensor.cpu();
var shapeString = string.Join(',', tensor.shape);
var tensor_1d = tensor.reshape(-1);
var tensor_index = torch.arange(tensor_1d.shape[0], dtype: ScalarType.Float32).to(tensor_1d.device).sqrt();
var avg = (tensor_1d * tensor_index).sum();
avg = avg / tensor_1d.sum();
// keep four decimal places
avg = avg.round(4);
var str = $"{id}: sum: {avg.ToSingle()} dtype: {dtype} shape: [{shapeString}]";
Console.WriteLine(str);
return str;
}
public static string Peek(this nn.Module model)
{
var sb = new StringBuilder();
var state_dict = model.state_dict();
// preview state_dict
int i = 0;
foreach (var (key, value) in state_dict.OrderBy(x => x.Key, StringComparer.OrdinalIgnoreCase))
{
var str = value.Peek(key);
sb.AppendLine($"{i}: {str}");
i++;
}
var res = sb.ToString();
//Console.WriteLine(res);
return res;
}
public static string Peek_Shape(this nn.Module model)
{
var sb = new StringBuilder();
var state_dict = model.state_dict();
// preview state_dict
int i = 0;
foreach (var (key, value) in state_dict.OrderBy(x => x.Key, StringComparer.OrdinalIgnoreCase))
{
// shape str: [x, y, z]
var shapeStr = string.Join(", ", value.shape);
sb.AppendLine($"{i}: {key} shape: [{shapeStr}]");
i++;
}
var res = sb.ToString();
Console.WriteLine(res);
return res;
}
public static void LoadStateDict(this Dictionary<string, Tensor> dict, string location)
{
using FileStream stream = File.OpenRead(location);
using BinaryReader reader = new BinaryReader(stream);
var num = reader.Decode();
Console.WriteLine($"num: {num}");
for (int i = 0; i < num; i++)
{
var key = reader.ReadString();
Tensor tensor = dict[key];
Console.WriteLine($"load key: {key} tensor: {tensor}");
var originalDevice = tensor.device;
var originalType = tensor.dtype;
if (tensor.dtype == ScalarType.BFloat16)
{
tensor = tensor.to_type(ScalarType.Float32);
}
TensorExtensionMethods.Load(ref tensor!, reader, skip: false);
// convert type to bf16 if type is float
tensor = tensor!.to_type(originalType);
dict[key] = tensor;
}
}
//
// 摘要:
// Decode a long value from a binary reader
//
// 参数:
// reader:
// A BinaryReader instance used for input.
//
// 返回结果:
// The decoded value
public static long Decode(this BinaryReader reader)
{
long num = 0L;
int num2 = 0;
while (true)
{
long num3 = reader.ReadByte();
num += (num3 & 0x7F) << num2 * 7;
if ((num3 & 0x80) == 0L)
{
break;
}
num2++;
}
return num;
}
}