mirror of
https://github.com/leejet/stable-diffusion.cpp.git
synced 2026-09-21 21:47:49 -05:00
Compare commits
86
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0657e6cdfe | ||
|
|
c678dfe704 | ||
|
|
b56c68617d | ||
|
|
187b2561ea | ||
|
|
15f335daa5 | ||
|
|
b8248a869c | ||
|
|
008ca5b492 | ||
|
|
137f7409bb | ||
|
|
1330cebae8 | ||
|
|
17860c0e45 | ||
|
|
275ab58e01 | ||
|
|
d32b4e893b | ||
|
|
9982c9caae | ||
|
|
3e037a81e4 | ||
|
|
2ea8aff7ef | ||
|
|
adcac69650 | ||
|
|
656a1354c3 | ||
|
|
269e726015 | ||
|
|
cc515a01f9 | ||
|
|
3161505fe8 | ||
|
|
59c23bce0d | ||
|
|
07a85c74cb | ||
|
|
f9ddc0f388 | ||
|
|
4964abdfc5 | ||
|
|
42d6c0ab92 | ||
|
|
5a5400bf0c | ||
|
|
ca37fad89a | ||
|
|
0bd72f075a | ||
|
|
4a7da26b73 | ||
|
|
9a977388a8 | ||
|
|
44dd13716d | ||
|
|
7f410a3793 | ||
|
|
5ebce93342 | ||
|
|
7f986a9d73 | ||
|
|
e06b205384 | ||
|
|
3191b23d4b | ||
|
|
e95ab96997 | ||
|
|
b68d58624d | ||
|
|
14eddb32b1 | ||
|
|
469fc49bb7 | ||
|
|
6b47fec013 | ||
|
|
d04e8950c1 | ||
|
|
9cdb6b6fc0 | ||
|
|
31ab2b2e08 | ||
|
|
d8fb10c029 | ||
|
|
80bac2d5fc | ||
|
|
dbb611264e | ||
|
|
462d675018 | ||
|
|
6c57cc3b38 | ||
|
|
6b3edaaf32 | ||
|
|
40e605f3f1 | ||
|
|
9029655a54 | ||
|
|
d9b6e27e9f | ||
|
|
134c8212de | ||
|
|
2540a4fc25 | ||
|
|
dc4000d9f8 | ||
|
|
c797899732 | ||
|
|
afd5306d88 | ||
|
|
2c929495ab | ||
|
|
be0e34480d | ||
|
|
50d6405683 | ||
|
|
0a565f2950 | ||
|
|
12ee60dc02 | ||
|
|
97d2990807 | ||
|
|
16304cc3fd | ||
|
|
760717a060 | ||
|
|
88b044be7f | ||
|
|
1706b32813 | ||
|
|
58b6cb6b0c | ||
|
|
6100d8339b | ||
|
|
de298c225b | ||
|
|
fabe481212 | ||
|
|
06c359f17a | ||
|
|
bcc7e29568 | ||
|
|
487de75c96 | ||
|
|
b4f1fd6d7f | ||
|
|
c6beeef355 | ||
|
|
bfbef5b7e6 | ||
|
|
ea7f0c87cf | ||
|
|
b4e67d1221 | ||
|
|
db99efdd6d | ||
|
|
eb7f35ca49 | ||
|
|
50062a4bba | ||
|
|
8457624101 | ||
|
|
10378f42db | ||
|
|
e31a86ce91 |
@@ -6,7 +6,9 @@ body:
|
||||
- type: markdown
|
||||
attributes:
|
||||
value: |
|
||||
Please use this template and include as many details as possible to help us reproduce and fix the issue.
|
||||
Before submitting a bug report, please read the [Troubleshooting guide](https://github.com/leejet/stable-diffusion.cpp/blob/master/docs/troubleshooting.md) and try the steps relevant to your problem.
|
||||
|
||||
If the problem persists, complete this form and include what you tried and the results, along with enough details to help us reproduce and fix the issue.
|
||||
- type: textarea
|
||||
id: commit
|
||||
attributes:
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
contact_links:
|
||||
- name: Troubleshooting
|
||||
url: https://github.com/leejet/stable-diffusion.cpp/blob/master/docs/troubleshooting.md
|
||||
about: Read the troubleshooting guide first. If the problem persists, submit a bug report.
|
||||
@@ -23,6 +23,7 @@ on:
|
||||
"**/*.c",
|
||||
"**/*.cpp",
|
||||
"**/*.cu",
|
||||
"ggml",
|
||||
"examples/server/frontend",
|
||||
"examples/server/frontend/**",
|
||||
]
|
||||
@@ -40,6 +41,7 @@ on:
|
||||
"**/*.c",
|
||||
"**/*.cpp",
|
||||
"**/*.cu",
|
||||
"ggml",
|
||||
"examples/server/frontend",
|
||||
"examples/server/frontend/**",
|
||||
]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
build*/
|
||||
cmake-build-*/
|
||||
test/
|
||||
tests/
|
||||
.vscode/
|
||||
.idea/
|
||||
.cache/
|
||||
|
||||
@@ -118,7 +118,8 @@ documentation.
|
||||
6. Run the narrowest useful build, test, or inspection command available.
|
||||
|
||||
Follow `CONTRIBUTING.md` for formatting, naming, PR expectations, dependency
|
||||
update policy, and security rules.
|
||||
update policy, and security rules. For tokenizer additions, follow its embedded-data
|
||||
allowlist and default to an external `tokenizer.json`.
|
||||
|
||||
---
|
||||
|
||||
|
||||
+23
-14
@@ -11,10 +11,11 @@ endif()
|
||||
if (MSVC)
|
||||
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
|
||||
add_compile_definitions(_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING)
|
||||
# /MP is MSVC-only: icx rejects it outright once offloading is enabled.
|
||||
add_compile_options(
|
||||
$<$<COMPILE_LANGUAGE:C>:/MP>
|
||||
$<$<AND:$<COMPILE_LANGUAGE:C>,$<C_COMPILER_ID:MSVC>>:/MP>
|
||||
$<$<COMPILE_LANGUAGE:C>:/utf-8>
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/MP>
|
||||
$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/MP>
|
||||
$<$<COMPILE_LANGUAGE:CXX>:/utf-8>
|
||||
)
|
||||
endif()
|
||||
@@ -94,6 +95,8 @@ option(SD_MUSA "sd: musa backend" OFF)
|
||||
option(SD_BUILD_SHARED_LIBS "sd: build shared libs" OFF)
|
||||
option(SD_BUILD_SHARED_GGML_LIB "sd: build ggml as a separate shared lib" OFF)
|
||||
option(SD_USE_SYSTEM_GGML "sd: use system-installed GGML library" OFF)
|
||||
option(SD_USE_UPSTREAM_GGML "sd: build with upstream GGML instead of the patched GGML extensions" OFF)
|
||||
set(SD_GGML_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/ggml" CACHE PATH "sd: ggml source directory (also supplies private headers for system ggml)")
|
||||
#option(SD_BUILD_SERVER "sd: build server example" ON)
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
@@ -228,6 +231,8 @@ file(GLOB SD_LIB_SOURCES CONFIGURE_DEPENDS
|
||||
"src/model/*/*.h"
|
||||
"src/model/*/*.cpp"
|
||||
"src/model/*/*.hpp"
|
||||
"src/pipeline/*.h"
|
||||
"src/pipeline/*.cpp"
|
||||
"src/runtime/*.h"
|
||||
"src/runtime/*.cpp"
|
||||
"src/runtime/*.hpp"
|
||||
@@ -289,6 +294,12 @@ else()
|
||||
add_library(${SD_LIB} STATIC ${SD_LIB_SOURCES})
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
target_compile_options(${SD_LIB} PRIVATE $<$<COMPILE_LANGUAGE:CXX>:/bigobj>)
|
||||
# ggml backends can throw C++ exceptions through their C API.
|
||||
target_compile_options(${SD_LIB} PRIVATE $<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CXX_COMPILER_ID:MSVC>>:/EHsc->)
|
||||
endif()
|
||||
|
||||
if(APPLE)
|
||||
sd_set_macos_rpaths(${SD_LIB})
|
||||
endif()
|
||||
@@ -316,23 +327,21 @@ if (NOT SD_USE_SYSTEM_GGML)
|
||||
endif()
|
||||
|
||||
# deps
|
||||
# Only add ggml if it hasn't been added yet
|
||||
if (NOT TARGET ggml)
|
||||
if (SD_USE_SYSTEM_GGML)
|
||||
find_package(ggml REQUIRED)
|
||||
if (NOT ggml_FOUND)
|
||||
message(FATAL_ERROR "System-installed GGML library not found.")
|
||||
endif()
|
||||
add_library(ggml ALIAS ggml::ggml)
|
||||
else()
|
||||
add_subdirectory(ggml)
|
||||
endif()
|
||||
endif()
|
||||
include(cmake/ggml.cmake)
|
||||
|
||||
add_subdirectory(thirdparty)
|
||||
|
||||
target_sources(${SD_LIB} PRIVATE $<TARGET_OBJECTS:zip>)
|
||||
target_link_libraries(${SD_LIB} PUBLIC ggml)
|
||||
find_package(Threads REQUIRED)
|
||||
target_link_libraries(${SD_LIB} PRIVATE Threads::Threads)
|
||||
target_link_libraries(${SD_LIB} PRIVATE onig sd-utf8proc)
|
||||
if (SD_CUDA)
|
||||
find_package(CUDAToolkit REQUIRED)
|
||||
# Keep the driver stub on downstream link lines when no driver is installed.
|
||||
target_link_libraries(${SD_LIB} PUBLIC CUDA::cuda_driver)
|
||||
set_property(SOURCE src/core/ggml_extend_backend.cpp APPEND PROPERTY COMPILE_DEFINITIONS SD_USE_CUDA)
|
||||
endif()
|
||||
target_include_directories(${SD_LIB} PUBLIC . src include)
|
||||
target_include_directories(${SD_LIB} PRIVATE src/core)
|
||||
target_include_directories(${SD_LIB} PUBLIC . thirdparty)
|
||||
|
||||
+18
-2
@@ -14,6 +14,8 @@ If you want to update a third-party dependency, please open an issue first inste
|
||||
|
||||
Keep each PR focused on one clear change. Large or overly complex PRs are harder to review and may not be merged.
|
||||
|
||||
Do not include test code or test scripts in commits or PRs. Keep them local and report verification results in the PR description.
|
||||
|
||||
Follow Conventional Commit-style subjects seen in history: `feat:`, `fix:`, `refactor:`, `ci:`, `docs:`, `chore:`. Keep subjects imperative and scoped.
|
||||
|
||||
PRs should include:
|
||||
@@ -35,17 +37,31 @@ Naming conventions:
|
||||
- In `PascalCase` names, preserve common abbreviations in uppercase, for example `SD`, `API`, `HTTP`, `JSON`, `RGB`, `VAE`, `TAE`, `LoRA`, and `WebP`.
|
||||
- Use `snake_case` for functions, methods, variables, and file names unless an existing API requires a different style.
|
||||
- Use a trailing underscore for private data member names, for example `hidden_size_` or `tokenizer_`.
|
||||
- Use `.h` for C and C++ header files. Do not introduce new `.hpp` headers.
|
||||
- Use `.hpp` for model headers under `src/model/`, including new model headers. Do not rename these headers to `.h`. Use `.h` for other C and C++ header files.
|
||||
- Use macro-based header include guards instead of `#pragma once`.
|
||||
- Format header include guards as `__SD_{PATH}__`, where `{PATH}` is the header path in uppercase snake case without the file extension. For example, `src/sample.h` should use `__SD_SAMPLE_H__`.
|
||||
- Do not introduce anonymous namespaces in new or modified code; prefer `static` file-local functions/variables or an explicit named namespace when scoping is needed.
|
||||
- In `class`/`struct` definitions, place data members before member functions unless an existing type already clearly follows a different pattern.
|
||||
- Keep `test_*.cpp` / `test_*.py` naming for tests.
|
||||
|
||||
Some older code in the project may not fully follow the current conventions. Please do not submit PRs that only rewrite existing code to match style rules.
|
||||
|
||||
When adding or modifying model implementations, follow the model config and weight detection conventions in [docs/model_config.md](docs/model_config.md).
|
||||
|
||||
## Tokenizer Data
|
||||
|
||||
New model integrations must use an external `tokenizer.json` by default. Do not
|
||||
embed new vocabularies or merge tables solely for less widely used models;
|
||||
these tables increase the binary size for every user.
|
||||
|
||||
The embedded-data allowlist is CLIP, T5/UMT5, Qwen 2/3, Mistral, and Gemma 3/4.
|
||||
Models may reuse an existing embedded tokenizer when its vocabulary and behavior
|
||||
match their text encoder. Gemma 2 and GPT-OSS require external JSON files.
|
||||
|
||||
Adding to this allowlist requires maintainer approval, supported by the model's
|
||||
usage, reuse across models, and measured binary-size cost. Document the matching
|
||||
JSON and CLI option for models that require an external tokenizer, and fail
|
||||
initialization clearly when it is missing.
|
||||
|
||||
## AI-Assisted Contributions
|
||||
|
||||
AI tools may be used to assist development, but contributors are responsible for the quality and correctness of the submitted code.
|
||||
|
||||
@@ -15,6 +15,9 @@ API and command-line option may change frequently.***
|
||||
|
||||
## 🔥Important News
|
||||
|
||||
* **2026/09/20** 🚀 stable-diffusion.cpp adds **Day-0 support for Qwen-Image-2.1**
|
||||
* **2026/08/20** 🚀 stable-diffusion.cpp now supports **LTX-2.5**
|
||||
* **2026/08/04** 🚀 stable-diffusion.cpp adds **Day-1 support for MiniMax-H3**
|
||||
* **2026/06/25** 🚀 stable-diffusion.cpp now supports **Krea2**
|
||||
* **2026/06/04** 🚀 stable-diffusion.cpp now supports **Ideogram4**
|
||||
* **2026/05/31** 🚀 stable-diffusion.cpp now supports **PiD**
|
||||
@@ -45,10 +48,12 @@ API and command-line option may change frequently.***
|
||||
- [Chroma](./docs/chroma.md)
|
||||
- [Chroma1-Radiance](./docs/chroma_radiance.md)
|
||||
- [Qwen Image](./docs/qwen_image.md)
|
||||
- [Qwen Image 2.1](./docs/qwen_image_2.1.md)
|
||||
- [PiD](./docs/pid.md)
|
||||
- [LongCat Image](./docs/longcat_image.md)
|
||||
- [Z-Image](./docs/z_image.md)
|
||||
- [MiniT2I](./docs/minit2i.md)
|
||||
- [SenseNova U1.5](./docs/sensenova_u1.md)
|
||||
- [Ovis-Image](./docs/ovis_image.md)
|
||||
- [Anima](./docs/anima.md)
|
||||
- [ERNIE-Image](./docs/ernie_image.md)
|
||||
@@ -58,19 +63,22 @@ API and command-line option may change frequently.***
|
||||
- [SeFi-Image](./docs/sefi_image.md)
|
||||
- [HiDream-O1-Image](./docs/hidream_o1_image.md)
|
||||
- [Ideogram4](./docs/ideogram4.md)
|
||||
- [LLaDA-Image](./docs/llada_image.md)
|
||||
- [Image Edit Models](./docs/edit.md)
|
||||
- [FLUX.1-Kontext-dev](./docs/kontext.md)
|
||||
- [Qwen Image Edit series](./docs/qwen_image_edit.md)
|
||||
- [LongCat Image Edit](./docs/longcat_image.md)
|
||||
- [Boogu Image Edit](./docs/boogu_image.md)
|
||||
- [Mage-Flow-Edit](./docs/mage_flow.md#image-editing)
|
||||
- [LLaDA-Image Edit](./docs/llada_image.md#image-editing)
|
||||
- Video Models
|
||||
- [Wan2.1/Wan2.2](./docs/wan.md)
|
||||
- [LTX-2.3](./docs/ltx2.md)
|
||||
- [MiniMax-H3](./docs/minimax_h3.md)
|
||||
- [LTX-2.3/LTX-2.5](./docs/ltx2.md)
|
||||
- [HunyuanVideo 1.5](./docs/hunyuan_video.md)
|
||||
- [LingBot-Video](./docs/lingbot_video.md)
|
||||
- [PhotoMaker](./docs/photo_maker.md) support.
|
||||
- [IP-Adapter](./docs/ip_adapter.md) support (SD 1.5 and SDXL)
|
||||
- [IP-Adapter](./docs/ip_adapter.md) support (SD 1.5 and SDXL, including Plus)
|
||||
- Control Net support with SD 1.5
|
||||
- [ADetailer](./docs/adetailer.md)
|
||||
- LoRA support, same as [stable-diffusion-webui](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#lora)
|
||||
@@ -144,12 +152,14 @@ For runtime and parameter backend placement, see the [backend selection guide](.
|
||||
|
||||
## More Guides
|
||||
|
||||
- [Troubleshooting](./docs/troubleshooting.md)
|
||||
- [Backend selection](./docs/backend.md)
|
||||
- [RPC](./docs/rpc.md)
|
||||
- [LoRA](./docs/lora.md)
|
||||
- [LCM/LCM-LoRA](./docs/lcm.md)
|
||||
- [Docker](./docs/docker.md)
|
||||
- [Quantization and GGUF](./docs/quantization_and_gguf.md)
|
||||
- [INT8 convrot safetensors](./docs/int8_convrot.md)
|
||||
- [Inference acceleration via caching](./docs/caching.md)
|
||||
|
||||
## Bindings
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 478 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 437 KiB |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 634 KiB |
@@ -0,0 +1,27 @@
|
||||
if(NOT TARGET ggml AND NOT TARGET ggml::ggml)
|
||||
if(SD_USE_SYSTEM_GGML)
|
||||
find_package(ggml REQUIRED)
|
||||
else()
|
||||
add_subdirectory("${SD_GGML_SOURCE_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/ggml")
|
||||
endif()
|
||||
endif()
|
||||
if(NOT TARGET ggml)
|
||||
add_library(ggml ALIAS ggml::ggml)
|
||||
endif()
|
||||
|
||||
get_target_property(sd_ggml_imported ggml IMPORTED)
|
||||
if(sd_ggml_imported)
|
||||
set(sd_ggml_private_include "${SD_GGML_SOURCE_DIR}/src")
|
||||
else()
|
||||
get_target_property(sd_ggml_private_include ggml SOURCE_DIR)
|
||||
endif()
|
||||
if(NOT EXISTS "${sd_ggml_private_include}/ggml-impl.h")
|
||||
message(FATAL_ERROR "Set SD_GGML_SOURCE_DIR to the source tree matching the selected ggml library (ggml-impl.h is required).")
|
||||
endif()
|
||||
target_include_directories(${SD_LIB} PRIVATE "${sd_ggml_private_include}")
|
||||
set_property(TARGET ${SD_LIB} PROPERTY SD_GGML_PRIVATE_INCLUDE_DIR "${sd_ggml_private_include}")
|
||||
|
||||
if(SD_USE_UPSTREAM_GGML)
|
||||
target_compile_definitions(${SD_LIB} PUBLIC SD_USE_UPSTREAM_GGML)
|
||||
message(WARNING "Using upstream GGML: INT8 tensorwise/convrot is disabled and FP8 weights are converted to F16 at load time. Some operators may be unsupported and performance may be lower than with patched GGML.")
|
||||
endif()
|
||||
@@ -10,6 +10,10 @@ set(SD_BIN_DIR "@PACKAGE_SD_BIN_INSTALL_DIR@")
|
||||
|
||||
include(CMakeFindDependencyMacro)
|
||||
find_dependency(ggml REQUIRED HINTS "${SD_LIB_DIR}/cmake")
|
||||
find_dependency(Threads REQUIRED)
|
||||
if(@SD_CUDA@)
|
||||
find_dependency(CUDAToolkit REQUIRED)
|
||||
endif()
|
||||
|
||||
if(NOT TARGET stable-diffusion)
|
||||
find_library(stable-diffusion_LIBRARY stable-diffusion
|
||||
@@ -22,12 +26,16 @@ if(NOT TARGET stable-diffusion)
|
||||
set_target_properties(stable-diffusion
|
||||
PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${SD_INCLUDE_DIR}"
|
||||
INTERFACE_LINK_LIBRARIES "ggml::ggml"
|
||||
INTERFACE_LINK_LIBRARIES "ggml::ggml;Threads::Threads"
|
||||
IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
|
||||
IMPORTED_LOCATION "${stable-diffusion_LIBRARY}"
|
||||
INTERFACE_COMPILE_FEATURES "c_std_11;cxx_std_17"
|
||||
POSITION_INDEPENDENT_CODE ON)
|
||||
|
||||
if(@SD_CUDA@)
|
||||
set_property(TARGET stable-diffusion APPEND PROPERTY INTERFACE_LINK_LIBRARIES CUDA::cuda_driver)
|
||||
endif()
|
||||
|
||||
if(SD_SHARED_LIB)
|
||||
target_compile_definitions(stable-diffusion
|
||||
INTERFACE SD_BUILD_SHARED_LIB)
|
||||
|
||||
@@ -7,5 +7,5 @@ Name: stable-diffusion
|
||||
Description: Diffusion model(SD,Flux,Wan,Qwen Image,Z-Image,...) inference in pure C/C++
|
||||
Version: @SDCPP_BUILD_VERSION@
|
||||
Libs: -L${libdir} -lstable-diffusion
|
||||
Libs.private: -lggml -lggml-base
|
||||
Libs.private: -lggml -lggml-base @CMAKE_THREAD_LIBS_INIT@
|
||||
Cflags: -I${includedir}
|
||||
|
||||
+1
-1
@@ -79,7 +79,7 @@ Low-VRAM streaming (verified with a 2 GiB cap on RTX 3060):
|
||||
.\bin\Release\sd-cli.exe -M vid_gen \
|
||||
--model ..\models\checkpoints\realisticVisionV60B1.safetensors \
|
||||
--motion-module ..\models\animatediff\mm_sd15_v3.safetensors \
|
||||
--max-vram 2.0 --stream-layers --diffusion-fa \
|
||||
--max-vram 2.0 --diffusion-fa \
|
||||
-p "photo of coastline, rocks, storm weather, wind, waves, lightning" \
|
||||
--cfg-scale 8.0 --sampling-method euler --scheduler discrete \
|
||||
-H 384 -W 384 --video-frames 8 --fps 8 --steps 20 -s 42 \
|
||||
|
||||
+98
-27
@@ -5,7 +5,8 @@
|
||||
- `--backend` selects the runtime backend used to execute model graphs.
|
||||
- `--params-backend` selects where model parameters are kept.
|
||||
|
||||
If `--params-backend` is not set, parameters use the same backend as their module runtime backend.
|
||||
If `--params-backend` is not set, auto-fit chooses parameter placement. With
|
||||
`--auto-fit off`, parameters use the same backend as their module runtime backend.
|
||||
|
||||
## Syntax
|
||||
|
||||
@@ -41,7 +42,11 @@ sd-cli -m model.safetensors -p "a cat" --backend cuda0 --params-backend disk
|
||||
sd-cli -m model.safetensors -p "a cat" --backend diffusion=cuda0,vae=vulkan0 --max-vram cuda0=6,vulkan0=2
|
||||
```
|
||||
|
||||
The budget applies to every module running on that backend.
|
||||
The value is a shared per-device budget for managed weights and registered
|
||||
runner compute/cache buffers. Live free memory can lower the effective limit
|
||||
for each graph run. Driver contexts and allocations made outside the managed
|
||||
model runners are not part of this accounting, so it is not a hard physical
|
||||
VRAM cap.
|
||||
|
||||
Module names are case-insensitive. Hyphens and underscores in module names are ignored, so `clip_vision`, `clip-vision`, and `clipvision` are equivalent.
|
||||
|
||||
@@ -79,9 +84,10 @@ with `--params-backend diffusion=disk`, released directly from) its own device;
|
||||
an explicit assignment such as `te=cpu` keeps the parameters on that backend
|
||||
and stages each range to its device on demand.
|
||||
|
||||
Layer split cannot be combined with `--max-vram` graph-cut segmentation or
|
||||
`--stream-layers` for the split module; those are single-device mechanisms and
|
||||
are disabled for it.
|
||||
Layer split uses the fixed graph-cut plan to assign blocks across devices, but
|
||||
single-device segmented execution and next-segment prefetch are disabled for
|
||||
the split module. `--max-vram` can still provide the per-device limits used by
|
||||
layer split and auto-fit.
|
||||
|
||||
Use `--list-devices` to see the device names available on the system.
|
||||
|
||||
@@ -104,43 +110,104 @@ Compared to a layer split this uses all GPUs within every layer (instead of
|
||||
sequentially device by device) at the cost of a cross-device reduction per
|
||||
matmul - usually the faster option when the devices have fast interconnect.
|
||||
|
||||
Row split requires backend support for split buffers and is currently
|
||||
available on CUDA only; on other backends (or when the listed devices belong
|
||||
to different backend registries) the module falls back to a layer split.
|
||||
Row split requires a compatible split-buffer export from the linked GGML
|
||||
backend. If it is unavailable (or the listed devices belong to different backend
|
||||
registries), the module falls back to a layer split.
|
||||
Embeddings, normalization weights, biases and other non-block tensors stay in
|
||||
regular buffers on the main device.
|
||||
|
||||
Row-split execution can use graph segments, but split weights are loaded
|
||||
synchronously instead of using the normal single-device prefetch path. Because
|
||||
GGML does not expose exact shard allocation sizes, the managed budget currently
|
||||
counts a split buffer's full size on each participating device. This is a
|
||||
conservative bound and can reject otherwise feasible layouts.
|
||||
|
||||
Direct ("immediately") LoRA application cannot patch row-split tensors; with
|
||||
`--split-mode row` the automatic LoRA mode selects runtime application, and an
|
||||
explicit `--lora-apply-mode immediately` skips the split tensors with a
|
||||
warning.
|
||||
|
||||
## Automatic placement (`--auto-fit`)
|
||||
## Automatic placement (`--auto-fit on|off`)
|
||||
|
||||
`--auto-fit` derives the `diffusion` / `te` / `vae` placements from the model
|
||||
metadata and the per-device memory budgets, then feeds them into the same
|
||||
backend assignment mechanism described above (the chosen specs are printed).
|
||||
`--backend` and `--params-backend` are ignored while auto-fit is enabled.
|
||||
`--auto-fit` requires `on` or `off` and defaults to `on` when omitted.
|
||||
Explicit `--params-backend` assignments disable auto-fit,
|
||||
regardless of argument order, even with `--auto-fit on`.
|
||||
|
||||
Auto-fit preserves explicit `--backend` assignments, including per-module
|
||||
assignments and device lists. For modules without a runtime assignment, it chooses
|
||||
the GPU with the largest available memory budget (the first device on a tie).
|
||||
It then derives parameter placements from the model metadata, each module's
|
||||
compute devices, and the remaining memory budgets. The chosen backend
|
||||
specifications are printed.
|
||||
|
||||
```shell
|
||||
sd-cli -m model.safetensors -p "a cat" --auto-fit
|
||||
sd-cli -m model.safetensors -p "a cat" --auto-fit --max-vram cuda0=8,cuda1=14
|
||||
sd-cli -m model.safetensors -p "a cat" --auto-fit --split-mode row
|
||||
sd-cli -m model.safetensors -p "a cat" --auto-fit on
|
||||
sd-cli -m model.safetensors -p "a cat" --auto-fit on --max-vram cuda0=8,cuda1=14
|
||||
sd-cli -m model.safetensors -p "a cat" --backend cuda0
|
||||
sd-cli -m model.safetensors -p "a cat" --backend diffusion=cuda0,te=cpu,vae=cuda1
|
||||
sd-cli -m model.safetensors -p "a cat" --auto-fit off
|
||||
```
|
||||
|
||||
Budgets reuse `--max-vram`: a positive per-device value caps what auto-fit
|
||||
plans with on that device, a negative value means "free memory minus that many
|
||||
GiB", and with no budget set each device's free memory minus a 512 MiB margin
|
||||
is used. (The same values still drive graph-cut segmented execution for
|
||||
modules that end up on a single device.)
|
||||
is used. These resolved GPU budgets, including the safety margin, also drive
|
||||
the runner's graph-cut capacity checks.
|
||||
|
||||
When everything fits resident, components are simply spread across the
|
||||
available GPUs. When it does not, auto-fit switches to time-share mode: the
|
||||
heavy components get `disk` params residency (loaded for their phase, freed
|
||||
after), and a component too large for any single device is split across all
|
||||
GPUs with the layer/row split mechanism (`--split-mode` selects which, layer
|
||||
by default). Components that fit nowhere fall back to the CPU. If a VAE decode
|
||||
still runs out of memory, tiling is enabled and the decode retried once.
|
||||
Runtime capacity checks also leave 512 MiB of currently free device memory for
|
||||
backend scratch buffers and pipelines, including with explicit backend assignments.
|
||||
They cap stale free-memory reports by the device's total memory minus tracked
|
||||
resident allocations and reject reports that exceed the device's total memory.
|
||||
|
||||
Components are considered in `diffusion`, `te`, `vae` order so that repeatedly
|
||||
used diffusion weights have priority. Each component's weights use the first
|
||||
storage location with enough remaining budget:
|
||||
|
||||
1. The component's compute GPU, leaving estimated space for computation and weight staging.
|
||||
2. CPU RAM, reserving the larger of 2 GiB or 10% of available RAM for other work.
|
||||
3. Another GPU, choosing the one with the largest remaining budget that fits.
|
||||
4. Disk, reloading weights on demand.
|
||||
|
||||
GPU cache space follows the same component priority. Before a lower-priority
|
||||
component can become permanently resident, the planner leaves room for the full
|
||||
weights and estimated compute space of higher-priority offloaded components.
|
||||
If offloaded diffusion already needs the entire main GPU budget, TE and VAE also
|
||||
use offloaded parameters. Their GPU copies can then be released after their
|
||||
phases, leaving more room to reuse diffusion weights across sampling steps.
|
||||
CPU parameter residency allows GPU weight caching; it does not force every
|
||||
weight to be copied again at every step.
|
||||
|
||||
RAM and GPU budgets are shared across components. Each component uses a single
|
||||
parameter backend; several other GPUs' capacities are not combined to store
|
||||
one component. If available RAM cannot be queried, RAM residency is skipped.
|
||||
Weights stored on another GPU are copied to the component's compute devices for
|
||||
execution. CPU modules use RAM or disk. Compute reserves and cache priority are
|
||||
accounted for separately on each device, so a CPU module does not reserve GPU
|
||||
space. Storage on another module's GPU also leaves room for that module's work.
|
||||
|
||||
Auto-fit does not select multi-GPU layer/row computation itself. Explicit device
|
||||
lists and `--split-mode` still control that computation. Before the runners have
|
||||
built their split plans, auto-fit conservatively counts the full component size
|
||||
on each listed GPU when checking residency and cache space. This can offload
|
||||
parameters even when a split layout would fit; use `--auto-fit off` to keep the
|
||||
default split-device parameter placement.
|
||||
|
||||
For example, a diffusion model whose full weights exceed the main GPU's budget
|
||||
can use `--backend diffusion=cuda0 --params-backend diffusion=cpu` when RAM is
|
||||
sufficient. Automatic graph segmentation can then load the required weights
|
||||
for each segment and reclaim idle GPU copies. `--disable-segmented-compute`
|
||||
still disables segmentation.
|
||||
|
||||
Initial compute reserves are estimates (2 GiB for diffusion and text encoders,
|
||||
1 GiB for VAE); higher-priority placements also leave staging space for the
|
||||
largest weight tensor of each lower-priority offloaded component. Actual segment
|
||||
weights, compute buffers and caches must
|
||||
still fit the runner's capacity checks. Offloading weights does not guarantee
|
||||
that every resolution or frame count will fit, and auto-fit does not change a
|
||||
component to CPU computation solely because its full weights exceed VRAM.
|
||||
If a VAE decode fails, decoding retries with spatial tiling even when `--auto-fit`
|
||||
is off; supported video decoders try temporal tiling first and can then add
|
||||
spatial tiling. Spatial retries use half-size tiles along each latent dimension.
|
||||
|
||||
## Modules
|
||||
|
||||
@@ -192,7 +259,7 @@ sd-cli -m model.safetensors -p "a cat" --backend cuda0 --params-backend disk
|
||||
|
||||
This runs all modules on `cuda0`, reloads parameters from the model file as needed, and releases those parameter buffers after use.
|
||||
|
||||
`disk` is never selected implicitly. If `--params-backend` is not set, parameters use the runtime backend.
|
||||
Outside `--auto-fit`, `disk` is never selected implicitly. If `--params-backend` is not set, parameters use the runtime backend.
|
||||
|
||||
Per-module assignments can be mixed:
|
||||
|
||||
@@ -241,4 +308,8 @@ The example CLI/server still accepts these older CPU placement flags as compatib
|
||||
|
||||
Because this default is inserted first, later explicit `--params-backend` entries can still override it, for example `--offload-to-cpu --params-backend te=disk` keeps non-TE parameters on CPU and reloads TE parameters from disk.
|
||||
|
||||
Library callers should set `backend` and `params_backend` directly. The old CPU/offload fields are no longer part of the C API. Explicit `--backend` and `--params-backend` assignments are preferred for new commands.
|
||||
Library callers should set `backend` and `params_backend` directly. `sd_ctx_params_init()`
|
||||
enables `auto_fit` by default; a nonempty `params_backend` assignment disables it.
|
||||
The `backend` assignment constrains auto-fit's compute placement.
|
||||
The old CPU/offload fields are no longer part of the C API. Explicit `--backend` and
|
||||
`--params-backend` assignments are preferred for new commands.
|
||||
|
||||
@@ -16,6 +16,40 @@ git submodule init
|
||||
git submodule update
|
||||
```
|
||||
|
||||
## Selecting a GGML source tree
|
||||
|
||||
By default, sd.cpp builds the patched GGML submodule in `ggml/`. To build with
|
||||
an upstream GGML checkout instead, enable `SD_USE_UPSTREAM_GGML` and set
|
||||
`SD_GGML_SOURCE_DIR`:
|
||||
|
||||
```shell
|
||||
cmake -S . -B build-upstream -DSD_USE_UPSTREAM_GGML=ON -DSD_GGML_SOURCE_DIR=../ggml-upstream
|
||||
cmake --build build-upstream --config Release
|
||||
```
|
||||
|
||||
The selected source tree supplies both the library and its private headers.
|
||||
Backend options such as `-DSD_CUDA=ON` apply to the selected tree as usual.
|
||||
|
||||
`SD_USE_UPSTREAM_GGML` defaults to `OFF`, which enables the patched GGML
|
||||
extensions. Set it to `ON` when using upstream GGML; it selects the compatibility
|
||||
mode and does not download or replace the GGML source tree. Upstream mode keeps
|
||||
the original FP8 safetensors handling: FP8 tensors are converted to F16 at load
|
||||
time (one byte per element in the file, two in RAM and VRAM). INT8
|
||||
tensorwise/convrot is disabled and its model files are rejected with an explicit
|
||||
error. FP8 GGUF files, FP8 weight type requests and tensor type rules are also
|
||||
rejected; no automatic conversion is performed.
|
||||
|
||||
Upstream GGML may lack some operators and performance optimizations provided by
|
||||
the patched version. A warning is emitted during CMake configuration and when
|
||||
creating an inference context. Ordinary floating-point and shared GGML
|
||||
quantization types remain available, subject to backend operator support.
|
||||
|
||||
`SD_USE_SYSTEM_GGML=ON` instead links an installed GGML CMake package, located
|
||||
with `ggml_DIR` or `CMAKE_PREFIX_PATH`. In that mode, `SD_GGML_SOURCE_DIR` must
|
||||
point to the matching source tree for private headers. The installed library
|
||||
must use the same ABI settings as sd.cpp, including `GGML_MAX_NAME`.
|
||||
Set `SD_USE_UPSTREAM_GGML=ON` as well if the installed package is upstream GGML.
|
||||
|
||||
## WebP and WebM Support in Examples
|
||||
|
||||
The example applications (`examples/cli` and `examples/server`) use `libwebp` to support WebP image I/O, and `examples/cli` can also use `libwebm` for `.webm` video output. Both are enabled by default. WebM output currently reuses `libwebp` to encode each frame as VP8 before muxing with `libwebm`.
|
||||
@@ -56,6 +90,10 @@ cmake --build . --config Release
|
||||
|
||||
## Build with CUDA
|
||||
|
||||
Native SageAttention is included when using CUDA with patched GGML
|
||||
(`SD_USE_UPSTREAM_GGML=OFF`).
|
||||
See [SageAttention](sage_attention.md) for GPU requirements and `--sage-attn` usage.
|
||||
|
||||
This provides GPU acceleration using NVIDIA GPU. Make sure to have the CUDA toolkit installed. You can download it from your Linux distro's package manager (e.g. `apt install nvidia-cuda-toolkit`) or from here: [CUDA Toolkit](https://developer.nvidia.com/cuda-downloads). Recommended to have at least 4 GB of VRAM.
|
||||
|
||||
```shell
|
||||
|
||||
@@ -17,6 +17,7 @@ Depending on the architecture, different models handle reference images differen
|
||||
| [**Boogu Image Edit**](./boogu_image.md) | `z_image_omni` |
|
||||
| **Krea2 (Community Edit LoRAs)** | `krea2_ostris_edit` |
|
||||
| [**Mage-Flow-Edit**](./mage_flow.md#image-editing) | `mage_flow` |
|
||||
| [**LLaDA-Image**](./llada_image.md#image-editing) | `llada_image` |
|
||||
| **Anima (Community Edit LoRAs)** | `cosmos_reference` |
|
||||
|
||||
Stable-diffusion.spp also supports basic Unet-based editing models like instruct-pix2pix or CosXL-Edit. This document is not about those.
|
||||
|
||||
+16
-1
@@ -11,7 +11,22 @@
|
||||
- Download Qwen3-VL-8B-Instruct
|
||||
- gguf: https://huggingface.co/unsloth/Qwen3-VL-8B-Instruct-GGUF/tree/main
|
||||
|
||||
## Convert weights
|
||||
## Use original FP8 weights
|
||||
|
||||
The original Ideogram4 FP8 safetensors can be loaded directly. FP8 tensors stay
|
||||
at one byte per element in RAM and VRAM. Backends that cannot multiply FP8
|
||||
weights directly cast only the active layer to a temporary BF16 tensor during
|
||||
execution; the loader does not expand the entire checkpoint to BF16.
|
||||
|
||||
With `SD_USE_UPSTREAM_GGML=ON`, FP8 tensors are converted to F16 at load time
|
||||
instead (two bytes per element in RAM and VRAM).
|
||||
|
||||
Use `ideogram4_fp8.safetensors` and `ideogram4_uncond_fp8.safetensors` directly
|
||||
with `--diffusion-model` and `--uncond-diffusion-model`, respectively.
|
||||
|
||||
## Optional conversion for quantization
|
||||
|
||||
The following conversion is only needed when creating a quantized GGUF model.
|
||||
|
||||
fp8 scale -> bf16
|
||||
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
# INT8 Convrot Safetensors
|
||||
|
||||
sd.cpp can load and execute ComfyUI `int8_tensorwise` safetensors with `convrot` metadata directly. The stored INT8 weights are not converted to another weight type at load time.
|
||||
|
||||
This requires the INT8 tensorwise/convrot extensions in the patched GGML.
|
||||
Builds with `SD_USE_UPSTREAM_GGML=ON` reject these files during loading.
|
||||
|
||||
## Checkpoint format
|
||||
|
||||
Each quantized linear module contains the following tensors:
|
||||
|
||||
- `<module>.weight`: an I8 weight matrix.
|
||||
- `<module>.weight_scale`: one floating-point scale for each output row. ComfyUI's two-dimensional `[out_features, 1]` representation is normalized to a one-dimensional tensor while loading.
|
||||
- `<module>.comfy_quant`: a U8 tensor containing the JSON quantization configuration.
|
||||
|
||||
A supported configuration has this form:
|
||||
|
||||
```json
|
||||
{
|
||||
"format": "int8_tensorwise",
|
||||
"convrot": true,
|
||||
"convrot_groupsize": 256
|
||||
}
|
||||
```
|
||||
|
||||
The convrot group size must be a power of four and must divide the input feature dimension. The commonly used configuration is H256, with `convrot_groupsize` set to `256`.
|
||||
|
||||
## How INT8 convrot works
|
||||
|
||||
Convrot combines an offline rotation of the weights with the same rotation of the activations at runtime. The rotation uses a normalized regular Hadamard matrix constructed recursively from
|
||||
|
||||
```text
|
||||
[ 1 1 1 -1 ]
|
||||
[ 1 1 -1 1 ]
|
||||
H4 = [ 1 -1 1 1 ] / 2
|
||||
[-1 1 1 1 ]
|
||||
```
|
||||
|
||||
For a group size `G = 4^n`, the transform is the normalized Kronecker power of `H4`. It is applied independently to every contiguous group of `G` input features. The resulting block-diagonal rotation matrix `R` is orthogonal and symmetric, so `R R^T = I`.
|
||||
|
||||
For an original floating-point linear layer
|
||||
|
||||
```text
|
||||
Y = X W^T + b
|
||||
```
|
||||
|
||||
the checkpoint stores a rotated weight matrix `W_rot = W R`, quantized per output row. At runtime sd.cpp computes `X_rot = X R`. Ignoring quantization error,
|
||||
|
||||
```text
|
||||
X_rot W_rot^T = X R (W R)^T = X R R^T W^T = X W^T
|
||||
```
|
||||
|
||||
The rotation therefore preserves the linear operation. Its purpose is to spread isolated large values across each feature group, reducing the effect of outliers on tensorwise INT8 quantization.
|
||||
|
||||
### Weight quantization
|
||||
|
||||
The rotated weights are quantized offline with one scale per output row:
|
||||
|
||||
```text
|
||||
s_w[o] = max_i(abs(W_rot[o, i])) / 127
|
||||
Q_w[o, i] = clamp(round(W_rot[o, i] / s_w[o]), -127, 127)
|
||||
```
|
||||
|
||||
`Q_w` is stored in `<module>.weight`, and `s_w` is stored in `<module>.weight_scale`.
|
||||
|
||||
### Runtime activation quantization
|
||||
|
||||
For every activation row, sd.cpp applies the group-wise Hadamard rotation and then calculates one dynamic scale across the entire rotated row:
|
||||
|
||||
```text
|
||||
s_x[r] = max_i(abs(X_rot[r, i])) / 127
|
||||
Q_x[r, i] = clamp(round(X_rot[r, i] / s_x[r]), -127, 127)
|
||||
```
|
||||
|
||||
The matrix multiplication accumulates into signed 32-bit integers:
|
||||
|
||||
```text
|
||||
A[r, o] = sum_i(Q_x[r, i] * Q_w[o, i])
|
||||
```
|
||||
|
||||
The floating-point output is reconstructed as
|
||||
|
||||
```text
|
||||
Y[r, o] ~= A[r, o] * s_x[r] * s_w[o] + b[o]
|
||||
```
|
||||
|
||||
The packed runtime activation tensor contains the I8 activation rows and their floating-point row scales. Linear layers that share the same input and convrot group size reuse this packed tensor, avoiding repeated rotation and activation quantization within the graph.
|
||||
|
||||
## Backend support
|
||||
|
||||
- CPU provides the portable regular Hadamard, activation quantization, INT8 matrix multiplication, and scale restoration implementations.
|
||||
- NVIDIA CUDA devices with compute capability 7.5 or newer use the native accelerated path. For H256, CUDA fuses the rotation, row-wise maximum reduction, and activation quantization. It uses cuBLAS for I8 x I8 to I32 GEMM and a CUDA kernel for scale restoration and bias addition.
|
||||
- Vulkan and other GPU backends do not currently have dedicated INT8 convrot kernels. They use the backend scheduler to fall back to CPU, which is expected to be substantially slower than the CUDA path.
|
||||
|
||||
LoRA adapters are applied at runtime without modifying the INT8 weights. The INT8 convrot path computes the base linear output, while LoRA, LoHa, LoKr, and raw weight-difference adapters compute their output corrections from the original, unrotated activation and add them to the base output. `--lora-apply-mode auto` selects this path for models containing INT8 tensorwise weights. If `immediately` is requested, sd.cpp falls back to runtime application because merging an adapter would require dequantizing and rotating its weight update, then recalculating the per-row scales and requantizing the result.
|
||||
|
||||
The dedicated CUDA convrot activation path currently requires a group size of `256`; other supported group sizes use CPU execution.
|
||||
|
||||
## Example
|
||||
|
||||
ComfyUI INT8 convrot safetensors can be passed to `--diffusion-model` without conversion:
|
||||
|
||||
```powershell
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\krea2_turbo_int8_convrot.safetensors --llm ..\models\text_encoders\Qwen3-VL-4B-Instruct-Q4_K_M.gguf --vae ..\models\vae\wan_2.1_vae.safetensors -p "a lovely cat holding a sign says 'krea2.cpp'" --steps 8 --cfg-scale 1 --diffusion-fa -v --offload-to-cpu
|
||||
```
|
||||
@@ -11,6 +11,10 @@ through a decoupled cross-attention added to every attn2 layer of the
|
||||
UNet. It composes with Control Net, so a reference image (appearance) and
|
||||
an OpenPose hint (pose) can be combined in a single generation.
|
||||
|
||||
Both the classic adapters and the higher-fidelity **Plus** adapters are
|
||||
supported; see [Plus variants](#plus-variants) below. The variant is
|
||||
detected from the weight file, so the same options work for both.
|
||||
|
||||
## Required weights
|
||||
|
||||
1. A base SD 1.5 or SDXL model.
|
||||
@@ -21,6 +25,11 @@ an OpenPose hint (pose) can be combined in a single generation.
|
||||
[h94/IP-Adapter](https://huggingface.co/h94/IP-Adapter):
|
||||
- SD 1.5: `models/ip-adapter_sd15.safetensors`
|
||||
- SDXL: `sdxl_models/ip-adapter_sdxl_vit-h.safetensors`
|
||||
- SD 1.5 Plus: `models/ip-adapter-plus_sd15.safetensors`
|
||||
- SDXL Plus: `sdxl_models/ip-adapter-plus_sdxl_vit-h.safetensors`
|
||||
|
||||
The Plus files (`ip-adapter-plus_*`) are used exactly like the classic
|
||||
ones; see [Plus variants](#plus-variants).
|
||||
|
||||
## Options
|
||||
|
||||
@@ -45,6 +54,29 @@ sd-cli -m ..\models\sdxl.safetensors --clip_vision ..\models\clip_vision_h.safet
|
||||
The SDXL VAE decode at 1024x1024 is memory heavy; add `--vae-tiling` (and
|
||||
`--offload-to-cpu`) on GPUs with limited VRAM.
|
||||
|
||||
## Plus variants
|
||||
|
||||
The Plus adapters (`ip-adapter-plus_sd15`, `ip-adapter-plus_sdxl_vit-h`)
|
||||
replace the small linear image projection with a Resampler (a
|
||||
Perceiver-style module with learned latent queries). Instead of pooling the
|
||||
CLIP-Vision output into one vector, the Resampler attends over the full grid
|
||||
of penultimate CLIP-Vision hidden states and emits more image tokens (16
|
||||
instead of 4). The result transfers finer detail and layout from the
|
||||
reference, at a small extra cost in the image-projection step.
|
||||
|
||||
No extra flags are needed. The variant is detected from the weight file (the
|
||||
Resampler's `image_proj.latents` tensor), and every Resampler dimension is
|
||||
read from the tensor shapes, so the same `--ip-adapter`,
|
||||
`--ip-adapter-image`, and `--ip-adapter-strength` options apply. Plus
|
||||
composes with Control Net in the same way as the classic adapters.
|
||||
|
||||
```
|
||||
sd-cli -m ..\models\sd_v1.5.safetensors --clip_vision ..\models\clip_vision_h.safetensors --ip-adapter ..\models\ip-adapter-plus_sd15.safetensors --ip-adapter-image ..\assets\reference.png --ip-adapter-strength 0.8 -p "a woman, best quality" -n "lowres, bad anatomy" --cfg-scale 7 --steps 30 --sampling-method dpm++2m --scheduler karras -W 512 -H 512
|
||||
```
|
||||
|
||||
The startup log line `IP-Adapter: 16 image tokens` (versus `4` for the
|
||||
classic adapters) confirms a Plus file was loaded.
|
||||
|
||||
## Combining with Control Net
|
||||
|
||||
Add the usual Control Net options to keep the reference appearance while
|
||||
|
||||
+6
-2
@@ -12,13 +12,17 @@ Lens uses a Lens diffusion transformer, the FLUX.2 VAE, and GPT-OSS-20B as the L
|
||||
- safetensors: https://huggingface.co/black-forest-labs/FLUX.2-dev/tree/main
|
||||
- Download GPT-OSS-20B
|
||||
- gguf: https://huggingface.co/unsloth/gpt-oss-20b-GGUF/tree/main
|
||||
- Download GPT-OSS-20B tokenizer.json
|
||||
- https://huggingface.co/openai/gpt-oss-20b/tree/main
|
||||
|
||||
Lens and Lens Turbo require an external GPT-OSS `tokenizer.json` matching the text encoder checkpoint. Save it as `tokenizer_gpt_oss.json` and pass it with `--tokenizer`; the tokenizer is not embedded in sd.cpp. See [JSON tokenizers](tokenizers.md) for CLI and C API usage.
|
||||
|
||||
## Examples
|
||||
|
||||
### Lens
|
||||
|
||||
```
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\lens_bf16.safetensors --llm "..\models\text_encoders\gpt-oss-20b-UD-Q8_K_XL.gguf" --vae ..\models\vae\flux2_ae.safetensors --cfg-scale 5.0 -p "A crystal dragon soaring through an aurora borealis sky, its entire body made of transparent faceted crystal refracting the green and purple aurora light into rainbow spectra, ice particles trailing from its wings, high fantasy digital art" --diffusion-fa -v
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\lens_bf16.safetensors --llm "..\models\text_encoders\gpt-oss-20b-UD-Q8_K_XL.gguf" --tokenizer ..\models\tokenizers\tokenizer_gpt_oss.json --vae ..\models\vae\flux2_ae.safetensors --cfg-scale 5.0 -p "A crystal dragon soaring through an aurora borealis sky, its entire body made of transparent faceted crystal refracting the green and purple aurora light into rainbow spectra, ice particles trailing from its wings, high fantasy digital art" --diffusion-fa -v
|
||||
```
|
||||
|
||||
<img width="256" alt="Lens example" src="../assets/lens/example.png" />
|
||||
@@ -26,7 +30,7 @@ Lens uses a Lens diffusion transformer, the FLUX.2 VAE, and GPT-OSS-20B as the L
|
||||
### Lens Turbo
|
||||
|
||||
```
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\lens_turbo_bf16.safetensors --llm "..\models\text_encoders\gpt-oss-20b-UD-Q8_K_XL.gguf" --vae ..\models\vae\flux2_ae.safetensors --cfg-scale 1.0 -p "A crystal dragon soaring through an aurora borealis sky, its entire body made of transparent faceted crystal refracting the green and purple aurora light into rainbow spectra, ice particles trailing from its wings, high fantasy digital art" --diffusion-fa -v --steps 4
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\lens_turbo_bf16.safetensors --llm "..\models\text_encoders\gpt-oss-20b-UD-Q8_K_XL.gguf" --tokenizer ..\models\tokenizers\tokenizer_gpt_oss.json --vae ..\models\vae\flux2_ae.safetensors --cfg-scale 1.0 -p "A crystal dragon soaring through an aurora borealis sky, its entire body made of transparent faceted crystal refracting the green and purple aurora light into rainbow spectra, ice particles trailing from its wings, high fantasy digital art" --diffusion-fa -v --steps 4
|
||||
```
|
||||
|
||||
<img width="256" alt="Lens Turbo example" src="../assets/lens/turbo_example.png" />
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
# How to Use
|
||||
|
||||
LLaDA-Image is a 6B text-to-image and instruction-guided editing model. The denoiser is a
|
||||
Lumina2/Z-Image-style NextDiT conditioned by a LLaDA2-MoE diffusion-LLM text encoder, and it
|
||||
reuses the Flux.2 VAE. Two checkpoints are published: a 50-step base model and
|
||||
LLaDA-Image-Turbo, a 4-step distilled model.
|
||||
|
||||
## Download weights
|
||||
|
||||
Four components are required: a transformer, a text encoder, a VAE, and a connectors file
|
||||
holding the QueryFormer, the text projection and, for editing, the SigVQ image encoder.
|
||||
|
||||
The two published checkpoints are **not** interchangeable. LLaDA-Image-Turbo and LLaDA-Image
|
||||
ship different transformers, text encoders, QueryFormers and text projections; only the VAE,
|
||||
the SigVQ encoder and the tokenizer are shared. Mixing the two produces degraded output rather
|
||||
than a clean error, so keep each checkpoint's files together.
|
||||
|
||||
Both need an external LLaDA2 `tokenizer.json`, which is not embedded in sd.cpp and is the same
|
||||
file for either checkpoint. Take `tokenizer/tokenizer.json` from either repository and pass it
|
||||
with `--tokenizer`. See [JSON tokenizers](tokenizers.md) for CLI and C API usage.
|
||||
|
||||
### LLaDA-Image-Turbo (4 steps)
|
||||
|
||||
Converted transformer, text encoder and pre-merged connectors are at
|
||||
https://huggingface.co/fszontagh/LLaDA-Image-Turbo-GGUF:
|
||||
|
||||
- `llada-image-turbo-f16.gguf`
|
||||
- `llada-image-turbo-text_encoder-q8_0.gguf`
|
||||
- `llada-image-turbo-connectors.safetensors` for text to image, or
|
||||
`llada-image-turbo-connectors-edit.safetensors`, which also carries the SigVQ encoder that
|
||||
editing needs.
|
||||
|
||||
Other quantizations of the transformer and the text encoder are in the same repository.
|
||||
|
||||
The VAE comes from the original repository,
|
||||
https://huggingface.co/inclusionAI/LLaDA-Image-Turbo: `vae/diffusion_pytorch_model.safetensors`,
|
||||
referred to below as `llada_vae.safetensors`.
|
||||
|
||||
### LLaDA-Image (50 steps)
|
||||
|
||||
Converted transformer, text encoder and pre-merged connectors are at
|
||||
https://huggingface.co/fszontagh/LLaDA-Image-GGUF:
|
||||
|
||||
- `llada-image-f16.gguf`
|
||||
- `llada-image-text_encoder-q8_0.gguf`
|
||||
- `llada-image-connectors.safetensors` for text to image, or
|
||||
`llada-image-connectors-edit.safetensors`, which also carries the SigVQ encoder that editing
|
||||
needs.
|
||||
|
||||
Other quantizations of the transformer and the text encoder are in the same repository.
|
||||
|
||||
The VAE comes from the original repository,
|
||||
https://huggingface.co/inclusionAI/LLaDA-Image, and is the same file as the Turbo one.
|
||||
|
||||
### Converting the weights yourself
|
||||
|
||||
The transformer has to go in through `--diffusion-model` so that its tensor names keep the
|
||||
prefix the loader expects, while the text encoder goes in through `-m`:
|
||||
|
||||
```bash
|
||||
./bin/sd-cli -M convert --diffusion-model transformer/diffusion_pytorch_model.safetensors.index.json \
|
||||
-o llada-image-f16.gguf --type f16
|
||||
./bin/sd-cli -M convert -m text_encoder/model.safetensors.index.json \
|
||||
-o llada-image-text_encoder-q8_0.gguf --type q8_0
|
||||
```
|
||||
|
||||
### Building the connector file yourself
|
||||
|
||||
`--embeddings-connectors` takes one file, so the QueryFormer, the text projection and
|
||||
(for editing) the SigVQ encoder have to be combined into a single Safetensors file, each
|
||||
tensor name prefixed with its component name. Leaving `sigvq` out skips loading the 2.6 GB
|
||||
encoder:
|
||||
|
||||
```python
|
||||
from safetensors.torch import load_file, save_file
|
||||
|
||||
merged = {}
|
||||
for prefix, path in [
|
||||
("queryformer", "queryformer/diffusion_pytorch_model.safetensors"),
|
||||
("text_projection", "text_projection/diffusion_pytorch_model.safetensors"),
|
||||
("sigvq", "sigvq/diffusion_pytorch_model.safetensors"),
|
||||
]:
|
||||
for name, tensor in load_file(path).items():
|
||||
merged[f"{prefix}.{name}"] = tensor
|
||||
save_file(merged, "llada_connectors.safetensors")
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Text to image
|
||||
|
||||
```bash
|
||||
./bin/sd-cli \
|
||||
--diffusion-model /path/to/llada-image-turbo-f16.gguf \
|
||||
--llm /path/to/llada-image-turbo-text_encoder-q8_0.gguf \
|
||||
--tokenizer /path/to/tokenizer.json \
|
||||
--vae /path/to/llada_vae.safetensors \
|
||||
--embeddings-connectors /path/to/llada-image-turbo-connectors.safetensors \
|
||||
--prompt "a lovely cat holding a sign says 'llada.cpp'" \
|
||||
--width 1024 \
|
||||
--height 1024 \
|
||||
--steps 4 \
|
||||
--cfg-scale 1.0 \
|
||||
--seed 42 \
|
||||
--output output.png
|
||||
```
|
||||
|
||||
<img width="256" alt="LLaDA-Image example" src="../assets/llada_image/example.png" />
|
||||
|
||||
### Image editing
|
||||
|
||||
```bash
|
||||
./bin/sd-cli \
|
||||
--diffusion-model /path/to/llada-image-turbo-f16.gguf \
|
||||
--llm /path/to/llada-image-turbo-text_encoder-q8_0.gguf \
|
||||
--tokenizer /path/to/tokenizer.json \
|
||||
--vae /path/to/llada_vae.safetensors \
|
||||
--embeddings-connectors /path/to/llada-image-turbo-connectors-edit.safetensors \
|
||||
--ref-image /path/to/input.png \
|
||||
--prompt "change the sign text to 'sd.cpp'" \
|
||||
--width 1024 \
|
||||
--height 1024 \
|
||||
--steps 4 \
|
||||
--cfg-scale 1.0 \
|
||||
--diffusion-fa \
|
||||
--output output.png
|
||||
```
|
||||
|
||||
<img width="256" alt="LLaDA-Image edit example" src="../assets/llada_image/edit_example.png" />
|
||||
|
||||
See [edit.md](./edit.md) for the shared reference-image options. LLaDA-Image uses the
|
||||
`llada_image` preset by default, resizing the reference image to the output width and height
|
||||
before VAE encoding. SigVQ uses bilinear resizing to half the output resolution and inputs
|
||||
normalized to `[-1, 1]`. CFG keeps the source latent in both branches and uses SigVQ features
|
||||
only in the positive branch. Editing requires connectors that include the SigVQ weights.
|
||||
|
||||
## Notes
|
||||
|
||||
- Use 4 steps and `--cfg-scale 1.0` for LLaDA-Image-Turbo; the guidance is distilled away, so
|
||||
a higher CFG degrades output and doubles the text encoder cost. The 50-step base model uses
|
||||
`--steps 50 --cfg-scale 5`.
|
||||
- Width and height are rounded up to a multiple of 16. For editing the reference pipeline
|
||||
requires them to be divisible by 32.
|
||||
- Edit the 50-step base model at 1024x1024. At 512x512 it returns the reference image almost
|
||||
unchanged instead of applying the instruction; LLaDA-Image-Turbo edits correctly at both.
|
||||
- Editing runs the reference and the target in one sequence, so it needs roughly twice the
|
||||
tokens of text to image at the same size. On 12 GB, editing at 1024x1024 needs
|
||||
`--diffusion-fa`; without it the diffusion graph does not fit.
|
||||
- The weights total about 16 GB, but segmented execution streams them, so a much smaller
|
||||
budget works. At 512x512, `--max-vram 6` costs almost nothing over unconstrained execution,
|
||||
and `--max-vram 3` still produces byte-identical output at roughly 2.5x the time.
|
||||
- `--scheduler` defaults to `llada_image`, which reproduces the reference Kumaraswamy sigma
|
||||
grid. `--extra-sample-args uniform=1` selects the uniform grid instead.
|
||||
- Prompt templating is handled automatically; pass a plain description.
|
||||
- VQ-conditioned generation (`generation_mode="vq"`, where the text encoder decodes image
|
||||
tokens before diffusion) is not implemented.
|
||||
+63
-1
@@ -1,7 +1,17 @@
|
||||
# How to Use
|
||||
|
||||
Both LTX-2.3 and LTX-2.5 are supported. The two share a transformer, video VAE and audio
|
||||
VAE architecture; LTX-2.5 drops the video FFN biases, adds a learned keyframe
|
||||
absolute-position embedding, and pairs with a Gemma 4 text encoder instead of Gemma 3.
|
||||
Everything is detected from the weights, so the command lines differ only in which files
|
||||
you pass.
|
||||
|
||||
# LTX-2.3
|
||||
|
||||
## Download weights
|
||||
|
||||
### LTX-2.3
|
||||
|
||||
- Download LTX-2.3
|
||||
- safetensors: https://huggingface.co/Kijai/LTX2.3_comfy/tree/main/diffusion_models
|
||||
- gguf: https://huggingface.co/unsloth/LTX-2.3-GGUF/tree/main
|
||||
@@ -16,6 +26,30 @@
|
||||
- Download LTX spatial latent upscaler
|
||||
- safetensors: https://huggingface.co/Lightricks/LTX-2.3/resolve/main/ltx-2.3-spatial-upscaler-x2-1.1.safetensors
|
||||
|
||||
### LTX-2.5
|
||||
|
||||
- Download LTX-2.5
|
||||
- safetensors: https://huggingface.co/Lightricks/LTX-2.5/tree/main/diffusion_models
|
||||
- gguf: https://huggingface.co/vantagewithai/LTX-2.5-GGUF/tree/main
|
||||
- Download the text encoder. This is a Gemma 4 12B fine-tuned for LTX with the text
|
||||
projection bundled in, so no separate `--embeddings-connectors` file is needed. Google's
|
||||
stock Gemma 4 is not a substitute.
|
||||
- safetensors: https://huggingface.co/Lightricks/LTX-2.5/blob/main/text_encoders/gemma4-12b-with-proj-ltx-2.5-bf16.safetensors
|
||||
- Download the video vae. Use the **conv** variant: `ltx-2.5-video-vae-conv-bf16.safetensors`.
|
||||
The default `ltx-2.5-video-vae-bf16.safetensors` is a diffusion decoder, which is not
|
||||
implemented here.
|
||||
- safetensors: https://huggingface.co/Lightricks/LTX-2.5/blob/main/vae/ltx-2.5-video-vae-conv-bf16.safetensors
|
||||
- Download the audio vae
|
||||
- safetensors: https://huggingface.co/Lightricks/LTX-2.5/blob/main/vae/ltx-2.5-audio-vae-bf16.safetensors
|
||||
- Download the LTX spatial latent upscaler
|
||||
- safetensors: https://huggingface.co/Lightricks/LTX-2.5/blob/main/latent_upscale_models/ltx-2.5-latent-spatial-upscaler-x2-bf16-1.0.safetensors
|
||||
|
||||
To run the text encoder quantized, convert it once with sd-cli:
|
||||
|
||||
```
|
||||
.\bin\Release\sd-cli.exe -M convert -m ..\models\text_encoders\gemma4-12b-with-proj-ltx-2.5-bf16.safetensors --type q8_0 -o ..\models\text_encoders\gemma4-12b-with-proj-ltx-2.5-Q8_0.gguf
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### LTX-2.3 dev T2V
|
||||
@@ -74,4 +108,32 @@ By default, the hires refine pass uses the main sampler and scheduler, then trim
|
||||
src="../assets/ltx2/hires_i2v.webm"
|
||||
controls
|
||||
muted
|
||||
style="max-width: 100%; height: auto;"></video>
|
||||
style="max-width: 100%; height: auto;"></video>
|
||||
|
||||
### LTX-2.5 dev T2V
|
||||
|
||||
```
|
||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\ltx-2.5-22b-dev-transformer-Q8_0.gguf --vae ..\models\vae\ltx-2.5-video-vae-conv-bf16.safetensors --audio-vae ..\models\vae\ltx-2.5-audio-vae-bf16.safetensors --llm ..\models\text_encoders\gemma4-12b-with-proj-ltx-2.5-Q8_0.gguf -p "A wide aerial shot of a red vintage convertible driving along a coastal cliff road at sunset, waves crashing below" --cfg-scale 3.0 --sampling-method euler -v -n "worst quality, low quality, blurry, distorted, artifacts" -W 1280 -H 720 --diffusion-fa --offload-to-cpu --video-frames 121 --fps 24 -o t2v.webm
|
||||
```
|
||||
|
||||
### LTX-2.5 dev I2V
|
||||
|
||||
```
|
||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\ltx-2.5-22b-dev-transformer-Q8_0.gguf --vae ..\models\vae\ltx-2.5-video-vae-conv-bf16.safetensors --audio-vae ..\models\vae\ltx-2.5-audio-vae-bf16.safetensors --llm ..\models\text_encoders\gemma4-12b-with-proj-ltx-2.5-Q8_0.gguf -p "a lovely cat blinking slowly, gentle camera push in" --cfg-scale 3.0 --sampling-method euler -v -W 1280 -H 720 --diffusion-fa --offload-to-cpu --video-frames 121 -i ..\assets\ernie_image\turbo_example.png -o i2v.webm
|
||||
```
|
||||
|
||||
### LTX-2.5 spatial latent upscale
|
||||
|
||||
Works exactly like the LTX-2.3 upscaler described below; put
|
||||
`ltx-2.5-latent-spatial-upscaler-x2-bf16-1.0.safetensors` under `--hires-upscalers-dir` and
|
||||
pass its name without path or extension to `--hires-upscaler`.
|
||||
|
||||
```
|
||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\ltx-2.5-22b-dev-transformer-Q8_0.gguf --vae ..\models\vae\ltx-2.5-video-vae-conv-bf16.safetensors --audio-vae ..\models\vae\ltx-2.5-audio-vae-bf16.safetensors --llm ..\models\text_encoders\gemma4-12b-with-proj-ltx-2.5-Q8_0.gguf --hires-upscalers-dir ..\models\latent_upscale_models --hires-upscaler ltx-2.5-latent-spatial-upscaler-x2-bf16-1.0 --hires --hires-steps 6 -p "a lovely cat" --cfg-scale 3.0 --sampling-method euler -v -W 640 -H 360 --diffusion-fa --offload-to-cpu --video-frames 121 -o hires_t2v.webm
|
||||
```
|
||||
|
||||
## Not implemented
|
||||
|
||||
- The diffusion video decoder (`ltx-2.5-video-vae-bf16.safetensors`). Use the conv VAE.
|
||||
- The temporal latent upscaler and the duration head (`--auto-duration`); pass
|
||||
`--video-frames` explicitly.
|
||||
@@ -0,0 +1,96 @@
|
||||
# MiniMax-H3
|
||||
|
||||
MiniMax-H3 jointly generates video and stereo audio with a packed diffusion
|
||||
transformer. The implementation supports text-to-audio-video (T2VA), optional
|
||||
first-frame conditioning (I2VA), first/last-frame conditioning (FL2VA), and
|
||||
image/video/audio reference conditioning (Ref2VA).
|
||||
|
||||
## Model files
|
||||
|
||||
Pass the four MiniMax-H3 components separately:
|
||||
|
||||
- `--diffusion-model`: MiniMax-H3 diffusion transformer
|
||||
- `--vae`: MiniMax-H3 video VAE
|
||||
- `--audio-vae`: MiniMax-H3 audio VAE
|
||||
- `--llm`: the MiniMax-H3 Qwen3-VL-32B text encoder checkpoint
|
||||
|
||||
The text encoder must be the MiniMax-H3 variant: Qwen3-VL-32B truncated to 50
|
||||
language layers and exported without the final language-model normalization.
|
||||
Its Qwen3-VL vision tower, including the three DeepStack mergers, must also be
|
||||
present. If the vision tower is stored separately, pass it with `--llm_vision`.
|
||||
|
||||
Both the original time-embedder DiT and the smaller AdaLN curve-table variant
|
||||
are detected from their weights.
|
||||
|
||||
### Download weights
|
||||
|
||||
- Download minimax_h3_fl2va/minimax_h3_ref2va
|
||||
- safetensors: https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/diffusion_models
|
||||
- gguf: https://huggingface.co/leejet/MiniMax-H3-GGUF/tree/main
|
||||
- Download qwen3vl_32b_minimax_h3
|
||||
- safetensors: https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/text_encoders
|
||||
- gguf: https://huggingface.co/leejet/MiniMax-H3-GGUF/tree/main
|
||||
- Download vae
|
||||
- safetensors: https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/vae
|
||||
- Download audio vae
|
||||
- safetensors: https://huggingface.co/Comfy-Org/MiniMax-H3/tree/main/vae
|
||||
|
||||
## Text-to-audio-video
|
||||
|
||||
```sh
|
||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\minimax_h3_fl2va-Q4_K_M.gguf --vae ..\models\vae\minimax_h3_video_vae_fp16.safetensors --audio-vae ..\models\vae\minimax_h3_audio_vae_fp32.safetensors --llm ..\models\text_encoders\qwen3vl_32b_minimax_h3-Q4_K_M.gguf -p "A cute American Shorthair silver tabby kitten surfs on a tropical ocean wave, riding a white surfboard with the clear text 'sd.cpp' on it. Cinematic tracking shot, realistic water, bright sunlight, smooth motion, and consistent character appearance. Add upbeat tropical surf-rock background music with cheerful drums and guitar, synchronized with the kitten’s energetic surfing." --cfg-scale 1.0 -v -W 864 -H 480 --diffusion-fa --offload-to-cpu --rng cpu --fps 24 --video-frames 56
|
||||
```
|
||||
|
||||
<video src=../assets/minimax-h3/t2av.mp4 controls="controls" muted="muted" type="video/mp4"></video>
|
||||
|
||||
Omitting `--audio-vae` still runs the joint diffusion model but produces video without a
|
||||
decoded audio track.
|
||||
|
||||
## First/last-frame conditioning
|
||||
|
||||
Add `--init-img` for I2VA, or both `--init-img` and `--end-img` for FL2VA:
|
||||
|
||||
```sh
|
||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\minimax_h3_fl2va-Q4_K_M.gguf --vae ..\models\vae\minimax_h3_video_vae_fp16.safetensors --audio-vae ..\models\vae\minimax_h3_audio_vae_fp32.safetensors --llm ..\models\text_encoders\qwen3vl_32b_minimax_h3-Q4_K_M.gguf -p "a lovely cat" -i ..\assets\ernie_image\turbo_example.png --cfg-scale 1.0 -v -W 864 -H 480 --diffusion-fa --offload-to-cpu --rng cpu --fps 24 --video-frames 56
|
||||
```
|
||||
|
||||
<video src=../assets/minimax-h3/i2av.mp4 controls="controls" muted="muted" type="video/mp4"></video>
|
||||
|
||||
## Reference-to-audio-video conditioning
|
||||
|
||||
Ref2VA accepts any combination of reference images, reference videos, paired
|
||||
video soundtracks, and standalone audio references:
|
||||
|
||||
```sh
|
||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\minimax_h3_ref2va_pruned-Q4_K_M.gguf --vae ..\models\vae\minimax_h3_video_vae_fp16.safetensors --audio-vae ..\models\vae\minimax_h3_audio_vae_fp32.safetensors --llm ..\models\text_encoders\qwen3vl_32b_minimax_h3-Q4_K_M.gguf -p "Use the cat from <Picture 1> as the main character. Keep the cat’s appearance, fur color, facial features, and identity consistent with the reference image. Create a 2-second cinematic video: start with an extreme close-up shot of the cat’s face, focusing on its cute expression and detailed fur texture. The camera slowly rotates around the cat’s head, creating a dynamic reveal. Then smoothly pull back and zoom out to reveal the full scene: the cat is standing confidently on a surfboard, riding ocean waves. Water splashes around the board, sea breeze gently moves the cat’s fur, and the cat maintains a cute and fearless expression while surfing. Smooth camera movement, cinematic orbit shot, seamless zoom-out transition, low-angle wide shot, realistic ocean environment, golden sunlight, dynamic waves, high-quality realistic style, natural motion, no distortion, keep the cat’s identity unchanged." -r ..\assets\ernie_image\turbo_example.png --cfg-scale 1.0 -v -W 864 -H 480 --diffusion-fa --offload-to-cpu --rng cpu --fps 24 --video-frames 56
|
||||
```
|
||||
|
||||
<video src=../assets/minimax-h3/r2av.mp4 controls="controls" muted="muted" type="video/mp4"></video>
|
||||
|
||||
`--ref-image`, `--ref-video`, and `--ref-audio` can each be repeated. A
|
||||
reference video is a directory of image frames sorted lexicographically and is
|
||||
treated as 24 fps. Repeated `--ref-video-audio` WAV files are paired by index
|
||||
with repeated `--ref-video` inputs. WAV PCM (8/16/24/32-bit) and 32/64-bit
|
||||
floating-point samples are accepted; audio is converted to stereo 32 kHz by the
|
||||
pipeline.
|
||||
|
||||
Reference inputs are presented to Qwen3-VL in image, video, then audio order.
|
||||
Videos are sampled at 2 fps for the Qwen presentation while their full 24 fps
|
||||
latents condition the diffusion transformer. Paired video and audio references
|
||||
share the same timeline. Ref2VA cannot be combined with `--init-img` or
|
||||
`--end-img` in one request.
|
||||
|
||||
Reference images keep their aspect ratio and are only downscaled when their
|
||||
pixel area exceeds the requested generation canvas.
|
||||
|
||||
The C API exposes the same inputs through `ref_images`, `ref_videos`, and
|
||||
`ref_audios` in `sd_vid_gen_params_t`. Each `sd_ref_video_t` supplies its own
|
||||
frame rate and optional soundtrack; non-24-fps inputs are resampled internally.
|
||||
|
||||
## Shape and runtime notes
|
||||
|
||||
- Width and height are aligned upward to a multiple of 32.
|
||||
- Frame count is aligned upward to the `17k + 5` grid, with a minimum of 5.
|
||||
- MiniMax-H3 runs at 24 fps; another requested value is overridden.
|
||||
- The default video flow shift is 12. The audio stream is mapped internally to
|
||||
its shift of 3, so the regular samplers can operate on the packed AV latent.
|
||||
@@ -67,21 +67,21 @@ Detection should respect `prefix`. For nested weights, construct full names from
|
||||
|
||||
Do not add persistent config fields such as `inferred_from_weights` only to
|
||||
record whether detection happened. If the function needs to decide whether to
|
||||
print a debug line, keep that as local control flow inside `detect_from_weights`.
|
||||
print a verbose line, keep that as local control flow inside `detect_from_weights`.
|
||||
|
||||
## Logging
|
||||
|
||||
When config values are inferred from weights, print one `LOG_DEBUG` line at the
|
||||
When config values are inferred from weights, print one `LOG_VERBOSE` line at the
|
||||
end of `detect_from_weights`.
|
||||
|
||||
Example:
|
||||
|
||||
```cpp
|
||||
LOG_DEBUG("llm: num_layers = %" PRId64 ", vocab_size = %" PRId64 ", hidden_size = %" PRId64 ", intermediate_size = %" PRId64,
|
||||
config.num_layers,
|
||||
config.vocab_size,
|
||||
config.hidden_size,
|
||||
config.intermediate_size);
|
||||
LOG_VERBOSE("llm: num_layers = %" PRId64 ", vocab_size = %" PRId64 ", hidden_size = %" PRId64 ", intermediate_size = %" PRId64,
|
||||
config.num_layers,
|
||||
config.vocab_size,
|
||||
config.hidden_size,
|
||||
config.intermediate_size);
|
||||
```
|
||||
|
||||
Only print the config detection log when the function actually inferred values
|
||||
|
||||
+28
-12
@@ -14,8 +14,12 @@ Run by adding `--diffusion-fa` to the arguments and watch for:
|
||||
```
|
||||
and the compute buffer shrink in the debug log:
|
||||
```
|
||||
[DEBUG] ggml_extend.hpp:1004 - flux compute buffer size: 650.00 MB(VRAM)
|
||||
[DEBUG] ggml_runner.cpp:280 - flux compute buffer size: 650.00 MB(VRAM) on CUDA0 (peak across 1 segment)
|
||||
```
|
||||
This reports the actual peak compute workspace capacity per backend, including
|
||||
CPU fallback. It excludes weights and cache buffers. Within a runner lifecycle,
|
||||
the summary is printed only on the first graph or when backend capacities or the
|
||||
segment count change.
|
||||
|
||||
## Offload weights to the CPU to save VRAM without reducing generation speed.
|
||||
|
||||
@@ -23,7 +27,7 @@ Using `--offload-to-cpu` allows you to offload weights to the CPU, saving VRAM w
|
||||
|
||||
## Use params backend to reduce VRAM or RAM usage.
|
||||
|
||||
`--params-backend` controls where model parameters are kept. If it is not set, parameters use the same backend as `--backend`, so a GPU runtime backend also keeps parameters in VRAM.
|
||||
`--params-backend` controls where model parameters are kept. If it is not set, auto-fit chooses parameter placement while preserving `--backend`. With `--auto-fit off`, parameters use the same backend as `--backend`, so a GPU runtime backend also keeps parameters in VRAM.
|
||||
|
||||
Use CPU params to reduce VRAM usage:
|
||||
|
||||
@@ -43,7 +47,7 @@ Use disk params to reduce both VRAM and RAM usage:
|
||||
--backend cuda0 --params-backend disk
|
||||
```
|
||||
|
||||
This reloads parameters from the model file on demand and releases them after use. It has the lowest memory residency, but can be slower because weights must be read again. `disk` is never selected implicitly; set it explicitly when RAM usage matters more than reload cost.
|
||||
This reloads parameters from the model file on demand, retains unpinned compute copies while space permits, and releases them under pressure or at module-run completion. It has the lowest source-memory residency, but can be slower because evicted weights must be read again. `disk` is never selected implicitly; set it explicitly when RAM usage matters more than reload cost.
|
||||
|
||||
Per-module assignments can target only the largest modules:
|
||||
|
||||
@@ -53,25 +57,37 @@ Per-module assignments can target only the largest modules:
|
||||
|
||||
See [backend selection](./backend.md) for full syntax.
|
||||
|
||||
## Run models that don't fit in VRAM (CPU streaming).
|
||||
## Run models that don't fit in VRAM (automatic segmented execution).
|
||||
|
||||
`--offload-to-cpu` alone keeps every parameter in system RAM and stages it to the runtime backend on first use, then leaves it resident there. If the diffusion model is larger than the runtime backend's free memory (e.g. Flux dev at bf16 on an 8 GiB GPU), that residency stops fitting during the sampling loop and generation fails. Two additional flags make it fit by trading a small amount of speed for room:
|
||||
`--offload-to-cpu` keeps the source parameters in system RAM and creates compute-side GPU replicas on demand. Unpinned replicas remain resident for reuse, but automatic graph-cut execution evicts them from the last segment backward when the next weight or compute allocation needs space. Disk-backed parameters follow the same policy without retaining a RAM source copy.
|
||||
|
||||
- `--max-vram <GiB>` sets a VRAM budget the graph-cut segmenter respects. It cuts each forward pass into segments sized to fit the budget, running them in sequence and freeing intermediate activations between them. Negative values auto-detect free VRAM and spare the given amount (`--max-vram -1` uses most of the free VRAM and keeps ~1 GiB headroom), a positive value caps the budget, `0` disables segmentation.
|
||||
- `--stream-layers` streams the diffusion model's transformer blocks one at a time. Each block's parameters are copied from the CPU to the runtime backend just before it runs and evicted when the residency budget is reached. Prefetching hides most of the copy latency behind compute. This flag only takes effect when the diffusion params backend is CPU, so it must be combined with `--offload-to-cpu` (or an explicit `--params-backend diffusion=cpu`); a warning is logged and the flag is ignored otherwise.
|
||||
When a graph has cut markers and its missing weights plus incremental compute workspace exceed the available device headroom, it runs its fixed segment list in order. A reusable monolithic compute buffer is not counted as a new allocation. An explicit `--max-vram` budget deducts already-resident managed weights and compute/cache buffers registered by every runner sharing the device, so later graph runs remain segmented when the full graph exceeds the budget. The current segment's weights are pinned during compute, and the next parameter-bearing segment is prefetched when the device supports asynchronous transfer. No opt-in streaming flag is required.
|
||||
|
||||
The three flags stack. The recommended shape for "biggest model my card can host":
|
||||
- `--max-vram <GiB>` optionally lowers the live-memory limit. A positive value is a managed per-device budget, `0` uses the device's current free memory without an explicit budget, and a negative value snapshots free memory at startup while reserving that many GiB (`--max-vram -1` reserves about 1 GiB). Driver contexts and unrelated external allocations remain outside the managed budget.
|
||||
- `--disable-prefetch` disables asynchronous next-segment prefetch while retaining synchronous loading, eviction, and segmented execution.
|
||||
- `--disable-segmented-compute` forces monolithic graph execution for diagnostics or compatibility, even when the automatic memory check would select segments.
|
||||
|
||||
Single-device monolithic execution also reclaims unpinned weight replicas before
|
||||
loading weights or allocating compute workspace, including graphs without cut
|
||||
markers and runs with `--disable-segmented-compute`. It still respects the managed
|
||||
device budget and fails if the graph cannot fit after reclamation.
|
||||
|
||||
Segment completion releases active workspace use while retaining the runner's
|
||||
allocator/scheduler capacity. Compatible gallocr reservations are reused across
|
||||
graphs; idle workspaces can be reclaimed under pressure and are freed at runner
|
||||
completion. Cross-graph caches survive individual graphs, but cut buffers do not.
|
||||
|
||||
The recommended shape for "biggest model my card can host" is:
|
||||
|
||||
```shell
|
||||
sd-cli --diffusion-model flux1-dev.safetensors ... \
|
||||
--offload-to-cpu --max-vram -1 --stream-layers
|
||||
--offload-to-cpu --max-vram -1
|
||||
```
|
||||
|
||||
- `--offload-to-cpu`: params in RAM, staged as needed.
|
||||
- `--max-vram -1`: use most of the free VRAM as the compute budget, spare 1 GiB headroom, let the graph-cut segmenter split each forward pass to fit.
|
||||
- `--stream-layers`: on top of the segmenter, stream individual transformer blocks so their weights don't all need to be resident at once.
|
||||
- `--max-vram -1`: reserve about 1 GiB from the startup free-memory snapshot; live free memory can still lower the effective limit for every graph.
|
||||
|
||||
Ordered from fastest to smallest-VRAM: no flags → `--offload-to-cpu` → `--offload-to-cpu --max-vram <N>` → `--offload-to-cpu --max-vram <N> --stream-layers`. Each step down costs a few percent of throughput to buy more room; combined they can run models roughly 3-4x larger than the raw VRAM would allow.
|
||||
Use `--params-backend diffusion=disk` instead when reducing system RAM residency is more important than avoiding repeated model-file reads.
|
||||
|
||||
## Use quantization to reduce memory usage.
|
||||
|
||||
|
||||
+5
-1
@@ -11,6 +11,8 @@ In stable-diffusion.cpp, PiD currently runs as an image edit pipeline: provide a
|
||||
- safetensors: https://huggingface.co/Comfy-Org/PixelDiT/tree/main/diffusion_models
|
||||
- Download Gemma 2 2B
|
||||
- safetensors: https://huggingface.co/Comfy-Org/PixelDiT/tree/main/text_encoders
|
||||
- Download Gemma 2 2B tokenizer.json
|
||||
- https://huggingface.co/google/gemma-2-2b/tree/main
|
||||
- Download the VAE that matches the PiD checkpoint backbone
|
||||
- safetensors: https://huggingface.co/nvidia/PiD/tree/main/checkpoints
|
||||
- Flux / Z-Image PiD: use the Flux VAE and pass `--vae-format flux`
|
||||
@@ -20,10 +22,12 @@ In stable-diffusion.cpp, PiD currently runs as an image edit pipeline: provide a
|
||||
|
||||
The official PiD model card should be checked before use. At the time of the initial PiD release, the official weights are under the NSCLv1 non-commercial license.
|
||||
|
||||
PiD and PiD 1.5 require an external Gemma 2 `tokenizer.json` matching the text encoder checkpoint. Save it as `tokenizer_gemma2.json` and pass it with `--tokenizer`; the tokenizer is not embedded in sd.cpp. See [JSON tokenizers](tokenizers.md) for CLI and C API usage.
|
||||
|
||||
## Examples
|
||||
|
||||
```
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\pid_flux1_512_to_2048_4step_bf16.safetensors --llm "..\models\text_encoders\gemma_2_2b_it_elm_bf16.safetensors" --vae ..\models\vae\ae.sft --vae-format flux --cfg-scale 1.0 -p "a lovely cat" -r ..\assets\ernie_image\turbo_example.png --diffusion-fa -v --steps 4 -H 2048 -W 2048 --rng cpu
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\pid_flux1_512_to_2048_4step_bf16.safetensors --llm "..\models\text_encoders\gemma_2_2b_it_elm_bf16.safetensors" --tokenizer ..\models\tokenizers\tokenizer_gemma2.json --vae ..\models\vae\ae.sft --vae-format flux --cfg-scale 1.0 -p "a lovely cat" -r ..\assets\ernie_image\turbo_example.png --diffusion-fa -v --steps 4 -H 2048 -W 2048 --rng cpu
|
||||
```
|
||||
|
||||
Before:
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# How to Use
|
||||
|
||||
Qwen Image 2.1 supports text-to-image generation and image editing, using Qwen3-VL-8B as the text encoder and its own VAE.
|
||||
|
||||
## Download weights
|
||||
|
||||
- Download Qwen Image 2.1
|
||||
- safetensors: https://huggingface.co/Comfy-Org/Qwen-Image-2.1/tree/main/diffusion_models
|
||||
- gguf: https://huggingface.co/leejet/Qwen-Image-2.1-GGUF/tree/main
|
||||
- Download vae
|
||||
- safetensors: https://huggingface.co/Comfy-Org/Qwen-Image-2.1/tree/main/vae
|
||||
- Download Qwen3-VL-8B-Instruct
|
||||
- safetensors (BF16 or INT8 convrot): https://huggingface.co/Comfy-Org/Qwen-Image-2.1/tree/main/text_encoders
|
||||
- gguf: https://huggingface.co/Qwen/Qwen3-VL-8B-Instruct-GGUF/tree/main
|
||||
- For image editing with a GGUF text encoder, also download `mmproj-Qwen3VL-8B-Instruct-F16.gguf` from the same repository and pass it with `--llm_vision`.
|
||||
|
||||
Use `qwen_image_2.1_vae_bf16.safetensors` with this model. The earlier Qwen Image and Wan 2.2 VAE weights are not interchangeable with the Qwen Image 2.1 VAE weights.
|
||||
|
||||
## Examples
|
||||
|
||||
Run the following commands from the build directory. Use image dimensions divisible by 32. The resolution-dependent flow schedule is selected automatically.
|
||||
|
||||
### Text to image
|
||||
|
||||
```powershell
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\qwen_image_2.1_int8_convrot.safetensors --vae ..\models\vae\qwen_image_2.1_vae_bf16.safetensors --llm ..\models\text_encoders\Qwen3VL-8B-Instruct-Q4_K_M.gguf -p "a lovely cat holding a sign says 'qwen2.1.cpp'" --cfg-scale 6.0 --sampling-method euler -v --offload-to-cpu -o qwen_image_2.1.png
|
||||
```
|
||||
|
||||
<img alt="Qwen Image 2.1 example" src="../assets/qwen/qwen_image_2.1.png" />
|
||||
|
||||
To use GGUF diffusion weights, set `--diffusion-model` to the path of a file such as `qwen_image_2.1-Q4_K.gguf`.
|
||||
|
||||
### Image editing
|
||||
|
||||
Pass the reference image with `-r` and describe the edit in `-p`. Vision weights are required; the example below loads them separately with `--llm_vision`.
|
||||
|
||||
```powershell
|
||||
.\bin\Release\sd-cli.exe --diffusion-model ..\models\diffusion_models\qwen_image_2.1_int8_convrot.safetensors --vae ..\models\vae\qwen_image_2.1_vae_bf16.safetensors --llm ..\models\text_encoders\Qwen3VL-8B-Instruct-Q4_K_M.gguf --llm_vision ..\models\text_encoders\Qwen3VL-8B-Instruct-mmproj-BF16.gguf -r ..\assets\qwen\qwen_image_2.1.png -p "change 'qwen2.1.cpp' to 'sd.cpp'" --cfg-scale 6.0 --sampling-method euler -v --offload-to-cpu -o qwen_image_2.1_edit.png
|
||||
```
|
||||
|
||||
For multiple reference images, repeat `-r` in the desired order, for example `-r first.png -r second.png`.
|
||||
+7
-7
@@ -57,7 +57,7 @@ The RPC server acts as the worker. You must explicitly enable the **backend** (t
|
||||
|
||||
To find the correct flags for your system, refer to the official documentation for the [`llama.cpp`](https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md) repository.
|
||||
|
||||
> **Crucial:** You must include the compiler flags required to satisfy the API compatibility with `stable-diffusion.cpp` (`-DGGML_MAX_NAME=128`). Without this flag, `GGML_MAX_NAME` will default to `64` for the server, and data transfers between the client and server will fail. Of course, `-DGGML_RPC` must also be enabled.
|
||||
> **Crucial:** You must include the compiler flags required to satisfy the API compatibility with `stable-diffusion.cpp` (`-DGGML_MAX_NAME=160`). Without this flag, `GGML_MAX_NAME` will default to `64` for the server, and data transfers between the client and server will fail. Of course, `-DGGML_RPC` must also be enabled.
|
||||
>
|
||||
> I recommend disabling the `LLAMA_CURL` flag to avoid unnecessary dependencies, and disabling shared library builds to avoid potential conflicts.
|
||||
|
||||
@@ -72,8 +72,8 @@ cmake .. -DGGML_RPC=ON \
|
||||
-DGGML_VULKAN=ON \ # Ensure backend is enabled
|
||||
-DGGML_BUILD_SHARED_LIBS=OFF \
|
||||
-DLLAMA_CURL=OFF \
|
||||
-DCMAKE_C_FLAGS=-DGGML_MAX_NAME=128 \
|
||||
-DCMAKE_CXX_FLAGS=-DGGML_MAX_NAME=128
|
||||
-DCMAKE_C_FLAGS=-DGGML_MAX_NAME=160 \
|
||||
-DCMAKE_CXX_FLAGS=-DGGML_MAX_NAME=160
|
||||
cmake --build . --config Release --target rpc-server -j $(nproc)
|
||||
```
|
||||
|
||||
@@ -86,8 +86,8 @@ cmake .. -DGGML_RPC=ON \
|
||||
-DGGML_METAL=ON \
|
||||
-DGGML_BUILD_SHARED_LIBS=OFF \
|
||||
-DLLAMA_CURL=OFF \
|
||||
-DCMAKE_C_FLAGS=-DGGML_MAX_NAME=128 \
|
||||
-DCMAKE_CXX_FLAGS=-DGGML_MAX_NAME=128
|
||||
-DCMAKE_C_FLAGS=-DGGML_MAX_NAME=160 \
|
||||
-DCMAKE_CXX_FLAGS=-DGGML_MAX_NAME=160
|
||||
cmake --build . --config Release --target rpc-server
|
||||
```
|
||||
|
||||
@@ -101,8 +101,8 @@ cmake .. -G "Visual Studio 17 2022" -A x64 `
|
||||
-DGGML_VULKAN=ON `
|
||||
-DGGML_BUILD_SHARED_LIBS=OFF `
|
||||
-DLLAMA_CURL=OFF `
|
||||
-DCMAKE_C_FLAGS=-DGGML_MAX_NAME=128 `
|
||||
-DCMAKE_CXX_FLAGS=-DGGML_MAX_NAME=128
|
||||
-DCMAKE_C_FLAGS=-DGGML_MAX_NAME=160 `
|
||||
-DCMAKE_CXX_FLAGS=-DGGML_MAX_NAME=160
|
||||
cmake --build . --config Release --target rpc-server
|
||||
```
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
# SageAttention
|
||||
|
||||
`--sage-attn` enables native CUDA SageAttention in the diffusion model, including
|
||||
the high-noise diffusion model when present. Python, PyTorch, and Triton are not
|
||||
required at build time or runtime.
|
||||
|
||||
The CUDA backend automatically selects a kernel supported by both the GPU and
|
||||
the compiled CUDA toolkit:
|
||||
|
||||
| GPU / toolkit | Implementation |
|
||||
| --- | --- |
|
||||
| SM89 or newer, CUDA 12.8 or newer (except SM90) | SageAttention2++: per-thread INT8 Q/K, FP8 PV, FP16 instruction accumulation with an FP32 buffer |
|
||||
| SM89 or newer, CUDA 12.4 or newer; SM90 also uses this path with newer toolkits | SageAttention2: per-thread INT8 Q/K, FP8 PV, two-level FP32 accumulation |
|
||||
| SM80 or newer, CUDA 12.0 or newer | INT8 Q/K, FP16 PV compatibility path |
|
||||
|
||||
The FP8 paths smooth K, quantize V per channel, and pad and permute V for FP8
|
||||
Tensor Cores. The 2++ path uses the upstream V scale limit of 2.25 to avoid
|
||||
overflow in its FP16 instruction accumulator. The public output remains FP32.
|
||||
These are the upstream **INT8** SageAttention2/2++ variants; the paper's INT4
|
||||
variant and Hopper-specific WGMMA kernel are not implemented here.
|
||||
|
||||
## Build
|
||||
|
||||
Use the bundled patched GGML, CUDA Toolkit 12.0 or newer, and an NVIDIA GPU with
|
||||
compute capability 8.0 or newer. Compile kernels for the GPU being used.
|
||||
|
||||
```sh
|
||||
cmake -S . -B build -DSD_CUDA=ON -DSD_USE_UPSTREAM_GGML=OFF
|
||||
cmake --build build --config Release
|
||||
```
|
||||
|
||||
No separate SageAttention build option is needed. Upstream GGML builds do not
|
||||
support it. A system GGML must include the matching patched API and CUDA
|
||||
backend. Enabling `--sage-attn` with an unavailable build or diffusion device
|
||||
reports an error. Building with CUDA 12.4 selects SageAttention2 on an RTX 4090;
|
||||
rebuild with CUDA 12.8 or newer to use SageAttention2++.
|
||||
|
||||
## Use
|
||||
|
||||
Replace `--diffusion-fa` with `--sage-attn` in an existing command. For example,
|
||||
from the build directory:
|
||||
|
||||
```powershell
|
||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\Wan2.2-T2V-A14B-LowNoise-Q8_0.gguf --high-noise-diffusion-model ..\models\diffusion_models\Wan2.2-T2V-A14B-HighNoise-Q8_0.gguf --vae ..\models\vae\wan_2.1_vae.safetensors --t5xxl ..\models\text_encoders\umt5-xxl-encoder-Q8_0.gguf -p "a lovely cat" --cfg-scale 3.5 --sampling-method euler --steps 10 --high-noise-cfg-scale 3.5 --high-noise-sampling-method euler --high-noise-steps 8 -v -n "色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,
|
||||
形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走" -W 832 -H 480 --diffusion-fa --offload-to-cpu --video-frames 33 --sage-attn
|
||||
```
|
||||
|
||||
SageAttention currently handles unmasked attention with head dimensions from
|
||||
1 through 128, including grouped-query attention, different query/key lengths,
|
||||
and multiple batches. Dimensions below 64 are zero-padded to 64; dimensions
|
||||
between 65 and 127 are zero-padded to 128. The original softmax scale is preserved,
|
||||
and the output is cropped back to the original dimension. Other attention
|
||||
operations fall back to FlashAttention when supported, then ordinary attention.
|
||||
SageAttention takes precedence in diffusion
|
||||
when combined with `--fa` or `--diffusion-fa`; `--fa` continues to control other
|
||||
modules. Existing attention scaling overrides remain effective.
|
||||
|
||||
Attention quantization changes numerical results. Compare image quality and
|
||||
end-to-end generation time using the same seed, dimensions, and sampling
|
||||
settings. Compare sampling steps after the first step for warmed-up inference
|
||||
speed, and report model loading and first-step initialization separately.
|
||||
Quantization, smoothing, and format conversion costs are included in generation
|
||||
time, so short sequences may not benefit.
|
||||
|
||||
Library callers set `sd_ctx_params_t.sage_attn = true` before `new_sd_ctx()`,
|
||||
like `diffusion_flash_attn`. Context creation fails if the requested feature is
|
||||
unavailable. Initialize the parameter structure with `sd_ctx_params_init()`.
|
||||
Rebuild library callers against the updated public header.
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
- download original weights(.ckpt or .safetensors). For example
|
||||
- Stable Diffusion v1.4 from https://huggingface.co/CompVis/stable-diffusion-v-1-4-original
|
||||
- Stable Diffusion v1.5 from https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5
|
||||
- Stable Diffuison v2.1 from https://huggingface.co/Manojb/stable-diffusion-2-1-base
|
||||
- Stable Diffusion v2.1 from https://huggingface.co/Manojb/stable-diffusion-2-1-base
|
||||
- Stable Diffusion 3 2B from https://huggingface.co/stabilityai/stable-diffusion-3-medium
|
||||
|
||||
### txt2img example
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@ The dispatcher picks `alpha` from the filename (`turbo` substring => 1.0, otherw
|
||||
### 5B (needs streaming on 12 GiB VRAM)
|
||||
|
||||
```
|
||||
./build/bin/sd-cli --diffusion-model /path/to/sefi_5b_turbo.safetensors --vae /path/to/flux2_ae.safetensors --llm /path/to/qwen3_vl_4b.safetensors -p "a photograph of an orange tabby cat sitting on a couch" --cfg-scale 1.0 --steps 4 -W 1024 -H 1024 -s 42 --diffusion-fa --max-vram 8 --stream-layers --offload-to-cpu -o out.png
|
||||
./build/bin/sd-cli --diffusion-model /path/to/sefi_5b_turbo.safetensors --vae /path/to/flux2_ae.safetensors --llm /path/to/qwen3_vl_4b.safetensors -p "a photograph of an orange tabby cat sitting on a couch" --cfg-scale 1.0 --steps 4 -W 1024 -H 1024 -s 42 --diffusion-fa --max-vram 8 --offload-to-cpu -o out.png
|
||||
```
|
||||
|
||||
<img alt="SeFi-Image 5B turbo example" src="../assets/sefi_image/example.png" />
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
# How to Use
|
||||
|
||||
SenseNova U1.5 is an 8B MoT model that performs diffusion directly in RGB pixel
|
||||
space. It does not require a separate text encoder or VAE.
|
||||
|
||||
## Download weights
|
||||
|
||||
- Download SenseNova U1.5 8B MoT
|
||||
- safetensors: https://huggingface.co/sensenova/SenseNova-U1.5-8B-MoT
|
||||
|
||||
Pass the complete downloaded repository directory to `--model`. The directory
|
||||
must contain `model.safetensors.index.json`, every referenced Safetensors shard,
|
||||
and the tokenizer files.
|
||||
|
||||
## Examples
|
||||
|
||||
### CUDA
|
||||
|
||||
```bash
|
||||
./bin/sd-cli \
|
||||
--model /path/to/SenseNova-U1.5-8B-MoT \
|
||||
--prompt "a red cube on a white background" \
|
||||
--width 2048 \
|
||||
--height 2048 \
|
||||
--steps 50 \
|
||||
--cfg-scale 4 \
|
||||
--flow-shift 3 \
|
||||
--seed 42 \
|
||||
--sampling-method euler \
|
||||
--rng cuda \
|
||||
--fa \
|
||||
--output output.png
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- To match the official non-thinking text-to-image pipeline, use 50 Euler
|
||||
steps, CFG 4, flow shift 3, seed 42, CUDA RNG, and an empty negative prompt.
|
||||
- Width and height must be multiples of 32. The trained 1:1 resolution is
|
||||
2048x2048; lower resolutions are useful for smoke tests but are outside the
|
||||
training buckets.
|
||||
- The SenseNova prompt template and unconditional prompt are built
|
||||
automatically.
|
||||
- This implementation supports non-thinking text-to-image generation. Image
|
||||
editing, visual understanding, interleaved generation, and thinking-mode
|
||||
prompt expansion are not implemented.
|
||||
@@ -0,0 +1,106 @@
|
||||
# Sol-Attn
|
||||
|
||||
`--sol-attn` enables native CUDA Sol-Attn in the diffusion model, including the
|
||||
high-noise diffusion model when present. It uses the shared attention dispatcher
|
||||
without classifying tokens as text, images, or video. Python, PyTorch, Triton,
|
||||
and CuTe DSL are not needed to build or run it.
|
||||
|
||||
This implementation follows the diagonal-threshold algorithm in
|
||||
[NVlabs/Sana's Sol-Attn](https://github.com/NVlabs/Sana/tree/sol-engine/techniques/sparse_backends/sol_attn).
|
||||
It summarizes 64-token KV blocks, selects exact blocks using proxy scores and
|
||||
an online threshold, and approximates the remaining blocks using their K means
|
||||
and V sums. Adjacent blocks remain exact. Both contributions share an online
|
||||
softmax normalizer. Q/K/V and probability tiles use BF16 Tensor Cores with FP32
|
||||
accumulation; the BF16 result is returned through the existing FP32 interface.
|
||||
|
||||
## Build
|
||||
|
||||
Use patched GGML, CUDA Toolkit 12.0 or newer, and an NVIDIA GPU with compute
|
||||
capability 8.0 or newer. Compile kernels for the target GPU:
|
||||
|
||||
```sh
|
||||
cmake -S . -B build -DSD_CUDA=ON -DSD_USE_UPSTREAM_GGML=OFF
|
||||
cmake --build build --config Release
|
||||
```
|
||||
|
||||
The feature is compiled with the CUDA backend; no separate build option is
|
||||
required. Upstream GGML and non-CUDA backends do not support it. A system GGML
|
||||
must provide the matching patched API and CUDA implementation. Tensor-parallel
|
||||
row splitting is not supported; layer splitting requires supported devices.
|
||||
|
||||
## Use
|
||||
|
||||
Add `--sol-attn` to an existing generation command:
|
||||
|
||||
```sh
|
||||
sd-cli ... --sol-attn
|
||||
sd-cli ... --sol-attn --sol-attn-tau 1.0
|
||||
```
|
||||
|
||||
The default threshold coefficient is `1.0`. Larger coefficients select fewer
|
||||
blocks for exact attention. The coefficient must be finite; zero does not mean
|
||||
dense attention. Omit `--sol-attn` to disable the feature.
|
||||
|
||||
The native kernel supports unmasked, noncausal attention with head dimension
|
||||
128, equal Q/K/V sequence lengths and head counts, and multiple batches. Other
|
||||
attention operations fall back to FlashAttention when available, then ordinary
|
||||
attention. Existing attention scaling overrides remain effective. `--fa` and
|
||||
`--diffusion-fa` may be used together with Sol-Attn; `--sage-attn` is mutually
|
||||
exclusive. Text encoders and VAEs retain their existing attention selection.
|
||||
|
||||
Initialization reports an error if the requested diffusion backend cannot run
|
||||
Sol-Attn. Graph logs report the number of Sol-Attn and FlashAttention nodes and
|
||||
warn when no Sol-Attn nodes are selected. CUDA execution errors are not silently
|
||||
converted into dense attention.
|
||||
|
||||
This is approximate attention. Validate quality and end-to-end speed with the
|
||||
same prompt, seed, dimensions, frame count, and sampling settings. Include
|
||||
packing, preprocessing, offload, and decode time in comparisons. Short sequences
|
||||
may not benefit. Upstream combined pipeline speedups are not measurements of
|
||||
this native kernel. Exact-covariance thresholds, text sinks, Morton ordering,
|
||||
and step/layer schedules are not implemented.
|
||||
|
||||
## Validation
|
||||
|
||||
On an RTX 4090 with CUDA 12.4, Wan 2.1 T2V 1.3B was tested at 832x480,
|
||||
33 frames, 20 Euler steps, seed 42, CFG 6, and flow shift 3, using the prompt
|
||||
`a lovely cat` and the same negative prompt for every run:
|
||||
|
||||
| Attention | Sampling time | Total process time |
|
||||
| --- | ---: | ---: |
|
||||
| FlashAttention | 45.73 s | 74.63 s |
|
||||
| Sol-Attn, tau 1 | 37.17 s | 66.20 s |
|
||||
| Sol-Attn, tau 0 | 40.66 s | 68.50 s |
|
||||
|
||||
These are single-run measurements. The graph selected 30 Sol-Attn nodes and
|
||||
30 FlashAttention nodes. At tau 1, sampled video frames showed washed-out
|
||||
colors and reduced detail. Tau 0 improved clarity in this example, but still
|
||||
changed the composition. Neither setting guarantees the baseline's quality.
|
||||
For this Wan command, `--sol-attn --sol-attn-tau 0` is a more conservative
|
||||
starting point. In the one-frame case, tau 1 increased warm sampling time from
|
||||
0.140 to 0.148 seconds per step.
|
||||
|
||||
Validation also covered 15 numerical reference cases, 11 layout/scaling/fallback
|
||||
cases, CUDA memory checking, and 36 existing SageAttention regression cases.
|
||||
CLI and server CUDA builds and the upstream GGML CPU library build passed.
|
||||
Other GPU architectures, multi-GPU execution, and other models have not been
|
||||
tested.
|
||||
|
||||
## Library API
|
||||
|
||||
Configure Sol-Attn in `sd_ctx_params_t` before creating the context:
|
||||
|
||||
```cpp
|
||||
sd_ctx_params_t params;
|
||||
sd_ctx_params_init(¶ms);
|
||||
// Set model paths and other context options here.
|
||||
params.sol_attn = true;
|
||||
params.sol_attn_tau = 1.0f;
|
||||
sd_ctx_t* ctx = new_sd_ctx(¶ms);
|
||||
```
|
||||
|
||||
`sd_ctx_params_init` defaults `sol_attn` to false and `sol_attn_tau` to 1.0.
|
||||
`new_sd_ctx` returns null for a nonfinite threshold, unavailable requested
|
||||
backends, or a conflict with SageAttention. The context owns a copy of these
|
||||
settings; changing the input structure after creation does not reconfigure it.
|
||||
Applications must be rebuilt against the updated `sd_ctx_params_t` definition.
|
||||
@@ -0,0 +1,107 @@
|
||||
# JSON tokenizers
|
||||
|
||||
Use a Hugging Face `tokenizer.json` to supply the tokenizer vocabulary, merges,
|
||||
added tokens, and processing stages. **PiD (including PiD 1.5) and Lens (including
|
||||
Lens Turbo) require an external JSON**; their Gemma 2 and GPT-OSS tokenizers are
|
||||
not embedded. Initialization fails if the main tokenizer is missing. Other
|
||||
models keep their embedded tokenizer when this option is omitted.
|
||||
|
||||
```shell
|
||||
sd-cli --diffusion-model model.gguf --llm text_encoder.gguf \
|
||||
--tokenizer tokenizer_gemma2.json --vae vae.safetensors -p "a cat"
|
||||
```
|
||||
|
||||
Choose the JSON belonging to the text encoder checkpoint. Checking that IDs fit
|
||||
the embedding table does not establish that two vocabularies have the same
|
||||
meaning. The JSON file is loaded when the text encoder is created; its embedded
|
||||
vocabulary is not loaded in this case.
|
||||
|
||||
| Model | Required text encoder tokenizer | Example |
|
||||
| --- | --- | --- |
|
||||
| PiD / PiD 1.5 | Gemma 2 matching the text encoder checkpoint | `--tokenizer tokenizer_gemma2.json` |
|
||||
| Lens / Lens Turbo | GPT-OSS matching the text encoder checkpoint | `--tokenizer tokenizer_gpt_oss.json` |
|
||||
|
||||
The Gemma 3/4 tokenizer used by LTX-2 remains embedded.
|
||||
|
||||
| Option | Encoder |
|
||||
| --- | --- |
|
||||
| `--tokenizer FILE` | Main LLM/BPE encoder: Gemma 2, Gemma 3, Qwen 2/3, Mistral, GPT-OSS; also Anima and HiDream-O1 |
|
||||
| `--tokenizer FILE` | Shared CLIP tokenizer in SD1/SD2/SDXL, or CLIP-L in Flux |
|
||||
| `--tokenizer clip-l=FILE` | Separate CLIP-L in SD3 or Flux |
|
||||
| `--tokenizer clip-g=FILE` | Separate CLIP-G in SD3 |
|
||||
|
||||
Use comma-separated assignments to configure multiple slots, for example
|
||||
`--tokenizer main=main.json,clip-l=clip_l.json,clip-g=clip_g.json`.
|
||||
A plain file path is equivalent to `main=FILE`. You may also repeat `--tokenizer`
|
||||
with explicit assignments, such as `--tokenizer main=main.json --tokenizer clip-l=clip.json`.
|
||||
Empty assignment paths, unknown keys, malformed assignments and
|
||||
duplicate slots are rejected. Commas separate entries in the assignment form;
|
||||
quote the complete argument when paths contain spaces.
|
||||
|
||||
SD3 overrides must name the `clip-l` or `clip-g` slot. SDXL uses one shared
|
||||
tokenizer for both CLIP encoders. Do not supply both `main` and `clip-l` for Flux.
|
||||
A slot targeting an absent or unsupported encoder fails initialization.
|
||||
T5/SentencePiece Unigram tokenizers are outside this implementation's scope.
|
||||
|
||||
For example, SD3 can load the same CLIP JSON into both slots:
|
||||
|
||||
```shell
|
||||
sd-cli --diffusion-model sd3.gguf --clip_l clip_l.safetensors \
|
||||
--clip_g clip_g.safetensors --t5xxl t5xxl.gguf --vae vae.safetensors \
|
||||
--tokenizer clip-l=tokenizer_clip.json,clip-g=tokenizer_clip.json \
|
||||
-p "a cat"
|
||||
```
|
||||
|
||||
The C API accepts the same string in `sd_ctx_params_t::tokenizer`. A null or
|
||||
empty value keeps an embedded tokenizer where available; PiD and Lens require
|
||||
a nonempty main tokenizer path. The CLI passes the string through;
|
||||
`TokenizerConfig` parses and validates it when text encoders are initialized.
|
||||
|
||||
```c
|
||||
sd_ctx_params_t params;
|
||||
sd_ctx_params_init(¶ms);
|
||||
params.tokenizer = "clip-l=tokenizer_clip.json,clip-g=tokenizer_clip.json";
|
||||
```
|
||||
|
||||
Rebuild applications against the updated public header when using the updated
|
||||
library.
|
||||
|
||||
## Supported components
|
||||
|
||||
| Stage | Supported configurations |
|
||||
| --- | --- |
|
||||
| Normalizer | `Sequence`, `NFC`, `Lowercase`, `Replace` with String/Regex patterns |
|
||||
| PreTokenizer | `Sequence`, `Split` with String/Regex patterns, all five delimiter behaviors and `invert`; `ByteLevel` with `add_prefix_space` and `use_regex` |
|
||||
| Model | Deterministic `BPE`, string or array-pair merges, `unk_token`, `fuse_unk`, `byte_fallback`, `ignore_merges`, `end_of_word_suffix` |
|
||||
| PostProcessor | Single-sequence `TemplateProcessing` with at most one prefix and one suffix token, `RobertaProcessing`, `ByteLevel` |
|
||||
| Decoder | `Sequence`, `Replace`, `ByteLevel`, `ByteFallback`, `Fuse` |
|
||||
| AddedToken | Special and ordinary added tokens, original IDs, raw or normalized matching, leftmost-longest matching |
|
||||
|
||||
`ByteLevel.use_regex` defaults to true when omitted. ByteLevel postprocessing
|
||||
changes offsets only and adds no tokens. Added tokens with `single_word`,
|
||||
`lstrip`, or `rstrip` enabled, nonzero BPE dropout, nonempty
|
||||
`continuing_subword_prefix`, and unsupported component types fail loading.
|
||||
New added-token IDs must follow the model vocabulary consecutively; configurations
|
||||
whose IDs Hugging Face would reassign are rejected.
|
||||
JSON `padding` and `truncation` must be null. This API returns IDs, not offsets,
|
||||
type IDs, or paired-input encodings; the pair template is not used.
|
||||
|
||||
The pipeline covers the CLIP, Gemma 2, Gemma 3, GPT-OSS, Mistral 3, Qwen 2 and
|
||||
Qwen 3 JSON configurations used by the differential test. It does not imply
|
||||
support for every tokenizer published under those model names.
|
||||
|
||||
## Prompt integration
|
||||
|
||||
Prompt attention parsing and model-specific chat/image templates remain in the
|
||||
conditioner. Raw `encode()` does not add BOS/EOS. The conditioner concatenates
|
||||
weighted prompt fragments, then the existing padding/chunking step applies the
|
||||
JSON single-sequence template once per sequence or CLIP chunk. Padding ID,
|
||||
direction, length limits and attention masks remain text encoder policies.
|
||||
CLIP requires both BOS and EOS because its chunking reserves those positions.
|
||||
|
||||
The internal `encode()`, `tokenize()`, and `decode()` interfaces return a success
|
||||
flag and write to an output parameter. A successful result may be empty; a failed
|
||||
call clears its output. JSON tokenizer input, normalization, and regex failures
|
||||
return `false` with diagnostic information instead of throwing. Invalid
|
||||
JSON, unsupported stages, conflicting IDs and IDs outside the encoder embedding
|
||||
table fail initialization instead of falling back to the embedded tokenizer.
|
||||
@@ -0,0 +1,45 @@
|
||||
# Troubleshooting
|
||||
|
||||
## Completely black or white images or videos / NaNs
|
||||
|
||||
Some ggml backends can encounter numerical overflow during inference, producing
|
||||
NaN (not-a-number) values. This can result in completely black or white images or videos.
|
||||
Whether it happens can depend on the backend, device, model, and weight format.
|
||||
|
||||
Known overflow issues have been addressed as far as possible, but the maintainer
|
||||
has limited hardware and cannot test every combination. Some cases may therefore
|
||||
still need a manual workaround.
|
||||
|
||||
These options are supported by both `sd-cli` and `sd-server`. If you encounter
|
||||
this problem, add them to your CLI generation command or server startup command:
|
||||
|
||||
```sh
|
||||
--linear-scale 0.0078125 --attn-scale 0.0078125
|
||||
```
|
||||
|
||||
For `sd-server`, restart the server after changing these startup options. Run the
|
||||
same prompt and seed again to see whether the output recovers. If the problem
|
||||
persists, try smaller positive values, for example:
|
||||
|
||||
```sh
|
||||
--linear-scale 0.00390625 --attn-scale 0.00390625
|
||||
```
|
||||
|
||||
These options reduce intermediate values and compensate afterwards to preserve
|
||||
the intended output scale:
|
||||
|
||||
- `--linear-scale` scales Linear inputs before matrix multiplication and rescales
|
||||
the result.
|
||||
- `--attn-scale` scales attention keys and values (K/V). It takes effect only in
|
||||
the Flash Attention path, where `--fa` or `--diffusion-fa` is enabled and the
|
||||
backend supports it.
|
||||
|
||||
The two values can be set independently and apply across model components. The
|
||||
default `0` preserves each model's built-in settings; `1` explicitly disables the
|
||||
corresponding scaling. Overrides must be finite positive values. C API users can
|
||||
set `linear_scale` and `attn_scale` in `sd_ctx_params_t`.
|
||||
|
||||
If the problem persists after trying the relevant steps above,
|
||||
[submit a bug report](https://github.com/leejet/stable-diffusion.cpp/issues/new?template=bug_report.yml).
|
||||
Include your full command, backend and hardware, model and weight format, logs,
|
||||
and the scale values you tried with their results.
|
||||
+49
@@ -34,6 +34,10 @@
|
||||
- Wan2.2 I2V A14B
|
||||
- safetensors: https://huggingface.co/Comfy-Org/Wan_2.2_ComfyUI_Repackaged/tree/main/split_files/diffusion_models
|
||||
- gguf: https://huggingface.co/QuantStack/Wan2.2-I2V-A14B-GGUF/tree/main
|
||||
- Wan2.2 S2V 14B
|
||||
- safetensors: https://huggingface.co/Comfy-Org/Wan_2.2_ComfyUI_Repackaged/tree/main/split_files/diffusion_models
|
||||
- gguf: https://huggingface.co/QuantStack/Wan2.2-S2V-14B-GGUF/tree/main
|
||||
- int8_convrot safetensors: https://huggingface.co/noctrex/Wan2.2-S2V-14B-int8_convrot
|
||||
- Download vae
|
||||
- wan_2.1_vae (for all the wan model except Wan2.2 TI2V 5B)
|
||||
- safetensors: https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/blob/main/split_files/vae/wan_2.1_vae.safetensors
|
||||
@@ -49,6 +53,9 @@
|
||||
- Download clip_vison_h (for Wan2.1 I2V/FLF2V only)
|
||||
- safetensors: https://huggingface.co/Comfy-Org/Wan_2.1_ComfyUI_repackaged/blob/main/split_files/clip_vision/clip_vision_h.safetensors
|
||||
|
||||
- Download audio_encoder (for Wan2.2 S2V only)
|
||||
- safetensors: https://huggingface.co/Comfy-Org/Wan_2.2_ComfyUI_Repackaged/blob/main/split_files/audio_encoders/wav2vec2_large_english_fp16.safetensors
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -94,6 +101,48 @@
|
||||
|
||||
<video src=../assets/wan/Wan2.2_14B_i2v.mp4 controls="controls" muted="muted" type="video/mp4"></video>
|
||||
|
||||
### Wan2.2 S2V 14B
|
||||
|
||||
Audio-driven video (speech-to-video). The reference image (`-i`) is the speaker
|
||||
portrait, `--audio` is the driving audio track and `--audio-encoder` is the
|
||||
wav2vec2 audio encoder. Wan2.2 S2V requires the wan_2.1 vae (16 channel), not
|
||||
the wan2.2 vae.
|
||||
|
||||
```
|
||||
.\bin\Release\sd-cli.exe -M vid_gen --diffusion-model ..\models\diffusion_models\wan2.2_s2v-14B-Q8_0.gguf --audio-encoder ..\models\audio_encoders\wav2vec2_large_english_fp16.safetensors --vae ..\models\vae\wan_2.1_vae.safetensors --t5xxl ..\models\text_encoders\umt5-xxl-encoder-Q8_0.gguf -p "a person is talking" --cfg-scale 6.0 --steps 20 --sampling-method euler -v -n "色调艳丽,过曝,静态,细节模糊不清,字幕,风格,作品,画作,画面,静止,整体发灰,最差质量,低质量,JPEG压缩残留,丑陋的,残缺的,多余的手指,画得不好的手部,画得不好的脸部,畸形的,毁容的,形态畸形的肢体,手指融合,静止不动的画面,杂乱的背景,三条腿,背景人很多,倒着走" -W 832 -H 480 --diffusion-fa --offload-to-cpu --vae-tiling --video-frames 81 -i ..\assets\cat_with_sd_cpp_42.png --audio .\input\speech.wav --flow-shift 3.0
|
||||
```
|
||||
|
||||
Notes:
|
||||
|
||||
- Recommended settings: `--sampling-method euler --steps 20 --cfg-scale 6.0`.
|
||||
`dpm++2m` produces heavy artifacts on S2V. 4 steps with the lightning LoRA
|
||||
(below) is the fast option.
|
||||
- Resolutions: width and height must be multiples of 16; the examples use
|
||||
multiples of 64. 832x480 is a fast starting point; generation cost scales
|
||||
with pixel area.
|
||||
- `--audio` accepts a WAV file; it is downmixed to mono and resampled to 16 kHz
|
||||
internally. Audio longer than the video is truncated, video longer than the
|
||||
audio is padded with silence. Pick `--video-frames` to match the audio:
|
||||
roughly `audio_seconds * 16` frames, capped at one chunk (77-81 frames,
|
||||
~5 s at the model's 16 fps). 33, 77 and 81 map to clean latent frame counts.
|
||||
- S2V always uses 16 fps. Other requested frame rates are automatically
|
||||
changed to 16 with a warning, including the CLI and server video output.
|
||||
`generate_video()` returns the actual frame rate through `fps_out`; C API
|
||||
callers should use that value when encoding the output video.
|
||||
- One generation covers the first S2V chunk window (`--video-frames` frames).
|
||||
Long-video chunked extend mode is not implemented yet.
|
||||
- Speed: the lightx2v lightning LoRA works with S2V at 4 steps and
|
||||
`--cfg-scale 1.0`. Use the **low_noise** variant;
|
||||
the high_noise variant produces artifacts on S2V:
|
||||
|
||||
```
|
||||
--lora-model-dir ..\models\loras
|
||||
-p "...<lora:lightx2v-Wan2.2-T2V-A14B-4steps-lora-rank64-Seko-V2.0-low_noise:1.0>"
|
||||
--cfg-scale 1.0 --steps 4
|
||||
```
|
||||
|
||||
Expect some quality/dynamics loss compared to the full 20-step run.
|
||||
|
||||
### Wan2.2 T2V A14B T2I
|
||||
|
||||
```
|
||||
|
||||
@@ -6,6 +6,11 @@ For detailed command-line arguments, run:
|
||||
./bin/sd-cli -h
|
||||
```
|
||||
|
||||
Logging defaults to `info`. Use `--log-level <level>` to select `debug`, `verbose`,
|
||||
`info`, `warn`, or `error` (from most to least detailed). Each level includes
|
||||
messages at that level and all less detailed levels. `-v` and `--verbose` are
|
||||
equivalent to `--log-level verbose`. If repeated, the last logging option wins.
|
||||
|
||||
For direct image repair or automatic post-generation YOLOv8 detection followed by cropped inpainting, see
|
||||
[ADetailer](../../docs/adetailer.md).
|
||||
|
||||
@@ -17,3 +22,10 @@ Metadata mode inspects PNG/JPEG container metadata without loading any model:
|
||||
./bin/sd-cli -M metadata --image ./output.png --metadata-raw
|
||||
./bin/sd-cli -M metadata --image ./output.png --metadata-all
|
||||
```
|
||||
|
||||
For completely black or white images or videos, NaNs, and the `--linear-scale` /
|
||||
`--attn-scale` workaround, see [Troubleshooting](../../docs/troubleshooting.md).
|
||||
|
||||
For native CUDA sparse attention in the diffusion model, use `--sol-attn`.
|
||||
See [Sol-Attn](../../docs/sol_attention.md) for requirements, supported shapes,
|
||||
and the `--sol-attn-tau` threshold coefficient.
|
||||
|
||||
+107
-51
@@ -36,12 +36,13 @@ struct SDCliParams {
|
||||
SDMode mode = IMG_GEN;
|
||||
std::string output_path = "output.png";
|
||||
int output_begin_idx = -1;
|
||||
int compression_quality = 90;
|
||||
std::string image_path;
|
||||
std::string metadata_format = "text";
|
||||
|
||||
bool verbose = false;
|
||||
bool canny_preprocess = false;
|
||||
bool convert_name = false;
|
||||
sd_log_level_t log_level = SD_LOG_INFO;
|
||||
bool canny_preprocess = false;
|
||||
bool convert_name = false;
|
||||
|
||||
preview_t preview_method = PREVIEW_NONE;
|
||||
int preview_interval = 1;
|
||||
@@ -80,7 +81,7 @@ struct SDCliParams {
|
||||
&metadata_format},
|
||||
{"",
|
||||
"--preview-path",
|
||||
"path to write preview image to (default: ./preview.png). Multi-frame previews support .avi, .webm, and animated .webp",
|
||||
"path to write preview image to (default: ./preview.png). For image generation, the filename can have %03d placeholder for sequential numbering. Multi-frame previews support .avi, .webm, and animated .webp",
|
||||
0,
|
||||
&preview_path},
|
||||
{"",
|
||||
@@ -93,12 +94,16 @@ struct SDCliParams {
|
||||
options.int_options = {
|
||||
{"",
|
||||
"--preview-interval",
|
||||
"interval in denoising steps between consecutive updates of the image preview file (default is 1, meaning updating at every step)",
|
||||
"preview interval: in each sampling pass, positive N updates every Nth denoiser step and -N previews only completed logical step N; 0 previews the final completed step of the first pass (base-resolution or high-noise). Default: 1",
|
||||
&preview_interval},
|
||||
{"",
|
||||
"--output-begin-idx",
|
||||
"starting index for output image sequence, must be non-negative (default 0 if specified %d in output path, 1 otherwise)",
|
||||
&output_begin_idx},
|
||||
{"",
|
||||
"--compression-quality",
|
||||
"compression quality of video and JPEG / WebP images (90 by default)",
|
||||
&compression_quality},
|
||||
};
|
||||
|
||||
options.bool_options = {
|
||||
@@ -110,10 +115,6 @@ struct SDCliParams {
|
||||
"--convert-name",
|
||||
"convert tensor name (for convert mode)",
|
||||
true, &convert_name},
|
||||
{"-v",
|
||||
"--verbose",
|
||||
"print extra info",
|
||||
true, &verbose},
|
||||
{"",
|
||||
"--color",
|
||||
"colors the logging tags according to level",
|
||||
@@ -215,6 +216,7 @@ struct SDCliParams {
|
||||
on_imatrix_in_arg},
|
||||
};
|
||||
|
||||
add_log_options(options, log_level);
|
||||
return options;
|
||||
};
|
||||
|
||||
@@ -264,7 +266,7 @@ struct SDCliParams {
|
||||
<< " output_path: \"" << output_path << "\",\n"
|
||||
<< " image_path: \"" << image_path << "\",\n"
|
||||
<< " metadata_format: \"" << metadata_format << "\",\n"
|
||||
<< " verbose: " << (verbose ? "true" : "false") << ",\n"
|
||||
<< " log_level: " << log_level_name(log_level) << ",\n"
|
||||
<< " color: " << (color ? "true" : "false") << ",\n"
|
||||
<< " canny_preprocess: " << (canny_preprocess ? "true" : "false") << ",\n"
|
||||
<< " convert_name: " << (convert_name ? "true" : "false") << ",\n"
|
||||
@@ -302,6 +304,9 @@ void parse_args(int argc, const char** argv, SDCliParams& cli_params, SDContextP
|
||||
exit(cli_params.normal_exit ? 0 : 1);
|
||||
}
|
||||
|
||||
log_level = cli_params.log_level;
|
||||
log_color = cli_params.color;
|
||||
|
||||
bool valid = cli_params.resolve_and_validate();
|
||||
if (valid && cli_params.mode != METADATA) {
|
||||
valid = ctx_params.resolve_and_validate(cli_params.mode) &&
|
||||
@@ -318,15 +323,14 @@ void parse_args(int argc, const char** argv, SDCliParams& cli_params, SDContextP
|
||||
|
||||
void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
|
||||
SDCliParams* cli_params = (SDCliParams*)data;
|
||||
log_print(level, log, cli_params->verbose, cli_params->color);
|
||||
log_print(level, log, cli_params->log_level, cli_params->color);
|
||||
}
|
||||
|
||||
bool load_images_from_dir(const std::string dir,
|
||||
std::vector<SDImageOwner>& images,
|
||||
int expected_width = 0,
|
||||
int expected_height = 0,
|
||||
int max_image_num = 0,
|
||||
bool verbose = false) {
|
||||
int max_image_num = 0) {
|
||||
if (!fs::exists(dir) || !fs::is_directory(dir)) {
|
||||
LOG_ERROR("'%s' is not a valid directory\n", dir.c_str());
|
||||
return false;
|
||||
@@ -350,7 +354,7 @@ bool load_images_from_dir(const std::string dir,
|
||||
std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
|
||||
|
||||
if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".bmp" || ext == ".webp") {
|
||||
LOG_DEBUG("load image %zu from '%s'", images.size(), path.c_str());
|
||||
LOG_VERBOSE("load image %zu from '%s'", images.size(), path.c_str());
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
uint8_t* image_buffer = load_image_from_file(path.c_str(), width, height, expected_width, expected_height);
|
||||
@@ -372,27 +376,6 @@ bool load_images_from_dir(const std::string dir,
|
||||
return true;
|
||||
}
|
||||
|
||||
void step_callback(int step, int frame_count, sd_image_t* image, bool is_noisy, void* data) {
|
||||
(void)step;
|
||||
(void)is_noisy;
|
||||
SDCliParams* cli_params = (SDCliParams*)data;
|
||||
// is_noisy is set to true if the preview corresponds to noisy latents, false if it's denoised latents
|
||||
// unused in this app, it will either be always noisy or always denoised here
|
||||
if (frame_count == 1) {
|
||||
if (!write_image_to_file(cli_params->preview_path,
|
||||
image->data,
|
||||
image->width,
|
||||
image->height,
|
||||
image->channel)) {
|
||||
LOG_ERROR("save preview image to '%s' failed", cli_params->preview_path.c_str());
|
||||
}
|
||||
} else {
|
||||
if (create_video_from_sd_images(cli_params->preview_path.c_str(), image, frame_count, cli_params->preview_fps) != 0) {
|
||||
LOG_ERROR("save preview video to '%s' failed", cli_params->preview_path.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string format_frame_idx(std::string pattern, int frame_idx) {
|
||||
std::smatch match;
|
||||
std::string result = pattern;
|
||||
@@ -412,6 +395,37 @@ std::string format_frame_idx(std::string pattern, int frame_idx) {
|
||||
return result;
|
||||
}
|
||||
|
||||
int continuous_preview_counter = 0;
|
||||
|
||||
void step_callback(int step, int frame_count, sd_image_t* image, bool is_noisy, void* data) {
|
||||
(void)step;
|
||||
(void)is_noisy;
|
||||
SDCliParams* cli_params = (SDCliParams*)data;
|
||||
// is_noisy is set to true if the preview corresponds to noisy latents, false if it's denoised latents
|
||||
// unused in this app, it will either be always noisy or always denoised here
|
||||
if (frame_count == 1) {
|
||||
fs::path path = cli_params->preview_path;
|
||||
if (encoded_image_format_from_path(path.string()) == EncodedImageFormat::UNKNOWN)
|
||||
path += ".png";
|
||||
if (std::regex_search(path.string(), format_specifier_regex))
|
||||
path = fs::path(format_frame_idx(path.string(), continuous_preview_counter++));
|
||||
if (!write_image_to_file(path.string(),
|
||||
image->data,
|
||||
image->width,
|
||||
image->height,
|
||||
image->channel,
|
||||
"",
|
||||
cli_params->compression_quality)) {
|
||||
LOG_ERROR("save preview image to '%s' failed", path.string().c_str());
|
||||
}
|
||||
} else {
|
||||
int fps = cli_params->preview_method == PREVIEW_PROJ ? cli_params->preview_fps / 4 : cli_params->preview_fps;
|
||||
if (create_video_from_sd_images(cli_params->preview_path.c_str(), image, frame_count, fps, cli_params->compression_quality) != 0) {
|
||||
LOG_ERROR("save preview video to '%s' failed", cli_params->preview_path.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static fs::path get_video_audio_sidecar_path(const SDCliParams& cli_params) {
|
||||
fs::path out_path = cli_params.output_path;
|
||||
fs::path base_path = out_path;
|
||||
@@ -486,7 +500,7 @@ bool save_results(const SDCliParams& cli_params,
|
||||
std::string params = gen_params.embed_image_metadata
|
||||
? get_image_params(ctx_params, gen_params, metadata_seed, cli_params.mode)
|
||||
: "";
|
||||
const bool ok = write_image_to_file(path.string(), img.data, img.width, img.height, img.channel, params, 90);
|
||||
const bool ok = write_image_to_file(path.string(), img.data, img.width, img.height, img.channel, params, cli_params.compression_quality);
|
||||
LOG_INFO("save result image %d to '%s' (%s)", idx, path.string().c_str(), ok ? "success" : "failure");
|
||||
return ok;
|
||||
};
|
||||
@@ -527,12 +541,16 @@ bool save_results(const SDCliParams& cli_params,
|
||||
if (cli_params.mode == VID_GEN && num_results > 1) {
|
||||
if (ext_lower != ".avi" && ext_lower != ".webp" && ext_lower != ".webm")
|
||||
ext = ".avi";
|
||||
std::string params = gen_params.embed_image_metadata
|
||||
? get_image_params(ctx_params, gen_params, gen_params.seed, cli_params.mode)
|
||||
: "";
|
||||
|
||||
fs::path video_path = base_path;
|
||||
video_path += ext;
|
||||
std::string final_ext_lower = ext.string();
|
||||
std::transform(final_ext_lower.begin(), final_ext_lower.end(), final_ext_lower.begin(), ::tolower);
|
||||
const bool mux_audio = generated_audio != nullptr && (final_ext_lower == ".avi" || final_ext_lower == ".webm");
|
||||
if (create_video_from_sd_images(video_path.string().c_str(), results, num_results, gen_params.fps, 90, mux_audio ? generated_audio : nullptr) == 0) {
|
||||
if (create_video_from_sd_images(video_path.string().c_str(), results, num_results, gen_params.fps, cli_params.compression_quality, mux_audio ? generated_audio : nullptr, params) == 0) {
|
||||
LOG_INFO("save result video to '%s'", video_path.string().c_str());
|
||||
if (generated_audio != nullptr && !mux_audio) {
|
||||
fs::path wav_path = video_path;
|
||||
@@ -637,8 +655,6 @@ int main(int argc, const char* argv[]) {
|
||||
|
||||
parse_args(argc, argv, cli_params, ctx_params, gen_params);
|
||||
sd_set_log_callback(sd_log_cb, (void*)&cli_params);
|
||||
log_verbose = cli_params.verbose;
|
||||
log_color = cli_params.color;
|
||||
|
||||
if (cli_params.mode == METADATA) {
|
||||
MetadataReadOptions options;
|
||||
@@ -676,8 +692,6 @@ int main(int argc, const char* argv[]) {
|
||||
}
|
||||
}
|
||||
cli_params.preview_fps = gen_params.fps;
|
||||
if (cli_params.preview_method == PREVIEW_PROJ)
|
||||
cli_params.preview_fps /= 4;
|
||||
|
||||
sd_set_preview_callback(step_callback,
|
||||
cli_params.preview_method,
|
||||
@@ -686,11 +700,11 @@ int main(int argc, const char* argv[]) {
|
||||
cli_params.preview_noisy,
|
||||
(void*)&cli_params);
|
||||
|
||||
LOG_DEBUG("version: %s", version_string().c_str());
|
||||
LOG_DEBUG("%s", sd_get_system_info());
|
||||
LOG_DEBUG("%s", cli_params.to_string().c_str());
|
||||
LOG_DEBUG("%s", ctx_params.to_string().c_str());
|
||||
LOG_DEBUG("%s", gen_params.to_string().c_str());
|
||||
LOG_VERBOSE("version: %s", version_string().c_str());
|
||||
LOG_VERBOSE("%s", sd_get_system_info());
|
||||
LOG_VERBOSE("%s", cli_params.to_string().c_str());
|
||||
LOG_VERBOSE("%s", ctx_params.to_string().c_str());
|
||||
LOG_VERBOSE("%s", gen_params.to_string().c_str());
|
||||
|
||||
if (!cli_params.imatrix_out.empty()) {
|
||||
if (fs::exists(cli_params.imatrix_out) &&
|
||||
@@ -754,6 +768,18 @@ int main(int argc, const char* argv[]) {
|
||||
return true;
|
||||
};
|
||||
|
||||
auto load_audio = [&](const std::string& path, SDAudioOwner& audio) -> bool {
|
||||
std::vector<float> samples;
|
||||
uint32_t sample_rate = 0;
|
||||
uint32_t channels = 0;
|
||||
if (!load_wav_from_file(path, samples, sample_rate, channels)) {
|
||||
LOG_ERROR("load WAV audio from '%s' failed", path.c_str());
|
||||
return false;
|
||||
}
|
||||
audio.reset(std::move(samples), sample_rate, channels);
|
||||
return true;
|
||||
};
|
||||
|
||||
if (gen_params.init_image_path.size() > 0) {
|
||||
if (!load_image_and_update_size(gen_params.init_image_path, gen_params.init_image)) {
|
||||
return 1;
|
||||
@@ -777,6 +803,37 @@ int main(int argc, const char* argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
if (!gen_params.ref_video_paths.empty()) {
|
||||
gen_params.ref_videos.clear();
|
||||
gen_params.ref_videos.reserve(gen_params.ref_video_paths.size());
|
||||
for (const auto& path : gen_params.ref_video_paths) {
|
||||
std::vector<SDImageOwner> frames;
|
||||
if (!load_images_from_dir(path, frames) || frames.empty()) {
|
||||
LOG_ERROR("load reference video frames from '%s' failed", path.c_str());
|
||||
return 1;
|
||||
}
|
||||
gen_params.ref_videos.push_back(std::move(frames));
|
||||
}
|
||||
|
||||
gen_params.ref_video_audios.clear();
|
||||
gen_params.ref_video_audios.resize(gen_params.ref_videos.size());
|
||||
for (size_t i = 0; i < gen_params.ref_video_audio_paths.size(); ++i) {
|
||||
if (!load_audio(gen_params.ref_video_audio_paths[i], gen_params.ref_video_audios[i])) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!gen_params.ref_audio_paths.empty()) {
|
||||
gen_params.ref_audios.clear();
|
||||
gen_params.ref_audios.resize(gen_params.ref_audio_paths.size());
|
||||
for (size_t i = 0; i < gen_params.ref_audio_paths.size(); ++i) {
|
||||
if (!load_audio(gen_params.ref_audio_paths[i], gen_params.ref_audios[i])) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gen_params.mask_image_path.size() > 0) {
|
||||
if (!load_sd_image_from_file(gen_params.mask_image.put(),
|
||||
gen_params.mask_image_path.c_str(),
|
||||
@@ -833,8 +890,7 @@ int main(int argc, const char* argv[]) {
|
||||
gen_params.control_frames,
|
||||
gen_params.get_resolved_width(),
|
||||
gen_params.get_resolved_height(),
|
||||
gen_params.video_frames,
|
||||
cli_params.verbose)) {
|
||||
gen_params.video_frames)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -845,8 +901,7 @@ int main(int argc, const char* argv[]) {
|
||||
gen_params.pm_id_images,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
cli_params.verbose)) {
|
||||
0)) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -899,9 +954,10 @@ int main(int argc, const char* argv[]) {
|
||||
} else if (cli_params.mode == VID_GEN) {
|
||||
sd_vid_gen_params_t vid_gen_params = gen_params.to_sd_vid_gen_params_t();
|
||||
sd_image_t* generated_video = nullptr;
|
||||
if (!generate_video(sd_ctx.get(), &vid_gen_params, &generated_video, &num_results, &generated_audio)) {
|
||||
if (!generate_video(sd_ctx.get(), &vid_gen_params, &generated_video, &num_results, &generated_audio, &cli_params.preview_fps)) {
|
||||
generated_video = nullptr;
|
||||
}
|
||||
gen_params.fps = cli_params.preview_fps;
|
||||
results.adopt(generated_video, num_results);
|
||||
}
|
||||
|
||||
|
||||
+259
-27
@@ -239,6 +239,26 @@ void ArgOptions::print() const {
|
||||
}
|
||||
}
|
||||
|
||||
void add_log_options(ArgOptions& options, sd_log_level_t& level) {
|
||||
options.manual_options.push_back({"", "--log-level",
|
||||
"minimum log level, one of [debug, verbose, info, warn, error] (default: info)",
|
||||
[&level](int argc, const char** argv, int index) {
|
||||
if (++index >= argc) {
|
||||
return -1;
|
||||
}
|
||||
if (!parse_log_level(argv[index], level)) {
|
||||
LOG_ERROR("invalid log level %s, must be one of [debug, verbose, info, warn, error]", argv[index]);
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}});
|
||||
options.manual_options.push_back({"-v", "--verbose", "equivalent to --log-level verbose",
|
||||
[&level](int, const char**, int) {
|
||||
level = SD_LOG_VERBOSE;
|
||||
return 0;
|
||||
}});
|
||||
}
|
||||
|
||||
bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& options_list) {
|
||||
bool invalid_arg = false;
|
||||
std::string arg;
|
||||
@@ -282,8 +302,12 @@ bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& o
|
||||
invalid_arg = true;
|
||||
return;
|
||||
}
|
||||
*option.target = std::stoi(argv[i]);
|
||||
found_arg = true;
|
||||
try {
|
||||
*option.target = std::stoi(argv[i]);
|
||||
} catch (const std::invalid_argument&) {
|
||||
invalid_arg = true;
|
||||
}
|
||||
found_arg = true;
|
||||
}))
|
||||
break;
|
||||
|
||||
@@ -292,8 +316,12 @@ bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& o
|
||||
invalid_arg = true;
|
||||
return;
|
||||
}
|
||||
*option.target = std::stof(argv[i]);
|
||||
found_arg = true;
|
||||
try {
|
||||
*option.target = std::stof(argv[i]);
|
||||
} catch (const std::invalid_argument&) {
|
||||
invalid_arg = true;
|
||||
}
|
||||
found_arg = true;
|
||||
}))
|
||||
break;
|
||||
|
||||
@@ -317,7 +345,8 @@ bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& o
|
||||
|
||||
if (invalid_arg) {
|
||||
if (!valid) {
|
||||
LOG_ERROR("error: invalid parameter for argument: %s", arg.c_str());
|
||||
LOG_ERROR("error: invalid parameter for argument \"%s\": \"%s\"",
|
||||
arg.c_str(), (i >= argc) ? "" : argv[i]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -330,6 +359,25 @@ bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& o
|
||||
return true;
|
||||
}
|
||||
|
||||
static int parse_scale_override(int argc, const char** argv, int index, float& scale) {
|
||||
if (++index >= argc) {
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
size_t end = 0;
|
||||
const std::string value = argv[index];
|
||||
float parsed = std::stof(value, &end);
|
||||
if (end != value.size() || !std::isfinite(parsed) || parsed < 0.f ||
|
||||
(parsed > 0.f && !std::isfinite(1.f / parsed))) {
|
||||
return -1;
|
||||
}
|
||||
scale = parsed;
|
||||
} catch (const std::exception&) {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
ArgOptions SDContextParams::get_options() {
|
||||
ArgOptions options;
|
||||
options.string_options = {
|
||||
@@ -362,6 +410,11 @@ ArgOptions SDContextParams::get_options() {
|
||||
"path to the llm text encoder. For example: (qwenvl2.5 for qwen-image, mistral-small3.2 for flux2, ...)",
|
||||
0,
|
||||
&llm_path},
|
||||
{"",
|
||||
"--tokenizer",
|
||||
"tokenizer.json path, or comma-separated main=FILE,clip-l=FILE,clip-g=FILE assignments; required for PiD and Lens",
|
||||
(int)',',
|
||||
&tokenizer},
|
||||
{"",
|
||||
"--llm_vision",
|
||||
"path to the llm vit",
|
||||
@@ -412,6 +465,11 @@ ArgOptions SDContextParams::get_options() {
|
||||
"path to standalone LTX audio vae model",
|
||||
0,
|
||||
&audio_vae_path},
|
||||
{"",
|
||||
"--audio-encoder",
|
||||
"path to wav2vec2 audio encoder model (Wan2.2 S2V)",
|
||||
0,
|
||||
&audio_encoder_path},
|
||||
{"",
|
||||
"--taesd",
|
||||
"path to taesd. Using Tiny AutoEncoder for fast decoding (low quality)",
|
||||
@@ -502,7 +560,7 @@ ArgOptions SDContextParams::get_options() {
|
||||
&rpc_servers},
|
||||
{"",
|
||||
"--max-vram",
|
||||
"maximum VRAM budget in GiB for graph-cut segmented execution. Accepts a single value or assignments by backend/device, e.g. 6 or cuda0=6,vulkan0=4. 0 disables graph splitting; a negative value auto-detects free VRAM, sparing the specified value",
|
||||
"optional per-device budget in GiB for managed weights and runner buffers during automatic graph-cut execution. Accepts a single value or assignments by backend/device, e.g. 6 or cuda0=6,vulkan0=4. 0 uses live free VRAM without an explicit budget; a negative value reserves that much free VRAM",
|
||||
0,
|
||||
&max_vram},
|
||||
};
|
||||
@@ -517,19 +575,17 @@ ArgOptions SDContextParams::get_options() {
|
||||
|
||||
options.bool_options = {
|
||||
{"",
|
||||
"--stream-layers",
|
||||
"enable residency+prefetch streaming on top of --max-vram (no effect without --max-vram; defaults to false)",
|
||||
true, &stream_layers},
|
||||
"--disable-prefetch",
|
||||
"disable asynchronous next-segment weight prefetch (defaults to false)",
|
||||
true, &disable_prefetch},
|
||||
{"",
|
||||
"--disable-segmented-compute",
|
||||
"force monolithic graph execution even when automatic graph cutting is needed (defaults to false)",
|
||||
true, &disable_segmented_compute},
|
||||
{"",
|
||||
"--eager-load",
|
||||
"load all params into the params backend at model-load time instead of lazily on first use (defaults to false)",
|
||||
true, &eager_load},
|
||||
{"",
|
||||
"--auto-fit",
|
||||
"pick the diffusion/te/vae device placements automatically from the model size and the per-device "
|
||||
"memory budgets (--max-vram; defaults to free memory minus a small margin). Overrides --backend and "
|
||||
"--params-backend; may split modules across GPUs (--split-mode still selects layer or row)",
|
||||
true, &auto_fit},
|
||||
{"",
|
||||
"--force-sdxl-vae-conv-scale",
|
||||
"force use of conv scale on sdxl vae",
|
||||
@@ -562,16 +618,41 @@ ArgOptions SDContextParams::get_options() {
|
||||
"--diffusion-fa",
|
||||
"use flash attention in the diffusion model only",
|
||||
true, &diffusion_flash_attn},
|
||||
{"",
|
||||
"--sage-attn",
|
||||
"use native CUDA SageAttention in the diffusion model, with flash/default attention fallback",
|
||||
true, &sage_attn},
|
||||
{"",
|
||||
"--sol-attn",
|
||||
"use native CUDA Sol-Attn in the diffusion model, with flash/default attention fallback",
|
||||
true, &sol_attn},
|
||||
{"",
|
||||
"--diffusion-conv-direct",
|
||||
"use ggml_conv2d_direct in the diffusion model",
|
||||
true, &diffusion_conv_direct},
|
||||
{"",
|
||||
"--vae-conv-direct",
|
||||
"use ggml_conv2d_direct in the vae model",
|
||||
"use direct 2D and 3D convolutions in the vae model",
|
||||
true, &vae_conv_direct},
|
||||
};
|
||||
|
||||
auto on_auto_fit_arg = [&](int argc, const char** argv, int index) {
|
||||
if (++index >= argc) {
|
||||
LOG_ERROR("--auto-fit requires 'on' or 'off'");
|
||||
return -1;
|
||||
}
|
||||
const std::string arg = argv[index];
|
||||
if (arg == "on") {
|
||||
auto_fit = true;
|
||||
} else if (arg == "off") {
|
||||
auto_fit = false;
|
||||
} else {
|
||||
LOG_ERROR("invalid --auto-fit value '%s'; expected 'on' or 'off'", argv[index]);
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
};
|
||||
|
||||
auto on_type_arg = [&](int argc, const char** argv, int index) {
|
||||
if (++index >= argc) {
|
||||
return -1;
|
||||
@@ -642,7 +723,27 @@ ArgOptions SDContextParams::get_options() {
|
||||
return 1;
|
||||
};
|
||||
|
||||
options.float_options.push_back({"", "--sol-attn-tau", "Sol-Attn routing threshold coefficient (default: 1; higher selects fewer exact blocks)", &sol_attn_tau});
|
||||
|
||||
options.manual_options = {
|
||||
{"",
|
||||
"--linear-scale",
|
||||
"linear input scale override (float, default: 0 = model default, 1 = no scaling)",
|
||||
[this](int argc, const char** argv, int index) {
|
||||
return parse_scale_override(argc, argv, index, linear_scale);
|
||||
}},
|
||||
{"",
|
||||
"--attn-scale",
|
||||
"flash-attention K/V scale override (float, default: 0 = model default, 1 = no scaling); requires --fa or --diffusion-fa",
|
||||
[this](int argc, const char** argv, int index) {
|
||||
return parse_scale_override(argc, argv, index, attn_scale);
|
||||
}},
|
||||
{"",
|
||||
"--auto-fit",
|
||||
"on|off (default: on). Preserve --backend (otherwise select one GPU) and place weights on the compute GPU, "
|
||||
"RAM, another GPU, or disk in that order, according to available memory (--max-vram limits GPU budgets). "
|
||||
"Disabled by explicit --params-backend; uses automatic graph segmentation when needed",
|
||||
on_auto_fit_arg},
|
||||
{"",
|
||||
"--type",
|
||||
"weight type (examples: f32, f16, q4_0, q4_1, q5_0, q5_1, q8_0, q2_K, q3_K, q4_K). "
|
||||
@@ -727,6 +828,14 @@ bool SDContextParams::resolve(SDMode mode) {
|
||||
}
|
||||
|
||||
bool SDContextParams::validate(SDMode mode) {
|
||||
if (sol_attn && sage_attn) {
|
||||
LOG_ERROR("--sol-attn and --sage-attn cannot be enabled together");
|
||||
return false;
|
||||
}
|
||||
if (!std::isfinite(sol_attn_tau)) {
|
||||
LOG_ERROR("--sol-attn-tau must be finite");
|
||||
return false;
|
||||
}
|
||||
if (mode == CONVERT) {
|
||||
const bool has_convert_input = model_path.length() != 0 ||
|
||||
clip_l_path.length() != 0 ||
|
||||
@@ -810,6 +919,7 @@ std::string SDContextParams::to_string() const {
|
||||
<< " t5xxl_path: \"" << t5xxl_path << "\",\n"
|
||||
<< " llm_path: \"" << llm_path << "\",\n"
|
||||
<< " llm_vision_path: \"" << llm_vision_path << "\",\n"
|
||||
<< " tokenizer: \"" << tokenizer << "\",\n"
|
||||
<< " diffusion_model_path: \"" << diffusion_model_path << "\",\n"
|
||||
<< " high_noise_diffusion_model_path: \"" << high_noise_diffusion_model_path << "\",\n"
|
||||
<< " uncond_diffusion_model_path: \"" << uncond_diffusion_model_path << "\",\n"
|
||||
@@ -817,6 +927,7 @@ std::string SDContextParams::to_string() const {
|
||||
<< " vae_path: \"" << vae_path << "\",\n"
|
||||
<< " vae_format: \"" << vae_format << "\",\n"
|
||||
<< " audio_vae_path: \"" << audio_vae_path << "\",\n"
|
||||
<< " audio_encoder_path: \"" << audio_encoder_path << "\",\n"
|
||||
<< " taesd_path: \"" << taesd_path << "\",\n"
|
||||
<< " esrgan_path: \"" << esrgan_path << "\",\n"
|
||||
<< " control_net_path: \"" << control_net_path << "\",\n"
|
||||
@@ -831,7 +942,8 @@ std::string SDContextParams::to_string() const {
|
||||
<< " sampler_rng_type: " << sd_rng_type_name(sampler_rng_type) << ",\n"
|
||||
<< " offload_params_to_cpu: " << (offload_params_to_cpu ? "true" : "false") << ",\n"
|
||||
<< " max_vram: \"" << max_vram << "\",\n"
|
||||
<< " stream_layers: " << (stream_layers ? "true" : "false") << ",\n"
|
||||
<< " disable_prefetch: " << (disable_prefetch ? "true" : "false") << ",\n"
|
||||
<< " disable_segmented_compute: " << (disable_segmented_compute ? "true" : "false") << ",\n"
|
||||
<< " eager_load: " << (eager_load ? "true" : "false") << ",\n"
|
||||
<< " backend: \"" << backend << "\",\n"
|
||||
<< " params_backend: \"" << params_backend << "\",\n"
|
||||
@@ -844,6 +956,11 @@ std::string SDContextParams::to_string() const {
|
||||
<< " vae_on_cpu: " << (vae_on_cpu ? "true" : "false") << ",\n"
|
||||
<< " flash_attn: " << (flash_attn ? "true" : "false") << ",\n"
|
||||
<< " diffusion_flash_attn: " << (diffusion_flash_attn ? "true" : "false") << ",\n"
|
||||
<< " sage_attn: " << (sage_attn ? "true" : "false") << ",\n"
|
||||
<< " sol_attn: " << (sol_attn ? "true" : "false") << ",\n"
|
||||
<< " sol_attn_tau: " << sol_attn_tau << ",\n"
|
||||
<< " linear_scale: " << linear_scale << ",\n"
|
||||
<< " attn_scale: " << attn_scale << ",\n"
|
||||
<< " diffusion_conv_direct: " << (diffusion_conv_direct ? "true" : "false") << ",\n"
|
||||
<< " vae_conv_direct: " << (vae_conv_direct ? "true" : "false") << ",\n"
|
||||
<< " prediction: " << sd_prediction_name(prediction) << ",\n"
|
||||
@@ -873,12 +990,14 @@ sd_ctx_params_t SDContextParams::to_sd_ctx_params_t(bool taesd_preview) {
|
||||
sd_ctx_params.t5xxl_path = t5xxl_path.c_str();
|
||||
sd_ctx_params.llm_path = llm_path.c_str();
|
||||
sd_ctx_params.llm_vision_path = llm_vision_path.c_str();
|
||||
sd_ctx_params.tokenizer = tokenizer.c_str();
|
||||
sd_ctx_params.diffusion_model_path = diffusion_model_path.c_str();
|
||||
sd_ctx_params.high_noise_diffusion_model_path = high_noise_diffusion_model_path.c_str();
|
||||
sd_ctx_params.uncond_diffusion_model_path = uncond_diffusion_model_path.c_str();
|
||||
sd_ctx_params.embeddings_connectors_path = embeddings_connectors_path.c_str();
|
||||
sd_ctx_params.vae_path = vae_path.c_str();
|
||||
sd_ctx_params.audio_vae_path = audio_vae_path.c_str();
|
||||
sd_ctx_params.audio_encoder_path = audio_encoder_path.c_str();
|
||||
sd_ctx_params.taesd_path = taesd_path.c_str();
|
||||
sd_ctx_params.control_net_path = control_net_path.c_str();
|
||||
sd_ctx_params.ip_adapter_path = ip_adapter_path.c_str();
|
||||
@@ -897,13 +1016,19 @@ sd_ctx_params_t SDContextParams::to_sd_ctx_params_t(bool taesd_preview) {
|
||||
sd_ctx_params.enable_mmap = enable_mmap;
|
||||
sd_ctx_params.flash_attn = flash_attn;
|
||||
sd_ctx_params.diffusion_flash_attn = diffusion_flash_attn;
|
||||
sd_ctx_params.sage_attn = sage_attn;
|
||||
sd_ctx_params.sol_attn = sol_attn;
|
||||
sd_ctx_params.sol_attn_tau = sol_attn_tau;
|
||||
sd_ctx_params.linear_scale = linear_scale;
|
||||
sd_ctx_params.attn_scale = attn_scale;
|
||||
sd_ctx_params.tae_preview_only = taesd_preview;
|
||||
sd_ctx_params.diffusion_conv_direct = diffusion_conv_direct;
|
||||
sd_ctx_params.vae_conv_direct = vae_conv_direct;
|
||||
sd_ctx_params.force_sdxl_vae_conv_scale = force_sdxl_vae_conv_scale;
|
||||
sd_ctx_params.vae_format = str_to_vae_format(vae_format);
|
||||
sd_ctx_params.max_vram = max_vram.c_str();
|
||||
sd_ctx_params.stream_layers = stream_layers;
|
||||
sd_ctx_params.disable_prefetch = disable_prefetch;
|
||||
sd_ctx_params.disable_segmented_compute = disable_segmented_compute;
|
||||
sd_ctx_params.eager_load = eager_load;
|
||||
sd_ctx_params.backend = effective_backend.c_str();
|
||||
sd_ctx_params.params_backend = effective_params_backend.c_str();
|
||||
@@ -1008,12 +1133,12 @@ ArgOptions SDGenerationParams::get_options() {
|
||||
&hires_upscaler},
|
||||
{"",
|
||||
"--extra-sample-args",
|
||||
"extra sampler/scheduler/guidance args, key=value list. CFG supports guidance_schedule; APG supports apg_eta, apg_momentum, apg_norm_threshold, apg_norm_threshold_smoothing; SLG supports slg_uncond; lcm supports noise_clip_std, noise_scale_start, noise_scale_end; flux supports base_shift, max_shift; ltx2 supports max_shift, base_shift, stretch, terminal; euler_ge supports gamma; beta scheduler supports alpha, beta; logit_normal supports mu, std, logsnr_min, logsnr_max, resolution_aware",
|
||||
"extra sampler/scheduler/guidance args, key=value list. CFG supports guidance_schedule; APG supports apg_eta, apg_momentum, apg_norm_threshold, apg_norm_threshold_smoothing; SLG supports slg_uncond; lcm supports noise_clip_std, noise_scale_start, noise_scale_end; flux supports base_shift, max_shift; ltx2 supports max_shift, base_shift, stretch, terminal; euler_ge supports gamma; beta scheduler supports alpha, beta; logit_normal supports mu, std, logsnr_min, logsnr_max, resolution_aware; llada_image supports uniform; lms supports lms_max_order, lms_shift, lms_divisions; noise-injecting samplers support noise_sampler with value iid (default except for dpm++2m_sde_bt) or brownian_tree; brownian_tree_rng supports cpu (default), cuda, std_default or sampler_rng",
|
||||
(int)',',
|
||||
&extra_sample_args},
|
||||
{"",
|
||||
"--extra-tiling-args",
|
||||
"extra VAE tiling args, key=value list. LTX video VAE supports temporal_tile_frames (default: 4), temporal_tile_overlap (default: 1)",
|
||||
"extra VAE tiling args, key=value list. Supported video VAEs accept temporal_tile_frames/temporal_tile_size (default: 4), temporal_tile_overlap (default: 1)",
|
||||
(int)',',
|
||||
&extra_tiling_args},
|
||||
{"",
|
||||
@@ -1230,7 +1355,7 @@ ArgOptions SDGenerationParams::get_options() {
|
||||
&vae_tiling_params.enabled},
|
||||
{"",
|
||||
"--temporal-tiling",
|
||||
"enable temporal tiling for LTX video VAE decode",
|
||||
"enable temporal tiling for supported video VAE decode",
|
||||
true,
|
||||
&vae_tiling_params.temporal_tiling},
|
||||
{"",
|
||||
@@ -1404,6 +1529,38 @@ ArgOptions SDGenerationParams::get_options() {
|
||||
return 1;
|
||||
};
|
||||
|
||||
auto on_ref_video_arg = [&](int argc, const char** argv, int index) {
|
||||
if (++index >= argc) {
|
||||
return -1;
|
||||
}
|
||||
ref_video_paths.push_back(argv[index]);
|
||||
return 1;
|
||||
};
|
||||
|
||||
auto on_ref_video_audio_arg = [&](int argc, const char** argv, int index) {
|
||||
if (++index >= argc) {
|
||||
return -1;
|
||||
}
|
||||
ref_video_audio_paths.push_back(argv[index]);
|
||||
return 1;
|
||||
};
|
||||
|
||||
auto on_ref_audio_arg = [&](int argc, const char** argv, int index) {
|
||||
if (++index >= argc) {
|
||||
return -1;
|
||||
}
|
||||
ref_audio_paths.push_back(argv[index]);
|
||||
return 1;
|
||||
};
|
||||
|
||||
auto on_audio_arg = [&](int argc, const char** argv, int index) {
|
||||
if (++index >= argc) {
|
||||
return -1;
|
||||
}
|
||||
ref_audio_paths.push_back(argv[index]);
|
||||
return 1;
|
||||
};
|
||||
|
||||
auto on_cache_mode_arg = [&](int argc, const char** argv, int index) {
|
||||
if (++index >= argc) {
|
||||
return -1;
|
||||
@@ -1531,6 +1688,16 @@ ArgOptions SDGenerationParams::get_options() {
|
||||
return 1;
|
||||
};
|
||||
|
||||
std::string sample_methods = sample_method_to_str[0];
|
||||
for (int i = 1; i < SAMPLE_METHOD_COUNT; i++) {
|
||||
sample_methods += ", " + std::string(sample_method_to_str[i]);
|
||||
}
|
||||
|
||||
std::string schedulers = scheduler_to_str[0];
|
||||
for (int i = 1; i < SCHEDULER_COUNT; i++) {
|
||||
schedulers += ", " + std::string(scheduler_to_str[i]);
|
||||
}
|
||||
|
||||
options.manual_options = {
|
||||
{"-s",
|
||||
"--seed",
|
||||
@@ -1538,17 +1705,18 @@ ArgOptions SDGenerationParams::get_options() {
|
||||
on_seed_arg},
|
||||
{"",
|
||||
"--sampling-method",
|
||||
"sampling method, one of [euler, euler_a, heun, dpm2, dpm++2s_a, dpm++2m, dpm++2mv2, dpm++2m_sde, dpm++2m_sde_bt, ipndm, ipndm_v, lcm, ddim_trailing, tcd, res_multistep, res_2s, er_sde, euler_cfg_pp, euler_a_cfg_pp]"
|
||||
"(default: euler for Flux/SD3/Wan, euler_a otherwise)",
|
||||
"sampling method, one of [" + sample_methods + "], "
|
||||
"default: euler for Flux/SD3/Wan, euler_a otherwise",
|
||||
on_sample_method_arg},
|
||||
{"",
|
||||
"--high-noise-sampling-method",
|
||||
"(high noise) sampling method, one of [euler, euler_a, heun, dpm2, dpm++2s_a, dpm++2m, dpm++2mv2, dpm++2m_sde, dpm++2m_sde_bt, ipndm, ipndm_v, lcm, ddim_trailing, tcd, res_multistep, res_2s, er_sde, euler_cfg_pp, euler_a_cfg_pp]"
|
||||
" default: euler for Flux/SD3/Wan, euler_a otherwise",
|
||||
"(high noise) sampling method, one of [" + sample_methods + "], "
|
||||
"default: euler for Flux/SD3/Wan, euler_a otherwise",
|
||||
on_high_noise_sample_method_arg},
|
||||
{"",
|
||||
"--scheduler",
|
||||
"denoiser sigma scheduler, one of [discrete, karras, exponential, ays, gits, smoothstep, sgm_uniform, simple, kl_optimal, lcm, bong_tangent, ltx2, logit_normal, flux2, flux, beta], alias: normal=discrete, default: model-specific",
|
||||
"denoiser sigma scheduler, one of [" + schedulers + "], "
|
||||
"alias: normal=discrete, default: model-specific",
|
||||
on_scheduler_arg},
|
||||
{"",
|
||||
"--sigmas",
|
||||
@@ -1568,8 +1736,24 @@ ArgOptions SDGenerationParams::get_options() {
|
||||
on_high_noise_skip_layers_arg},
|
||||
{"-r",
|
||||
"--ref-image",
|
||||
"reference image for Flux Kontext models (can be used multiple times)",
|
||||
"reference image for Flux Kontext or MiniMax-H3 Ref2VA (can be used multiple times)",
|
||||
on_ref_image_arg},
|
||||
{"",
|
||||
"--ref-video",
|
||||
"MiniMax-H3 Ref2VA reference video frame directory at 24 fps (can be used multiple times)",
|
||||
on_ref_video_arg},
|
||||
{"",
|
||||
"--ref-video-audio",
|
||||
"WAV soundtrack paired by index with --ref-video (can be used multiple times)",
|
||||
on_ref_video_audio_arg},
|
||||
{"",
|
||||
"--ref-audio",
|
||||
"standalone WAV reference for MiniMax-H3 Ref2VA (can be used multiple times)",
|
||||
on_ref_audio_arg},
|
||||
{"",
|
||||
"--audio",
|
||||
"driving audio track (Wan2.2 S2V; can be used once)",
|
||||
on_audio_arg},
|
||||
{"",
|
||||
"--cache-mode",
|
||||
"caching method: 'easycache' (DiT), 'ucache' (UNET), 'dbcache'/'taylorseer'/'cache-dit' (DiT block-level), 'spectrum' (UNET/DiT Chebyshev+Taylor forecasting)",
|
||||
@@ -2366,6 +2550,16 @@ bool SDGenerationParams::validate(SDMode mode) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ref_video_audio_paths.size() > ref_video_paths.size()) {
|
||||
LOG_ERROR("error: each --ref-video-audio needs a corresponding --ref-video");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (mode != VID_GEN && (!ref_video_paths.empty() || !ref_video_audio_paths.empty() || !ref_audio_paths.empty())) {
|
||||
LOG_ERROR("error: reference video and audio inputs require vid_gen mode");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (sample_params.shifted_timestep < 0 || sample_params.shifted_timestep > 1000) {
|
||||
LOG_ERROR("error: shifted_timestep must be in range [0, 1000]");
|
||||
return false;
|
||||
@@ -2560,6 +2754,35 @@ sd_vid_gen_params_t SDGenerationParams::to_sd_vid_gen_params_t() {
|
||||
control_frame_views.push_back(frame.get());
|
||||
}
|
||||
|
||||
ref_image_views.clear();
|
||||
ref_image_views.reserve(ref_images.size());
|
||||
for (auto& image : ref_images) {
|
||||
ref_image_views.push_back(image.get());
|
||||
}
|
||||
|
||||
ref_video_frame_views.clear();
|
||||
ref_video_frame_views.resize(ref_videos.size());
|
||||
ref_video_views.clear();
|
||||
ref_video_views.reserve(ref_videos.size());
|
||||
for (size_t i = 0; i < ref_videos.size(); ++i) {
|
||||
auto& frame_views = ref_video_frame_views[i];
|
||||
frame_views.reserve(ref_videos[i].size());
|
||||
for (auto& frame : ref_videos[i]) {
|
||||
frame_views.push_back(frame.get());
|
||||
}
|
||||
sd_audio_t audio = i < ref_video_audios.size() ? ref_video_audios[i].get() : sd_audio_t{};
|
||||
ref_video_views.push_back({frame_views.empty() ? nullptr : frame_views.data(),
|
||||
static_cast<int>(frame_views.size()),
|
||||
24,
|
||||
audio});
|
||||
}
|
||||
|
||||
ref_audio_views.clear();
|
||||
ref_audio_views.reserve(ref_audios.size());
|
||||
for (auto& audio : ref_audios) {
|
||||
ref_audio_views.push_back(audio.get());
|
||||
}
|
||||
|
||||
sample_params.guidance.slg.layers = skip_layers.empty() ? nullptr : skip_layers.data();
|
||||
sample_params.guidance.slg.layer_count = skip_layers.size();
|
||||
high_noise_sample_params.guidance.slg.layers = high_noise_skip_layers.empty() ? nullptr : high_noise_skip_layers.data();
|
||||
@@ -2578,6 +2801,12 @@ sd_vid_gen_params_t SDGenerationParams::to_sd_vid_gen_params_t() {
|
||||
params.clip_skip = clip_skip;
|
||||
params.init_image = init_image.get();
|
||||
params.end_image = end_image.get();
|
||||
params.ref_images = ref_image_views.empty() ? nullptr : ref_image_views.data();
|
||||
params.ref_images_count = static_cast<int>(ref_image_views.size());
|
||||
params.ref_videos = ref_video_views.empty() ? nullptr : ref_video_views.data();
|
||||
params.ref_videos_count = static_cast<int>(ref_video_views.size());
|
||||
params.ref_audios = ref_audio_views.empty() ? nullptr : ref_audio_views.data();
|
||||
params.ref_audios_count = static_cast<int>(ref_audio_views.size());
|
||||
params.control_frames = control_frame_views.empty() ? nullptr : control_frame_views.data();
|
||||
params.control_frames_size = static_cast<int>(control_frame_views.size());
|
||||
params.width = get_resolved_width();
|
||||
@@ -2657,6 +2886,9 @@ std::string SDGenerationParams::to_string() const {
|
||||
<< " mask_image_path: \"" << mask_image_path << "\",\n"
|
||||
<< " control_image_path: \"" << control_image_path << "\",\n"
|
||||
<< " ref_image_paths: " << vec_str_to_string(ref_image_paths) << ",\n"
|
||||
<< " ref_video_paths: " << vec_str_to_string(ref_video_paths) << ",\n"
|
||||
<< " ref_video_audio_paths: " << vec_str_to_string(ref_video_audio_paths) << ",\n"
|
||||
<< " ref_audio_paths: " << vec_str_to_string(ref_audio_paths) << ",\n"
|
||||
<< " control_video_path: \"" << control_video_path << "\",\n"
|
||||
<< " auto_resize_ref_image: " << (auto_resize_ref_image ? "true" : "false") << ",\n"
|
||||
<< " increase_ref_index: " << (increase_ref_index ? "true" : "false") << ",\n"
|
||||
|
||||
@@ -107,6 +107,7 @@ struct ArgOptions {
|
||||
void print() const;
|
||||
};
|
||||
|
||||
void add_log_options(ArgOptions& options, sd_log_level_t& level);
|
||||
bool parse_options(int argc, const char** argv, const std::vector<ArgOptions>& options_list);
|
||||
bool decode_base64_image(const std::string& encoded_input,
|
||||
int target_channels,
|
||||
@@ -123,6 +124,7 @@ struct SDContextParams {
|
||||
std::string t5xxl_path;
|
||||
std::string llm_path;
|
||||
std::string llm_vision_path;
|
||||
std::string tokenizer;
|
||||
std::string diffusion_model_path;
|
||||
std::string high_noise_diffusion_model_path;
|
||||
std::string uncond_diffusion_model_path;
|
||||
@@ -130,6 +132,7 @@ struct SDContextParams {
|
||||
std::string vae_path;
|
||||
std::string vae_format = "auto";
|
||||
std::string audio_vae_path;
|
||||
std::string audio_encoder_path;
|
||||
std::string taesd_path;
|
||||
std::string esrgan_path;
|
||||
std::string control_net_path;
|
||||
@@ -146,17 +149,18 @@ struct SDContextParams {
|
||||
std::map<std::string, std::string> embedding_map;
|
||||
std::vector<sd_embedding_t> embedding_vec;
|
||||
|
||||
rng_type_t rng_type = CUDA_RNG;
|
||||
rng_type_t sampler_rng_type = RNG_TYPE_COUNT;
|
||||
bool offload_params_to_cpu = false;
|
||||
std::string max_vram = "0";
|
||||
bool stream_layers = false;
|
||||
bool eager_load = false;
|
||||
rng_type_t rng_type = CUDA_RNG;
|
||||
rng_type_t sampler_rng_type = RNG_TYPE_COUNT;
|
||||
bool offload_params_to_cpu = false;
|
||||
std::string max_vram = "0";
|
||||
bool disable_prefetch = false;
|
||||
bool disable_segmented_compute = false;
|
||||
bool eager_load = false;
|
||||
std::string backend;
|
||||
std::string params_backend;
|
||||
std::string split_mode;
|
||||
std::string model_args;
|
||||
bool auto_fit = false;
|
||||
bool auto_fit = true;
|
||||
std::string rpc_servers;
|
||||
std::string effective_backend;
|
||||
std::string effective_params_backend;
|
||||
@@ -166,6 +170,9 @@ struct SDContextParams {
|
||||
bool vae_on_cpu = false;
|
||||
bool flash_attn = false;
|
||||
bool diffusion_flash_attn = false;
|
||||
bool sage_attn = false;
|
||||
bool sol_attn = false;
|
||||
float sol_attn_tau = 1.f;
|
||||
bool diffusion_conv_direct = false;
|
||||
bool vae_conv_direct = false;
|
||||
|
||||
@@ -173,6 +180,8 @@ struct SDContextParams {
|
||||
lora_apply_mode_t lora_apply_mode = LORA_APPLY_AUTO;
|
||||
|
||||
bool force_sdxl_vae_conv_scale = false;
|
||||
float linear_scale = 0.f;
|
||||
float attn_scale = 0.f;
|
||||
|
||||
float flow_shift = INFINITY;
|
||||
ArgOptions get_options();
|
||||
@@ -212,6 +221,9 @@ struct SDGenerationParams {
|
||||
std::string control_image_path;
|
||||
std::string ip_adapter_image_path;
|
||||
std::vector<std::string> ref_image_paths;
|
||||
std::vector<std::string> ref_video_paths;
|
||||
std::vector<std::string> ref_video_audio_paths;
|
||||
std::vector<std::string> ref_audio_paths;
|
||||
std::string control_video_path;
|
||||
|
||||
sd_sample_params_t sample_params;
|
||||
@@ -275,6 +287,9 @@ struct SDGenerationParams {
|
||||
SDImageOwner init_image;
|
||||
SDImageOwner end_image;
|
||||
std::vector<SDImageOwner> ref_images;
|
||||
std::vector<std::vector<SDImageOwner>> ref_videos;
|
||||
std::vector<SDAudioOwner> ref_video_audios;
|
||||
std::vector<SDAudioOwner> ref_audios;
|
||||
SDImageOwner mask_image;
|
||||
SDImageOwner control_image;
|
||||
SDImageOwner ip_adapter_image;
|
||||
@@ -283,6 +298,9 @@ struct SDGenerationParams {
|
||||
|
||||
// Backing storage for sd_img_gen_params_t view fields.
|
||||
std::vector<sd_image_t> ref_image_views;
|
||||
std::vector<std::vector<sd_image_t>> ref_video_frame_views;
|
||||
std::vector<sd_ref_video_t> ref_video_views;
|
||||
std::vector<sd_audio_t> ref_audio_views;
|
||||
std::vector<sd_image_t> pm_id_image_views;
|
||||
std::vector<sd_image_t> control_frame_views;
|
||||
|
||||
|
||||
+40
-7
@@ -2,8 +2,8 @@
|
||||
|
||||
#include <vector>
|
||||
|
||||
bool log_verbose = false;
|
||||
bool log_color = false;
|
||||
sd_log_level_t log_level = SD_LOG_INFO;
|
||||
bool log_color = false;
|
||||
|
||||
std::string sd_basename(const std::string& path) {
|
||||
size_t pos = path.find_last_of('/');
|
||||
@@ -51,12 +51,40 @@ void print_utf8(FILE* stream, const char* utf8) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void log_print(enum sd_log_level_t level, const char* log, bool verbose, bool color) {
|
||||
const char* log_level_name(sd_log_level_t level) {
|
||||
switch (level) {
|
||||
case SD_LOG_DEBUG:
|
||||
return "debug";
|
||||
case SD_LOG_VERBOSE:
|
||||
return "verbose";
|
||||
case SD_LOG_INFO:
|
||||
return "info";
|
||||
case SD_LOG_WARN:
|
||||
return "warn";
|
||||
case SD_LOG_ERROR:
|
||||
return "error";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
bool parse_log_level(const std::string& name, sd_log_level_t& level) {
|
||||
const sd_log_level_t levels[] = {SD_LOG_DEBUG, SD_LOG_VERBOSE, SD_LOG_INFO, SD_LOG_WARN, SD_LOG_ERROR};
|
||||
for (sd_log_level_t candidate : levels) {
|
||||
if (name == log_level_name(candidate)) {
|
||||
level = candidate;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void log_print(enum sd_log_level_t level, const char* log, sd_log_level_t min_level, bool color) {
|
||||
int tag_color;
|
||||
const char* level_str;
|
||||
FILE* out_stream = (level == SD_LOG_ERROR) ? stderr : stdout;
|
||||
|
||||
if (!log || (!verbose && level <= SD_LOG_DEBUG)) {
|
||||
if (!log || level < min_level) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -65,6 +93,10 @@ void log_print(enum sd_log_level_t level, const char* log, bool verbose, bool co
|
||||
tag_color = 37;
|
||||
level_str = "DEBUG";
|
||||
break;
|
||||
case SD_LOG_VERBOSE:
|
||||
tag_color = 37;
|
||||
level_str = "VERBOSE";
|
||||
break;
|
||||
case SD_LOG_INFO:
|
||||
tag_color = 34;
|
||||
level_str = "INFO";
|
||||
@@ -84,10 +116,11 @@ void log_print(enum sd_log_level_t level, const char* log, bool verbose, bool co
|
||||
}
|
||||
|
||||
if (color) {
|
||||
fprintf(out_stream, "\033[%d;1m[%-5s]\033[0m ", tag_color, level_str);
|
||||
fprintf(out_stream, "\033[%d;1m[%-7s]\033[0m ", tag_color, level_str);
|
||||
} else {
|
||||
fprintf(out_stream, "[%-5s] ", level_str);
|
||||
fprintf(out_stream, "[%-7s] ", level_str);
|
||||
}
|
||||
fflush(out_stream);
|
||||
print_utf8(out_stream, log);
|
||||
fflush(out_stream);
|
||||
}
|
||||
@@ -109,7 +142,7 @@ void example_log_printf(sd_log_level_t level, const char* file, int line, const
|
||||
strncat(log_buffer, "\n", LOG_BUFFER_SIZE - len);
|
||||
}
|
||||
|
||||
log_print(level, log_buffer, log_verbose, log_color);
|
||||
log_print(level, log_buffer, log_level, log_color);
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@@ -16,15 +16,18 @@
|
||||
|
||||
#include "stable-diffusion.h"
|
||||
|
||||
extern bool log_verbose;
|
||||
extern sd_log_level_t log_level;
|
||||
extern bool log_color;
|
||||
|
||||
std::string sd_basename(const std::string& path);
|
||||
void print_utf8(FILE* stream, const char* utf8);
|
||||
void log_print(sd_log_level_t level, const char* log, bool verbose, bool color);
|
||||
const char* log_level_name(sd_log_level_t level);
|
||||
bool parse_log_level(const std::string& name, sd_log_level_t& level);
|
||||
void log_print(sd_log_level_t level, const char* log, sd_log_level_t min_level, bool color);
|
||||
void example_log_printf(sd_log_level_t level, const char* file, int line, const char* format, ...);
|
||||
|
||||
#define LOG_DEBUG(format, ...) example_log_printf(SD_LOG_DEBUG, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
#define LOG_VERBOSE(format, ...) example_log_printf(SD_LOG_VERBOSE, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
#define LOG_INFO(format, ...) example_log_printf(SD_LOG_INFO, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
#define LOG_WARN(format, ...) example_log_printf(SD_LOG_WARN, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
#define LOG_ERROR(format, ...) example_log_printf(SD_LOG_ERROR, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
|
||||
+185
-11
@@ -810,7 +810,31 @@ uint8_t* load_image_from_memory(const char* image_bytes,
|
||||
return load_image_common(true, image_bytes, len, width, height, expected_width, expected_height, expected_channel);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> create_mjpg_avi_from_sd_images_to_vector(sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio) {
|
||||
static void append_avi_metadata(std::vector<uint8_t>& data, const std::string& parameters) {
|
||||
if (parameters.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> info_content;
|
||||
|
||||
write_fourcc(info_content, "INFO");
|
||||
|
||||
const size_t comment_size = parameters.size() + 1;
|
||||
write_fourcc(info_content, "ICMT");
|
||||
write_u32_le(info_content, static_cast<uint32_t>(comment_size));
|
||||
info_content.insert(info_content.end(), parameters.begin(), parameters.end());
|
||||
info_content.push_back(0);
|
||||
if (comment_size & 1u) {
|
||||
info_content.push_back(0);
|
||||
}
|
||||
|
||||
write_fourcc(data, "LIST");
|
||||
write_u32_le(data, static_cast<uint32_t>(info_content.size()));
|
||||
data.insert(data.end(), info_content.begin(), info_content.end());
|
||||
size_t start_pos = data.size();
|
||||
}
|
||||
|
||||
std::vector<uint8_t> create_mjpg_avi_from_sd_images_to_vector(sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio, const std::string& parameters) {
|
||||
if (num_images == 0) {
|
||||
fprintf(stderr, "Error: Image array is empty.\n");
|
||||
return {};
|
||||
@@ -835,6 +859,9 @@ std::vector<uint8_t> create_mjpg_avi_from_sd_images_to_vector(sd_image_t* images
|
||||
const uint32_t audio_byte_rate = has_audio ? static_cast<uint32_t>(audio->sample_rate * audio_block_align) : 0;
|
||||
const uint32_t audio_data_size = has_audio ? static_cast<uint32_t>(audio_pcm.size()) : 0;
|
||||
|
||||
if (mjpg_quality != quality)
|
||||
LOG_VERBOSE("create_mjpg_avi...(): compression quality was limited from %i to %i", quality, mjpg_quality);
|
||||
|
||||
std::vector<uint8_t> avi_data;
|
||||
avi_data.reserve(static_cast<size_t>(num_images) * 1024);
|
||||
|
||||
@@ -997,6 +1024,8 @@ std::vector<uint8_t> create_mjpg_avi_from_sd_images_to_vector(sd_image_t* images
|
||||
const size_t movi_size = avi_data.size() - movi_size_pos - 4;
|
||||
patch_u32_le(avi_data, movi_size_pos, static_cast<uint32_t>(movi_size));
|
||||
|
||||
append_avi_metadata(avi_data, parameters);
|
||||
|
||||
write_fourcc(avi_data, "idx1");
|
||||
write_u32_le(avi_data, static_cast<uint32_t>(index.size() * 16));
|
||||
for (const auto& entry : index) {
|
||||
@@ -1012,8 +1041,8 @@ std::vector<uint8_t> create_mjpg_avi_from_sd_images_to_vector(sd_image_t* images
|
||||
return avi_data;
|
||||
}
|
||||
|
||||
int create_mjpg_avi_from_sd_images(const char* filename, sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio) {
|
||||
std::vector<uint8_t> avi_data = create_mjpg_avi_from_sd_images_to_vector(images, num_images, fps, quality, audio);
|
||||
int create_mjpg_avi_from_sd_images(const char* filename, sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio, const std::string& parameters) {
|
||||
std::vector<uint8_t> avi_data = create_mjpg_avi_from_sd_images_to_vector(images, num_images, fps, quality, audio, parameters);
|
||||
if (avi_data.empty()) {
|
||||
return -1;
|
||||
}
|
||||
@@ -1143,7 +1172,7 @@ int create_animated_webp_from_sd_images(const char* filename, sd_image_t* images
|
||||
#endif
|
||||
|
||||
#ifdef SD_USE_WEBM
|
||||
std::vector<uint8_t> create_webm_from_sd_images_to_vector(sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio) {
|
||||
std::vector<uint8_t> create_webm_from_sd_images_to_vector(sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio, const std::string& parameters) {
|
||||
if (num_images == 0) {
|
||||
fprintf(stderr, "Error: Image array is empty.\n");
|
||||
return {};
|
||||
@@ -1210,6 +1239,21 @@ std::vector<uint8_t> create_webm_from_sd_images_to_vector(sd_image_t* images, in
|
||||
segment.GetSegmentInfo()->set_writing_app("stable-diffusion.cpp");
|
||||
segment.GetSegmentInfo()->set_muxing_app("stable-diffusion.cpp");
|
||||
|
||||
LOG_DEBUG("Embedding parameters to metadata: %s", parameters.c_str());
|
||||
if (!parameters.empty()) {
|
||||
mkvmuxer::Tag* tag = segment.AddTag();
|
||||
|
||||
if (tag) {
|
||||
if (!tag->add_simple_tag("COMMENT", parameters.c_str())) {
|
||||
LOG_WARN("Failed to add COMMENT simple tag.");
|
||||
}
|
||||
} else {
|
||||
LOG_WARN("Failed to add tag to segment.");
|
||||
}
|
||||
} else {
|
||||
LOG_INFO("Paramaters is empty, COMMENT tag not embedded.\n");
|
||||
}
|
||||
|
||||
const uint64_t frame_duration_ns = std::max<uint64_t>(
|
||||
1, static_cast<uint64_t>(std::llround(1000000000.0 / static_cast<double>(fps))));
|
||||
uint64_t timestamp_ns = 0;
|
||||
@@ -1268,8 +1312,8 @@ std::vector<uint8_t> create_webm_from_sd_images_to_vector(sd_image_t* images, in
|
||||
return writer.data();
|
||||
}
|
||||
|
||||
int create_webm_from_sd_images(const char* filename, sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio) {
|
||||
std::vector<uint8_t> webm_data = create_webm_from_sd_images_to_vector(images, num_images, fps, quality, audio);
|
||||
int create_webm_from_sd_images(const char* filename, sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio, const std::string& parameters) {
|
||||
std::vector<uint8_t> webm_data = create_webm_from_sd_images_to_vector(images, num_images, fps, quality, audio, parameters);
|
||||
if (webm_data.empty()) {
|
||||
return -1;
|
||||
}
|
||||
@@ -1286,7 +1330,8 @@ std::vector<uint8_t> create_video_from_sd_images_to_vector(const std::string& ou
|
||||
int num_images,
|
||||
int fps,
|
||||
int quality,
|
||||
const sd_audio_t* audio) {
|
||||
const sd_audio_t* audio,
|
||||
const std::string& parameters) {
|
||||
std::string format = output_format;
|
||||
std::transform(format.begin(), format.end(), format.begin(),
|
||||
[](unsigned char c) { return static_cast<char>(tolower(c)); });
|
||||
@@ -1296,7 +1341,7 @@ std::vector<uint8_t> create_video_from_sd_images_to_vector(const std::string& ou
|
||||
|
||||
#ifdef SD_USE_WEBM
|
||||
if (format == "webm") {
|
||||
return create_webm_from_sd_images_to_vector(images, num_images, fps, quality, audio);
|
||||
return create_webm_from_sd_images_to_vector(images, num_images, fps, quality, audio, parameters);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1306,14 +1351,14 @@ std::vector<uint8_t> create_video_from_sd_images_to_vector(const std::string& ou
|
||||
}
|
||||
#endif
|
||||
|
||||
return create_mjpg_avi_from_sd_images_to_vector(images, num_images, fps, quality, audio);
|
||||
return create_mjpg_avi_from_sd_images_to_vector(images, num_images, fps, quality, audio, parameters);
|
||||
}
|
||||
|
||||
int create_video_from_sd_images(const char* filename, sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio) {
|
||||
int create_video_from_sd_images(const char* filename, sd_image_t* images, int num_images, int fps, int quality, const sd_audio_t* audio, const std::string& parameters) {
|
||||
std::string path = filename ? filename : "";
|
||||
auto pos = path.find_last_of('.');
|
||||
std::string ext = pos == std::string::npos ? "" : path.substr(pos);
|
||||
std::vector<uint8_t> video_data = create_video_from_sd_images_to_vector(ext, images, num_images, fps, quality, audio);
|
||||
std::vector<uint8_t> video_data = create_video_from_sd_images_to_vector(ext, images, num_images, fps, quality, audio, parameters);
|
||||
if (video_data.empty()) {
|
||||
return -1;
|
||||
}
|
||||
@@ -1374,3 +1419,132 @@ bool write_wav_to_file(const std::string& path,
|
||||
file.write(reinterpret_cast<const char*>(pcm.data()), static_cast<std::streamsize>(pcm.size() * sizeof(int16_t)));
|
||||
return file.good();
|
||||
}
|
||||
|
||||
static uint16_t read_le16(const uint8_t* data) {
|
||||
return static_cast<uint16_t>(data[0]) |
|
||||
(static_cast<uint16_t>(data[1]) << 8);
|
||||
}
|
||||
|
||||
static uint32_t read_le32(const uint8_t* data) {
|
||||
return static_cast<uint32_t>(data[0]) |
|
||||
(static_cast<uint32_t>(data[1]) << 8) |
|
||||
(static_cast<uint32_t>(data[2]) << 16) |
|
||||
(static_cast<uint32_t>(data[3]) << 24);
|
||||
}
|
||||
|
||||
bool load_wav_from_file(const std::string& path,
|
||||
std::vector<float>& interleaved_samples,
|
||||
uint32_t& sample_rate,
|
||||
uint32_t& channels) {
|
||||
interleaved_samples.clear();
|
||||
sample_rate = 0;
|
||||
channels = 0;
|
||||
|
||||
std::ifstream file(path, std::ios::binary);
|
||||
uint8_t riff_header[12];
|
||||
if (!file.read(reinterpret_cast<char*>(riff_header), sizeof(riff_header)) ||
|
||||
std::memcmp(riff_header, "RIFF", 4) != 0 ||
|
||||
std::memcmp(riff_header + 8, "WAVE", 4) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t audio_format = 0;
|
||||
uint16_t bits_per_sample = 0;
|
||||
uint16_t block_align = 0;
|
||||
std::streampos data_pos = std::streampos(-1);
|
||||
uint32_t data_size = 0;
|
||||
|
||||
while (file.good()) {
|
||||
uint8_t chunk_header[8];
|
||||
if (!file.read(reinterpret_cast<char*>(chunk_header), sizeof(chunk_header))) {
|
||||
break;
|
||||
}
|
||||
uint32_t chunk_size = read_le32(chunk_header + 4);
|
||||
std::streampos chunk_data_pos = file.tellg();
|
||||
|
||||
if (std::memcmp(chunk_header, "fmt ", 4) == 0) {
|
||||
if (chunk_size < 16) {
|
||||
return false;
|
||||
}
|
||||
std::vector<uint8_t> fmt(chunk_size);
|
||||
if (!file.read(reinterpret_cast<char*>(fmt.data()), chunk_size)) {
|
||||
return false;
|
||||
}
|
||||
audio_format = read_le16(fmt.data());
|
||||
channels = read_le16(fmt.data() + 2);
|
||||
sample_rate = read_le32(fmt.data() + 4);
|
||||
block_align = read_le16(fmt.data() + 12);
|
||||
bits_per_sample = read_le16(fmt.data() + 14);
|
||||
if (audio_format == 0xfffe && chunk_size >= 40) {
|
||||
audio_format = read_le16(fmt.data() + 24);
|
||||
}
|
||||
} else if (std::memcmp(chunk_header, "data", 4) == 0) {
|
||||
data_pos = chunk_data_pos;
|
||||
data_size = chunk_size;
|
||||
file.seekg(chunk_size, std::ios::cur);
|
||||
} else {
|
||||
file.seekg(chunk_size, std::ios::cur);
|
||||
}
|
||||
|
||||
if (!file.good()) {
|
||||
break;
|
||||
}
|
||||
if ((chunk_size & 1) != 0) {
|
||||
file.seekg(1, std::ios::cur);
|
||||
}
|
||||
}
|
||||
|
||||
const uint32_t bytes_per_sample = (bits_per_sample + 7) / 8;
|
||||
if (data_pos == std::streampos(-1) || data_size == 0 || channels == 0 || sample_rate == 0 ||
|
||||
block_align == 0 || bytes_per_sample == 0 || block_align < channels * bytes_per_sample ||
|
||||
(audio_format != 1 && audio_format != 3)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint64_t frame_count = data_size / block_align;
|
||||
if (frame_count == 0 || frame_count > SIZE_MAX / channels) {
|
||||
return false;
|
||||
}
|
||||
std::vector<uint8_t> pcm(data_size);
|
||||
file.clear();
|
||||
file.seekg(data_pos);
|
||||
if (!file.read(reinterpret_cast<char*>(pcm.data()), data_size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
interleaved_samples.resize(static_cast<size_t>(frame_count * channels));
|
||||
for (uint64_t frame = 0; frame < frame_count; ++frame) {
|
||||
const uint8_t* frame_data = pcm.data() + frame * block_align;
|
||||
for (uint32_t channel = 0; channel < channels; ++channel) {
|
||||
const uint8_t* sample_data = frame_data + channel * bytes_per_sample;
|
||||
float sample = 0.0f;
|
||||
if (audio_format == 3 && bits_per_sample == 32) {
|
||||
std::memcpy(&sample, sample_data, sizeof(sample));
|
||||
} else if (audio_format == 3 && bits_per_sample == 64) {
|
||||
double value;
|
||||
std::memcpy(&value, sample_data, sizeof(value));
|
||||
sample = static_cast<float>(value);
|
||||
} else if (audio_format == 1 && bits_per_sample == 8) {
|
||||
sample = (static_cast<int>(sample_data[0]) - 128) / 128.0f;
|
||||
} else if (audio_format == 1 && bits_per_sample == 16) {
|
||||
sample = static_cast<int16_t>(read_le16(sample_data)) / 32768.0f;
|
||||
} else if (audio_format == 1 && bits_per_sample == 24) {
|
||||
int32_t value = static_cast<int32_t>(sample_data[0]) |
|
||||
(static_cast<int32_t>(sample_data[1]) << 8) |
|
||||
(static_cast<int32_t>(sample_data[2]) << 16);
|
||||
if ((value & 0x800000) != 0) {
|
||||
value |= ~0xffffff;
|
||||
}
|
||||
sample = value / 8388608.0f;
|
||||
} else if (audio_format == 1 && bits_per_sample == 32) {
|
||||
int32_t value = static_cast<int32_t>(read_le32(sample_data));
|
||||
sample = value / 2147483648.0f;
|
||||
} else {
|
||||
interleaved_samples.clear();
|
||||
return false;
|
||||
}
|
||||
interleaved_samples[static_cast<size_t>(frame * channels + channel)] = sample;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
+23
-12
@@ -57,13 +57,15 @@ int create_mjpg_avi_from_sd_images(const char* filename,
|
||||
sd_image_t* images,
|
||||
int num_images,
|
||||
int fps,
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr);
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr,
|
||||
const std::string& parameters = "");
|
||||
std::vector<uint8_t> create_mjpg_avi_from_sd_images_to_vector(sd_image_t* images,
|
||||
int num_images,
|
||||
int fps,
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr);
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr,
|
||||
const std::string& parameters = "");
|
||||
|
||||
#ifdef SD_USE_WEBP
|
||||
int create_animated_webp_from_sd_images(const char* filename,
|
||||
@@ -82,27 +84,31 @@ int create_webm_from_sd_images(const char* filename,
|
||||
sd_image_t* images,
|
||||
int num_images,
|
||||
int fps,
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr);
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr,
|
||||
const std::string& parameters = "");
|
||||
std::vector<uint8_t> create_webm_from_sd_images_to_vector(sd_image_t* images,
|
||||
int num_images,
|
||||
int fps,
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr);
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr,
|
||||
const std::string& parameters = "");
|
||||
#endif
|
||||
|
||||
int create_video_from_sd_images(const char* filename,
|
||||
sd_image_t* images,
|
||||
int num_images,
|
||||
int fps,
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr);
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr,
|
||||
const std::string& parameters = "");
|
||||
std::vector<uint8_t> create_video_from_sd_images_to_vector(const std::string& output_format,
|
||||
sd_image_t* images,
|
||||
int num_images,
|
||||
int fps,
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr);
|
||||
int quality = 90,
|
||||
const sd_audio_t* audio = nullptr,
|
||||
const std::string& parameters = "");
|
||||
|
||||
bool write_wav_to_file(const std::string& path,
|
||||
const float* interleaved_samples,
|
||||
@@ -110,4 +116,9 @@ bool write_wav_to_file(const std::string& path,
|
||||
uint32_t channels,
|
||||
uint32_t sample_rate);
|
||||
|
||||
bool load_wav_from_file(const std::string& path,
|
||||
std::vector<float>& interleaved_samples,
|
||||
uint32_t& sample_rate,
|
||||
uint32_t& channels);
|
||||
|
||||
#endif // __MEDIA_IO_H__
|
||||
|
||||
@@ -141,6 +141,37 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class SDAudioOwner {
|
||||
private:
|
||||
uint32_t sample_rate_ = 0;
|
||||
uint32_t channels_ = 0;
|
||||
std::vector<float> samples_;
|
||||
|
||||
public:
|
||||
SDAudioOwner() = default;
|
||||
|
||||
void reset(std::vector<float> samples = {}, uint32_t sample_rate = 0, uint32_t channels = 0) {
|
||||
samples_ = std::move(samples);
|
||||
sample_rate_ = sample_rate;
|
||||
channels_ = channels;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return samples_.empty();
|
||||
}
|
||||
|
||||
sd_audio_t get() {
|
||||
return {sample_rate_,
|
||||
channels_,
|
||||
channels_ == 0 ? 0 : static_cast<uint64_t>(samples_.size() / channels_),
|
||||
samples_.empty() ? nullptr : samples_.data()};
|
||||
}
|
||||
|
||||
const std::vector<float>& samples() const {
|
||||
return samples_;
|
||||
}
|
||||
};
|
||||
|
||||
class SDImageVec {
|
||||
private:
|
||||
std::vector<sd_image_t> images_;
|
||||
|
||||
@@ -13,9 +13,14 @@ What this example does:
|
||||
* `--llm` selects the text encoder / language model used by this pipeline
|
||||
* `--diffusion-fa` enables flash attention in the diffusion model
|
||||
* `--offload-to-cpu` reduces VRAM pressure by keeping weights in RAM when possible
|
||||
* `-v` enables verbose logging
|
||||
* `-v` enables verbose logging (equivalent to `--log-level verbose`)
|
||||
* `--cfg-scale 1.0` sets the default CFG scale for generation
|
||||
|
||||
Logging defaults to `info`. Use `--log-level <level>` to select `debug`, `verbose`,
|
||||
`info`, `warn`, or `error` (from most to least detailed). Each level includes
|
||||
messages at that level and all less detailed levels. `-v` and `--verbose` are
|
||||
equivalent to `--log-level verbose`. If repeated, the last logging option wins.
|
||||
|
||||
After the server starts successfully:
|
||||
|
||||
* the web UI is available at `http://127.0.0.1:1234/`
|
||||
@@ -124,3 +129,6 @@ For detailed command-line arguments, run:
|
||||
```bash
|
||||
./bin/sd-server -h
|
||||
```
|
||||
|
||||
For completely black or white images or videos, NaNs, and the `--linear-scale` /
|
||||
`--attn-scale` startup options, see [Troubleshooting](../../docs/troubleshooting.md).
|
||||
|
||||
@@ -518,7 +518,8 @@ Shared default fields used by both `img_gen` and `vid_gen`:
|
||||
| `output_format` | `string` |
|
||||
| `output_compression` | `integer` |
|
||||
|
||||
`vae_tiling_params.extra_tiling_args` accepts a key=value list. For LTX video VAE temporal tiling, `temporal_tile_frames` defaults to `4` and `temporal_tile_overlap` defaults to `1`.
|
||||
`vae_tiling_params.extra_tiling_args` accepts a key=value list. Supported video VAEs accept `temporal_tile_frames` (alias `temporal_tile_size`, default `4`) and `temporal_tile_overlap` (default `1`).
|
||||
LTX and Wan preserve causal state between temporal tiles. Hunyuan Video and TAEHV use overlap blending. MiniMax H3 keeps its model-specific fixed temporal windows because its latent-to-frame mapping is non-linear.
|
||||
|
||||
`img_gen`-specific default fields:
|
||||
|
||||
|
||||
@@ -237,6 +237,9 @@ bool execute_vid_gen_job(ServerRuntime& runtime,
|
||||
int& output_fps,
|
||||
std::string& error_message) {
|
||||
sd_vid_gen_params_t params = job.vid_gen.to_sd_vid_gen_params_t();
|
||||
std::string str_params = job.vid_gen.gen_params.embed_image_metadata
|
||||
? get_image_params(*runtime.ctx_params, job.vid_gen.gen_params, job.vid_gen.gen_params.seed, VID_GEN)
|
||||
: "";
|
||||
|
||||
SDImageVec results;
|
||||
int num_results = 0;
|
||||
@@ -245,7 +248,7 @@ bool execute_vid_gen_job(ServerRuntime& runtime,
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(*runtime.sd_ctx_mutex);
|
||||
sd_image_t* raw_results = nullptr;
|
||||
if (!generate_video(runtime.sd_ctx, ¶ms, &raw_results, &num_results, &generated_audio)) {
|
||||
if (!generate_video(runtime.sd_ctx, ¶ms, &raw_results, &num_results, &generated_audio, &output_fps)) {
|
||||
raw_results = nullptr;
|
||||
}
|
||||
results.adopt(raw_results, num_results);
|
||||
@@ -261,9 +264,10 @@ bool execute_vid_gen_job(ServerRuntime& runtime,
|
||||
std::vector<uint8_t> video_bytes = create_video_from_sd_images_to_vector(job.vid_gen.output_format,
|
||||
results.data(),
|
||||
num_results,
|
||||
job.vid_gen.gen_params.fps,
|
||||
output_fps,
|
||||
job.vid_gen.output_compression,
|
||||
generated_audio);
|
||||
generated_audio,
|
||||
str_params);
|
||||
free_sd_audio(generated_audio);
|
||||
if (video_bytes.empty()) {
|
||||
error_message = "failed to encode generated video container";
|
||||
@@ -273,7 +277,6 @@ bool execute_vid_gen_job(ServerRuntime& runtime,
|
||||
output_media_b64 = base64_encode(video_bytes);
|
||||
output_media_mime_type = video_mime_type(job.vid_gen.output_format);
|
||||
output_frame_count = num_results;
|
||||
output_fps = job.vid_gen.gen_params.fps;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,9 @@ static void parse_args(int argc,
|
||||
exit(svr_params.normal_exit ? 0 : 1);
|
||||
}
|
||||
|
||||
log_level = svr_params.log_level;
|
||||
log_color = svr_params.color;
|
||||
|
||||
const bool random_seed_requested = default_gen_params.seed < 0;
|
||||
|
||||
if (!svr_params.resolve_and_validate() ||
|
||||
@@ -62,7 +65,7 @@ static void parse_args(int argc,
|
||||
|
||||
void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
|
||||
SDSvrParams* svr_params = (SDSvrParams*)data;
|
||||
log_print(level, log, svr_params->verbose, svr_params->color);
|
||||
log_print(level, log, svr_params->log_level, svr_params->color);
|
||||
}
|
||||
|
||||
int main(int argc, const char** argv) {
|
||||
@@ -76,14 +79,12 @@ int main(int argc, const char** argv) {
|
||||
parse_args(argc, argv, svr_params, ctx_params, default_gen_params);
|
||||
|
||||
sd_set_log_callback(sd_log_cb, (void*)&svr_params);
|
||||
log_verbose = svr_params.verbose;
|
||||
log_color = svr_params.color;
|
||||
|
||||
LOG_DEBUG("version: %s", version_string().c_str());
|
||||
LOG_DEBUG("%s", sd_get_system_info());
|
||||
LOG_DEBUG("%s", svr_params.to_string().c_str());
|
||||
LOG_DEBUG("%s", ctx_params.to_string().c_str());
|
||||
LOG_DEBUG("%s", default_gen_params.to_string().c_str());
|
||||
LOG_VERBOSE("version: %s", version_string().c_str());
|
||||
LOG_VERBOSE("%s", sd_get_system_info());
|
||||
LOG_VERBOSE("%s", svr_params.to_string().c_str());
|
||||
LOG_VERBOSE("%s", ctx_params.to_string().c_str());
|
||||
LOG_VERBOSE("%s", default_gen_params.to_string().c_str());
|
||||
|
||||
sd_ctx_params_t sd_ctx_params = ctx_params.to_sd_ctx_params_t(false);
|
||||
SDCtxPtr sd_ctx(new_sd_ctx(&sd_ctx_params));
|
||||
|
||||
@@ -270,7 +270,7 @@ void register_openai_api_endpoints(httplib::Server& svr, ServerRuntime& rt) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_DEBUG("%s\n", request.gen_params.to_string().c_str());
|
||||
LOG_VERBOSE("%s\n", request.gen_params.to_string().c_str());
|
||||
|
||||
SDImageVec results;
|
||||
if (!execute_sync_img_gen_request(*runtime, request, results, error_message)) {
|
||||
@@ -344,7 +344,7 @@ void register_openai_api_endpoints(httplib::Server& svr, ServerRuntime& rt) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_DEBUG("%s\n", request.gen_params.to_string().c_str());
|
||||
LOG_VERBOSE("%s\n", request.gen_params.to_string().c_str());
|
||||
|
||||
SDImageVec results;
|
||||
if (!execute_sync_img_gen_request(*runtime, request, results, error_message)) {
|
||||
|
||||
@@ -330,7 +330,7 @@ void register_sdapi_endpoints(httplib::Server& svr, ServerRuntime& rt) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_DEBUG("%s\n", request.gen_params.to_string().c_str());
|
||||
LOG_VERBOSE("%s\n", request.gen_params.to_string().c_str());
|
||||
|
||||
sd_img_gen_params_t img_gen_params = request.to_sd_img_gen_params_t();
|
||||
SDImageVec results;
|
||||
|
||||
@@ -199,7 +199,6 @@ ArgOptions SDSvrParams::get_options() {
|
||||
};
|
||||
|
||||
options.bool_options = {
|
||||
{"-v", "--verbose", "print extra info", true, &verbose},
|
||||
{"", "--color", "colors the logging tags according to level", true, &color},
|
||||
};
|
||||
|
||||
@@ -212,6 +211,7 @@ ArgOptions SDSvrParams::get_options() {
|
||||
options.manual_options = {
|
||||
{"-h", "--help", "show this help message and exit", on_help_arg},
|
||||
};
|
||||
add_log_options(options, log_level);
|
||||
return options;
|
||||
}
|
||||
|
||||
@@ -243,6 +243,7 @@ bool SDSvrParams::resolve_and_validate() {
|
||||
std::string SDSvrParams::to_string() const {
|
||||
std::ostringstream oss;
|
||||
oss << "SDSvrParams {\n"
|
||||
<< " log_level: " << log_level_name(log_level) << ",\n"
|
||||
<< " listen_ip: " << listen_ip << ",\n"
|
||||
<< " listen_port: \"" << listen_port << "\",\n"
|
||||
<< " serve_html_path: \"" << serve_html_path << "\",\n"
|
||||
|
||||
@@ -22,7 +22,7 @@ struct SDSvrParams {
|
||||
int listen_port = 1234;
|
||||
std::string serve_html_path;
|
||||
bool normal_exit = false;
|
||||
bool verbose = false;
|
||||
sd_log_level_t log_level = SD_LOG_INFO;
|
||||
bool color = false;
|
||||
|
||||
ArgOptions get_options();
|
||||
|
||||
+1
-1
Submodule ggml updated: eced84c86f...223feb34ab
@@ -56,9 +56,12 @@ enum sample_method_t {
|
||||
EULER_GE_SAMPLE_METHOD,
|
||||
DPMPP2M_SDE_SAMPLE_METHOD,
|
||||
DPMPP2M_SDE_BT_SAMPLE_METHOD,
|
||||
LMS_SAMPLE_METHOD,
|
||||
SAMPLE_METHOD_COUNT
|
||||
};
|
||||
|
||||
extern SD_API const char* sample_method_to_str[];
|
||||
|
||||
enum scheduler_t {
|
||||
DISCRETE_SCHEDULER,
|
||||
KARRAS_SCHEDULER,
|
||||
@@ -76,9 +79,12 @@ enum scheduler_t {
|
||||
FLUX2_SCHEDULER,
|
||||
FLUX_SCHEDULER,
|
||||
BETA_SCHEDULER,
|
||||
LLADA_IMAGE_SCHEDULER,
|
||||
SCHEDULER_COUNT
|
||||
};
|
||||
|
||||
extern SD_API const char* scheduler_to_str[];
|
||||
|
||||
enum prediction_t {
|
||||
EPS_PRED,
|
||||
V_PRED,
|
||||
@@ -87,6 +93,7 @@ enum prediction_t {
|
||||
FLUX_FLOW_PRED,
|
||||
SEFI_FLOW_PRED,
|
||||
MINIT2I_FLOW_PRED,
|
||||
SENSENOVA_U1_FLOW_PRED,
|
||||
PREDICTION_COUNT
|
||||
};
|
||||
|
||||
@@ -131,14 +138,18 @@ enum sd_type_t {
|
||||
// SD_TYPE_IQ4_NL_4_4 = 36,
|
||||
// SD_TYPE_IQ4_NL_4_8 = 37,
|
||||
// SD_TYPE_IQ4_NL_8_8 = 38,
|
||||
SD_TYPE_MXFP4 = 39, // MXFP4 (1 block)
|
||||
SD_TYPE_NVFP4 = 40, // NVFP4 (4 blocks, E4M3 scale)
|
||||
SD_TYPE_Q1_0 = 41,
|
||||
SD_TYPE_COUNT = 42,
|
||||
SD_TYPE_MXFP4 = 39, // MXFP4 (1 block)
|
||||
SD_TYPE_NVFP4 = 40, // NVFP4 (4 blocks, E4M3 scale)
|
||||
SD_TYPE_Q1_0 = 41,
|
||||
SD_TYPE_Q2_0 = 42,
|
||||
SD_TYPE_F8_E4M3 = 43,
|
||||
SD_TYPE_F8_E5M2 = 44,
|
||||
SD_TYPE_COUNT = 45,
|
||||
};
|
||||
|
||||
enum sd_log_level_t {
|
||||
SD_LOG_DEBUG,
|
||||
SD_LOG_VERBOSE,
|
||||
SD_LOG_INFO,
|
||||
SD_LOG_WARN,
|
||||
SD_LOG_ERROR
|
||||
@@ -198,6 +209,7 @@ typedef struct {
|
||||
const char* embeddings_connectors_path;
|
||||
const char* vae_path;
|
||||
const char* audio_vae_path;
|
||||
const char* audio_encoder_path;
|
||||
const char* taesd_path;
|
||||
const char* control_net_path;
|
||||
const char* ip_adapter_path;
|
||||
@@ -221,8 +233,8 @@ typedef struct {
|
||||
bool vae_conv_direct;
|
||||
bool force_sdxl_vae_conv_scale;
|
||||
enum sd_vae_format_t vae_format;
|
||||
const char* max_vram; // GiB budget or backend assignment spec for graph-cut segmented param offload (0 = disabled, -1 = auto)
|
||||
bool stream_layers; // Enable residency+prefetch streaming on top of --max-vram (no effect without --max-vram)
|
||||
const char* max_vram; // Optional per-device GiB budget for managed weights and runner buffers; 0 uses live free VRAM without an explicit budget
|
||||
bool disable_prefetch; // Disable asynchronous next-segment weight prefetch
|
||||
bool eager_load; // Load all params into the params backend at model-load time instead of lazily on first use
|
||||
const char* backend;
|
||||
const char* params_backend;
|
||||
@@ -230,6 +242,13 @@ typedef struct {
|
||||
bool auto_fit;
|
||||
const char* rpc_servers;
|
||||
const char* model_args;
|
||||
bool disable_segmented_compute; // Force monolithic graph execution even when automatic graph cutting would fit memory better
|
||||
float linear_scale; // Override linear input scaling; 0 keeps the model default
|
||||
float attn_scale; // Override flash-attention K/V scaling; 0 keeps the model default
|
||||
const char* tokenizer; // tokenizer.json path or main=FILE,clip-l=FILE,clip-g=FILE assignments; required for PiD and Lens
|
||||
bool sage_attn;
|
||||
bool sol_attn;
|
||||
float sol_attn_tau;
|
||||
} sd_ctx_params_t;
|
||||
|
||||
typedef struct {
|
||||
@@ -246,6 +265,13 @@ typedef struct {
|
||||
uint8_t* data;
|
||||
} sd_image_t;
|
||||
|
||||
typedef struct {
|
||||
sd_image_t* frames;
|
||||
int frame_count;
|
||||
int fps;
|
||||
sd_audio_t audio;
|
||||
} sd_ref_video_t;
|
||||
|
||||
typedef struct {
|
||||
int* layers;
|
||||
size_t layer_count;
|
||||
@@ -396,6 +422,12 @@ typedef struct {
|
||||
int clip_skip;
|
||||
sd_image_t init_image;
|
||||
sd_image_t end_image;
|
||||
sd_image_t* ref_images;
|
||||
int ref_images_count;
|
||||
sd_ref_video_t* ref_videos;
|
||||
int ref_videos_count;
|
||||
sd_audio_t* ref_audios;
|
||||
int ref_audios_count;
|
||||
sd_image_t* control_frames;
|
||||
int control_frames_size;
|
||||
int width;
|
||||
@@ -425,6 +457,9 @@ typedef bool (*sd_graph_eval_callback_t)(struct ggml_tensor* t, bool ask, void*
|
||||
|
||||
SD_API void sd_set_log_callback(sd_log_cb_t sd_log_cb, void* data);
|
||||
SD_API void sd_set_progress_callback(sd_progress_cb_t cb, void* data);
|
||||
// In each sampling pass, a positive interval previews every Nth denoiser step, while a
|
||||
// negative interval previews only completed logical step -interval. Zero previews the final
|
||||
// completed step of the first sampling pass (base-resolution or high-noise).
|
||||
SD_API void sd_set_preview_callback(sd_preview_cb_t cb, enum preview_t mode, int interval, bool denoised, bool noisy, void* data);
|
||||
SD_API void sd_set_backend_eval_callback(sd_graph_eval_callback_t cb, void* data);
|
||||
SD_API int32_t sd_get_num_physical_cores();
|
||||
@@ -467,6 +502,9 @@ SD_API void free_sd_audio(sd_audio_t* audio);
|
||||
SD_API void sd_sample_params_init(sd_sample_params_t* sample_params);
|
||||
SD_API char* sd_sample_params_to_str(const sd_sample_params_t* sample_params);
|
||||
|
||||
// Requires a loaded context; returns a static string owned by the library, or "Unknown".
|
||||
SD_API const char* sd_get_model_version_name(const sd_ctx_t* sd_ctx);
|
||||
|
||||
SD_API enum sample_method_t sd_get_default_sample_method(const sd_ctx_t* sd_ctx);
|
||||
SD_API enum scheduler_t sd_get_default_scheduler(const sd_ctx_t* sd_ctx, enum sample_method_t sample_method);
|
||||
|
||||
@@ -489,11 +527,13 @@ enum sd_cancel_mode_t {
|
||||
SD_API void sd_cancel_generation(sd_ctx_t* sd_ctx, enum sd_cancel_mode_t mode);
|
||||
|
||||
SD_API void sd_vid_gen_params_init(sd_vid_gen_params_t* sd_vid_gen_params);
|
||||
// If non-NULL, fps_out receives the effective encoding frame rate before preview callbacks.
|
||||
SD_API bool generate_video(sd_ctx_t* sd_ctx,
|
||||
const sd_vid_gen_params_t* sd_vid_gen_params,
|
||||
sd_image_t** frames_out,
|
||||
int* num_frames_out,
|
||||
sd_audio_t** audio_out);
|
||||
sd_audio_t** audio_out,
|
||||
int* fps_out);
|
||||
|
||||
typedef struct upscaler_ctx_t upscaler_ctx_t;
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ $patterns = @(
|
||||
"src/extensions/*.cpp"
|
||||
"src/extensions/*.h"
|
||||
"src/extensions/*.hpp"
|
||||
"src/pipeline/*.cpp"
|
||||
"src/pipeline/*.h"
|
||||
"src/runtime/*.cpp"
|
||||
"src/runtime/*.h"
|
||||
"src/runtime/*.hpp"
|
||||
|
||||
@@ -9,6 +9,7 @@ for f in src/*.cpp src/*.h src/*.hpp \
|
||||
src/conditioning/*.cpp src/conditioning/*.h src/conditioning/*.hpp \
|
||||
src/core/*.cpp src/core/*.h src/core/*.hpp \
|
||||
src/extensions/*.cpp src/extensions/*.h src/extensions/*.hpp \
|
||||
src/pipeline/*.cpp src/pipeline/*.h \
|
||||
src/runtime/*.cpp src/runtime/*.h src/runtime/*.hpp \
|
||||
src/model/*/*.cpp src/model/*/*.h src/model/*/*.hpp \
|
||||
src/tokenizers/*.h src/tokenizers/*.cpp src/tokenizers/vocab/*.h src/tokenizers/vocab/*.cpp \
|
||||
|
||||
@@ -0,0 +1,335 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Merge selected tensors from multiple safetensors files without loading weights.
|
||||
|
||||
Edit ``OUTPUT_PATH`` and ``SOURCE_RULES`` below, then run:
|
||||
|
||||
python scripts/merge_safetensors.py
|
||||
|
||||
Each source rule uses regular expressions against complete tensor names.
|
||||
``include`` is required and matches when any expression succeeds. ``exclude``
|
||||
wins over ``include``. Expressions are evaluated with ``re.search``.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import struct
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import BinaryIO
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Configuration
|
||||
# -----------------------------------------------------------------------------
|
||||
|
||||
OUTPUT_PATH = Path(r".minimax_h3_fl2va_pruned_bf16.safetensors")
|
||||
|
||||
SOURCE_RULES = [
|
||||
{
|
||||
"path": Path(r".minimax_h3_fl2va_bf16.safetensors"),
|
||||
"include": [r".*"],
|
||||
"exclude": [r".*adaln_proj\.linear.*", r"time_embedder.*"],
|
||||
},
|
||||
{
|
||||
"path": Path(r".minimax_h3_fl2va_pruned_int8_convrot.safetensors"),
|
||||
"include": [r"^.*adaln_proj\.linear.*", "adaln_t_table"],
|
||||
"exclude": [],
|
||||
},
|
||||
]
|
||||
|
||||
# Safetensors metadata is optional. Set this to a dict[str, str] if needed.
|
||||
OUTPUT_METADATA = None
|
||||
|
||||
# Refuse to replace an existing output unless explicitly enabled.
|
||||
OVERWRITE_OUTPUT = False
|
||||
|
||||
# Only tensor headers and this fixed-size buffer are held in memory.
|
||||
COPY_BUFFER_SIZE = 8 * 1024 * 1024
|
||||
PROGRESS_INTERVAL = 1024 * 1024 * 1024
|
||||
MAX_HEADER_SIZE = 256 * 1024 * 1024
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TensorEntry:
|
||||
name: str
|
||||
source_path: Path
|
||||
source_data_offset: int
|
||||
source_start: int
|
||||
source_end: int
|
||||
dtype: str
|
||||
shape: list[int]
|
||||
|
||||
@property
|
||||
def size(self) -> int:
|
||||
return self.source_end - self.source_start
|
||||
|
||||
|
||||
def format_bytes(size: int) -> str:
|
||||
value = float(size)
|
||||
for unit in ("B", "KiB", "MiB", "GiB", "TiB"):
|
||||
if value < 1024.0 or unit == "TiB":
|
||||
return f"{value:.2f} {unit}"
|
||||
value /= 1024.0
|
||||
raise AssertionError("unreachable")
|
||||
|
||||
|
||||
def read_exact(file: BinaryIO, size: int, description: str) -> bytes:
|
||||
data = file.read(size)
|
||||
if len(data) != size:
|
||||
raise ValueError(f"truncated {description}: expected {size} bytes, got {len(data)}")
|
||||
return data
|
||||
|
||||
|
||||
def read_safetensors_header(path: Path) -> tuple[dict, int, int]:
|
||||
file_size = path.stat().st_size
|
||||
with path.open("rb") as file:
|
||||
header_size = struct.unpack("<Q", read_exact(file, 8, f"header size in {path}"))[0]
|
||||
if header_size == 0 or header_size > MAX_HEADER_SIZE:
|
||||
raise ValueError(
|
||||
f"invalid header size in {path}: {header_size} "
|
||||
f"(limit: {MAX_HEADER_SIZE})"
|
||||
)
|
||||
header_bytes = read_exact(file, header_size, f"header in {path}")
|
||||
|
||||
try:
|
||||
header = json.loads(header_bytes)
|
||||
except (UnicodeDecodeError, json.JSONDecodeError) as error:
|
||||
raise ValueError(f"invalid safetensors JSON header in {path}: {error}") from error
|
||||
if not isinstance(header, dict):
|
||||
raise ValueError(f"safetensors header in {path} is not an object")
|
||||
|
||||
data_offset = 8 + header_size
|
||||
if data_offset > file_size:
|
||||
raise ValueError(f"safetensors data offset is past end of file: {path}")
|
||||
return header, data_offset, file_size
|
||||
|
||||
|
||||
def parse_tensor_entry(
|
||||
name: str,
|
||||
info: object,
|
||||
source_path: Path,
|
||||
source_data_offset: int,
|
||||
source_file_size: int,
|
||||
) -> TensorEntry:
|
||||
if not isinstance(info, dict):
|
||||
raise ValueError(f"{source_path}: tensor {name!r} has an invalid header entry")
|
||||
|
||||
dtype = info.get("dtype")
|
||||
shape = info.get("shape")
|
||||
offsets = info.get("data_offsets")
|
||||
if not isinstance(dtype, str):
|
||||
raise ValueError(f"{source_path}: tensor {name!r} has an invalid dtype")
|
||||
if not isinstance(shape, list) or not all(
|
||||
isinstance(dimension, int) and dimension >= 0 for dimension in shape
|
||||
):
|
||||
raise ValueError(f"{source_path}: tensor {name!r} has an invalid shape")
|
||||
if (
|
||||
not isinstance(offsets, list)
|
||||
or len(offsets) != 2
|
||||
or not all(isinstance(offset, int) for offset in offsets)
|
||||
):
|
||||
raise ValueError(f"{source_path}: tensor {name!r} has invalid data offsets")
|
||||
|
||||
start, end = offsets
|
||||
if start < 0 or end < start or source_data_offset + end > source_file_size:
|
||||
raise ValueError(
|
||||
f"{source_path}: tensor {name!r} byte range [{start}, {end}) "
|
||||
"is outside the file"
|
||||
)
|
||||
|
||||
return TensorEntry(
|
||||
name=name,
|
||||
source_path=source_path,
|
||||
source_data_offset=source_data_offset,
|
||||
source_start=start,
|
||||
source_end=end,
|
||||
dtype=dtype,
|
||||
shape=list(shape),
|
||||
)
|
||||
|
||||
|
||||
def compile_patterns(rule_index: int, field: str, values: object) -> list[re.Pattern[str]]:
|
||||
if not isinstance(values, list) or not all(isinstance(value, str) for value in values):
|
||||
raise TypeError(f"SOURCE_RULES[{rule_index}][{field!r}] must be a list of strings")
|
||||
try:
|
||||
return [re.compile(value) for value in values]
|
||||
except re.error as error:
|
||||
raise ValueError(
|
||||
f"invalid regex in SOURCE_RULES[{rule_index}][{field!r}]: {error}"
|
||||
) from error
|
||||
|
||||
|
||||
def collect_entries() -> list[TensorEntry]:
|
||||
if not SOURCE_RULES:
|
||||
raise ValueError("SOURCE_RULES must contain at least one source")
|
||||
|
||||
entries: list[TensorEntry] = []
|
||||
selected_by_name: dict[str, TensorEntry] = {}
|
||||
header_cache: dict[Path, tuple[dict, int, int]] = {}
|
||||
|
||||
for rule_index, rule in enumerate(SOURCE_RULES):
|
||||
if not isinstance(rule, dict) or "path" not in rule or "include" not in rule:
|
||||
raise TypeError(
|
||||
f"SOURCE_RULES[{rule_index}] must contain 'path' and 'include'"
|
||||
)
|
||||
|
||||
source_path = Path(rule["path"])
|
||||
if not source_path.is_file():
|
||||
raise FileNotFoundError(f"source file does not exist: {source_path}")
|
||||
source_path = source_path.resolve()
|
||||
|
||||
include = compile_patterns(rule_index, "include", rule["include"])
|
||||
exclude = compile_patterns(rule_index, "exclude", rule.get("exclude", []))
|
||||
if not include:
|
||||
raise ValueError(f"SOURCE_RULES[{rule_index}]['include'] must not be empty")
|
||||
|
||||
if source_path not in header_cache:
|
||||
header_cache[source_path] = read_safetensors_header(source_path)
|
||||
header, data_offset, file_size = header_cache[source_path]
|
||||
|
||||
matched = 0
|
||||
for name, info in header.items():
|
||||
if name == "__metadata__":
|
||||
continue
|
||||
if not any(pattern.search(name) for pattern in include):
|
||||
continue
|
||||
if any(pattern.search(name) for pattern in exclude):
|
||||
continue
|
||||
|
||||
entry = parse_tensor_entry(name, info, source_path, data_offset, file_size)
|
||||
previous = selected_by_name.get(name)
|
||||
if previous is not None:
|
||||
raise ValueError(
|
||||
f"tensor {name!r} was selected more than once:\n"
|
||||
f" first: {previous.source_path}\n"
|
||||
f" second: {source_path}"
|
||||
)
|
||||
selected_by_name[name] = entry
|
||||
print(f"entry {entry}")
|
||||
entries.append(entry)
|
||||
matched += 1
|
||||
|
||||
print(f"Rule {rule_index}: selected {matched} tensors from {source_path}")
|
||||
if matched == 0:
|
||||
raise ValueError(
|
||||
f"SOURCE_RULES[{rule_index}] did not select any tensors; check its regexes"
|
||||
)
|
||||
|
||||
if not entries:
|
||||
raise ValueError("no tensors were selected")
|
||||
return entries
|
||||
|
||||
|
||||
def build_output_header(entries: list[TensorEntry]) -> tuple[bytes, int]:
|
||||
header: dict[str, object] = {}
|
||||
if OUTPUT_METADATA is not None:
|
||||
if not isinstance(OUTPUT_METADATA, dict) or not all(
|
||||
isinstance(key, str) and isinstance(value, str)
|
||||
for key, value in OUTPUT_METADATA.items()
|
||||
):
|
||||
raise TypeError("OUTPUT_METADATA must be None or a dict[str, str]")
|
||||
header["__metadata__"] = OUTPUT_METADATA
|
||||
|
||||
output_offset = 0
|
||||
for entry in entries:
|
||||
header[entry.name] = {
|
||||
"dtype": entry.dtype,
|
||||
"shape": entry.shape,
|
||||
"data_offsets": [output_offset, output_offset + entry.size],
|
||||
}
|
||||
output_offset += entry.size
|
||||
|
||||
header_bytes = json.dumps(header, separators=(",", ":"), ensure_ascii=False).encode(
|
||||
"utf-8"
|
||||
)
|
||||
header_bytes += b" " * (-len(header_bytes) % 8)
|
||||
return header_bytes, output_offset
|
||||
|
||||
|
||||
def copy_tensor(source: BinaryIO, output: BinaryIO, entry: TensorEntry) -> None:
|
||||
source.seek(entry.source_data_offset + entry.source_start)
|
||||
remaining = entry.size
|
||||
while remaining:
|
||||
chunk = source.read(min(COPY_BUFFER_SIZE, remaining))
|
||||
if not chunk:
|
||||
raise OSError(
|
||||
f"unexpected end of file while copying {entry.name!r} "
|
||||
f"from {entry.source_path}"
|
||||
)
|
||||
output.write(chunk)
|
||||
remaining -= len(chunk)
|
||||
|
||||
|
||||
def write_output(entries: list[TensorEntry]) -> None:
|
||||
if COPY_BUFFER_SIZE <= 0:
|
||||
raise ValueError("COPY_BUFFER_SIZE must be positive")
|
||||
|
||||
output_path = OUTPUT_PATH.resolve()
|
||||
source_paths = {entry.source_path.resolve() for entry in entries}
|
||||
if output_path in source_paths:
|
||||
raise ValueError("OUTPUT_PATH must not be one of the source files")
|
||||
if output_path.exists() and not OVERWRITE_OUTPUT:
|
||||
raise FileExistsError(
|
||||
f"output already exists: {output_path}; set OVERWRITE_OUTPUT = True to replace it"
|
||||
)
|
||||
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
partial_path = output_path.with_name(output_path.name + ".partial")
|
||||
if partial_path.exists():
|
||||
raise FileExistsError(
|
||||
f"partial output already exists: {partial_path}; remove it before retrying"
|
||||
)
|
||||
|
||||
header_bytes, tensor_bytes = build_output_header(entries)
|
||||
print(
|
||||
f"Writing {len(entries)} tensors ({format_bytes(tensor_bytes)}) to {output_path}"
|
||||
)
|
||||
|
||||
current_source_path: Path | None = None
|
||||
current_source: BinaryIO | None = None
|
||||
copied = 0
|
||||
next_progress = PROGRESS_INTERVAL
|
||||
try:
|
||||
with partial_path.open("xb") as output:
|
||||
output.write(struct.pack("<Q", len(header_bytes)))
|
||||
output.write(header_bytes)
|
||||
|
||||
try:
|
||||
for entry in entries:
|
||||
if entry.source_path != current_source_path:
|
||||
if current_source is not None:
|
||||
current_source.close()
|
||||
current_source = entry.source_path.open("rb")
|
||||
current_source_path = entry.source_path
|
||||
|
||||
copy_tensor(current_source, output, entry)
|
||||
copied += entry.size
|
||||
if PROGRESS_INTERVAL > 0 and copied >= next_progress:
|
||||
print(
|
||||
f" copied {format_bytes(copied)} / "
|
||||
f"{format_bytes(tensor_bytes)}"
|
||||
)
|
||||
while next_progress <= copied:
|
||||
next_progress += PROGRESS_INTERVAL
|
||||
finally:
|
||||
if current_source is not None:
|
||||
current_source.close()
|
||||
|
||||
if copied != tensor_bytes:
|
||||
raise OSError(f"copied {copied} tensor bytes, expected {tensor_bytes}")
|
||||
os.replace(partial_path, output_path)
|
||||
except BaseException:
|
||||
partial_path.unlink(missing_ok=True)
|
||||
raise
|
||||
|
||||
print(f"Done: {output_path} ({format_bytes(output_path.stat().st_size)})")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
entries = collect_entries()
|
||||
write_output(entries)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+968
-289
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,103 @@
|
||||
#include "wan_audio.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
|
||||
namespace sd::wan_audio {
|
||||
|
||||
static BucketPlan plan_buckets(int audio_frames, int batch_frames, int video_rate, int fps) {
|
||||
BucketPlan plan;
|
||||
plan.audio_frames = audio_frames;
|
||||
plan.batch_frames = batch_frames;
|
||||
plan.video_rate = video_rate;
|
||||
plan.fps = fps;
|
||||
const double scale = static_cast<double>(video_rate) / fps;
|
||||
// Keep a trailing chunk even when audio ends on a chunk boundary.
|
||||
plan.num_chunks = static_cast<int>(audio_frames / (batch_frames * scale)) + 1;
|
||||
plan.bucket_frames = plan.num_chunks * batch_frames;
|
||||
plan.padded_audio_frames = static_cast<int>(
|
||||
std::ceil(plan.bucket_frames / static_cast<double>(fps) * video_rate));
|
||||
return plan;
|
||||
}
|
||||
|
||||
// Match NumPy's round-half-even sampling.
|
||||
static int bucket_source_frame(int bucket_frame, int video_rate, int fps) {
|
||||
return static_cast<int>(std::nearbyint(static_cast<double>(bucket_frame) * video_rate / fps));
|
||||
}
|
||||
|
||||
static int interpolated_frame_count(int in_frames, int input_fps, int output_fps) {
|
||||
return static_cast<int>(in_frames / static_cast<double>(input_fps) * output_fps);
|
||||
}
|
||||
|
||||
// Match PyTorch linear interpolation with align_corners=True.
|
||||
static std::vector<float> linear_interpolate_frames(const std::vector<float>& in,
|
||||
int num_layers,
|
||||
int in_frames,
|
||||
int dim,
|
||||
int out_frames) {
|
||||
std::vector<float> out(static_cast<size_t>(num_layers) * out_frames * dim, 0.0f);
|
||||
if (in.empty() || in_frames <= 0 || out_frames <= 0 || num_layers <= 0 || dim <= 0) {
|
||||
return out;
|
||||
}
|
||||
const double scale = out_frames > 1 ? static_cast<double>(in_frames - 1) / (out_frames - 1) : 0.0;
|
||||
for (int layer = 0; layer < num_layers; ++layer) {
|
||||
for (int out_i = 0; out_i < out_frames; ++out_i) {
|
||||
const double pos = out_i * scale;
|
||||
const int src0 = static_cast<int>(pos);
|
||||
const int src1 = std::min(src0 + 1, in_frames - 1);
|
||||
const float frac = static_cast<float>(pos - src0);
|
||||
const float* in_row = &in[(static_cast<size_t>(layer) * in_frames + src0) * dim];
|
||||
const float* in_next = &in[(static_cast<size_t>(layer) * in_frames + src1) * dim];
|
||||
float* out_row = &out[(static_cast<size_t>(layer) * out_frames + out_i) * dim];
|
||||
for (int d = 0; d < dim; ++d) {
|
||||
out_row[d] = in_row[d] * (1.0f - frac) + in_next[d] * frac;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<float> build_audio_buckets(const float* stacked_states,
|
||||
int num_layers,
|
||||
int in_frames,
|
||||
int dim,
|
||||
int batch_frames,
|
||||
BucketPlan* plan_out,
|
||||
int input_fps,
|
||||
int video_rate,
|
||||
int fps) {
|
||||
if (stacked_states == nullptr || num_layers <= 0 || in_frames <= 0 || dim <= 0 || batch_frames <= 0) {
|
||||
return {};
|
||||
}
|
||||
const int audio_frames = interpolated_frame_count(in_frames, input_fps, video_rate);
|
||||
if (audio_frames <= 0) {
|
||||
return {};
|
||||
}
|
||||
const std::vector<float> interpolated =
|
||||
linear_interpolate_frames(std::vector<float>(stacked_states,
|
||||
stacked_states + static_cast<size_t>(num_layers) * in_frames * dim),
|
||||
num_layers,
|
||||
in_frames,
|
||||
dim,
|
||||
audio_frames);
|
||||
const BucketPlan plan = plan_buckets(audio_frames, batch_frames, video_rate, fps);
|
||||
if (plan_out != nullptr) {
|
||||
*plan_out = plan;
|
||||
}
|
||||
std::vector<float> buckets(static_cast<size_t>(plan.bucket_frames) * num_layers * dim, 0.0f);
|
||||
for (int frame = 0; frame < plan.bucket_frames; ++frame) {
|
||||
const int src = bucket_source_frame(frame, video_rate, fps);
|
||||
if (src >= plan.audio_frames) {
|
||||
continue;
|
||||
}
|
||||
for (int layer = 0; layer < num_layers; ++layer) {
|
||||
std::copy_n(interpolated.data() + (static_cast<size_t>(layer) * audio_frames + src) * dim,
|
||||
static_cast<size_t>(dim),
|
||||
buckets.data() + (static_cast<size_t>(frame) * num_layers + layer) * dim);
|
||||
}
|
||||
}
|
||||
return buckets;
|
||||
}
|
||||
|
||||
} // namespace sd::wan_audio
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef __SD_CONDITIONING_WAN_AUDIO_H__
|
||||
#define __SD_CONDITIONING_WAN_AUDIO_H__
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace sd::wan_audio {
|
||||
|
||||
struct BucketPlan {
|
||||
int audio_frames; // frames at video_rate
|
||||
int batch_frames; // latent_t * 4
|
||||
int video_rate;
|
||||
int fps; // bucket frame rate
|
||||
int num_chunks; // includes trailing padding
|
||||
int bucket_frames;
|
||||
int padded_audio_frames;
|
||||
};
|
||||
|
||||
// [layers, frames, dim] at input_fps -> [bucket_frames, layers, dim] at fps.
|
||||
// Pads past the audio end; returns an empty vector on invalid input.
|
||||
std::vector<float> build_audio_buckets(const float* stacked_states,
|
||||
int num_layers,
|
||||
int in_frames,
|
||||
int dim,
|
||||
int batch_frames,
|
||||
BucketPlan* plan_out = nullptr,
|
||||
int input_fps = 50,
|
||||
int video_rate = 30,
|
||||
int fps = 16);
|
||||
|
||||
} // namespace sd::wan_audio
|
||||
|
||||
#endif // __SD_CONDITIONING_WAN_AUDIO_H__
|
||||
@@ -362,6 +362,9 @@ bool convert_with_components(const char* model_path,
|
||||
const char* tensor_type_rules,
|
||||
bool convert_name,
|
||||
int n_threads) {
|
||||
if (!validate_tensor_types(output_type, tensor_type_rules)) {
|
||||
return false;
|
||||
}
|
||||
ModelLoader model_loader;
|
||||
bool loaded_any = false;
|
||||
|
||||
|
||||
+430
-314
@@ -2,388 +2,504 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#ifndef NOMINMAX
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <windows.h>
|
||||
#elif defined(__APPLE__)
|
||||
#include <mach/mach.h>
|
||||
#endif
|
||||
|
||||
#include "core/ggml_extend_backend.h"
|
||||
#include "core/util.h"
|
||||
#include "ggml-backend.h"
|
||||
|
||||
namespace sd::backend_fit {
|
||||
namespace {
|
||||
|
||||
constexpr int64_t MiB = 1024ll * 1024;
|
||||
static constexpr int64_t MiB = 1024ll * 1024;
|
||||
|
||||
enum class ComponentKind {
|
||||
DIT = 0,
|
||||
VAE = 1,
|
||||
CONDITIONER = 2,
|
||||
};
|
||||
enum class ComponentKind {
|
||||
DIT,
|
||||
CONDITIONER,
|
||||
VAE,
|
||||
};
|
||||
|
||||
struct Component {
|
||||
struct Component {
|
||||
ComponentKind kind;
|
||||
const char* name;
|
||||
int64_t params_bytes = 0;
|
||||
int64_t reserve_bytes = 0;
|
||||
int64_t staging_bytes = 0;
|
||||
};
|
||||
|
||||
struct Device {
|
||||
std::string name;
|
||||
std::string description;
|
||||
int64_t free_bytes = 0;
|
||||
int64_t budget_bytes = 0;
|
||||
};
|
||||
|
||||
enum class ParamsLocation {
|
||||
MAIN_GPU,
|
||||
CPU,
|
||||
OTHER_GPU,
|
||||
DISK,
|
||||
};
|
||||
|
||||
struct Decision {
|
||||
ParamsLocation params_location = ParamsLocation::DISK;
|
||||
size_t params_device = SIZE_MAX;
|
||||
};
|
||||
|
||||
struct Runtime {
|
||||
std::string name;
|
||||
std::vector<size_t> devices;
|
||||
};
|
||||
|
||||
struct Plan {
|
||||
bool valid = false;
|
||||
size_t main_device = SIZE_MAX;
|
||||
std::vector<Runtime> runtimes;
|
||||
std::vector<Decision> decisions;
|
||||
};
|
||||
|
||||
static bool classify_tensor(const std::string& name, ComponentKind& out) {
|
||||
auto contains = [&](const char* s) { return name.find(s) != std::string::npos; };
|
||||
|
||||
if (contains("model.diffusion_model.") || contains("unet.")) {
|
||||
out = ComponentKind::DIT;
|
||||
return true;
|
||||
}
|
||||
if (contains("first_stage_model.") ||
|
||||
name.rfind("vae.", 0) == 0 ||
|
||||
name.rfind("tae.", 0) == 0) {
|
||||
out = ComponentKind::VAE;
|
||||
return true;
|
||||
}
|
||||
if (contains("text_encoders") ||
|
||||
contains("cond_stage_model") ||
|
||||
contains("te.text_model.") ||
|
||||
contains("conditioner") ||
|
||||
name.rfind("text_encoder.", 0) == 0 ||
|
||||
name.rfind("text_embedding_projection.", 0) == 0 ||
|
||||
contains(".aggregate_embed.")) {
|
||||
out = ComponentKind::CONDITIONER;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static std::vector<Component> estimate_components(ModelLoader& loader, ggml_type override_wtype) {
|
||||
int64_t bytes[3] = {0, 0, 0};
|
||||
int64_t largest_tensor[3] = {0, 0, 0};
|
||||
for (const auto& [name, stored_tensor] : loader.get_tensor_storage_map()) {
|
||||
TensorStorage ts = stored_tensor;
|
||||
ComponentKind kind;
|
||||
const char* name;
|
||||
int64_t params_bytes = 0;
|
||||
int64_t reserve_bytes = 0;
|
||||
bool splittable = false;
|
||||
};
|
||||
|
||||
struct Device {
|
||||
ggml_backend_dev_t dev = nullptr;
|
||||
std::string name;
|
||||
std::string description;
|
||||
int64_t free_bytes = 0;
|
||||
int64_t total_bytes = 0;
|
||||
int64_t budget_bytes = 0;
|
||||
};
|
||||
|
||||
struct Decision {
|
||||
ComponentKind kind;
|
||||
bool on_cpu = false;
|
||||
std::vector<size_t> device_idxs;
|
||||
};
|
||||
|
||||
struct Plan {
|
||||
bool valid = false;
|
||||
bool time_share = false;
|
||||
std::vector<Decision> decisions;
|
||||
};
|
||||
|
||||
bool classify_tensor(const std::string& name, ComponentKind& out) {
|
||||
auto contains = [&](const char* s) { return name.find(s) != std::string::npos; };
|
||||
|
||||
if (contains("model.diffusion_model.") || contains("unet.")) {
|
||||
out = ComponentKind::DIT;
|
||||
return true;
|
||||
if (!classify_tensor(ts.name, kind)) {
|
||||
continue;
|
||||
}
|
||||
if (contains("first_stage_model.") ||
|
||||
name.rfind("vae.", 0) == 0 ||
|
||||
name.rfind("tae.", 0) == 0) {
|
||||
out = ComponentKind::VAE;
|
||||
return true;
|
||||
if (ts.expected_type != GGML_TYPE_COUNT) {
|
||||
ts.type = ts.expected_type;
|
||||
} else if (override_wtype != GGML_TYPE_COUNT && loader.tensor_should_be_converted(ts, override_wtype)) {
|
||||
ts.type = override_wtype;
|
||||
}
|
||||
if (contains("text_encoders") ||
|
||||
contains("cond_stage_model") ||
|
||||
contains("te.text_model.") ||
|
||||
contains("conditioner") ||
|
||||
name.rfind("text_encoder.", 0) == 0 ||
|
||||
name.rfind("text_embedding_projection.", 0) == 0 ||
|
||||
contains(".aggregate_embed.")) {
|
||||
out = ComponentKind::CONDITIONER;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
const int64_t tensor_bytes = (int64_t)ts.nbytes() + 64;
|
||||
bytes[int(kind)] += tensor_bytes;
|
||||
largest_tensor[int(kind)] = std::max(largest_tensor[int(kind)], tensor_bytes);
|
||||
}
|
||||
|
||||
std::vector<Component> estimate_components(ModelLoader& loader, ggml_type override_wtype) {
|
||||
const auto& storage = loader.get_tensor_storage_map();
|
||||
return {
|
||||
{ComponentKind::DIT, "DiT", bytes[int(ComponentKind::DIT)], 2048 * MiB, largest_tensor[int(ComponentKind::DIT)]},
|
||||
{ComponentKind::CONDITIONER, "Conditioner", bytes[int(ComponentKind::CONDITIONER)], 2048 * MiB, largest_tensor[int(ComponentKind::CONDITIONER)]},
|
||||
{ComponentKind::VAE, "VAE", bytes[int(ComponentKind::VAE)], 1024 * MiB, largest_tensor[int(ComponentKind::VAE)]},
|
||||
};
|
||||
}
|
||||
|
||||
int64_t bytes[3] = {0, 0, 0};
|
||||
for (const auto& [name, ts_const] : storage) {
|
||||
TensorStorage ts = ts_const;
|
||||
if (is_unused_tensor(ts.name)) {
|
||||
continue;
|
||||
}
|
||||
ComponentKind kind;
|
||||
if (!classify_tensor(ts.name, kind)) {
|
||||
continue;
|
||||
}
|
||||
if (override_wtype != GGML_TYPE_COUNT &&
|
||||
loader.tensor_should_be_converted(ts, override_wtype)) {
|
||||
ts.type = override_wtype;
|
||||
} else if (ts.expected_type != GGML_TYPE_COUNT && ts.expected_type != ts.type) {
|
||||
ts.type = ts.expected_type;
|
||||
}
|
||||
bytes[int(kind)] += (int64_t)ts.nbytes() + 64;
|
||||
static std::string budget_key(std::string name) {
|
||||
std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c) { return (char)std::tolower(c); });
|
||||
return name;
|
||||
}
|
||||
|
||||
static std::vector<Device> enumerate_gpu_devices(const sd::ggml_graph_cut::MaxVramAssignment& budgets,
|
||||
bool include_other_devices) {
|
||||
std::vector<Device> out;
|
||||
for (size_t i = 0; i < ggml_backend_dev_count(); ++i) {
|
||||
ggml_backend_dev_t dev = ggml_backend_dev_get(i);
|
||||
const auto type = ggml_backend_dev_type(dev);
|
||||
if (type != GGML_BACKEND_DEVICE_TYPE_GPU &&
|
||||
(!include_other_devices || type == GGML_BACKEND_DEVICE_TYPE_CPU)) {
|
||||
continue;
|
||||
}
|
||||
Device device;
|
||||
device.name = ggml_backend_dev_name(dev);
|
||||
device.description = ggml_backend_dev_description(dev);
|
||||
size_t free_bytes = 0, total_bytes = 0;
|
||||
ggml_backend_dev_memory(dev, &free_bytes, &total_bytes);
|
||||
device.free_bytes = (int64_t)free_bytes;
|
||||
|
||||
std::vector<Component> out;
|
||||
out.push_back({ComponentKind::DIT, "DiT", bytes[int(ComponentKind::DIT)], 2048 * MiB, true});
|
||||
out.push_back({ComponentKind::VAE, "VAE", bytes[int(ComponentKind::VAE)], 1024 * MiB, false});
|
||||
out.push_back({ComponentKind::CONDITIONER, "Conditioner", bytes[int(ComponentKind::CONDITIONER)], 2048 * MiB, true});
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<Device> enumerate_gpu_devices(const sd::ggml_graph_cut::MaxVramAssignment& budgets) {
|
||||
std::vector<Device> out;
|
||||
for (size_t i = 0; i < ggml_backend_dev_count(); i++) {
|
||||
ggml_backend_dev_t dev = ggml_backend_dev_get(i);
|
||||
if (ggml_backend_dev_type(dev) != GGML_BACKEND_DEVICE_TYPE_GPU) {
|
||||
continue;
|
||||
}
|
||||
Device d;
|
||||
d.dev = dev;
|
||||
d.name = ggml_backend_dev_name(dev);
|
||||
d.description = ggml_backend_dev_description(dev);
|
||||
size_t free_bytes = 0, total_bytes = 0;
|
||||
ggml_backend_dev_memory(dev, &free_bytes, &total_bytes);
|
||||
d.free_bytes = (int64_t)free_bytes;
|
||||
d.total_bytes = (int64_t)total_bytes;
|
||||
|
||||
std::string budget_key = d.name;
|
||||
std::transform(budget_key.begin(), budget_key.end(), budget_key.begin(),
|
||||
[](unsigned char c) { return (char)std::tolower(c); });
|
||||
float gib = budgets.default_gib;
|
||||
auto it = budgets.backend_gib.find(budget_key);
|
||||
if (it != budgets.backend_gib.end()) {
|
||||
gib = it->second;
|
||||
}
|
||||
if (gib > 0.f) {
|
||||
d.budget_bytes = std::min<int64_t>((int64_t)(gib * 1024.0 * 1024.0 * 1024.0), d.free_bytes);
|
||||
} else if (gib < 0.f) {
|
||||
d.budget_bytes = d.free_bytes + (int64_t)(gib * 1024.0 * 1024.0 * 1024.0);
|
||||
} else {
|
||||
d.budget_bytes = d.free_bytes - 512 * MiB;
|
||||
}
|
||||
d.budget_bytes = std::max<int64_t>(d.budget_bytes, 0);
|
||||
out.push_back(d);
|
||||
float gib = budgets.default_gib;
|
||||
auto it = budgets.backend_gib.find(budget_key(device.name));
|
||||
if (it != budgets.backend_gib.end()) {
|
||||
gib = it->second;
|
||||
}
|
||||
return out;
|
||||
if (gib > 0.f) {
|
||||
device.budget_bytes = (int64_t)std::min(gib * 1024.0 * MiB, (double)device.free_bytes);
|
||||
} else if (gib < 0.f) {
|
||||
device.budget_bytes = (int64_t)std::max<double>(device.free_bytes + gib * 1024.0 * MiB, 0);
|
||||
} else {
|
||||
device.budget_bytes = std::max<int64_t>(device.free_bytes - 512 * MiB, 0);
|
||||
}
|
||||
out.push_back(std::move(device));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Plan compute_plan(const std::vector<Component>& components, const std::vector<Device>& devices) {
|
||||
Plan plan;
|
||||
if (devices.empty()) {
|
||||
static int64_t available_ram_bytes() {
|
||||
#if defined(_WIN32)
|
||||
MEMORYSTATUSEX status{};
|
||||
status.dwLength = sizeof(status);
|
||||
if (GlobalMemoryStatusEx(&status)) {
|
||||
return (int64_t)status.ullAvailPhys;
|
||||
}
|
||||
#elif defined(__linux__)
|
||||
std::ifstream meminfo("/proc/meminfo");
|
||||
std::string key, unit;
|
||||
int64_t kib = 0;
|
||||
while (meminfo >> key >> kib >> unit) {
|
||||
if (key == "MemAvailable:" && unit == "kB" && kib >= 0) {
|
||||
return kib * 1024;
|
||||
}
|
||||
}
|
||||
#elif defined(__APPLE__)
|
||||
const mach_port_t host = mach_host_self();
|
||||
vm_size_t page_size = 0;
|
||||
vm_statistics64_data_t stats{};
|
||||
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
|
||||
const bool ok = host_page_size(host, &page_size) == KERN_SUCCESS &&
|
||||
host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&stats, &count) == KERN_SUCCESS;
|
||||
mach_port_deallocate(mach_task_self(), host);
|
||||
if (ok) {
|
||||
return ((int64_t)stats.free_count + stats.inactive_count) * page_size;
|
||||
}
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
|
||||
static size_t select_main_device(const std::vector<Device>& devices) {
|
||||
size_t main_device = SIZE_MAX;
|
||||
for (size_t di = 0; di < devices.size(); ++di) {
|
||||
if (devices[di].budget_bytes > 0 &&
|
||||
(main_device == SIZE_MAX || devices[di].budget_bytes > devices[main_device].budget_bytes)) {
|
||||
main_device = di;
|
||||
}
|
||||
}
|
||||
return main_device;
|
||||
}
|
||||
|
||||
static Plan compute_plan(const std::vector<Component>& components,
|
||||
const std::vector<Device>& devices,
|
||||
int64_t ram_budget_bytes,
|
||||
const std::vector<Runtime>& runtimes = {}) {
|
||||
Plan plan;
|
||||
plan.main_device = select_main_device(devices);
|
||||
plan.runtimes = runtimes;
|
||||
if (plan.runtimes.empty()) {
|
||||
if (plan.main_device == SIZE_MAX) {
|
||||
return plan;
|
||||
}
|
||||
plan.runtimes.resize(components.size(), {devices[plan.main_device].name, {plan.main_device}});
|
||||
}
|
||||
|
||||
std::vector<size_t> order(components.size());
|
||||
for (size_t i = 0; i < order.size(); i++) {
|
||||
order[i] = i;
|
||||
}
|
||||
std::sort(order.begin(), order.end(), [&](size_t a, size_t b) {
|
||||
return components[a].params_bytes > components[b].params_bytes;
|
||||
});
|
||||
std::vector<size_t> order(components.size());
|
||||
for (size_t ci = 0; ci < components.size(); ++ci) {
|
||||
order[ci] = ci;
|
||||
}
|
||||
std::stable_sort(order.begin(), order.end(), [&](size_t a, size_t b) {
|
||||
return components[a].kind < components[b].kind;
|
||||
});
|
||||
|
||||
{
|
||||
std::vector<int64_t> params_sum(devices.size(), 0);
|
||||
std::vector<int64_t> max_reserve(devices.size(), 0);
|
||||
std::vector<Decision> decisions(components.size());
|
||||
bool ok = true;
|
||||
for (size_t ci : order) {
|
||||
const Component& comp = components[ci];
|
||||
decisions[ci].kind = comp.kind;
|
||||
if (comp.params_bytes == 0) {
|
||||
continue;
|
||||
}
|
||||
int best = -1;
|
||||
for (size_t di = 0; di < devices.size(); di++) {
|
||||
int64_t need = params_sum[di] + comp.params_bytes + std::max(max_reserve[di], comp.reserve_bytes);
|
||||
if (need <= devices[di].budget_bytes &&
|
||||
(best < 0 || devices[di].budget_bytes - params_sum[di] > devices[best].budget_bytes - params_sum[best])) {
|
||||
best = (int)di;
|
||||
}
|
||||
}
|
||||
if (best < 0) {
|
||||
ok = false;
|
||||
break;
|
||||
}
|
||||
params_sum[best] += comp.params_bytes;
|
||||
max_reserve[best] = std::max(max_reserve[best], comp.reserve_bytes);
|
||||
decisions[ci].device_idxs.push_back((size_t)best);
|
||||
}
|
||||
if (ok) {
|
||||
plan.valid = true;
|
||||
plan.time_share = false;
|
||||
plan.decisions = std::move(decisions);
|
||||
return plan;
|
||||
}
|
||||
}
|
||||
std::vector<int64_t> remaining;
|
||||
for (const Device& device : devices) {
|
||||
remaining.push_back(std::max<int64_t>(device.budget_bytes, 0));
|
||||
}
|
||||
ram_budget_bytes = std::max<int64_t>(ram_budget_bytes, 0);
|
||||
plan.decisions.resize(components.size());
|
||||
|
||||
plan.decisions.assign(components.size(), {});
|
||||
for (size_t ci : order) {
|
||||
const Component& comp = components[ci];
|
||||
Decision& decision = plan.decisions[ci];
|
||||
decision.kind = comp.kind;
|
||||
if (comp.params_bytes == 0) {
|
||||
auto uses_device = [&](size_t ci, size_t di) {
|
||||
const auto& runtime_devices = plan.runtimes[ci].devices;
|
||||
return std::find(runtime_devices.begin(), runtime_devices.end(), di) != runtime_devices.end();
|
||||
};
|
||||
auto headroom_for = [&](size_t ci, size_t di) {
|
||||
// Higher-priority offloaded weights need cache space on their compute devices.
|
||||
int64_t headroom = 0;
|
||||
for (size_t other = 0; other < components.size(); ++other) {
|
||||
if (components[other].params_bytes == 0 || !uses_device(other, di)) {
|
||||
continue;
|
||||
}
|
||||
int best = -1;
|
||||
for (size_t di = 0; di < devices.size(); di++) {
|
||||
if (comp.params_bytes + comp.reserve_bytes <= devices[di].budget_bytes &&
|
||||
(best < 0 || devices[di].budget_bytes > devices[best].budget_bytes)) {
|
||||
best = (int)di;
|
||||
}
|
||||
const bool resident = other == ci || plan.decisions[other].params_location == ParamsLocation::MAIN_GPU;
|
||||
const int64_t cached_weights = components[other].kind < components[ci].kind
|
||||
? components[other].params_bytes
|
||||
: components[other].staging_bytes;
|
||||
headroom = std::max(headroom, components[other].reserve_bytes +
|
||||
(resident ? 0 : cached_weights));
|
||||
}
|
||||
return headroom;
|
||||
};
|
||||
|
||||
for (size_t ci : order) {
|
||||
const Component& comp = components[ci];
|
||||
Decision& decision = plan.decisions[ci];
|
||||
if (comp.params_bytes == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const auto& runtime_devices = plan.runtimes[ci].devices;
|
||||
const bool fits_runtime = !runtime_devices.empty() &&
|
||||
std::all_of(runtime_devices.begin(), runtime_devices.end(), [&](size_t di) {
|
||||
const int64_t headroom = headroom_for(ci, di);
|
||||
return headroom <= remaining[di] && comp.params_bytes <= remaining[di] - headroom;
|
||||
});
|
||||
if (fits_runtime) {
|
||||
decision.params_location = ParamsLocation::MAIN_GPU;
|
||||
decision.params_device = runtime_devices.front();
|
||||
// Exact split allocations are unavailable until the runners build their plans.
|
||||
for (size_t di : runtime_devices) {
|
||||
remaining[di] -= comp.params_bytes;
|
||||
}
|
||||
if (best >= 0) {
|
||||
decision.device_idxs.push_back((size_t)best);
|
||||
continue;
|
||||
}
|
||||
if (comp.params_bytes <= ram_budget_bytes) {
|
||||
decision.params_location = ParamsLocation::CPU;
|
||||
ram_budget_bytes -= comp.params_bytes;
|
||||
continue;
|
||||
}
|
||||
if (runtime_devices.empty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
size_t best = SIZE_MAX;
|
||||
for (size_t di = 0; di < devices.size(); ++di) {
|
||||
const int64_t headroom = headroom_for(ci, di);
|
||||
if (!uses_device(ci, di) && headroom <= remaining[di] && comp.params_bytes <= remaining[di] - headroom &&
|
||||
(best == SIZE_MAX || remaining[di] > remaining[best])) {
|
||||
best = di;
|
||||
}
|
||||
}
|
||||
if (best != SIZE_MAX) {
|
||||
decision.params_location = ParamsLocation::OTHER_GPU;
|
||||
decision.params_device = best;
|
||||
remaining[best] -= comp.params_bytes;
|
||||
}
|
||||
}
|
||||
plan.valid = true;
|
||||
return plan;
|
||||
}
|
||||
|
||||
static std::string params_backend_name(const Decision& decision, const std::vector<Device>& devices) {
|
||||
switch (decision.params_location) {
|
||||
case ParamsLocation::MAIN_GPU:
|
||||
case ParamsLocation::OTHER_GPU:
|
||||
return devices[decision.params_device].name;
|
||||
case ParamsLocation::CPU:
|
||||
return "cpu";
|
||||
case ParamsLocation::DISK:
|
||||
return "disk";
|
||||
}
|
||||
return "disk";
|
||||
}
|
||||
|
||||
static void print_plan(const Plan& plan,
|
||||
const std::vector<Component>& components,
|
||||
const std::vector<Device>& devices,
|
||||
int64_t free_ram,
|
||||
int64_t ram_budget) {
|
||||
LOG_INFO("auto-fit plan:");
|
||||
LOG_INFO(" devices:");
|
||||
for (const Device& device : devices) {
|
||||
LOG_INFO(" %-12s %-32s free %6lld MiB, budget %6lld MiB",
|
||||
device.name.c_str(), device.description.c_str(),
|
||||
(long long)(device.free_bytes / MiB), (long long)(device.budget_bytes / MiB));
|
||||
}
|
||||
if (free_ram < 0) {
|
||||
LOG_WARN("auto-fit: available RAM is unknown; skipping CPU parameter residency");
|
||||
} else {
|
||||
LOG_INFO(" RAM free %6lld MiB, params budget %6lld MiB",
|
||||
(long long)(free_ram / MiB), (long long)(ram_budget / MiB));
|
||||
}
|
||||
LOG_INFO(" compute-device weight cache priority: diffusion > te > vae");
|
||||
LOG_INFO(" components (params: compute device -> RAM -> other GPU -> disk):");
|
||||
for (size_t ci = 0; ci < components.size(); ++ci) {
|
||||
const Component& comp = components[ci];
|
||||
if (comp.params_bytes == 0) {
|
||||
continue;
|
||||
}
|
||||
const std::string params = plan.decisions[ci].params_location == ParamsLocation::MAIN_GPU
|
||||
? plan.runtimes[ci].name
|
||||
: params_backend_name(plan.decisions[ci], devices);
|
||||
LOG_INFO(" %-12s params %6lld MiB, compute reserve %5lld MiB -> compute %s, params %s",
|
||||
comp.name, (long long)(comp.params_bytes / MiB), (long long)(comp.reserve_bytes / MiB),
|
||||
plan.runtimes[ci].name.c_str(), params.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
static void append_assignment(std::string& spec, const char* key, const std::string& value) {
|
||||
if (!spec.empty()) {
|
||||
spec += ",";
|
||||
}
|
||||
spec += key;
|
||||
spec += "=";
|
||||
spec += value;
|
||||
}
|
||||
|
||||
static const char* module_key(ComponentKind kind) {
|
||||
switch (kind) {
|
||||
case ComponentKind::DIT:
|
||||
return "diffusion";
|
||||
case ComponentKind::CONDITIONER:
|
||||
return "te";
|
||||
case ComponentKind::VAE:
|
||||
return "vae";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
static bool resolve_runtimes(const std::vector<Component>& components,
|
||||
const std::vector<Device>& devices,
|
||||
std::string& runtime_spec,
|
||||
std::vector<Runtime>& runtimes,
|
||||
std::string& error) {
|
||||
SDBackendAssignment assignment;
|
||||
if (!sd_parse_backend_assignment(runtime_spec, &assignment, &error)) {
|
||||
return false;
|
||||
}
|
||||
const size_t main_device = select_main_device(devices);
|
||||
const SDBackendModule modules[] = {SDBackendModule::DIFFUSION, SDBackendModule::TE, SDBackendModule::VAE};
|
||||
for (const Component& comp : components) {
|
||||
std::string name = assignment.get(modules[int(comp.kind)]);
|
||||
if (name.empty()) {
|
||||
name = main_device == SIZE_MAX ? "cpu" : devices[main_device].name;
|
||||
if (comp.params_bytes > 0) {
|
||||
append_assignment(runtime_spec, module_key(comp.kind), name);
|
||||
}
|
||||
}
|
||||
Runtime runtime;
|
||||
for (const std::string& part : split_string(name, '&')) {
|
||||
if (trim(part).empty()) {
|
||||
continue;
|
||||
}
|
||||
if (comp.splittable && devices.size() > 1) {
|
||||
int64_t capacity = 0;
|
||||
for (const Device& d : devices) {
|
||||
capacity += std::max<int64_t>(d.budget_bytes - comp.reserve_bytes, 0);
|
||||
}
|
||||
if (comp.params_bytes <= capacity) {
|
||||
std::vector<size_t> idxs(devices.size());
|
||||
for (size_t i = 0; i < idxs.size(); i++) {
|
||||
idxs[i] = i;
|
||||
}
|
||||
std::sort(idxs.begin(), idxs.end(), [&](size_t a, size_t b) {
|
||||
return devices[a].budget_bytes > devices[b].budget_bytes;
|
||||
});
|
||||
decision.device_idxs = std::move(idxs);
|
||||
continue;
|
||||
const std::string resolved = sd_backend_resolve_name(part);
|
||||
if (resolved.empty()) {
|
||||
error = "backend '" + part + "' was not found";
|
||||
return false;
|
||||
}
|
||||
if (!runtime.name.empty()) {
|
||||
runtime.name += "&";
|
||||
}
|
||||
runtime.name += resolved;
|
||||
for (size_t di = 0; di < devices.size(); ++di) {
|
||||
if (devices[di].name == resolved &&
|
||||
std::find(runtime.devices.begin(), runtime.devices.end(), di) == runtime.devices.end()) {
|
||||
runtime.devices.push_back(di);
|
||||
}
|
||||
}
|
||||
decision.on_cpu = true;
|
||||
}
|
||||
plan.valid = true;
|
||||
plan.time_share = true;
|
||||
return plan;
|
||||
runtimes.push_back(std::move(runtime));
|
||||
}
|
||||
|
||||
void print_plan(const Plan& plan,
|
||||
const std::vector<Component>& components,
|
||||
const std::vector<Device>& devices) {
|
||||
LOG_INFO("auto-fit plan%s:", plan.time_share ? " (time-share: params load per phase and free after)" : "");
|
||||
LOG_INFO(" devices:");
|
||||
for (const Device& d : devices) {
|
||||
LOG_INFO(" %-12s %-32s free %6lld MiB, budget %6lld MiB",
|
||||
d.name.c_str(), d.description.c_str(),
|
||||
(long long)(d.free_bytes / MiB), (long long)(d.budget_bytes / MiB));
|
||||
}
|
||||
LOG_INFO(" components:");
|
||||
for (size_t ci = 0; ci < components.size(); ci++) {
|
||||
const Component& comp = components[ci];
|
||||
const Decision& decision = plan.decisions[ci];
|
||||
std::string target;
|
||||
if (comp.params_bytes == 0) {
|
||||
target = "(not present)";
|
||||
} else if (decision.on_cpu) {
|
||||
target = "CPU";
|
||||
} else {
|
||||
for (size_t k = 0; k < decision.device_idxs.size(); k++) {
|
||||
if (k > 0) {
|
||||
target += " & ";
|
||||
}
|
||||
target += devices[decision.device_idxs[k]].name;
|
||||
}
|
||||
if (decision.device_idxs.size() > 1) {
|
||||
target += " (split)";
|
||||
}
|
||||
}
|
||||
LOG_INFO(" %-12s params %6lld MiB, compute reserve %5lld MiB -> %s",
|
||||
comp.name,
|
||||
(long long)(comp.params_bytes / MiB),
|
||||
(long long)(comp.reserve_bytes / MiB),
|
||||
target.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void append_assignment(std::string& spec, const char* key, const std::string& value) {
|
||||
if (!spec.empty()) {
|
||||
spec += ",";
|
||||
}
|
||||
spec += key;
|
||||
spec += "=";
|
||||
spec += value;
|
||||
}
|
||||
|
||||
void append_component_decision(const std::vector<Component>& components,
|
||||
const std::vector<Device>& devices,
|
||||
const Plan& plan,
|
||||
ComponentKind kind,
|
||||
const char* module_key,
|
||||
std::string& runtime_spec,
|
||||
std::string& params_spec) {
|
||||
for (size_t ci = 0; ci < components.size(); ci++) {
|
||||
if (components[ci].kind != kind || components[ci].params_bytes == 0) {
|
||||
continue;
|
||||
}
|
||||
const Decision& decision = plan.decisions[ci];
|
||||
if (decision.on_cpu) {
|
||||
append_assignment(runtime_spec, module_key, "cpu");
|
||||
return;
|
||||
}
|
||||
if (decision.device_idxs.empty()) {
|
||||
return;
|
||||
}
|
||||
std::string device_list;
|
||||
for (size_t k = 0; k < decision.device_idxs.size(); k++) {
|
||||
if (k > 0) {
|
||||
device_list += "&";
|
||||
}
|
||||
device_list += devices[decision.device_idxs[k]].name;
|
||||
}
|
||||
append_assignment(runtime_spec, module_key, device_list);
|
||||
if (plan.time_share) {
|
||||
append_assignment(params_spec, module_key, "disk");
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
return true;
|
||||
}
|
||||
|
||||
bool derive_backend_specs(ModelLoader& loader,
|
||||
ggml_type override_wtype,
|
||||
sd::ggml_graph_cut::MaxVramAssignment& budgets,
|
||||
std::string& runtime_spec,
|
||||
std::string& params_spec) {
|
||||
if (!runtime_spec.empty() || !params_spec.empty()) {
|
||||
LOG_WARN("--auto-fit is enabled; ignoring --backend / --params-backend");
|
||||
std::string error;
|
||||
if (!budgets.canonicalize_backend_keys(&error)) {
|
||||
LOG_ERROR("%s", error.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
{
|
||||
std::string error;
|
||||
if (!budgets.canonicalize_backend_keys(&error)) {
|
||||
LOG_ERROR("%s", error.c_str());
|
||||
return false;
|
||||
}
|
||||
// Resolve once to ensure dynamic backends are loaded before enumerating devices.
|
||||
sd_backend_resolve_name("");
|
||||
const auto components = estimate_components(loader, override_wtype);
|
||||
const auto devices = enumerate_gpu_devices(budgets, !runtime_spec.empty());
|
||||
std::vector<Runtime> runtimes;
|
||||
if (!runtime_spec.empty() && !resolve_runtimes(components, devices, runtime_spec, runtimes, error)) {
|
||||
LOG_ERROR("%s", error.c_str());
|
||||
return false;
|
||||
}
|
||||
|
||||
auto components = estimate_components(loader, override_wtype);
|
||||
auto devices = enumerate_gpu_devices(budgets);
|
||||
auto plan = compute_plan(components, devices);
|
||||
const int64_t free_ram = available_ram_bytes();
|
||||
const int64_t ram_budget = std::max<int64_t>(free_ram - std::max<int64_t>(2048 * MiB, free_ram / 10), 0);
|
||||
const auto plan = compute_plan(components, devices, ram_budget, runtimes);
|
||||
params_spec.clear();
|
||||
if (!plan.valid) {
|
||||
LOG_WARN("auto-fit: no usable GPU devices; using the default backend");
|
||||
runtime_spec.clear();
|
||||
params_spec.clear();
|
||||
if (devices.empty()) {
|
||||
LOG_WARN("auto-fit: no GPU devices; using the default backend");
|
||||
} else {
|
||||
LOG_WARN("auto-fit: no GPU memory budget available; using CPU");
|
||||
runtime_spec = "cpu";
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
print_plan(plan, components, devices);
|
||||
print_plan(plan, components, devices, free_ram, ram_budget);
|
||||
for (size_t ci = 0; ci < components.size(); ++ci) {
|
||||
if (components[ci].params_bytes == 0) {
|
||||
continue;
|
||||
}
|
||||
const char* key = module_key(components[ci].kind);
|
||||
if (runtimes.empty()) {
|
||||
append_assignment(runtime_spec, key, plan.runtimes[ci].name);
|
||||
}
|
||||
if (plan.decisions[ci].params_location != ParamsLocation::MAIN_GPU) {
|
||||
append_assignment(params_spec, key, params_backend_name(plan.decisions[ci], devices));
|
||||
}
|
||||
}
|
||||
|
||||
std::string derived_runtime_spec;
|
||||
std::string derived_params_spec;
|
||||
append_component_decision(components, devices, plan, ComponentKind::DIT, "diffusion", derived_runtime_spec, derived_params_spec);
|
||||
append_component_decision(components, devices, plan, ComponentKind::CONDITIONER, "te", derived_runtime_spec, derived_params_spec);
|
||||
append_component_decision(components, devices, plan, ComponentKind::VAE, "vae", derived_runtime_spec, derived_params_spec);
|
||||
|
||||
runtime_spec = std::move(derived_runtime_spec);
|
||||
params_spec = std::move(derived_params_spec);
|
||||
// Keep the planner's safety margin when the runner resolves its device limits.
|
||||
for (const Device& device : devices) {
|
||||
if (device.budget_bytes > 0) {
|
||||
budgets.backend_gib[budget_key(device.name)] = (float)(device.budget_bytes / (1024.0 * MiB));
|
||||
}
|
||||
}
|
||||
budgets.resolved_backend_bytes.clear();
|
||||
|
||||
LOG_INFO("auto-fit: --backend \"%s\"%s%s%s",
|
||||
runtime_spec.empty() ? "(default)" : runtime_spec.c_str(),
|
||||
params_spec.empty() ? "" : " --params-backend \"",
|
||||
params_spec.c_str(),
|
||||
params_spec.empty() ? "" : "\"");
|
||||
params_spec.c_str(), params_spec.empty() ? "" : "\"");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool prepare_vae_decode_retry_tiling(sd_tiling_params_t& tiling_params, bool prefer_temporal_tiling) {
|
||||
if (prefer_temporal_tiling) {
|
||||
if (tiling_params.temporal_tiling) {
|
||||
return false;
|
||||
}
|
||||
const char* retry_mode = nullptr;
|
||||
if (prefer_temporal_tiling && !tiling_params.temporal_tiling) {
|
||||
tiling_params.temporal_tiling = true;
|
||||
} else {
|
||||
if (tiling_params.enabled) {
|
||||
return false;
|
||||
}
|
||||
tiling_params.enabled = true;
|
||||
retry_mode = tiling_params.enabled ? "spatial+temporal" : "temporal";
|
||||
} else if (!tiling_params.enabled) {
|
||||
tiling_params.enabled = true;
|
||||
tiling_params.rel_size_x = 0.5f;
|
||||
tiling_params.rel_size_y = 0.5f;
|
||||
if (tiling_params.tile_size_x <= 0) {
|
||||
tiling_params.tile_size_x = 256;
|
||||
}
|
||||
if (tiling_params.tile_size_y <= 0) {
|
||||
tiling_params.tile_size_y = 256;
|
||||
}
|
||||
retry_mode = tiling_params.temporal_tiling ? "spatial+temporal" : "spatial";
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
LOG_WARN("auto-fit: VAE decode failed (likely out of memory); retrying with %s tiling",
|
||||
tiling_params.temporal_tiling ? "temporal" : "spatial");
|
||||
LOG_WARN("VAE decode failed (likely out of memory); retrying with %s tiling",
|
||||
retry_mode);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,271 @@
|
||||
#include "core/compute_workspace.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "core/ggml_extend_backend.h"
|
||||
#include "core/ggml_graph_cut.h"
|
||||
#include "core/util.h"
|
||||
#include "ggml-cpu.h"
|
||||
#include "ggml-impl.h"
|
||||
|
||||
namespace sd {
|
||||
ComputeWorkspace::~ComputeWorkspace() {
|
||||
segment_end();
|
||||
release();
|
||||
ggml_backend_free(cpu_backend_);
|
||||
}
|
||||
|
||||
void ComputeWorkspace::set_extra_backends(const std::vector<ggml_backend_t>& backends) {
|
||||
if (extra_backends_ != backends) {
|
||||
GGML_ASSERT(!active_);
|
||||
release();
|
||||
extra_backends_ = backends;
|
||||
}
|
||||
}
|
||||
|
||||
bool ComputeWorkspace::needs_scheduler(ggml_cgraph* graph) const {
|
||||
if (!extra_backends_.empty()) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < ggml_graph_n_nodes(graph); ++i) {
|
||||
if (!ggml_backend_supports_op(backend_, ggml_graph_node(graph, i))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ggml_backend_sched_t ComputeWorkspace::make_scheduler(size_t graph_size) {
|
||||
std::vector<ggml_backend_t> backends{backend_};
|
||||
backends.insert(backends.end(), extra_backends_.begin(), extra_backends_.end());
|
||||
if (!sd_backend_is_cpu(backend_)) {
|
||||
if (cpu_backend_ == nullptr) {
|
||||
cpu_backend_ = sd_backend_cpu_init();
|
||||
}
|
||||
if (cpu_backend_ == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
backends.push_back(cpu_backend_);
|
||||
}
|
||||
std::vector<ggml_backend_buffer_type_t> bufts;
|
||||
for (auto backend : backends) {
|
||||
auto buft = backend == cpu_backend_
|
||||
? ggml_backend_dev_host_buffer_type(ggml_backend_get_device(backend_))
|
||||
: nullptr;
|
||||
bufts.push_back(buft != nullptr ? buft : ggml_backend_get_default_buffer_type(backend));
|
||||
}
|
||||
return ggml_backend_sched_new(backends.data(), bufts.data(), static_cast<int>(backends.size()),
|
||||
graph_size, false, false);
|
||||
}
|
||||
|
||||
bool ComputeWorkspace::measurement_matches(ggml_cgraph* graph, const Measurement& measurement) const {
|
||||
return measurement.scheduler == needs_scheduler(graph);
|
||||
}
|
||||
|
||||
bool ComputeWorkspace::prepare(const Measurement& measurement) {
|
||||
GGML_ASSERT(!active_);
|
||||
if (measurement.buffers.empty()) {
|
||||
return false;
|
||||
}
|
||||
const bool grows = std::any_of(measurement.buffers.begin(), measurement.buffers.end(),
|
||||
[&](const BackendBufferSize& size) { return size.bytes > bytes(size.backend); });
|
||||
if (measurement.scheduler != (scheduler_ != nullptr) || grows) {
|
||||
release();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ComputeWorkspace::release_excess(const Measurement& measurement) {
|
||||
return std::any_of(measurement.buffers.begin(), measurement.buffers.end(),
|
||||
[&](const BackendBufferSize& size) { return bytes(size.backend) > size.bytes; }) &&
|
||||
release();
|
||||
}
|
||||
|
||||
bool ComputeWorkspace::allocate(ggml_cgraph* graph, const AssignNodes& assign_nodes) {
|
||||
GGML_ASSERT(!active_);
|
||||
const bool use_scheduler = needs_scheduler(graph);
|
||||
if (use_scheduler) {
|
||||
if (allocator_ != nullptr) {
|
||||
release();
|
||||
}
|
||||
const size_t capacity = static_cast<size_t>(graph->n_nodes + graph->n_leafs) + 8;
|
||||
if (scheduler_ == nullptr || capacity > scheduler_capacity_) {
|
||||
release();
|
||||
scheduler_ = make_scheduler(capacity);
|
||||
scheduler_capacity_ = capacity;
|
||||
}
|
||||
if (scheduler_ == nullptr) {
|
||||
return false;
|
||||
}
|
||||
ggml_backend_sched_reset(scheduler_);
|
||||
assign_nodes(scheduler_, graph);
|
||||
// Scheduler allocation rewrites sources. Split the execution graph only once.
|
||||
if (!ggml_backend_sched_alloc_graph(scheduler_, graph)) {
|
||||
release();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if (scheduler_ != nullptr) {
|
||||
release();
|
||||
}
|
||||
if (allocator_ == nullptr) {
|
||||
allocator_ = ggml_gallocr_new(ggml_backend_get_default_buffer_type(backend_));
|
||||
}
|
||||
auto signature = ggml_graph_cut::graph_layout(graph, true);
|
||||
if (signature != reservation_) {
|
||||
if (!ggml_gallocr_reserve(allocator_, graph)) {
|
||||
release();
|
||||
return false;
|
||||
}
|
||||
reservation_ = std::move(signature);
|
||||
++reservations_;
|
||||
}
|
||||
if (!ggml_gallocr_alloc_graph(allocator_, graph)) {
|
||||
release();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
active_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
ComputeWorkspace::Measurement ComputeWorkspace::measure(
|
||||
ggml_cgraph* graph,
|
||||
size_t direct_bytes,
|
||||
const std::function<ggml_backend_t(const ggml_tensor*)>& external_backend,
|
||||
const AssignNodes& assign_nodes) {
|
||||
if (!needs_scheduler(graph)) {
|
||||
return {{{backend_, direct_bytes}}, false};
|
||||
}
|
||||
std::vector<const ggml_tensor*> tensors;
|
||||
std::unordered_set<const ggml_tensor*> seen;
|
||||
auto visit = [&](const ggml_tensor* tensor) {
|
||||
if (tensor != nullptr && seen.insert(tensor).second) {
|
||||
tensors.push_back(tensor);
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < graph->n_nodes; ++i) {
|
||||
visit(graph->nodes[i]);
|
||||
}
|
||||
for (int i = 0; i < graph->n_leafs; ++i) {
|
||||
visit(graph->leafs[i]);
|
||||
}
|
||||
for (size_t i = 0; i < tensors.size(); ++i) {
|
||||
visit(tensors[i]->view_src);
|
||||
for (auto source : tensors[i]->src) {
|
||||
visit(source);
|
||||
}
|
||||
}
|
||||
const size_t graph_size = tensors.size() + 8;
|
||||
auto context = ggml_init({tensors.size() * ggml_tensor_overhead() + ggml_graph_overhead_custom(graph_size, false), nullptr, true});
|
||||
if (context == nullptr) {
|
||||
return {};
|
||||
}
|
||||
std::unordered_map<const ggml_tensor*, ggml_tensor*> copies;
|
||||
std::map<ggml_backend_t, ggml_backend_buffer_t> external_buffers;
|
||||
for (auto tensor : tensors) {
|
||||
auto copy = ggml_dup_tensor(context, tensor);
|
||||
*copy = *tensor;
|
||||
copies[tensor] = copy;
|
||||
}
|
||||
for (const auto& entry : copies) {
|
||||
auto source = entry.first;
|
||||
auto copy = entry.second;
|
||||
copy->view_src = source->view_src == nullptr ? nullptr : copies.at(source->view_src);
|
||||
for (int i = 0; i < GGML_MAX_SRC; ++i) {
|
||||
copy->src[i] = source->src[i] == nullptr ? nullptr : copies.at(source->src[i]);
|
||||
}
|
||||
auto external = external_backend(source);
|
||||
if (external != nullptr && source->view_src == nullptr) {
|
||||
auto& buffer = external_buffers[external];
|
||||
if (buffer == nullptr) {
|
||||
buffer = ggml_backend_alloc_buffer(external, 0);
|
||||
GGML_ASSERT(buffer != nullptr);
|
||||
ggml_backend_buffer_set_usage(buffer, GGML_BACKEND_BUFFER_USAGE_WEIGHTS);
|
||||
}
|
||||
copy->buffer = buffer;
|
||||
copy->data = reinterpret_cast<void*>(static_cast<uintptr_t>(1));
|
||||
copy->extra = nullptr;
|
||||
}
|
||||
}
|
||||
auto copy_graph = ggml_new_graph_custom(context, graph_size, false);
|
||||
copy_graph->n_nodes = graph->n_nodes;
|
||||
copy_graph->n_leafs = graph->n_leafs;
|
||||
for (int i = 0; i < graph->n_nodes; ++i) {
|
||||
copy_graph->nodes[i] = copies.at(graph->nodes[i]);
|
||||
}
|
||||
for (int i = 0; i < graph->n_leafs; ++i) {
|
||||
copy_graph->leafs[i] = copies.at(graph->leafs[i]);
|
||||
}
|
||||
Measurement result;
|
||||
result.scheduler = true;
|
||||
auto scheduler = make_scheduler(graph_size);
|
||||
if (scheduler != nullptr) {
|
||||
assign_nodes(scheduler, copy_graph);
|
||||
std::vector<size_t> sizes(extra_backends_.size() + 2);
|
||||
ggml_backend_sched_reserve_size(scheduler, copy_graph, sizes.data());
|
||||
result.buffers.push_back({backend_, sizes[0]});
|
||||
for (size_t i = 0; i < extra_backends_.size(); ++i) {
|
||||
result.buffers.push_back({extra_backends_[i], sizes[i + 1]});
|
||||
}
|
||||
ggml_backend_sched_free(scheduler);
|
||||
}
|
||||
for (const auto& entry : external_buffers) {
|
||||
ggml_backend_buffer_free(entry.second);
|
||||
}
|
||||
ggml_free(context);
|
||||
return result;
|
||||
}
|
||||
|
||||
void ComputeWorkspace::synchronize() const {
|
||||
if (scheduler_ != nullptr) {
|
||||
ggml_backend_sched_synchronize(scheduler_);
|
||||
} else {
|
||||
ggml_backend_synchronize(backend_);
|
||||
}
|
||||
}
|
||||
|
||||
bool ComputeWorkspace::segment_end() noexcept {
|
||||
if (!active_) {
|
||||
return true;
|
||||
}
|
||||
// Outer cleanup guards must not retry a failed backend submission.
|
||||
active_ = false;
|
||||
try {
|
||||
synchronize();
|
||||
return true;
|
||||
} catch (const std::exception& error) {
|
||||
LOG_ERROR("%s workspace synchronization failed during segment cleanup: %s",
|
||||
ggml_backend_name(backend_), error.what());
|
||||
} catch (...) {
|
||||
LOG_ERROR("%s workspace synchronization failed during segment cleanup: unknown exception",
|
||||
ggml_backend_name(backend_));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ComputeWorkspace::release() {
|
||||
if (active_) {
|
||||
return false;
|
||||
}
|
||||
ggml_gallocr_free(allocator_);
|
||||
allocator_ = nullptr;
|
||||
ggml_backend_sched_free(scheduler_);
|
||||
scheduler_ = nullptr;
|
||||
scheduler_capacity_ = 0;
|
||||
reservation_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t ComputeWorkspace::bytes(ggml_backend_t backend) const {
|
||||
if (scheduler_ != nullptr) {
|
||||
return ggml_backend_sched_get_buffer_size(scheduler_, backend);
|
||||
}
|
||||
return allocator_ != nullptr && backend == backend_ ? ggml_gallocr_get_buffer_size(allocator_, 0) : 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#ifndef __SD_CORE_COMPUTE_WORKSPACE_H__
|
||||
#define __SD_CORE_COMPUTE_WORKSPACE_H__
|
||||
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
#include "ggml-alloc.h"
|
||||
#include "ggml-backend.h"
|
||||
|
||||
namespace sd {
|
||||
struct BackendBufferSize {
|
||||
ggml_backend_t backend = nullptr;
|
||||
size_t bytes = 0;
|
||||
};
|
||||
|
||||
class ComputeWorkspace {
|
||||
ggml_backend_t backend_;
|
||||
std::vector<ggml_backend_t> extra_backends_;
|
||||
ggml_backend_t cpu_backend_ = nullptr;
|
||||
ggml_gallocr_t allocator_ = nullptr;
|
||||
ggml_backend_sched_t scheduler_ = nullptr;
|
||||
size_t scheduler_capacity_ = 0;
|
||||
std::vector<uint64_t> reservation_;
|
||||
bool active_ = false;
|
||||
size_t reservations_ = 0;
|
||||
|
||||
ggml_backend_sched_t make_scheduler(size_t graph_size);
|
||||
bool needs_scheduler(ggml_cgraph* graph) const;
|
||||
|
||||
public:
|
||||
struct Measurement {
|
||||
std::vector<BackendBufferSize> buffers;
|
||||
bool scheduler = false;
|
||||
};
|
||||
using AssignNodes = std::function<void(ggml_backend_sched_t, ggml_cgraph*)>;
|
||||
|
||||
explicit ComputeWorkspace(ggml_backend_t backend)
|
||||
: backend_(backend) {}
|
||||
~ComputeWorkspace();
|
||||
ComputeWorkspace(const ComputeWorkspace&) = delete;
|
||||
ComputeWorkspace& operator=(const ComputeWorkspace&) = delete;
|
||||
|
||||
void set_extra_backends(const std::vector<ggml_backend_t>& backends);
|
||||
bool measurement_matches(ggml_cgraph* graph, const Measurement& measurement) const;
|
||||
bool prepare(const Measurement& measurement);
|
||||
bool release_excess(const Measurement& measurement);
|
||||
bool allocate(ggml_cgraph* graph, const AssignNodes& assign_nodes);
|
||||
Measurement measure(
|
||||
ggml_cgraph* graph,
|
||||
size_t direct_bytes,
|
||||
const std::function<ggml_backend_t(const ggml_tensor*)>& external_backend,
|
||||
const AssignNodes& assign_nodes);
|
||||
void synchronize() const;
|
||||
bool segment_end() noexcept;
|
||||
bool release();
|
||||
bool active() const { return active_; }
|
||||
ggml_backend_sched_t scheduler() const { return scheduler_; }
|
||||
ggml_backend_t cpu_backend() const { return cpu_backend_; }
|
||||
size_t bytes(ggml_backend_t backend) const;
|
||||
size_t reservation_count() const { return reservations_; }
|
||||
};
|
||||
}
|
||||
|
||||
#endif // __SD_CORE_COMPUTE_WORKSPACE_H__
|
||||
@@ -0,0 +1,876 @@
|
||||
#include "core/ggml_extend.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
#include "core/ggml_extend_backend.h"
|
||||
|
||||
ggml_tensor* ggml_ext_mul_n_mode(ggml_context* ctx, ggml_tensor* a, ggml_tensor* b, int mode) {
|
||||
// reshape A
|
||||
// swap 0th and nth axis
|
||||
a = ggml_cont(ctx, ggml_permute(ctx, a, mode, mode != 1 ? 1 : 0, mode != 2 ? 2 : 0, mode != 3 ? 3 : 0));
|
||||
int64_t ne1 = a->ne[1];
|
||||
int64_t ne2 = a->ne[2];
|
||||
int64_t ne3 = a->ne[3];
|
||||
// make 2D
|
||||
a = ggml_cont(ctx, ggml_reshape_2d(ctx, a, a->ne[0], (ne3 * ne2 * ne1)));
|
||||
|
||||
ggml_tensor* result = ggml_cont(ctx, ggml_transpose(ctx, ggml_mul_mat(ctx, a, b)));
|
||||
|
||||
// reshape output (same shape as a after permutation except first dim)
|
||||
result = ggml_reshape_4d(ctx, result, result->ne[0], ne1, ne2, ne3);
|
||||
// swap back 0th and nth axis
|
||||
result = ggml_permute(ctx, result, mode, mode != 1 ? 1 : 0, mode != 2 ? 2 : 0, mode != 3 ? 3 : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_kronecker(ggml_context* ctx, ggml_tensor* a, ggml_tensor* b) {
|
||||
return ggml_mul(ctx,
|
||||
ggml_interpolate(ctx,
|
||||
a,
|
||||
a->ne[0] * b->ne[0],
|
||||
a->ne[1] * b->ne[1],
|
||||
a->ne[2] * b->ne[2],
|
||||
a->ne[3] * b->ne[3],
|
||||
GGML_SCALE_MODE_NEAREST),
|
||||
b);
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_cont(ggml_context* ctx,
|
||||
ggml_tensor* x) {
|
||||
if (ggml_is_contiguous(x)) {
|
||||
return x;
|
||||
}
|
||||
return ggml_cont(ctx, x);
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_torch_permute(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
int axis0,
|
||||
int axis1,
|
||||
int axis2,
|
||||
int axis3) {
|
||||
int torch_axes[4] = {axis0, axis1, axis2, axis3};
|
||||
|
||||
int ggml_axes[4] = {0};
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
int found = 0;
|
||||
for (int j = 0; j < 4; ++j) {
|
||||
if (torch_axes[j] == i) {
|
||||
ggml_axes[i] = j;
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
GGML_ASSERT(found && "Invalid permute input: must be a permutation of 0-3");
|
||||
}
|
||||
|
||||
return ggml_permute(ctx, x, ggml_axes[0], ggml_axes[1], ggml_axes[2], ggml_axes[3]);
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_slice(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
int dim,
|
||||
int64_t start,
|
||||
int64_t end,
|
||||
bool cont) {
|
||||
GGML_ASSERT(dim >= 0 && dim < 4);
|
||||
if (x->ne[dim] == 1) {
|
||||
return x;
|
||||
}
|
||||
while (start < 0) {
|
||||
start = x->ne[dim] + start;
|
||||
}
|
||||
while (end < 0) {
|
||||
end = x->ne[dim] + end;
|
||||
}
|
||||
GGML_ASSERT(end > start);
|
||||
GGML_ASSERT(start >= 0 && start < x->ne[dim]);
|
||||
GGML_ASSERT(end > start && end <= x->ne[dim]);
|
||||
|
||||
int64_t slice_size = end - start;
|
||||
int64_t slice_ne[4] = {x->ne[0], x->ne[1], x->ne[2], x->ne[3]};
|
||||
slice_ne[dim] = slice_size;
|
||||
|
||||
x = ggml_view_4d(ctx, x,
|
||||
slice_ne[0], slice_ne[1], slice_ne[2], slice_ne[3],
|
||||
x->nb[1], x->nb[2], x->nb[3], start * x->nb[dim]);
|
||||
|
||||
if (cont) {
|
||||
x = ggml_cont(ctx, x);
|
||||
}
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
std::vector<ggml_tensor*> ggml_ext_chunk(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
int num,
|
||||
int64_t dim,
|
||||
bool cont) {
|
||||
GGML_ASSERT(dim >= 0 && dim < 4);
|
||||
GGML_ASSERT(x->ne[dim] % num == 0);
|
||||
|
||||
std::vector<ggml_tensor*> chunks;
|
||||
int64_t chunk_size = x->ne[dim] / num;
|
||||
int64_t stride = chunk_size * x->nb[dim];
|
||||
int64_t chunk_ne[4] = {x->ne[0], x->ne[1], x->ne[2], x->ne[3]};
|
||||
chunk_ne[dim] = chunk_size;
|
||||
for (int i = 0; i < num; i++) {
|
||||
auto chunk = ggml_view_4d(
|
||||
ctx, x,
|
||||
chunk_ne[0], chunk_ne[1], chunk_ne[2], chunk_ne[3],
|
||||
x->nb[1], x->nb[2], x->nb[3], stride * i);
|
||||
if (cont) {
|
||||
chunk = ggml_cont(ctx, chunk);
|
||||
}
|
||||
chunks.push_back(chunk);
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_silu_act(ggml_context* ctx, ggml_tensor* x, bool gate_first) {
|
||||
// x: [ne3, ne2, ne1, ne0]
|
||||
// return: [ne3, ne2, ne1, ne0/2]
|
||||
|
||||
auto x_vec = ggml_ext_chunk(ctx, x, 2, 0, false);
|
||||
ggml_tensor* gate;
|
||||
if (gate_first) {
|
||||
gate = x_vec[0];
|
||||
x = x_vec[1];
|
||||
} else {
|
||||
x = x_vec[0];
|
||||
gate = x_vec[1];
|
||||
}
|
||||
gate = ggml_cont(ctx, gate);
|
||||
gate = ggml_silu_inplace(ctx, gate);
|
||||
|
||||
x = ggml_mul(ctx, x, gate); // [ne3, ne2, ne1, ne0/2]
|
||||
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_group_norm_32(ggml_context* ctx,
|
||||
ggml_tensor* a) {
|
||||
const float eps = 1e-6f; // default eps parameter
|
||||
return ggml_group_norm(ctx, a, 32, eps);
|
||||
}
|
||||
|
||||
static bool ggml_ext_is_padded_1d(const ggml_tensor* x) {
|
||||
return x->nb[0] == ggml_type_size(x->type) &&
|
||||
x->nb[2] == x->nb[1] * x->ne[1] &&
|
||||
x->nb[3] == x->nb[2] * x->ne[2];
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_scale(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
float factor,
|
||||
bool inplace) {
|
||||
if (!ggml_ext_is_padded_1d(x)) {
|
||||
x = ggml_cont(ctx, x);
|
||||
}
|
||||
if (inplace) {
|
||||
x = ggml_scale_inplace(ctx, x, factor);
|
||||
} else {
|
||||
x = ggml_scale(ctx, x, factor);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_gelu(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
bool inplace) {
|
||||
if (!ggml_is_contiguous(x)) {
|
||||
x = ggml_cont(ctx, x);
|
||||
}
|
||||
if (inplace) {
|
||||
x = ggml_gelu_inplace(ctx, x);
|
||||
} else {
|
||||
x = ggml_gelu(ctx, x);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_gelu_quick(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
bool inplace) {
|
||||
if (!ggml_is_contiguous(x)) {
|
||||
x = ggml_cont(ctx, x);
|
||||
}
|
||||
if (inplace) {
|
||||
x = ggml_gelu_quick_inplace(ctx, x);
|
||||
} else {
|
||||
x = ggml_gelu_quick(ctx, x);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_linear(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
bool force_prec_f32,
|
||||
float scale) {
|
||||
if (scale != 1.f) {
|
||||
x = ggml_ext_scale(ctx, x, scale);
|
||||
}
|
||||
if (x->ne[2] * x->ne[3] > 1024) {
|
||||
// workaround: avoid ggml cuda error
|
||||
int64_t ne2 = x->ne[2];
|
||||
int64_t ne3 = x->ne[3];
|
||||
x = ggml_reshape_2d(ctx, x, x->ne[0], x->ne[1] * x->ne[2] * x->ne[3]);
|
||||
x = ggml_mul_mat(ctx, w, x);
|
||||
if (force_prec_f32) {
|
||||
ggml_mul_mat_set_prec(x, GGML_PREC_F32);
|
||||
}
|
||||
x = ggml_reshape_4d(ctx, x, x->ne[0], x->ne[1] / ne2 / ne3, ne2, ne3);
|
||||
} else {
|
||||
x = ggml_mul_mat(ctx, w, x);
|
||||
if (force_prec_f32) {
|
||||
ggml_mul_mat_set_prec(x, GGML_PREC_F32);
|
||||
}
|
||||
}
|
||||
if (scale != 1.f) {
|
||||
x = ggml_ext_scale(ctx, x, 1.f / scale);
|
||||
}
|
||||
if (b != nullptr) {
|
||||
x = ggml_add_inplace(ctx, x, b);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_linear_i8_tensorwise(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* weight_scale,
|
||||
ggml_tensor* b,
|
||||
int convrot_group_size,
|
||||
float scale) {
|
||||
#ifndef SD_USE_UPSTREAM_GGML
|
||||
GGML_ASSERT(x->type == GGML_TYPE_F32 || (x->type == GGML_TYPE_I8 && scale == 1.f));
|
||||
if (scale != 1.f) {
|
||||
x = ggml_ext_scale(ctx, x, scale);
|
||||
}
|
||||
|
||||
ggml_tensor* fused_bias = scale == 1.f ? b : nullptr;
|
||||
if (x->ne[2] * x->ne[3] > 1024) {
|
||||
int64_t ne2 = x->ne[2];
|
||||
int64_t ne3 = x->ne[3];
|
||||
x = ggml_reshape_2d(ctx, x, x->ne[0], x->ne[1] * x->ne[2] * x->ne[3]);
|
||||
x = ggml_mul_mat_i8_tensorwise(ctx, w, x, weight_scale, fused_bias, convrot_group_size);
|
||||
x = ggml_reshape_4d(ctx, x, x->ne[0], x->ne[1] / ne2 / ne3, ne2, ne3);
|
||||
} else {
|
||||
x = ggml_mul_mat_i8_tensorwise(ctx, w, x, weight_scale, fused_bias, convrot_group_size);
|
||||
}
|
||||
|
||||
if (scale != 1.f) {
|
||||
x = ggml_ext_scale(ctx, x, 1.f / scale);
|
||||
if (b != nullptr) {
|
||||
x = ggml_add_inplace(ctx, x, b);
|
||||
}
|
||||
}
|
||||
return x;
|
||||
#else
|
||||
GGML_UNUSED(ctx);
|
||||
GGML_UNUSED(x);
|
||||
GGML_UNUSED(w);
|
||||
GGML_UNUSED(weight_scale);
|
||||
GGML_UNUSED(b);
|
||||
GGML_UNUSED(convrot_group_size);
|
||||
GGML_UNUSED(scale);
|
||||
throw std::runtime_error("INT8 tensorwise/convrot is not supported by this ggml build");
|
||||
#endif
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_pad_ext(ggml_context* ctx,
|
||||
ggml_backend_t backend,
|
||||
ggml_tensor* x,
|
||||
int lp0,
|
||||
int rp0,
|
||||
int lp1,
|
||||
int rp1,
|
||||
int lp2,
|
||||
int rp2,
|
||||
int lp3,
|
||||
int rp3,
|
||||
bool circular_x,
|
||||
bool circular_y) {
|
||||
if (circular_x && circular_y) {
|
||||
return ggml_pad_ext_circular(ctx, x, lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3);
|
||||
}
|
||||
|
||||
if (circular_x && (lp0 != 0 || rp0 != 0)) {
|
||||
x = ggml_pad_ext_circular(ctx, x, lp0, rp0, 0, 0, 0, 0, 0, 0);
|
||||
lp0 = rp0 = 0;
|
||||
}
|
||||
if (circular_y && (lp1 != 0 || rp1 != 0)) {
|
||||
x = ggml_pad_ext_circular(ctx, x, 0, 0, lp1, rp1, 0, 0, 0, 0);
|
||||
lp1 = rp1 = 0;
|
||||
}
|
||||
|
||||
if (lp0 != 0 || rp0 != 0 || lp1 != 0 || rp1 != 0 || lp2 != 0 || rp2 != 0 || lp3 != 0 || rp3 != 0) {
|
||||
ggml_tensor* padded = ggml_pad_ext(ctx, x, lp0, rp0, lp1, rp1, lp2, rp2, lp3, rp3);
|
||||
if (backend == nullptr || ggml_backend_supports_op(backend, padded)) {
|
||||
x = padded;
|
||||
} else {
|
||||
// Some backends (e.g. Metal) only implement right-padding for
|
||||
// GGML_OP_PAD (see #850): pad right by lp+rp instead, then roll
|
||||
// the padding around to the left. shift < ne always holds because
|
||||
// ne grew by lp+rp.
|
||||
x = ggml_pad_ext(ctx, x, 0, lp0 + rp0, 0, lp1 + rp1, 0, lp2 + rp2, 0, lp3 + rp3);
|
||||
x = ggml_roll(ctx, x, lp0, lp1, lp2, lp3);
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_pad(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
int p0,
|
||||
int p1,
|
||||
int p2,
|
||||
int p3,
|
||||
bool circular_x,
|
||||
bool circular_y) {
|
||||
return ggml_ext_pad_ext(ctx, nullptr, x, 0, p0, 0, p1, 0, p2, 0, p3, circular_x, circular_y);
|
||||
}
|
||||
|
||||
static ggml_tensor* conv_1d(ggml_context* ctx, ggml_tensor* x, ggml_tensor* w, int s0, int p0, int d0, bool force_prec_f32) {
|
||||
ggml_tensor* result;
|
||||
if (force_prec_f32) {
|
||||
ggml_tensor* patches = ggml_im2col(ctx, w, x, s0, 0, p0, 0, d0, 0, false, GGML_TYPE_F32);
|
||||
result = ggml_mul_mat(ctx,
|
||||
ggml_reshape_2d(ctx, patches, patches->ne[0], patches->ne[2] * patches->ne[1]),
|
||||
ggml_reshape_2d(ctx, w, w->ne[0] * w->ne[1], w->ne[2]));
|
||||
result = ggml_reshape_3d(ctx, result, patches->ne[1], w->ne[2], patches->ne[2]);
|
||||
} else {
|
||||
result = ggml_conv_1d(ctx, w, x, s0, p0, d0);
|
||||
}
|
||||
if (x->ne[2] > 1) {
|
||||
// mul_mat packs positions and batches before output channels: [OL, N, OC].
|
||||
result = ggml_reshape_3d(ctx, result, result->ne[0], x->ne[2], w->ne[2]);
|
||||
result = ggml_cont(ctx, ggml_permute(ctx, result, 0, 2, 1, 3));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_conv_1d(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int s0,
|
||||
int p0,
|
||||
int d0,
|
||||
int64_t groups,
|
||||
bool force_prec_f32) {
|
||||
GGML_ASSERT(s0 > 0 && p0 >= 0 && d0 > 0 && groups > 0);
|
||||
GGML_ASSERT(x->type == GGML_TYPE_F32 && x->ne[3] == 1 && w->ne[3] == 1);
|
||||
GGML_ASSERT(x->ne[1] % groups == 0 && w->ne[2] % groups == 0);
|
||||
GGML_ASSERT(w->ne[1] == x->ne[1] / groups);
|
||||
GGML_ASSERT(b == nullptr || (b->type == GGML_TYPE_F32 && ggml_is_vector(b) && b->ne[0] == w->ne[2]));
|
||||
|
||||
// im2col requires contiguous time rows; group views must retain the real channel and batch strides.
|
||||
if (!ggml_is_contiguous(x)) {
|
||||
x = ggml_cont(ctx, x);
|
||||
}
|
||||
if (force_prec_f32 && w->type != GGML_TYPE_F32) {
|
||||
w = ggml_cast(ctx, w, GGML_TYPE_F32);
|
||||
}
|
||||
if (!ggml_is_contiguous(w)) {
|
||||
w = ggml_cont(ctx, w);
|
||||
}
|
||||
|
||||
ggml_tensor* result = nullptr;
|
||||
if (groups == 1) {
|
||||
result = conv_1d(ctx, x, w, s0, p0, d0, force_prec_f32);
|
||||
} else {
|
||||
const int64_t ic_g = x->ne[1] / groups;
|
||||
const int64_t oc_g = w->ne[2] / groups;
|
||||
std::vector<ggml_tensor*> outputs;
|
||||
outputs.reserve(groups);
|
||||
for (int64_t group = 0; group < groups; ++group) {
|
||||
ggml_tensor* x_i = ggml_view_3d(ctx, x, x->ne[0], ic_g, x->ne[2], x->nb[1], x->nb[2], group * ic_g * x->nb[1]);
|
||||
ggml_tensor* w_i = ggml_view_3d(ctx, w, w->ne[0], ic_g, oc_g, w->nb[1], w->nb[2], group * oc_g * w->nb[2]);
|
||||
outputs.push_back(conv_1d(ctx, x_i, w_i, s0, p0, d0, force_prec_f32));
|
||||
}
|
||||
result = ggml_ext_vec_concat(ctx, outputs, 1);
|
||||
}
|
||||
if (b != nullptr) {
|
||||
if (!ggml_is_contiguous(b)) {
|
||||
b = ggml_cont(ctx, b);
|
||||
}
|
||||
b = ggml_reshape_3d(ctx, b, 1, w->ne[2], 1);
|
||||
result = ggml_add_inplace(ctx, result, b);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_conv_2d(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int s0,
|
||||
int s1,
|
||||
int p0,
|
||||
int p1,
|
||||
int d0,
|
||||
int d1,
|
||||
bool direct,
|
||||
bool circular_x,
|
||||
bool circular_y,
|
||||
float scale) {
|
||||
if (scale != 1.f) {
|
||||
x = ggml_ext_scale(ctx, x, scale);
|
||||
}
|
||||
if (w->ne[2] != x->ne[2] && ggml_n_dims(w) == 2) {
|
||||
w = ggml_reshape_4d(ctx, w, 1, 1, w->ne[0], w->ne[1]);
|
||||
}
|
||||
|
||||
if ((p0 != 0 || p1 != 0) && (circular_x || circular_y)) {
|
||||
x = ggml_ext_pad_ext(ctx, nullptr, x, p0, p0, p1, p1, 0, 0, 0, 0, circular_x, circular_y);
|
||||
p0 = 0;
|
||||
p1 = 0;
|
||||
}
|
||||
|
||||
if (direct) {
|
||||
x = ggml_conv_2d_direct(ctx, w, x, s0, s1, p0, p1, d0, d1);
|
||||
} else {
|
||||
x = ggml_conv_2d(ctx, w, x, s0, s1, p0, p1, d0, d1);
|
||||
}
|
||||
if (scale != 1.f) {
|
||||
x = ggml_ext_scale(ctx, x, 1.f / scale);
|
||||
}
|
||||
if (b != nullptr) {
|
||||
b = ggml_reshape_4d(ctx, b, 1, 1, b->ne[0], 1);
|
||||
x = ggml_add_inplace(ctx, x, b);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_conv_3d(ggml_context* ctx,
|
||||
ggml_backend_t backend,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int64_t IC,
|
||||
int s0,
|
||||
int s1,
|
||||
int s2,
|
||||
int p0,
|
||||
int p1,
|
||||
int p2,
|
||||
int d0,
|
||||
int d1,
|
||||
int d2,
|
||||
bool force_prec_f32,
|
||||
bool direct) {
|
||||
if (direct) {
|
||||
int64_t OC = w->ne[3] / IC;
|
||||
int64_t N = x->ne[3] / IC;
|
||||
x = ggml_conv_3d_direct(ctx, w, x, s0, s1, s2, p0, p1, p2, d0, d1, d2, (int)IC, (int)N, (int)OC);
|
||||
} else if (force_prec_f32) {
|
||||
ggml_tensor* im2col = ggml_im2col_3d(ctx, w, x, IC, s0, s1, s2, p0, p1, p2, d0, d1, d2, w->type);
|
||||
|
||||
int64_t OC = w->ne[3] / IC;
|
||||
int64_t N = x->ne[3] / IC;
|
||||
x = ggml_mul_mat(ctx,
|
||||
ggml_reshape_2d(ctx, im2col, im2col->ne[0], im2col->ne[3] * im2col->ne[2] * im2col->ne[1]),
|
||||
ggml_reshape_2d(ctx, w, w->ne[0] * w->ne[1] * w->ne[2] * IC, OC));
|
||||
ggml_mul_mat_set_prec(x, GGML_PREC_F32);
|
||||
|
||||
int64_t OD = im2col->ne[3] / N;
|
||||
x = ggml_reshape_4d(ctx, x, im2col->ne[1] * im2col->ne[2], OD, N, OC);
|
||||
x = ggml_cont(ctx, ggml_permute(ctx, x, 0, 1, 3, 2));
|
||||
x = ggml_reshape_4d(ctx, x, im2col->ne[1], im2col->ne[2], OD, OC * N);
|
||||
} else {
|
||||
// ggml_conv_3d decomposes into GGML_OP_IM2COL_3D, which some backends
|
||||
// (e.g. Metal, see #850) do not implement. Fall back to
|
||||
// GGML_OP_CONV_3D on those backends.
|
||||
bool im2col_3d_supported = true;
|
||||
if (backend != nullptr) {
|
||||
ggml_tensor* im2col = ggml_im2col_3d(ctx, w, x, IC, s0, s1, s2, p0, p1, p2, d0, d1, d2, w->type);
|
||||
im2col_3d_supported = ggml_backend_supports_op(backend, im2col);
|
||||
}
|
||||
if (im2col_3d_supported) {
|
||||
x = ggml_conv_3d(ctx, w, x, IC, s0, s1, s2, p0, p1, p2, d0, d1, d2);
|
||||
} else {
|
||||
int64_t OC = w->ne[3] / IC;
|
||||
int64_t N = x->ne[3] / IC;
|
||||
x = ggml_conv_3d_direct(ctx, w, x, s0, s1, s2, p0, p1, p2, d0, d1, d2, (int)IC, (int)N, (int)OC);
|
||||
}
|
||||
}
|
||||
|
||||
if (b != nullptr) {
|
||||
b = ggml_reshape_4d(ctx, b, 1, 1, 1, b->ne[0]); // [OC, 1, 1, 1]
|
||||
x = ggml_add_inplace(ctx, x, b);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_conv_3d_nx1x1(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int s2,
|
||||
int p2,
|
||||
int d2) {
|
||||
x = ggml_conv_2d(ctx, w, x, 1, s2, 0, p2, 1, d2); // [N, OC, T, OH * OW]
|
||||
if (b != nullptr) {
|
||||
b = ggml_reshape_4d(ctx, b, 1, 1, b->ne[0], 1);
|
||||
x = ggml_add(ctx, x, b);
|
||||
}
|
||||
return x; // [N, OC, T, OH * OW]
|
||||
}
|
||||
|
||||
std::vector<ggml_tensor*> split_qkv(ggml_context* ctx,
|
||||
ggml_tensor* qkv) {
|
||||
qkv = ggml_reshape_4d(ctx, qkv, qkv->ne[0] / 3, 3, qkv->ne[1], qkv->ne[2]); // [N, L, 3, C]
|
||||
qkv = ggml_cont(ctx, ggml_permute(ctx, qkv, 0, 3, 1, 2)); // [3, N, L, C]
|
||||
|
||||
int64_t offset = qkv->nb[2] * qkv->ne[2];
|
||||
auto q = ggml_view_3d(ctx, qkv, qkv->ne[0], qkv->ne[1], qkv->ne[2], qkv->nb[1], qkv->nb[2], offset * 0); // [N, L, C]
|
||||
auto k = ggml_view_3d(ctx, qkv, qkv->ne[0], qkv->ne[1], qkv->ne[2], qkv->nb[1], qkv->nb[2], offset * 1); // [N, L, C]
|
||||
auto v = ggml_view_3d(ctx, qkv, qkv->ne[0], qkv->ne[1], qkv->ne[2], qkv->nb[1], qkv->nb[2], offset * 2); // [N, L, C]
|
||||
return {q, k, v};
|
||||
}
|
||||
|
||||
std::vector<ggml_tensor*> split_image_qkv(ggml_context* ctx,
|
||||
ggml_tensor* qkv) {
|
||||
int64_t W = qkv->ne[0];
|
||||
int64_t H = qkv->ne[1];
|
||||
int64_t C = qkv->ne[2] / 3;
|
||||
int64_t N = qkv->ne[3];
|
||||
int64_t nb1 = qkv->nb[1];
|
||||
int64_t nb2 = qkv->nb[2];
|
||||
qkv = ggml_reshape_4d(ctx, qkv, W * H, C, 3, N); // [N, 3, C, H*W]
|
||||
qkv = ggml_cont(ctx, ggml_ext_torch_permute(ctx, qkv, 0, 1, 3, 2)); // [3, N, C, H*W]
|
||||
|
||||
int64_t offset = qkv->nb[2] * qkv->ne[2];
|
||||
auto q = ggml_view_4d(ctx, qkv, W, H, C, N, nb1, nb2, qkv->nb[3], offset * 0); // [N, C, H, W]
|
||||
auto k = ggml_view_4d(ctx, qkv, W, H, C, N, nb1, nb2, qkv->nb[3], offset * 1); // [N, C, H, W]
|
||||
auto v = ggml_view_4d(ctx, qkv, W, H, C, N, nb1, nb2, qkv->nb[3], offset * 2); // [N, C, H, W]
|
||||
return {q, k, v};
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_full(ggml_context* ctx,
|
||||
float value,
|
||||
int64_t ne0,
|
||||
int64_t ne1,
|
||||
int64_t ne2,
|
||||
int64_t ne3) {
|
||||
auto one = ggml_get_tensor(ctx, "ggml_runner_build_in_tensor:one");
|
||||
auto t = ggml_ext_scale(ctx, one, value); // [1,]
|
||||
t = ggml_repeat_4d(ctx, t, ne0, ne1, ne2, ne3); // [ne0, ne1, ne2, ne3]
|
||||
return t;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_zeros(ggml_context* ctx,
|
||||
int64_t ne0,
|
||||
int64_t ne1,
|
||||
int64_t ne2,
|
||||
int64_t ne3) {
|
||||
return ggml_ext_full(ctx, 0.f, ne0, ne1, ne2, ne3);
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_zeros_like(ggml_context* ctx,
|
||||
ggml_tensor* x) {
|
||||
return ggml_ext_zeros(ctx, x->ne[0], x->ne[1], x->ne[2], x->ne[3]);
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_ones(ggml_context* ctx,
|
||||
int64_t ne0,
|
||||
int64_t ne1,
|
||||
int64_t ne2,
|
||||
int64_t ne3) {
|
||||
return ggml_ext_full(ctx, 1.f, ne0, ne1, ne2, ne3);
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_ones_like(ggml_context* ctx,
|
||||
ggml_tensor* x) {
|
||||
return ggml_ext_ones(ctx, x->ne[0], x->ne[1], x->ne[2], x->ne[3]);
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_cast_f32(ggml_context* ctx, ggml_backend_t backend, ggml_tensor* a) {
|
||||
if (sd_backend_is(backend, "Vulkan")) {
|
||||
auto zero_index = ggml_get_tensor(ctx, "ggml_runner_build_in_tensor:zero_int");
|
||||
auto out = ggml_reshape_1d(ctx, a, ggml_nelements(a));
|
||||
out = ggml_get_rows(ctx, out, zero_index);
|
||||
out = ggml_reshape(ctx, out, a);
|
||||
// auto out = ggml_cast(ctx, a, GGML_TYPE_F32);
|
||||
return out;
|
||||
} else {
|
||||
auto out = ggml_reshape_2d(ctx, a, 1, ggml_nelements(a));
|
||||
ggml_tensor* one = ggml_ext_ones(ctx, 1, 1, 1, 1); // [1,]
|
||||
if (ggml_is_transposed(out)) {
|
||||
out = ggml_mul_mat(ctx, one, out);
|
||||
} else {
|
||||
out = ggml_mul_mat(ctx, out, one);
|
||||
}
|
||||
out = ggml_reshape(ctx, out, a);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_attention_ext(ggml_context* ctx,
|
||||
ggml_backend_t backend,
|
||||
ggml_tensor* q,
|
||||
ggml_tensor* k,
|
||||
ggml_tensor* v,
|
||||
int64_t n_head,
|
||||
ggml_tensor* mask,
|
||||
bool skip_reshape,
|
||||
bool flash_attn,
|
||||
float kv_scale,
|
||||
bool sage_attn,
|
||||
bool sol_attn,
|
||||
float sol_attn_tau) { // avoid overflow
|
||||
int64_t L_q;
|
||||
int64_t L_k;
|
||||
int64_t C;
|
||||
int64_t N;
|
||||
int64_t d_head;
|
||||
int64_t n_kv_head;
|
||||
if (!skip_reshape) {
|
||||
L_q = q->ne[1];
|
||||
L_k = k->ne[1];
|
||||
C = q->ne[0];
|
||||
N = q->ne[2];
|
||||
d_head = C / n_head;
|
||||
n_kv_head = k->ne[0] / d_head;
|
||||
|
||||
q = ggml_reshape_4d(ctx, q, d_head, n_head, L_q, N); // [N, L_q, n_head, d_head]
|
||||
q = ggml_ext_cont(ctx, ggml_permute(ctx, q, 0, 2, 1, 3)); // [N, n_head, L_q, d_head]
|
||||
q = ggml_reshape_3d(ctx, q, d_head, L_q, n_head * N); // [N * n_head, L_q, d_head]
|
||||
|
||||
k = ggml_reshape_4d(ctx, k, d_head, n_kv_head, L_k, N); // [N, L_k, n_kv_head, d_head]
|
||||
k = ggml_ext_cont(ctx, ggml_permute(ctx, k, 0, 2, 1, 3)); // [N, n_kv_head, L_k, d_head]
|
||||
k = ggml_reshape_3d(ctx, k, d_head, L_k, n_kv_head * N); // [N * n_kv_head, L_k, d_head]
|
||||
|
||||
v = ggml_reshape_4d(ctx, v, d_head, n_kv_head, L_k, N); // [N, L_k, n_kv_head, d_head]
|
||||
} else {
|
||||
L_q = q->ne[1];
|
||||
L_k = k->ne[1];
|
||||
d_head = v->ne[0];
|
||||
N = v->ne[3];
|
||||
n_kv_head = k->ne[2] / N;
|
||||
C = d_head * n_head;
|
||||
}
|
||||
|
||||
float scale = (1.0f / sqrt((float)d_head));
|
||||
|
||||
ggml_tensor* kqv = nullptr;
|
||||
|
||||
auto build_kqv = [&](ggml_tensor* q_in, ggml_tensor* k_in, ggml_tensor* v_in, ggml_tensor* mask_in) -> ggml_tensor* {
|
||||
const bool pad_head = d_head > 0 && d_head < 64 && q_in->ne[0] == d_head && k_in->ne[0] == d_head &&
|
||||
q_in->type == GGML_TYPE_F32 && k_in->type == GGML_TYPE_F32 &&
|
||||
v_in->type == GGML_TYPE_F32 && sd_backend_supports_cuda_mma(backend);
|
||||
if (pad_head) {
|
||||
// CUDA FA MMA starts at 64 channels; keep the original head's attention scale.
|
||||
q_in = ggml_pad(ctx, q_in, 64 - d_head, 0, 0, 0);
|
||||
k_in = ggml_pad(ctx, k_in, 64 - d_head, 0, 0, 0);
|
||||
}
|
||||
if (kv_scale != 1.0f) {
|
||||
k_in = ggml_ext_scale(ctx, k_in, kv_scale);
|
||||
}
|
||||
k_in = ggml_cast(ctx, k_in, GGML_TYPE_F16);
|
||||
|
||||
v_in = ggml_ext_cont(ctx, ggml_permute(ctx, v_in, 0, 2, 1, 3));
|
||||
v_in = ggml_reshape_3d(ctx, v_in, d_head, L_k, n_kv_head * N);
|
||||
if (pad_head) {
|
||||
v_in = ggml_pad(ctx, v_in, 64 - d_head, 0, 0, 0);
|
||||
}
|
||||
if (kv_scale != 1.0f) {
|
||||
v_in = ggml_ext_scale(ctx, v_in, kv_scale);
|
||||
}
|
||||
v_in = ggml_cast(ctx, v_in, GGML_TYPE_F16);
|
||||
|
||||
if (mask_in != nullptr) {
|
||||
// ggml_flash_attn_ext expects the mask as a contiguous F16 tensor shaped
|
||||
// [n_kv, n_q, (heads), (batch)] (ne0 = key length, ne1 = query length) and,
|
||||
// unlike the manual-attention path, does not broadcast the query dimension.
|
||||
// Some callers (e.g. Chroma/T5) pass a per-key padding mask broadcast over
|
||||
// queries ([n_kv, 1, ...]); materialize the query dimension to L_q so the
|
||||
// kernel indexes it correctly. (A bare ggml_transpose here produced a
|
||||
// [1, n_kv, ...] mask that the kernel silently misreads, yielding NaN/blank
|
||||
// output for masked flash attention.)
|
||||
if (mask_in->ne[1] != L_q) {
|
||||
mask_in = ggml_repeat(ctx, mask_in,
|
||||
ggml_new_tensor_4d(ctx, mask_in->type, mask_in->ne[0], L_q, mask_in->ne[2], mask_in->ne[3]));
|
||||
}
|
||||
mask_in = ggml_cast(ctx, mask_in, GGML_TYPE_F16);
|
||||
}
|
||||
|
||||
auto out = ggml_flash_attn_ext(ctx, q_in, k_in, v_in, mask_in, scale / kv_scale, 0, 0);
|
||||
if (!ggml_backend_supports_op(backend, out)) {
|
||||
return nullptr;
|
||||
}
|
||||
ggml_flash_attn_ext_set_prec(out, GGML_PREC_F32);
|
||||
if (kv_scale != 1.0f) {
|
||||
out = ggml_ext_scale(ctx, out, 1.0f / kv_scale);
|
||||
}
|
||||
if (pad_head) {
|
||||
out = ggml_ext_slice(ctx, out, 0, 0, d_head);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
#ifndef SD_USE_UPSTREAM_GGML
|
||||
if (sol_attn && mask == nullptr && d_head == 128 && L_q == L_k && n_head == n_kv_head) {
|
||||
auto q_in = ggml_reshape_4d(ctx, ggml_ext_cont(ctx, q->type == GGML_TYPE_F32 ? q : ggml_cast(ctx, q, GGML_TYPE_F32)), d_head, L_q, n_head, N);
|
||||
auto k_in = ggml_reshape_4d(ctx, ggml_ext_cont(ctx, k->type == GGML_TYPE_F32 ? k : ggml_cast(ctx, k, GGML_TYPE_F32)), d_head, L_k, n_kv_head, N);
|
||||
auto v_in = ggml_ext_cont(ctx, ggml_permute(ctx, v, 0, 2, 1, 3));
|
||||
if (v_in->type != GGML_TYPE_F32) {
|
||||
v_in = ggml_cast(ctx, v_in, GGML_TYPE_F32);
|
||||
}
|
||||
if (kv_scale != 1.0f) {
|
||||
k_in = ggml_ext_scale(ctx, k_in, kv_scale);
|
||||
v_in = ggml_ext_scale(ctx, v_in, kv_scale);
|
||||
}
|
||||
auto out = ggml_sol_attn(ctx, q_in, k_in, v_in, scale / kv_scale, sol_attn_tau);
|
||||
if (ggml_backend_supports_op(backend, out)) {
|
||||
kqv = kv_scale != 1.0f ? ggml_ext_scale(ctx, out, 1.0f / kv_scale) : out;
|
||||
}
|
||||
}
|
||||
if (kqv == nullptr && sage_attn && mask == nullptr && d_head > 0 && d_head <= 128) {
|
||||
auto q_in = ggml_reshape_4d(ctx, ggml_ext_cont(ctx, q->type == GGML_TYPE_F32 ? q : ggml_cast(ctx, q, GGML_TYPE_F32)), d_head, L_q, n_head, N);
|
||||
auto k_in = ggml_reshape_4d(ctx, ggml_ext_cont(ctx, k->type == GGML_TYPE_F32 ? k : ggml_cast(ctx, k, GGML_TYPE_F32)), d_head, L_k, n_kv_head, N);
|
||||
auto v_in = ggml_ext_cont(ctx, ggml_permute(ctx, v, 0, 2, 1, 3));
|
||||
const int64_t padded_head = d_head <= 64 ? 64 : 128;
|
||||
if ((padded_head != d_head || kv_scale != 1.0f) && v_in->type != GGML_TYPE_F32) {
|
||||
v_in = ggml_cast(ctx, v_in, GGML_TYPE_F32);
|
||||
}
|
||||
if (padded_head != d_head) {
|
||||
// Keep the original head's softmax scale when padding for the CUDA kernel.
|
||||
q_in = ggml_pad(ctx, q_in, padded_head - d_head, 0, 0, 0);
|
||||
k_in = ggml_pad(ctx, k_in, padded_head - d_head, 0, 0, 0);
|
||||
v_in = ggml_pad(ctx, v_in, padded_head - d_head, 0, 0, 0);
|
||||
}
|
||||
if (kv_scale != 1.0f) {
|
||||
k_in = ggml_ext_scale(ctx, k_in, kv_scale);
|
||||
v_in = ggml_ext_scale(ctx, v_in, kv_scale);
|
||||
}
|
||||
v_in = ggml_cast(ctx, v_in, GGML_TYPE_F16);
|
||||
auto out = ggml_sage_attn(ctx, q_in, k_in, v_in, scale / kv_scale, GGML_SAGE_ATTN_AUTO);
|
||||
if (ggml_backend_supports_op(backend, out)) {
|
||||
kqv = kv_scale != 1.0f ? ggml_ext_scale(ctx, out, 1.0f / kv_scale) : out;
|
||||
if (padded_head != d_head) {
|
||||
kqv = ggml_ext_slice(ctx, kqv, 0, 0, d_head);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (kqv == nullptr && (flash_attn || sage_attn || sol_attn)) {
|
||||
// LOG_VERBOSE("attention_ext L_q:%d L_k:%d n_head:%d C:%d d_head:%d N:%d", L_q, L_k, n_head, C, d_head, N);
|
||||
bool can_use_flash_attn = true;
|
||||
if (mask != nullptr) {
|
||||
// TODO: figure out if we can bend t5 to work too
|
||||
can_use_flash_attn = can_use_flash_attn && mask->ne[3] == 1;
|
||||
}
|
||||
|
||||
if (can_use_flash_attn) {
|
||||
kqv = build_kqv(q, k, v, mask);
|
||||
if (kqv != nullptr) {
|
||||
kqv = ggml_view_4d(ctx,
|
||||
kqv,
|
||||
d_head,
|
||||
n_head,
|
||||
L_q,
|
||||
N,
|
||||
kqv->nb[1],
|
||||
kqv->nb[2],
|
||||
kqv->nb[1] * n_head,
|
||||
0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (kqv == nullptr) {
|
||||
// if (flash_attn) {
|
||||
// LOG_VERBOSE("fallback to default attention, L_q:%d L_k:%d n_head:%d C:%d d_head:%d N:%d", L_q, L_k, n_head, C, d_head, N);
|
||||
// }
|
||||
v = ggml_ext_cont(ctx, ggml_permute(ctx, v, 1, 2, 0, 3)); // [N, n_kv_head, d_head, L_k]
|
||||
v = ggml_reshape_3d(ctx, v, L_k, d_head, n_kv_head * N); // [N * n_kv_head, d_head, L_k]
|
||||
|
||||
auto kq = ggml_mul_mat(ctx, k, q); // [N * n_head, L_q, L_k]
|
||||
ggml_mul_mat_set_prec(kq, GGML_PREC_F32);
|
||||
kq = ggml_scale_inplace(ctx, kq, scale);
|
||||
if (mask) {
|
||||
kq = ggml_add_inplace(ctx, kq, mask);
|
||||
}
|
||||
kq = ggml_soft_max_inplace(ctx, kq);
|
||||
|
||||
kqv = ggml_mul_mat(ctx, v, kq); // [N * n_head, L_q, d_head]
|
||||
|
||||
kqv = ggml_reshape_4d(ctx, kqv, d_head, L_q, n_head, N); // [N, n_head, L_q, d_head]
|
||||
kqv = ggml_permute(ctx, kqv, 0, 2, 1, 3); // [N, L_q, n_head, d_head]
|
||||
}
|
||||
|
||||
kqv = ggml_ext_cont(ctx, kqv);
|
||||
kqv = ggml_reshape_3d(ctx, kqv, d_head * n_head, L_q, N); // [N, L_q, C]
|
||||
|
||||
return kqv;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_layer_norm(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
float eps) {
|
||||
x = ggml_norm(ctx, x, eps);
|
||||
if (w != nullptr) {
|
||||
x = ggml_mul_inplace(ctx, x, w);
|
||||
if (b != nullptr) {
|
||||
x = ggml_add_inplace(ctx, x, b);
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_group_norm(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int num_groups,
|
||||
float eps) {
|
||||
if (ggml_n_dims(x) >= 3 && w != nullptr && b != nullptr) {
|
||||
w = ggml_reshape_4d(ctx, w, 1, 1, w->ne[0], 1);
|
||||
b = ggml_reshape_4d(ctx, b, 1, 1, b->ne[0], 1);
|
||||
}
|
||||
|
||||
x = ggml_group_norm(ctx, x, num_groups, eps);
|
||||
if (w != nullptr && b != nullptr) {
|
||||
x = ggml_mul_inplace(ctx, x, w);
|
||||
x = ggml_add_inplace(ctx, x, b);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_timestep_embedding(
|
||||
ggml_context* ctx,
|
||||
ggml_tensor* timesteps,
|
||||
int dim,
|
||||
int max_period,
|
||||
float time_factor) {
|
||||
timesteps = ggml_ext_scale(ctx, timesteps, time_factor);
|
||||
return ggml_timestep_embedding(ctx, timesteps, dim, max_period);
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_vec_concat(ggml_context* ctx,
|
||||
std::vector<ggml_tensor*>& tensors,
|
||||
int dim) {
|
||||
while (tensors.size() > 1) {
|
||||
std::vector<ggml_tensor*> next_level;
|
||||
for (size_t i = 0; i < tensors.size(); i += 2) {
|
||||
if (i + 1 < tensors.size()) {
|
||||
next_level.push_back(ggml_concat(ctx, tensors[i], tensors[i + 1], dim));
|
||||
} else {
|
||||
next_level.push_back(tensors[i]);
|
||||
}
|
||||
}
|
||||
tensors = std::move(next_level);
|
||||
}
|
||||
return tensors[0];
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
#ifndef __SD_CORE_GGML_EXTEND_H__
|
||||
#define __SD_CORE_GGML_EXTEND_H__
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "ggml-backend.h"
|
||||
#include "ggml.h"
|
||||
|
||||
#define EPS 1e-05f
|
||||
|
||||
static_assert(GGML_MAX_NAME >= 160, "GGML_MAX_NAME must be at least 160");
|
||||
|
||||
// n-mode tensor-matrix product
|
||||
// example: 2-mode product
|
||||
// A: [ne03, k, ne01, ne00]
|
||||
// B: k rows, m columns => [k, m]
|
||||
// result is [ne03, m, ne01, ne00]
|
||||
ggml_tensor* ggml_ext_mul_n_mode(ggml_context* ctx, ggml_tensor* a, ggml_tensor* b, int mode = 0);
|
||||
|
||||
// Kronecker product
|
||||
// [ne03,ne02,ne01,ne00] x [ne13,ne12,ne11,ne10] => [ne03*ne13,ne02*ne12,ne01*ne11,ne00*ne10]
|
||||
ggml_tensor* ggml_ext_kronecker(ggml_context* ctx, ggml_tensor* a, ggml_tensor* b);
|
||||
|
||||
ggml_tensor* ggml_ext_cont(ggml_context* ctx,
|
||||
ggml_tensor* x);
|
||||
|
||||
// torch like permute
|
||||
ggml_tensor* ggml_ext_torch_permute(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
int axis0,
|
||||
int axis1,
|
||||
int axis2,
|
||||
int axis3);
|
||||
|
||||
ggml_tensor* ggml_ext_slice(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
int dim,
|
||||
int64_t start,
|
||||
int64_t end,
|
||||
bool cont = true);
|
||||
|
||||
// example: [N, 3*C, H, W] => ([N, C, H, W], [N, C, H, W], [N, C, H, W])
|
||||
std::vector<ggml_tensor*> ggml_ext_chunk(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
int num,
|
||||
int64_t dim,
|
||||
bool cont = true);
|
||||
|
||||
ggml_tensor* ggml_ext_silu_act(ggml_context* ctx, ggml_tensor* x, bool gate_first = true);
|
||||
|
||||
ggml_tensor* ggml_ext_group_norm_32(ggml_context* ctx,
|
||||
ggml_tensor* a);
|
||||
|
||||
ggml_tensor* ggml_ext_scale(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
float factor,
|
||||
bool inplace = false);
|
||||
|
||||
ggml_tensor* ggml_ext_gelu(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
bool inplace = false);
|
||||
|
||||
ggml_tensor* ggml_ext_gelu_quick(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
bool inplace = false);
|
||||
|
||||
ggml_tensor* ggml_ext_linear(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
bool force_prec_f32 = false,
|
||||
float scale = 1.f);
|
||||
|
||||
ggml_tensor* ggml_ext_linear_i8_tensorwise(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* weight_scale,
|
||||
ggml_tensor* b,
|
||||
int convrot_group_size,
|
||||
float scale = 1.f);
|
||||
|
||||
ggml_tensor* ggml_ext_pad_ext(ggml_context* ctx,
|
||||
ggml_backend_t backend,
|
||||
ggml_tensor* x,
|
||||
int lp0,
|
||||
int rp0,
|
||||
int lp1,
|
||||
int rp1,
|
||||
int lp2,
|
||||
int rp2,
|
||||
int lp3,
|
||||
int rp3,
|
||||
bool circular_x = false,
|
||||
bool circular_y = false);
|
||||
|
||||
ggml_tensor* ggml_ext_pad(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
int p0,
|
||||
int p1,
|
||||
int p2 = 0,
|
||||
int p3 = 0,
|
||||
bool circular_x = false,
|
||||
bool circular_y = false);
|
||||
|
||||
// ggml layout: x [L, IC, N], w [K, IC/groups, OC], b [OC], result [OL, OC, N].
|
||||
// force_prec_f32 keeps both input patches and weights in F32.
|
||||
ggml_tensor* ggml_ext_conv_1d(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int s0 = 1,
|
||||
int p0 = 0,
|
||||
int d0 = 1,
|
||||
int64_t groups = 1,
|
||||
bool force_prec_f32 = false);
|
||||
|
||||
// w: [OC,IC, KH, KW]
|
||||
// x: [N, IC, IH, IW]
|
||||
// b: [OC,]
|
||||
// result: [N, OC, OH, OW]
|
||||
ggml_tensor* ggml_ext_conv_2d(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int s0 = 1,
|
||||
int s1 = 1,
|
||||
int p0 = 0,
|
||||
int p1 = 0,
|
||||
int d0 = 1,
|
||||
int d1 = 1,
|
||||
bool direct = false,
|
||||
bool circular_x = false,
|
||||
bool circular_y = false,
|
||||
float scale = 1.f);
|
||||
|
||||
// w: [OC,IC, KD, 1 * 1]
|
||||
// x: [N, IC, IH, IW]
|
||||
// b: [OC,]
|
||||
// result: [N*OC, OD, OH, OW]
|
||||
ggml_tensor* ggml_ext_conv_3d(ggml_context* ctx,
|
||||
ggml_backend_t backend,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int64_t IC,
|
||||
int s0 = 1,
|
||||
int s1 = 1,
|
||||
int s2 = 1,
|
||||
int p0 = 0,
|
||||
int p1 = 0,
|
||||
int p2 = 0,
|
||||
int d0 = 1,
|
||||
int d1 = 1,
|
||||
int d2 = 1,
|
||||
bool force_prec_f32 = false,
|
||||
bool direct = false);
|
||||
|
||||
// w: [OC,IC, KD, 1 * 1]
|
||||
// x: [N, IC, ID, IH*IW]
|
||||
// b: [OC,]
|
||||
// result: [N, OC, OD, OH*OW]
|
||||
ggml_tensor* ggml_ext_conv_3d_nx1x1(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int s2 = 1,
|
||||
int p2 = 1,
|
||||
int d2 = 1);
|
||||
|
||||
// qkv: [N, L, 3*C]
|
||||
// return: ([N, L, C], [N, L, C], [N, L, C])
|
||||
std::vector<ggml_tensor*> split_qkv(ggml_context* ctx,
|
||||
ggml_tensor* qkv);
|
||||
|
||||
// qkv: [N, 3*C, H, W]
|
||||
// return: ([N, C, H, W], [N, C, H, W], [N, C, H, W])
|
||||
std::vector<ggml_tensor*> split_image_qkv(ggml_context* ctx,
|
||||
ggml_tensor* qkv);
|
||||
|
||||
// Constant and cast helpers require the built-in tensors initialized by GGMLRunner.
|
||||
ggml_tensor* ggml_ext_full(ggml_context* ctx,
|
||||
float value,
|
||||
int64_t ne0,
|
||||
int64_t ne1,
|
||||
int64_t ne2,
|
||||
int64_t ne3);
|
||||
|
||||
ggml_tensor* ggml_ext_zeros(ggml_context* ctx,
|
||||
int64_t ne0,
|
||||
int64_t ne1,
|
||||
int64_t ne2,
|
||||
int64_t ne3);
|
||||
|
||||
ggml_tensor* ggml_ext_zeros_like(ggml_context* ctx,
|
||||
ggml_tensor* x);
|
||||
|
||||
ggml_tensor* ggml_ext_ones(ggml_context* ctx,
|
||||
int64_t ne0,
|
||||
int64_t ne1,
|
||||
int64_t ne2,
|
||||
int64_t ne3);
|
||||
|
||||
ggml_tensor* ggml_ext_ones_like(ggml_context* ctx,
|
||||
ggml_tensor* x);
|
||||
|
||||
ggml_tensor* ggml_ext_cast_f32(ggml_context* ctx, ggml_backend_t backend, ggml_tensor* a);
|
||||
|
||||
// q: [N, L_q, C(n_head*d_head)] or [N*n_head, L_q, d_head]
|
||||
// k: [N, L_k, n_kv_head*d_head] or [N*n_kv_head, L_k, d_head]
|
||||
// v: [N, L_k, n_kv_head*d_head] or [N, L_k, n_kv_head, d_head]
|
||||
// mask: [N, L_q, L_k]
|
||||
// return: [N, L_q, C]
|
||||
ggml_tensor* ggml_ext_attention_ext(ggml_context* ctx,
|
||||
ggml_backend_t backend,
|
||||
ggml_tensor* q,
|
||||
ggml_tensor* k,
|
||||
ggml_tensor* v,
|
||||
int64_t n_head,
|
||||
ggml_tensor* mask = nullptr,
|
||||
bool skip_reshape = false,
|
||||
bool flash_attn = false,
|
||||
float kv_scale = 1.0f,
|
||||
bool sage_attn = false,
|
||||
bool sol_attn = false,
|
||||
float sol_attn_tau = 1.0f);
|
||||
|
||||
ggml_tensor* ggml_ext_layer_norm(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
float eps = EPS);
|
||||
|
||||
ggml_tensor* ggml_ext_group_norm(ggml_context* ctx,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
int num_groups = 32,
|
||||
float eps = 1e-6f);
|
||||
|
||||
ggml_tensor* ggml_ext_timestep_embedding(
|
||||
ggml_context* ctx,
|
||||
ggml_tensor* timesteps,
|
||||
int dim,
|
||||
int max_period = 10000,
|
||||
float time_factor = 1.0f);
|
||||
|
||||
ggml_tensor* ggml_ext_vec_concat(ggml_context* ctx,
|
||||
std::vector<ggml_tensor*>& tensors,
|
||||
int dim);
|
||||
|
||||
#endif // __SD_CORE_GGML_EXTEND_H__
|
||||
File diff suppressed because it is too large
Load Diff
@@ -8,8 +8,12 @@
|
||||
#include <stdexcept>
|
||||
#include <vector>
|
||||
|
||||
#ifdef SD_USE_CUDA
|
||||
#include <cuda.h>
|
||||
#endif
|
||||
|
||||
#include "core/util.h"
|
||||
#include "ggml/src/ggml-impl.h"
|
||||
#include "ggml-impl.h"
|
||||
#include "stable-diffusion.h"
|
||||
|
||||
static std::string trim_copy(const std::string& value) {
|
||||
@@ -87,6 +91,10 @@ static bool parse_backend_module(const std::string& raw_name, SDBackendModule* m
|
||||
*module = SDBackendModule::DETECTOR;
|
||||
return true;
|
||||
}
|
||||
if (name == "audioencoder" || name == "audio") {
|
||||
*module = SDBackendModule::AUDIO_ENCODER;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -392,7 +400,7 @@ static bool backend_name_exists(const std::string& name) {
|
||||
|
||||
static ggml_backend_t init_named_backend(const std::string& name) {
|
||||
ggml_backend_load_all_once();
|
||||
LOG_DEBUG("Initializing backend: %s", name.c_str());
|
||||
LOG_VERBOSE("Initializing backend: %s", name.c_str());
|
||||
if (trim_copy(name).empty()) {
|
||||
return ggml_backend_init_best();
|
||||
}
|
||||
@@ -425,6 +433,70 @@ bool sd_backend_is_cpu(ggml_backend_t backend) {
|
||||
return dev != nullptr && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU;
|
||||
}
|
||||
|
||||
bool sd_backend_supports_cuda_mma(ggml_backend_t backend) {
|
||||
#ifdef SD_USE_CUDA
|
||||
if (!sd_backend_is(backend, "CUDA")) {
|
||||
return false;
|
||||
}
|
||||
auto dev = ggml_backend_get_device(backend);
|
||||
if (dev == nullptr) {
|
||||
return false;
|
||||
}
|
||||
static std::mutex mutex;
|
||||
static std::unordered_map<ggml_backend_dev_t, bool> cache;
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
auto it = cache.find(dev);
|
||||
if (it != cache.end()) {
|
||||
return it->second;
|
||||
}
|
||||
const bool supported = [&]() {
|
||||
ggml_backend_dev_props props{};
|
||||
ggml_backend_dev_get_props(dev, &props);
|
||||
CUdevice device;
|
||||
int major = 0, minor = 0;
|
||||
if (props.device_id == nullptr || cuInit(0) != CUDA_SUCCESS ||
|
||||
cuDeviceGetByPCIBusId(&device, props.device_id) != CUDA_SUCCESS ||
|
||||
cuDeviceGetAttribute(&major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device) != CUDA_SUCCESS ||
|
||||
cuDeviceGetAttribute(&minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device) != CUDA_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
auto reg = ggml_backend_dev_backend_reg(dev);
|
||||
auto get_features = reinterpret_cast<ggml_backend_get_features_t>(
|
||||
ggml_backend_reg_get_proc_address(reg, "ggml_backend_get_features"));
|
||||
if (get_features == nullptr) {
|
||||
return false;
|
||||
}
|
||||
// Match ggml's highest compiled architecture for this device, including PTX fallback.
|
||||
const int cc = 100 * major + 10 * minor;
|
||||
int compiled_arch = 0;
|
||||
for (auto feature = get_features(reg); feature != nullptr && feature->name != nullptr; ++feature) {
|
||||
if (std::strcmp(feature->name, "ARCHS") != 0 || feature->value == nullptr) {
|
||||
continue;
|
||||
}
|
||||
const char* arch = feature->value;
|
||||
while (*arch != '\0') {
|
||||
char* end = nullptr;
|
||||
const long value = std::strtol(arch, &end, 10);
|
||||
if (end == arch) {
|
||||
++arch;
|
||||
continue;
|
||||
}
|
||||
if (value <= cc && value > compiled_arch) {
|
||||
compiled_arch = static_cast<int>(value);
|
||||
}
|
||||
arch = end;
|
||||
}
|
||||
}
|
||||
return compiled_arch == 700 || compiled_arch >= 750;
|
||||
}();
|
||||
cache.emplace(dev, supported);
|
||||
return supported;
|
||||
#else
|
||||
(void)backend;
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
ggml_backend_t sd_backend_cpu_init() {
|
||||
ggml_backend_load_all_once();
|
||||
return ggml_backend_init_by_type(GGML_BACKEND_DEVICE_TYPE_CPU, nullptr);
|
||||
@@ -542,10 +614,10 @@ static ggml_backend_t sd_get_default_backend() {
|
||||
if (dev_count == 0) {
|
||||
LOG_ERROR("No devices found!");
|
||||
} else {
|
||||
LOG_DEBUG("Found %zu backend devices:", dev_count);
|
||||
LOG_VERBOSE("Found %zu backend devices:", dev_count);
|
||||
for (size_t i = 0; i < dev_count; ++i) {
|
||||
auto dev = ggml_backend_dev_get(i);
|
||||
LOG_DEBUG("#%zu: %s", i, ggml_backend_dev_name(dev));
|
||||
LOG_VERBOSE("#%zu: %s", i, ggml_backend_dev_name(dev));
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -587,13 +659,13 @@ static ggml_backend_t sd_get_default_backend() {
|
||||
}
|
||||
|
||||
if (sd_backend_is_cpu(backend)) {
|
||||
LOG_DEBUG("Using CPU backend");
|
||||
LOG_VERBOSE("Using CPU backend");
|
||||
}
|
||||
|
||||
return backend;
|
||||
}
|
||||
|
||||
static bool sd_parse_backend_assignment(const std::string& spec, SDBackendAssignment* assignment, std::string* error) {
|
||||
bool sd_parse_backend_assignment(const std::string& spec, SDBackendAssignment* assignment, std::string* error) {
|
||||
if (assignment == nullptr) {
|
||||
return false;
|
||||
}
|
||||
@@ -660,7 +732,13 @@ void SDBackendAssignment::set_module(SDBackendModule module, const std::string&
|
||||
}
|
||||
|
||||
void SDBackendHandleDeleter::operator()(ggml_backend_t backend) const {
|
||||
ggml_backend_free(backend);
|
||||
try {
|
||||
ggml_backend_free(backend);
|
||||
} catch (const std::exception& error) {
|
||||
LOG_ERROR("backend cleanup failed: %s", error.what());
|
||||
} catch (...) {
|
||||
LOG_ERROR("backend cleanup failed: unknown exception");
|
||||
}
|
||||
}
|
||||
|
||||
SDBackendManager::~SDBackendManager() {
|
||||
@@ -962,6 +1040,40 @@ const char* sd_backend_module_name(SDBackendModule module) {
|
||||
return "upscaler";
|
||||
case SDBackendModule::DETECTOR:
|
||||
return "detector";
|
||||
case SDBackendModule::AUDIO_ENCODER:
|
||||
return "audio_encoder";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
void ggml_ext_backend_tensor_get_and_sync(ggml_backend_t backend, const ggml_tensor* tensor, void* data, size_t offset, size_t size) {
|
||||
if ((sd_backend_is(backend, "ROCm") || sd_backend_is(backend, "CUDA") || sd_backend_is(backend, "SYCL")) &&
|
||||
!sd_backend_is_cpu(backend)) {
|
||||
ggml_backend_tensor_get_async(backend, tensor, data, offset, size);
|
||||
ggml_backend_synchronize(backend);
|
||||
return;
|
||||
}
|
||||
|
||||
ggml_backend_tensor_get(tensor, data, offset, size);
|
||||
}
|
||||
|
||||
float ggml_ext_backend_tensor_get_f32(ggml_tensor* tensor) {
|
||||
GGML_ASSERT(tensor->type == GGML_TYPE_F32 || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_I32 || tensor->type == GGML_TYPE_BF16);
|
||||
float value;
|
||||
if (tensor->type == GGML_TYPE_F32) {
|
||||
ggml_backend_tensor_get(tensor, &value, 0, sizeof(value));
|
||||
} else if (tensor->type == GGML_TYPE_BF16) {
|
||||
ggml_bf16_t bf16_value;
|
||||
ggml_backend_tensor_get(tensor, &bf16_value, 0, sizeof(bf16_value));
|
||||
value = ggml_bf16_to_fp32(bf16_value);
|
||||
} else if (tensor->type == GGML_TYPE_F16) {
|
||||
ggml_fp16_t f16_value;
|
||||
ggml_backend_tensor_get(tensor, &f16_value, 0, sizeof(f16_value));
|
||||
value = ggml_fp16_to_fp32(f16_value);
|
||||
} else { // GGML_TYPE_I32
|
||||
int int32_value;
|
||||
ggml_backend_tensor_get(tensor, &int32_value, 0, sizeof(int32_value));
|
||||
value = (float)int32_value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ enum class SDBackendModule {
|
||||
PHOTOMAKER,
|
||||
UPSCALER,
|
||||
DETECTOR,
|
||||
AUDIO_ENCODER,
|
||||
};
|
||||
|
||||
struct SDBackendAssignment {
|
||||
@@ -86,6 +87,7 @@ private:
|
||||
|
||||
bool sd_backend_is(ggml_backend_t backend, const std::string& name);
|
||||
bool sd_backend_is_cpu(ggml_backend_t backend);
|
||||
bool sd_backend_supports_cuda_mma(ggml_backend_t backend);
|
||||
ggml_backend_t sd_backend_cpu_init();
|
||||
bool sd_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads);
|
||||
ggml_status sd_backend_graph_compute_with_eval_callback(ggml_backend_t backend,
|
||||
@@ -93,7 +95,10 @@ ggml_status sd_backend_graph_compute_with_eval_callback(ggml_backend_t backend,
|
||||
sd_graph_eval_callback_t callback_eval,
|
||||
void* callback_eval_user_data);
|
||||
std::string sd_backend_resolve_name(const std::string& name);
|
||||
bool sd_parse_backend_assignment(const std::string& spec, SDBackendAssignment* assignment, std::string* error);
|
||||
const char* sd_backend_module_name(SDBackendModule module);
|
||||
void ggml_ext_im_set_f32_1d(const struct ggml_tensor* tensor, int i, float value);
|
||||
bool add_rpc_devices(const std::string& servers);
|
||||
void ggml_ext_backend_tensor_get_and_sync(ggml_backend_t backend, const ggml_tensor* tensor, void* data, size_t offset, size_t size);
|
||||
float ggml_ext_backend_tensor_get_f32(ggml_tensor* tensor);
|
||||
#endif // __SD_CORE_GGML_EXTEND_BACKEND_H__
|
||||
|
||||
+372
-346
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <climits>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
@@ -15,7 +16,7 @@
|
||||
#include "ggml-alloc.h"
|
||||
#include "ggml-backend.h"
|
||||
|
||||
#include "ggml/src/ggml-impl.h"
|
||||
#include "ggml-impl.h"
|
||||
|
||||
namespace sd::ggml_graph_cut {
|
||||
|
||||
@@ -67,25 +68,6 @@ namespace sd::ggml_graph_cut {
|
||||
return -1;
|
||||
}
|
||||
|
||||
static Plan::InputShape input_shape(const ggml_tensor* tensor) {
|
||||
Plan::InputShape shape;
|
||||
if (tensor == nullptr) {
|
||||
return shape;
|
||||
}
|
||||
shape.type = tensor->type;
|
||||
for (int i = 0; i < GGML_MAX_DIMS; ++i) {
|
||||
shape.ne[static_cast<size_t>(i)] = tensor->ne[i];
|
||||
}
|
||||
return shape;
|
||||
}
|
||||
|
||||
static size_t graph_cut_segment_vram_bytes(const Segment& segment) {
|
||||
return segment.compute_buffer_size +
|
||||
segment.input_param_bytes +
|
||||
segment.input_previous_cut_bytes +
|
||||
segment.output_bytes;
|
||||
}
|
||||
|
||||
static std::string lower_ascii_copy(std::string value) {
|
||||
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char c) {
|
||||
return static_cast<char>(std::tolower(c));
|
||||
@@ -291,55 +273,6 @@ namespace sd::ggml_graph_cut {
|
||||
return max_vram_bytes_to_gib(resolve_auto_max_vram_bytes(-max_vram, backend));
|
||||
}
|
||||
|
||||
static bool is_segment_output_needed_after(const Plan& plan,
|
||||
size_t end_segment_index,
|
||||
int output_node_index) {
|
||||
if (end_segment_index + 1 >= plan.segments.size()) {
|
||||
return false;
|
||||
}
|
||||
for (size_t seg_idx = end_segment_index + 1; seg_idx < plan.segments.size(); ++seg_idx) {
|
||||
const auto& segment = plan.segments[seg_idx];
|
||||
for (const auto& input_ref : segment.input_refs) {
|
||||
if (input_ref.type == Segment::INPUT_PREVIOUS_CUT &&
|
||||
input_ref.node_index == output_node_index) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static Segment make_segment_seed(const Plan& plan,
|
||||
size_t start_segment_index,
|
||||
size_t end_segment_index) {
|
||||
GGML_ASSERT(start_segment_index < plan.segments.size());
|
||||
GGML_ASSERT(end_segment_index < plan.segments.size());
|
||||
GGML_ASSERT(start_segment_index <= end_segment_index);
|
||||
|
||||
Segment seed;
|
||||
const auto& start_segment = plan.segments[start_segment_index];
|
||||
const auto& target_segment = plan.segments[end_segment_index];
|
||||
std::unordered_set<int> seen_output_node_indices;
|
||||
for (size_t seg_idx = start_segment_index; seg_idx <= end_segment_index; ++seg_idx) {
|
||||
const bool is_boundary_segment = seg_idx == end_segment_index;
|
||||
for (int output_node_index : plan.segments[seg_idx].output_node_indices) {
|
||||
if ((is_boundary_segment ||
|
||||
is_segment_output_needed_after(plan, end_segment_index, output_node_index)) &&
|
||||
seen_output_node_indices.insert(output_node_index).second) {
|
||||
seed.output_node_indices.push_back(output_node_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (start_segment_index == end_segment_index) {
|
||||
seed.group_name = target_segment.group_name;
|
||||
} else {
|
||||
seed.group_name = sd_format("%s..%s",
|
||||
start_segment.group_name.c_str(),
|
||||
target_segment.group_name.c_str());
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
|
||||
static void build_segment(ggml_cgraph* gf,
|
||||
Plan& plan,
|
||||
Segment& segment,
|
||||
@@ -416,31 +349,7 @@ namespace sd::ggml_graph_cut {
|
||||
}
|
||||
return a.display_name < b.display_name;
|
||||
});
|
||||
segment.input_refs = input_refs;
|
||||
for (const auto& input : input_refs) {
|
||||
ggml_tensor* current_input = input_tensor(gf, input);
|
||||
size_t tensor_bytes = current_input == nullptr
|
||||
? 0
|
||||
: (input.type == Segment::INPUT_PREVIOUS_CUT
|
||||
? cache_tensor_bytes(current_input)
|
||||
: ggml_nbytes(current_input));
|
||||
switch (input.type) {
|
||||
case Segment::INPUT_PREVIOUS_CUT:
|
||||
segment.input_previous_cut_bytes += tensor_bytes;
|
||||
break;
|
||||
case Segment::INPUT_PARAM:
|
||||
segment.input_param_bytes += tensor_bytes;
|
||||
break;
|
||||
case Segment::INPUT_EXTERNAL:
|
||||
default:
|
||||
segment.input_external_bytes += tensor_bytes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int output_node_index : segment.output_node_indices) {
|
||||
ggml_tensor* output = ggml_graph_node(gf, output_node_index);
|
||||
segment.output_bytes += cache_tensor_bytes(output);
|
||||
}
|
||||
segment.input_refs = input_refs;
|
||||
segment.compute_buffer_size = measure_segment_compute_buffer(backend, gf, segment, log_desc);
|
||||
|
||||
for (int output_node_index : segment.output_node_indices) {
|
||||
@@ -449,15 +358,80 @@ namespace sd::ggml_graph_cut {
|
||||
plan.segments.push_back(std::move(segment));
|
||||
}
|
||||
|
||||
static bool validate_plan(ggml_cgraph* gf,
|
||||
const Plan& plan,
|
||||
std::string* validation_error) {
|
||||
auto fail = [&](const std::string& reason) {
|
||||
if (validation_error != nullptr) {
|
||||
*validation_error = reason;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
if (!plan.has_cuts) {
|
||||
return true;
|
||||
}
|
||||
if (plan.segments.size() <= 1) {
|
||||
return fail("fewer than two segments");
|
||||
}
|
||||
const int n_nodes = ggml_graph_n_nodes(gf);
|
||||
std::unordered_set<int> completed_outputs;
|
||||
for (size_t segment_index = 0; segment_index < plan.segments.size(); ++segment_index) {
|
||||
const Segment& segment = plan.segments[segment_index];
|
||||
const std::string segment_label = "segment " + std::to_string(segment_index) +
|
||||
" ('" + segment.group_name + "')";
|
||||
if (segment.internal_node_indices.empty() || segment.output_node_indices.empty()) {
|
||||
return fail(segment_label + " has no internal nodes or outputs");
|
||||
}
|
||||
for (const Segment::InputRef& input : segment.input_refs) {
|
||||
if (input.type == Segment::INPUT_PREVIOUS_CUT) {
|
||||
if (input.node_index < 0 || input.node_index >= n_nodes ||
|
||||
completed_outputs.find(input.node_index) == completed_outputs.end()) {
|
||||
return fail(segment_label + " references an unavailable cut node " +
|
||||
std::to_string(input.node_index));
|
||||
}
|
||||
} else if (input.leaf_index < 0 || input.leaf_index >= gf->n_leafs) {
|
||||
return fail(segment_label + " references an invalid leaf " +
|
||||
std::to_string(input.leaf_index));
|
||||
}
|
||||
}
|
||||
std::unordered_set<int> segment_nodes;
|
||||
segment_nodes.reserve(segment.internal_node_indices.size());
|
||||
for (int node_index : segment.internal_node_indices) {
|
||||
if (node_index < 0 || node_index >= n_nodes) {
|
||||
return fail(segment_label + " contains an invalid node " +
|
||||
std::to_string(node_index));
|
||||
}
|
||||
if (!segment_nodes.insert(node_index).second) {
|
||||
return fail(segment_label + " contains duplicate node " +
|
||||
std::to_string(node_index));
|
||||
}
|
||||
}
|
||||
for (int output_index : segment.output_node_indices) {
|
||||
if (output_index < 0 || output_index >= n_nodes ||
|
||||
segment_nodes.find(output_index) == segment_nodes.end()) {
|
||||
return fail(segment_label + " has an output outside its node set: " +
|
||||
std::to_string(output_index));
|
||||
}
|
||||
if (completed_outputs.find(output_index) != completed_outputs.end()) {
|
||||
return fail(segment_label + " repeats output node " +
|
||||
std::to_string(output_index));
|
||||
}
|
||||
completed_outputs.insert(output_index);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool is_graph_cut_tensor(const ggml_tensor* tensor) {
|
||||
if (tensor == nullptr || tensor->name[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
return std::strncmp(tensor->name, GGML_RUNNER_CUT_PREFIX, std::strlen(GGML_RUNNER_CUT_PREFIX)) == 0;
|
||||
return std::strncmp(tensor->name, GGML_RUNNER_CUT_PREFIX, std::strlen(GGML_RUNNER_CUT_PREFIX)) == 0 &&
|
||||
tensor->name[std::strlen(tensor->name) - 1] == GGML_RUNNER_CUT_SUFFIX[0];
|
||||
}
|
||||
|
||||
std::string make_graph_cut_name(const std::string& group, const std::string& output) {
|
||||
return std::string(GGML_RUNNER_CUT_PREFIX) + group + "|" + output;
|
||||
return std::string(GGML_RUNNER_CUT_PREFIX) + group + "|" + output + GGML_RUNNER_CUT_SUFFIX;
|
||||
}
|
||||
|
||||
void mark_graph_cut(ggml_tensor* tensor, const std::string& group, const std::string& output) {
|
||||
@@ -508,26 +482,156 @@ namespace sd::ggml_graph_cut {
|
||||
return ggml_nbytes(cache_src);
|
||||
}
|
||||
|
||||
bool plan_matches_graph(ggml_cgraph* gf, const Plan& plan) {
|
||||
GGML_ASSERT(gf != nullptr);
|
||||
if (ggml_graph_n_nodes(gf) != plan.n_nodes || gf->n_leafs != plan.n_leafs) {
|
||||
return false;
|
||||
static bool can_ignore_op_params(ggml_op op) {
|
||||
// Exempt only parameters that cannot affect graph layout or backend allocation size.
|
||||
switch (op) {
|
||||
case GGML_OP_SCALE:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
for (const auto& input_shape_ref : plan.input_shapes) {
|
||||
if (input_shape_ref.leaf_index < 0 || input_shape_ref.leaf_index >= gf->n_leafs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct GraphLayoutTensors {
|
||||
struct Entry {
|
||||
const ggml_tensor* tensor = nullptr;
|
||||
size_t index = 0;
|
||||
};
|
||||
|
||||
std::vector<ggml_tensor*> tensors;
|
||||
std::vector<Entry> entries;
|
||||
|
||||
explicit GraphLayoutTensors(size_t graph_size) {
|
||||
tensors.reserve(graph_size);
|
||||
size_t capacity = 2;
|
||||
while (capacity < 2 * graph_size) {
|
||||
capacity *= 2;
|
||||
}
|
||||
ggml_tensor* leaf = gf->leafs[input_shape_ref.leaf_index];
|
||||
if (leaf == nullptr || input_shape_ref.type != leaf->type) {
|
||||
return false;
|
||||
entries.resize(capacity);
|
||||
}
|
||||
|
||||
size_t find(const ggml_tensor* tensor) const {
|
||||
size_t hash = reinterpret_cast<uintptr_t>(tensor) >> 4;
|
||||
hash ^= hash >> 16;
|
||||
const size_t mask = entries.size() - 1;
|
||||
size_t slot = hash & mask;
|
||||
while (entries[slot].tensor != nullptr && entries[slot].tensor != tensor) {
|
||||
slot = (slot + 1) & mask;
|
||||
}
|
||||
return slot;
|
||||
}
|
||||
|
||||
void add(ggml_tensor* tensor) {
|
||||
if (tensor == nullptr) {
|
||||
return;
|
||||
}
|
||||
size_t slot = find(tensor);
|
||||
if (entries[slot].tensor != nullptr) {
|
||||
return;
|
||||
}
|
||||
if (2 * (tensors.size() + 1) > entries.size()) {
|
||||
// Segment graphs can reference tensors outside their node and leaf arrays.
|
||||
std::vector<Entry> next(2 * entries.size());
|
||||
entries.swap(next);
|
||||
for (size_t i = 0; i < tensors.size(); ++i) {
|
||||
entries[find(tensors[i])] = {tensors[i], i + 1};
|
||||
}
|
||||
slot = find(tensor);
|
||||
}
|
||||
entries[slot] = {tensor, tensors.size() + 1};
|
||||
tensors.push_back(tensor);
|
||||
}
|
||||
|
||||
size_t index(const ggml_tensor* tensor) const {
|
||||
return tensor == nullptr ? 0 : entries[find(tensor)].index;
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<uint64_t> graph_layout(ggml_cgraph* graph, bool include_bindings) {
|
||||
const size_t graph_size = static_cast<size_t>(graph->n_leafs) + graph->n_nodes;
|
||||
GraphLayoutTensors layout_tensors(graph_size);
|
||||
const auto& tensors = layout_tensors.tensors;
|
||||
for (int i = 0; i < graph->n_leafs; ++i) {
|
||||
layout_tensors.add(graph->leafs[i]);
|
||||
}
|
||||
for (int i = 0; i < graph->n_nodes; ++i) {
|
||||
layout_tensors.add(graph->nodes[i]);
|
||||
}
|
||||
for (size_t i = 0; i < tensors.size(); ++i) {
|
||||
layout_tensors.add(tensors[i]->view_src);
|
||||
for (auto source : tensors[i]->src) {
|
||||
layout_tensors.add(source);
|
||||
}
|
||||
}
|
||||
std::vector<uint64_t> signature;
|
||||
const size_t tensor_fields = 5 + 2 * GGML_MAX_DIMS + GGML_MAX_SRC +
|
||||
GGML_MAX_OP_PARAMS / sizeof(int32_t) + (include_bindings ? 2 : 0);
|
||||
signature.reserve(2 + graph_size + tensors.size() * tensor_fields);
|
||||
signature.push_back(graph->n_nodes);
|
||||
signature.push_back(graph->n_leafs);
|
||||
for (int i = 0; i < graph->n_leafs; ++i) {
|
||||
signature.push_back(layout_tensors.index(graph->leafs[i]));
|
||||
}
|
||||
for (int i = 0; i < graph->n_nodes; ++i) {
|
||||
signature.push_back(layout_tensors.index(graph->nodes[i]));
|
||||
}
|
||||
for (auto tensor : tensors) {
|
||||
signature.push_back(tensor->op);
|
||||
signature.push_back(tensor->type);
|
||||
signature.push_back(tensor->flags);
|
||||
signature.push_back(tensor->view_offs);
|
||||
if (include_bindings) {
|
||||
signature.push_back(tensor->data != nullptr);
|
||||
auto buffer = tensor_buffer(tensor);
|
||||
signature.push_back(reinterpret_cast<uintptr_t>(buffer == nullptr ? nullptr : ggml_backend_buffer_get_type(buffer)));
|
||||
}
|
||||
for (int d = 0; d < GGML_MAX_DIMS; ++d) {
|
||||
if (input_shape_ref.ne[static_cast<size_t>(d)] != leaf->ne[d]) {
|
||||
return false;
|
||||
signature.push_back(tensor->ne[d]);
|
||||
signature.push_back(tensor->nb[d]);
|
||||
}
|
||||
signature.push_back(layout_tensors.index(tensor->view_src));
|
||||
for (auto source : tensor->src) {
|
||||
signature.push_back(layout_tensors.index(source));
|
||||
}
|
||||
if (!can_ignore_op_params(tensor->op)) {
|
||||
for (int value : tensor->op_params) {
|
||||
signature.push_back(static_cast<uint32_t>(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return signature;
|
||||
}
|
||||
|
||||
static bool plan_matches_graph(ggml_cgraph* gf,
|
||||
const Plan& plan,
|
||||
const std::vector<uint64_t>& layout) {
|
||||
GGML_ASSERT(gf != nullptr);
|
||||
if (plan.leaf_names.size() != static_cast<size_t>(gf->n_leafs) ||
|
||||
plan.layout != layout) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < gf->n_leafs; ++i) {
|
||||
if (plan.leaf_names[i] != gf->leafs[i]->name) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
size_t cut_index = 0;
|
||||
for (int i = 0; i < gf->n_nodes; ++i) {
|
||||
auto node = gf->nodes[i];
|
||||
if (is_graph_cut_tensor(node)) {
|
||||
if (cut_index >= plan.cut_markers.size() ||
|
||||
plan.cut_markers[cut_index].first != i || plan.cut_markers[cut_index].second != node->name) {
|
||||
return false;
|
||||
}
|
||||
++cut_index;
|
||||
}
|
||||
}
|
||||
return cut_index == plan.cut_markers.size();
|
||||
}
|
||||
|
||||
bool plan_matches_graph(ggml_cgraph* gf, const Plan& plan) {
|
||||
GGML_ASSERT(gf != nullptr);
|
||||
return plan_matches_graph(gf, plan, graph_layout(gf, false));
|
||||
}
|
||||
|
||||
ggml_tensor* output_tensor(ggml_cgraph* gf, const Segment& segment, size_t output_index) {
|
||||
@@ -577,33 +681,49 @@ namespace sd::ggml_graph_cut {
|
||||
return tensors;
|
||||
}
|
||||
|
||||
std::unordered_set<std::string> collect_future_input_names(ggml_cgraph* gf,
|
||||
const Plan& plan,
|
||||
size_t current_segment_index) {
|
||||
GGML_ASSERT(gf != nullptr);
|
||||
std::unordered_set<std::string> future_input_names;
|
||||
for (size_t seg_idx = current_segment_index + 1; seg_idx < plan.segments.size(); ++seg_idx) {
|
||||
const auto& segment = plan.segments[seg_idx];
|
||||
for (const auto& input_ref : segment.input_refs) {
|
||||
if (input_ref.type != Segment::INPUT_PREVIOUS_CUT) {
|
||||
continue;
|
||||
}
|
||||
ggml_tensor* current_input = input_tensor(gf, input_ref);
|
||||
if (current_input != nullptr && current_input->name[0] != '\0') {
|
||||
future_input_names.insert(current_input->name);
|
||||
}
|
||||
}
|
||||
}
|
||||
return future_input_names;
|
||||
}
|
||||
|
||||
ggml_cgraph* build_segment_graph(ggml_cgraph* gf,
|
||||
const Segment& segment,
|
||||
ggml_context** graph_ctx_out) {
|
||||
GGML_ASSERT(gf != nullptr);
|
||||
GGML_ASSERT(graph_ctx_out != nullptr);
|
||||
|
||||
const size_t graph_size = segment.internal_node_indices.size() + segment.input_refs.size() + 8;
|
||||
// Collect leaf inputs and internal nodes, then any tensor they
|
||||
// reference that is not already represented, notably the view_src of a
|
||||
// view-typed input leaf. ggml_gallocr sizes its hash set from
|
||||
// n_nodes + n_leafs (plus a 25% margin that rounds down to zero for a
|
||||
// one-node segment), so every distinct tensor it will hash must be
|
||||
// counted here or a tiny segment overflows the hash set and aborts.
|
||||
std::vector<ggml_tensor*> leaves;
|
||||
std::unordered_set<ggml_tensor*> represented;
|
||||
for (const auto& input : segment.input_refs) {
|
||||
ggml_tensor* current_input = input_tensor(gf, input);
|
||||
if (current_input == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (represented.insert(current_input).second) {
|
||||
leaves.push_back(current_input);
|
||||
}
|
||||
}
|
||||
for (int node_idx : segment.internal_node_indices) {
|
||||
represented.insert(ggml_graph_node(gf, node_idx));
|
||||
}
|
||||
auto add_reference = [&](ggml_tensor* tensor) {
|
||||
if (tensor != nullptr && represented.insert(tensor).second) {
|
||||
leaves.push_back(tensor);
|
||||
}
|
||||
};
|
||||
for (int node_idx : segment.internal_node_indices) {
|
||||
ggml_tensor* node = ggml_graph_node(gf, node_idx);
|
||||
for (int src_idx = 0; src_idx < GGML_MAX_SRC; ++src_idx) {
|
||||
add_reference(node->src[src_idx]);
|
||||
}
|
||||
add_reference(node->view_src);
|
||||
}
|
||||
for (size_t i = 0; i < leaves.size(); ++i) {
|
||||
add_reference(leaves[i]->view_src);
|
||||
}
|
||||
|
||||
const size_t graph_size = segment.internal_node_indices.size() + leaves.size() + 8;
|
||||
ggml_init_params params = {
|
||||
/*.mem_size =*/ggml_graph_overhead_custom(graph_size, false) + 1024,
|
||||
/*.mem_buffer =*/nullptr,
|
||||
@@ -614,13 +734,9 @@ namespace sd::ggml_graph_cut {
|
||||
ggml_cgraph* segment_graph = ggml_new_graph_custom(graph_ctx, graph_size, false);
|
||||
GGML_ASSERT(segment_graph != nullptr);
|
||||
|
||||
for (const auto& input : segment.input_refs) {
|
||||
ggml_tensor* current_input = input_tensor(gf, input);
|
||||
if (current_input == nullptr) {
|
||||
continue;
|
||||
}
|
||||
for (ggml_tensor* leaf : leaves) {
|
||||
GGML_ASSERT(segment_graph->n_leafs < segment_graph->size);
|
||||
segment_graph->leafs[segment_graph->n_leafs++] = current_input;
|
||||
segment_graph->leafs[segment_graph->n_leafs++] = leaf;
|
||||
}
|
||||
|
||||
for (int output_node_index : segment.output_node_indices) {
|
||||
@@ -629,6 +745,10 @@ namespace sd::ggml_graph_cut {
|
||||
continue;
|
||||
}
|
||||
ggml_set_output(output);
|
||||
if (output->view_src != nullptr) {
|
||||
// A consumed output view does not keep its storage alive in gallocr.
|
||||
ggml_set_output(output->view_src);
|
||||
}
|
||||
}
|
||||
for (int node_idx : segment.internal_node_indices) {
|
||||
ggml_graph_add_node(segment_graph, ggml_graph_node(gf, node_idx));
|
||||
@@ -683,6 +803,10 @@ namespace sd::ggml_graph_cut {
|
||||
if (output != nullptr && saved_output_flags.find(output) == saved_output_flags.end()) {
|
||||
saved_output_flags[output] = output->flags;
|
||||
}
|
||||
if (output != nullptr && output->view_src != nullptr &&
|
||||
saved_output_flags.find(output->view_src) == saved_output_flags.end()) {
|
||||
saved_output_flags[output->view_src] = output->view_src->flags;
|
||||
}
|
||||
}
|
||||
|
||||
ggml_context* graph_ctx = nullptr;
|
||||
@@ -711,6 +835,46 @@ namespace sd::ggml_graph_cut {
|
||||
return buffer_size;
|
||||
}
|
||||
|
||||
static size_t measure_graph_compute_buffer(
|
||||
ggml_backend_t backend,
|
||||
ggml_cgraph* gf,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set) {
|
||||
struct TensorRuntimeBinding {
|
||||
ggml_backend_buffer_t buffer = nullptr;
|
||||
void* data = nullptr;
|
||||
void* extra = nullptr;
|
||||
};
|
||||
std::unordered_map<ggml_tensor*, TensorRuntimeBinding> saved_bindings;
|
||||
auto mark_external = [&](ggml_tensor* tensor) {
|
||||
if (tensor == nullptr || saved_bindings.find(tensor) != saved_bindings.end()) {
|
||||
return;
|
||||
}
|
||||
saved_bindings[tensor] = {tensor->buffer, tensor->data, tensor->extra};
|
||||
tensor->data = reinterpret_cast<void*>(static_cast<uintptr_t>(1));
|
||||
};
|
||||
for (int i = 0; i < leaf_count(gf); ++i) {
|
||||
ggml_tensor* leaf = leaf_tensor(gf, i);
|
||||
if (!is_params_tensor(params_tensor_set, leaf)) {
|
||||
continue;
|
||||
}
|
||||
mark_external(leaf);
|
||||
mark_external(leaf->view_src);
|
||||
}
|
||||
|
||||
ggml_gallocr_t allocr = ggml_gallocr_new(
|
||||
ggml_backend_get_default_buffer_type(backend));
|
||||
size_t sizes[1] = {0};
|
||||
ggml_gallocr_reserve_n_size(allocr, gf, nullptr, nullptr, sizes);
|
||||
ggml_gallocr_free(allocr);
|
||||
|
||||
for (const auto& kv : saved_bindings) {
|
||||
kv.first->buffer = kv.second.buffer;
|
||||
kv.first->data = kv.second.data;
|
||||
kv.first->extra = kv.second.extra;
|
||||
}
|
||||
return sizes[0];
|
||||
}
|
||||
|
||||
Plan build_plan(ggml_backend_t backend,
|
||||
ggml_cgraph* gf,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set,
|
||||
@@ -723,24 +887,22 @@ namespace sd::ggml_graph_cut {
|
||||
if (n_nodes <= 0) {
|
||||
return plan;
|
||||
}
|
||||
plan.n_nodes = n_nodes;
|
||||
plan.n_leafs = gf->n_leafs;
|
||||
plan.layout = graph_layout(gf, false);
|
||||
for (int i = 0; i < gf->n_leafs; ++i) {
|
||||
ggml_tensor* leaf = gf->leafs[i];
|
||||
if (is_params_tensor(params_tensor_set, leaf)) {
|
||||
continue;
|
||||
}
|
||||
auto shape = input_shape(leaf);
|
||||
shape.leaf_index = i;
|
||||
plan.input_shapes.push_back(shape);
|
||||
plan.leaf_names.emplace_back(gf->leafs[i]->name);
|
||||
}
|
||||
plan.compute_buffer_size =
|
||||
measure_graph_compute_buffer(backend, gf, params_tensor_set);
|
||||
|
||||
std::unordered_map<const ggml_tensor*, int> producer_index;
|
||||
producer_index.reserve(static_cast<size_t>(n_nodes));
|
||||
for (int i = 0; i < n_nodes; ++i) {
|
||||
producer_index[ggml_graph_node(gf, i)] = i;
|
||||
ggml_tensor* node = ggml_graph_node(gf, i);
|
||||
producer_index[node] = i;
|
||||
if (is_graph_cut_tensor(node)) {
|
||||
plan.cut_markers.push_back({i, node->name});
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<Segment> grouped_segments;
|
||||
std::unordered_map<std::string, size_t> group_to_segment;
|
||||
for (int i = 0; i < n_nodes; ++i) {
|
||||
@@ -751,7 +913,9 @@ namespace sd::ggml_graph_cut {
|
||||
|
||||
plan.has_cuts = true;
|
||||
std::string full_name(node->name);
|
||||
std::string payload = full_name.substr(std::strlen(GGML_RUNNER_CUT_PREFIX));
|
||||
size_t prefix_len = std::strlen(GGML_RUNNER_CUT_PREFIX);
|
||||
size_t suffix_len = std::strlen(GGML_RUNNER_CUT_SUFFIX);
|
||||
std::string payload = full_name.substr(prefix_len, full_name.size() - prefix_len - suffix_len);
|
||||
size_t sep = payload.find('|');
|
||||
std::string group = sep == std::string::npos ? payload : payload.substr(0, sep);
|
||||
|
||||
@@ -789,11 +953,24 @@ namespace sd::ggml_graph_cut {
|
||||
if (final_output_index < 0) {
|
||||
final_output_index = n_nodes - 1;
|
||||
}
|
||||
ggml_tensor* final_output = final_output_index >= 0 ? ggml_graph_node(gf, final_output_index) : nullptr;
|
||||
if (final_output != nullptr && available_cut_output_node_indices.find(final_output_index) == available_cut_output_node_indices.end()) {
|
||||
Segment final_segment;
|
||||
final_segment.group_name = "ggml_runner.final";
|
||||
Segment final_segment;
|
||||
final_segment.group_name = "ggml_runner.final";
|
||||
if (final_output_index >= 0 &&
|
||||
available_cut_output_node_indices.find(final_output_index) ==
|
||||
available_cut_output_node_indices.end()) {
|
||||
final_segment.output_node_indices.push_back(final_output_index);
|
||||
}
|
||||
for (int i = 0; i < n_nodes; ++i) {
|
||||
ggml_tensor* node = ggml_graph_node(gf, i);
|
||||
if (i == final_output_index || node == nullptr ||
|
||||
(node->flags & GGML_TENSOR_FLAG_OUTPUT) == 0 ||
|
||||
available_cut_output_node_indices.find(i) !=
|
||||
available_cut_output_node_indices.end()) {
|
||||
continue;
|
||||
}
|
||||
final_segment.output_node_indices.push_back(i);
|
||||
}
|
||||
if (!final_segment.output_node_indices.empty()) {
|
||||
build_segment(gf,
|
||||
plan,
|
||||
final_segment,
|
||||
@@ -804,210 +981,59 @@ namespace sd::ggml_graph_cut {
|
||||
log_desc);
|
||||
}
|
||||
|
||||
std::unordered_set<std::string> future_cut_names;
|
||||
for (auto segment = plan.segments.rbegin(); segment != plan.segments.rend(); ++segment) {
|
||||
segment->future_cut_names = future_cut_names;
|
||||
segment->live_cut_names = future_cut_names;
|
||||
for (const auto& input : segment->input_refs) {
|
||||
if (input.type != Segment::INPUT_PREVIOUS_CUT) {
|
||||
continue;
|
||||
}
|
||||
segment->live_cut_names.insert(input.display_name);
|
||||
future_cut_names.insert(input.display_name);
|
||||
}
|
||||
}
|
||||
|
||||
std::string plan_validation_error;
|
||||
plan.valid = validate_plan(gf, plan, &plan_validation_error);
|
||||
if (!plan.valid && log_desc != nullptr) {
|
||||
LOG_WARN("%s graph cut plan validation failed (%s); using monolithic execution",
|
||||
log_desc,
|
||||
plan_validation_error.c_str());
|
||||
}
|
||||
|
||||
return plan;
|
||||
}
|
||||
|
||||
Plan apply_max_vram_budget(ggml_cgraph* gf,
|
||||
const Plan& base_plan,
|
||||
size_t max_graph_vram_bytes,
|
||||
ggml_backend_t backend,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set,
|
||||
const char* log_desc) {
|
||||
GGML_ASSERT(backend != nullptr);
|
||||
GGML_ASSERT(gf != nullptr);
|
||||
int64_t t_budget_begin = ggml_time_ms();
|
||||
if (max_graph_vram_bytes == 0 || !base_plan.has_cuts || base_plan.segments.size() <= 1) {
|
||||
return base_plan;
|
||||
}
|
||||
|
||||
const int n_nodes = ggml_graph_n_nodes(gf);
|
||||
std::unordered_map<const ggml_tensor*, int> producer_index;
|
||||
producer_index.reserve(static_cast<size_t>(n_nodes));
|
||||
for (int i = 0; i < n_nodes; ++i) {
|
||||
producer_index[ggml_graph_node(gf, i)] = i;
|
||||
}
|
||||
|
||||
Plan merged_plan;
|
||||
merged_plan.available = true;
|
||||
merged_plan.has_cuts = base_plan.has_cuts;
|
||||
merged_plan.valid = base_plan.valid;
|
||||
merged_plan.n_nodes = base_plan.n_nodes;
|
||||
merged_plan.n_leafs = base_plan.n_leafs;
|
||||
|
||||
std::unordered_set<int> available_cut_output_node_indices;
|
||||
available_cut_output_node_indices.reserve(static_cast<size_t>(n_nodes));
|
||||
|
||||
size_t start_segment_index = 0;
|
||||
while (start_segment_index < base_plan.segments.size()) {
|
||||
Plan single_plan;
|
||||
auto single_available_cut_output_node_indices = available_cut_output_node_indices;
|
||||
auto single_seed = make_segment_seed(base_plan,
|
||||
start_segment_index,
|
||||
start_segment_index);
|
||||
build_segment(gf,
|
||||
single_plan,
|
||||
single_seed,
|
||||
producer_index,
|
||||
single_available_cut_output_node_indices,
|
||||
backend,
|
||||
params_tensor_set,
|
||||
log_desc);
|
||||
GGML_ASSERT(!single_plan.segments.empty());
|
||||
|
||||
size_t best_end_segment_index = start_segment_index;
|
||||
bool can_merge_next_segment = graph_cut_segment_vram_bytes(single_plan.segments.back()) <= max_graph_vram_bytes;
|
||||
|
||||
while (can_merge_next_segment && best_end_segment_index + 1 < base_plan.segments.size()) {
|
||||
const size_t next_end_segment_index = best_end_segment_index + 1;
|
||||
Plan candidate_plan;
|
||||
auto candidate_available_cut_output_node_indices = available_cut_output_node_indices;
|
||||
auto candidate_seed = make_segment_seed(base_plan,
|
||||
start_segment_index,
|
||||
next_end_segment_index);
|
||||
build_segment(gf,
|
||||
candidate_plan,
|
||||
candidate_seed,
|
||||
producer_index,
|
||||
candidate_available_cut_output_node_indices,
|
||||
backend,
|
||||
params_tensor_set,
|
||||
log_desc);
|
||||
GGML_ASSERT(!candidate_plan.segments.empty());
|
||||
|
||||
const auto& candidate_segment = candidate_plan.segments.back();
|
||||
const size_t candidate_bytes = graph_cut_segment_vram_bytes(candidate_segment);
|
||||
if (candidate_bytes > max_graph_vram_bytes) {
|
||||
break;
|
||||
}
|
||||
|
||||
best_end_segment_index = next_end_segment_index;
|
||||
}
|
||||
|
||||
auto best_seed = make_segment_seed(base_plan,
|
||||
start_segment_index,
|
||||
best_end_segment_index);
|
||||
build_segment(gf,
|
||||
merged_plan,
|
||||
best_seed,
|
||||
producer_index,
|
||||
available_cut_output_node_indices,
|
||||
backend,
|
||||
params_tensor_set,
|
||||
log_desc);
|
||||
start_segment_index = best_end_segment_index + 1;
|
||||
}
|
||||
|
||||
if (log_desc != nullptr && merged_plan.segments.size() != base_plan.segments.size()) {
|
||||
LOG_INFO("%s graph cut max_vram=%.2f MB merged %zu segments -> %zu segments",
|
||||
log_desc,
|
||||
max_graph_vram_bytes / 1024.0 / 1024.0,
|
||||
base_plan.segments.size(),
|
||||
merged_plan.segments.size());
|
||||
}
|
||||
|
||||
if (log_desc != nullptr) {
|
||||
LOG_DEBUG("%s graph cut max_vram budget merge took %lld ms",
|
||||
log_desc,
|
||||
ggml_time_ms() - t_budget_begin);
|
||||
}
|
||||
|
||||
return merged_plan;
|
||||
}
|
||||
|
||||
Plan resolve_plan(ggml_backend_t backend,
|
||||
ggml_cgraph* gf,
|
||||
PlanCache* cache,
|
||||
size_t max_graph_vram_bytes,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set,
|
||||
const char* log_desc) {
|
||||
const Plan& resolve_plan(ggml_backend_t backend,
|
||||
ggml_cgraph* gf,
|
||||
PlanCache* cache,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set,
|
||||
const char* log_desc) {
|
||||
GGML_ASSERT(backend != nullptr);
|
||||
GGML_ASSERT(gf != nullptr);
|
||||
GGML_ASSERT(cache != nullptr);
|
||||
|
||||
int64_t t_prepare_begin = ggml_time_ms();
|
||||
Plan base_plan;
|
||||
const auto layout = graph_layout(gf, false);
|
||||
auto& plans = cache->graph_cut_plans;
|
||||
for (auto it = plans.begin(); it != plans.end(); ++it) {
|
||||
if (it->available && plan_matches_graph(gf, *it, layout)) {
|
||||
plans.splice(plans.begin(), plans, it);
|
||||
return plans.front();
|
||||
}
|
||||
}
|
||||
|
||||
int64_t t_plan_begin = ggml_time_ms();
|
||||
if (cache->graph_cut_plan.available && plan_matches_graph(gf, cache->graph_cut_plan)) {
|
||||
base_plan = cache->graph_cut_plan;
|
||||
} else {
|
||||
base_plan = build_plan(backend, gf, params_tensor_set, log_desc);
|
||||
cache->graph_cut_plan = base_plan;
|
||||
cache->graph_cut_plan.available = true;
|
||||
cache->budgeted_graph_cut_plan.available = false;
|
||||
if (log_desc != nullptr) {
|
||||
LOG_INFO("%s build cached graph cut plan done (taking %lld ms)", log_desc, ggml_time_ms() - t_plan_begin);
|
||||
}
|
||||
plans.push_front(build_plan(backend, gf, params_tensor_set, log_desc));
|
||||
if (plans.size() > PlanCache::MAX_PLANS) {
|
||||
plans.pop_back();
|
||||
}
|
||||
|
||||
Plan resolved_plan = base_plan;
|
||||
if (max_graph_vram_bytes > 0 && base_plan.has_cuts) {
|
||||
if (cache->budgeted_graph_cut_plan.available &&
|
||||
cache->budgeted_graph_cut_plan_max_vram_bytes == max_graph_vram_bytes &&
|
||||
plan_matches_graph(gf, cache->budgeted_graph_cut_plan)) {
|
||||
resolved_plan = cache->budgeted_graph_cut_plan;
|
||||
} else {
|
||||
resolved_plan = apply_max_vram_budget(gf,
|
||||
base_plan,
|
||||
max_graph_vram_bytes,
|
||||
backend,
|
||||
params_tensor_set,
|
||||
log_desc);
|
||||
cache->budgeted_graph_cut_plan = resolved_plan;
|
||||
cache->budgeted_graph_cut_plan.available = true;
|
||||
cache->budgeted_graph_cut_plan_max_vram_bytes = max_graph_vram_bytes;
|
||||
}
|
||||
}
|
||||
return resolved_plan;
|
||||
}
|
||||
|
||||
void annotate_residency(Plan& plan, size_t max_graph_vram_bytes) {
|
||||
// Cached plans may be reused with a smaller live budget.
|
||||
for (auto& seg : plan.segments) {
|
||||
seg.residency = SegmentResidency::STREAMED;
|
||||
}
|
||||
if (max_graph_vram_bytes == 0 || plan.segments.size() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool any_param_bearing = false;
|
||||
for (const auto& seg : plan.segments) {
|
||||
if (seg.input_param_bytes > 0) {
|
||||
any_param_bearing = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!any_param_bearing) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Leave room for the largest active streamed segment.
|
||||
size_t worst_streamed_footprint = 0;
|
||||
for (const auto& seg : plan.segments) {
|
||||
const size_t seg_footprint = seg.input_param_bytes +
|
||||
seg.compute_buffer_size +
|
||||
seg.output_bytes +
|
||||
seg.input_previous_cut_bytes +
|
||||
seg.input_external_bytes;
|
||||
if (seg_footprint > worst_streamed_footprint) {
|
||||
worst_streamed_footprint = seg_footprint;
|
||||
}
|
||||
}
|
||||
constexpr size_t safety = 512ull * 1024 * 1024;
|
||||
const size_t reserved = safety + worst_streamed_footprint;
|
||||
|
||||
if (max_graph_vram_bytes <= reserved) {
|
||||
return;
|
||||
}
|
||||
const size_t available = max_graph_vram_bytes - reserved;
|
||||
|
||||
size_t cumulative = 0;
|
||||
for (auto& seg : plan.segments) {
|
||||
if (cumulative + seg.input_param_bytes > available) {
|
||||
break;
|
||||
}
|
||||
seg.residency = SegmentResidency::RESIDENT;
|
||||
cumulative += seg.input_param_bytes;
|
||||
if (log_desc != nullptr) {
|
||||
LOG_INFO("%s build cached graph cut plan done (taking %lld ms)",
|
||||
log_desc,
|
||||
ggml_time_ms() - t_plan_begin);
|
||||
}
|
||||
return plans.front();
|
||||
}
|
||||
|
||||
} // namespace sd::ggml_graph_cut
|
||||
|
||||
+23
-45
@@ -3,22 +3,17 @@
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "ggml-backend.h"
|
||||
#include "ggml.h"
|
||||
|
||||
namespace sd::ggml_graph_cut {
|
||||
|
||||
// Streaming residency for a segment's params.
|
||||
enum class SegmentResidency : uint8_t {
|
||||
STREAMED = 0,
|
||||
RESIDENT = 1,
|
||||
};
|
||||
|
||||
struct Segment {
|
||||
enum InputType {
|
||||
INPUT_EXTERNAL = 0,
|
||||
@@ -33,41 +28,33 @@ namespace sd::ggml_graph_cut {
|
||||
int node_index = -1;
|
||||
};
|
||||
|
||||
size_t compute_buffer_size = 0;
|
||||
size_t output_bytes = 0;
|
||||
size_t input_external_bytes = 0;
|
||||
size_t input_previous_cut_bytes = 0;
|
||||
size_t input_param_bytes = 0;
|
||||
size_t compute_buffer_size = 0;
|
||||
std::string group_name;
|
||||
std::vector<int> internal_node_indices;
|
||||
std::vector<int> output_node_indices;
|
||||
std::vector<InputRef> input_refs;
|
||||
SegmentResidency residency = SegmentResidency::STREAMED;
|
||||
std::unordered_set<std::string> future_cut_names;
|
||||
std::unordered_set<std::string> live_cut_names;
|
||||
};
|
||||
|
||||
struct Plan {
|
||||
struct InputShape {
|
||||
int leaf_index = -1;
|
||||
ggml_type type = GGML_TYPE_COUNT;
|
||||
std::array<int64_t, GGML_MAX_DIMS> ne = {0, 0, 0, 0};
|
||||
};
|
||||
|
||||
bool available = false;
|
||||
bool has_cuts = false;
|
||||
bool valid = true;
|
||||
int n_nodes = 0;
|
||||
int n_leafs = 0;
|
||||
std::vector<InputShape> input_shapes;
|
||||
bool available = false;
|
||||
bool has_cuts = false;
|
||||
bool valid = true;
|
||||
size_t compute_buffer_size = 0;
|
||||
std::vector<uint64_t> layout;
|
||||
std::vector<std::string> leaf_names;
|
||||
std::vector<std::pair<int, std::string>> cut_markers;
|
||||
std::vector<Segment> segments;
|
||||
};
|
||||
|
||||
struct PlanCache {
|
||||
Plan graph_cut_plan;
|
||||
Plan budgeted_graph_cut_plan;
|
||||
size_t budgeted_graph_cut_plan_max_vram_bytes = 0;
|
||||
static constexpr size_t MAX_PLANS = 4;
|
||||
std::list<Plan> graph_cut_plans;
|
||||
};
|
||||
|
||||
static constexpr const char* GGML_RUNNER_CUT_PREFIX = "ggml_runner_cut:";
|
||||
static constexpr const char* GGML_RUNNER_CUT_SUFFIX = "|";
|
||||
|
||||
struct MaxVramAssignment {
|
||||
float default_gib = 0.f;
|
||||
@@ -88,13 +75,12 @@ namespace sd::ggml_graph_cut {
|
||||
ggml_backend_buffer_t tensor_buffer(const ggml_tensor* tensor);
|
||||
ggml_tensor* cache_source_tensor(ggml_tensor* tensor);
|
||||
size_t cache_tensor_bytes(const ggml_tensor* tensor);
|
||||
// Plans ignore runtime bindings; allocator reservations must include them.
|
||||
std::vector<uint64_t> graph_layout(ggml_cgraph* graph, bool include_bindings);
|
||||
bool plan_matches_graph(ggml_cgraph* gf, const Plan& plan);
|
||||
ggml_tensor* output_tensor(ggml_cgraph* gf, const Segment& segment, size_t output_index);
|
||||
ggml_tensor* input_tensor(ggml_cgraph* gf, const Segment::InputRef& input_ref);
|
||||
std::vector<ggml_tensor*> param_tensors(ggml_cgraph* gf, const Segment& segment);
|
||||
std::unordered_set<std::string> collect_future_input_names(ggml_cgraph* gf,
|
||||
const Plan& plan,
|
||||
size_t current_segment_index);
|
||||
ggml_cgraph* build_segment_graph(ggml_cgraph* gf,
|
||||
const Segment& segment,
|
||||
ggml_context** graph_ctx_out);
|
||||
@@ -108,21 +94,13 @@ namespace sd::ggml_graph_cut {
|
||||
ggml_cgraph* gf,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set,
|
||||
const char* log_desc);
|
||||
Plan apply_max_vram_budget(ggml_cgraph* gf,
|
||||
const Plan& base_plan,
|
||||
size_t max_graph_vram_bytes,
|
||||
ggml_backend_t backend,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set,
|
||||
const char* log_desc);
|
||||
Plan resolve_plan(ggml_backend_t backend,
|
||||
ggml_cgraph* gf,
|
||||
PlanCache* cache,
|
||||
size_t max_graph_vram_bytes,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set,
|
||||
const char* log_desc);
|
||||
// The returned reference is valid until its cache entry is evicted or the cache is destroyed.
|
||||
const Plan& resolve_plan(ggml_backend_t backend,
|
||||
ggml_cgraph* gf,
|
||||
PlanCache* cache,
|
||||
const std::unordered_set<const ggml_tensor*>& params_tensor_set,
|
||||
const char* log_desc);
|
||||
|
||||
// Mark leading segments resident when they fit after streamed-segment headroom.
|
||||
void annotate_residency(Plan& plan, size_t max_graph_vram_bytes);
|
||||
} // namespace sd::ggml_graph_cut
|
||||
|
||||
#endif // __SD_CORE_GGML_GRAPH_CUT_H__
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,398 @@
|
||||
#ifndef __SD_CORE_GGML_RUNNER_H__
|
||||
#define __SD_CORE_GGML_RUNNER_H__
|
||||
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "core/compute_workspace.h"
|
||||
#include "core/ggml_graph_cut.h"
|
||||
#include "core/runner_cache.h"
|
||||
#include "core/tensor_ggml.hpp"
|
||||
#include "core/util.h"
|
||||
#include "device_residency_manager.h"
|
||||
|
||||
/* SDXL with LoRA requires more space */
|
||||
#define MAX_PARAMS_TENSOR_NUM 32768
|
||||
#define MAX_GRAPH_SIZE 327680
|
||||
|
||||
struct WeightAdapter {
|
||||
struct ForwardParams {
|
||||
enum class op_type_t {
|
||||
OP_LINEAR,
|
||||
OP_CONV2D,
|
||||
} op_type;
|
||||
struct {
|
||||
bool force_prec_f32 = false;
|
||||
float scale = 1.f;
|
||||
} linear;
|
||||
struct conv2d_params_t {
|
||||
int s0 = 1;
|
||||
int s1 = 1;
|
||||
int p0 = 0;
|
||||
int p1 = 0;
|
||||
int d0 = 1;
|
||||
int d1 = 1;
|
||||
bool direct = false;
|
||||
bool circular_x = false;
|
||||
bool circular_y = false;
|
||||
float scale = 1.f;
|
||||
} conv2d;
|
||||
};
|
||||
virtual ggml_tensor* patch_weight(ggml_context* ctx, ggml_backend_t backend, ggml_tensor* weight, const std::string& weight_name) = 0;
|
||||
virtual ggml_tensor* forward_with_lora(ggml_context* ctx,
|
||||
ggml_backend_t backend,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* b,
|
||||
const std::string& prefix,
|
||||
ForwardParams forward_params) = 0;
|
||||
virtual ggml_tensor* add_lora_to_output(ggml_context* ctx,
|
||||
ggml_backend_t backend,
|
||||
ggml_tensor* x,
|
||||
ggml_tensor* w,
|
||||
ggml_tensor* output,
|
||||
const std::string& prefix,
|
||||
ForwardParams forward_params) = 0;
|
||||
virtual size_t get_extra_graph_size() = 0;
|
||||
};
|
||||
|
||||
struct GGMLRunnerContext {
|
||||
ggml_backend_t backend = nullptr;
|
||||
ggml_context* ggml_ctx = nullptr;
|
||||
bool flash_attn_enabled = false;
|
||||
bool sage_attn_enabled = false;
|
||||
bool sol_attn_enabled = false;
|
||||
float sol_attn_tau = 1.f;
|
||||
float linear_scale = 0.f;
|
||||
float attn_scale = 0.f;
|
||||
bool conv2d_direct_enabled = false;
|
||||
bool conv3d_direct_enabled = false;
|
||||
bool circular_x_enabled = false;
|
||||
bool circular_y_enabled = false;
|
||||
ggml_tensor* ip_context = nullptr;
|
||||
float ip_scale = 1.0f;
|
||||
std::shared_ptr<WeightAdapter> weight_adapter = nullptr;
|
||||
std::vector<std::pair<ggml_tensor*, std::string>>* debug_tensors = nullptr;
|
||||
std::function<ggml_tensor*(const std::string&)> get_cache_tensor;
|
||||
std::function<void(const std::string&, ggml_tensor*)> cache_tensor;
|
||||
std::function<void(ggml_tensor*, const void*)> set_backend_tensor_data;
|
||||
std::map<std::pair<ggml_tensor*, int>, ggml_tensor*> int8_convrot_cache;
|
||||
|
||||
void capture_tensor(const std::string& name, ggml_tensor* tensor) {
|
||||
if (debug_tensors == nullptr || tensor == nullptr) {
|
||||
return;
|
||||
}
|
||||
ggml_tensor* snapshot = ggml_cont(ggml_ctx, tensor);
|
||||
ggml_tensor* dst = ggml_dup_tensor(ggml_ctx, snapshot);
|
||||
snapshot = ggml_cpy(ggml_ctx, snapshot, dst);
|
||||
ggml_set_output(snapshot);
|
||||
debug_tensors->push_back({snapshot, name});
|
||||
}
|
||||
|
||||
ggml_tensor* load_cache_tensor(const std::string& name) const {
|
||||
if (!get_cache_tensor) {
|
||||
return nullptr;
|
||||
}
|
||||
return get_cache_tensor(name);
|
||||
}
|
||||
|
||||
void persist_cache_tensor(const std::string& name, ggml_tensor* tensor) const {
|
||||
if (!cache_tensor || tensor == nullptr) {
|
||||
return;
|
||||
}
|
||||
cache_tensor(name, tensor);
|
||||
}
|
||||
|
||||
void bind_backend_tensor_data(ggml_tensor* tensor, const void* data) const {
|
||||
if (!set_backend_tensor_data || tensor == nullptr || data == nullptr) {
|
||||
return;
|
||||
}
|
||||
set_backend_tensor_data(tensor, data);
|
||||
}
|
||||
};
|
||||
|
||||
ggml_tensor* ggml_ext_attention_ext(GGMLRunnerContext* ctx,
|
||||
ggml_tensor* q,
|
||||
ggml_tensor* k,
|
||||
ggml_tensor* v,
|
||||
int64_t n_head,
|
||||
ggml_tensor* mask = nullptr,
|
||||
bool skip_reshape = false,
|
||||
bool flash_attn = false,
|
||||
float kv_scale = 1.f);
|
||||
|
||||
struct GGMLRunner {
|
||||
private:
|
||||
std::map<ggml_backend_t, size_t> logged_compute_bytes_;
|
||||
size_t logged_segment_count_ = 0;
|
||||
|
||||
sd::ComputeWorkspace::Measurement measure(ggml_cgraph* graph, size_t direct_bytes);
|
||||
std::vector<DeviceMemoryRequest> memory_requests(const std::vector<sd::BackendBufferSize>& sizes,
|
||||
size_t pending_cache_bytes) const;
|
||||
bool fits(const std::vector<DeviceMemoryRequest>& requests,
|
||||
const std::vector<ggml_tensor*>& params) const;
|
||||
bool execute_segment(ggml_cgraph* graph, int n_threads);
|
||||
std::optional<sd::Tensor<float>> execute_graph(ggml_cgraph* graph, int n_threads, bool no_return, const std::function<bool()>& read_outputs);
|
||||
|
||||
protected:
|
||||
typedef std::function<ggml_cgraph*()> get_graph_cb_t;
|
||||
using GraphCutPlan = sd::ggml_graph_cut::Plan;
|
||||
|
||||
ggml_backend_t runtime_backend = nullptr;
|
||||
|
||||
ggml_context* params_ctx = nullptr;
|
||||
|
||||
sd::RunnerCache cache_;
|
||||
sd::GraphCutTensorCache cut_cache_;
|
||||
sd::ComputeWorkspace workspace_;
|
||||
ggml_context* compute_ctx = nullptr;
|
||||
bool runner_started_ = false;
|
||||
bool graph_active_ = false;
|
||||
|
||||
size_t max_graph_vram_bytes = 0;
|
||||
bool graph_cut_layer_split_enabled = false;
|
||||
std::vector<size_t> graph_cut_layer_split_backend_vram_limits_;
|
||||
|
||||
std::vector<ggml_backend_t> extra_runtime_backends; // borrowed (SDBackendManager-owned)
|
||||
bool multi_device_eval_callback_warned = false;
|
||||
|
||||
std::shared_ptr<WeightAdapter> weight_adapter = nullptr;
|
||||
std::weak_ptr<DeviceResidencyManager> residency_manager;
|
||||
bool params_tensor_set_dirty_ = true;
|
||||
|
||||
std::vector<float> one_vec = {1.f};
|
||||
ggml_tensor* one_tensor = nullptr;
|
||||
|
||||
std::vector<int> zero_int_vec = {0};
|
||||
ggml_tensor* zero_int_tensor = nullptr;
|
||||
|
||||
std::map<ggml_tensor*, const void*> backend_tensor_data_map;
|
||||
std::vector<std::pair<ggml_tensor*, std::string>> debug_tensors;
|
||||
const std::string final_result_name = "ggml_runner_final_result_tensor";
|
||||
|
||||
bool flash_attn_enabled = false;
|
||||
bool sage_attn_enabled = false;
|
||||
bool sol_attn_enabled = false;
|
||||
float sol_attn_tau = 1.f;
|
||||
bool sol_attn_graph_logged = false;
|
||||
float linear_scale = 0.f;
|
||||
float attn_scale = 0.f;
|
||||
bool conv2d_direct_enabled = false;
|
||||
bool conv3d_direct_enabled = false;
|
||||
bool circular_x_enabled = false;
|
||||
bool circular_y_enabled = false;
|
||||
|
||||
sd::ggml_graph_cut::PlanCache graph_cut_plan_cache_;
|
||||
std::unordered_set<const ggml_tensor*> params_tensor_set_;
|
||||
std::unordered_map<const ggml_tensor*, ggml_backend_t> graph_cut_layer_split_assignments_;
|
||||
std::unordered_map<const ggml_tensor*, ggml_backend_t> graph_cut_layer_split_node_assignments_;
|
||||
bool graph_cut_layer_split_primary_notice_logged_ = false;
|
||||
|
||||
template <typename T>
|
||||
static sd::Tensor<T> take_or_empty(std::optional<sd::Tensor<T>> tensor) {
|
||||
if (!tensor.has_value()) {
|
||||
return {};
|
||||
}
|
||||
return std::move(*tensor);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static sd::Tensor<T> restore_trailing_singleton_dims(std::optional<sd::Tensor<T>> tensor,
|
||||
size_t expected_dim) {
|
||||
return restore_trailing_singleton_dims(take_or_empty(std::move(tensor)), expected_dim);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static sd::Tensor<T> restore_trailing_singleton_dims(sd::Tensor<T> tensor,
|
||||
size_t expected_dim) {
|
||||
if (tensor.empty()) {
|
||||
return tensor;
|
||||
}
|
||||
while (static_cast<size_t>(tensor.dim()) < expected_dim) {
|
||||
tensor.unsqueeze_(tensor.dim());
|
||||
}
|
||||
return tensor;
|
||||
}
|
||||
|
||||
void alloc_params_ctx();
|
||||
|
||||
void free_params_ctx();
|
||||
|
||||
void alloc_compute_ctx();
|
||||
|
||||
void free_compute_ctx();
|
||||
|
||||
void rebuild_params_tensor_set();
|
||||
|
||||
ggml_tensor* canonical_param_tensor(ggml_tensor* tensor);
|
||||
|
||||
std::vector<ggml_tensor*> collect_used_param_tensors(ggml_cgraph* gf);
|
||||
|
||||
void evict_compute_backend_param_tensors(const std::vector<ggml_tensor*>& tensors);
|
||||
|
||||
void prepare_build_in_tensor_before();
|
||||
|
||||
void prepare_build_in_tensor_after(ggml_cgraph* gf);
|
||||
|
||||
ggml_cgraph* new_graph_custom(size_t graph_size);
|
||||
|
||||
ggml_cgraph* get_compute_graph(get_graph_cb_t get_graph);
|
||||
|
||||
bool prepare_compute_graph(get_graph_cb_t get_graph,
|
||||
ggml_cgraph** gf_out);
|
||||
|
||||
ggml_backend_t backend_for_weight(const ggml_tensor* tensor) const;
|
||||
|
||||
// Weightless ops have no scheduler anchor, so pin them to the most recent
|
||||
// weight device. Views must stay unpinned or cross-device copies can be
|
||||
// skipped for their consumers.
|
||||
void pin_multi_device_nodes(ggml_backend_sched_t sched, ggml_cgraph* gf, ggml_cgraph* original_graph = nullptr);
|
||||
|
||||
bool is_multi_device() const {
|
||||
return !extra_runtime_backends.empty();
|
||||
}
|
||||
|
||||
size_t reusable_compute_buffer_bytes() const {
|
||||
return workspace_.bytes(runtime_backend);
|
||||
}
|
||||
|
||||
size_t retained_runtime_buffer_bytes(ggml_backend_t backend = nullptr) const;
|
||||
|
||||
void sync_runtime_residency();
|
||||
|
||||
std::optional<sd::Tensor<float>> read_graph_tensor(ggml_tensor* tensor, const char* label);
|
||||
|
||||
void copy_data_to_backend_tensor(ggml_cgraph* gf, bool clear_after_copy = true);
|
||||
|
||||
const GraphCutPlan& resolve_graph_cut_plan(ggml_cgraph* gf);
|
||||
|
||||
const GraphCutPlan& resolve_graph_cut_layer_split_plan(ggml_cgraph* gf);
|
||||
|
||||
bool assign_graph_cut_layer_split_backends(ggml_cgraph* gf);
|
||||
|
||||
public:
|
||||
bool runner_start();
|
||||
|
||||
bool runner_started() const { return runner_started_; }
|
||||
|
||||
void runner_end();
|
||||
|
||||
public:
|
||||
virtual std::string get_desc() = 0;
|
||||
|
||||
GGMLRunner(ggml_backend_t backend,
|
||||
std::shared_ptr<DeviceResidencyManager> manager = nullptr);
|
||||
|
||||
virtual ~GGMLRunner();
|
||||
|
||||
virtual GGMLRunnerContext get_context();
|
||||
|
||||
void reset_compute_ctx();
|
||||
|
||||
public:
|
||||
void free_cache_ctx_and_buffer();
|
||||
|
||||
// do copy after alloc graph
|
||||
void set_backend_tensor_data(ggml_tensor* tensor, const void* data);
|
||||
|
||||
template <typename T>
|
||||
ggml_tensor* make_input(const sd::Tensor<T>& tensor) {
|
||||
ggml_tensor* input = sd::make_ggml_tensor(compute_ctx, tensor, false);
|
||||
set_backend_tensor_data(input, tensor.data());
|
||||
return input;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
ggml_tensor* make_optional_input(const sd::Tensor<T>& tensor) {
|
||||
if (tensor.empty()) {
|
||||
return nullptr;
|
||||
}
|
||||
return make_input(tensor);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
ggml_tensor* make_optional_input(const sd::Tensor<T>* tensor) {
|
||||
if (tensor == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return make_input(*tensor);
|
||||
}
|
||||
|
||||
ggml_tensor* to_backend(ggml_tensor* tensor);
|
||||
|
||||
void cache(const std::string name, ggml_tensor* tensor);
|
||||
|
||||
ggml_tensor* get_cache_tensor_by_name(const std::string& name) {
|
||||
return cache_.get(name);
|
||||
}
|
||||
|
||||
std::optional<sd::Tensor<float>> compute(get_graph_cb_t get_graph,
|
||||
int n_threads,
|
||||
bool auto_runner_end = true,
|
||||
bool no_return = false,
|
||||
const std::function<bool()>& read_outputs = {});
|
||||
|
||||
void set_flash_attention_enabled(bool enabled) {
|
||||
flash_attn_enabled = enabled;
|
||||
}
|
||||
|
||||
void set_sage_attention_enabled(bool enabled) {
|
||||
if (sage_attn_enabled != enabled) {
|
||||
free_cache_ctx_and_buffer();
|
||||
graph_cut_plan_cache_.graph_cut_plans.clear();
|
||||
sage_attn_enabled = enabled;
|
||||
}
|
||||
}
|
||||
|
||||
void set_sol_attention_enabled(bool enabled, float tau) {
|
||||
if (sol_attn_enabled != enabled || sol_attn_tau != tau) {
|
||||
free_cache_ctx_and_buffer();
|
||||
graph_cut_plan_cache_.graph_cut_plans.clear();
|
||||
sol_attn_enabled = enabled;
|
||||
sol_attn_tau = tau;
|
||||
sol_attn_graph_logged = false;
|
||||
}
|
||||
}
|
||||
|
||||
void set_scale_overrides(float linear_scale, float attn_scale) {
|
||||
this->linear_scale = linear_scale;
|
||||
this->attn_scale = attn_scale;
|
||||
}
|
||||
|
||||
void set_conv2d_direct_enabled(bool enabled) {
|
||||
conv2d_direct_enabled = enabled;
|
||||
}
|
||||
|
||||
void set_conv3d_direct_enabled(bool enabled) {
|
||||
conv3d_direct_enabled = enabled;
|
||||
}
|
||||
|
||||
void set_circular_axes(bool circular_x, bool circular_y) {
|
||||
circular_x_enabled = circular_x;
|
||||
circular_y_enabled = circular_y;
|
||||
}
|
||||
|
||||
void set_weight_adapter(const std::shared_ptr<WeightAdapter>& adapter) {
|
||||
weight_adapter = adapter;
|
||||
}
|
||||
|
||||
void set_max_graph_vram_bytes(size_t max_vram_bytes) {
|
||||
max_graph_vram_bytes = max_vram_bytes;
|
||||
}
|
||||
|
||||
void set_graph_cut_layer_split_enabled(bool enabled);
|
||||
|
||||
void set_graph_cut_layer_split_backend_vram_limits(const std::vector<size_t>& limits);
|
||||
|
||||
void set_runtime_backends(const std::vector<ggml_backend_t>& backends);
|
||||
};
|
||||
|
||||
#endif // __SD_CORE_GGML_RUNNER_H__
|
||||
@@ -0,0 +1,428 @@
|
||||
#include "core/ggml_tensor_utils.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
|
||||
#include "core/ggml_extend_backend.h"
|
||||
#include "core/rng.hpp"
|
||||
|
||||
void ggml_ext_im_set_randn_f32(ggml_tensor* tensor, std::shared_ptr<RNG> rng) {
|
||||
uint32_t n = (uint32_t)ggml_nelements(tensor);
|
||||
std::vector<float> random_numbers = rng->randn(n);
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
ggml_ext_im_set_f32_1d(tensor, i, random_numbers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void print_ggml_tensor(ggml_tensor* tensor, bool shape_only, const char* mark) {
|
||||
printf("%s (%s): shape(%zu, %zu, %zu, %zu)\n", mark, ggml_type_name(tensor->type), tensor->ne[0], tensor->ne[1], tensor->ne[2], tensor->ne[3]);
|
||||
fflush(stdout);
|
||||
if (shape_only) {
|
||||
return;
|
||||
}
|
||||
int range = 3;
|
||||
for (int i3 = 0; i3 < tensor->ne[3]; i3++) {
|
||||
if (i3 >= range && i3 + range < tensor->ne[3]) {
|
||||
continue;
|
||||
}
|
||||
for (int i2 = 0; i2 < tensor->ne[2]; i2++) {
|
||||
if (i2 >= range && i2 + range < tensor->ne[2]) {
|
||||
continue;
|
||||
}
|
||||
for (int i1 = 0; i1 < tensor->ne[1]; i1++) {
|
||||
if (i1 >= range && i1 + range < tensor->ne[1]) {
|
||||
continue;
|
||||
}
|
||||
for (int i0 = 0; i0 < tensor->ne[0]; i0++) {
|
||||
if (i0 >= range && i0 + range < tensor->ne[0]) {
|
||||
continue;
|
||||
}
|
||||
if (tensor->type == GGML_TYPE_F32) {
|
||||
printf(" [%d, %d, %d, %d] = %f\n", i3, i2, i1, i0, ggml_ext_tensor_get_f32(tensor, i0, i1, i2, i3));
|
||||
} else if (tensor->type == GGML_TYPE_F16) {
|
||||
printf(" [%d, %d, %d, %d] = %f\n", i3, i2, i1, i0, ggml_fp16_to_fp32(ggml_ext_tensor_get_f16(tensor, i0, i1, i2, i3)));
|
||||
} else if (tensor->type == GGML_TYPE_I32) {
|
||||
printf(" [%d, %d, %d, %d] = %i3\n", i3, i2, i1, i0, ggml_ext_tensor_get_i32(tensor, i0, i1, i2, i3));
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_ext_tensor_iter(
|
||||
ggml_tensor* tensor,
|
||||
const std::function<void(ggml_tensor*, int64_t, int64_t, int64_t, int64_t)>& fn) {
|
||||
int64_t n0 = tensor->ne[0];
|
||||
int64_t n1 = tensor->ne[1];
|
||||
int64_t n2 = tensor->ne[2];
|
||||
int64_t n3 = tensor->ne[3];
|
||||
|
||||
for (int64_t i3 = 0; i3 < n3; i3++) {
|
||||
for (int64_t i2 = 0; i2 < n2; i2++) {
|
||||
for (int64_t i1 = 0; i1 < n1; i1++) {
|
||||
for (int64_t i0 = 0; i0 < n0; i0++) {
|
||||
fn(tensor, i0, i1, i2, i3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_ext_tensor_iter(
|
||||
ggml_tensor* tensor,
|
||||
const std::function<void(ggml_tensor*, int64_t)>& fn) {
|
||||
int64_t n0 = tensor->ne[0];
|
||||
int64_t n1 = tensor->ne[1];
|
||||
int64_t n2 = tensor->ne[2];
|
||||
int64_t n3 = tensor->ne[3];
|
||||
|
||||
for (int64_t i = 0; i < ggml_nelements(tensor); i++) {
|
||||
fn(tensor, i);
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_ext_tensor_diff(
|
||||
ggml_tensor* a,
|
||||
ggml_tensor* b,
|
||||
float gap) {
|
||||
GGML_ASSERT(ggml_nelements(a) == ggml_nelements(b));
|
||||
ggml_ext_tensor_iter(a, [&](ggml_tensor* a, int64_t i0, int64_t i1, int64_t i2, int64_t i3) {
|
||||
float a_value = ggml_ext_tensor_get_f32(a, i0, i1, i2, i3);
|
||||
float b_value = ggml_ext_tensor_get_f32(b, i0, i1, i2, i3);
|
||||
if (abs(a_value - b_value) > gap) {
|
||||
LOG_WARN("[%ld, %ld, %ld, %ld] %f %f", i3, i2, i1, i0, a_value, b_value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
ggml_tensor* load_tensor_from_file(ggml_context* ctx, const std::string& file_path) {
|
||||
std::ifstream file(file_path, std::ios::binary);
|
||||
if (!file.is_open()) {
|
||||
LOG_ERROR("failed to open '%s'", file_path.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
int32_t n_dims;
|
||||
int32_t length;
|
||||
int32_t ttype;
|
||||
|
||||
file.read(reinterpret_cast<char*>(&n_dims), sizeof(n_dims));
|
||||
file.read(reinterpret_cast<char*>(&length), sizeof(length));
|
||||
file.read(reinterpret_cast<char*>(&ttype), sizeof(ttype));
|
||||
|
||||
LOG_VERBOSE("load_tensor_from_file %d %d %d", n_dims, length, ttype);
|
||||
|
||||
if (file.eof()) {
|
||||
LOG_ERROR("incomplete file '%s'", file_path.c_str());
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int32_t nelements = 1;
|
||||
int32_t ne[4] = {1, 1, 1, 1};
|
||||
for (int i = 0; i < n_dims; ++i) {
|
||||
file.read(reinterpret_cast<char*>(&ne[i]), sizeof(ne[i]));
|
||||
nelements *= ne[i];
|
||||
}
|
||||
std::string name(length, 0);
|
||||
file.read(&name[0], length);
|
||||
ggml_tensor* tensor = ggml_new_tensor_4d(ctx, (ggml_type)ttype, ne[0], ne[1], ne[2], ne[3]);
|
||||
const size_t bpe = ggml_type_size(ggml_type(ttype));
|
||||
file.read(reinterpret_cast<char*>(tensor->data), ggml_nbytes(tensor));
|
||||
return tensor;
|
||||
}
|
||||
|
||||
// __STATIC_INLINE__ void save_tensor_to_file(const std::string& file_name, ggml_tensor* tensor, const std::string & name) {
|
||||
// std::string file_name_ = file_name + ".tensor";
|
||||
// std::string name_ = name;
|
||||
// std::ofstream file("./" + file_name_, std::ios::binary);
|
||||
// file.write(reinterpret_cast<char*>(&tensor->n_dims), sizeof(tensor->n_dims));
|
||||
// int len = (int)name_.size();
|
||||
// file.write(reinterpret_cast<char*>(&len), sizeof(len));
|
||||
// int ttype = (int)tensor->type;
|
||||
// file.write(reinterpret_cast<char*>(&ttype), sizeof(ttype));
|
||||
// for (int i = 0; i < tensor->n_dims; ++i) {
|
||||
// int ne_ = (int) tensor->ne[i];
|
||||
// file.write(reinterpret_cast<char*>(&ne_), sizeof(ne_));
|
||||
// }
|
||||
// file.write(&name_[0], len);
|
||||
// char* data = nullptr;
|
||||
// file.write((char*)tensor->data, ggml_nbytes(tensor));
|
||||
// file.close();
|
||||
// }
|
||||
|
||||
uint8_t* ggml_tensor_to_sd_image(ggml_tensor* input, uint8_t* image_data) {
|
||||
int64_t width = input->ne[0];
|
||||
int64_t height = input->ne[1];
|
||||
int64_t channels = input->ne[2];
|
||||
GGML_ASSERT(input->type == GGML_TYPE_F32);
|
||||
if (image_data == nullptr) {
|
||||
image_data = (uint8_t*)malloc(width * height * channels);
|
||||
}
|
||||
for (int iy = 0; iy < height; iy++) {
|
||||
for (int ix = 0; ix < width; ix++) {
|
||||
for (int k = 0; k < channels; k++) {
|
||||
float value = ggml_ext_tensor_get_f32(input, ix, iy, k);
|
||||
*(image_data + iy * width * channels + ix * channels + k) = (uint8_t)(value * 255.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
return image_data;
|
||||
}
|
||||
|
||||
uint8_t* ggml_tensor_to_sd_image(ggml_tensor* input, int idx, bool video) {
|
||||
int64_t width = input->ne[0];
|
||||
int64_t height = input->ne[1];
|
||||
int64_t channels;
|
||||
if (video) {
|
||||
channels = input->ne[3];
|
||||
} else {
|
||||
channels = input->ne[2];
|
||||
}
|
||||
GGML_ASSERT(channels == 3 && input->type == GGML_TYPE_F32);
|
||||
uint8_t* image_data = (uint8_t*)malloc(width * height * channels);
|
||||
for (int ih = 0; ih < height; ih++) {
|
||||
for (int iw = 0; iw < width; iw++) {
|
||||
for (int ic = 0; ic < channels; ic++) {
|
||||
float value;
|
||||
if (video) {
|
||||
value = ggml_ext_tensor_get_f32(input, iw, ih, idx, ic);
|
||||
} else {
|
||||
value = ggml_ext_tensor_get_f32(input, iw, ih, ic, idx);
|
||||
}
|
||||
*(image_data + ih * width * channels + iw * channels + ic) = (uint8_t)(value * 255.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
return image_data;
|
||||
}
|
||||
|
||||
void sd_image_to_ggml_tensor(sd_image_t image,
|
||||
ggml_tensor* tensor,
|
||||
bool scale) {
|
||||
GGML_ASSERT(image.width == tensor->ne[0]);
|
||||
GGML_ASSERT(image.height == tensor->ne[1]);
|
||||
GGML_ASSERT(image.channel == tensor->ne[2]);
|
||||
GGML_ASSERT(1 == tensor->ne[3]);
|
||||
GGML_ASSERT(tensor->type == GGML_TYPE_F32);
|
||||
ggml_ext_tensor_iter(tensor, [&](ggml_tensor* tensor, int64_t i0, int64_t i1, int64_t i2, int64_t i3) {
|
||||
float value = sd_image_get_f32(image, i0, i1, i2, scale);
|
||||
ggml_ext_tensor_set_f32(tensor, value, i0, i1, i2, i3);
|
||||
});
|
||||
}
|
||||
|
||||
void ggml_ext_tensor_apply_mask(ggml_tensor* image_data,
|
||||
ggml_tensor* mask,
|
||||
ggml_tensor* output,
|
||||
float masked_value) {
|
||||
int64_t width = output->ne[0];
|
||||
int64_t height = output->ne[1];
|
||||
int64_t channels = output->ne[2];
|
||||
float rescale_mx = 1.f * mask->ne[0] / output->ne[0];
|
||||
float rescale_my = 1.f * mask->ne[1] / output->ne[1];
|
||||
GGML_ASSERT(output->type == GGML_TYPE_F32);
|
||||
for (int ix = 0; ix < width; ix++) {
|
||||
for (int iy = 0; iy < height; iy++) {
|
||||
int mx = (int)(ix * rescale_mx);
|
||||
int my = (int)(iy * rescale_my);
|
||||
float m = ggml_ext_tensor_get_f32(mask, mx, my);
|
||||
m = round(m); // inpaint models need binary masks
|
||||
ggml_ext_tensor_set_f32(mask, m, mx, my);
|
||||
for (int k = 0; k < channels; k++) {
|
||||
float value = ggml_ext_tensor_get_f32(image_data, ix, iy, k);
|
||||
value = (1 - m) * (value - masked_value) + masked_value;
|
||||
ggml_ext_tensor_set_f32(output, value, ix, iy, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
float ggml_ext_tensor_mean(ggml_tensor* src) {
|
||||
float mean = 0.0f;
|
||||
int64_t nelements = ggml_nelements(src);
|
||||
float* data = (float*)src->data;
|
||||
for (int i = 0; i < nelements; i++) {
|
||||
mean += data[i] / nelements * 1.0f;
|
||||
}
|
||||
return mean;
|
||||
}
|
||||
|
||||
void ggml_ext_tensor_add_inplace(ggml_tensor* a, ggml_tensor* b) {
|
||||
GGML_ASSERT(ggml_nelements(a) == ggml_nelements(b));
|
||||
int64_t nelements = ggml_nelements(a);
|
||||
float* vec_a = (float*)a->data;
|
||||
float* vec_b = (float*)b->data;
|
||||
for (int i = 0; i < nelements; i++) {
|
||||
vec_a[i] = vec_a[i] + vec_b[i];
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_ext_tensor_scale_inplace(ggml_tensor* src, float scale) {
|
||||
int64_t nelements = ggml_nelements(src);
|
||||
float* data = (float*)src->data;
|
||||
for (int i = 0; i < nelements; i++) {
|
||||
data[i] = data[i] * scale;
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_ext_tensor_clamp_inplace(ggml_tensor* src, float min, float max) {
|
||||
int64_t nelements = ggml_nelements(src);
|
||||
float* data = (float*)src->data;
|
||||
for (int i = 0; i < nelements; i++) {
|
||||
float val = data[i];
|
||||
data[i] = val < min ? min : (val > max ? max : val);
|
||||
}
|
||||
}
|
||||
|
||||
ggml_tensor* ggml_ext_tensor_concat(ggml_context* ctx,
|
||||
ggml_tensor* a,
|
||||
ggml_tensor* b,
|
||||
int dim) {
|
||||
int64_t ne[GGML_MAX_DIMS];
|
||||
for (int d = 0; d < GGML_MAX_DIMS; ++d) {
|
||||
if (d == dim) {
|
||||
ne[d] = a->ne[d] + b->ne[d];
|
||||
continue;
|
||||
}
|
||||
GGML_ASSERT(a->ne[d] == b->ne[d]);
|
||||
ne[d] = a->ne[d];
|
||||
}
|
||||
ggml_tensor* result = ggml_new_tensor(ctx, a->type, GGML_MAX_DIMS, ne);
|
||||
int64_t o[4] = {0, 0, 0, 0};
|
||||
o[dim] = a->ne[dim];
|
||||
|
||||
float v;
|
||||
for (int i3 = 0; i3 < result->ne[3]; i3++) {
|
||||
for (int i2 = 0; i2 < result->ne[2]; i2++) {
|
||||
for (int i1 = 0; i1 < result->ne[1]; i1++) {
|
||||
for (int i0 = 0; i0 < result->ne[0]; i0++) {
|
||||
if (i0 < a->ne[0] && i1 < a->ne[1] && i2 < a->ne[2] && i3 < a->ne[3]) {
|
||||
v = ggml_ext_tensor_get_f32(a, i0, i1, i2, i3);
|
||||
} else {
|
||||
v = ggml_ext_tensor_get_f32(b, i0 - o[0], i1 - o[1], i2 - o[2], i3 - o[3]);
|
||||
}
|
||||
|
||||
ggml_ext_tensor_set_f32(result, v, i0, i1, i2, i3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void scale_to_minus1_1(ggml_tensor* src) {
|
||||
int64_t nelements = ggml_nelements(src);
|
||||
float* data = (float*)src->data;
|
||||
for (int i = 0; i < nelements; i++) {
|
||||
float val = data[i];
|
||||
data[i] = val * 2.0f - 1.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void scale_to_0_1(ggml_tensor* src) {
|
||||
int64_t nelements = ggml_nelements(src);
|
||||
float* data = (float*)src->data;
|
||||
for (int i = 0; i < nelements; i++) {
|
||||
float val = data[i];
|
||||
data[i] = (val + 1.0f) * 0.5f;
|
||||
}
|
||||
}
|
||||
|
||||
ggml_tensor* vector_to_ggml_tensor(ggml_context* ctx,
|
||||
const std::vector<float>& vec) {
|
||||
ggml_tensor* t = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, vec.size());
|
||||
memcpy(t->data, (const void*)vec.data(), ggml_nbytes(t));
|
||||
return t;
|
||||
}
|
||||
|
||||
ggml_tensor* vector_to_ggml_tensor_i32(ggml_context* ctx,
|
||||
const std::vector<int>& vec) {
|
||||
ggml_tensor* t = ggml_new_tensor_1d(ctx, GGML_TYPE_I32, vec.size());
|
||||
memcpy(t->data, (const void*)vec.data(), ggml_nbytes(t));
|
||||
return t;
|
||||
}
|
||||
|
||||
std::vector<float> arange(float start, float end, float step) {
|
||||
std::vector<float> result;
|
||||
|
||||
for (float value = start; value < end; value += step) {
|
||||
result.push_back(value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<float> timestep_embedding(std::vector<float> timesteps,
|
||||
int dim,
|
||||
int max_period,
|
||||
bool flip_sin_to_cos,
|
||||
float scale) {
|
||||
// timesteps: [N,]
|
||||
// embedding: [N, dim]
|
||||
size_t N = timesteps.size();
|
||||
std::vector<float> embedding(N * dim, 0.f);
|
||||
int half = dim / 2;
|
||||
std::vector<float> freqs(half);
|
||||
for (int i = 0; i < half; ++i) {
|
||||
freqs[i] = (float)std::exp(-std::log(max_period) * i / half);
|
||||
}
|
||||
for (int i = 0; i < N; ++i) {
|
||||
for (int j = 0; j < half; ++j) {
|
||||
float arg = timesteps[i] * freqs[j] * scale;
|
||||
if (flip_sin_to_cos) {
|
||||
embedding[i * dim + j] = std::cos(arg);
|
||||
embedding[i * dim + j + half] = std::sin(arg);
|
||||
} else {
|
||||
embedding[i * dim + j] = std::sin(arg);
|
||||
embedding[i * dim + j + half] = std::cos(arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
return embedding;
|
||||
}
|
||||
|
||||
void set_timestep_embedding(std::vector<float> timesteps,
|
||||
ggml_tensor* embedding,
|
||||
int dim,
|
||||
int max_period) {
|
||||
std::vector<float> embedding_vec = timestep_embedding(timesteps, dim, max_period);
|
||||
memcpy(((char*)embedding->data), ((char*)embedding_vec.data()), ggml_nbytes(embedding));
|
||||
}
|
||||
|
||||
void set_timestep_embedding(std::vector<float> timesteps,
|
||||
sd::Tensor<float>* embedding,
|
||||
int dim,
|
||||
int max_period) {
|
||||
GGML_ASSERT(embedding != nullptr);
|
||||
std::vector<float> embedding_vec = timestep_embedding(timesteps, dim, max_period);
|
||||
if (embedding->numel() != static_cast<int64_t>(embedding_vec.size())) {
|
||||
embedding->resize({dim, static_cast<int64_t>(timesteps.size())});
|
||||
}
|
||||
std::copy(embedding_vec.begin(), embedding_vec.end(), embedding->values().begin());
|
||||
}
|
||||
|
||||
ggml_tensor* new_timestep_embedding(ggml_context* ctx,
|
||||
std::vector<float> timesteps,
|
||||
int dim,
|
||||
int max_period) {
|
||||
// timesteps: [N,]
|
||||
// embedding: [N, dim]
|
||||
std::vector<float> embedding_vec = timestep_embedding(timesteps, dim, max_period);
|
||||
ggml_tensor* embedding = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, dim, timesteps.size());
|
||||
if (embedding->data != nullptr) {
|
||||
memcpy(((char*)embedding->data), ((char*)embedding_vec.data()), ggml_nbytes(embedding));
|
||||
} else {
|
||||
ggml_backend_tensor_set(embedding, embedding_vec.data(), 0, ggml_nbytes(embedding));
|
||||
}
|
||||
return embedding;
|
||||
}
|
||||
|
||||
size_t ggml_tensor_num(ggml_context* ctx) {
|
||||
size_t num = 0;
|
||||
for (ggml_tensor* t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) {
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
#ifndef __SD_CORE_GGML_TENSOR_UTILS_H__
|
||||
#define __SD_CORE_GGML_TENSOR_UTILS_H__
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "core/tensor.hpp"
|
||||
#include "core/util.h"
|
||||
#include "ggml-backend.h"
|
||||
#include "ggml.h"
|
||||
#include "stable-diffusion.h"
|
||||
|
||||
class RNG;
|
||||
|
||||
__STATIC_INLINE__ int align_up_offset(int n, int multiple) {
|
||||
return (multiple - n % multiple) % multiple;
|
||||
}
|
||||
|
||||
__STATIC_INLINE__ int align_up(int n, int multiple) {
|
||||
return n + align_up_offset(n, multiple);
|
||||
}
|
||||
|
||||
void ggml_ext_im_set_randn_f32(ggml_tensor* tensor, std::shared_ptr<RNG> rng);
|
||||
|
||||
__STATIC_INLINE__ void ggml_ext_tensor_set_f32(ggml_tensor* tensor, float value, int64_t i0, int64_t i1 = 0, int64_t i2 = 0, int64_t i3 = 0) {
|
||||
GGML_ASSERT(tensor->nb[0] == sizeof(float));
|
||||
*(float*)((char*)(tensor->data) + i3 * tensor->nb[3] + i2 * tensor->nb[2] + i1 * tensor->nb[1] + i0 * tensor->nb[0]) = value;
|
||||
}
|
||||
|
||||
__STATIC_INLINE__ float ggml_ext_tensor_get_f32(const ggml_tensor* tensor, int64_t i0, int64_t i1 = 0, int64_t i2 = 0, int64_t i3 = 0) {
|
||||
if (tensor->buffer != nullptr) {
|
||||
float value;
|
||||
ggml_backend_tensor_get(tensor, &value, i3 * tensor->nb[3] + i2 * tensor->nb[2] + i1 * tensor->nb[1] + i0 * tensor->nb[0], sizeof(float));
|
||||
return value;
|
||||
}
|
||||
GGML_ASSERT(tensor->nb[0] == sizeof(float));
|
||||
return *(float*)((char*)(tensor->data) + i3 * tensor->nb[3] + i2 * tensor->nb[2] + i1 * tensor->nb[1] + i0 * tensor->nb[0]);
|
||||
}
|
||||
|
||||
__STATIC_INLINE__ int ggml_ext_tensor_get_i32(const ggml_tensor* tensor, int64_t i0, int64_t i1 = 0, int64_t i2 = 0, int64_t i3 = 0) {
|
||||
if (tensor->buffer != nullptr) {
|
||||
int value;
|
||||
ggml_backend_tensor_get(tensor, &value, i3 * tensor->nb[3] + i2 * tensor->nb[2] + i1 * tensor->nb[1] + i0 * tensor->nb[0], sizeof(int));
|
||||
return value;
|
||||
}
|
||||
GGML_ASSERT(tensor->nb[0] == sizeof(int));
|
||||
return *(int*)((char*)(tensor->data) + i3 * tensor->nb[3] + i2 * tensor->nb[2] + i1 * tensor->nb[1] + i0 * tensor->nb[0]);
|
||||
}
|
||||
|
||||
__STATIC_INLINE__ ggml_fp16_t ggml_ext_tensor_get_f16(const ggml_tensor* tensor, int64_t i0, int64_t i1 = 0, int64_t i2 = 0, int64_t i3 = 0) {
|
||||
GGML_ASSERT(tensor->nb[0] == sizeof(ggml_fp16_t));
|
||||
return *(ggml_fp16_t*)((char*)(tensor->data) + i3 * tensor->nb[3] + i2 * tensor->nb[2] + i1 * tensor->nb[1] + i0 * tensor->nb[0]);
|
||||
}
|
||||
|
||||
__STATIC_INLINE__ float sd_image_get_f32(sd_image_t image, int64_t iw, int64_t ih, int64_t ic, bool scale = true) {
|
||||
float value = *(image.data + ih * image.width * image.channel + iw * image.channel + ic);
|
||||
if (scale) {
|
||||
value /= 255.f;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
void print_ggml_tensor(ggml_tensor* tensor, bool shape_only = false, const char* mark = "");
|
||||
|
||||
template <typename T>
|
||||
__STATIC_INLINE__ void print_sd_tensor(const sd::Tensor<T>& tensor, bool shape_only = false, const char* mark = "") {
|
||||
printf("%s: shape(", mark);
|
||||
for (size_t i = 0; i < static_cast<size_t>(tensor.dim()); ++i) {
|
||||
printf("%s%lld", i == 0 ? "" : ", ", static_cast<long long>(tensor.shape()[i]));
|
||||
}
|
||||
printf(")\n");
|
||||
fflush(stdout);
|
||||
if (shape_only) {
|
||||
return;
|
||||
}
|
||||
if (tensor.empty()) {
|
||||
return;
|
||||
}
|
||||
int range = 3;
|
||||
std::vector<int64_t> shape = tensor.shape();
|
||||
while (shape.size() < 4) {
|
||||
shape.push_back(1);
|
||||
}
|
||||
for (int64_t i3 = 0; i3 < shape[3]; i3++) {
|
||||
if (i3 >= range && i3 + range < shape[3]) {
|
||||
continue;
|
||||
}
|
||||
for (int64_t i2 = 0; i2 < shape[2]; i2++) {
|
||||
if (i2 >= range && i2 + range < shape[2]) {
|
||||
continue;
|
||||
}
|
||||
for (int64_t i1 = 0; i1 < shape[1]; i1++) {
|
||||
if (i1 >= range && i1 + range < shape[1]) {
|
||||
continue;
|
||||
}
|
||||
for (int64_t i0 = 0; i0 < shape[0]; i0++) {
|
||||
if (i0 >= range && i0 + range < shape[0]) {
|
||||
continue;
|
||||
}
|
||||
size_t offset = static_cast<size_t>(i0 + shape[0] * (i1 + shape[1] * (i2 + shape[2] * i3)));
|
||||
printf(" [%lld, %lld, %lld, %lld] = ", static_cast<long long>(i3), static_cast<long long>(i2), static_cast<long long>(i1), static_cast<long long>(i0));
|
||||
if constexpr (std::is_same_v<T, float>) {
|
||||
printf("%f\n", tensor[static_cast<int64_t>(offset)]);
|
||||
} else if constexpr (std::is_same_v<T, ggml_fp16_t>) {
|
||||
printf("%f\n", ggml_fp16_to_fp32(tensor[static_cast<int64_t>(offset)]));
|
||||
} else if constexpr (std::is_same_v<T, int32_t>) {
|
||||
printf("%d\n", tensor[static_cast<int64_t>(offset)]);
|
||||
} else if constexpr (std::is_same_v<T, int64_t>) {
|
||||
printf("%lld\n", static_cast<long long>(tensor[static_cast<int64_t>(offset)]));
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ggml_ext_tensor_iter(
|
||||
ggml_tensor* tensor,
|
||||
const std::function<void(ggml_tensor*, int64_t, int64_t, int64_t, int64_t)>& fn);
|
||||
|
||||
void ggml_ext_tensor_iter(
|
||||
ggml_tensor* tensor,
|
||||
const std::function<void(ggml_tensor*, int64_t)>& fn);
|
||||
|
||||
void ggml_ext_tensor_diff(
|
||||
ggml_tensor* a,
|
||||
ggml_tensor* b,
|
||||
float gap = 0.1f);
|
||||
|
||||
ggml_tensor* load_tensor_from_file(ggml_context* ctx, const std::string& file_path);
|
||||
|
||||
__STATIC_INLINE__ float sigmoid(float x) {
|
||||
return 1 / (1.0f + expf(-x));
|
||||
}
|
||||
|
||||
// SPECIAL OPERATIONS WITH TENSORS
|
||||
|
||||
uint8_t* ggml_tensor_to_sd_image(ggml_tensor* input, uint8_t* image_data = nullptr);
|
||||
|
||||
uint8_t* ggml_tensor_to_sd_image(ggml_tensor* input, int idx, bool video = false);
|
||||
|
||||
void sd_image_to_ggml_tensor(sd_image_t image,
|
||||
ggml_tensor* tensor,
|
||||
bool scale = true);
|
||||
|
||||
void ggml_ext_tensor_apply_mask(ggml_tensor* image_data,
|
||||
ggml_tensor* mask,
|
||||
ggml_tensor* output,
|
||||
float masked_value = 0.5f);
|
||||
|
||||
float ggml_ext_tensor_mean(ggml_tensor* src);
|
||||
|
||||
// a = a+b
|
||||
void ggml_ext_tensor_add_inplace(ggml_tensor* a, ggml_tensor* b);
|
||||
|
||||
void ggml_ext_tensor_scale_inplace(ggml_tensor* src, float scale);
|
||||
|
||||
void ggml_ext_tensor_clamp_inplace(ggml_tensor* src, float min, float max);
|
||||
|
||||
ggml_tensor* ggml_ext_tensor_concat(ggml_context* ctx,
|
||||
ggml_tensor* a,
|
||||
ggml_tensor* b,
|
||||
int dim);
|
||||
|
||||
// convert values from [0, 1] to [-1, 1]
|
||||
void scale_to_minus1_1(ggml_tensor* src);
|
||||
|
||||
// convert values from [-1, 1] to [0, 1]
|
||||
void scale_to_0_1(ggml_tensor* src);
|
||||
|
||||
ggml_tensor* vector_to_ggml_tensor(ggml_context* ctx,
|
||||
const std::vector<float>& vec);
|
||||
|
||||
ggml_tensor* vector_to_ggml_tensor_i32(ggml_context* ctx,
|
||||
const std::vector<int>& vec);
|
||||
|
||||
std::vector<float> arange(float start, float end, float step = 1.f);
|
||||
|
||||
// Ref: https://github.com/CompVis/stable-diffusion/blob/main/ldm/modules/diffusionmodules/util.py#L151
|
||||
std::vector<float> timestep_embedding(std::vector<float> timesteps,
|
||||
int dim,
|
||||
int max_period = 10000,
|
||||
bool flip_sin_to_cos = true,
|
||||
float scale = 1.f);
|
||||
|
||||
void set_timestep_embedding(std::vector<float> timesteps,
|
||||
ggml_tensor* embedding,
|
||||
int dim,
|
||||
int max_period = 10000);
|
||||
|
||||
void set_timestep_embedding(std::vector<float> timesteps,
|
||||
sd::Tensor<float>* embedding,
|
||||
int dim,
|
||||
int max_period = 10000);
|
||||
|
||||
ggml_tensor* new_timestep_embedding(ggml_context* ctx,
|
||||
std::vector<float> timesteps,
|
||||
int dim,
|
||||
int max_period = 10000);
|
||||
|
||||
size_t ggml_tensor_num(ggml_context* ctx);
|
||||
|
||||
#endif // __SD_CORE_GGML_TENSOR_UTILS_H__
|
||||
@@ -145,19 +145,24 @@ namespace sd {
|
||||
std::vector<int64_t> backend_capacities = graph_cut_layer_split_backend_capacities(split_backends,
|
||||
backend_vram_limits,
|
||||
primary_backend_vram_limit);
|
||||
// Existing placements may already occupy the reported free VRAM. Reuse
|
||||
// them; execution checks missing weights and reclaims memory as needed.
|
||||
const bool reuse_assignments = std::all_of(seen_params.begin(), seen_params.end(), [&](ggml_tensor* param) {
|
||||
return param_assignments.count(param) != 0;
|
||||
});
|
||||
|
||||
std::vector<ggml_backend_t> backend_by_segment(plan.segments.size(), split_backends[0]);
|
||||
size_t current_backend = 0;
|
||||
int64_t current_used = 0;
|
||||
for (size_t seg_idx = 0; seg_idx < plan.segments.size(); seg_idx++) {
|
||||
int64_t bytes = segment_param_bytes[seg_idx];
|
||||
while (current_backend + 1 < split_backends.size() &&
|
||||
while (!reuse_assignments && current_backend + 1 < split_backends.size() &&
|
||||
bytes > 0 &&
|
||||
current_used + bytes > backend_capacities[current_backend]) {
|
||||
current_backend++;
|
||||
current_used = 0;
|
||||
}
|
||||
if (bytes > 0 && current_used + bytes > backend_capacities[current_backend]) {
|
||||
if (!reuse_assignments && bytes > 0 && current_used + bytes > backend_capacities[current_backend]) {
|
||||
LOG_ERROR("%s graph-cut layer split: segment %zu needs %.1f MB on %s, but only %.1f MB is available under current VRAM limits",
|
||||
desc,
|
||||
seg_idx,
|
||||
@@ -167,7 +172,6 @@ namespace sd {
|
||||
return false;
|
||||
}
|
||||
current_used += bytes;
|
||||
backend_by_segment[seg_idx] = split_backends[current_backend];
|
||||
|
||||
for (ggml_tensor* param : segment_params[seg_idx]) {
|
||||
ggml_backend_t target_backend = split_backends[current_backend];
|
||||
@@ -186,12 +190,16 @@ namespace sd {
|
||||
ggml_get_name(param));
|
||||
return false;
|
||||
}
|
||||
size_t backend_idx = (size_t)std::distance(split_backends.begin(), backend_it);
|
||||
size_t backend_idx = (size_t)std::distance(split_backends.begin(), backend_it);
|
||||
if (reuse_assignments) {
|
||||
current_backend = backend_idx;
|
||||
}
|
||||
assignment.first_segment_by_backend[backend_idx] = std::min(assignment.first_segment_by_backend[backend_idx], seg_idx);
|
||||
assignment.last_segment_by_backend[backend_idx] = std::max(assignment.last_segment_by_backend[backend_idx], seg_idx + 1);
|
||||
assignment.tensors_by_backend[backend_idx].push_back(param);
|
||||
assignment.bytes_by_backend[backend_idx] += (int64_t)ggml_nbytes(param);
|
||||
}
|
||||
backend_by_segment[seg_idx] = split_backends[current_backend];
|
||||
}
|
||||
|
||||
const int n_nodes = ggml_graph_n_nodes(gf);
|
||||
@@ -243,13 +251,13 @@ namespace sd {
|
||||
assignment.tensors_by_backend[i].size(),
|
||||
assignment.bytes_by_backend[i] / (1024.0 * 1024.0));
|
||||
} else {
|
||||
LOG_DEBUG("%s graph-cut layer split: %s <- segments [%zu, %zu), %zu tensors, %.1f MB",
|
||||
desc,
|
||||
layer_split_backend_device_display_name(split_backends[i]).c_str(),
|
||||
first_segment,
|
||||
last_segment,
|
||||
assignment.tensors_by_backend[i].size(),
|
||||
assignment.bytes_by_backend[i] / (1024.0 * 1024.0));
|
||||
LOG_VERBOSE("%s graph-cut layer split: %s <- segments [%zu, %zu), %zu tensors, %.1f MB",
|
||||
desc,
|
||||
layer_split_backend_device_display_name(split_backends[i]).c_str(),
|
||||
first_segment,
|
||||
last_segment,
|
||||
assignment.tensors_by_backend[i].size(),
|
||||
assignment.bytes_by_backend[i] / (1024.0 * 1024.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,9 +38,19 @@ public:
|
||||
insert(kv);
|
||||
}
|
||||
|
||||
OrderedMap(const OrderedMap&) = default;
|
||||
OrderedMap(OrderedMap&&) noexcept = default;
|
||||
OrderedMap& operator=(const OrderedMap&) = default;
|
||||
OrderedMap(const OrderedMap& other) {
|
||||
for (const auto& value : other) {
|
||||
insert(value);
|
||||
}
|
||||
}
|
||||
OrderedMap(OrderedMap&&) noexcept = default;
|
||||
OrderedMap& operator=(const OrderedMap& other) {
|
||||
if (this != &other) {
|
||||
OrderedMap copy(other);
|
||||
swap(copy);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
OrderedMap& operator=(OrderedMap&&) noexcept = default;
|
||||
|
||||
// --- element access ---
|
||||
@@ -174,4 +184,4 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __SD_CORE_ORDERED_MAP_HPP__
|
||||
#endif // __SD_CORE_ORDERED_MAP_HPP__
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
#include "core/parallel.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <condition_variable>
|
||||
#include <exception>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
namespace sd {
|
||||
|
||||
namespace parallel_detail {
|
||||
thread_local ParallelExecutor* executor = nullptr;
|
||||
thread_local bool active = false;
|
||||
}
|
||||
|
||||
struct ParallelExecutor::Impl {
|
||||
struct Worker {
|
||||
std::condition_variable wake;
|
||||
std::thread thread;
|
||||
bool ready = false;
|
||||
};
|
||||
|
||||
ParallelExecutor* owner;
|
||||
std::mutex invocation_mutex;
|
||||
std::mutex mutex;
|
||||
std::condition_variable finished;
|
||||
std::vector<std::unique_ptr<Worker>> workers;
|
||||
bool stopping = false;
|
||||
int pending = 0;
|
||||
int participants = 1;
|
||||
int64_t begin = 0;
|
||||
int64_t count = 0;
|
||||
const std::function<void(int64_t, int64_t)>* task = nullptr;
|
||||
std::exception_ptr error;
|
||||
|
||||
explicit Impl(ParallelExecutor* owner)
|
||||
: owner(owner) {}
|
||||
|
||||
~Impl() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
stopping = true;
|
||||
}
|
||||
for (auto& worker : workers) {
|
||||
worker->wake.notify_one();
|
||||
}
|
||||
for (auto& worker : workers) {
|
||||
worker->thread.join();
|
||||
}
|
||||
}
|
||||
|
||||
void execute(int index) {
|
||||
ParallelScope scope(owner);
|
||||
parallel_detail::Region region;
|
||||
const int64_t size = count / participants;
|
||||
const int64_t extra = count % participants;
|
||||
const int64_t first = begin + index * size + std::min<int64_t>(index, extra);
|
||||
const int64_t last = first + size + (index < extra ? 1 : 0);
|
||||
try {
|
||||
(*task)(first, last);
|
||||
} catch (...) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
if (!error) {
|
||||
error = std::current_exception();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void worker_loop(Worker* worker, int index) {
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
for (;;) {
|
||||
worker->wake.wait(lock, [&] { return stopping || worker->ready; });
|
||||
if (stopping) {
|
||||
return;
|
||||
}
|
||||
worker->ready = false;
|
||||
lock.unlock();
|
||||
execute(index);
|
||||
lock.lock();
|
||||
if (--pending == 0) {
|
||||
finished.notify_one();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void run(int64_t first, int64_t last, int n_tasks, const std::function<void(int64_t, int64_t)>& callback) {
|
||||
std::lock_guard<std::mutex> invocation_lock(invocation_mutex);
|
||||
std::unique_lock<std::mutex> lock(mutex);
|
||||
while (static_cast<int>(workers.size()) < n_tasks - 1) {
|
||||
workers.push_back(std::make_unique<Worker>());
|
||||
auto* worker = workers.back().get();
|
||||
const int index = static_cast<int>(workers.size());
|
||||
try {
|
||||
worker->thread = std::thread([this, worker, index] { worker_loop(worker, index); });
|
||||
} catch (...) {
|
||||
workers.pop_back();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
begin = first;
|
||||
count = last - first;
|
||||
participants = n_tasks;
|
||||
pending = n_tasks - 1;
|
||||
task = &callback;
|
||||
error = nullptr;
|
||||
for (int i = 0; i < pending; ++i) {
|
||||
workers[i]->ready = true;
|
||||
workers[i]->wake.notify_one();
|
||||
}
|
||||
lock.unlock();
|
||||
execute(0);
|
||||
lock.lock();
|
||||
finished.wait(lock, [&] { return pending == 0; });
|
||||
task = nullptr;
|
||||
if (error) {
|
||||
std::rethrow_exception(error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ParallelExecutor::ParallelExecutor(int n_threads)
|
||||
: n_threads_(std::max(1, n_threads)), impl_(std::make_unique<Impl>(this)) {}
|
||||
|
||||
ParallelExecutor::~ParallelExecutor() = default;
|
||||
|
||||
void ParallelExecutor::run(int64_t begin, int64_t end, int64_t grain_size, const std::function<void(int64_t, int64_t)>& task) {
|
||||
if (begin < 0 || grain_size <= 0) {
|
||||
throw std::invalid_argument("parallel_for requires begin >= 0 and grain_size > 0");
|
||||
}
|
||||
if (end <= begin) {
|
||||
return;
|
||||
}
|
||||
const int n_tasks = static_cast<int>(std::min<int64_t>(n_threads_, (end - begin) / grain_size));
|
||||
if (parallel_detail::active || n_tasks <= 1) {
|
||||
parallel_detail::Region region;
|
||||
task(begin, end);
|
||||
return;
|
||||
}
|
||||
impl_->run(begin, end, n_tasks, task);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
#ifndef __SD_CORE_PARALLEL_H__
|
||||
#define __SD_CORE_PARALLEL_H__
|
||||
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
namespace sd {
|
||||
|
||||
class ParallelExecutor {
|
||||
struct Impl;
|
||||
int n_threads_;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
|
||||
public:
|
||||
explicit ParallelExecutor(int n_threads);
|
||||
~ParallelExecutor();
|
||||
ParallelExecutor(const ParallelExecutor&) = delete;
|
||||
ParallelExecutor& operator=(const ParallelExecutor&) = delete;
|
||||
|
||||
int num_threads() const { return n_threads_; }
|
||||
void run(int64_t begin, int64_t end, int64_t grain_size, const std::function<void(int64_t, int64_t)>& task);
|
||||
};
|
||||
|
||||
namespace parallel_detail {
|
||||
extern thread_local ParallelExecutor* executor;
|
||||
extern thread_local bool active;
|
||||
|
||||
class Region {
|
||||
bool previous_;
|
||||
|
||||
public:
|
||||
Region()
|
||||
: previous_(active) { active = true; }
|
||||
~Region() { active = previous_; }
|
||||
Region(const Region&) = delete;
|
||||
Region& operator=(const Region&) = delete;
|
||||
};
|
||||
}
|
||||
|
||||
class ParallelScope {
|
||||
ParallelExecutor* previous_;
|
||||
|
||||
public:
|
||||
explicit ParallelScope(ParallelExecutor* executor)
|
||||
: previous_(parallel_detail::executor) {
|
||||
parallel_detail::executor = executor;
|
||||
}
|
||||
~ParallelScope() { parallel_detail::executor = previous_; }
|
||||
ParallelScope(const ParallelScope&) = delete;
|
||||
ParallelScope& operator=(const ParallelScope&) = delete;
|
||||
};
|
||||
|
||||
// Ranges are non-negative. The callback may run concurrently and must own its writes.
|
||||
template <typename F>
|
||||
inline void parallel_for(int64_t begin, int64_t end, int64_t grain_size, F&& task) {
|
||||
if (begin < 0 || grain_size <= 0) {
|
||||
throw std::invalid_argument("parallel_for requires begin >= 0 and grain_size > 0");
|
||||
}
|
||||
if (end <= begin) {
|
||||
return;
|
||||
}
|
||||
auto* executor = parallel_detail::executor;
|
||||
if (parallel_detail::active || executor == nullptr || executor->num_threads() <= 1 ||
|
||||
(end - begin) / grain_size < 2) {
|
||||
parallel_detail::Region region;
|
||||
task(begin, end);
|
||||
return;
|
||||
}
|
||||
executor->run(begin, end, grain_size, std::forward<F>(task));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // __SD_CORE_PARALLEL_H__
|
||||
@@ -0,0 +1,171 @@
|
||||
#include "regex.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <mutex>
|
||||
|
||||
#define ONIG_ESCAPE_UCHAR_COLLISION
|
||||
#define ONIG_ESCAPE_REGEX_T_COLLISION
|
||||
#include <oniguruma.h>
|
||||
|
||||
namespace sd {
|
||||
|
||||
struct Regex::Impl {
|
||||
OnigRegex regex = nullptr;
|
||||
|
||||
~Impl() {
|
||||
if (regex) {
|
||||
onig_free(regex);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
struct RegexRegionDeleter {
|
||||
void operator()(OnigRegion* region) const {
|
||||
onig_region_free(region, 1);
|
||||
}
|
||||
};
|
||||
|
||||
static bool regex_error(std::string* error, const std::string& message) {
|
||||
if (error) {
|
||||
*error = message;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool regex_onig_error(std::string* error, int code, OnigErrorInfo* info = nullptr) {
|
||||
OnigUChar buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
|
||||
onig_error_code_to_str(buffer, code, info);
|
||||
return regex_error(error, reinterpret_cast<const char*>(buffer));
|
||||
}
|
||||
|
||||
static int regex_initialize() {
|
||||
static std::once_flag once;
|
||||
static int result = ONIG_NORMAL;
|
||||
std::call_once(once, [] {
|
||||
OnigEncoding encodings[] = {ONIG_ENCODING_UTF8};
|
||||
result = onig_initialize(encodings, 1);
|
||||
});
|
||||
// onig_end() would invalidate expressions held by other Regex instances.
|
||||
return result;
|
||||
}
|
||||
|
||||
// Rust str excludes overlong encodings, surrogates and extended UTF-8 accepted by Oniguruma.
|
||||
static bool regex_valid_utf8(const std::string& text) {
|
||||
size_t position = 0;
|
||||
while (position < text.size()) {
|
||||
const auto lead = static_cast<unsigned char>(text[position++]);
|
||||
if (lead < 0x80) {
|
||||
continue;
|
||||
}
|
||||
int count = 0;
|
||||
if (lead >= 0xC2 && lead <= 0xDF) {
|
||||
count = 1;
|
||||
} else if (lead >= 0xE0 && lead <= 0xEF) {
|
||||
count = 2;
|
||||
} else if (lead >= 0xF0 && lead <= 0xF4) {
|
||||
count = 3;
|
||||
}
|
||||
if (count == 0 || text.size() - position < static_cast<size_t>(count)) {
|
||||
return false;
|
||||
}
|
||||
uint32_t codepoint = lead & (0x7F >> count);
|
||||
for (int i = 0; i < count; ++i) {
|
||||
const auto byte = static_cast<unsigned char>(text[position++]);
|
||||
if ((byte & 0xC0) != 0x80) {
|
||||
return false;
|
||||
}
|
||||
codepoint = (codepoint << 6) | (byte & 0x3F);
|
||||
}
|
||||
constexpr uint32_t minimum[] = {0, 0x80, 0x800, 0x10000};
|
||||
if (codepoint < minimum[count] || codepoint > 0x10FFFF || (codepoint >= 0xD800 && codepoint <= 0xDFFF)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Regex::Regex() = default;
|
||||
Regex::~Regex() = default;
|
||||
Regex::Regex(Regex&&) noexcept = default;
|
||||
Regex& Regex::operator=(Regex&&) noexcept = default;
|
||||
|
||||
bool Regex::compile(const std::string& pattern, std::string* error) {
|
||||
if (error) {
|
||||
error->clear();
|
||||
}
|
||||
const int initialized = regex_initialize();
|
||||
if (initialized != ONIG_NORMAL) {
|
||||
return regex_onig_error(error, initialized);
|
||||
}
|
||||
if (pattern.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
|
||||
return regex_error(error, "regex pattern exceeds Oniguruma's offset range");
|
||||
}
|
||||
const auto* begin = reinterpret_cast<const OnigUChar*>(pattern.data());
|
||||
const auto* end = begin + pattern.size();
|
||||
if (!regex_valid_utf8(pattern)) {
|
||||
return regex_error(error, "regex pattern is not valid UTF-8");
|
||||
}
|
||||
|
||||
auto next = std::make_unique<Impl>();
|
||||
OnigErrorInfo info{};
|
||||
static std::mutex compile_mutex;
|
||||
std::lock_guard<std::mutex> lock(compile_mutex);
|
||||
const int result = onig_new(&next->regex, begin, end, ONIG_OPTION_NONE,
|
||||
ONIG_ENCODING_UTF8, ONIG_SYNTAX_ONIGURUMA, &info);
|
||||
if (result != ONIG_NORMAL) {
|
||||
return regex_onig_error(error, result, &info);
|
||||
}
|
||||
impl_ = std::move(next);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Regex::find_matches(const std::string& text, std::vector<Match>& matches, std::string* error) const {
|
||||
matches.clear();
|
||||
if (error) {
|
||||
error->clear();
|
||||
}
|
||||
if (!impl_) {
|
||||
return regex_error(error, "regex has not been compiled");
|
||||
}
|
||||
if (text.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
|
||||
return regex_error(error, "regex input exceeds Oniguruma's offset range");
|
||||
}
|
||||
const auto* begin = reinterpret_cast<const OnigUChar*>(text.data());
|
||||
const auto* end = begin + text.size();
|
||||
if (!regex_valid_utf8(text)) {
|
||||
return regex_error(error, "regex input is not valid UTF-8");
|
||||
}
|
||||
std::unique_ptr<OnigRegion, RegexRegionDeleter> region(onig_region_new());
|
||||
if (!region) {
|
||||
return regex_error(error, "failed to allocate regex match region");
|
||||
}
|
||||
|
||||
size_t position = 0;
|
||||
while (position <= text.size()) {
|
||||
const int result = onig_search(impl_->regex, begin, end, begin + position, end,
|
||||
region.get(), ONIG_OPTION_NONE);
|
||||
if (result == ONIG_MISMATCH) {
|
||||
break;
|
||||
}
|
||||
if (result < 0) {
|
||||
matches.clear();
|
||||
return regex_onig_error(error, result);
|
||||
}
|
||||
const size_t match_begin = static_cast<size_t>(region->beg[0]);
|
||||
const size_t match_end = static_cast<size_t>(region->end[0]);
|
||||
// Match rust-onig's find_iter: suppress an empty match at the previous match's end.
|
||||
if (match_begin == match_end && !matches.empty() && matches.back().second == match_end) {
|
||||
if (position == text.size()) {
|
||||
break;
|
||||
}
|
||||
position += static_cast<size_t>(ONIGENC_MBC_ENC_LEN(ONIG_ENCODING_UTF8, begin + position));
|
||||
continue;
|
||||
}
|
||||
matches.emplace_back(match_begin, match_end);
|
||||
position = match_end;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace sd
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef __SD_CORE_REGEX_H__
|
||||
#define __SD_CORE_REGEX_H__
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace sd {
|
||||
|
||||
class Regex {
|
||||
struct Impl;
|
||||
std::unique_ptr<Impl> impl_;
|
||||
|
||||
public:
|
||||
using Match = std::pair<size_t, size_t>;
|
||||
|
||||
Regex();
|
||||
~Regex();
|
||||
Regex(Regex&&) noexcept;
|
||||
Regex& operator=(Regex&&) noexcept;
|
||||
|
||||
// Failed compilation leaves the previous expression intact.
|
||||
bool compile(const std::string& pattern, std::string* error = nullptr);
|
||||
// Matches are non-overlapping UTF-8 byte ranges; each call owns its search state.
|
||||
bool find_matches(const std::string& text, std::vector<Match>& matches, std::string* error = nullptr) const;
|
||||
};
|
||||
|
||||
} // namespace sd
|
||||
|
||||
#endif // __SD_CORE_REGEX_H__
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef __SD_CORE_RNG_HPP__
|
||||
#define __SD_CORE_RNG_HPP__
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
@@ -8,6 +10,7 @@ class RNG {
|
||||
public:
|
||||
virtual void manual_seed(uint64_t seed) = 0;
|
||||
virtual std::vector<float> randn(uint32_t n) = 0;
|
||||
virtual std::shared_ptr<RNG> clone() const = 0;
|
||||
};
|
||||
|
||||
class STDDefaultRNG : public RNG {
|
||||
@@ -15,6 +18,10 @@ private:
|
||||
std::default_random_engine generator;
|
||||
|
||||
public:
|
||||
std::shared_ptr<RNG> clone() const override {
|
||||
return std::make_shared<STDDefaultRNG>(*this);
|
||||
}
|
||||
|
||||
void manual_seed(uint64_t seed) override {
|
||||
generator.seed((unsigned int)seed);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#ifndef __SD_CORE_RNG_MT19937_HPP__
|
||||
#define __SD_CORE_RNG_MT19937_HPP__
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
#include "core/rng.hpp"
|
||||
@@ -123,6 +125,10 @@ class MT19937RNG : public RNG {
|
||||
public:
|
||||
MT19937RNG(uint64_t seed = 0) { manual_seed(seed); }
|
||||
|
||||
std::shared_ptr<RNG> clone() const override {
|
||||
return std::make_shared<MT19937RNG>(*this);
|
||||
}
|
||||
|
||||
void manual_seed(uint64_t seed) override {
|
||||
s.seed_ = seed;
|
||||
s.seeded_ = true;
|
||||
|
||||
+31
-60
@@ -1,6 +1,7 @@
|
||||
#ifndef __SD_CORE_RNG_PHILOX_HPP__
|
||||
#define __SD_CORE_RNG_PHILOX_HPP__
|
||||
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
@@ -14,67 +15,35 @@ private:
|
||||
uint32_t offset;
|
||||
|
||||
private:
|
||||
std::vector<uint32_t> philox_m = {0xD2511F53, 0xCD9E8D57};
|
||||
std::vector<uint32_t> philox_w = {0x9E3779B9, 0xBB67AE85};
|
||||
float two_pow32_inv = 2.3283064e-10f;
|
||||
float two_pow32_inv_2pi = 2.3283064e-10f * 6.2831855f;
|
||||
using Counter = std::array<std::vector<uint32_t>, 4>;
|
||||
|
||||
std::vector<uint32_t> uint32(uint64_t x) {
|
||||
std::vector<uint32_t> result(2);
|
||||
result[0] = static_cast<uint32_t>(x & 0xFFFFFFFF);
|
||||
result[1] = static_cast<uint32_t>(x >> 32);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::vector<uint32_t>> uint32(const std::vector<uint64_t>& x) {
|
||||
uint32_t N = (uint32_t)x.size();
|
||||
std::vector<std::vector<uint32_t>> result(2, std::vector<uint32_t>(N));
|
||||
|
||||
for (uint32_t i = 0; i < N; ++i) {
|
||||
result[0][i] = static_cast<uint32_t>(x[i] & 0xFFFFFFFF);
|
||||
result[1][i] = static_cast<uint32_t>(x[i] >> 32);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
static constexpr uint32_t philox_m[2] = {0xD2511F53, 0xCD9E8D57};
|
||||
static constexpr uint32_t philox_w[2] = {0x9E3779B9, 0xBB67AE85};
|
||||
float two_pow32_inv = 2.3283064e-10f;
|
||||
float two_pow32_inv_2pi = 2.3283064e-10f * 6.2831855f;
|
||||
|
||||
// A single round of the Philox 4x32 random number generator.
|
||||
void philox4_round(std::vector<std::vector<uint32_t>>& counter,
|
||||
const std::vector<std::vector<uint32_t>>& key) {
|
||||
void philox4_round(Counter& counter, uint32_t key0, uint32_t key1) {
|
||||
uint32_t N = (uint32_t)counter[0].size();
|
||||
for (uint32_t i = 0; i < N; i++) {
|
||||
std::vector<uint32_t> v1 = uint32(static_cast<uint64_t>(counter[0][i]) * static_cast<uint64_t>(philox_m[0]));
|
||||
std::vector<uint32_t> v2 = uint32(static_cast<uint64_t>(counter[2][i]) * static_cast<uint64_t>(philox_m[1]));
|
||||
const uint64_t v1 = static_cast<uint64_t>(counter[0][i]) * static_cast<uint64_t>(philox_m[0]);
|
||||
const uint64_t v2 = static_cast<uint64_t>(counter[2][i]) * static_cast<uint64_t>(philox_m[1]);
|
||||
|
||||
counter[0][i] = v2[1] ^ counter[1][i] ^ key[0][i];
|
||||
counter[1][i] = v2[0];
|
||||
counter[2][i] = v1[1] ^ counter[3][i] ^ key[1][i];
|
||||
counter[3][i] = v1[0];
|
||||
counter[0][i] = static_cast<uint32_t>(v2 >> 32) ^ counter[1][i] ^ key0;
|
||||
counter[1][i] = static_cast<uint32_t>(v2);
|
||||
counter[2][i] = static_cast<uint32_t>(v1 >> 32) ^ counter[3][i] ^ key1;
|
||||
counter[3][i] = static_cast<uint32_t>(v1);
|
||||
}
|
||||
}
|
||||
|
||||
// Generates 32-bit random numbers using the Philox 4x32 random number generator.
|
||||
// Parameters:
|
||||
// counter : A 4xN array of 32-bit integers representing the counter values (offset into generation).
|
||||
// key : A 2xN array of 32-bit integers representing the key values (seed).
|
||||
// rounds : The number of rounds to perform.
|
||||
// Returns:
|
||||
// std::vector<std::vector<uint32_t>>: A 4xN array of 32-bit integers containing the generated random numbers.
|
||||
std::vector<std::vector<uint32_t>> philox4_32(std::vector<std::vector<uint32_t>>& counter,
|
||||
std::vector<std::vector<uint32_t>>& key,
|
||||
int rounds = 10) {
|
||||
uint32_t N = (uint32_t)counter[0].size();
|
||||
void philox4_32(Counter& counter, uint32_t key0, uint32_t key1, int rounds = 10) {
|
||||
for (int i = 0; i < rounds - 1; ++i) {
|
||||
philox4_round(counter, key);
|
||||
|
||||
for (uint32_t j = 0; j < N; ++j) {
|
||||
key[0][j] += philox_w[0];
|
||||
key[1][j] += philox_w[1];
|
||||
}
|
||||
philox4_round(counter, key0, key1);
|
||||
key0 += philox_w[0];
|
||||
key1 += philox_w[1];
|
||||
}
|
||||
|
||||
philox4_round(counter, key);
|
||||
return counter;
|
||||
philox4_round(counter, key0, key1);
|
||||
}
|
||||
|
||||
float box_muller(float x, float y) {
|
||||
@@ -93,33 +62,35 @@ public:
|
||||
this->offset = 0;
|
||||
}
|
||||
|
||||
std::shared_ptr<RNG> clone() const override {
|
||||
return std::make_shared<PhiloxRNG>(*this);
|
||||
}
|
||||
|
||||
void manual_seed(uint64_t seed) override {
|
||||
this->seed = seed;
|
||||
this->offset = 0;
|
||||
}
|
||||
|
||||
std::vector<float> randn(uint32_t n) override {
|
||||
std::vector<std::vector<uint32_t>> counter(4, std::vector<uint32_t>(n, 0));
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
counter[0][i] = this->offset;
|
||||
}
|
||||
Counter counter;
|
||||
counter[0].resize(n, this->offset);
|
||||
counter[1].resize(n);
|
||||
counter[2].resize(n);
|
||||
counter[3].resize(n);
|
||||
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
counter[2][i] = i;
|
||||
}
|
||||
this->offset += 1;
|
||||
|
||||
std::vector<uint64_t> key(n, this->seed);
|
||||
std::vector<std::vector<uint32_t>> key_uint32 = uint32(key);
|
||||
philox4_32(counter, static_cast<uint32_t>(this->seed), static_cast<uint32_t>(this->seed >> 32));
|
||||
|
||||
std::vector<std::vector<uint32_t>> g = philox4_32(counter, key_uint32);
|
||||
|
||||
std::vector<float> result;
|
||||
std::vector<float> result(n);
|
||||
for (uint32_t i = 0; i < n; ++i) {
|
||||
result.push_back(box_muller((float)g[0][i], (float)g[1][i]));
|
||||
result[i] = box_muller((float)counter[0][i], (float)counter[1][i]);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // __SD_CORE_RNG_PHILOX_HPP__
|
||||
#endif // __SD_CORE_RNG_PHILOX_HPP__
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
#include "core/runner_cache.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "core/ggml_graph_cut.h"
|
||||
#include "core/util.h"
|
||||
|
||||
namespace sd {
|
||||
static std::unordered_set<const ggml_tensor*> cache_graph_tensors(ggml_cgraph* graph) {
|
||||
std::unordered_set<const ggml_tensor*> tensors;
|
||||
for (int i = 0; i < ggml_graph_n_nodes(graph); ++i) {
|
||||
tensors.insert(ggml_graph_node(graph, i));
|
||||
}
|
||||
for (int i = 0; i < ggml_graph_cut::leaf_count(graph); ++i) {
|
||||
tensors.insert(ggml_graph_cut::leaf_tensor(graph, i));
|
||||
}
|
||||
return tensors;
|
||||
}
|
||||
|
||||
CachedTensor::~CachedTensor() {
|
||||
ggml_backend_buffer_free(buffer);
|
||||
ggml_free(context);
|
||||
}
|
||||
|
||||
std::unique_ptr<CachedTensor> CachedTensor::copy(ggml_backend_t backend,
|
||||
const std::string& name,
|
||||
ggml_tensor* source) {
|
||||
if (ggml_graph_cut::tensor_buffer(source) == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
auto entry = std::make_unique<CachedTensor>();
|
||||
entry->context = ggml_init({2 * ggml_tensor_overhead(), nullptr, true});
|
||||
if (entry->context == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
entry->tensor = ggml_dup_tensor(entry->context, source);
|
||||
// Cut views are rebound with their original strides and offsets.
|
||||
std::copy(std::begin(source->nb), std::end(source->nb), std::begin(entry->tensor->nb));
|
||||
ggml_set_name(entry->tensor, name.c_str());
|
||||
entry->buffer = ggml_backend_alloc_ctx_tensors(entry->context, backend);
|
||||
if (entry->buffer == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
if (source->view_src != nullptr || !ggml_is_contiguous(source) || source->buffer == nullptr) {
|
||||
std::vector<uint8_t> data(ggml_nbytes(source));
|
||||
ggml_backend_tensor_get(source, data.data(), 0, data.size());
|
||||
ggml_backend_tensor_set(entry->tensor, data.data(), 0, data.size());
|
||||
} else {
|
||||
ggml_backend_tensor_copy(source, entry->tensor);
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
static ggml_tensor* cached_tensor(const CachedTensors& tensors, const std::string& name) {
|
||||
auto entry = tensors.find(name);
|
||||
return entry == tensors.end() ? nullptr : entry->second->tensor;
|
||||
}
|
||||
|
||||
static size_t resident_bytes(const CachedTensors& tensors, ggml_backend_dev_t device) {
|
||||
size_t bytes = 0;
|
||||
for (const auto& entry : tensors) {
|
||||
auto buffer = entry.second->buffer;
|
||||
if (!ggml_backend_buffer_is_host(buffer) &&
|
||||
ggml_backend_buft_get_device(ggml_backend_buffer_get_type(buffer)) == device) {
|
||||
const size_t size = ggml_backend_buffer_get_size(buffer);
|
||||
bytes = size > SIZE_MAX - bytes ? SIZE_MAX : bytes + size;
|
||||
}
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
ggml_tensor* RunnerCache::get(const std::string& name) const {
|
||||
return cached_tensor(committed_, name);
|
||||
}
|
||||
|
||||
void RunnerCache::stage(const std::string& name, ggml_tensor* tensor) {
|
||||
if (tensor != nullptr) {
|
||||
ggml_set_output(tensor);
|
||||
outputs_[name] = tensor;
|
||||
}
|
||||
}
|
||||
|
||||
size_t RunnerCache::pending_bytes(ggml_cgraph* graph) const {
|
||||
if (outputs_.empty()) {
|
||||
return 0;
|
||||
}
|
||||
auto tensors = cache_graph_tensors(graph);
|
||||
auto buft = ggml_backend_get_default_buffer_type(backend_);
|
||||
size_t bytes = 0;
|
||||
for (const auto& output : outputs_) {
|
||||
if (pending_.count(output.first) || !tensors.count(output.second)) {
|
||||
continue;
|
||||
}
|
||||
const size_t size = GGML_PAD(ggml_backend_buft_get_alloc_size(buft, output.second),
|
||||
ggml_backend_buft_get_alignment(buft));
|
||||
bytes = size > SIZE_MAX - bytes ? SIZE_MAX : bytes + size;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
size_t RunnerCache::resident_bytes(ggml_backend_dev_t device) const {
|
||||
const size_t committed = sd::resident_bytes(committed_, device);
|
||||
const size_t pending = sd::resident_bytes(pending_, device);
|
||||
return pending > SIZE_MAX - committed ? SIZE_MAX : committed + pending;
|
||||
}
|
||||
|
||||
bool RunnerCache::capture(ggml_cgraph* graph) {
|
||||
if (outputs_.empty()) {
|
||||
return true;
|
||||
}
|
||||
const auto tensors = cache_graph_tensors(graph);
|
||||
for (const auto& output : outputs_) {
|
||||
if (pending_.count(output.first) || !tensors.count(output.second)) {
|
||||
continue;
|
||||
}
|
||||
GGML_ASSERT(ggml_is_contiguous(output.second));
|
||||
auto entry = CachedTensor::copy(backend_, output.first, output.second);
|
||||
if (entry == nullptr) {
|
||||
return false;
|
||||
}
|
||||
pending_[output.first] = std::move(entry);
|
||||
}
|
||||
ggml_backend_synchronize(backend_);
|
||||
return true;
|
||||
}
|
||||
|
||||
void RunnerCache::graph_end(bool success) {
|
||||
// Graph inputs can still reference the previous generation until graph end.
|
||||
if (success) {
|
||||
for (auto& entry : pending_) {
|
||||
committed_[entry.first] = std::move(entry.second);
|
||||
}
|
||||
}
|
||||
pending_.clear();
|
||||
outputs_.clear();
|
||||
}
|
||||
|
||||
void RunnerCache::clear() {
|
||||
graph_end(false);
|
||||
committed_.clear();
|
||||
}
|
||||
|
||||
ggml_tensor* GraphCutTensorCache::get(const std::string& name) const {
|
||||
return cached_tensor(tensors_, name);
|
||||
}
|
||||
|
||||
size_t GraphCutTensorCache::resident_bytes(ggml_backend_dev_t device) const {
|
||||
return sd::resident_bytes(tensors_, device);
|
||||
}
|
||||
|
||||
size_t GraphCutTensorCache::estimate_output_bytes(
|
||||
ggml_cgraph* graph,
|
||||
const ggml_graph_cut::Segment& segment) const {
|
||||
ggml_backend_buffer_type_t buffer_type =
|
||||
ggml_backend_get_default_buffer_type(backend_);
|
||||
if (buffer_type == nullptr) {
|
||||
return SIZE_MAX;
|
||||
}
|
||||
const size_t alignment = ggml_backend_buft_get_alignment(buffer_type);
|
||||
size_t total_size = 0;
|
||||
for (size_t output_idx = 0; output_idx < segment.output_node_indices.size(); ++output_idx) {
|
||||
ggml_tensor* output = ggml_graph_cut::output_tensor(graph, segment, output_idx);
|
||||
if (output == nullptr || !ggml_graph_cut::is_graph_cut_tensor(output) ||
|
||||
!segment.future_cut_names.count(output->name)) {
|
||||
continue;
|
||||
}
|
||||
ggml_tensor* source = ggml_graph_cut::cache_source_tensor(output);
|
||||
const size_t tensor_size = GGML_PAD(
|
||||
ggml_backend_buft_get_alloc_size(buffer_type, source), alignment);
|
||||
total_size = tensor_size > SIZE_MAX - total_size ? SIZE_MAX : total_size + tensor_size;
|
||||
}
|
||||
return total_size;
|
||||
}
|
||||
|
||||
void GraphCutTensorCache::prune(const std::unordered_set<std::string>& keep_names) {
|
||||
for (auto it = tensors_.begin(); it != tensors_.end();) {
|
||||
it = keep_names.count(it->first) ? std::next(it) : tensors_.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
bool GraphCutTensorCache::capture(ggml_cgraph* graph,
|
||||
const ggml_graph_cut::Segment& segment,
|
||||
const char* log_desc) {
|
||||
size_t copied_bytes = 0;
|
||||
size_t copied_count = 0;
|
||||
for (int index : segment.output_node_indices) {
|
||||
auto output = ggml_graph_node(graph, index);
|
||||
if (!ggml_graph_cut::is_graph_cut_tensor(output) ||
|
||||
!segment.future_cut_names.count(output->name)) {
|
||||
continue;
|
||||
}
|
||||
auto entry = CachedTensor::copy(backend_, output->name, ggml_graph_cut::cache_source_tensor(output));
|
||||
if (entry == nullptr) {
|
||||
LOG_ERROR("%s failed to capture graph cut tensor: %s", log_desc, output->name);
|
||||
return false;
|
||||
}
|
||||
const size_t size = ggml_backend_buffer_get_size(entry->buffer);
|
||||
copied_bytes = size > SIZE_MAX - copied_bytes ? SIZE_MAX : copied_bytes + size;
|
||||
++copied_count;
|
||||
tensors_[output->name] = std::move(entry);
|
||||
}
|
||||
ggml_backend_synchronize(backend_);
|
||||
if (copied_count > 0) {
|
||||
LOG_DEBUG("%s graph cut cache added %6.2f MB (%zu tensors)",
|
||||
log_desc, copied_bytes / (1024.f * 1024.f), copied_count);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#ifndef __SD_CORE_RUNNER_CACHE_H__
|
||||
#define __SD_CORE_RUNNER_CACHE_H__
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
|
||||
#include "ggml-backend.h"
|
||||
|
||||
namespace sd::ggml_graph_cut {
|
||||
struct Segment;
|
||||
}
|
||||
|
||||
namespace sd {
|
||||
struct CachedTensor {
|
||||
ggml_context* context = nullptr;
|
||||
ggml_backend_buffer_t buffer = nullptr;
|
||||
ggml_tensor* tensor = nullptr;
|
||||
~CachedTensor();
|
||||
static std::unique_ptr<CachedTensor> copy(ggml_backend_t backend,
|
||||
const std::string& name,
|
||||
ggml_tensor* source);
|
||||
};
|
||||
using CachedTensors = std::map<std::string, std::unique_ptr<CachedTensor>>;
|
||||
|
||||
class RunnerCache {
|
||||
ggml_backend_t backend_;
|
||||
CachedTensors committed_;
|
||||
CachedTensors pending_;
|
||||
std::map<std::string, ggml_tensor*> outputs_;
|
||||
|
||||
public:
|
||||
explicit RunnerCache(ggml_backend_t backend)
|
||||
: backend_(backend) {}
|
||||
RunnerCache(const RunnerCache&) = delete;
|
||||
RunnerCache& operator=(const RunnerCache&) = delete;
|
||||
|
||||
ggml_tensor* get(const std::string& name) const;
|
||||
void stage(const std::string& name, ggml_tensor* tensor);
|
||||
const std::map<std::string, ggml_tensor*>& outputs() const { return outputs_; }
|
||||
size_t pending_bytes(ggml_cgraph* graph) const;
|
||||
size_t resident_bytes(ggml_backend_dev_t device) const;
|
||||
bool capture(ggml_cgraph* graph);
|
||||
void graph_end(bool success);
|
||||
void clear();
|
||||
};
|
||||
|
||||
class GraphCutTensorCache {
|
||||
ggml_backend_t backend_;
|
||||
CachedTensors tensors_;
|
||||
|
||||
public:
|
||||
explicit GraphCutTensorCache(ggml_backend_t backend)
|
||||
: backend_(backend) {}
|
||||
ggml_tensor* get(const std::string& name) const;
|
||||
size_t resident_bytes(ggml_backend_dev_t device) const;
|
||||
size_t estimate_output_bytes(ggml_cgraph* graph,
|
||||
const ggml_graph_cut::Segment& segment) const;
|
||||
bool capture(ggml_cgraph* graph, const ggml_graph_cut::Segment& segment, const char* log_desc);
|
||||
void prune(const std::unordered_set<std::string>& keep_names);
|
||||
void clear() { tensors_.clear(); }
|
||||
};
|
||||
}
|
||||
|
||||
#endif // __SD_CORE_RUNNER_CACHE_H__
|
||||
@@ -0,0 +1,137 @@
|
||||
#include "core/segment_graph_bindings.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
#include "core/ggml_graph_cut.h"
|
||||
#include "core/runner_cache.h"
|
||||
#include "core/util.h"
|
||||
#include "ggml.h"
|
||||
|
||||
namespace sd {
|
||||
SegmentGraphBindings::SegmentGraphBindings(GraphCutTensorCache& tensor_cache,
|
||||
const ggml_graph_cut::Plan& plan,
|
||||
ggml_cgraph* graph)
|
||||
: tensor_cache_(tensor_cache),
|
||||
graph_(graph) {
|
||||
GGML_ASSERT(graph_ != nullptr);
|
||||
for (int i = 0; i < ggml_graph_n_nodes(graph_); ++i) {
|
||||
ggml_tensor* tensor = ggml_graph_node(graph_, i);
|
||||
Topology topology{tensor->op, {}, tensor->view_src, tensor->flags};
|
||||
std::copy(std::begin(tensor->src), std::end(tensor->src), topology.sources.begin());
|
||||
topology_[tensor] = topology;
|
||||
}
|
||||
for (const auto& segment : plan.segments) {
|
||||
for (const auto& input : segment.input_refs) {
|
||||
if (input.type != ggml_graph_cut::Segment::INPUT_EXTERNAL) {
|
||||
continue;
|
||||
}
|
||||
ggml_tensor* tensor = ggml_graph_cut::input_tensor(graph_, input);
|
||||
if (tensor == nullptr || tensor->buffer == nullptr) {
|
||||
continue;
|
||||
}
|
||||
external_bindings_[tensor] = {tensor->buffer, tensor->data, tensor->extra};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SegmentGraphBindings::reset(const ggml_graph_cut::Segment& segment) {
|
||||
restore();
|
||||
for (const auto& input : segment.input_refs) {
|
||||
ggml_tensor* tensor = ggml_graph_cut::input_tensor(graph_, input);
|
||||
if (tensor == nullptr) {
|
||||
continue;
|
||||
}
|
||||
switch (input.type) {
|
||||
case ggml_graph_cut::Segment::INPUT_PREVIOUS_CUT:
|
||||
tensor->buffer = nullptr;
|
||||
tensor->data = nullptr;
|
||||
tensor->extra = nullptr;
|
||||
break;
|
||||
case ggml_graph_cut::Segment::INPUT_EXTERNAL: {
|
||||
auto binding = external_bindings_.find(tensor);
|
||||
if (binding != external_bindings_.end()) {
|
||||
tensor->buffer = binding->second.buffer;
|
||||
tensor->data = binding->second.data;
|
||||
tensor->extra = binding->second.extra;
|
||||
} else {
|
||||
tensor->buffer = nullptr;
|
||||
tensor->data = nullptr;
|
||||
tensor->extra = nullptr;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ggml_graph_cut::Segment::INPUT_PARAM:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int node_index : segment.internal_node_indices) {
|
||||
ggml_tensor* node = ggml_graph_node(graph_, node_index);
|
||||
if (node == nullptr) {
|
||||
continue;
|
||||
}
|
||||
node->buffer = nullptr;
|
||||
node->data = nullptr;
|
||||
node->extra = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void SegmentGraphBindings::restore() {
|
||||
for (const auto& entry : topology_) {
|
||||
entry.first->op = entry.second.op;
|
||||
entry.first->view_src = entry.second.view_source;
|
||||
entry.first->flags = entry.second.flags;
|
||||
std::copy(entry.second.sources.begin(), entry.second.sources.end(), std::begin(entry.first->src));
|
||||
}
|
||||
}
|
||||
|
||||
bool SegmentGraphBindings::bind_cached_inputs(
|
||||
const ggml_graph_cut::Segment& segment,
|
||||
const char* log_desc) {
|
||||
std::unordered_map<ggml_tensor*, ggml_tensor*> cached_view_sources;
|
||||
for (const auto& input : segment.input_refs) {
|
||||
if (input.type != ggml_graph_cut::Segment::INPUT_PREVIOUS_CUT) {
|
||||
continue;
|
||||
}
|
||||
ggml_tensor* input_tensor = ggml_graph_cut::input_tensor(graph_, input);
|
||||
if (input_tensor == nullptr) {
|
||||
continue;
|
||||
}
|
||||
ggml_tensor* cached_tensor = tensor_cache_.get(input.display_name);
|
||||
if (cached_tensor == nullptr) {
|
||||
LOG_ERROR("%s missing graph cut cache tensor: %s",
|
||||
log_desc,
|
||||
input.display_name.c_str());
|
||||
return false;
|
||||
}
|
||||
if (input_tensor->view_src != nullptr) {
|
||||
cached_view_sources[topology_.at(input_tensor).view_source] = cached_tensor;
|
||||
input_tensor->view_src = cached_tensor;
|
||||
input_tensor->buffer = nullptr;
|
||||
input_tensor->data = cached_tensor->data == nullptr
|
||||
? nullptr
|
||||
: static_cast<void*>(static_cast<char*>(cached_tensor->data) +
|
||||
input_tensor->view_offs);
|
||||
input_tensor->extra = cached_tensor->extra;
|
||||
} else {
|
||||
input_tensor->buffer = cached_tensor->buffer;
|
||||
input_tensor->data = cached_tensor->data;
|
||||
input_tensor->extra = cached_tensor->extra;
|
||||
}
|
||||
for (int source_index = 0; source_index < GGML_MAX_SRC; ++source_index) {
|
||||
input_tensor->src[source_index] = nullptr;
|
||||
}
|
||||
input_tensor->op = GGML_OP_NONE;
|
||||
}
|
||||
// ggml flattens view chains, so descendants also need the cached root.
|
||||
for (int node_index : segment.internal_node_indices) {
|
||||
ggml_tensor* node = ggml_graph_node(graph_, node_index);
|
||||
auto cached_source = cached_view_sources.find(topology_.at(node).view_source);
|
||||
if (cached_source != cached_view_sources.end()) {
|
||||
node->view_src = cached_source->second;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef __SD_CORE_SEGMENT_GRAPH_BINDINGS_H__
|
||||
#define __SD_CORE_SEGMENT_GRAPH_BINDINGS_H__
|
||||
|
||||
#include <array>
|
||||
#include <unordered_map>
|
||||
#include "ggml.h"
|
||||
|
||||
struct ggml_backend_buffer;
|
||||
struct ggml_cgraph;
|
||||
struct ggml_tensor;
|
||||
|
||||
namespace sd {
|
||||
class GraphCutTensorCache;
|
||||
|
||||
namespace ggml_graph_cut {
|
||||
struct Plan;
|
||||
struct Segment;
|
||||
}
|
||||
|
||||
class SegmentGraphBindings {
|
||||
public:
|
||||
SegmentGraphBindings(GraphCutTensorCache& tensor_cache,
|
||||
const ggml_graph_cut::Plan& plan,
|
||||
ggml_cgraph* graph);
|
||||
|
||||
void reset(const ggml_graph_cut::Segment& segment);
|
||||
void restore();
|
||||
~SegmentGraphBindings() { restore(); }
|
||||
bool bind_cached_inputs(const ggml_graph_cut::Segment& segment,
|
||||
const char* log_desc);
|
||||
|
||||
private:
|
||||
struct ExternalBinding {
|
||||
ggml_backend_buffer* buffer = nullptr;
|
||||
void* data = nullptr;
|
||||
void* extra = nullptr;
|
||||
};
|
||||
|
||||
GraphCutTensorCache& tensor_cache_;
|
||||
ggml_cgraph* graph_ = nullptr;
|
||||
std::unordered_map<ggml_tensor*, ExternalBinding> external_bindings_;
|
||||
struct Topology {
|
||||
ggml_op op;
|
||||
std::array<ggml_tensor*, GGML_MAX_SRC> sources;
|
||||
ggml_tensor* view_source;
|
||||
int flags;
|
||||
};
|
||||
std::unordered_map<ggml_tensor*, Topology> topology_;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // __SD_CORE_SEGMENT_GRAPH_BINDINGS_H__
|
||||
@@ -0,0 +1,205 @@
|
||||
#include "core/segment_weight_pipeline.h"
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "core/ggml_graph_cut.h"
|
||||
#include "device_residency_manager.h"
|
||||
|
||||
namespace sd {
|
||||
static ggml_tensor* canonical_param(
|
||||
ggml_tensor* tensor,
|
||||
const std::unordered_set<const ggml_tensor*>& params) {
|
||||
for (ggml_tensor* current = tensor; current != nullptr; current = current->view_src) {
|
||||
if (params.find(current) != params.end()) {
|
||||
return current;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
SegmentWeightPipeline::SegmentWeightPipeline(
|
||||
const std::shared_ptr<DeviceResidencyManager>& residency_manager,
|
||||
ggml_backend_t compute_backend,
|
||||
uintptr_t owner_id,
|
||||
ggml_cgraph* graph,
|
||||
const ggml_graph_cut::Plan& plan,
|
||||
const std::unordered_set<const ggml_tensor*>& params,
|
||||
bool enabled)
|
||||
: residency_manager_(residency_manager),
|
||||
compute_backend_(compute_backend),
|
||||
owner_id_(owner_id),
|
||||
enabled_(enabled && residency_manager != nullptr) {
|
||||
segment_params_.resize(plan.segments.size());
|
||||
for (size_t segment_index = 0; segment_index < plan.segments.size(); ++segment_index) {
|
||||
std::unordered_set<ggml_tensor*> seen;
|
||||
for (ggml_tensor* tensor :
|
||||
ggml_graph_cut::param_tensors(graph, plan.segments[segment_index])) {
|
||||
ggml_tensor* param = canonical_param(tensor, params);
|
||||
if (param != nullptr && seen.insert(param).second) {
|
||||
segment_params_[segment_index].push_back(param);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SegmentWeightPipeline::~SegmentWeightPipeline() {
|
||||
segment_end();
|
||||
clear();
|
||||
}
|
||||
|
||||
size_t SegmentWeightPipeline::next_parameter_segment(size_t segment_index) const {
|
||||
for (size_t next = segment_index + 1; next < segment_params_.size(); ++next) {
|
||||
if (!segment_params_[next].empty()) {
|
||||
return next;
|
||||
}
|
||||
}
|
||||
return SIZE_MAX;
|
||||
}
|
||||
|
||||
std::vector<std::vector<ggml_tensor*>> SegmentWeightPipeline::preferred_eviction_order() const {
|
||||
return {segment_params_.rbegin(), segment_params_.rend()};
|
||||
}
|
||||
|
||||
void SegmentWeightPipeline::disable() {
|
||||
clear();
|
||||
enabled_ = false;
|
||||
}
|
||||
|
||||
void SegmentWeightPipeline::activate(size_t segment_index) {
|
||||
if (!enabled_ || queued_segment_ == SIZE_MAX || queued_segment_ != segment_index) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto manager = residency_manager_.lock();
|
||||
if (manager == nullptr ||
|
||||
!manager->activate_prefetched_params(owner_id_, queued_params_)) {
|
||||
disable();
|
||||
return;
|
||||
}
|
||||
queued_params_.clear();
|
||||
queued_segment_ = SIZE_MAX;
|
||||
}
|
||||
|
||||
bool SegmentWeightPipeline::ensure_segment_capacity(
|
||||
size_t segment_index,
|
||||
const std::vector<DeviceMemoryRequest>& requests) {
|
||||
if (segment_index >= segment_params_.size()) {
|
||||
return false;
|
||||
}
|
||||
auto manager = residency_manager_.lock();
|
||||
if (manager == nullptr) {
|
||||
return segment_params_[segment_index].empty();
|
||||
}
|
||||
std::vector<ggml_tensor*> protected_params = segment_params_[segment_index];
|
||||
protected_params.insert(protected_params.end(), queued_params_.begin(), queued_params_.end());
|
||||
for (const auto& request : requests) {
|
||||
if (!manager->ensure_compute_backend_capacity(request, segment_params_[segment_index],
|
||||
preferred_eviction_order(), protected_params)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SegmentWeightPipeline::segment_start(size_t segment_index, const std::function<bool()>& ensure_capacity) {
|
||||
GGML_ASSERT(pinned_params_.empty());
|
||||
activate(segment_index);
|
||||
if (!ensure_capacity()) {
|
||||
return false;
|
||||
}
|
||||
auto manager = residency_manager_.lock();
|
||||
if (manager == nullptr) {
|
||||
return segment_params_[segment_index].empty();
|
||||
}
|
||||
if (!manager->prepare_params(segment_params_[segment_index])) {
|
||||
return false;
|
||||
}
|
||||
pinned_params_ = segment_params_[segment_index];
|
||||
return true;
|
||||
}
|
||||
|
||||
void SegmentWeightPipeline::segment_end() {
|
||||
if (auto manager = residency_manager_.lock()) {
|
||||
manager->release_compute_backend_params(pinned_params_);
|
||||
}
|
||||
pinned_params_.clear();
|
||||
}
|
||||
|
||||
void SegmentWeightPipeline::enqueue_next(
|
||||
size_t segment_index,
|
||||
const DeviceMemoryRequest& request) {
|
||||
if (!enabled_ || queued_segment_ != SIZE_MAX) {
|
||||
return;
|
||||
}
|
||||
|
||||
const size_t next_segment = next_parameter_segment(segment_index);
|
||||
if (next_segment == SIZE_MAX) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::unordered_set<ggml_tensor*> active_params(
|
||||
segment_params_[segment_index].begin(),
|
||||
segment_params_[segment_index].end());
|
||||
std::vector<ggml_tensor*> params;
|
||||
params.reserve(segment_params_[next_segment].size());
|
||||
for (ggml_tensor* param : segment_params_[next_segment]) {
|
||||
if (active_params.find(param) == active_params.end()) {
|
||||
params.push_back(param);
|
||||
}
|
||||
}
|
||||
if (params.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto manager = residency_manager_.lock();
|
||||
if (manager == nullptr) {
|
||||
disable();
|
||||
return;
|
||||
}
|
||||
const WeightResidencyInfo residency =
|
||||
manager->inspect_compute_backend_params(params);
|
||||
if (residency.missing_bytes == 0) {
|
||||
return;
|
||||
}
|
||||
if (!residency.async_prefetch_supported) {
|
||||
disable();
|
||||
return;
|
||||
}
|
||||
|
||||
DeviceMemoryRequest backend_request = request;
|
||||
backend_request.compute_backend = compute_backend_;
|
||||
backend_request.owner_id = owner_id_;
|
||||
std::vector<ggml_tensor*> protected_params = segment_params_[segment_index];
|
||||
protected_params.insert(protected_params.end(), params.begin(), params.end());
|
||||
if (!manager->ensure_compute_backend_capacity(backend_request,
|
||||
params,
|
||||
preferred_eviction_order(),
|
||||
protected_params)) {
|
||||
disable();
|
||||
return;
|
||||
}
|
||||
switch (manager->prefetch_params(owner_id_, params)) {
|
||||
case WeightPrefetchResult::Scheduled:
|
||||
queued_params_ = std::move(params);
|
||||
queued_segment_ = next_segment;
|
||||
return;
|
||||
case WeightPrefetchResult::AlreadyResident:
|
||||
return;
|
||||
case WeightPrefetchResult::Unsupported:
|
||||
disable();
|
||||
return;
|
||||
case WeightPrefetchResult::Failed:
|
||||
disable();
|
||||
return;
|
||||
}
|
||||
disable();
|
||||
}
|
||||
|
||||
void SegmentWeightPipeline::clear() {
|
||||
if (auto manager = residency_manager_.lock()) {
|
||||
manager->clear_prefetched_params(owner_id_);
|
||||
}
|
||||
queued_params_.clear();
|
||||
queued_segment_ = SIZE_MAX;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#ifndef __SD_CORE_SEGMENT_WEIGHT_PIPELINE_H__
|
||||
#define __SD_CORE_SEGMENT_WEIGHT_PIPELINE_H__
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "ggml-backend.h"
|
||||
|
||||
struct DeviceMemoryRequest;
|
||||
struct DeviceResidencyManager;
|
||||
struct ggml_cgraph;
|
||||
struct ggml_tensor;
|
||||
|
||||
namespace sd::ggml_graph_cut {
|
||||
struct Plan;
|
||||
}
|
||||
|
||||
namespace sd {
|
||||
class SegmentWeightPipeline {
|
||||
private:
|
||||
std::weak_ptr<DeviceResidencyManager> residency_manager_;
|
||||
ggml_backend_t compute_backend_ = nullptr;
|
||||
uintptr_t owner_id_ = 0;
|
||||
std::vector<std::vector<ggml_tensor*>> segment_params_;
|
||||
std::vector<ggml_tensor*> queued_params_;
|
||||
std::vector<ggml_tensor*> pinned_params_;
|
||||
size_t queued_segment_ = SIZE_MAX;
|
||||
bool enabled_ = true;
|
||||
|
||||
size_t next_parameter_segment(size_t segment_index) const;
|
||||
std::vector<std::vector<ggml_tensor*>> preferred_eviction_order() const;
|
||||
void disable();
|
||||
void activate(size_t segment_index);
|
||||
void clear();
|
||||
|
||||
public:
|
||||
SegmentWeightPipeline(
|
||||
const std::shared_ptr<DeviceResidencyManager>& residency_manager,
|
||||
ggml_backend_t compute_backend,
|
||||
uintptr_t owner_id,
|
||||
ggml_cgraph* graph,
|
||||
const ggml_graph_cut::Plan& plan,
|
||||
const std::unordered_set<const ggml_tensor*>& params,
|
||||
bool enabled = true);
|
||||
~SegmentWeightPipeline();
|
||||
|
||||
const std::vector<ggml_tensor*>& params(size_t index) const { return segment_params_[index]; }
|
||||
bool ensure_segment_capacity(size_t segment_index,
|
||||
const std::vector<DeviceMemoryRequest>& requests);
|
||||
bool segment_start(size_t segment_index, const std::function<bool()>& ensure_capacity);
|
||||
void segment_end();
|
||||
// Prefetch is best effort; segment_start falls back to synchronous loading.
|
||||
void enqueue_next(size_t segment_index,
|
||||
const DeviceMemoryRequest& request);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // __SD_CORE_SEGMENT_WEIGHT_PIPELINE_H__
|
||||
+121
-89
@@ -16,6 +16,7 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "core/parallel.h"
|
||||
#include "core/rng.hpp"
|
||||
|
||||
namespace sd {
|
||||
@@ -59,6 +60,15 @@ namespace sd {
|
||||
return numel;
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline void tensor_for_each(int64_t count, F&& fn, int64_t grain_size = 65536) {
|
||||
parallel_for(0, count, grain_size, [&](int64_t begin, int64_t end) {
|
||||
for (int64_t i = begin; i < end; ++i) {
|
||||
fn(i);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class Tensor {
|
||||
public:
|
||||
@@ -230,7 +240,10 @@ namespace sd {
|
||||
}
|
||||
|
||||
void fill_(const T& value) {
|
||||
std::fill(data_.begin(), data_.end(), value);
|
||||
const T fill_value = value;
|
||||
parallel_for(0, numel(), 65536, [&](int64_t begin, int64_t end) {
|
||||
std::fill_n(data_.data() + begin, end - begin, fill_value);
|
||||
});
|
||||
}
|
||||
|
||||
Tensor& masked_fill_(const Tensor<uint8_t>& mask, const T& value);
|
||||
@@ -390,7 +403,7 @@ namespace sd {
|
||||
tensor_shape_to_string(lhs) + ", rhs_shape=" +
|
||||
tensor_shape_to_string(rhs));
|
||||
}
|
||||
shape[i] = std::max(lhs_dim, rhs_dim);
|
||||
shape[i] = lhs_dim == 1 ? rhs_dim : lhs_dim;
|
||||
}
|
||||
return shape;
|
||||
}
|
||||
@@ -425,39 +438,55 @@ namespace sd {
|
||||
const std::vector<int64_t>& rhs_shape_raw,
|
||||
const std::vector<int64_t>& rhs_strides_raw,
|
||||
F&& fn) {
|
||||
const size_t ndim = out_shape.size();
|
||||
std::vector<int64_t> out_strides = tensor_compute_strides(out_shape);
|
||||
std::vector<int64_t> lhs_shape(ndim, 1);
|
||||
std::vector<int64_t> lhs_strides(ndim, 0);
|
||||
std::vector<int64_t> rhs_shape(ndim, 1);
|
||||
std::vector<int64_t> rhs_strides(ndim, 0);
|
||||
|
||||
for (size_t i = 0; i < lhs_shape_raw.size(); ++i) {
|
||||
lhs_shape[i] = lhs_shape_raw[i];
|
||||
lhs_strides[i] = lhs_strides_raw[i];
|
||||
}
|
||||
for (size_t i = 0; i < rhs_shape_raw.size(); ++i) {
|
||||
rhs_shape[i] = rhs_shape_raw[i];
|
||||
rhs_strides[i] = rhs_strides_raw[i];
|
||||
}
|
||||
|
||||
const int64_t numel = tensor_numel(out_shape);
|
||||
for (int64_t flat = 0; flat < numel; ++flat) {
|
||||
int64_t remaining = flat;
|
||||
int64_t lhs_offset = 0;
|
||||
int64_t rhs_offset = 0;
|
||||
for (size_t i = ndim; i-- > 0;) {
|
||||
int64_t coord = remaining / out_strides[i];
|
||||
remaining %= out_strides[i];
|
||||
if (lhs_shape[i] != 1) {
|
||||
lhs_offset += coord * lhs_strides[i];
|
||||
const int64_t numel = tensor_numel(out_shape);
|
||||
const size_t ndim = out_shape.size();
|
||||
auto broadcast_strides = [&](const std::vector<int64_t>& shape,
|
||||
const std::vector<int64_t>& strides) {
|
||||
if ((numel != 0 && tensor_numel(shape) == 0) || strides.size() != shape.size()) {
|
||||
tensor_throw_invalid_argument("Tensor broadcast requires non-empty inputs and matching strides");
|
||||
}
|
||||
std::vector<int64_t> result(ndim, 0);
|
||||
for (size_t i = 0; i < std::max(ndim, shape.size()); ++i) {
|
||||
const int64_t input_dim = i < shape.size() ? shape[i] : 1;
|
||||
const int64_t output_dim = i < ndim ? out_shape[i] : 1;
|
||||
if (input_dim != 1 && input_dim != output_dim) {
|
||||
tensor_throw_invalid_argument("Tensor broadcast cannot expand the destination: input_shape=" +
|
||||
tensor_shape_to_string(shape) + ", output_shape=" +
|
||||
tensor_shape_to_string(out_shape));
|
||||
}
|
||||
if (rhs_shape[i] != 1) {
|
||||
rhs_offset += coord * rhs_strides[i];
|
||||
if (i < ndim && input_dim != 1) {
|
||||
result[i] = strides[i];
|
||||
}
|
||||
}
|
||||
fn(flat, lhs_offset, rhs_offset);
|
||||
return result;
|
||||
};
|
||||
const auto lhs_strides = broadcast_strides(lhs_shape_raw, lhs_strides_raw);
|
||||
const auto rhs_strides = broadcast_strides(rhs_shape_raw, rhs_strides_raw);
|
||||
if (numel == 0) {
|
||||
return;
|
||||
}
|
||||
parallel_for(0, numel, 16384, [&](int64_t begin, int64_t end) {
|
||||
auto coord = tensor_unravel_index(begin, out_shape);
|
||||
int64_t lhs_offset = 0;
|
||||
int64_t rhs_offset = 0;
|
||||
for (size_t i = 0; i < ndim; ++i) {
|
||||
lhs_offset += coord[i] * lhs_strides[i];
|
||||
rhs_offset += coord[i] * rhs_strides[i];
|
||||
}
|
||||
for (int64_t flat = begin; flat < end; ++flat) {
|
||||
fn(flat, lhs_offset, rhs_offset);
|
||||
for (size_t i = 0; i < ndim; ++i) {
|
||||
lhs_offset += lhs_strides[i];
|
||||
rhs_offset += rhs_strides[i];
|
||||
if (++coord[i] < out_shape[i]) {
|
||||
break;
|
||||
}
|
||||
coord[i] = 0;
|
||||
lhs_offset -= out_shape[i] * lhs_strides[i];
|
||||
rhs_offset -= out_shape[i] * rhs_strides[i];
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
@@ -469,6 +498,7 @@ namespace sd {
|
||||
const std::vector<int64_t> data_strides = tensor_compute_strides(shape_);
|
||||
const std::vector<int64_t> mask_strides = tensor_compute_strides(mask.shape());
|
||||
const uint8_t* mask_data = mask.data();
|
||||
const T fill_value = value;
|
||||
tensor_for_each_broadcast_offset(shape_,
|
||||
shape_,
|
||||
data_strides,
|
||||
@@ -476,7 +506,7 @@ namespace sd {
|
||||
mask_strides,
|
||||
[&](int64_t, int64_t data_offset, int64_t mask_offset) {
|
||||
if (mask_data[mask_offset] != 0) {
|
||||
data_[static_cast<size_t>(data_offset)] = value;
|
||||
data_[static_cast<size_t>(data_offset)] = fill_value;
|
||||
}
|
||||
});
|
||||
return *this;
|
||||
@@ -486,9 +516,9 @@ namespace sd {
|
||||
inline Tensor<uint8_t> operator<(const Tensor<T>& lhs, Scalar rhs) {
|
||||
Tensor<uint8_t> result(lhs.shape());
|
||||
const T value = static_cast<T>(rhs);
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
result[i] = lhs[i] < value ? 1 : 0;
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
result.data()[i] = lhs.data()[i] < value ? 1 : 0;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -496,9 +526,9 @@ namespace sd {
|
||||
inline Tensor<uint8_t> operator<(Scalar lhs, const Tensor<T>& rhs) {
|
||||
Tensor<uint8_t> result(rhs.shape());
|
||||
const T value = static_cast<T>(lhs);
|
||||
for (int64_t i = 0; i < rhs.numel(); ++i) {
|
||||
result[i] = value < rhs[i] ? 1 : 0;
|
||||
}
|
||||
tensor_for_each(rhs.numel(), [&](int64_t i) {
|
||||
result.data()[i] = value < rhs.data()[i] ? 1 : 0;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -516,7 +546,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t flat, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
result[flat] = lhs_data[lhs_offset] < rhs_data[rhs_offset] ? 1 : 0;
|
||||
result.data()[flat] = lhs_data[lhs_offset] < rhs_data[rhs_offset] ? 1 : 0;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -524,9 +554,9 @@ namespace sd {
|
||||
template <typename T>
|
||||
inline Tensor<T>& operator+=(Tensor<T>& lhs, const Tensor<T>& rhs) {
|
||||
if (lhs.shape() == rhs.shape()) {
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
lhs[i] += rhs[i];
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
lhs.data()[i] += rhs.data()[i];
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
tensor_broadcast_shape(lhs.shape(), rhs.shape());
|
||||
@@ -539,7 +569,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
lhs[static_cast<int64_t>(lhs_offset)] += rhs_data[rhs_offset];
|
||||
lhs.data()[lhs_offset] += rhs_data[rhs_offset];
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
@@ -547,18 +577,18 @@ namespace sd {
|
||||
template <typename T, typename Scalar, typename = std::enable_if_t<std::is_arithmetic<Scalar>::value>>
|
||||
inline Tensor<T>& operator+=(Tensor<T>& lhs, Scalar rhs) {
|
||||
const T value = static_cast<T>(rhs);
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
lhs[i] += value;
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
lhs.data()[i] += value;
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline Tensor<T>& operator-=(Tensor<T>& lhs, const Tensor<T>& rhs) {
|
||||
if (lhs.shape() == rhs.shape()) {
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
lhs[i] -= rhs[i];
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
lhs.data()[i] -= rhs.data()[i];
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
tensor_broadcast_shape(lhs.shape(), rhs.shape());
|
||||
@@ -571,7 +601,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
lhs[static_cast<int64_t>(lhs_offset)] -= rhs_data[rhs_offset];
|
||||
lhs.data()[lhs_offset] -= rhs_data[rhs_offset];
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
@@ -579,18 +609,18 @@ namespace sd {
|
||||
template <typename T, typename Scalar, typename = std::enable_if_t<std::is_arithmetic<Scalar>::value>>
|
||||
inline Tensor<T>& operator-=(Tensor<T>& lhs, Scalar rhs) {
|
||||
const T value = static_cast<T>(rhs);
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
lhs[i] -= value;
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
lhs.data()[i] -= value;
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline Tensor<T>& operator*=(Tensor<T>& lhs, const Tensor<T>& rhs) {
|
||||
if (lhs.shape() == rhs.shape()) {
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
lhs[i] *= rhs[i];
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
lhs.data()[i] *= rhs.data()[i];
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
tensor_broadcast_shape(lhs.shape(), rhs.shape());
|
||||
@@ -603,7 +633,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
lhs[static_cast<int64_t>(lhs_offset)] *= rhs_data[rhs_offset];
|
||||
lhs.data()[lhs_offset] *= rhs_data[rhs_offset];
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
@@ -611,18 +641,18 @@ namespace sd {
|
||||
template <typename T, typename Scalar, typename = std::enable_if_t<std::is_arithmetic<Scalar>::value>>
|
||||
inline Tensor<T>& operator*=(Tensor<T>& lhs, Scalar rhs) {
|
||||
const T value = static_cast<T>(rhs);
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
lhs[i] *= value;
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
lhs.data()[i] *= value;
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline Tensor<T>& operator/=(Tensor<T>& lhs, const Tensor<T>& rhs) {
|
||||
if (lhs.shape() == rhs.shape()) {
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
lhs[i] /= rhs[i];
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
lhs.data()[i] /= rhs.data()[i];
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
tensor_broadcast_shape(lhs.shape(), rhs.shape());
|
||||
@@ -635,7 +665,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
lhs[static_cast<int64_t>(lhs_offset)] /= rhs_data[rhs_offset];
|
||||
lhs.data()[lhs_offset] /= rhs_data[rhs_offset];
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
@@ -643,9 +673,9 @@ namespace sd {
|
||||
template <typename T, typename Scalar, typename = std::enable_if_t<std::is_arithmetic<Scalar>::value>>
|
||||
inline Tensor<T>& operator/=(Tensor<T>& lhs, Scalar rhs) {
|
||||
const T value = static_cast<T>(rhs);
|
||||
for (int64_t i = 0; i < lhs.numel(); ++i) {
|
||||
lhs[i] /= value;
|
||||
}
|
||||
tensor_for_each(lhs.numel(), [&](int64_t i) {
|
||||
lhs.data()[i] /= value;
|
||||
});
|
||||
return lhs;
|
||||
}
|
||||
|
||||
@@ -664,7 +694,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t flat, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
result[flat] = lhs_data[lhs_offset] + rhs_data[rhs_offset];
|
||||
result.data()[flat] = lhs_data[lhs_offset] + rhs_data[rhs_offset];
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -699,7 +729,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t flat, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
result[flat] = lhs_data[lhs_offset] - rhs_data[rhs_offset];
|
||||
result.data()[flat] = lhs_data[lhs_offset] - rhs_data[rhs_offset];
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -717,9 +747,9 @@ namespace sd {
|
||||
inline Tensor<T> operator-(Scalar lhs, const Tensor<T>& rhs) {
|
||||
Tensor<T> result = rhs;
|
||||
const T value = static_cast<T>(lhs);
|
||||
for (int64_t i = 0; i < result.numel(); ++i) {
|
||||
result[i] = value - result[i];
|
||||
}
|
||||
tensor_for_each(result.numel(), [&](int64_t i) {
|
||||
result.data()[i] = value - result.data()[i];
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -738,7 +768,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t flat, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
result[flat] = lhs_data[lhs_offset] * rhs_data[rhs_offset];
|
||||
result.data()[flat] = lhs_data[lhs_offset] * rhs_data[rhs_offset];
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -773,7 +803,7 @@ namespace sd {
|
||||
rhs.shape(),
|
||||
rhs_strides,
|
||||
[&](int64_t flat, int64_t lhs_offset, int64_t rhs_offset) {
|
||||
result[flat] = lhs_data[lhs_offset] / rhs_data[rhs_offset];
|
||||
result.data()[flat] = lhs_data[lhs_offset] / rhs_data[rhs_offset];
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -791,18 +821,18 @@ namespace sd {
|
||||
inline Tensor<T> operator/(Scalar lhs, const Tensor<T>& rhs) {
|
||||
Tensor<T> result = rhs;
|
||||
const T value = static_cast<T>(lhs);
|
||||
for (int64_t i = 0; i < result.numel(); ++i) {
|
||||
result[i] = value / result[i];
|
||||
}
|
||||
tensor_for_each(result.numel(), [&](int64_t i) {
|
||||
result.data()[i] = value / result.data()[i];
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline Tensor<T> operator-(const Tensor<T>& tensor) {
|
||||
Tensor<T> result = tensor;
|
||||
for (int64_t i = 0; i < result.numel(); ++i) {
|
||||
result[i] = -result[i];
|
||||
}
|
||||
tensor_for_each(result.numel(), [&](int64_t i) {
|
||||
result.data()[i] = -result.data()[i];
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1067,9 +1097,11 @@ namespace sd {
|
||||
template <typename T>
|
||||
inline Tensor<T> exp(const Tensor<T>& input) {
|
||||
Tensor<T> output(input.shape());
|
||||
for (int64_t i = 0; i < input.numel(); ++i) {
|
||||
output[i] = static_cast<T>(std::exp(static_cast<double>(input[i])));
|
||||
}
|
||||
tensor_for_each(
|
||||
input.numel(), [&](int64_t i) {
|
||||
output.data()[i] = static_cast<T>(std::exp(static_cast<double>(input.data()[i])));
|
||||
},
|
||||
4096);
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -1079,18 +1111,18 @@ namespace sd {
|
||||
tensor_throw_invalid_argument("Tensor clamp requires min_value <= max_value");
|
||||
}
|
||||
Tensor<T> output(input.shape());
|
||||
for (int64_t i = 0; i < input.numel(); ++i) {
|
||||
output[i] = std::clamp(input[i], min_value, max_value);
|
||||
}
|
||||
tensor_for_each(input.numel(), [&](int64_t i) {
|
||||
output.data()[i] = std::clamp(input.data()[i], min_value, max_value);
|
||||
});
|
||||
return output;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline Tensor<T> round(const Tensor<T>& input) {
|
||||
Tensor<T> output(input.shape());
|
||||
for (int64_t i = 0; i < input.numel(); ++i) {
|
||||
output[i] = static_cast<T>(std::round(static_cast<double>(input[i])));
|
||||
}
|
||||
tensor_for_each(input.numel(), [&](int64_t i) {
|
||||
output.data()[i] = static_cast<T>(std::round(static_cast<double>(input.data()[i])));
|
||||
});
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <type_traits>
|
||||
|
||||
#include "core/tensor.hpp"
|
||||
#include "ggml-backend.h"
|
||||
#include "ggml.h"
|
||||
|
||||
namespace sd {
|
||||
@@ -54,10 +55,28 @@ namespace sd {
|
||||
GGML_ABORT("ggml tensor type does not match sd::Tensor type");
|
||||
}
|
||||
Tensor<T> result(shape_from_ggml(tensor));
|
||||
if (tensor->buffer != nullptr) {
|
||||
ggml_backend_tensor_get(tensor, result.data(), 0, ggml_nbytes(tensor));
|
||||
std::vector<uint8_t> strided_data;
|
||||
void* destination = result.data();
|
||||
if (!ggml_is_contiguous(tensor)) {
|
||||
strided_data.resize(ggml_nbytes(tensor));
|
||||
destination = strided_data.data();
|
||||
}
|
||||
auto buffer = tensor->view_src != nullptr ? tensor->view_src->buffer : tensor->buffer;
|
||||
if (buffer != nullptr) {
|
||||
ggml_backend_tensor_get(tensor, destination, 0, ggml_nbytes(tensor));
|
||||
} else {
|
||||
std::memcpy(result.data(), tensor->data, ggml_nbytes(tensor));
|
||||
std::memcpy(destination, tensor->data, ggml_nbytes(tensor));
|
||||
}
|
||||
if (!strided_data.empty()) {
|
||||
for (int64_t i = 0; i < result.numel(); ++i) {
|
||||
int64_t index = i;
|
||||
size_t offset = 0;
|
||||
for (int d = 0; d < GGML_MAX_DIMS; ++d) {
|
||||
offset += static_cast<size_t>(index % tensor->ne[d]) * tensor->nb[d];
|
||||
index /= tensor->ne[d];
|
||||
}
|
||||
std::memcpy(result.data() + i, strided_data.data() + offset, sizeof(T));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
+141
-33
@@ -15,6 +15,7 @@
|
||||
#include <thread>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
#include "core/ggml_tensor_utils.h"
|
||||
#include "runtime/preprocessing.hpp"
|
||||
|
||||
#if defined(__APPLE__) && defined(__MACH__)
|
||||
@@ -61,17 +62,34 @@ void replace_all_chars(std::string& str, char target, char replacement) {
|
||||
}
|
||||
}
|
||||
|
||||
static std::string sd_vformat(const char* fmt, va_list ap) {
|
||||
char small[128];
|
||||
va_list ap2;
|
||||
va_copy(ap2, ap);
|
||||
int size = vsnprintf(small, sizeof small, fmt, ap);
|
||||
if (size < 0) {
|
||||
va_end(ap2);
|
||||
return {};
|
||||
}
|
||||
size_t needed = (size_t)size;
|
||||
if (needed < sizeof small) {
|
||||
va_end(ap2);
|
||||
return std::string(small, needed);
|
||||
}
|
||||
std::string out(needed, '\0');
|
||||
int size2 = vsnprintf(out.data(), needed + 1, fmt, ap2);
|
||||
va_end(ap2);
|
||||
if (size2 < 0)
|
||||
out.clear();
|
||||
return out;
|
||||
}
|
||||
|
||||
std::string sd_format(const char* fmt, ...) {
|
||||
va_list ap;
|
||||
va_list ap2;
|
||||
va_start(ap, fmt);
|
||||
va_copy(ap2, ap);
|
||||
int size = vsnprintf(nullptr, 0, fmt, ap);
|
||||
std::vector<char> buf(size + 1);
|
||||
int size2 = vsnprintf(buf.data(), size + 1, fmt, ap2);
|
||||
va_end(ap2);
|
||||
std::string result = sd_vformat(fmt, ap);
|
||||
va_end(ap);
|
||||
return std::string(buf.data(), size);
|
||||
return result;
|
||||
}
|
||||
|
||||
int round_up_to(int value, int base) {
|
||||
@@ -413,14 +431,43 @@ std::vector<std::string> split_string(const std::string& str, char delimiter) {
|
||||
}
|
||||
|
||||
ggml_type sd_type_to_ggml_type(sd_type_t sdtype) {
|
||||
if (sdtype == SD_TYPE_F8_E4M3 || sdtype == SD_TYPE_F8_E5M2) {
|
||||
#ifndef SD_USE_UPSTREAM_GGML
|
||||
return sdtype == SD_TYPE_F8_E4M3 ? GGML_TYPE_F8_E4M3 : GGML_TYPE_F8_E5M2;
|
||||
#else
|
||||
return GGML_TYPE_COUNT;
|
||||
#endif
|
||||
}
|
||||
const int type_value = static_cast<int>(sdtype);
|
||||
if (type_value < std::min<int>(SD_TYPE_COUNT, GGML_TYPE_COUNT)) {
|
||||
if (type_value >= 0 && type_value < std::min<int>(SD_TYPE_COUNT, GGML_TYPE_COUNT)) {
|
||||
return static_cast<ggml_type>(type_value);
|
||||
} else {
|
||||
return GGML_TYPE_COUNT;
|
||||
}
|
||||
}
|
||||
|
||||
bool validate_tensor_types(sd_type_t type, const char* tensor_type_rules) {
|
||||
if (type != SD_TYPE_COUNT && sd_type_to_ggml_type(type) == GGML_TYPE_COUNT) {
|
||||
LOG_ERROR("weight type %s is not supported by this ggml build", sd_type_name(type));
|
||||
return false;
|
||||
}
|
||||
#ifdef SD_USE_UPSTREAM_GGML
|
||||
for (const auto& rule : split_string(SAFE_STR(tensor_type_rules), ',')) {
|
||||
const auto pos = rule.find('=');
|
||||
if (pos != std::string::npos) {
|
||||
const auto name = rule.substr(pos + 1);
|
||||
if (name == "f8_e4m3" || name == "f8_e5m2") {
|
||||
LOG_ERROR("FP8 is not supported by this ggml build (tensor type rule '%s')", rule.c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
GGML_UNUSED(tensor_type_rules);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
KeyValueArgs parse_key_value_args(const char* args, const char* context) {
|
||||
KeyValueArgs pairs;
|
||||
|
||||
@@ -594,28 +641,45 @@ std::string trim(const std::string& s) {
|
||||
static sd_log_cb_t sd_log_cb = nullptr;
|
||||
void* sd_log_cb_data = nullptr;
|
||||
|
||||
#define LOG_BUFFER_SIZE 4096
|
||||
static void sd_log_dispatch(sd_log_level_t level, const std::string& origin, const std::string& text) {
|
||||
if (sd_log_cb == nullptr)
|
||||
return;
|
||||
std::string message = origin + " - " + text;
|
||||
if (message.back() != '\n') {
|
||||
message += '\n';
|
||||
}
|
||||
sd_log_cb(level, message.c_str(), sd_log_cb_data);
|
||||
}
|
||||
|
||||
void log_printf(sd_log_level_t level, const char* file, int line, const char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
static char log_buffer[LOG_BUFFER_SIZE + 1];
|
||||
int written = snprintf(log_buffer, LOG_BUFFER_SIZE, "%s:%-4d - ", sd_basename(file).c_str(), line);
|
||||
|
||||
if (written >= 0 && written < LOG_BUFFER_SIZE) {
|
||||
vsnprintf(log_buffer + written, LOG_BUFFER_SIZE - written, format, args);
|
||||
}
|
||||
size_t len = strlen(log_buffer);
|
||||
if (log_buffer[len - 1] != '\n') {
|
||||
strncat(log_buffer, "\n", LOG_BUFFER_SIZE - len);
|
||||
}
|
||||
|
||||
if (sd_log_cb) {
|
||||
sd_log_cb(level, log_buffer, sd_log_cb_data);
|
||||
}
|
||||
|
||||
std::string message = sd_vformat(format, args);
|
||||
va_end(args);
|
||||
std::string origin = sd_format("%s:%-4d", sd_basename(file).c_str(), line);
|
||||
sd_log_dispatch(level, origin, message);
|
||||
}
|
||||
|
||||
void sd_ggml_log_callback(ggml_log_level level, const char* text, void*) {
|
||||
sd_log_level_t sd_level = SD_LOG_VERBOSE;
|
||||
switch (level) {
|
||||
case GGML_LOG_LEVEL_DEBUG:
|
||||
sd_level = SD_LOG_VERBOSE;
|
||||
break;
|
||||
case GGML_LOG_LEVEL_INFO:
|
||||
sd_level = SD_LOG_INFO;
|
||||
break;
|
||||
case GGML_LOG_LEVEL_WARN:
|
||||
sd_level = SD_LOG_WARN;
|
||||
break;
|
||||
case GGML_LOG_LEVEL_ERROR:
|
||||
sd_level = SD_LOG_ERROR;
|
||||
break;
|
||||
default:
|
||||
sd_level = SD_LOG_VERBOSE;
|
||||
break;
|
||||
}
|
||||
sd_log_dispatch(sd_level, "ggml", text);
|
||||
}
|
||||
|
||||
void sd_set_log_callback(sd_log_cb_t cb, void* data) {
|
||||
@@ -735,6 +799,13 @@ sd::Tensor<float> clip_preprocess(const sd::Tensor<float>& image, int target_wid
|
||||
int64_t resized_width = static_cast<int64_t>(scale * static_cast<float>(image.shape()[0]));
|
||||
int64_t resized_height = static_cast<int64_t>(scale * static_cast<float>(image.shape()[1]));
|
||||
|
||||
// The resized image must cover the crop window. Floating-point rounding can
|
||||
// leave a side one pixel short of the crop target (e.g. 730 -> 735.999...
|
||||
// -> 735 after truncation), so clamp to keep the center crop in bounds.
|
||||
// Truncation is otherwise preserved to avoid changing existing results.
|
||||
resized_width = std::max<int64_t>(resized_width, target_width);
|
||||
resized_height = std::max<int64_t>(resized_height, target_height);
|
||||
|
||||
sd::Tensor<float> resized = sd::ops::interpolate(
|
||||
image,
|
||||
{resized_width, resized_height, image.shape()[2], image.shape()[3]});
|
||||
@@ -801,7 +872,11 @@ std::vector<std::pair<std::string, float>> parse_prompt_attention(const std::str
|
||||
float round_bracket_multiplier = 1.1f;
|
||||
float square_bracket_multiplier = 1 / 1.1f;
|
||||
|
||||
std::regex re_attention(R"(\\\(|\\\)|\\\[|\\\]|\\\\|\\|\(|\[|:([+-]?[.\d]+)\)|\)|\]|\bBREAK\b|[^\\()\[\]:B]+|:|\bB)");
|
||||
// libstdc++ std::regex recurses per matched character, so unbounded runs
|
||||
// overflow the stack. Split runs are merged back below.
|
||||
const int max_plain_text_run = 1024;
|
||||
std::regex re_attention(R"(\\\(|\\\)|\\\[|\\\]|\\\\|\\|\(|\[|\)|\]|\bBREAK\b|[^\\()\[\]:B]{1,)" +
|
||||
std::to_string(max_plain_text_run) + R"(}|:|\bB)");
|
||||
std::regex re_break(R"(\s*\bBREAK\b\s*)");
|
||||
|
||||
auto multiply_range = [&](int start_position, float multiplier) {
|
||||
@@ -810,22 +885,55 @@ std::vector<std::pair<std::string, float>> parse_prompt_attention(const std::str
|
||||
}
|
||||
};
|
||||
|
||||
// Kept out of the regex: bounding the repetition rejects valid long weights,
|
||||
// leaving it unbounded overflows the stack.
|
||||
auto lex_weight = [](const std::string& s, float& value) -> size_t {
|
||||
size_t end = 0;
|
||||
if (end < s.size() && (s[end] == '+' || s[end] == '-')) {
|
||||
++end;
|
||||
}
|
||||
while (end < s.size() && (std::isdigit((unsigned char)s[end]) || s[end] == '.')) {
|
||||
++end;
|
||||
}
|
||||
if (end >= s.size() || s[end] != ')') {
|
||||
return 0;
|
||||
}
|
||||
std::string number = s.substr(0, end);
|
||||
char* number_end = nullptr;
|
||||
float parsed = std::strtof(number.c_str(), &number_end);
|
||||
const char* expected = number.c_str() + number.size();
|
||||
// Without this ".", "+." and "1.2.3" would silently become weights.
|
||||
if (number.empty() || number_end != expected || !std::isfinite(parsed)) {
|
||||
return 0;
|
||||
}
|
||||
value = parsed;
|
||||
return end + 1;
|
||||
};
|
||||
|
||||
std::smatch m, m2;
|
||||
std::string remaining_text = text;
|
||||
|
||||
while (std::regex_search(remaining_text, m, re_attention)) {
|
||||
std::string text = m[0];
|
||||
std::string weight = m[1];
|
||||
std::string suffix = m.suffix();
|
||||
|
||||
if (text == ":") {
|
||||
float weight_value = 1.0f;
|
||||
size_t weight_length = lex_weight(suffix, weight_value);
|
||||
if (weight_length > 0) {
|
||||
if (!round_brackets.empty()) {
|
||||
multiply_range(round_brackets.back(), weight_value);
|
||||
round_brackets.pop_back();
|
||||
}
|
||||
remaining_text = suffix.substr(weight_length);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (text == "(") {
|
||||
round_brackets.push_back((int)res.size());
|
||||
} else if (text == "[") {
|
||||
square_brackets.push_back((int)res.size());
|
||||
} else if (!weight.empty()) {
|
||||
if (!round_brackets.empty()) {
|
||||
multiply_range(round_brackets.back(), std::stof(weight));
|
||||
round_brackets.pop_back();
|
||||
}
|
||||
} else if (text == ")" && !round_brackets.empty()) {
|
||||
multiply_range(round_brackets.back(), round_bracket_multiplier);
|
||||
round_brackets.pop_back();
|
||||
@@ -840,7 +948,7 @@ std::vector<std::pair<std::string, float>> parse_prompt_attention(const std::str
|
||||
res.push_back({text, 1.0f});
|
||||
}
|
||||
|
||||
remaining_text = m.suffix();
|
||||
remaining_text = suffix;
|
||||
}
|
||||
|
||||
for (int pos : round_brackets) {
|
||||
|
||||
@@ -11,6 +11,14 @@
|
||||
#include "ggml-backend.h"
|
||||
#include "stable-diffusion.h"
|
||||
|
||||
#ifndef __STATIC_INLINE__
|
||||
#define __STATIC_INLINE__ static inline
|
||||
#endif
|
||||
|
||||
#ifndef SD_UNUSED
|
||||
#define SD_UNUSED(x) (void)(x)
|
||||
#endif
|
||||
|
||||
#define SAFE_STR(s) ((s) ? (s) : "")
|
||||
#define BOOL_STR(b) ((b) ? "true" : "false")
|
||||
|
||||
@@ -79,8 +87,10 @@ void pretty_progress(int step, int steps, float time);
|
||||
void pretty_bytes_progress(int step, int steps, uint64_t bytes_processed, float elapsed_seconds);
|
||||
|
||||
void log_printf(sd_log_level_t level, const char* file, int line, const char* format, ...);
|
||||
void sd_ggml_log_callback(ggml_log_level level, const char* text, void*);
|
||||
|
||||
ggml_type sd_type_to_ggml_type(sd_type_t sdtype);
|
||||
bool validate_tensor_types(sd_type_t type, const char* tensor_type_rules);
|
||||
|
||||
std::string trim(const std::string& s);
|
||||
|
||||
@@ -105,6 +115,7 @@ void* sd_get_backend_eval_callback_data();
|
||||
bool sd_backend_is(ggml_backend_t backend, const std::string& name);
|
||||
|
||||
#define LOG_DEBUG(format, ...) log_printf(SD_LOG_DEBUG, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
#define LOG_VERBOSE(format, ...) log_printf(SD_LOG_VERBOSE, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
#define LOG_INFO(format, ...) log_printf(SD_LOG_INFO, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
#define LOG_WARN(format, ...) log_printf(SD_LOG_WARN, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
#define LOG_ERROR(format, ...) log_printf(SD_LOG_ERROR, __FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user