-
Notifications
You must be signed in to change notification settings - Fork 6
/
sd.go
386 lines (344 loc) · 8.79 KB
/
sd.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
// Copyright (c) seasonjs. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package sd
import (
"errors"
"image"
"image/color"
"image/jpeg"
"image/png"
"io"
"log"
"os"
"path/filepath"
)
type OutputsImageType string
const (
PNG OutputsImageType = "PNG"
JPEG = "JPEG"
)
type Options struct {
VaePath string
TaesdPath string
LoraModelDir string
VaeDecodeOnly bool
VaeTiling bool
FreeParamsImmediately bool
Threads int
Wtype WType
RngType RNGType
Schedule Schedule
GpuEnable bool
}
type FullParams struct {
NegativePrompt string
ClipSkip int
CfgScale float32
Width int
Height int
SampleMethod SampleMethod
SampleSteps int
Strength float32
Seed int64
BatchCount int
OutputsImageType OutputsImageType
}
var DefaultOptions = Options{
Threads: -1, // auto
VaeDecodeOnly: true,
FreeParamsImmediately: true,
RngType: CUDA_RNG,
Wtype: F32,
Schedule: DEFAULT,
}
var DefaultFullParams = FullParams{
NegativePrompt: "out of frame, lowers, text, error, cropped, worst quality, low quality, jpeg artifacts, ugly, duplicate, morbid, mutilated, out of frame, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, mutation, deformed, blurry, dehydrated, bad anatomy, bad proportions, extra limbs, cloned face, disfigured, gross proportions, malformed limbs, missing arms, missing legs, extra arms, extra legs, fused fingers, too many fingers, long neck, username, watermark, signature",
CfgScale: 7.0,
Width: 512,
Height: 512,
SampleMethod: EULER_A,
SampleSteps: 20,
Strength: 0.4,
Seed: 42,
BatchCount: 1,
OutputsImageType: PNG,
}
type Model struct {
ctx *CStableDiffusionCtx
options *Options
csd CStableDiffusion
isAutoLoad bool
dylibPath string
diffusionModelPath string
esrganPath string
upscalerCtx *CUpScalerCtx
}
func NewAutoModel(options Options) (*Model, error) {
file, err := dumpSDLibrary(options.GpuEnable)
if err != nil {
return nil, err
}
if options.GpuEnable {
log.Printf("If you want to try offload your model to the GPU. " +
"Please confirm the size of your GPU memory to prevent memory overflow.")
}
dylibPath := file.Name()
model, err := NewModel(dylibPath, options)
if err != nil {
return nil, err
}
model.isAutoLoad = true
return model, nil
}
func NewModel(dylibPath string, options Options) (*Model, error) {
csd, err := NewCStableDiffusion(dylibPath)
if err != nil {
return nil, err
}
return &Model{
dylibPath: dylibPath,
options: &options,
csd: csd,
}, nil
}
func (sd *Model) LoadFromFile(path string) error {
if sd.ctx != nil {
sd.csd.FreeCtx(sd.ctx)
sd.ctx = nil
log.Printf("model already loaded, free old model")
}
_, err := os.Stat(path)
if err != nil {
return errors.New("the system cannot find the model file specified")
}
if !filepath.IsAbs(path) {
sd.diffusionModelPath, err = filepath.Abs(path)
if err != nil {
return err
}
} else {
sd.diffusionModelPath = path
}
ctx := sd.csd.NewCtx(path,
sd.options.VaePath,
sd.options.TaesdPath,
sd.options.LoraModelDir,
sd.options.VaeDecodeOnly,
sd.options.VaeTiling,
sd.options.FreeParamsImmediately,
sd.options.Threads,
sd.options.Wtype,
sd.options.RngType,
sd.options.Schedule)
sd.ctx = ctx
return nil
}
func (sd *Model) SetOptions(options Options) {
if sd.ctx != nil {
sd.csd.FreeCtx(sd.ctx)
sd.ctx = nil
log.Printf("model already loaded, free old model and set new options")
}
sd.options = &options
ctx := sd.csd.NewCtx(
sd.diffusionModelPath,
sd.options.VaePath,
sd.options.TaesdPath,
sd.options.LoraModelDir,
sd.options.VaeDecodeOnly,
sd.options.VaeTiling,
sd.options.FreeParamsImmediately,
sd.options.Threads,
sd.options.Wtype,
sd.options.RngType,
sd.options.Schedule)
sd.ctx = ctx
}
func (sd *Model) Predict(prompt string, params FullParams, writer []io.Writer) error {
if len(writer) != params.BatchCount {
return errors.New("writer count not match batch count")
}
if sd.ctx == nil {
return errors.New("model not loaded")
}
if params.Width%8 != 0 || params.Height%8 != 0 {
return errors.New("width and height must be multiples of 8")
}
images := sd.csd.PredictImage(
sd.ctx,
prompt,
params.NegativePrompt,
params.ClipSkip,
params.CfgScale,
params.Width,
params.Height,
params.SampleMethod,
params.SampleSteps,
params.Seed,
params.BatchCount,
)
if images == nil || len(images) != params.BatchCount {
return errors.New("predict failed")
}
for i, img := range images {
outputsImage := bytesToImage(img.Data, int(img.Width), int(img.Height))
err := imageToWriter(outputsImage, params.OutputsImageType, writer[i])
if err != nil {
return err
}
}
return nil
}
func (sd *Model) ImagePredict(reader io.Reader, prompt string, params FullParams, writer []io.Writer) error {
if len(writer) != params.BatchCount {
return errors.New("writer count not match batch count")
}
if sd.ctx == nil {
return errors.New("model not loaded")
}
decode, _, err := image.Decode(reader)
if err != nil {
return err
}
initImage := imageToBytes(decode)
images := sd.csd.ImagePredictImage(
sd.ctx,
initImage,
prompt,
params.NegativePrompt,
params.ClipSkip,
params.CfgScale,
params.Width,
params.Height,
params.SampleMethod,
params.SampleSteps,
params.Strength,
params.Seed,
params.BatchCount,
)
for i, img := range images {
outputsImage := bytesToImage(img.Data, int(img.Width), int(img.Height))
err = imageToWriter(outputsImage, params.OutputsImageType, writer[i])
if err != nil {
return err
}
}
return nil
}
func (sd *Model) UpscaleImage(reader io.Reader, esrganPath string, upscaleFactor uint32, writer io.Writer) error {
if sd.upscalerCtx == nil {
sd.esrganPath = esrganPath
sd.upscalerCtx = sd.csd.NewUpscalerCtx(esrganPath, sd.options.Threads, sd.options.Wtype)
}
if sd.esrganPath != esrganPath {
if sd.upscalerCtx != nil {
sd.csd.FreeUpscalerCtx(sd.upscalerCtx)
}
sd.upscalerCtx = sd.csd.NewUpscalerCtx(esrganPath, sd.options.Threads, sd.options.Wtype)
}
decode, _, err := image.Decode(reader)
if err != nil {
return err
}
initImage := imageToBytes(decode)
img := sd.csd.UpscaleImage(sd.upscalerCtx, initImage, upscaleFactor)
outputsImage := bytesToImage(img.Data, int(img.Width), int(img.Height))
err = imageToWriter(outputsImage, PNG, writer)
return err
}
func (sd *Model) SetLogCallback(cb CLogCallback) {
sd.csd.SetLogCallBack(cb)
}
func (sd *Model) Close() error {
if sd.ctx != nil {
sd.csd.FreeCtx(sd.ctx)
sd.ctx = nil
}
if sd.upscalerCtx != nil {
sd.csd.FreeUpscalerCtx(sd.upscalerCtx)
sd.upscalerCtx = nil
}
if sd.csd != nil {
err := sd.csd.Close()
if err != nil {
return err
}
}
if sd.isAutoLoad {
err := os.Remove(sd.dylibPath)
if err != nil {
return err
}
}
return nil
}
func imageToBytes(decode image.Image) Image {
bounds := decode.Bounds()
width := bounds.Max.X - bounds.Min.X
height := bounds.Max.Y - bounds.Min.Y
size := width * height * 3
bytesImg := make([]byte, size)
for x := bounds.Min.X; x < bounds.Max.X; x++ {
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
idx := (y*width + x) * 3
r, g, b, _ := decode.At(x, y).RGBA()
bytesImg[idx] = byte(r >> 8)
bytesImg[idx+1] = byte(g >> 8)
bytesImg[idx+2] = byte(b >> 8)
}
}
return Image{
Width: uint32(width),
Height: uint32(height),
Data: bytesImg,
Channel: 3,
}
}
func bytesToImage(byteData []byte, width, height int) image.Image {
img := image.NewRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
idx := (y*width + x) * 3
img.Set(x, y, color.RGBA{
R: byteData[idx],
G: byteData[idx+1],
B: byteData[idx+2],
A: 255,
})
}
}
return img
}
func imageToWriter(image image.Image, imageType OutputsImageType, writer io.Writer) error {
switch imageType {
case PNG:
err := png.Encode(writer, image)
if err != nil {
return err
}
case JPEG:
err := jpeg.Encode(writer, image, &jpeg.Options{Quality: 100})
if err != nil {
return err
}
default:
return errors.New("unknown image type")
}
return nil
}
//func chunkBytes(data []byte, chunks int) [][]byte {
// length := len(data)
// chunkSize := (length + chunks - 1) / chunks
// result := make([][]byte, chunks)
//
// for i := 0; i < chunks; i++ {
// start := i * chunkSize
// end := (i + 1) * chunkSize
// if end > length {
// end = length
// }
// result[i] = data[start:end:end]
// }
//
// return result
//}