-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_estimate__stablediffusioncpp.go
413 lines (369 loc) · 12.7 KB
/
file_estimate__stablediffusioncpp.go
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package gguf_parser
import (
"regexp"
"github.com/gpustack/gguf-parser-go/util/ptr"
"github.com/gpustack/gguf-parser-go/util/stringx"
)
// Types for StableDiffusionCpp estimation.
type (
// StableDiffusionCppRunEstimate represents the estimated result of loading the GGUF file in stable-diffusion.cpp.
StableDiffusionCppRunEstimate struct {
// Type describes what type this GGUF file is.
Type string `json:"type"`
// Architecture describes what architecture this GGUF file implements.
//
// All lowercase ASCII.
Architecture string `json:"architecture"`
// FlashAttention is the flag to indicate whether enable the flash attention,
// true for enable.
FlashAttention bool `json:"flashAttention"`
// FullOffloaded is the flag to indicate whether the layers are fully offloaded,
// false for partial offloaded or zero offloaded.
FullOffloaded bool `json:"fullOffloaded"`
// NoMMap is the flag to indicate whether support the mmap,
// true for support.
NoMMap bool `json:"noMMap"`
// ImageOnly is the flag to indicate whether the model is used for generating image,
// true for embedding only.
ImageOnly bool `json:"imageOnly"`
// Distributable is the flag to indicate whether the model is distributable,
// true for distributable.
Distributable bool `json:"distributable"`
// Devices represents the usage for running the GGUF file,
// the first device is the CPU, and the rest are GPUs.
Devices []StableDiffusionCppRunDeviceUsage `json:"devices"`
// Autoencoder is the estimated result of the autoencoder.
Autoencoder *StableDiffusionCppRunEstimate `json:"autoencoder,omitempty"`
// Conditioners is the estimated result of the conditioners.
Conditioners []StableDiffusionCppRunEstimate `json:"conditioners,omitempty"`
// Upscaler is the estimated result of the upscaler.
Upscaler *StableDiffusionCppRunEstimate `json:"upscaler,omitempty"`
// ControlNet is the estimated result of the control net.
ControlNet *StableDiffusionCppRunEstimate `json:"controlNet,omitempty"`
}
// StableDiffusionCppRunDeviceUsage represents the usage for running the GGUF file in llama.cpp.
StableDiffusionCppRunDeviceUsage struct {
// Remote is the flag to indicate whether the device is remote,
// true for remote.
Remote bool `json:"remote"`
// Position is the relative position of the device,
// starts from 0.
//
// If Remote is true, Position is the position of the remote devices,
// Otherwise, Position is the position of the device in the local devices.
Position int `json:"position"`
// Footprint is the memory footprint for bootstrapping.
Footprint GGUFBytesScalar `json:"footprint"`
// Parameter is the running parameters that the device processes.
Parameter GGUFParametersScalar `json:"parameter"`
// Weight is the memory usage of weights that the device loads.
Weight GGUFBytesScalar `json:"weight"`
// Computation is the memory usage of computation that the device processes.
Computation GGUFBytesScalar `json:"computation"`
}
)
func (gf *GGUFFile) EstimateStableDiffusionCppRun(opts ...GGUFRunEstimateOption) (e StableDiffusionCppRunEstimate) {
// Options
var o _GGUFRunEstimateOptions
for _, opt := range opts {
opt(&o)
}
switch {
case o.TensorSplitFraction == nil:
o.TensorSplitFraction = []float64{1}
o.MainGPUIndex = 0
case o.MainGPUIndex < 0 || o.MainGPUIndex >= len(o.TensorSplitFraction):
panic("main device index must be range of 0 to the length of tensor split fraction")
}
if len(o.DeviceMetrics) > 0 {
for i, j := 0, len(o.DeviceMetrics)-1; i < len(o.TensorSplitFraction)-j; i++ {
o.DeviceMetrics = append(o.DeviceMetrics, o.DeviceMetrics[j])
}
o.DeviceMetrics = o.DeviceMetrics[:len(o.TensorSplitFraction)+1]
}
if o.SDCBatchCount == nil {
o.SDCBatchCount = ptr.To[int32](1)
}
if o.SDCHeight == nil {
o.SDCHeight = ptr.To[uint32](512)
}
if o.SDCWidth == nil {
o.SDCWidth = ptr.To[uint32](512)
}
if o.SDCOffloadConditioner == nil {
o.SDCOffloadConditioner = ptr.To(true)
}
if o.SDCOffloadAutoencoder == nil {
o.SDCOffloadAutoencoder = ptr.To(true)
}
if o.SDCAutoencoderTiling == nil {
o.SDCAutoencoderTiling = ptr.To(true)
}
// Devices.
e.Devices = make([]StableDiffusionCppRunDeviceUsage, len(o.TensorSplitFraction)+1)
// Metadata.
a := gf.Architecture()
e.Type = a.Type
e.Architecture = normalizeArchitecture(a.DiffusionArchitecture)
// Flash attention.
e.FlashAttention = false // TODO: Implement this.
// Distributable.
e.Distributable = false // TODO: Implement this.
// Offload.
e.FullOffloaded = true // TODO: Implement this.
// NoMMap.
e.NoMMap = true // TODO: Implement this.
// ImageOnly.
e.ImageOnly = true // TODO: Implement this.
// Autoencoder.
if a.DiffusionAutoencoder != nil {
e.Autoencoder = &StableDiffusionCppRunEstimate{
Type: "model",
Architecture: e.Architecture + "_vae",
FlashAttention: e.FlashAttention,
Distributable: e.Distributable,
FullOffloaded: e.FullOffloaded,
NoMMap: e.NoMMap,
Devices: make([]StableDiffusionCppRunDeviceUsage, len(e.Devices)),
}
}
// Conditioners.
if len(a.DiffusionConditioners) != 0 {
e.Conditioners = make([]StableDiffusionCppRunEstimate, 0, len(a.DiffusionConditioners))
for i := range a.DiffusionConditioners {
e.Conditioners = append(e.Conditioners, StableDiffusionCppRunEstimate{
Type: "model",
Architecture: normalizeArchitecture(a.DiffusionConditioners[i].Architecture),
FlashAttention: e.FlashAttention,
Distributable: e.Distributable,
FullOffloaded: e.FullOffloaded,
NoMMap: e.NoMMap,
Devices: make([]StableDiffusionCppRunDeviceUsage, len(e.Devices)),
})
}
}
// Footprint
{
// Bootstrap.
e.Devices[0].Footprint = GGUFBytesScalar(5*1024*1024) /* model load */ + (gf.Size - gf.ModelSize) /* metadata */
// Output buffer,
// see
// TODO: Implement this.
}
var cdLs, aeLs, cpLs GGUFLayerTensorInfos
{
var tis GGUFTensorInfos
tis = gf.TensorInfos.Search(regexp.MustCompile(`^cond_stage_model\..*`))
if len(tis) != 0 {
cdLs = tis.Layers()
if len(cdLs) != len(e.Conditioners) {
panic("conditioners' layers count mismatch")
}
// NB(thxCode): resort the layers to match the order of the conditioners.
cdLsSorted := make([]IGGUFTensorInfos, len(cdLs))
cdLsSorted[0] = cdLs[len(cdLs)-1]
for i := 1; i < len(cdLs); i++ {
cdLsSorted[i] = cdLs[i-1]
}
cdLs = cdLsSorted
}
tis = gf.TensorInfos.Search(regexp.MustCompile(`^first_stage_model\..*`))
if len(tis) != 0 {
aeLs = tis.Layers()
}
tis = gf.TensorInfos.Search(regexp.MustCompile(`^model\.diffusion_model\..*`))
if len(tis) != 0 {
cpLs = tis.Layers()
} else {
cpLs = gf.TensorInfos.Layers()
}
}
// Weight & Parameter.
{
// Conditioners.
if cdLs != nil {
d := 0
if *o.SDCOffloadConditioner {
d = 1
}
for i := range cdLs {
e.Conditioners[i].Devices[d].Weight = GGUFBytesScalar(cdLs[i].Bytes())
e.Conditioners[i].Devices[d].Parameter = GGUFParametersScalar(cdLs[i].Elements())
}
}
// Autoencoder.
if aeLs != nil {
d := 0
if *o.SDCOffloadAutoencoder {
d = 1
}
e.Autoencoder.Devices[d].Weight = GGUFBytesScalar(aeLs.Bytes())
e.Autoencoder.Devices[d].Parameter = GGUFParametersScalar(aeLs.Elements())
}
// Compute.
if cpLs != nil {
e.Devices[1].Weight = GGUFBytesScalar(cpLs.Bytes())
e.Devices[1].Parameter = GGUFParametersScalar(cpLs.Elements())
}
}
// Computation.
{
// TODO: Implement this.
}
return e
}
// Types for StableDiffusionCpp estimated summary.
type (
// StableDiffusionCppRunEstimateSummary represents the estimated summary of loading the GGUF file in stable-diffusion.cpp.
StableDiffusionCppRunEstimateSummary struct {
/* Basic */
// Items
Items []StableDiffusionCppRunEstimateSummaryItem `json:"items"`
/* Appendix */
// Type describes what type this GGUF file is.
Type string `json:"type"`
// Architecture describes what architecture this GGUF file implements.
//
// All lowercase ASCII.
Architecture string `json:"architecture"`
// FlashAttention is the flag to indicate whether enable the flash attention,
// true for enable.
FlashAttention bool `json:"flashAttention"`
// NoMMap is the flag to indicate whether the file must be loaded without mmap,
// true for total loaded.
NoMMap bool `json:"noMMap"`
// ImageOnly is the flag to indicate whether the model is used for generating image,
// true for embedding only.
ImageOnly bool `json:"imageOnly"`
// Distributable is the flag to indicate whether the model is distributable,
// true for distributable.
Distributable bool `json:"distributable"`
}
// StableDiffusionCppRunEstimateSummaryItem represents the estimated summary item of loading the GGUF file in stable-diffusion.cpp.
StableDiffusionCppRunEstimateSummaryItem struct {
// FullOffloaded is the flag to indicate whether the layers are fully offloaded,
// false for partial offloaded or zero offloaded.
FullOffloaded bool `json:"fullOffloaded"`
// RAM is the memory usage for loading the GGUF file in RAM.
RAM StableDiffusionCppRunEstimateMemory `json:"ram"`
// VRAMs is the memory usage for loading the GGUF file in VRAM per device.
VRAMs []StableDiffusionCppRunEstimateMemory `json:"vrams"`
}
// StableDiffusionCppRunEstimateMemory represents the memory usage for loading the GGUF file in llama.cpp.
StableDiffusionCppRunEstimateMemory struct {
// Remote is the flag to indicate whether the device is remote,
// true for remote.
Remote bool `json:"remote"`
// Position is the relative position of the device,
// starts from 0.
//
// If Remote is true, Position is the position of the remote devices,
// Otherwise, Position is the position of the device in the local devices.
Position int `json:"position"`
// UMA represents the usage of Unified Memory Architecture.
UMA GGUFBytesScalar `json:"uma"`
// NonUMA represents the usage of Non-Unified Memory Architecture.
NonUMA GGUFBytesScalar `json:"nonuma"`
}
)
// SummarizeItem returns the corresponding LLaMACppRunEstimateSummaryItem with the given options.
func (e StableDiffusionCppRunEstimate) SummarizeItem(
mmap bool,
nonUMARamFootprint, nonUMAVramFootprint uint64,
) (emi StableDiffusionCppRunEstimateSummaryItem) {
emi.FullOffloaded = e.FullOffloaded
// RAM.
{
fp := e.Devices[0].Footprint
wg := e.Devices[0].Weight
cp := e.Devices[0].Computation
// UMA.
emi.RAM.UMA = fp + wg + cp
// NonUMA.
emi.RAM.NonUMA = GGUFBytesScalar(nonUMARamFootprint) + emi.RAM.UMA
}
// VRAMs.
emi.VRAMs = make([]StableDiffusionCppRunEstimateMemory, len(e.Devices)-1)
{
for i, d := range e.Devices[1:] {
fp := d.Footprint
wg := d.Weight
cp := d.Computation
// UMA.
emi.VRAMs[i].UMA = fp + wg + cp
// NonUMA.
emi.VRAMs[i].NonUMA = GGUFBytesScalar(nonUMAVramFootprint) + emi.VRAMs[i].UMA
}
}
// Add antoencoder's usage.
if e.Autoencoder != nil {
aemi := e.Autoencoder.SummarizeItem(mmap, 0, 0)
emi.RAM.UMA += aemi.RAM.UMA
emi.RAM.NonUMA += aemi.RAM.NonUMA
for i, v := range aemi.VRAMs {
emi.VRAMs[i].UMA += v.UMA
emi.VRAMs[i].NonUMA += v.NonUMA
}
}
// Add conditioners' usage.
for i := range e.Conditioners {
cemi := e.Conditioners[i].SummarizeItem(mmap, 0, 0)
emi.RAM.UMA += cemi.RAM.UMA
emi.RAM.NonUMA += cemi.RAM.NonUMA
for i, v := range cemi.VRAMs {
emi.VRAMs[i].UMA += v.UMA
emi.VRAMs[i].NonUMA += v.NonUMA
}
}
// Add upscaler's usage.
if e.Upscaler != nil {
uemi := e.Upscaler.SummarizeItem(mmap, 0, 0)
emi.RAM.UMA += uemi.RAM.UMA
emi.RAM.NonUMA += uemi.RAM.NonUMA
for i, v := range uemi.VRAMs {
emi.VRAMs[i].UMA += v.UMA
emi.VRAMs[i].NonUMA += v.NonUMA
}
}
// Add control net's usage.
if e.ControlNet != nil {
cnemi := e.ControlNet.SummarizeItem(mmap, 0, 0)
emi.RAM.UMA += cnemi.RAM.UMA
emi.RAM.NonUMA += cnemi.RAM.NonUMA
for i, v := range cnemi.VRAMs {
emi.VRAMs[i].UMA += v.UMA
emi.VRAMs[i].NonUMA += v.NonUMA
}
}
return emi
}
// Summarize returns the corresponding StableDiffusionCppRunEstimate with the given options.
func (e StableDiffusionCppRunEstimate) Summarize(
mmap bool,
nonUMARamFootprint, nonUMAVramFootprint uint64,
) (es StableDiffusionCppRunEstimateSummary) {
// Items.
es.Items = []StableDiffusionCppRunEstimateSummaryItem{
e.SummarizeItem(mmap, nonUMARamFootprint, nonUMAVramFootprint),
}
// Just copy from the original estimate.
es.Type = e.Type
es.Architecture = e.Architecture
es.FlashAttention = e.FlashAttention
es.NoMMap = e.NoMMap
es.ImageOnly = e.ImageOnly
es.Distributable = e.Distributable
return es
}
func normalizeArchitecture(arch string) string {
return stringx.ReplaceAllFunc(arch, func(r rune) rune {
switch r {
case ' ', '.', '-', '/', ':':
return '_' // Replace with underscore.
}
if r >= 'A' && r <= 'Z' {
r += 'a' - 'A' // Lowercase.
}
return r
})
}