-
Notifications
You must be signed in to change notification settings - Fork 70
feat(diffusion): add LoRA support to addon run config #1474
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
46d813c
feat(diffusion): add LoRA support via run config (JS → native → sd.cpp)
gabrielgrigoras-serv d756a84
test(diffusion): add real LoRA integration test
gabrielgrigoras-serv 12b849a
chore(diffusion): bump version for LoRA support
gabrielgrigoras-serv c5fd40b
Merge branch 'main' into feat/diffusion-lora
gabrielgrigoras-serv 98ee72d
Merge branch 'main' into feat/diffusion-lora
gabrielgrigoras-serv 7b9cd79
Merge branch 'main' into feat/diffusion-lora
gabrielgrigoras-serv 0899b97
Merge branch 'main' into feat/diffusion-lora
gabrielgrigoras-serv 2cbe486
Validate diffusion LoRA paths
gabrielgrigoras-serv abae45b
Merge branch 'main' into feat/diffusion-lora
gabrielgrigoras-serv 63a7e29
Export generated mobile integration runners
gabrielgrigoras-serv e76ec6a
Merge branch 'main' into feat/diffusion-lora
gabrielgrigoras-serv 311c161
Merge branch 'main' into feat/diffusion-lora
gabrielgrigoras-serv f6392d3
Merge branch 'main' into feat/diffusion-lora
gianni-cor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
packages/lib-infer-diffusion/test/integration/lora-bridge.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| 'use strict' | ||
|
|
||
| const fs = require('bare-fs') | ||
| const path = require('bare-path') | ||
| const os = require('bare-os') | ||
| const proc = require('bare-process') | ||
| const test = require('brittle') | ||
| const binding = require('../../binding') | ||
| const ImgStableDiffusion = require('../../index') | ||
| const { | ||
| ensureModel, | ||
| detectPlatform, | ||
| setupJsLogger, | ||
| isPng | ||
| } = require('./utils') | ||
|
|
||
| const platform = detectPlatform() | ||
| const isDarwinX64 = os.platform() === 'darwin' && os.arch() === 'x64' | ||
| const isLinuxArm64 = os.platform() === 'linux' && os.arch() === 'arm64' | ||
| const isMobile = os.platform() === 'ios' || os.platform() === 'android' | ||
| const noGpu = proc.env && proc.env.NO_GPU === 'true' | ||
| const useCpu = isDarwinX64 || isLinuxArm64 || noGpu | ||
| const skip = isMobile || noGpu | ||
|
|
||
| const DEFAULT_MODEL = { | ||
| name: 'stable-diffusion-v2-1-Q8_0.gguf', | ||
| url: 'https://huggingface.co/gpustack/stable-diffusion-v2-1-GGUF/resolve/main/stable-diffusion-v2-1-Q8_0.gguf' | ||
| } | ||
|
|
||
| const LORA_ADAPTER = { | ||
| name: 'pytorch_lora_weights-sd21-comfyui.safetensors', | ||
| url: 'https://huggingface.co/radames/sd-21-DPO-LoRA/resolve/main/pytorch_lora_weights-sd21-comfyui.safetensors' | ||
| } | ||
|
|
||
| test('SD2.1 txt2img with LoRA — generates a valid PNG image', { timeout: 600000, skip }, async (t) => { | ||
| setupJsLogger(binding) | ||
|
|
||
| const [downloadedModelName, modelDir] = await ensureModel({ | ||
| modelName: DEFAULT_MODEL.name, | ||
| downloadUrl: DEFAULT_MODEL.url | ||
| }) | ||
|
|
||
| const [downloadedLoraName] = await ensureModel({ | ||
| modelName: LORA_ADAPTER.name, | ||
| downloadUrl: LORA_ADAPTER.url | ||
| }) | ||
|
|
||
| console.log('\n' + '='.repeat(60)) | ||
| console.log('STABLE DIFFUSION 2.1 — LORA INTEGRATION TEST') | ||
| console.log('='.repeat(60)) | ||
| console.log(` Platform : ${platform}`) | ||
| console.log(` Model : ${downloadedModelName}`) | ||
| console.log(` LoRA : ${downloadedLoraName}`) | ||
| console.log(` Models dir: ${modelDir}`) | ||
|
|
||
| const modelPath = path.join(modelDir, downloadedModelName) | ||
| const loraPath = path.join(modelDir, downloadedLoraName) | ||
| t.ok(fs.existsSync(modelPath), 'Model file exists on disk') | ||
| t.ok(fs.existsSync(loraPath), 'LoRA adapter exists on disk') | ||
|
|
||
| const model = new ImgStableDiffusion({ | ||
| files: { | ||
| model: modelPath | ||
| }, | ||
| config: { | ||
| threads: 4, | ||
| device: useCpu ? 'cpu' : 'gpu', | ||
| prediction: 'v' // SD2.1 uses v-prediction | ||
| }, | ||
| logger: console | ||
| }) | ||
|
|
||
| const images = [] | ||
| const progressTicks = [] | ||
|
|
||
| try { | ||
| // ── Load ───────────────────────────────────────────────────────────────── | ||
| console.log('\n=== Loading model ===') | ||
| const tLoad = Date.now() | ||
| await model.load() | ||
| const loadMs = Date.now() - tLoad | ||
| console.log(`Loaded in ${(loadMs / 1000).toFixed(1)}s`) | ||
| t.ok(loadMs < 120000, `Model loaded within 120s (took ${(loadMs / 1000).toFixed(1)}s)`) | ||
|
|
||
| // ── Generate ────────────────────────────────────────────────────────────── | ||
| console.log('\n=== Generating image with LoRA ===') | ||
| const tGen = Date.now() | ||
|
|
||
| const response = await model.run({ | ||
| prompt: 'a bright red sports car parked on a street, clean background, high detail, studio lighting', | ||
| negative_prompt: 'blurry, low quality, watermark', | ||
| lora: loraPath, | ||
| steps: 1, | ||
| width: 512, | ||
| height: 512, | ||
| cfg_scale: 7.5, | ||
| seed: 42 // fixed seed for reproducibility | ||
| }) | ||
|
|
||
| await response | ||
| .onUpdate((data) => { | ||
| if (data instanceof Uint8Array) { | ||
| images.push(data) | ||
| } else if (typeof data === 'string') { | ||
| try { | ||
| const tick = JSON.parse(data) | ||
| if ('step' in tick && 'total' in tick) { | ||
| progressTicks.push(tick) | ||
| } | ||
| } catch (_) {} | ||
| } | ||
| }) | ||
| .await() | ||
|
|
||
| const genMs = Date.now() - tGen | ||
| console.log(`\nGenerated in ${(genMs / 1000).toFixed(1)}s`) | ||
|
|
||
| // ── Assertions ──────────────────────────────────────────────────────────── | ||
| t.ok(progressTicks.length > 0, `Received progress ticks (got ${progressTicks.length})`) | ||
| t.is(images.length, 1, 'Received exactly 1 image') | ||
|
|
||
| const img = images[0] | ||
| t.ok(img instanceof Uint8Array, 'Image is a Uint8Array') | ||
| t.ok(img.length > 0, `Image is non-empty (${img.length} bytes)`) | ||
| t.ok(isPng(img), 'Image has valid PNG magic bytes') | ||
|
|
||
| // Save output for CI artifact upload — filename encodes test origin. | ||
| // Saved to modelDir so mobile has write permission to the same path. | ||
| const outPath = path.join(modelDir, 'generate-image--sd2-lora-txt2img-seed42.png') | ||
| fs.writeFileSync(outPath, img) | ||
| console.log(`\nSaved → ${outPath}`) | ||
|
|
||
| // ── Summary ─────────────────────────────────────────────────────────────── | ||
| console.log('\n' + '='.repeat(60)) | ||
| console.log('TEST SUMMARY') | ||
| console.log('='.repeat(60)) | ||
| console.log(` Load time : ${(loadMs / 1000).toFixed(1)}s`) | ||
| console.log(` Gen time : ${(genMs / 1000).toFixed(1)}s`) | ||
| console.log(` Steps ticks : ${progressTicks.length}`) | ||
| console.log(` Image size : ${img.length} bytes`) | ||
| console.log(' PNG valid : true') | ||
| console.log('='.repeat(60)) | ||
| } finally { | ||
| console.log('\n=== Cleanup ===') | ||
| await model.unload() | ||
| try { | ||
| binding.releaseLogger() | ||
| } catch (_) {} | ||
| console.log('Done.') | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.