Skip to content

[draft] Attempt to implement the Flux Klein bonsai model - #1

Closed
Juste-Leo2 wants to merge 7 commits into
masterfrom
bonsai
Closed

[draft] Attempt to implement the Flux Klein bonsai model#1
Juste-Leo2 wants to merge 7 commits into
masterfrom
bonsai

Conversation

@Juste-Leo2

Copy link
Copy Markdown
Owner

This draft adds the necessary mappings to make the dequantized 4B model work.

Opencode was used, but I am noticing significant differences during inference that I haven't been able to patch using DeepSeek Flash on Opencode.

Here are some avenues to explore:

  • The issue could potentially stem from the scheduler behaving differently.
  • Re-analyze the VAE for the umpteenth time :)

Note: The text encoder seems to use <think> tags, but it doesn't appear to have much of an impact.

I suspect the problem lies either in the denoising process or in the VAE conversion. However, despite comparing it with the Python implementation, I haven't been able to pinpoint the issue yet. Any help would be greatly appreciated!

Here is an inference example:

git clone --recursive https://github.com/Juste-Leo2/stable-diffusion.cpp
cd stable-diffusion.cpp
 
git checkout bonsai

git submodule init
git submodule update

cmake -B build -DCMAKE_BUILD_TYPE=Release -DSD_CUDA=ON

# Compilation
cmake --build build -j

cd /home/leo/stable-diffusion.cpp && timeout 180 ./build/bin/sd-cli --cfg-scale 1 --width 512 --height 512 --steps 4 --seed 42 -p "a cat sitting on a window sill" -o /tmp/vae_fp32_test.png --diffusion-model /tmp/hf_cache_bf16/prism-ml_bonsai-image-ternary-4B-unpacked/transformer/diffusion_pytorch_model.safetensors --vae /tmp/hf_cache_bf16/prism-ml_bonsai-image-ternary-4B-unpacked/vae/diffusion_pytorch_model.safetensors --llm /tmp/hf_cache_bf16/prism-ml_bonsai-image-ternary-4B-unpacked/text_encoder/ 2>&1 | tail -30

Here is the result using the Python reference:

image

Here is the result using the fork:
image

- Add FLUX.2 tensor name mappings for shared modulation, fused
  single-block QKV+MLP, and separate double-block Q/K/V
- Add SelfAttention fused_qkv option for separate Q/K/V weights
- Add sharded safetensors loader for LLM text encoder
- Add image-studio workspace
…Bonsai)

VAE config has 'force_upcast: true', which diffusers uses to cast the
entire VAE to float32 before decode. C++ has no equivalent, causing
yellow/chroma noise in output. Workaround: set expected_type = GGML_TYPE_F32
on all first_stage_model tensor entries before ae.init().
gemini-code-assist[bot]

This comment was marked as off-topic.

@Juste-Leo2

Copy link
Copy Markdown
Owner Author

The conversation below is interesting and might provide some insights for the future (see leejet#1580)

@Juste-Leo2

Copy link
Copy Markdown
Owner Author

I don't think this approach will work. So I'm going to fall back on the method suggested in this discussion leejet#1580 . I'm closing this PR and will open another one for potential operational optimizations.

@Juste-Leo2 Juste-Leo2 closed this May 31, 2026
@Juste-Leo2
Juste-Leo2 deleted the bonsai branch May 31, 2026 19:43
@Juste-Leo2
Juste-Leo2 restored the bonsai branch June 1, 2026 07:47
…compat

Diffusers stores shift/scale in opposite order from what the C model
expects. Swap the two halves after loading to produce correct images.
@Juste-Leo2 Juste-Leo2 reopened this Jun 1, 2026
@Juste-Leo2

Copy link
Copy Markdown
Owner Author

Well, I got this branch working again :)

@Juste-Leo2

Copy link
Copy Markdown
Owner Author

In the end, I think I'll close this branch, even though it works. It's best to keep the original implementation; modifying Safetensor makes the code easier to read.

@Juste-Leo2 Juste-Leo2 closed this Jun 1, 2026
@Juste-Leo2
Juste-Leo2 deleted the bonsai branch June 1, 2026 09:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant