Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): add stablediffusion-ggml #4289

Merged
merged 9 commits into from
Dec 3, 2024
Merged

Conversation

mudler
Copy link
Owner

@mudler mudler commented Nov 29, 2024

Description

This PR is a first attempt into adding a very straightforward backend for stablediffusion.cpp (https://github.com/leejet/stable-diffusion.cpp which is using ggml-based engine). Part of #1126

To test it:

name: sd-ggml
backend: stablediffusion-ggml
parameters:
  model: huggingface://city96/FLUX.1-dev-gguf/flux1-dev-Q2_K.gguf

options:
 - "diffusion_model"
 - "clip_l_path:clip_l.safetensors"
 - "t5xxl_path:t5xxl_fp16.safetensors"
 - "vae_path:ae.safetensors"
 - "sampler:euler"

step: 25

download_files:
- filename: ae.safetensors
  sha256: afc8e28272cd15db3919bacdb6918ce9c1ed22e96cb12c4d5ed0fba823529e38
  uri: https://huggingface.co/ChuckMcSneed/FLUX.1-dev/resolve/main/ae.safetensors
- filename: clip_l.safetensors
  sha256: 660c6f5b1abae9dc498ac2d21e1347d2abdb0cf6c0c0c8576cd796491d9a6cdd
  uri: https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/clip_l.safetensors
- filename: t5xxl_fp16.safetensors
  sha256: 6e480b09fae049a72d2a8c5fbccb8d3e92febeb233bbe9dfe7256958a9167635
  uri: https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/t5xxl_fp16.safetensors
curl http://localhost:8080/v1/images/generations -H "Content-Type: application/json" -d '{
  "prompt": "A cute baby sea otter", "model": "sd-ggml",
  "size": "256x256"
}'

Notes for Reviewers

It does not wire up all the options available to stablediffusion.cpp, the purpose of this PR is to introduce the backend first, optimizations are left as an exercise for later.

Signed commits

  • Yes, I signed my commits.

backend/go/image/stablediffusion-ggml/gosd.go Fixed Show fixed Hide fixed
backend/go/image/stablediffusion-ggml/gosd.go Fixed Show fixed Hide fixed

func (sd *SDGGML) GenerateImage(opts *pb.GenerateImageRequest) error {
t := C.CString(opts.PositivePrompt)
defer C.free(unsafe.Pointer(t))

Check warning

Code scanning / gosec

Use of unsafe calls should be audited Warning

Use of unsafe calls should be audited
defer C.free(unsafe.Pointer(t))

dst := C.CString(opts.Dst)
defer C.free(unsafe.Pointer(dst))

Check warning

Code scanning / gosec

Use of unsafe calls should be audited Warning

Use of unsafe calls should be audited
backend/go/image/stablediffusion-ggml/gosd.go Fixed Show fixed Hide fixed
backend/go/image/stablediffusion-ggml/gosd.go Fixed Show fixed Hide fixed
@mudler mudler added the enhancement New feature or request label Nov 29, 2024
Copy link

netlify bot commented Nov 29, 2024

Deploy Preview for localai ready!

Name Link
🔨 Latest commit c8964f0
🔍 Latest deploy log https://app.netlify.com/sites/localai/deploys/674f7264ff591000077052ad
😎 Deploy Preview https://deploy-preview-4289--localai.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@mudler mudler force-pushed the feat/stablediffusion-ggml branch from 597a649 to b5bcda2 Compare November 29, 2024 17:10
@mudler mudler marked this pull request as draft November 29, 2024 17:27
@mudler
Copy link
Owner Author

mudler commented Nov 29, 2024

Testing with:

name: sd-ggml
backend: stablediffusion-ggml
parameters:
  model: huggingface://city96/FLUX.1-dev-gguf/flux1-dev-Q2_K.gguf

WIP as doesn't work still

@mudler
Copy link
Owner Author

mudler commented Dec 2, 2024

Testing it with:

name: sd-ggml
backend: stablediffusion-ggml
parameters:
  model: huggingface://city96/FLUX.1-dev-gguf/flux1-dev-Q2_K.gguf

options:
 - "diffusion_model"
 - "clip_l_path:clip_l.safetensors"
 - "t5xxl_path:t5xxl_fp16.safetensors"
 - "vae_path:ae.safetensors"
 - "sampler:euler"

download_files:
- filename: ae.safetensors
  sha256: afc8e28272cd15db3919bacdb6918ce9c1ed22e96cb12c4d5ed0fba823529e38
  uri: https://huggingface.co/ChuckMcSneed/FLUX.1-dev/resolve/main/ae.safetensors
- filename: clip_l.safetensors
  sha256: 660c6f5b1abae9dc498ac2d21e1347d2abdb0cf6c0c0c8576cd796491d9a6cdd
  uri: https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/clip_l.safetensors
- filename: t5xxl_fp16.safetensors
  sha256: 6e480b09fae049a72d2a8c5fbccb8d3e92febeb233bbe9dfe7256958a9167635
  uri: https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/t5xxl_fp16.safetensors

backend/go/image/stablediffusion-ggml/gosd.go Fixed Show fixed Hide fixed
size := C.size_t(unsafe.Sizeof((*C.char)(nil)))
length := C.size_t(len(opts.Options))
options = (**C.char)(C.malloc(length * size))
view := (*[1 << 30]*C.char)(unsafe.Pointer(options))[0:len(opts.Options):len(opts.Options)]

Check warning

Code scanning / gosec

Use of unsafe calls should be audited Warning

Use of unsafe calls should be audited
defer C.free(unsafe.Pointer(dst))

negative := C.CString(opts.NegativePrompt)
defer C.free(unsafe.Pointer(negative))

Check warning

Code scanning / gosec

Use of unsafe calls should be audited Warning

Use of unsafe calls should be audited
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler mudler force-pushed the feat/stablediffusion-ggml branch from 765da54 to 606f215 Compare December 2, 2024 18:13
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
@mudler mudler marked this pull request as ready for review December 3, 2024 18:25
@mudler
Copy link
Owner Author

mudler commented Dec 3, 2024

finally working now:

name: sd-ggml
backend: stablediffusion-ggml
parameters:
  model: huggingface://city96/FLUX.1-dev-gguf/flux1-dev-Q2_K.gguf

options:
 - "diffusion_model"
 - "clip_l_path:clip_l.safetensors"
 - "t5xxl_path:t5xxl_fp16.safetensors"
 - "vae_path:ae.safetensors"
 - "sampler:euler"

step: 25

download_files:
- filename: ae.safetensors
  sha256: afc8e28272cd15db3919bacdb6918ce9c1ed22e96cb12c4d5ed0fba823529e38
  uri: https://huggingface.co/ChuckMcSneed/FLUX.1-dev/resolve/main/ae.safetensors
- filename: clip_l.safetensors
  sha256: 660c6f5b1abae9dc498ac2d21e1347d2abdb0cf6c0c0c8576cd796491d9a6cdd
  uri: https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/clip_l.safetensors
- filename: t5xxl_fp16.safetensors
  sha256: 6e480b09fae049a72d2a8c5fbccb8d3e92febeb233bbe9dfe7256958a9167635
  uri: https://huggingface.co/comfyanonymous/flux_text_encoders/resolve/main/t5xxl_fp16.safetensors
curl http://localhost:8080/v1/images/generations -H "Content-Type: application/json" -d '{
  "prompt": "A cute baby sea otter", "model": "sd-ggml",
  "size": "256x256"
}'

sd.threads = int(opts.Threads)

modelFile := C.CString(opts.ModelFile)
defer C.free(unsafe.Pointer(modelFile))

Check warning

Code scanning / gosec

Use of unsafe calls should be audited Warning

Use of unsafe calls should be audited
@mudler mudler merged commit 44a5dac into master Dec 3, 2024
31 checks passed
@mudler mudler deleted the feat/stablediffusion-ggml branch December 3, 2024 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ai-model enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant