sd

package module
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2023 License: MIT Imports: 12 Imported by: 6

README

stable-diffusion

pure go for stable-diffusion and support cross-platform.

Go Reference

sd.go is a wrapper around stable-diffusion-cpp, which is an adaption of ggml.cpp.

Installation

go get github.com/seasonjs/stable-diffusion

AutoModel Compatibility

See deps folder for dylib compatibility, push request is welcome.

platform x32 x64 arm cuda
windows not support support avx/avx2/avx512 not support support cuda12
linux not support support not support
darwin not support support support

AutoModel Dynamic Libraries Disclaimer

The Source of dynamic Libraries

These dynamic libraries come from stable-diffusion.cpp-build release, The dynamic library version can be obtained by viewing stable-diffusion.version file Anyone can check the consistency of the file by checksum ( MD5 ).

The Security Of Dynamic Libraries

All I can say is that the creation of the dynamic library is public and does not contain any subjective malicious logic. If you are worried about the security of the dynamic library during the use process, you can build it yourself.

I and any author related to dynamic libraries do not assume any problems, responsibilities or legal liability during use.

Usage

This stable-diffusion golang library provide two api Predict and ImagePredict.

You don't need to download stable-diffusion dynamic library.

package main

import (
	sd "github.com/seasonjs/stable-diffusion"
	"io"
	"os"
	"path/filepath"
)

func main() {
	options := sd.DefaultStableDiffusionOptions
	options.Width = 512
	options.Height = 512

	model, err := sd.NewStableDiffusionAutoModel(options)
	if err != nil {
		print(err.Error())
		return
	}
	defer model.Close()

	err = model.LoadFromFile("./models/mysd.safetensors")
	if err != nil {
		print(err.Error())
		return
	}
	var writers []io.Writer

	girl, err := filepath.Abs("./assets/a_girl.png")
	if err != nil {
		print(err.Error())
		return
	}

	filenames := []string{
		girl,
	}
	for _, filename := range filenames {
		file, err := os.Create(filename)
		if err != nil {
			print(err.Error())
			return
		}
		defer file.Close()
		writers = append(writers, file)
	}

	err = model.Predict("a girl, high quality", writers)
	if err != nil {
		print(err.Error())
	}
}

If NewStableDiffusionAutoModel can't automatic loading of dynamic library, please use NewStableDiffusionModel method load manually.

package main

import (
	"fmt"
	sd "github.com/seasonjs/stable-diffusion"
	"runtime"
)

func getLibrary() string {
	switch runtime.GOOS {
	case "darwin":
		return "./deps/darwin/libstable-diffusion_arm64.dylib"
	case "linux":
		return "./deps/linux/libstable-diffusion.so"
	case "windows":
		return "./deps/windows/stable-diffusion_avx2_x64.dll"
	default:
		panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
	}
}
func main() {
	options := sd.DefaultStableDiffusionOptions
	//It's important to set image size,different model support different size
	options.Width = 256
	options.Height = 256
	model, err := sd.NewStableDiffusionModel(getLibrary(), options)
	print(model, err)
	//.... the usage is same as `NewStableDiffusionAutoModel`. they both return `StableDiffusionModel` struct

}

Packaging

To ship a working program that includes this AI, you will need to include the following files:

  • libstable-diffusion.dylib / libstable-diffusion.so / stable-diffusion.dll (buildin)
  • the model file
  • the tokenizer file (buildin)

Low level API

This package also provide low level Api which is same as stable-diffusion-cpp. See detail at stable-diffusion-doc.

Thanks

Successful Examples

License

Copyright (c) seasonjs. All rights reserved. Licensed under the MIT License. See License.txt in the project root for license information.

Documentation

Index

Constants

View Source
const (
	DEBUG SDLogLevel = "DEBUG"
	INFO             = "INFO"
	WARN             = "WARN"
	ERROR            = "ERROR"
)
View Source
const (
	EULER_A          SampleMethod = "EULER_A"
	EULER                         = "EULER"
	HEUN                          = "HEUN"
	DPM2                          = "DPM2"
	DPMPP2S_A                     = "DPMPP2S_A"
	DPMPP2M                       = "DPMPP2M"
	DPMPP2Mv2                     = "DPMPP2Mv2"
	LCM                           = "LCM"
	N_SAMPLE_METHODS              = "N_SAMPLE_METHODS"
)
View Source
const (
	DEFAULT     Schedule = "DEFAULT"
	DISCRETE             = "DISCRETE"
	KARRAS               = "KARRAS"
	N_SCHEDULES          = "N_SCHEDULES"
)
View Source
const (
	T_DEFAULT GgmlType = "DEFAULT"
	F32                = "F32"
	F16                = "F16"
	Q4_0               = "Q4_0"
	Q4_1               = "Q4_1"
	Q5_0               = "Q5_0"
	Q5_1               = "Q5_1"
	Q8_0               = "Q8_0"
)

Variables

View Source
var DefaultStableDiffusionOptions = StableDiffusionOptions{
	Threads:               -1,
	VaeDecodeOnly:         true,
	FreeParamsImmediately: true,
	LoraModelDir:          "",
	RngType:               CUDA_RNG,
	WType:                 T_DEFAULT,
	Schedule:              DEFAULT,

	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:            500,
	Height:           500,
	SampleMethod:     EULER_A,
	SampleSteps:      20,
	Strength:         0.4,
	Seed:             42,
	BatchCount:       1,
	OutputsImageType: PNG,
}

Functions

This section is empty.

Types

type CStableDiffusion

type CStableDiffusion interface {
	StableDiffusionFullDefaultParamsRef() *StableDiffusionFullParams
	StableDiffusionFullParamsSetNegativePrompt(params *StableDiffusionFullParams, negativePrompt string)
	StableDiffusionFullParamsSetCfgScale(params *StableDiffusionFullParams, cfgScale float32)
	StableDiffusionFullParamsSetWidth(params *StableDiffusionFullParams, width int)
	StableDiffusionFullParamsSetHeight(params *StableDiffusionFullParams, height int)
	StableDiffusionFullParamsSetSampleMethod(params *StableDiffusionFullParams, sampleMethod SampleMethod)
	StableDiffusionFullParamsSetSampleSteps(params *StableDiffusionFullParams, sampleSteps int)
	StableDiffusionFullParamsSetSeed(params *StableDiffusionFullParams, seed int64)
	StableDiffusionFullParamsSetBatchCount(params *StableDiffusionFullParams, batchCount int)
	StableDiffusionFullParamsSetStrength(params *StableDiffusionFullParams, strength float32)

	StableDiffusionInit(nThreads int, vaeDecodeOnly bool, taesdPath string, freeParamsImmediately bool, loraModelDir string, rngType RNGType) *StableDiffusionCtx
	StableDiffusionLoadFromFile(ctx *StableDiffusionCtx, filePath string, vaePath string, wtype GgmlType, schedule Schedule)
	StableDiffusionPredictImage(ctx *StableDiffusionCtx, params *StableDiffusionFullParams, prompt string) []byte
	StableDiffusionImagePredictImage(ctx *StableDiffusionCtx, params *StableDiffusionFullParams, initImage []byte, prompt string) []byte
	StableDiffusionSetLogLevel(level SDLogLevel)
	StableDiffusionGetSystemInfo() string
	StableDiffusionFree(ctx *StableDiffusionCtx)
	StableDiffusionFreeBuffer(buffer uintptr)
	StableDiffusionFreeFullParams(params *StableDiffusionFullParams)
}

func NewCStableDiffusion

func NewCStableDiffusion(libraryPath string) (CStableDiffusion, error)

type CStableDiffusionImpl

type CStableDiffusionImpl struct {
	// contains filtered or unexported fields
}

func (*CStableDiffusionImpl) StableDiffusionFree

func (c *CStableDiffusionImpl) StableDiffusionFree(ctx *StableDiffusionCtx)

func (*CStableDiffusionImpl) StableDiffusionFreeBuffer

func (c *CStableDiffusionImpl) StableDiffusionFreeBuffer(buffer uintptr)

func (*CStableDiffusionImpl) StableDiffusionFreeFullParams

func (c *CStableDiffusionImpl) StableDiffusionFreeFullParams(params *StableDiffusionFullParams)

func (*CStableDiffusionImpl) StableDiffusionFullDefaultParamsRef

func (c *CStableDiffusionImpl) StableDiffusionFullDefaultParamsRef() *StableDiffusionFullParams

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetBatchCount

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetBatchCount(params *StableDiffusionFullParams, batchCount int)

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetCfgScale

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetCfgScale(params *StableDiffusionFullParams, cfgScale float32)

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetHeight

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetHeight(params *StableDiffusionFullParams, height int)

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetNegativePrompt

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetNegativePrompt(params *StableDiffusionFullParams, negativePrompt string)

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetSampleMethod

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetSampleMethod(params *StableDiffusionFullParams, sampleMethod SampleMethod)

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetSampleSteps

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetSampleSteps(params *StableDiffusionFullParams, sampleSteps int)

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetSeed

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetSeed(params *StableDiffusionFullParams, seed int64)

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetStrength

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetStrength(params *StableDiffusionFullParams, strength float32)

func (*CStableDiffusionImpl) StableDiffusionFullParamsSetWidth

func (c *CStableDiffusionImpl) StableDiffusionFullParamsSetWidth(params *StableDiffusionFullParams, width int)

func (*CStableDiffusionImpl) StableDiffusionGetSystemInfo

func (c *CStableDiffusionImpl) StableDiffusionGetSystemInfo() string

func (*CStableDiffusionImpl) StableDiffusionImagePredictImage

func (c *CStableDiffusionImpl) StableDiffusionImagePredictImage(ctx *StableDiffusionCtx, params *StableDiffusionFullParams, initImage []byte, prompt string) []byte

func (*CStableDiffusionImpl) StableDiffusionInit

func (c *CStableDiffusionImpl) StableDiffusionInit(nThreads int, vaeDecodeOnly bool, taesdPath string, freeParamsImmediately bool, loraModelDir string, rngType RNGType) *StableDiffusionCtx

func (*CStableDiffusionImpl) StableDiffusionLoadFromFile

func (c *CStableDiffusionImpl) StableDiffusionLoadFromFile(ctx *StableDiffusionCtx, filePath string, vaePath string, wtype GgmlType, schedule Schedule)

func (*CStableDiffusionImpl) StableDiffusionPredictImage

func (c *CStableDiffusionImpl) StableDiffusionPredictImage(ctx *StableDiffusionCtx, params *StableDiffusionFullParams, prompt string) []byte

func (*CStableDiffusionImpl) StableDiffusionSetLogLevel

func (c *CStableDiffusionImpl) StableDiffusionSetLogLevel(level SDLogLevel)

type GgmlType

type GgmlType string

type OutputsImageType

type OutputsImageType string
const (
	PNG  OutputsImageType = "PNG"
	JPEG                  = "JPEG"
)

type RNGType

type RNGType string
const (
	STD_DEFAULT_RNG RNGType = "STD_DEFAULT_RNG"
	CUDA_RNG                = "CUDA_RNG"
)

type SDLogLevel

type SDLogLevel string

type SampleMethod

type SampleMethod string

type Schedule

type Schedule string

type StableDiffusionCtx

type StableDiffusionCtx struct {
	// contains filtered or unexported fields
}

type StableDiffusionFullParams

type StableDiffusionFullParams struct {
	// contains filtered or unexported fields
}

type StableDiffusionModel

type StableDiffusionModel struct {
	// contains filtered or unexported fields
}

func NewStableDiffusionAutoModel

func NewStableDiffusionAutoModel(options StableDiffusionOptions) (*StableDiffusionModel, error)

func NewStableDiffusionModel

func NewStableDiffusionModel(dylibPath string, options StableDiffusionOptions) (*StableDiffusionModel, error)

func (*StableDiffusionModel) Close

func (sd *StableDiffusionModel) Close() error

func (*StableDiffusionModel) ImagePredict

func (sd *StableDiffusionModel) ImagePredict(reader io.Reader, prompt string, writer io.Writer) error

func (*StableDiffusionModel) LoadFromFile

func (sd *StableDiffusionModel) LoadFromFile(path string) error

func (*StableDiffusionModel) Predict

func (sd *StableDiffusionModel) Predict(prompt string, writer []io.Writer) error

func (*StableDiffusionModel) SetOptions

func (sd *StableDiffusionModel) SetOptions(options StableDiffusionOptions)

type StableDiffusionOptions

type StableDiffusionOptions struct {
	Threads               int
	VaeDecodeOnly         bool
	TaesdPath             string
	FreeParamsImmediately bool
	LoraModelDir          string
	RngType               RNGType
	VaePath               string
	WType                 GgmlType
	Schedule              Schedule

	NegativePrompt   string
	CfgScale         float32
	Width            int
	Height           int
	SampleMethod     SampleMethod
	SampleSteps      int
	Strength         float32
	Seed             int64
	BatchCount       int
	GpuEnable        bool
	OutputsImageType OutputsImageType
}

Directories

Path Synopsis
deps

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL