38 Commits

Author SHA1 Message Date
Toby Roseman bdbe459eaf In Swift, allow serial unet predictions 2024-11-08 09:18:39 -08:00
Alejandro Isaza 6775c2c01a Allow not using classifier free guidance for inference in Swift code 2024-10-08 12:06:39 -07:00
Zach Nagengast c891f4357b Add SD3 Pipeline (#329)
* Add SD3 Pipeline

Co-authored-by: atiorh <atiorh@users.noreply.github.com>
Co-authored-by: arda-argmax <arda-argmax@users.noreply.github.com>

* Use swift-transformers for tokenization

* Use diffusionkit converters in torch2coreml

* Documentation and cleanup

* Add model link

* Consolidate batch prediction logic

* Remove DecoderSD3.swift and consolidate logic into Decoder.swift

* Remove DiffusionKit MLX inference reference from README

---------

Co-authored-by: atiorh <atiorh@users.noreply.github.com>
Co-authored-by: arda-argmax <arda-argmax@users.noreply.github.com>
Co-authored-by: atila <atiorh@icloud.com>
2024-07-22 21:16:50 -07:00
Stuart Moore 7449ce46a4 Update CGImage+vImage.swift 2023-11-14 10:35:27 -08:00
Stuart Moore 5c62fab073 Use sRGB colorspace 2023-11-14 10:35:27 -08:00
Pedro Cuenca 94814cfa41 Sigma and timestep spacing configuration for DPMSolverMultistepScheduler (#265)
* `leading` timestep spacing for DPMSolverMultistepScheduler

* Karras sigmas and timesteps

* Make sigmas/timesteps externally configurable
2023-09-27 09:35:39 -07:00
Zach Nagengast 94dfc6b548 Support for SDXL refiner (#227)
* Initial support for SDXL refiner

* Cleanup

* Add arg for converting Unet in float32 precision

* Setup scale factor with pipeline in CLI

* Update cli arg and future warning

* Bundle refiner unet if specified

* Update script for bundled refiner

- Also skip loading model if check_output_correctness is missing, since the model does not require inferencing at conversion time

* Flip skip_model_load bool

* Cleanup

* Support bundled UnetRefiner

* Add seperate refiner config value

- Includes unloading base unet when swapping to refiner

* Update readme for SDXL refiner

* Add condition for new SDXL coreml input features

* Revert pipeline interface change, add extra logging on pipe load

* Reset model_version after refiner conversion

* Reset model_version before refiner conversion but after pipe init

* Add refiner chunking

* Ensure unets are unloaded for reduceMemory true

* Handle missing UnetRefiner.mlmodelc on pipeline load

Co-authored-by: Pedro Cuenca <pedro@huggingface.co>

* Prewarm refiner on load, unload on complete

* Force cpu_and_gpu for VAE until it can be fixed

* Include output dtype of np.float32 for all conversions

* Allow a custom VAE to be converted.

* Revert hardcoded reduceMemory

* Fix merge

* Default chunking arg for --merge-chunks-in-pipeline-model when called from torch2coreml

---------

Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
2023-09-26 13:09:58 -07:00
Liu Liu 7b59c2b663 Add NvRandomSource (#257)
Draw Things 1.20230913.1 released with NVIDIA GPU Compatible mode. See
example: https://twitter.com/drawthingsapp/status/1702387783166476689

This is the implementation that should be compatible with the
ml-stable-diffusion project.
2023-09-14 16:34:29 -07:00
Ryu-ga c5063225aa Update Decoder.swift (#249)
* Update Decoder.swift

* Update Encoder.swift

---------

Co-authored-by: Ryu-ga <frech5679@icloud.com>
2023-09-01 11:50:01 -07:00
Pedro Cuenca 80c0551419 CGImage to shaped array fix (#239)
* Take row padding into account.

* Rename symbols for clarity.
2023-08-26 23:51:58 -07:00
Zach Nagengast b392a0aca0 Use denoised latent for final decoder (#230)
* Send denoised latent to final decoder

- Previously it was sending the noisy latent from the last timestep

* Include fix for no-xl pipeline
2023-08-08 10:47:42 -07:00
Atila Orhon 8cf34376f9 SDXL and other improvements
Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
Co-authored-by: Zach Nagengast <zacharynagengast@gmail.com>
2023-07-27 09:08:30 -10:00
Atila Orhon f741511ef2 import Accelerate 2023-07-19 20:56:57 -07:00
JustinMeans b8b5e886df Update ControlNet.swift (#214)
Speed up multi-control net by implementing Accelerate vectorized routines.
2023-07-18 09:34:25 -07:00
Zach Nagengast b61c9aea05 Support for denoised latent images on progress updates (#210)
* Use denoised latents for currentLatentSamples

* Remove incorrect scheduler reference

* Add denoise option to preserve original functionality
2023-07-14 15:52:13 -07:00
Zsolt Váradi db13060fec Restore compatibility with Xcode 14.3/macOS SDK 13.4 (#208) 2023-07-12 11:49:22 -07:00
Zsolt Váradi 823d21ecf4 Extend ControlNet image conversion with hole punching for InPaint (#205)
* Extend ControlNet image conversion with hole punching for InPaint

The inpainting ControlNet model expects the masked areas to be filled with -1. This never occured with the original conversion, as the pixel data was converted to values between [0;1], thus the inpainting CN model never had any effect (apart from maybe generating artifacts).

This commit uses the _previously discarded_ alpha channel of the source image to mark masked pixels: any non-opaque pixel is cleared out to -1.
For an end-user, this allows creating masks simply by erasing areas with Preview to transparent, saving the result as PNG, and feeding it to the ControlNet.

* Mask only fully transparent pixels
2023-07-09 21:11:26 -07:00
Atila Orhon 7f9c58a6c1 WWDC23 update
Co-authored-by: davidfindlay1 <david.arthur.findlay@icloud.com>
2023-06-14 08:27:02 -07:00
Oleg Ponomaryov ea904e31d7 Image encoder resources management fix (#182)
* Unload image encoder for image2image in reduceMemory mode

* Include image encoder to resource management functions
2023-05-06 21:22:19 -07:00
Alejandro Isaza a7bf219247 Improve BPETokenizer.readMerges performance (#169)
From 240ms to 19ms on M1 MacBook Pro

Co-authored-by: Alejandro Isaza <aisaza@apple.com>
2023-04-26 21:51:25 -07:00
Zsolt Váradi fbef6c3826 Fix img2img starting image shape (#168) 2023-04-26 13:01:45 -07:00
Alejandro Isaza 28a29312fd Scheduler performance optimizations in Swift 2023-04-26 08:25:16 -07:00
Chimme 7f65e1c84b Support ControlNet (#153)
* add controlnet tentatively

* add controlnet in python code

* implement swift part

* support 8-bit quantization

* add controlnet unload when reduce memory

* remove irrelevant changes

* add more description about controlnet option in swift

* fix some for pr and update README

* pre-allocate zero shapedArray + make multi-controlnet faster
2023-04-18 16:17:59 -07:00
Atila Orhon ab12eee10c [swift] Added scaleFactor explanation 2023-04-03 12:37:13 -07:00
Guillermo Cique 1147e87b79 Simplify Encoder module (#145)
* Simplify encoder conversion

* Add nextNormal to RandomSource

* Add addNoise method to Scheduler

* Update Encoder implementation

* Clean up Pipeline

* Remove AlphasCumprodCalculation

* Revert Encoder’s input shape size

* Make Encoder/Decoder's scale factor configurable
2023-04-03 12:31:53 -07:00
Nikita Rossik 42889f6b2a Update documentation comments (#147) 2023-03-27 08:26:57 -07:00
Liu Liu ddefb61673 Add random source that matches PyTorch (#124)
* Add random source that matches PyTorch

This added random source that matches PyTorch on CPU. In particular, it
matches: `torch.randn([], dtype=torch.float)` result.

PyTorch's RNG is a bit convoluted and not claimed to be version-stable
(will open a separate issue in PyTorch repo on this). However, the
current implementation on CPU is fairly straightforward^*.

1. If it is less than 16 elements, it uses Gaussian distribution sampled
   from MT19937 for double + Box-Muller transformation.

2. If it is more than 16 (16 included), it first do uniform sampling
   with whatever the resulting data type would be (in this case, torch.float),
   and then apply Box-Muller transformation over 16-element segment at a
   type, treating the first floating-point and the 8th as a pair, so on
   so forth.

3. If it is not a multiple of 16, trace back from the end for 16
   elements and redo step 2.

* Update with configuration available in SwiftDiffusionCLI

* Fix the RNG is not passed into pipelineConfig.
2023-02-14 21:45:27 -08:00
Norton 00390a6418 fix (#125) 2023-02-09 20:07:18 -08:00
Timothy Kautz fa7bbdc255 Image2image - swift (#116)
* Image2Image Encoder

Encoder

* Scheduler and pipeline

* fix scheduler

* cli

* remove CLI comment

* disable dpm multistep solver with image2image

* clamp initial timestamp

* Store timesteps in reverse order for consistency.

* Report actual number of steps.

* uint32

* PRComments

* remove old initializer

* pr comments

* change name and add error handling

also fix 512 hard coded

* Add fix for Jpegs

---------

Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
2023-02-09 10:30:21 -08:00
Pedro Cuenca e3db2ec99e Fix timesteps of DPMSolverMultistepScheduler. (#88)
There were minor differences in the timesteps because the linspace was
computed slightly differently. This PR makes the Swift implementation
identical to the current Python implementation in diffusers, which was
originally contributed by the DPM-Solver++ author.

See
https://github.com/huggingface/diffusers/blob/main/src/diffusers/schedulers/scheduling_dpmsolver_multistep.py#L199-L204
for reference.
2022-12-28 12:04:34 -08:00
Wanaldino Antimonio e07c4d00c3 Move guidanceScale as generation parameter (#46)
* Move guidanceScale as generation parameter

* Added guidanceScale in CLI

* Reverted identation change
2022-12-24 10:33:39 -08:00
JustinMeans c90b705334 Adds Negative Prompts (#61)
* Synced to main branch and minimizes line changes

* Adds negative prompt argument to CLI

Co-authored-by: Wanaldino Antimonio <carlosmm31093@gmail.com>
2022-12-20 22:57:34 -08:00
Wanaldino Antimonio 4c00b32010 Changed seed type into UInt32 (#47) 2022-12-20 14:34:53 -08:00
Pedro Cuenca 3758abc652 Implement DPM-Solver++ scheduler (#59) 2022-12-14 18:05:30 -08:00
Christian Clauss d6a54fc2b0 Fix typos: Successfully facilitate getting pipeline overridden (#30) 2022-12-14 10:42:27 -08:00
Nathan Tannar 152dd65e7d Add Availability Annotations (#18) 2022-12-14 10:23:55 -08:00
Atila Orhon a4238a3718 README improvements and reduceMemory option in Swift
Co-authored-by: msiracusa <msiracusa+github@gmail.com>
2022-12-14 08:23:55 -08:00
Atila Orhon 583cc04ece Initial commit
Co-authored-by: aseemw <aseem.elec@gmail.com>
Co-authored-by: msiracusa <msiracusa+github@gmail.com>
2022-11-30 13:41:05 -06:00