Skip to content

Resync with upstream master-1050 and port the ggml patches - #52

Open
danielhanchen wants to merge 72 commits into
masterfrom
resync/upstream-master-1050
Open

danielhanchen wants to merge 72 commits into
masterfrom
resync/upstream-master-1050

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Summary

This merges upstream master-1050-58bb3c1 into the fork and ports the carried ggml patches onto the new ggml submodule commit (135ce1e0). The current Studio pin, master-982-0518b34-uaba3a1c, is 68 upstream commits behind. Among them are leejet#2183 (the model manager reads free memory again before it gives up, also vendored on its own in #50) and leejet#2273 (faster MOSS normalization and staging).

Why now

MOSS-Image-SFT Q8_0 with Qwen3.5-9B Q8_0 through sd-server, 1024x1024, 25 steps. One Colab L4 job, both arms on the same card, with VRAM held down to 7.5 GiB free at load. Each cell ran twice, and repeats agree within 1.7 s.

Free VRAM during the render current pin (master-982-0518b34-uaba3a1c) this branch (7c062d2, patches applied)
7.5 GiB, nothing taken after load 95.5 s / 93.9 s (sampling 82.4 s / 80.9 s) 73.2 s / 73.2 s (sampling 59.9 s / 59.7 s)
2.0 GiB, VRAM taken after load 148.7 s / 150.4 s 87.4 s / 87.8 s (sampling 72.9 s / 72.9 s)

So this branch is about 1.3x faster on the normal path and about 1.7x faster under memory pressure. Both renders are correct images.

In a separate job, a plain upstream build of the same base (58bb3c1, no fork patches) next to this branch took 89.5 s vs 101.7 s with nothing taken, and 89.9 s vs 89.5 s at 2 GiB. Sampling took 76.1 s vs 68.2 s and 75.3 s vs 74.7 s, so the ported patches cost MOSS nothing. At 1.5 GiB free, the pin, plain upstream and this branch all fail the same way (the text encoder segment needs 1543 MB, 1534 MB is free). That case is a real shortfall, not something an engine update fixes. unslothai/unsloth#16815 makes Studio report it as out of memory.

Changes

  • 64fb9d4: merge of master-1050-58bb3c1. Conflicts were resolved keeping both sides (fork-only server and workflow pieces plus upstream's new code).
  • d4ed8d3: upstream moves ROPE_APPLY, SAGE_ATTN, SOL_ATTN and QUANTIZE_I8_CONVROT out of ggml and into stable-diffusion.cpp backend extensions, so several patches in scripts/unsloth/ggml-patches no longer applied:
    • 0002 (long sequence flash attention): reuses upstream's new 128x128 tile for head size 128 and keeps only the head size 64 instance.
    • 0003, 0004: context and op enum position only.
    • 0006 (cuDNN attention): the SAGE_ATTN half is dropped together with the op. cuDNN now serves FLASH_ATTN_EXT only.
    • 0007 (rope_apply PDL race fix): dropped together with the op. The extension kernel that replaces it launches without PDL.
    • 0010 (Metal ROPE_APPLY kernel): dropped. The op no longer exists and ggml's extension registry has no Metal hook, so Metal runs the unfused rope graph again.
    • 0001, 0005, 0008, 0009 and 0011 to 0013 apply unchanged.
  • 7c062d2: the prebuilt workflow now also fetches thirdparty/cutlass. Upstream's CUDA build includes cmake/cutlass.cmake, which stops at configure time without the CUTLASS headers. The source stamp only fetched ggml, so every CUDA leg would have failed. The step keeps only include/ and the license, which keeps the source tarball small.
  • 6f12cd9: the macOS arm64 leg moves from macos-14 to macos-15. GitHub retired the macos-14 image, so that leg was cancelled before it started (The configuration 'macos-14-arm64-us-default' is not supported). master has the same runner label. CMAKE_OSX_DEPLOYMENT_TARGET=14.0 stays, so the binaries still run on macOS 14.

Testing

  • Every patch applies on 135ce1e0. The CPU and CUDA builds of ggml (with test-backend-ops) and of sd-server succeed with the patches applied.
  • CUDA source build of this branch on Colab (sm_89, CUTLASS headers from the submodule): the MOSS renders in the table above.
  • Artifact-only prebuilt run of 6f12cd9 (publish=false, https://github.com/unslothai/stable-diffusion.cpp/actions/runs/42178497743): every leg is green, including macOS arm64 on macos-15. That covers Linux CPU, Vulkan, aarch64, CUDA 12 and CUDA 13; Windows CPU, Vulkan, CUDA 12 and CUDA 13; and macOS arm64 and x86_64. The first CUDA build of the new ggml commit with the CUTLASS kernels is cold and takes 1.3 to 2.8 hours (run 42157974628). With ccache warm, the CUDA legs took 4 to 7 minutes in this run.
  • CPU sd-cli render of MOSS-Image-SFT Q8_0 renders with the patches applied (18.2 s) and without them (66.8 s), image saved both times.

Not verified yet

  • MiniMax-H3 was not rendered on this tree.
  • Metal: because 0010 was dropped, MiniMax-H3 on Apple Silicon may be slower until the kernel is ported to the new extension path.
  • sm100: losing the cuDNN Sage path (0006) may cost MiniMax-H3 speed on Blackwell.

Once it is merged and the nightly publishes, Studio's DEFAULT_TAG can move to the new tag, and #50 becomes redundant.

losewayy and others added 30 commits October 21, 2026 23:11
leejet and others added 18 commits November 2, 2026 00:17
Conflicts resolved keeping both sides:
- build.yml: ours (the mirror's customised upstream CI).
- compute_workspace: upstream now always allocates through the scheduler (gallocr path and
  needs_scheduler removed); the CPU BLAS backend, its thread count, residency accounting and
  scratch release are carried on top of that.
- ggml_extend_backend.cpp: upstream moved sd_backend_dev_buffer_from_host_ptr to ggml_compat.cpp;
  the CPU weight repack helpers and sd_backend_blas_init stay.
- ggml_runner.cpp: upstream's scheduler-only execute_segment (eval callback always set, pre-compute
  hook) plus the BLAS thread count; runner_end also drops the BLAS scratch.
- minimax_h3.hpp: upstream's project_qkv/attend/project_output, INT8-aware MLP scales and chunked
  native-GLU MLP, with the mirror's head-major fused attention, folded MLP and fused segment block
  as fast paths in front; the old segment helpers upstream deleted are dropped.
- minimax_h3_vae.hpp: upstream's native GLU (leejet#2250) for the decoder feed-forward.
- model_manager.cpp: both includes.
- diffusion_engine.cpp: P-H3-Image squeeze plus the temporal tiling retry bookkeeping.
- cmake/ggml.cmake: upstream now resolves the ggml private include dir only for
  SD_USE_UPSTREAM_GGML, which left the fused-op probe reading a non-existent ggml.h and
  silently building MiniMax-H3 unfused; the probe now finds ggml.h from the ggml target.
…submodule)

Upstream master-1050 moves the ggml submodule from dc99877 to 135ce1e (57 commits), which
moves ROPE_APPLY, SAGE_ATTN, SOL_ATTN and QUANTIZE_I8_CONVROT out of ggml into
stable-diffusion.cpp backend extensions. 0002-0004, 0006 and 0007 no longer applied and were
ported by cherry-picking each patch onto the new submodule commit:

- 0002: upstream 0d84d52a added its own 128x128 wide tile for head size 128 (config row,
  instance file, shared-memory helper); the port reuses it, keeps only the head size 64
  instance, and gives the helper an nstages override for the long-sequence variant.
- 0003: context only.
- 0004: the fused ops now follow GLU directly in the op enum (GGML_OP_COUNT 104).
- 0006: the SAGE_ATTN half (sage-attn.cu hook, GGML_CUDA_CUDNN_SAGE) is dropped with the op;
  cuDNN now serves FLASH_ATTN_EXT only.
- 0007: the rope_apply PDL race fix is dropped with the op; the extension kernel that
  replaces it launches without PDL.

0010 (Metal ROPE_APPLY kernel) is dropped: GGML_OP_ROPE_APPLY no longer exists in ggml and
ggml's extension registry has no Metal hook, so Metal runs the unfused rope graph again.
0001, 0005, 0008, 0009 and 0011-0013 apply unchanged.
Upstream's CUDA build includes cmake/cutlass.cmake, which stops at configure time when
thirdparty/cutlass is empty. The source stamp only fetched ggml, so every CUDA leg would fail.
Fetch the submodule shallowly and keep only include/ and the license in the tarball.
GitHub retired the macos-14 image, so the Darwin-macOS-arm64 leg is cancelled before it starts
("The configuration 'macos-14-arm64-us-default' is not supported"). macos-15 is Apple Silicon too,
and CMAKE_OSX_DEPLOYMENT_TARGET=14.0 keeps the binaries running on macOS 14.
@danielhanchen
danielhanchen deployed to release-signing November 6, 2026 11:14 — with GitHub Actions Active
@danielhanchen
danielhanchen marked this pull request as ready for review November 6, 2026 13:52
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-06T13:56:28.795881Z 6f12cd9 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6f12cd92e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 556 to +558
SD_API bool generate_image(sd_ctx_t* sd_ctx,
const sd_img_gen_params_t* sd_img_gen_params,
sd_image_t** images_out,
int* num_images_out);
sd_image_generation_result_t** result_out);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve the existing C ABI for generation entry points

For applications or language bindings compiled against the previous library, replacing the arguments while retaining the same exported C symbol silently breaks the ABI: generate_image now writes an sd_image_generation_result_t* where callers expect an sd_image_t*, and never updates their image count. Such callers can misinterpret the returned allocation or crash when accessing or freeing it. The same incompatibility affects generate_video, upscale, and adetail_image. Keep the existing entry points as compatibility wrappers and expose metadata-bearing results through new symbols. The repository explicitly requires public-header compatibility unless a breaking change is requested.

AGENTS.md reference: AGENTS.md:L74-L76

Useful? React with 👍 / 👎.

Comment on lines +160 to 161
std::string sd_cpp_extra_args_str = extract_and_remove_sd_cpp_extra_args(request.gen_params.prompt);
for (auto& bytes : images_bytes) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restore uploaded-image initialization for OpenAI edits

For /v1/images/edits requests using ordinary SD1/SDXL models, or requests supplying a mask, removing the init-image policy leaves the uploaded image only in ref_images; this builder no longer assigns init_image unless the client supplies a separate prompt-embedded override. There is no replacement fallback in the common request conversion or pipeline: src/pipeline/image.cpp only encodes an initial image when init_image.data is present and otherwise generates a fresh latent. Consequently, previously working image edits ignore the source image, and masked edits cannot preserve its unmasked content. Restore the model- and mask-dependent assignment of the uploaded image to init_image.

Useful? React with 👍 / 👎.

This branch was successfully deployed

1 active deployment
release-signing — 6f12cd92 Deployed Nov 6, 2026 by danielhanchen via win-cpu-x64 #153
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.