-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathModuleExtension.cs
More file actions
292 lines (260 loc) · 9.79 KB
/
ModuleExtension.cs
File metadata and controls
292 lines (260 loc) · 9.79 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TorchSharp;
using static TorchSharp.torch;
namespace Microsoft.ML.GenAI.Core.Extension;
public static class ModuleExtension
{
public static long GetSizeInBytes(this nn.Module model)
{
var stateDict = model.state_dict();
long size = 0;
foreach (var (_, value) in stateDict)
{
size += value.numel() * value.element_size();
}
return size;
}
public static Dictionary<string, long> GetSizeForEachDynamicLayerInBytes(this nn.Module model)
{
var stateDict = model.named_children();
if (stateDict.Count() == 0)
{
return new();
}
else
{
var dict = new Dictionary<string, long>();
foreach (var (key, value) in stateDict)
{
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;
}
}
/// <summary>
/// Quantize the module using zero-point int8 quantization.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
public static void ToInt8QuantizeModule<T>(
this T model)
where T : nn.Module
{
if (model is IQuantizeModule quantized)
{
quantized.Int8();
return;
}
foreach (var (_, value) in model.named_children())
{
if (value is IQuantizeModule quantizeModule)
{
quantizeModule.Int8();
}
else
{
value.ToInt8QuantizeModule();
}
}
}
/// <summary>
/// Quantize the module using zero-point int4 quantization.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <param name="quantizedDType">Quantized data type, can be "fp4" or "nf4". "fp4" means 4-bits floating point (1-bit sign, 2-bit exponent and 1-bit mantissa) and "nf4" means normalized 4-bits floating point, which uses a specialized non-uniform quantization aimed at neural network weight distributions (ranged from -1 to 1).</param>
/// <param name="blockSize">Block size for quantization, can be [64, 128, 256, 512, 1024]. The larger the size, the faster the speed and the lower the precision.</param>
public static void ToQuantize4BitModule<T>(
this T model,
string quantizedDType = "fp4",
int blockSize = 64)
where T : nn.Module
{
var config = new Quantize4BitConfig(quantizedDType, blockSize);
if (model is IQuantizeModule quantized)
{
quantized.Quantize4Bit(config);
return;
}
foreach (var (_, value) in model.named_children())
{
if (value is IQuantizeModule quantizeModule)
{
quantizeModule.Quantize4Bit(config);
}
else
{
value.ToQuantize4BitModule(quantizedDType, blockSize);
}
}
}
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;
}
// for each module in the model, update device if it is IDynamicLoadModule
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);
}
}
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 inferring 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;
}
/// <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 inferring 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="numberOfLayerToBePlaced">a list of key-value pairs where the key is the device id (e.g. "cuda:0") and the value is the number of layers to be placed on the device.
/// If you want to place all remaining layers on the device, set that value to -1.
/// e.g. [{"cuda:0", 2}, {"cpu", -1}], the first 2 layers will be placed on "cuda:0" and the rest will be placed on "cpu".
/// </param>
/// <returns></returns>
public static Dictionary<string, string> InferDeviceMapForEachLayer(
this nn.Module model,
IEnumerable<KeyValuePair<string, int>> numberOfLayerToBePlaced)
{
var layerSizeMap = model.GetSizeForEachDynamicLayerInBytes()
.OrderByDescending(x => x.Value)
.ToList();
var deviceMap = new Dictionary<string, string>();
foreach (var (device, count) in numberOfLayerToBePlaced)
{
if (count != -1)
{
var topK = layerSizeMap.Take(count).ToList();
layerSizeMap = layerSizeMap.Skip(count).ToList();
foreach (var (key, value) in topK)
{
deviceMap[key] = device;
}
}
else
{
foreach (var (key, value) in layerSizeMap)
{
deviceMap[key] = device;
}
layerSizeMap.Clear();
break;
}
}
if (layerSizeMap.Count > 0)
{
throw new ArgumentException("The layer count is not enough to cover all layers, did you forget to set the last layer count to -1?");
}
return deviceMap;
}
internal static string Peek(this nn.Module model)
{
var sb = new StringBuilder();
var stateDict = model.state_dict();
// preview state_dict
int i = 0;
foreach (var (key, value) in stateDict.OrderBy(x => x.Key, StringComparer.OrdinalIgnoreCase))
{
var str = value.Peek(key);
sb.AppendLine($"{i}: {str}");
i++;
}
var res = sb.ToString();
return res;
}
internal static string PeekShape(this nn.Module model)
{
var sb = new StringBuilder();
var stateDict = model.state_dict();
// preview state_dict
int i = 0;
foreach (var (key, value) in stateDict.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();
return res;
}
}