Skip to content

fix: make parameter loading backend-aware - #1828

Merged
leejet merged 1 commit into
leejet:masterfrom
happyyzy:agent/sdcpp-q4-loader-getrows-fix
Jul 29, 2026
Merged

fix: make parameter loading backend-aware#1828
leejet merged 1 commit into
leejet:masterfrom
happyyzy:agent/sdcpp-q4-loader-getrows-fix

Conversation

@happyyzy

Copy link
Copy Markdown
Contributor

Summary

Fix two backend correctness issues in model loading:

  • serialize non-host backend uploads while keeping file I/O and host conversion parallel
  • carry parameter usage ops into ModelManager, select a compatible buffer with supports_op, and enable CPU fallback only for unsupported graph nodes

This follows llama.cpp's per-op buffer selection. It adds no model, tensor-name, or shape special cases and does not modify ggml.

Previously loader workers entered the same backend upload path concurrently, while Q4_0 token embeddings were placed in an OpenCL SoA buffer that rejects GET_ROWS(Q4_0).

Image evidence

Tested on Adreno 830 with OpenCL, GGML_OPENCL_SOA_Q, Adreno kernels, Q4_0 DP4A GEMM, and direct VAE convolution. The image runs used ggml from llama.cpp commit 7e1e28cae36d41fe7bbe9dae7c9625de6565c063.

Case Before After
Flux.2 Klein Q4_0, 512x512 Klein before Klein after
Z-Image Turbo Q4_0, 512x512 Z-Image before Z-Image after

The unpatched default four-thread load aborts in clCreateSubBuffer. The before images use -t 1 only to isolate the independent incompatible-buffer error. Patched runs use the default four loader threads.

Reproduction commands

export GGML_OPENCL_Q4_0_DENSE_DP4A=1

# Flux.2 Klein, before
./bin/sd-cli \
  --diffusion-model "$KLEIN_Q4_0_GGUF" \
  --llm "$QWEN_3_4B_Q4_0_GGUF" \
  --vae "$FLUX2_VAE_SAFETENSORS" \
  -p "a lovely cat" --cfg-scale 1.0 --steps 4 --seed 42 \
  -W 512 -H 512 --vae-conv-direct -t 1 -o klein_before.png -v

# Flux.2 Klein, after
./bin/sd-cli \
  --diffusion-model "$KLEIN_Q4_0_GGUF" \
  --llm "$QWEN_3_4B_Q4_0_GGUF" \
  --vae "$FLUX2_VAE_SAFETENSORS" \
  -p "a lovely cat" --cfg-scale 1.0 --steps 4 --seed 42 \
  -W 512 -H 512 --vae-conv-direct -o klein_after.png -v

# Z-Image Turbo, before
./bin/sd-cli \
  --diffusion-model "$Z_IMAGE_TURBO_Q4_0_GGUF" \
  --llm "$QWEN_3_4B_Q4_0_GGUF" \
  --vae "$Z_IMAGE_AE_SAFETENSORS" \
  -p "a lovely cat" --cfg-scale 1.0 --steps 8 --seed 42 \
  -W 512 -H 512 --vae-conv-direct -t 1 -o zimage_before.png -v

# Z-Image Turbo, after
./bin/sd-cli \
  --diffusion-model "$Z_IMAGE_TURBO_Q4_0_GGUF" \
  --llm "$QWEN_3_4B_Q4_0_GGUF" \
  --vae "$Z_IMAGE_AE_SAFETENSORS" \
  -p "a lovely cat wearing black sunglasses, studio photo" \
  --cfg-scale 1.0 --steps 4 --seed 42 \
  -W 512 -H 512 --vae-conv-direct -o zimage_after.png -v

Checks

git diff --check origin/master..HEAD

cmake -S . -B build-pr -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DGGML_OPENCL=ON \
  -DGGML_OPENCL_USE_ADRENO_KERNELS=ON \
  -DGGML_OPENCL_EMBED_KERNELS=ON

cmake --build build-pr --target sd-cli -j

The Android/OpenCL sd-cli build also passes against the repository's pinned ggml@eced84c8.

Checklist

@happyyzy
happyyzy marked this pull request as ready for review July 29, 2026 03:22
@leejet
leejet merged commit 2993b7f into leejet:master Jul 29, 2026
8 of 9 checks passed
@happyyzy happyyzy mentioned this pull request Jul 29, 2026
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.

2 participants