Compare commits

..

No commits in common. "88411ef1e0688ff2df1010aeeb5d92b2d8cea2be" and "c92d73c408515c94beef32161bb5960764fde7a0" have entirely different histories.

45 changed files with 752 additions and 1375 deletions

View File

@ -1,61 +0,0 @@
name: Close PRs from organization forks
on:
pull_request_target:
types: [opened, reopened]
permissions:
pull-requests: write
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: false
jobs:
close-organization-fork-pr:
if: >-
github.event.pull_request.head.repo.owner.type == 'Organization' &&
github.event.pull_request.head.repo.id != github.event.pull_request.base.repo.id
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Explain the contribution policy and close the PR
uses: actions/github-script@v9
with:
script: |
const { data: pr } = await github.rest.pulls.get({
...context.repo,
pull_number: context.issue.number,
});
const headRepo = pr.head.repo;
if (pr.state !== 'open' || !headRepo ||
headRepo.id === pr.base.repo.id || headRepo.owner.type !== 'Organization') {
return;
}
const marker = '<!-- organization-fork-policy -->';
const comments = await github.paginate(github.rest.issues.listComments, {
...context.repo,
issue_number: pr.number,
per_page: 100,
});
const alreadyExplained = comments.some(comment =>
comment.user?.login === 'github-actions[bot]' && comment.body?.includes(marker));
if (!alreadyExplained) {
await github.rest.issues.createComment({
...context.repo,
issue_number: pr.number,
body: [
marker,
'This repository requires contributions from forks to use a personal fork with **Allow edits from maintainers** enabled.',
'GitHub does not support this option for organization-owned forks, so this PR is being closed automatically.',
'Please open a new PR from a fork in your personal GitHub account and enable **Allow edits from maintainers** so maintainers can help update the branch.',
'See [the GitHub documentation](https://docs.github.com/en/pull-requests/how-tos/work-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork).',
].join('\n\n'),
});
}
await github.rest.pulls.update({
...context.repo,
pull_number: pr.number,
state: 'closed',
});

View File

@ -12,10 +12,6 @@ If you want to update a third-party dependency, please open an issue first inste
## Pull Requests ## Pull Requests
When contributing from a fork, use a fork under your personal GitHub account and enable **Allow edits from maintainers**. This lets maintainers make follow-up fixes directly on the PR branch.
PRs from organization-owned forks are automatically closed when opened or reopened because GitHub does not support this maintainer-edit option for those forks. Submit the changes from a personal fork instead. See [GitHub's documentation](https://docs.github.com/en/pull-requests/how-tos/work-with-forks/allowing-changes-to-a-pull-request-branch-created-from-a-fork).
Keep each PR focused on one clear change. Large or overly complex PRs are harder to review and may not be merged. 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. Do not include test code or test scripts in commits or PRs. Keep them local and report verification results in the PR description.

View File

@ -161,9 +161,6 @@ resident allocations. Vulkan reports exceeding total memory are rejected because
its heap-budget subtraction can underflow. Other backends use the cap instead of its heap-budget subtraction can underflow. Other backends use the cap instead of
treating such reports as zero free memory. Failed checks log the reported free and treating such reports as zero free memory. Failed checks log the reported free and
total memory alongside tracked weight and runtime allocations. total memory alongside tracked weight and runtime allocations.
With `--mmap`, device-backed mappings count toward these budgets at their full
mapped-file size, once per device buffer even when multiple parameter blocks
share it. Mappings retained in the loader cache continue to count.
Components are considered in `diffusion`, `te`, `vae` order so that repeatedly Components are considered in `diffusion`, `te`, `vae` order so that repeatedly
used diffusion weights have priority. Each component's weights use the first used diffusion weights have priority. Each component's weights use the first

View File

@ -2,16 +2,6 @@
Caching methods accelerate diffusion inference by reusing intermediate computations when changes between steps are small. Caching methods accelerate diffusion inference by reusing intermediate computations when changes between steps are small.
### Conditioning Cache
Conditioning results are cached per model context using an LRU cache. The default
capacity is **0 (disabled) for `sd-cli`** and **4 entries for `sd-server` and the C
API**. Set `--conditioning-cache-size N` to change the limit; `0` disables caching.
For example, `sd-cli -m model.safetensors -p "a cat" --conditioning-cache-size 4`
enables the cache in the CLI. The C API option is
`sd_ctx_params_t::conditioning_cache_size`, initialized by `sd_ctx_params_init()`.
This cache is independent of the diffusion-step `--cache-mode` options below.
### Cache Modes ### Cache Modes
| Mode | Target | Description | | Mode | Target | Description |

View File

@ -40,14 +40,6 @@ Pass the reference image with `-r` and describe the edit in `-p`. Vision weights
For multiple reference images, repeat `-r` in the desired order, for example `-r first.png -r second.png`. For multiple reference images, repeat `-r` in the desired order, for example `-r first.png -r second.png`.
### Prefix cache
By default, the first denoising call for each fixed condition saves the text and reference-image keys and values from every transformer layer. Later calls only compute the target-image tokens. Positive and negative conditions use separate caches, which are released when sampling ends.
The cache uses FP32 on all attention backends. For the default 32-layer model, a prefix of 4096 tokens takes about 4 GiB per condition, in addition to weights and working buffers. The runner accounts for the cache when checking the memory budget. If a cached execution runs out of memory, it releases the prefix caches, disables caching for the rest of that sampling run, and retries the full sequence once. Per-step conditioning extensions currently use the full-sequence path.
Disable this optimization with `--model-args qwen_image_2_1_prefix_cache=false`. It reuses step-independent activations; numerical results can still differ slightly because the matrix sizes change.
### Alpha channel ### Alpha channel
This model supports alpha channel output. As the model determines whether to output a regular image or with transparency through the prompt, according to [official recommendation](https://github.com/QwenLM/Qwen-Image-2.1#transparent-image-generation-rgba), use the following prompt format for better results: This model supports alpha channel output. As the model determines whether to output a regular image or with transparency through the prompt, according to [official recommendation](https://github.com/QwenLM/Qwen-Image-2.1#transparent-image-generation-rgba), use the following prompt format for better results:

View File

@ -644,7 +644,6 @@ int main(int argc, const char* argv[]) {
SDCliParams cli_params; SDCliParams cli_params;
SDContextParams ctx_params; SDContextParams ctx_params;
ctx_params.conditioning_cache_size = 0;
SDGenerationParams gen_params; SDGenerationParams gen_params;
sd_set_log_callback(sd_log_cb, (void*)&cli_params); sd_set_log_callback(sd_log_cb, (void*)&cli_params);

View File

@ -518,7 +518,7 @@ ArgOptions SDContextParams::get_options() {
{"", {"",
"--model-args", "--model-args",
"extra model args, key=value list. Supports chroma_use_dit_mask, chroma_use_t5_mask, " "extra model args, key=value list. Supports chroma_use_dit_mask, chroma_use_t5_mask, "
"chroma_t5_mask_pad, qwen_image_zero_cond_t, qwen_image_2_1_prefix_cache", "chroma_t5_mask_pad, qwen_image_zero_cond_t",
(int)',', (int)',',
&model_args}, &model_args},
{"", {"",
@ -571,10 +571,6 @@ ArgOptions SDContextParams::get_options() {
"number of threads to use during computation (default: -1). " "number of threads to use during computation (default: -1). "
"If threads <= 0, then threads will be set to the number of CPU physical cores", "If threads <= 0, then threads will be set to the number of CPU physical cores",
&n_threads}, &n_threads},
{"",
"--conditioning-cache-size",
"maximum number of conditioning results cached per model context (default: " + std::to_string(conditioning_cache_size) + ", 0 disables caching)",
&conditioning_cache_size},
}; };
options.bool_options = { options.bool_options = {
@ -826,10 +822,6 @@ bool SDContextParams::resolve(SDMode mode) {
} }
bool SDContextParams::validate(SDMode mode) { bool SDContextParams::validate(SDMode mode) {
if (conditioning_cache_size < 0) {
LOG_ERROR("error: conditioning-cache-size must be non-negative");
return false;
}
if (mode == CONVERT) { if (mode == CONVERT) {
const bool has_convert_input = model_path.length() != 0 || const bool has_convert_input = model_path.length() != 0 ||
clip_l_path.length() != 0 || clip_l_path.length() != 0 ||
@ -906,7 +898,6 @@ std::string SDContextParams::to_string() const {
std::ostringstream oss; std::ostringstream oss;
oss << "SDContextParams {\n" oss << "SDContextParams {\n"
<< " n_threads: " << n_threads << ",\n" << " n_threads: " << n_threads << ",\n"
<< " conditioning_cache_size: " << conditioning_cache_size << ",\n"
<< " model_path: \"" << model_path << "\",\n" << " model_path: \"" << model_path << "\",\n"
<< " clip_l_path: \"" << clip_l_path << "\",\n" << " clip_l_path: \"" << clip_l_path << "\",\n"
<< " clip_g_path: \"" << clip_g_path << "\",\n" << " clip_g_path: \"" << clip_g_path << "\",\n"
@ -1001,7 +992,6 @@ sd_ctx_params_t SDContextParams::to_sd_ctx_params_t(bool taesd_preview) {
sd_ctx_params.pulid_weights_path = pulid_weights_path.c_str(); sd_ctx_params.pulid_weights_path = pulid_weights_path.c_str();
sd_ctx_params.tensor_type_rules = tensor_type_rules.c_str(); sd_ctx_params.tensor_type_rules = tensor_type_rules.c_str();
sd_ctx_params.n_threads = n_threads; sd_ctx_params.n_threads = n_threads;
sd_ctx_params.conditioning_cache_size = conditioning_cache_size;
sd_ctx_params.wtype = wtype; sd_ctx_params.wtype = wtype;
sd_ctx_params.rng_type = rng_type; sd_ctx_params.rng_type = rng_type;
sd_ctx_params.sampler_rng_type = sampler_rng_type; sd_ctx_params.sampler_rng_type = sampler_rng_type;

View File

@ -116,8 +116,7 @@ bool decode_base64_image(const std::string& encoded_input,
SDImageOwner& out_image); SDImageOwner& out_image);
struct SDContextParams { struct SDContextParams {
int n_threads = -1; int n_threads = -1;
int conditioning_cache_size = 4;
std::string model_path; std::string model_path;
std::string clip_l_path; std::string clip_l_path;
std::string clip_g_path; std::string clip_g_path;

View File

@ -247,7 +247,6 @@ typedef struct {
float attn_scale; // Override flash-attention K/V 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 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 sage_attn;
int conditioning_cache_size; // Maximum cached conditioning entries per context; 0 disables caching (default: 4)
} sd_ctx_params_t; } sd_ctx_params_t;
typedef struct { typedef struct {

View File

@ -137,6 +137,7 @@ struct ConditionerParams {
const std::vector<sd::Tensor<float>>* ref_images = nullptr; // for qwen image edit const std::vector<sd::Tensor<float>>* ref_images = nullptr; // for qwen image edit
const std::vector<MiniMaxH3PresentationItem>* minimax_h3_references = nullptr; const std::vector<MiniMaxH3PresentationItem>* minimax_h3_references = nullptr;
RefImageParams ref_image_params; RefImageParams ref_image_params;
bool allow_cache = false;
}; };
struct Conditioner { struct Conditioner {
@ -1954,6 +1955,10 @@ struct LLMEmbedder : public Conditioner {
std::shared_ptr<LLM::LLMRunner> llm; std::shared_ptr<LLM::LLMRunner> llm;
std::shared_ptr<T5Runner> byt5; std::shared_ptr<T5Runner> byt5;
bool h3_text_cache_valid = false;
std::string h3_text_cache_text;
SDCondition h3_text_cache;
LLMEmbedder(ggml_backend_t backend, LLMEmbedder(ggml_backend_t backend,
const String2TensorStorage& tensor_storage_map = {}, const String2TensorStorage& tensor_storage_map = {},
SDVersion version = VERSION_QWEN_IMAGE, SDVersion version = VERSION_QWEN_IMAGE,
@ -2298,6 +2303,25 @@ struct LLMEmbedder : public Conditioner {
SDCondition get_learned_condition(int n_threads, SDCondition get_learned_condition(int n_threads,
const ConditionerParams& conditioner_params) override { const ConditionerParams& conditioner_params) override {
const bool h3_text_cacheable =
sd_version_is_minimax_h3(version) &&
conditioner_params.allow_cache &&
(conditioner_params.minimax_h3_references == nullptr ||
conditioner_params.minimax_h3_references->empty()) &&
(conditioner_params.ref_images == nullptr ||
conditioner_params.ref_images->empty());
if (sd_version_is_minimax_h3(version) && !h3_text_cacheable) {
h3_text_cache_valid = false;
}
if (h3_text_cacheable &&
h3_text_cache_valid &&
h3_text_cache_text == conditioner_params.text) {
LOG_INFO("H3 conditioning cache hit");
return h3_text_cache;
}
std::string prompt; std::string prompt;
std::pair<int, int> prompt_attn_range; std::pair<int, int> prompt_attn_range;
std::vector<std::string> extra_prompts; std::vector<std::string> extra_prompts;
@ -3167,6 +3191,13 @@ struct LLMEmbedder : public Conditioner {
result.c_token_types = sd::Tensor<int32_t>({tag_count}, std::move(tags)); result.c_token_types = sd::Tensor<int32_t>({tag_count}, std::move(tags));
} }
if (h3_text_cacheable) {
h3_text_cache_text = conditioner_params.text;
h3_text_cache = result;
h3_text_cache_valid = true;
LOG_INFO("H3 conditioning cache stored");
}
return result; return result;
} }
}; };

View File

@ -1,107 +0,0 @@
#ifndef __SD_CONDITIONING_CONDITIONING_CACHE_H__
#define __SD_CONDITIONING_CONDITIONING_CACHE_H__
#include <algorithm>
#include <list>
#include <tuple>
#include "conditioning/conditioner.hpp"
class ConditioningCache {
struct Entry {
ConditionerParams params;
std::vector<sd::Tensor<float>> ref_images;
std::vector<MiniMaxH3PresentationItem> references;
SDCondition condition;
Entry(const ConditionerParams& input, const SDCondition& output)
: params(input), condition(output) {
// Request-owned reference pointers must not outlive the request.
if (input.ref_images != nullptr) {
ref_images = *input.ref_images;
params.ref_images = &ref_images;
}
if (input.minimax_h3_references != nullptr) {
references = *input.minimax_h3_references;
params.minimax_h3_references = &references;
}
}
Entry(const Entry&) = delete;
Entry& operator=(const Entry&) = delete;
};
size_t capacity_ = 4;
std::list<Entry> entries_;
static bool same_images(const std::vector<sd::Tensor<float>>& a,
const std::vector<sd::Tensor<float>>& b) {
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
[](const sd::Tensor<float>& x, const sd::Tensor<float>& y) {
return x.shape() == y.shape() && x.values() == y.values();
});
}
static bool same_params(const ConditionerParams& a, const ConditionerParams& b) {
const auto fields = [](const ConditionerParams& p) {
const auto& r = p.ref_image_params;
return std::tie(p.text, p.clip_skip, p.width, p.height, p.zero_out_masked,
r.pass_to_vlm, r.pass_to_dit, r.ref_index_mode,
r.force_ref_timestep_zero, r.resize_before_vae, r.vae_input_max_pixels,
r.vlm_resize_mode, r.vlm_min_size, r.vlm_max_size, r.resize_vae_to_target);
};
if (fields(a) != fields(b) ||
(a.ref_images == nullptr) != (b.ref_images == nullptr) ||
(a.minimax_h3_references == nullptr) != (b.minimax_h3_references == nullptr)) {
return false;
}
if (a.ref_images != nullptr && !same_images(*a.ref_images, *b.ref_images)) {
return false;
}
if (a.minimax_h3_references != nullptr &&
!std::equal(a.minimax_h3_references->begin(), a.minimax_h3_references->end(),
b.minimax_h3_references->begin(), b.minimax_h3_references->end(),
[](const MiniMaxH3PresentationItem& x, const MiniMaxH3PresentationItem& y) {
return x.kind == y.kind && x.timestamps == y.timestamps && same_images(x.frames, y.frames);
})) {
return false;
}
return true;
}
public:
void set_capacity(size_t capacity) {
capacity_ = capacity;
while (entries_.size() > capacity_) {
entries_.pop_back();
}
}
void clear() {
entries_.clear();
}
SDCondition get(Conditioner& conditioner, int n_threads, const ConditionerParams& params) {
if (capacity_ == 0) {
return conditioner.get_learned_condition(n_threads, params);
}
for (auto it = entries_.begin(); it != entries_.end(); ++it) {
if (same_params(it->params, params)) {
entries_.splice(entries_.begin(), entries_, it);
LOG_INFO("conditioning cache hit");
return entries_.front().condition;
}
}
auto condition = conditioner.get_learned_condition(n_threads, params);
if (!condition.empty()) {
if (entries_.size() == capacity_) {
entries_.pop_back();
}
entries_.emplace_front(params, condition);
LOG_VERBOSE("conditioning cache stored (%zu/%zu)", entries_.size(), capacity_);
}
return condition;
}
};
#endif // __SD_CONDITIONING_CONDITIONING_CACHE_H__

View File

@ -13,7 +13,6 @@
#endif #endif
#include "core/util.h" #include "core/util.h"
#include "ggml-backend-impl.h"
#include "ggml-impl.h" #include "ggml-impl.h"
#include "stable-diffusion.h" #include "stable-diffusion.h"
@ -434,24 +433,6 @@ bool sd_backend_is_cpu(ggml_backend_t backend) {
return dev != nullptr && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU; return dev != nullptr && ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU;
} }
ggml_backend_buffer_t sd_backend_dev_buffer_from_host_ptr(ggml_backend_dev_t device,
void* ptr,
size_t size,
size_t max_tensor_size) {
ggml_backend_buffer_t buffer = ggml_backend_dev_buffer_from_host_ptr(device, ptr, size, max_tensor_size);
if (buffer != nullptr && buffer->context == nullptr) {
ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(device);
if (reg != nullptr && std::strcmp(ggml_backend_reg_name(reg), "Metal") == 0) {
// Metal can wrap a failed mapping in a non-null buffer. Its free callback also
// dereferences the missing context, so only release the outer buffer.
buffer->iface.free_buffer = nullptr;
ggml_backend_buffer_free(buffer);
return nullptr;
}
}
return buffer;
}
bool sd_backend_supports_cuda_mma(ggml_backend_t backend) { bool sd_backend_supports_cuda_mma(ggml_backend_t backend) {
#ifdef SD_USE_CUDA #ifdef SD_USE_CUDA
if (!sd_backend_is(backend, "CUDA")) { if (!sd_backend_is(backend, "CUDA")) {

View File

@ -88,10 +88,6 @@ private:
bool sd_backend_is(ggml_backend_t backend, const std::string& name); bool sd_backend_is(ggml_backend_t backend, const std::string& name);
bool sd_backend_is_cpu(ggml_backend_t backend); bool sd_backend_is_cpu(ggml_backend_t backend);
bool sd_backend_supports_cuda_mma(ggml_backend_t backend); bool sd_backend_supports_cuda_mma(ggml_backend_t backend);
ggml_backend_buffer_t sd_backend_dev_buffer_from_host_ptr(ggml_backend_dev_t device,
void* ptr,
size_t size,
size_t max_tensor_size);
ggml_backend_t sd_backend_cpu_init(); ggml_backend_t sd_backend_cpu_init();
bool sd_backend_cpu_set_n_threads(ggml_backend_t backend_cpu, int n_threads); 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, ggml_status sd_backend_graph_compute_with_eval_callback(ggml_backend_t backend,

View File

@ -644,10 +644,6 @@ std::optional<sd::Tensor<float>> GGMLRunner::compute(get_graph_cb_t get_graph,
std::optional<sd::Tensor<float>> output; std::optional<sd::Tensor<float>> output;
try { try {
output = execute_graph(graph, n_threads, no_return, read_outputs); output = execute_graph(graph, n_threads, no_return, read_outputs);
} catch (const std::bad_alloc&) {
last_compute_status_ = GGML_STATUS_ALLOC_FAILED;
LOG_ERROR("%s graph allocation failed", get_desc().c_str());
return std::nullopt;
} catch (const std::exception& error) { } catch (const std::exception& error) {
last_compute_status_ = GGML_STATUS_FAILED; last_compute_status_ = GGML_STATUS_FAILED;
LOG_ERROR("%s graph execution failed on %s: %s", get_desc().c_str(), LOG_ERROR("%s graph execution failed on %s: %s", get_desc().c_str(),
@ -968,16 +964,10 @@ std::optional<Tensor<float>> GGMLRunner::execute_graph(ggml_cgraph* graph, int n
} }
LOG_DEBUG("%s executing segment %zu/%zu: %s", get_desc().c_str(), LOG_DEBUG("%s executing segment %zu/%zu: %s", get_desc().c_str(),
index + 1, plan.segments.size(), segment.group_name.c_str()); index + 1, plan.segments.size(), segment.group_name.c_str());
if (!execute_segment(segment_graph, n_threads)) { if (!execute_segment(segment_graph, n_threads) ||
return fail_segment("execution"); !cache_.capture(segment_graph) ||
} !cut_cache_.capture(graph, segment, get_desc().c_str())) {
auto cache_status = cache_.capture(segment_graph); return fail_segment("execution or output caching");
if (cache_status == GGML_STATUS_SUCCESS) {
cache_status = cut_cache_.capture(graph, segment, get_desc().c_str());
}
if (cache_status != GGML_STATUS_SUCCESS) {
last_compute_status_ = cache_status;
return fail_segment("output caching");
} }
sync_runtime_residency(); sync_runtime_residency();
if (last) { if (last) {

View File

@ -26,13 +26,10 @@ namespace sd {
std::unique_ptr<CachedTensor> CachedTensor::copy(ggml_backend_t backend, std::unique_ptr<CachedTensor> CachedTensor::copy(ggml_backend_t backend,
const std::string& name, const std::string& name,
ggml_tensor* source, ggml_tensor* source) {
ggml_status& status) {
status = GGML_STATUS_FAILED;
if (ggml_graph_cut::tensor_buffer(source) == nullptr) { if (ggml_graph_cut::tensor_buffer(source) == nullptr) {
return nullptr; return nullptr;
} }
status = GGML_STATUS_ALLOC_FAILED;
auto entry = std::make_unique<CachedTensor>(); auto entry = std::make_unique<CachedTensor>();
entry->context = ggml_init({2 * ggml_tensor_overhead(), nullptr, true}); entry->context = ggml_init({2 * ggml_tensor_overhead(), nullptr, true});
if (entry->context == nullptr) { if (entry->context == nullptr) {
@ -53,7 +50,6 @@ namespace sd {
} else { } else {
ggml_backend_tensor_copy(source, entry->tensor); ggml_backend_tensor_copy(source, entry->tensor);
} }
status = GGML_STATUS_SUCCESS;
return entry; return entry;
} }
@ -110,9 +106,9 @@ namespace sd {
return pending > SIZE_MAX - committed ? SIZE_MAX : committed + pending; return pending > SIZE_MAX - committed ? SIZE_MAX : committed + pending;
} }
ggml_status RunnerCache::capture(ggml_cgraph* graph) { bool RunnerCache::capture(ggml_cgraph* graph) {
if (outputs_.empty()) { if (outputs_.empty()) {
return GGML_STATUS_SUCCESS; return true;
} }
const auto tensors = cache_graph_tensors(graph); const auto tensors = cache_graph_tensors(graph);
for (const auto& output : outputs_) { for (const auto& output : outputs_) {
@ -120,15 +116,14 @@ namespace sd {
continue; continue;
} }
GGML_ASSERT(ggml_is_contiguous(output.second)); GGML_ASSERT(ggml_is_contiguous(output.second));
ggml_status status; auto entry = CachedTensor::copy(backend_, output.first, output.second);
auto entry = CachedTensor::copy(backend_, output.first, output.second, status);
if (entry == nullptr) { if (entry == nullptr) {
return status; return false;
} }
pending_[output.first] = std::move(entry); pending_[output.first] = std::move(entry);
} }
ggml_backend_synchronize(backend_); ggml_backend_synchronize(backend_);
return GGML_STATUS_SUCCESS; return true;
} }
void RunnerCache::graph_end(bool success) { void RunnerCache::graph_end(bool success) {
@ -185,9 +180,9 @@ namespace sd {
} }
} }
ggml_status GraphCutTensorCache::capture(ggml_cgraph* graph, bool GraphCutTensorCache::capture(ggml_cgraph* graph,
const ggml_graph_cut::Segment& segment, const ggml_graph_cut::Segment& segment,
const char* log_desc) { const char* log_desc) {
size_t copied_bytes = 0; size_t copied_bytes = 0;
size_t copied_count = 0; size_t copied_count = 0;
for (int index : segment.output_node_indices) { for (int index : segment.output_node_indices) {
@ -196,11 +191,10 @@ namespace sd {
!segment.future_cut_names.count(output->name)) { !segment.future_cut_names.count(output->name)) {
continue; continue;
} }
ggml_status status; auto entry = CachedTensor::copy(backend_, output->name, ggml_graph_cut::cache_source_tensor(output));
auto entry = CachedTensor::copy(backend_, output->name, ggml_graph_cut::cache_source_tensor(output), status);
if (entry == nullptr) { if (entry == nullptr) {
LOG_ERROR("%s failed to capture graph cut tensor: %s", log_desc, output->name); LOG_ERROR("%s failed to capture graph cut tensor: %s", log_desc, output->name);
return status; return false;
} }
const size_t size = ggml_backend_buffer_get_size(entry->buffer); const size_t size = ggml_backend_buffer_get_size(entry->buffer);
copied_bytes = size > SIZE_MAX - copied_bytes ? SIZE_MAX : copied_bytes + size; copied_bytes = size > SIZE_MAX - copied_bytes ? SIZE_MAX : copied_bytes + size;
@ -212,6 +206,6 @@ namespace sd {
LOG_DEBUG("%s graph cut cache added %6.2f MB (%zu tensors)", LOG_DEBUG("%s graph cut cache added %6.2f MB (%zu tensors)",
log_desc, copied_bytes / (1024.f * 1024.f), copied_count); log_desc, copied_bytes / (1024.f * 1024.f), copied_count);
} }
return GGML_STATUS_SUCCESS; return true;
} }
} }

View File

@ -20,8 +20,7 @@ namespace sd {
~CachedTensor(); ~CachedTensor();
static std::unique_ptr<CachedTensor> copy(ggml_backend_t backend, static std::unique_ptr<CachedTensor> copy(ggml_backend_t backend,
const std::string& name, const std::string& name,
ggml_tensor* source, ggml_tensor* source);
ggml_status& status);
}; };
using CachedTensors = std::map<std::string, std::unique_ptr<CachedTensor>>; using CachedTensors = std::map<std::string, std::unique_ptr<CachedTensor>>;
@ -42,8 +41,7 @@ namespace sd {
const std::map<std::string, ggml_tensor*>& outputs() const { return outputs_; } const std::map<std::string, ggml_tensor*>& outputs() const { return outputs_; }
size_t pending_bytes(ggml_cgraph* graph) const; size_t pending_bytes(ggml_cgraph* graph) const;
size_t resident_bytes(ggml_backend_dev_t device) const; size_t resident_bytes(ggml_backend_dev_t device) const;
bool empty() const { return committed_.empty(); } bool capture(ggml_cgraph* graph);
ggml_status capture(ggml_cgraph* graph);
void graph_end(bool success); void graph_end(bool success);
void clear(); void clear();
}; };
@ -59,7 +57,7 @@ namespace sd {
size_t resident_bytes(ggml_backend_dev_t device) const; size_t resident_bytes(ggml_backend_dev_t device) const;
size_t estimate_output_bytes(ggml_cgraph* graph, size_t estimate_output_bytes(ggml_cgraph* graph,
const ggml_graph_cut::Segment& segment) const; const ggml_graph_cut::Segment& segment) const;
ggml_status capture(ggml_cgraph* graph, const ggml_graph_cut::Segment& segment, const char* log_desc); 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 prune(const std::unordered_set<std::string>& keep_names);
void clear() { tensors_.clear(); } void clear() { tensors_.clear(); }
}; };

View File

@ -5,7 +5,6 @@
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
#include <set> #include <set>
#include <utility>
#include <vector> #include <vector>
#include "core/ggml_extend.h" #include "core/ggml_extend.h"
#include "core/ggml_runner.h" #include "core/ggml_runner.h"
@ -17,45 +16,6 @@ namespace Rope {
ErnieImage, ErnieImage,
}; };
struct SpatialRegion {
size_t begin;
size_t count;
float height_period;
float width_period;
int height_axis = 1;
int width_axis = 2;
};
struct PositionLayout {
// Token ranges are relative to one batch item.
std::vector<SpatialRegion> images;
size_t token_count = 0;
void append_tokens(size_t count) {
token_count += count;
}
void append_image(int height, int width, int frames = 1, float height_step = 1.f, float width_step = 1.f) {
size_t count = static_cast<size_t>(height) * width * frames;
images.push_back({token_count, count, height * height_step, width * width_step});
append_tokens(count);
}
};
struct Frequency {
size_t axis;
float omega;
};
struct Embedding {
std::vector<float> values;
std::vector<std::vector<float>> ids;
PositionLayout positions;
std::vector<Frequency> frequencies;
EmbedNDLayout layout = EmbedNDLayout::Matrix;
int batch_size = 1;
};
enum class RefIndexMode { enum class RefIndexMode {
FIXED, FIXED,
INCREASE, INCREASE,
@ -96,25 +56,40 @@ namespace Rope {
return flat_vec; return flat_vec;
} }
__STATIC_INLINE__ std::vector<float> rope_frequencies(int dim, float theta) { __STATIC_INLINE__ std::vector<std::vector<float>> rope(const std::vector<float>& pos,
int dim,
float theta,
const std::vector<int>& axis_wrap_dims = {}) {
assert(dim % 2 == 0); assert(dim % 2 == 0);
int half_dim = dim / 2; int half_dim = dim / 2;
std::vector<float> scale = linspace(0.f, (dim * 1.f - 2) / dim, half_dim); std::vector<float> scale = linspace(0.f, (dim * 1.f - 2) / dim, half_dim);
std::vector<float> omega(half_dim); std::vector<float> omega(half_dim);
for (int i = 0; i < half_dim; ++i) { for (int i = 0; i < half_dim; ++i) {
omega[i] = 1.0f / ::powf(1.f * theta, scale[i]); omega[i] = 1.0f / ::powf(1.f * theta, scale[i]);
} }
return omega;
}
__STATIC_INLINE__ std::vector<std::vector<float>> rope(const std::vector<float>& pos,
const std::vector<float>& omega) {
int half_dim = static_cast<int>(omega.size());
size_t pos_size = pos.size(); size_t pos_size = pos.size();
std::vector<std::vector<float>> out(pos_size, std::vector<float>(half_dim)); std::vector<std::vector<float>> out(pos_size, std::vector<float>(half_dim));
for (size_t i = 0; i < pos_size; ++i) { for (size_t i = 0; i < pos_size; ++i) {
for (size_t j = 0; j < half_dim; ++j) { for (size_t j = 0; j < half_dim; ++j) {
float angle = pos[i] * omega[j]; float angle = pos[i] * omega[j];
if (!axis_wrap_dims.empty()) {
size_t wrap_size = axis_wrap_dims.size();
// mod batch size since we only store this for one item in the batch
size_t wrap_idx = wrap_size > 0 ? (i % wrap_size) : 0;
int wrap_dim = axis_wrap_dims[wrap_idx];
if (wrap_dim > 0) {
constexpr float TWO_PI = 6.28318530717958647692f;
float cycles = omega[j] * wrap_dim / TWO_PI;
// closest periodic harmonic, necessary to ensure things neatly tile
// without this round, things don't tile at the boundaries and you end up
// with the model knowing what is "center"
float rounded = std::round(cycles);
angle = pos[i] * TWO_PI * rounded / wrap_dim;
}
}
out[i][j] = angle; out[i][j] = angle;
} }
@ -133,12 +108,6 @@ namespace Rope {
return result; return result;
} }
__STATIC_INLINE__ std::vector<std::vector<float>> rope(const std::vector<float>& pos,
int dim,
float theta) {
return rope(pos, rope_frequencies(dim, theta));
}
// Generate IDs for image patches and text // Generate IDs for image patches and text
__STATIC_INLINE__ std::vector<std::vector<float>> gen_flux_txt_ids(int bs, int context_len, int axes_dim_num, std::set<int> arange_dims) { __STATIC_INLINE__ std::vector<std::vector<float>> gen_flux_txt_ids(int bs, int context_len, int axes_dim_num, std::set<int> arange_dims) {
auto txt_ids = std::vector<std::vector<float>>(bs * context_len, std::vector<float>(axes_dim_num, 0.0f)); auto txt_ids = std::vector<std::vector<float>>(bs * context_len, std::vector<float>(axes_dim_num, 0.0f));
@ -167,16 +136,12 @@ namespace Rope {
int patch_size, int patch_size,
int bs, int bs,
int axes_dim_num, int axes_dim_num,
int index = 0, int index = 0,
int h_offset = 0, int h_offset = 0,
int w_offset = 0, int w_offset = 0,
bool scale_rope = false, bool scale_rope = false) {
PositionLayout* layout = nullptr) {
int h_len = (h + (patch_size / 2)) / patch_size; int h_len = (h + (patch_size / 2)) / patch_size;
int w_len = (w + (patch_size / 2)) / patch_size; int w_len = (w + (patch_size / 2)) / patch_size;
if (layout) {
layout->append_image(h_len, w_len);
}
std::vector<std::vector<float>> img_ids(h_len * w_len, std::vector<float>(axes_dim_num, 0.0)); std::vector<std::vector<float>> img_ids(h_len * w_len, std::vector<float>(axes_dim_num, 0.0));
int h_start = h_offset; int h_start = h_offset;
@ -227,8 +192,8 @@ namespace Rope {
int bs, int bs,
const std::vector<float>& axis_thetas, const std::vector<float>& axis_thetas,
const std::vector<int>& axes_dim, const std::vector<int>& axes_dim,
EmbedNDLayout layout = EmbedNDLayout::Matrix, const std::vector<std::vector<int>>& wrap_dims = {},
std::vector<Frequency>* frequencies = nullptr) { EmbedNDLayout layout = EmbedNDLayout::Matrix) {
std::vector<std::vector<float>> trans_ids = transpose(ids); std::vector<std::vector<float>> trans_ids = transpose(ids);
size_t pos_len = ids.size() / bs; size_t pos_len = ids.size() / bs;
size_t num_axes = axes_dim.size(); size_t num_axes = axes_dim.size();
@ -240,25 +205,19 @@ namespace Rope {
for (int d : axes_dim) for (int d : axes_dim)
emb_dim += d / 2; emb_dim += d / 2;
if (frequencies) {
frequencies->clear();
frequencies->reserve(emb_dim);
}
std::vector<std::vector<float>> emb(bs * pos_len, std::vector<float>(emb_dim * 2 * 2, 0.0)); std::vector<std::vector<float>> emb(bs * pos_len, std::vector<float>(emb_dim * 2 * 2, 0.0));
size_t offset = 0; size_t offset = 0;
for (size_t i = 0; i < num_axes; ++i) { for (size_t i = 0; i < num_axes; ++i) {
std::vector<int> axis_wrap_dims;
if (!wrap_dims.empty() && i < (int)wrap_dims.size()) {
axis_wrap_dims = wrap_dims[i];
}
float axis_theta = 10000.0f; float axis_theta = 10000.0f;
if (!axis_thetas.empty()) { if (!axis_thetas.empty()) {
axis_theta = axis_thetas[std::min(i, axis_thetas.size() - 1)]; axis_theta = axis_thetas[std::min(i, axis_thetas.size() - 1)];
} }
auto omega = rope_frequencies(axes_dim[i], axis_theta);
if (frequencies) {
for (float frequency : omega) {
frequencies->push_back({i, frequency});
}
}
std::vector<std::vector<float>> rope_emb = std::vector<std::vector<float>> rope_emb =
rope(trans_ids[i], omega); // [bs*pos_len, axes_dim[i]/2 * 2 * 2] rope(trans_ids[i], axes_dim[i], axis_theta, axis_wrap_dims); // [bs*pos_len, axes_dim[i]/2 * 2 * 2]
for (int b = 0; b < bs; ++b) { for (int b = 0; b < bs; ++b) {
for (int j = 0; j < pos_len; ++j) { for (int j = 0; j < pos_len; ++j) {
for (int k = 0; k < rope_emb[0].size(); ++k) { for (int k = 0; k < rope_emb[0].size(); ++k) {
@ -294,10 +253,10 @@ namespace Rope {
int bs, int bs,
float theta, float theta,
const std::vector<int>& axes_dim, const std::vector<int>& axes_dim,
EmbedNDLayout layout = EmbedNDLayout::Matrix, const std::vector<std::vector<int>>& wrap_dims = {},
std::vector<Frequency>* frequencies = nullptr) { EmbedNDLayout layout = EmbedNDLayout::Matrix) {
std::vector<float> axis_thetas(axes_dim.size(), theta); std::vector<float> axis_thetas(axes_dim.size(), theta);
return embed_nd(ids, bs, axis_thetas, axes_dim, layout, frequencies); return embed_nd(ids, bs, axis_thetas, axes_dim, wrap_dims, layout);
} }
__STATIC_INLINE__ std::vector<float> embed_interleaved_mrope(const std::vector<std::vector<float>>& ids, __STATIC_INLINE__ std::vector<float> embed_interleaved_mrope(const std::vector<std::vector<float>>& ids,
@ -305,7 +264,7 @@ namespace Rope {
float theta, float theta,
int head_dim, int head_dim,
const std::vector<int>& mrope_section, const std::vector<int>& mrope_section,
std::vector<Frequency>* frequencies = nullptr) { const std::vector<std::vector<int>>& axis_wrap_dims = {}) {
GGML_ASSERT(bs > 0); GGML_ASSERT(bs > 0);
GGML_ASSERT(head_dim % 2 == 0); GGML_ASSERT(head_dim % 2 == 0);
GGML_ASSERT(mrope_section.size() >= 3); GGML_ASSERT(mrope_section.size() >= 3);
@ -314,26 +273,20 @@ namespace Rope {
size_t pos_len = ids.size() / bs; size_t pos_len = ids.size() / bs;
int half_dim = head_dim / 2; int half_dim = head_dim / 2;
auto omega = rope_frequencies(head_dim, theta);
if (frequencies) {
frequencies->clear();
for (float frequency : omega) {
frequencies->push_back({0, frequency});
}
}
std::vector<std::vector<std::vector<float>>> axis_embs; std::vector<std::vector<std::vector<float>>> axis_embs;
axis_embs.reserve(3); axis_embs.reserve(3);
for (int axis = 0; axis < 3; ++axis) { for (int axis = 0; axis < 3; ++axis) {
axis_embs.push_back(rope(trans_ids[axis], omega)); std::vector<int> axis_wrap;
if (axis < static_cast<int>(axis_wrap_dims.size())) {
axis_wrap = axis_wrap_dims[axis];
}
axis_embs.push_back(rope(trans_ids[axis], head_dim, theta, axis_wrap));
} }
std::vector<std::vector<float>> emb = axis_embs[0]; std::vector<std::vector<float>> emb = axis_embs[0];
for (int axis = 1; axis < 3; ++axis) { for (int axis = 1; axis < 3; ++axis) {
int length = std::min<int>(mrope_section[axis] * 3, half_dim); int length = std::min<int>(mrope_section[axis] * 3, half_dim);
for (int freq_idx = axis; freq_idx < length; freq_idx += 3) { for (int freq_idx = axis; freq_idx < length; freq_idx += 3) {
if (frequencies) {
(*frequencies)[freq_idx].axis = axis;
}
for (size_t pos_idx = 0; pos_idx < bs * pos_len; ++pos_idx) { for (size_t pos_idx = 0; pos_idx < bs * pos_len; ++pos_idx) {
for (int k = 0; k < 4; ++k) { for (int k = 0; k < 4; ++k) {
emb[pos_idx][4 * freq_idx + k] = axis_embs[axis][pos_idx][4 * freq_idx + k]; emb[pos_idx][4 * freq_idx + k] = axis_embs[axis][pos_idx][4 * freq_idx + k];
@ -345,13 +298,13 @@ namespace Rope {
return flatten(emb); return flatten(emb);
} }
__STATIC_INLINE__ Embedding embed_2d_interleaved(int height, __STATIC_INLINE__ std::vector<float> embed_2d_interleaved(int height,
int width, int width,
int dim, int dim,
float theta = 10000.f, float theta = 10000.f,
float scale = 16.f, float scale = 16.f,
int ref_grid_h = 0, int ref_grid_h = 0,
int ref_grid_w = 0) { int ref_grid_w = 0) {
assert(dim % 4 == 0); assert(dim % 4 == 0);
int half_dim = dim / 2; int half_dim = dim / 2;
int dim_axis = dim / 2; int dim_axis = dim / 2;
@ -365,10 +318,6 @@ namespace Rope {
w_ntk = std::pow(static_cast<float>(width) / static_cast<float>(ref_grid_w), power); w_ntk = std::pow(static_cast<float>(width) / static_cast<float>(ref_grid_w), power);
} }
Embedding result;
result.positions.append_image(height, width, 1,
height > 1 ? scale / (height - 1) : 1.f,
width > 1 ? scale / (width - 1) : 1.f);
std::vector<float> x_pos; std::vector<float> x_pos;
std::vector<float> y_pos; std::vector<float> y_pos;
x_pos.reserve(static_cast<size_t>(height) * width); x_pos.reserve(static_cast<size_t>(height) * width);
@ -377,20 +326,13 @@ namespace Rope {
float y = height == 1 ? 0.f : scale * static_cast<float>(iy) / static_cast<float>(height - 1); float y = height == 1 ? 0.f : scale * static_cast<float>(iy) / static_cast<float>(height - 1);
for (int ix = 0; ix < width; ++ix) { for (int ix = 0; ix < width; ++ix) {
float x = width == 1 ? 0.f : scale * static_cast<float>(ix) / static_cast<float>(width - 1); float x = width == 1 ? 0.f : scale * static_cast<float>(ix) / static_cast<float>(width - 1);
result.ids.push_back({0.f, y, x});
x_pos.push_back(x); x_pos.push_back(x);
y_pos.push_back(y); y_pos.push_back(y);
} }
} }
auto x_freq = rope_frequencies(dim_axis, theta * w_ntk); auto x_emb = rope(x_pos, dim_axis, theta * w_ntk);
auto y_freq = rope_frequencies(dim_axis, theta * h_ntk); auto y_emb = rope(y_pos, dim_axis, theta * h_ntk);
auto x_emb = rope(x_pos, x_freq);
auto y_emb = rope(y_pos, y_freq);
for (int i = 0; i < axis_half_dim; ++i) {
result.frequencies.push_back({2, x_freq[i]});
result.frequencies.push_back({1, y_freq[i]});
}
std::vector<float> out(static_cast<size_t>(height) * width * half_dim * 4); std::vector<float> out(static_cast<size_t>(height) * width * half_dim * 4);
for (int pos = 0; pos < height * width; ++pos) { for (int pos = 0; pos < height * width; ++pos) {
@ -406,8 +348,7 @@ namespace Rope {
} }
} }
} }
result.values = std::move(out); return out;
return result;
} }
__STATIC_INLINE__ std::vector<std::vector<float>> gen_refs_ids(int patch_size, __STATIC_INLINE__ std::vector<std::vector<float>> gen_refs_ids(int patch_size,
@ -418,8 +359,7 @@ namespace Rope {
RefIndexMode ref_index_mode, RefIndexMode ref_index_mode,
float ref_index_scale, float ref_index_scale,
bool scale_rope, bool scale_rope,
int base_offset = 0, int base_offset = 0) {
PositionLayout* layout = nullptr) {
std::vector<std::vector<float>> ids; std::vector<std::vector<float>> ids;
int curr_h_offset = 0; int curr_h_offset = 0;
int curr_w_offset = 0; int curr_w_offset = 0;
@ -446,8 +386,7 @@ namespace Rope {
static_cast<int>(index * ref_index_scale), static_cast<int>(index * ref_index_scale),
h_offset + base_offset, h_offset + base_offset,
w_offset + base_offset, w_offset + base_offset,
scale_rope, scale_rope);
layout);
ids = concat_ids(ids, ref_ids, bs); ids = concat_ids(ids, ref_ids, bs);
if (ref_index_mode == RefIndexMode::INCREASE) { if (ref_index_mode == RefIndexMode::INCREASE) {
@ -470,53 +409,88 @@ namespace Rope {
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
RefIndexMode ref_index_mode, RefIndexMode ref_index_mode,
float ref_index_scale, float ref_index_scale,
bool is_longcat, bool is_longcat) {
PositionLayout* layout = nullptr) {
if (layout) {
layout->append_tokens(context_len);
}
int x_index = is_longcat ? 1 : 0; int x_index = is_longcat ? 1 : 0;
auto txt_ids = is_longcat ? gen_longcat_txt_ids(bs, context_len, axes_dim_num) : gen_flux_txt_ids(bs, context_len, axes_dim_num, txt_arange_dims); auto txt_ids = is_longcat ? gen_longcat_txt_ids(bs, context_len, axes_dim_num) : gen_flux_txt_ids(bs, context_len, axes_dim_num, txt_arange_dims);
int offset = is_longcat ? context_len : 0; int offset = is_longcat ? context_len : 0;
auto img_ids = gen_flux_img_ids(h, w, patch_size, bs, axes_dim_num, x_index, offset, offset, false, layout); auto img_ids = gen_flux_img_ids(h, w, patch_size, bs, axes_dim_num, x_index, offset, offset);
auto ids = concat_ids(txt_ids, img_ids, bs); auto ids = concat_ids(txt_ids, img_ids, bs);
if (ref_latents.size() > 0) { if (ref_latents.size() > 0) {
auto refs_ids = gen_refs_ids(patch_size, bs, axes_dim_num, x_index + 1, ref_latents, ref_index_mode, ref_index_scale, false, offset, layout); auto refs_ids = gen_refs_ids(patch_size, bs, axes_dim_num, x_index + 1, ref_latents, ref_index_mode, ref_index_scale, false, offset);
ids = concat_ids(ids, refs_ids, bs); ids = concat_ids(ids, refs_ids, bs);
} }
return ids; return ids;
} }
// Generate flux positional embeddings // Generate flux positional embeddings
__STATIC_INLINE__ Embedding gen_flux_pe(int h, __STATIC_INLINE__ std::vector<float> gen_flux_pe(int h,
int w, int w,
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len,
std::set<int> txt_arange_dims, std::set<int> txt_arange_dims,
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
RefIndexMode ref_index_mode, RefIndexMode ref_index_mode,
float ref_index_scale, float ref_index_scale,
int theta, int theta,
const std::vector<int>& axes_dim, bool circular_h,
bool is_longcat) { bool circular_w,
Embedding result; const std::vector<int>& axes_dim,
result.batch_size = bs; bool is_longcat) {
result.ids = gen_flux_ids(h, std::vector<std::vector<float>> ids = gen_flux_ids(h,
w, w,
patch_size, patch_size,
bs, bs,
static_cast<int>(axes_dim.size()), static_cast<int>(axes_dim.size()),
context_len, context_len,
txt_arange_dims, txt_arange_dims,
ref_latents, ref_latents,
ref_index_mode, ref_index_mode,
ref_index_scale, ref_index_scale,
is_longcat, &result.positions); is_longcat);
result.values = embed_nd(result.ids, bs, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies); std::vector<std::vector<int>> wrap_dims;
return result; if ((circular_h || circular_w) && bs > 0 && axes_dim.size() >= 3) {
int h_len = (h + (patch_size / 2)) / patch_size;
int w_len = (w + (patch_size / 2)) / patch_size;
if (h_len > 0 && w_len > 0) {
size_t pos_len = ids.size() / bs;
wrap_dims.assign(axes_dim.size(), std::vector<int>(pos_len, 0));
size_t cursor = context_len; // text first
const size_t img_tokens = static_cast<size_t>(h_len) * static_cast<size_t>(w_len);
for (size_t token_i = 0; token_i < img_tokens; ++token_i) {
if (circular_h) {
wrap_dims[1][cursor + token_i] = h_len;
}
if (circular_w) {
wrap_dims[2][cursor + token_i] = w_len;
}
}
cursor += img_tokens;
// reference latents
for (ggml_tensor* ref : ref_latents) {
if (ref == nullptr) {
continue;
}
int ref_h = static_cast<int>(ref->ne[1]);
int ref_w = static_cast<int>(ref->ne[0]);
int ref_h_l = (ref_h + (patch_size / 2)) / patch_size;
int ref_w_l = (ref_w + (patch_size / 2)) / patch_size;
size_t ref_tokens = static_cast<size_t>(ref_h_l) * static_cast<size_t>(ref_w_l);
for (size_t token_i = 0; token_i < ref_tokens; ++token_i) {
if (circular_h) {
wrap_dims[1][cursor + token_i] = ref_h_l;
}
if (circular_w) {
wrap_dims[2][cursor + token_i] = ref_w_l;
}
}
cursor += ref_tokens;
}
}
}
return embed_nd(ids, bs, static_cast<float>(theta), axes_dim, wrap_dims);
} }
__STATIC_INLINE__ std::vector<std::vector<float>> gen_vid_ids(int t, __STATIC_INLINE__ std::vector<std::vector<float>> gen_vid_ids(int t,
@ -526,18 +500,14 @@ namespace Rope {
int ph, int ph,
int pw, int pw,
int bs, int bs,
int t_offset = 0, int t_offset = 0,
int h_offset = 0, int h_offset = 0,
int w_offset = 0, int w_offset = 0,
bool scale_rope = false, bool scale_rope = false) {
PositionLayout* layout = nullptr) {
int t_len = (t + (pt / 2)) / pt; int t_len = (t + (pt / 2)) / pt;
int h_len = (h + (ph / 2)) / ph; int h_len = (h + (ph / 2)) / ph;
int w_len = (w + (pw / 2)) / pw; int w_len = (w + (pw / 2)) / pw;
if (layout) {
layout->append_image(h_len, w_len, t_len);
}
std::vector<std::vector<float>> vid_ids(t_len * h_len * w_len, std::vector<float>(3, 0.0)); std::vector<std::vector<float>> vid_ids(t_len * h_len * w_len, std::vector<float>(3, 0.0));
if (scale_rope) { if (scale_rope) {
@ -603,11 +573,7 @@ namespace Rope {
int bs, int bs,
int context_len, int context_len,
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
RefIndexMode ref_index_mode, RefIndexMode ref_index_mode) {
PositionLayout* layout = nullptr) {
if (layout) {
layout->append_tokens(context_len);
}
int h_len = (h + (patch_size / 2)) / patch_size; int h_len = (h + (patch_size / 2)) / patch_size;
int w_len = (w + (patch_size / 2)) / patch_size; int w_len = (w + (patch_size / 2)) / patch_size;
int txt_id_start = std::max(h_len, w_len) / 2; int txt_id_start = std::max(h_len, w_len) / 2;
@ -619,49 +585,90 @@ namespace Rope {
} }
} }
int axes_dim_num = 3; int axes_dim_num = 3;
auto img_ids = gen_vid_ids(t, h, w, 1, patch_size, patch_size, bs, 0, 0, 0, true, layout); auto img_ids = gen_vid_ids(t, h, w, 1, patch_size, patch_size, bs, 0, 0, 0, true);
auto ids = concat_ids(txt_ids_repeated, img_ids, bs); auto ids = concat_ids(txt_ids_repeated, img_ids, bs);
if (ref_latents.size() > 0) { if (ref_latents.size() > 0) {
int ref_start_index = ref_index_mode == RefIndexMode::DECREASE ? 0 : 1; int ref_start_index = ref_index_mode == RefIndexMode::DECREASE ? 0 : 1;
auto refs_ids = gen_refs_ids(patch_size, bs, axes_dim_num, ref_start_index, ref_latents, ref_index_mode, 1.f, true, 0, layout); auto refs_ids = gen_refs_ids(patch_size, bs, axes_dim_num, ref_start_index, ref_latents, ref_index_mode, 1.f, true);
ids = concat_ids(ids, refs_ids, bs); ids = concat_ids(ids, refs_ids, bs);
} }
return ids; return ids;
} }
// Generate qwen_image positional embeddings // Generate qwen_image positional embeddings
__STATIC_INLINE__ Embedding gen_qwen_image_pe(int t, __STATIC_INLINE__ std::vector<float> gen_qwen_image_pe(int t,
int h, int h,
int w, int w,
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len,
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
RefIndexMode ref_index_mode, RefIndexMode ref_index_mode,
int theta, int theta,
const std::vector<int>& axes_dim) { bool circular_h,
Embedding result; bool circular_w,
result.batch_size = bs; const std::vector<int>& axes_dim) {
result.ids = gen_qwen_image_ids(t, h, w, patch_size, bs, context_len, ref_latents, ref_index_mode, &result.positions); std::vector<std::vector<float>> ids = gen_qwen_image_ids(t, h, w, patch_size, bs, context_len, ref_latents, ref_index_mode);
result.values = embed_nd(result.ids, bs, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies); std::vector<std::vector<int>> wrap_dims;
return result; // This logic simply stores the (pad and patch_adjusted) sizes of images so we can make sure rope correctly tiles
if ((circular_h || circular_w) && bs > 0 && axes_dim.size() >= 3) {
int pad_h = (patch_size - (h % patch_size)) % patch_size;
int pad_w = (patch_size - (w % patch_size)) % patch_size;
int h_len = (h + pad_h) / patch_size;
int w_len = (w + pad_w) / patch_size;
if (h_len > 0 && w_len > 0) {
const size_t total_tokens = ids.size();
// Track per-token wrap lengths for the row/column axes so only spatial tokens become periodic.
wrap_dims.assign(axes_dim.size(), std::vector<int>(total_tokens / bs, 0));
size_t cursor = context_len; // ignore text tokens
const size_t img_tokens = static_cast<size_t>(t) * static_cast<size_t>(h_len) * static_cast<size_t>(w_len);
for (size_t token_i = 0; token_i < img_tokens; ++token_i) {
if (circular_h) {
wrap_dims[1][cursor + token_i] = h_len;
}
if (circular_w) {
wrap_dims[2][cursor + token_i] = w_len;
}
}
cursor += img_tokens;
// For each reference image, store wrap sizes as well
for (ggml_tensor* ref : ref_latents) {
if (ref == nullptr) {
continue;
}
int ref_h = static_cast<int>(ref->ne[1]);
int ref_w = static_cast<int>(ref->ne[0]);
int ref_pad_h = (patch_size - (ref_h % patch_size)) % patch_size;
int ref_pad_w = (patch_size - (ref_w % patch_size)) % patch_size;
int ref_h_len = (ref_h + ref_pad_h) / patch_size;
int ref_w_len = (ref_w + ref_pad_w) / patch_size;
size_t ref_n_tokens = static_cast<size_t>(ref_h_len) * static_cast<size_t>(ref_w_len);
for (size_t token_i = 0; token_i < ref_n_tokens; ++token_i) {
if (circular_h) {
wrap_dims[1][cursor + token_i] = ref_h_len;
}
if (circular_w) {
wrap_dims[2][cursor + token_i] = ref_w_len;
}
}
cursor += ref_n_tokens;
}
}
}
return embed_nd(ids, bs, static_cast<float>(theta), axes_dim, wrap_dims);
} }
__STATIC_INLINE__ Embedding gen_mage_flow_pe(int h, __STATIC_INLINE__ std::vector<float> gen_mage_flow_pe(int h,
int w, int w,
int bs, int bs,
int context_len, int context_len,
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
int theta, int theta,
const std::vector<int>& axes_dim) { const std::vector<int>& axes_dim) {
Embedding result;
result.batch_size = bs;
result.positions.append_tokens(context_len);
const int axes_dim_num = static_cast<int>(axes_dim.size()); const int axes_dim_num = static_cast<int>(axes_dim.size());
auto make_image_ids = [=, &result](int image_h, int image_w, int image_index) { auto make_image_ids = [=](int image_h, int image_w, int image_index) {
std::vector<std::vector<float>> image_ids(static_cast<size_t>(bs) * image_h * image_w, std::vector<std::vector<float>> image_ids(static_cast<size_t>(bs) * image_h * image_w,
std::vector<float>(axes_dim_num, 0.f)); std::vector<float>(axes_dim_num, 0.f));
result.positions.append_image(image_h, image_w);
int h_start = -(image_h - image_h / 2); int h_start = -(image_h - image_h / 2);
int w_start = -(image_w - image_w / 2); int w_start = -(image_w - image_w / 2);
for (int b = 0; b < bs; ++b) { for (int b = 0; b < bs; ++b) {
@ -685,18 +692,15 @@ namespace Rope {
static_cast<int>(i + 1)); static_cast<int>(i + 1));
ids = concat_ids(ids, ref_ids, bs); ids = concat_ids(ids, ref_ids, bs);
} }
result.ids = std::move(ids); return embed_nd(ids, bs, static_cast<float>(theta), axes_dim);
result.values = embed_nd(result.ids, bs, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies);
return result;
} }
__STATIC_INLINE__ std::vector<std::vector<float>> gen_lens_ids(int h, __STATIC_INLINE__ std::vector<std::vector<float>> gen_lens_ids(int h,
int w, int w,
int bs, int bs,
int context_len, int context_len,
bool scale_rope = true, bool scale_rope = true) {
PositionLayout* layout = nullptr) { auto img_ids_repeated = gen_flux_img_ids(h, w, 1, bs, 3, 0, 0, 0, scale_rope);
auto img_ids_repeated = gen_flux_img_ids(h, w, 1, bs, 3, 0, 0, 0, scale_rope, layout);
int txt_id_start = scale_rope ? std::max(h / 2, w / 2) : 0; int txt_id_start = scale_rope ? std::max(h / 2, w / 2) : 0;
auto txt_ids = linspace<float>(1.f * txt_id_start, 1.f * context_len + txt_id_start, context_len); auto txt_ids = linspace<float>(1.f * txt_id_start, 1.f * context_len + txt_id_start, context_len);
@ -707,37 +711,44 @@ namespace Rope {
} }
} }
if (layout) {
layout->append_tokens(context_len);
}
return concat_ids(img_ids_repeated, txt_ids_repeated, bs); return concat_ids(img_ids_repeated, txt_ids_repeated, bs);
} }
__STATIC_INLINE__ Embedding gen_lens_pe(int h, __STATIC_INLINE__ std::vector<float> gen_lens_pe(int h,
int w, int w,
int bs, int bs,
int context_len, int context_len,
int theta, int theta,
const std::vector<int>& axes_dim) { bool circular_h,
Embedding result; bool circular_w,
result.batch_size = bs; const std::vector<int>& axes_dim) {
result.ids = gen_lens_ids(h, w, bs, context_len, true, &result.positions); std::vector<std::vector<float>> ids = gen_lens_ids(h, w, bs, context_len, true);
result.values = embed_nd(result.ids, bs, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies); std::vector<std::vector<int>> wrap_dims;
return result; if ((circular_h || circular_w) && bs > 0 && axes_dim.size() >= 3) {
size_t pos_len = ids.size() / bs;
wrap_dims.assign(axes_dim.size(), std::vector<int>(pos_len, 0));
const size_t img_tokens = static_cast<size_t>(h) * static_cast<size_t>(w);
for (size_t token_i = 0; token_i < img_tokens; ++token_i) {
if (circular_h) {
wrap_dims[1][token_i] = h;
}
if (circular_w) {
wrap_dims[2][token_i] = w;
}
}
}
return embed_nd(ids, bs, static_cast<float>(theta), axes_dim, wrap_dims);
} }
__STATIC_INLINE__ std::vector<std::vector<float>> gen_ernie_image_ids(int h, __STATIC_INLINE__ std::vector<std::vector<float>> gen_ernie_image_ids(int h,
int w, int w,
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len) {
PositionLayout* layout = nullptr) {
int h_len = h / patch_size; int h_len = h / patch_size;
int w_len = w / patch_size; int w_len = w / patch_size;
if (layout) {
layout->append_image(h_len, w_len);
}
std::vector<std::vector<float>> img_ids(h_len * w_len, std::vector<float>(3, 0.0f)); std::vector<std::vector<float>> img_ids(h_len * w_len, std::vector<float>(3, 0.0f));
std::vector<float> h_ids = linspace<float>(0.f, static_cast<float>(h_len - 1), h_len); std::vector<float> h_ids = linspace<float>(0.f, static_cast<float>(h_len - 1), h_len);
std::vector<float> w_ids = linspace<float>(0.f, static_cast<float>(w_len - 1), w_len); std::vector<float> w_ids = linspace<float>(0.f, static_cast<float>(w_len - 1), w_len);
@ -763,25 +774,39 @@ namespace Rope {
} }
} }
if (layout) {
layout->append_tokens(context_len);
}
return concat_ids(img_ids_repeated, txt_ids, bs); return concat_ids(img_ids_repeated, txt_ids, bs);
} }
__STATIC_INLINE__ Embedding gen_ernie_image_pe(int h, __STATIC_INLINE__ std::vector<float> gen_ernie_image_pe(int h,
int w, int w,
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len,
int theta, int theta,
const std::vector<int>& axes_dim) { bool circular_h,
Embedding result; bool circular_w,
result.batch_size = bs; const std::vector<int>& axes_dim) {
result.layout = EmbedNDLayout::ErnieImage; std::vector<std::vector<float>> ids = gen_ernie_image_ids(h, w, patch_size, bs, context_len);
result.ids = gen_ernie_image_ids(h, w, patch_size, bs, context_len, &result.positions); std::vector<std::vector<int>> wrap_dims;
result.values = embed_nd(result.ids, bs, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies); if ((circular_h || circular_w) && bs > 0 && axes_dim.size() >= 3) {
return result; int h_len = h / patch_size;
int w_len = w / patch_size;
if (h_len > 0 && w_len > 0) {
size_t pos_len = ids.size() / bs;
wrap_dims.assign(axes_dim.size(), std::vector<int>(pos_len, 0));
const size_t img_tokens = static_cast<size_t>(h_len) * static_cast<size_t>(w_len);
for (size_t token_i = 0; token_i < img_tokens; ++token_i) {
if (circular_h) {
wrap_dims[1][token_i] = h_len;
}
if (circular_w) {
wrap_dims[2][token_i] = w_len;
}
}
}
}
return embed_nd(ids, bs, static_cast<float>(theta), axes_dim, wrap_dims, EmbedNDLayout::ErnieImage);
} }
// Generate wan positional embeddings // Generate wan positional embeddings
@ -880,8 +905,7 @@ namespace Rope {
int context_len, int context_len,
int seq_multi_of, int seq_multi_of,
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
RefIndexMode ref_index_mode, RefIndexMode ref_index_mode) {
PositionLayout* layout = nullptr) {
SD_UNUSED(ref_index_mode); SD_UNUSED(ref_index_mode);
int padded_context_len = context_len + bound_mod(context_len, seq_multi_of); int padded_context_len = context_len + bound_mod(context_len, seq_multi_of);
auto txt_ids = std::vector<std::vector<float>>(bs * padded_context_len, std::vector<float>(3, 0.0f)); auto txt_ids = std::vector<std::vector<float>>(bs * padded_context_len, std::vector<float>(3, 0.0f));
@ -889,17 +913,11 @@ namespace Rope {
txt_ids[i][0] = (i % padded_context_len) + 1.f; txt_ids[i][0] = (i % padded_context_len) + 1.f;
} }
if (layout) {
layout->append_tokens(padded_context_len);
}
int axes_dim_num = 3; int axes_dim_num = 3;
int index = padded_context_len + 1; int index = padded_context_len + 1;
auto img_ids = gen_flux_img_ids(h, w, patch_size, bs, axes_dim_num, index, 0, 0, false, layout); auto img_ids = gen_flux_img_ids(h, w, patch_size, bs, axes_dim_num, index);
int img_pad_len = bound_mod(static_cast<int>(img_ids.size() / bs), seq_multi_of); int img_pad_len = bound_mod(static_cast<int>(img_ids.size() / bs), seq_multi_of);
if (layout) {
layout->append_tokens(img_pad_len);
}
if (img_pad_len > 0) { if (img_pad_len > 0) {
std::vector<std::vector<float>> img_pad_ids(bs * img_pad_len, std::vector<float>(3, 0.f)); std::vector<std::vector<float>> img_pad_ids(bs * img_pad_len, std::vector<float>(3, 0.f));
img_ids = concat_ids(img_ids, img_pad_ids, bs); img_ids = concat_ids(img_ids, img_pad_ids, bs);
@ -918,8 +936,7 @@ namespace Rope {
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len,
int seq_multi_of, int seq_multi_of) {
PositionLayout* layout = nullptr) {
int context_pad_len = bound_mod(context_len, seq_multi_of); int context_pad_len = bound_mod(context_len, seq_multi_of);
int padded_context_len = context_len + context_pad_len; int padded_context_len = context_len + context_pad_len;
auto txt_ids = std::vector<std::vector<float>>(bs * padded_context_len, std::vector<float>(3, 0.0f)); auto txt_ids = std::vector<std::vector<float>>(bs * padded_context_len, std::vector<float>(3, 0.0f));
@ -930,17 +947,11 @@ namespace Rope {
} }
} }
if (layout) {
layout->append_tokens(padded_context_len);
}
int axes_dim_num = 3; int axes_dim_num = 3;
int index = padded_context_len + 1; int index = padded_context_len + 1;
auto img_ids = gen_flux_img_ids(h, w, patch_size, bs, axes_dim_num, index, 0, 0, false, layout); auto img_ids = gen_flux_img_ids(h, w, patch_size, bs, axes_dim_num, index);
int img_pad_len = bound_mod(static_cast<int>(img_ids.size() / bs), seq_multi_of); int img_pad_len = bound_mod(static_cast<int>(img_ids.size() / bs), seq_multi_of);
if (layout) {
layout->append_tokens(img_pad_len);
}
if (img_pad_len > 0) { if (img_pad_len > 0) {
std::vector<std::vector<float>> img_pad_ids(bs * img_pad_len, std::vector<float>(3, 0.f)); std::vector<std::vector<float>> img_pad_ids(bs * img_pad_len, std::vector<float>(3, 0.f));
img_ids = concat_ids(img_ids, img_pad_ids, bs); img_ids = concat_ids(img_ids, img_pad_ids, bs);
@ -957,8 +968,7 @@ namespace Rope {
int patch_size, int patch_size,
int context_len, int context_len,
int sigvq_len, int sigvq_len,
int seq_multi_of, int seq_multi_of) {
PositionLayout* layout = nullptr) {
const int context_pad = bound_mod(context_len, seq_multi_of); const int context_pad = bound_mod(context_len, seq_multi_of);
const int padded_context = context_len + context_pad; const int padded_context = context_len + context_pad;
const int h_len = (h + (patch_size / 2)) / patch_size; const int h_len = (h + (patch_size / 2)) / patch_size;
@ -984,17 +994,11 @@ namespace Rope {
cursor += 2; cursor += 2;
} }
if (layout) {
layout->append_tokens(cap_ids.size());
}
std::vector<std::vector<float>> img_ids; std::vector<std::vector<float>> img_ids;
for (int copy = 0; copy < 2; ++copy) { for (int copy = 0; copy < 2; ++copy) {
auto ids = gen_flux_img_ids(h, w, patch_size, 1, 3, cap_end_positions[copy], 0, 0, false, layout); auto ids = gen_flux_img_ids(h, w, patch_size, 1, 3, cap_end_positions[copy]);
img_ids.insert(img_ids.end(), ids.begin(), ids.end()); img_ids.insert(img_ids.end(), ids.begin(), ids.end());
img_ids.insert(img_ids.end(), image_pad, std::vector<float>(3, 0.f)); img_ids.insert(img_ids.end(), image_pad, std::vector<float>(3, 0.f));
if (layout) {
layout->append_tokens(image_pad);
}
} }
const int sigvq_start = static_cast<int>(cap_ids.size() + img_ids.size()) + 1; const int sigvq_start = static_cast<int>(cap_ids.size() + img_ids.size()) + 1;
@ -1012,59 +1016,95 @@ namespace Rope {
ids.insert(ids.end(), cap_ids.begin(), cap_ids.end()); ids.insert(ids.end(), cap_ids.begin(), cap_ids.end());
ids.insert(ids.end(), img_ids.begin(), img_ids.end()); ids.insert(ids.end(), img_ids.begin(), img_ids.end());
ids.insert(ids.end(), sigvq_ids.begin(), sigvq_ids.end()); ids.insert(ids.end(), sigvq_ids.begin(), sigvq_ids.end());
if (layout) {
layout->append_tokens(sigvq_ids.size());
}
SD_UNUSED(padded_image); SD_UNUSED(padded_image);
return ids; return ids;
} }
__STATIC_INLINE__ Embedding gen_llada_image_edit_pe(int h, __STATIC_INLINE__ std::vector<float> gen_llada_image_edit_pe(int h,
int w, int w,
int patch_size, int patch_size,
int context_len, int context_len,
int sigvq_len, int sigvq_len,
int seq_multi_of, int seq_multi_of,
int theta, int theta,
const std::vector<int>& axes_dim) { const std::vector<int>& axes_dim) {
Embedding result; auto ids = gen_llada_image_edit_ids(h, w, patch_size, context_len, sigvq_len, seq_multi_of);
result.batch_size = 1; return embed_nd(ids, 1, static_cast<float>(theta), axes_dim, {});
result.ids = gen_llada_image_edit_ids(h, w, patch_size, context_len, sigvq_len, seq_multi_of, &result.positions);
result.values = embed_nd(result.ids, 1, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies);
return result;
} }
__STATIC_INLINE__ Embedding gen_llada_image_pe(int h, __STATIC_INLINE__ std::vector<float> gen_llada_image_pe(int h,
int w, int w,
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len,
int seq_multi_of, int seq_multi_of,
int theta, int theta,
const std::vector<int>& axes_dim) { bool circular_h,
Embedding result; bool circular_w,
result.batch_size = bs; const std::vector<int>& axes_dim) {
result.ids = gen_llada_image_ids(h, w, patch_size, bs, context_len, seq_multi_of, &result.positions); std::vector<std::vector<float>> ids = gen_llada_image_ids(h, w, patch_size, bs, context_len, seq_multi_of);
result.values = embed_nd(result.ids, bs, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies); std::vector<std::vector<int>> wrap_dims;
return result; if ((circular_h || circular_w) && bs > 0 && axes_dim.size() >= 3) {
int pad_h = (patch_size - (h % patch_size)) % patch_size;
int pad_w = (patch_size - (w % patch_size)) % patch_size;
int h_len = (h + pad_h) / patch_size;
int w_len = (w + pad_w) / patch_size;
if (h_len > 0 && w_len > 0) {
size_t pos_len = ids.size() / bs;
wrap_dims.assign(axes_dim.size(), std::vector<int>(pos_len, 0));
size_t cursor = context_len + bound_mod(context_len, seq_multi_of);
size_t img_tokens = static_cast<size_t>(h_len) * static_cast<size_t>(w_len);
for (size_t token_i = 0; token_i < img_tokens; ++token_i) {
if (circular_h) {
wrap_dims[1][cursor + token_i] = h_len;
}
if (circular_w) {
wrap_dims[2][cursor + token_i] = w_len;
}
}
}
}
return embed_nd(ids, bs, static_cast<float>(theta), axes_dim, wrap_dims);
} }
// Generate z_image positional embeddings // Generate z_image positional embeddings
__STATIC_INLINE__ Embedding gen_z_image_pe(int h, __STATIC_INLINE__ std::vector<float> gen_z_image_pe(int h,
int w, int w,
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len,
int seq_multi_of, int seq_multi_of,
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
RefIndexMode ref_index_mode, RefIndexMode ref_index_mode,
int theta, int theta,
const std::vector<int>& axes_dim) { bool circular_h,
Embedding result; bool circular_w,
result.batch_size = bs; const std::vector<int>& axes_dim) {
result.ids = gen_z_image_ids(h, w, patch_size, bs, context_len, seq_multi_of, ref_latents, ref_index_mode, &result.positions); std::vector<std::vector<float>> ids = gen_z_image_ids(h, w, patch_size, bs, context_len, seq_multi_of, ref_latents, ref_index_mode);
result.values = embed_nd(result.ids, bs, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies); std::vector<std::vector<int>> wrap_dims;
return result; if ((circular_h || circular_w) && bs > 0 && axes_dim.size() >= 3) {
int pad_h = (patch_size - (h % patch_size)) % patch_size;
int pad_w = (patch_size - (w % patch_size)) % patch_size;
int h_len = (h + pad_h) / patch_size;
int w_len = (w + pad_w) / patch_size;
if (h_len > 0 && w_len > 0) {
size_t pos_len = ids.size() / bs;
wrap_dims.assign(axes_dim.size(), std::vector<int>(pos_len, 0));
size_t cursor = context_len + bound_mod(context_len, seq_multi_of); // skip text (and its padding)
size_t img_tokens = static_cast<size_t>(h_len) * static_cast<size_t>(w_len);
for (size_t token_i = 0; token_i < img_tokens; ++token_i) {
if (circular_h) {
wrap_dims[1][cursor + token_i] = h_len;
}
if (circular_w) {
wrap_dims[2][cursor + token_i] = w_len;
}
}
}
}
return embed_nd(ids, bs, static_cast<float>(theta), axes_dim, wrap_dims);
} }
__STATIC_INLINE__ ggml_tensor* apply_rope(ggml_context* ctx, __STATIC_INLINE__ ggml_tensor* apply_rope(ggml_context* ctx,

View File

@ -1,65 +0,0 @@
#ifndef __SD_MODEL_COMMON_ROPE_CIRCULAR_HPP__
#define __SD_MODEL_COMMON_ROPE_CIRCULAR_HPP__
#include "model/common/rope.hpp"
namespace Rope {
__STATIC_INLINE__ void apply_circular(Embedding& embedding, bool circular_x, bool circular_y) {
if (!circular_x && !circular_y) {
return;
}
GGML_ASSERT(embedding.batch_size > 0);
GGML_ASSERT(embedding.ids.size() % embedding.batch_size == 0);
size_t pos_len = embedding.ids.size() / embedding.batch_size;
size_t half_dim = embedding.frequencies.size();
GGML_ASSERT(embedding.positions.token_count == pos_len);
GGML_ASSERT(embedding.values.size() == embedding.ids.size() * half_dim * 4);
constexpr float TWO_PI = 6.28318530717958647692f;
for (const auto& region : embedding.positions.images) {
GGML_ASSERT(region.begin <= pos_len && region.count <= pos_len - region.begin);
for (size_t j = 0; j < half_dim; ++j) {
const auto& frequency = embedding.frequencies[j];
float period = 0.f;
if (circular_y && frequency.axis == static_cast<size_t>(region.height_axis)) {
period = region.height_period;
} else if (circular_x && frequency.axis == static_cast<size_t>(region.width_axis)) {
period = region.width_period;
}
if (period <= 0) {
continue;
}
// Quantize to periodic harmonics while preserving the original coordinate offsets.
float rounded = std::round(frequency.omega * period / TWO_PI);
for (int b = 0; b < embedding.batch_size; ++b) {
size_t begin = b * pos_len + region.begin;
for (size_t i = begin; i < begin + region.count; ++i) {
GGML_ASSERT(frequency.axis < embedding.ids[i].size());
float angle = embedding.ids[i][frequency.axis] * TWO_PI * rounded / period;
float cos_val = std::cos(angle);
float sin_val = std::sin(angle);
if (embedding.layout == EmbedNDLayout::ErnieImage) {
size_t cos_offset = (i * half_dim + j) * 2;
size_t sin_offset = embedding.ids.size() * half_dim * 2 + cos_offset;
embedding.values[cos_offset] = cos_val;
embedding.values[cos_offset + 1] = cos_val;
embedding.values[sin_offset] = sin_val;
embedding.values[sin_offset + 1] = sin_val;
} else {
size_t offset = (i * half_dim + j) * 4;
embedding.values[offset] = cos_val;
embedding.values[offset + 1] = -sin_val;
embedding.values[offset + 2] = sin_val;
embedding.values[offset + 3] = cos_val;
}
}
}
}
}
}
} // namespace Rope
#endif // __SD_MODEL_COMMON_ROPE_CIRCULAR_HPP__

View File

@ -603,37 +603,34 @@ namespace Anima {
return std::pow(extrapolation_ratio, static_cast<float>(axis_dim) / static_cast<float>(axis_dim - 2)); return std::pow(extrapolation_ratio, static_cast<float>(axis_dim) / static_cast<float>(axis_dim - 2));
} }
static Rope::Embedding gen_anima_image_pe_vec(int bs, static std::vector<float> gen_anima_image_pe_vec(int bs,
int h, int h,
int w, int w,
int patch_size, int patch_size,
int theta, int theta,
const std::vector<int>& axes_dim, const std::vector<int>& axes_dim,
float h_extrapolation_ratio, float h_extrapolation_ratio,
float w_extrapolation_ratio, float w_extrapolation_ratio,
float t_extrapolation_ratio, float t_extrapolation_ratio,
const std::vector<ggml_tensor*>& ref_latents) { const std::vector<ggml_tensor*>& ref_latents) {
Rope::Embedding result; auto ids = Rope::gen_flux_ids(h,
result.batch_size = bs; w,
result.ids = Rope::gen_flux_ids(h, patch_size,
w, bs,
patch_size, static_cast<int>(axes_dim.size()),
bs, 0,
static_cast<int>(axes_dim.size()), {},
0, ref_latents,
{}, Rope::RefIndexMode::FIXED,
ref_latents, 1.0f,
Rope::RefIndexMode::FIXED, false);
1.0f,
false, &result.positions);
std::vector<float> axis_thetas = { std::vector<float> axis_thetas = {
static_cast<float>(theta) * calc_ntk_factor(t_extrapolation_ratio, axes_dim[0]), static_cast<float>(theta) * calc_ntk_factor(t_extrapolation_ratio, axes_dim[0]),
static_cast<float>(theta) * calc_ntk_factor(h_extrapolation_ratio, axes_dim[1]), static_cast<float>(theta) * calc_ntk_factor(h_extrapolation_ratio, axes_dim[1]),
static_cast<float>(theta) * calc_ntk_factor(w_extrapolation_ratio, axes_dim[2]), static_cast<float>(theta) * calc_ntk_factor(w_extrapolation_ratio, axes_dim[2]),
}; };
result.values = Rope::embed_nd(result.ids, bs, axis_thetas, axes_dim, result.layout, &result.frequencies); return Rope::embed_nd(ids, bs, axis_thetas, axes_dim);
return result;
} }
ggml_cgraph* build_graph(const sd::Tensor<float>& x_tensor, ggml_cgraph* build_graph(const sd::Tensor<float>& x_tensor,
@ -660,16 +657,16 @@ namespace Anima {
int64_t h_pad = x->ne[1] + pad_h; int64_t h_pad = x->ne[1] + pad_h;
int64_t w_pad = x->ne[0] + pad_w; int64_t w_pad = x->ne[0] + pad_w;
image_pe_vec = finish_rope_pe(gen_anima_image_pe_vec(1, image_pe_vec = gen_anima_image_pe_vec(1,
static_cast<int>(h_pad), static_cast<int>(h_pad),
static_cast<int>(w_pad), static_cast<int>(w_pad),
static_cast<int>(config.patch_size), static_cast<int>(config.patch_size),
config.theta, config.theta,
config.axes_dim, config.axes_dim,
4.0f, 4.0f,
4.0f, 4.0f,
1.0f, 1.0f,
ref_latents)); ref_latents);
int64_t image_pos_len = static_cast<int64_t>(image_pe_vec.size()) / (2 * 2 * (config.head_dim / 2)); int64_t image_pos_len = static_cast<int64_t>(image_pe_vec.size()) / (2 * 2 * (config.head_dim / 2));
auto image_pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.head_dim / 2, image_pos_len); auto image_pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.head_dim / 2, image_pos_len);
set_backend_tensor_data(image_pe, image_pe_vec.data()); set_backend_tensor_data(image_pe, image_pe_vec.data());

View File

@ -720,18 +720,15 @@ namespace Boogu {
} }
} }
__STATIC_INLINE__ Rope::Embedding gen_boogu_pe(int h, __STATIC_INLINE__ std::vector<float> gen_boogu_pe(int h,
int w, int w,
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len,
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
int theta, int theta,
const std::vector<int>& axes_dim) { const std::vector<int>& axes_dim) {
Rope::Embedding result; std::vector<std::vector<float>> ids;
result.batch_size = bs;
result.positions.append_tokens(context_len);
auto& ids = result.ids;
ids.reserve(static_cast<size_t>(bs) * context_len); ids.reserve(static_cast<size_t>(bs) * context_len);
for (int b = 0; b < bs; b++) { for (int b = 0; b < bs; b++) {
for (int i = 0; i < context_len; i++) { for (int i = 0; i < context_len; i++) {
@ -744,18 +741,15 @@ namespace Boogu {
for (ggml_tensor* ref : ref_latents) { for (ggml_tensor* ref : ref_latents) {
int ref_h_tokens = patched_token_count(ref->ne[1], patch_size); int ref_h_tokens = patched_token_count(ref->ne[1], patch_size);
int ref_w_tokens = patched_token_count(ref->ne[0], patch_size); int ref_w_tokens = patched_token_count(ref->ne[0], patch_size);
result.positions.append_image(ref_h_tokens, ref_w_tokens);
append_spatial_ids(ids, bs, pe_shift, ref_h_tokens, ref_w_tokens); append_spatial_ids(ids, bs, pe_shift, ref_h_tokens, ref_w_tokens);
pe_shift += std::max(ref_h_tokens, ref_w_tokens); pe_shift += std::max(ref_h_tokens, ref_w_tokens);
} }
int h_tokens = patched_token_count(h, patch_size); int h_tokens = patched_token_count(h, patch_size);
int w_tokens = patched_token_count(w, patch_size); int w_tokens = patched_token_count(w, patch_size);
result.positions.append_image(h_tokens, w_tokens);
append_spatial_ids(ids, bs, pe_shift, h_tokens, w_tokens); append_spatial_ids(ids, bs, pe_shift, h_tokens, w_tokens);
result.values = Rope::embed_nd(ids, bs, static_cast<float>(theta), axes_dim, result.layout, &result.frequencies); return Rope::embed_nd(ids, bs, static_cast<float>(theta), axes_dim);
return result;
} }
struct BooguImageRunner : public DiffusionModelRunner { struct BooguImageRunner : public DiffusionModelRunner {
@ -799,14 +793,14 @@ namespace Boogu {
ref_latents.push_back(make_input(ref_latent_tensor)); ref_latents.push_back(make_input(ref_latent_tensor));
} }
pe_vec = finish_rope_pe(gen_boogu_pe(static_cast<int>(x->ne[1]), pe_vec = gen_boogu_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
config.patch_size, config.patch_size,
static_cast<int>(x->ne[3]), static_cast<int>(x->ne[3]),
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
ref_latents, ref_latents,
config.theta, config.theta,
config.axes_dim)); config.axes_dim);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);
set_backend_tensor_data(pe, pe_vec.data()); set_backend_tensor_data(pe, pe_vec.data());

View File

@ -415,13 +415,15 @@ namespace ErnieImage {
GGML_ASSERT(!context_tensor.empty()); GGML_ASSERT(!context_tensor.empty());
ggml_tensor* context = make_input(context_tensor); ggml_tensor* context = make_input(context_tensor);
pe_vec = finish_rope_pe(Rope::gen_ernie_image_pe(static_cast<int>(x->ne[1]), pe_vec = Rope::gen_ernie_image_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
config.patch_size, config.patch_size,
static_cast<int>(x->ne[3]), static_cast<int>(x->ne[3]),
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
config.theta, config.theta,
config.axes_dim)); circular_y_enabled,
circular_x_enabled,
config.axes_dim);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, config.axes_dim_sum, 1, pos_len, 2); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, config.axes_dim_sum, 1, pos_len, 2);
set_backend_tensor_data(pe, pe_vec.data()); set_backend_tensor_data(pe, pe_vec.data());

View File

@ -1548,18 +1548,20 @@ namespace Flux {
} else if (version == VERSION_OVIS_IMAGE) { } else if (version == VERSION_OVIS_IMAGE) {
txt_arange_dims = {1, 2}; txt_arange_dims = {1, 2};
} }
pe_vec = finish_rope_pe(Rope::gen_flux_pe(static_cast<int>(x->ne[1]), pe_vec = Rope::gen_flux_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
config.patch_size, config.patch_size,
static_cast<int>(x->ne[3]), static_cast<int>(x->ne[3]),
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
txt_arange_dims, txt_arange_dims,
ref_latents, ref_latents,
ref_index_mode, ref_index_mode,
config.ref_index_scale, config.ref_index_scale,
config.theta, config.theta,
config.axes_dim, circular_y_enabled,
sd_version_is_longcat(version))); circular_x_enabled,
config.axes_dim,
sd_version_is_longcat(version));
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
// LOG_VERBOSE("pos_len %d", pos_len); // LOG_VERBOSE("pos_len %d", pos_len);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);

View File

@ -149,21 +149,18 @@ namespace Ideogram4 {
return std::make_shared<Linear>(in_features, out_features, bias); return std::make_shared<Linear>(in_features, out_features, bias);
} }
__STATIC_INLINE__ Rope::Embedding gen_ideogram4_pe(int grid_h, __STATIC_INLINE__ std::vector<float> gen_ideogram4_pe(int grid_h,
int grid_w, int grid_w,
int bs, int bs,
int context_len, int context_len,
int head_dim, int head_dim,
int rope_theta, int rope_theta,
const std::vector<int>& mrope_section) { const std::vector<int>& mrope_section,
bool circular_x = false,
bool circular_y = false) {
GGML_ASSERT(bs == 1); GGML_ASSERT(bs == 1);
Rope::Embedding result; std::vector<std::vector<float>> ids(static_cast<size_t>(bs) * (context_len + grid_h * grid_w),
result.batch_size = bs; std::vector<float>(3, 0.f));
result.positions.append_tokens(context_len);
result.positions.append_image(grid_h, grid_w);
result.ids.assign(static_cast<size_t>(bs) * (context_len + grid_h * grid_w),
std::vector<float>(3, 0.f));
auto& ids = result.ids;
for (int i = 0; i < context_len; ++i) { for (int i = 0; i < context_len; ++i) {
ids[i] = {static_cast<float>(i), static_cast<float>(i), static_cast<float>(i)}; ids[i] = {static_cast<float>(i), static_cast<float>(i), static_cast<float>(i)};
@ -178,13 +175,29 @@ namespace Ideogram4 {
} }
} }
result.values = Rope::embed_interleaved_mrope(ids, std::vector<std::vector<int>> axis_wrap_dims(3);
bs, if (circular_y || circular_x) {
static_cast<float>(rope_theta), size_t total_len = static_cast<size_t>(bs) * (context_len + grid_h * grid_w);
head_dim, axis_wrap_dims[1].assign(total_len, 0);
mrope_section, axis_wrap_dims[2].assign(total_len, 0);
&result.frequencies); if (circular_y) {
return result; for (size_t idx = static_cast<size_t>(context_len); idx < total_len; ++idx) {
axis_wrap_dims[1][idx] = grid_h;
}
}
if (circular_x) {
for (size_t idx = static_cast<size_t>(context_len); idx < total_len; ++idx) {
axis_wrap_dims[2][idx] = grid_w;
}
}
}
return Rope::embed_interleaved_mrope(ids,
bs,
static_cast<float>(rope_theta),
head_dim,
mrope_section,
axis_wrap_dims);
} }
class Ideogram4Attention : public GGMLBlock { class Ideogram4Attention : public GGMLBlock {
@ -496,13 +509,15 @@ namespace Ideogram4 {
int64_t head_dim = config.emb_dim / config.num_heads; int64_t head_dim = config.emb_dim / config.num_heads;
auto runner_ctx = get_context(); auto runner_ctx = get_context();
pe_vec = finish_rope_pe(gen_ideogram4_pe(static_cast<int>(grid_h), pe_vec = gen_ideogram4_pe(static_cast<int>(grid_h),
static_cast<int>(grid_w), static_cast<int>(grid_w),
static_cast<int>(x->ne[3]), static_cast<int>(x->ne[3]),
static_cast<int>(context_len), static_cast<int>(context_len),
static_cast<int>(head_dim), static_cast<int>(head_dim),
static_cast<int>(config.rope_theta), static_cast<int>(config.rope_theta),
config.mrope_section)); config.mrope_section,
runner_ctx.circular_x_enabled,
runner_ctx.circular_y_enabled);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, head_dim / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, head_dim / 2, pos_len);
set_backend_tensor_data(pe, pe_vec.data()); set_backend_tensor_data(pe, pe_vec.data());

View File

@ -689,28 +689,23 @@ namespace Krea2 {
} }
}; };
__STATIC_INLINE__ Rope::Embedding gen_krea2_pe(int h, __STATIC_INLINE__ std::vector<float> gen_krea2_pe(int h,
int w, int w,
int patch_size, int patch_size,
int bs, int bs,
int context_len, int context_len,
float theta, float theta,
const std::vector<int>& axes_dim, const std::vector<int>& axes_dim,
const std::vector<ggml_tensor*>& ref_latents, const std::vector<ggml_tensor*>& ref_latents,
Rope::RefIndexMode ref_index_mode) { Rope::RefIndexMode ref_index_mode) {
Rope::Embedding result;
result.batch_size = bs;
result.positions.append_tokens(context_len);
auto txt_ids = Rope::gen_flux_txt_ids(bs, context_len, 3, {}); auto txt_ids = Rope::gen_flux_txt_ids(bs, context_len, 3, {});
auto img_ids = Rope::gen_flux_img_ids(h, w, patch_size, bs, 3, 0, 0, 0, false, &result.positions); auto img_ids = Rope::gen_flux_img_ids(h, w, patch_size, bs, 3, 0, 0, 0, false);
auto ids = Rope::concat_ids(txt_ids, img_ids, bs); auto ids = Rope::concat_ids(txt_ids, img_ids, bs);
if (ref_latents.size() > 0) { if (ref_latents.size() > 0) {
auto refs_ids = Rope::gen_refs_ids(patch_size, bs, 3, 1, ref_latents, ref_index_mode, 1.0f, false, 0, &result.positions); auto refs_ids = Rope::gen_refs_ids(patch_size, bs, 3, 1, ref_latents, ref_index_mode, 1.0f, false, 0);
ids = Rope::concat_ids(ids, refs_ids, bs); ids = Rope::concat_ids(ids, refs_ids, bs);
} }
result.ids = std::move(ids); return Rope::embed_nd(ids, bs, theta, axes_dim);
result.values = Rope::embed_nd(result.ids, bs, theta, axes_dim, result.layout, &result.frequencies);
return result;
} }
struct Krea2Runner : public DiffusionModelRunner { struct Krea2Runner : public DiffusionModelRunner {
@ -754,15 +749,15 @@ namespace Krea2 {
ref_latents.push_back(make_input(ref_latent_tensor)); ref_latents.push_back(make_input(ref_latent_tensor));
} }
pe_vec = finish_rope_pe(gen_krea2_pe(static_cast<int>(x->ne[1]), pe_vec = gen_krea2_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
config.patch_size, config.patch_size,
static_cast<int>(x->ne[3]), static_cast<int>(x->ne[3]),
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
config.theta, config.theta,
config.axes_dim, config.axes_dim,
ref_latents, ref_latents,
ref_image_params.ref_index_mode)); ref_image_params.ref_index_mode);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);
set_backend_tensor_data(pe, pe_vec.data()); set_backend_tensor_data(pe, pe_vec.data());

View File

@ -384,12 +384,14 @@ namespace Lens {
GGML_ASSERT(!context_tensor.empty()); GGML_ASSERT(!context_tensor.empty());
ggml_tensor* context = make_input(context_tensor); ggml_tensor* context = make_input(context_tensor);
pe_vec = finish_rope_pe(Rope::gen_lens_pe(static_cast<int>(x->ne[1]), pe_vec = Rope::gen_lens_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
static_cast<int>(x->ne[3]), static_cast<int>(x->ne[3]),
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
config.theta, config.theta,
config.axes_dim)); circular_y_enabled,
circular_x_enabled,
config.axes_dim);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);
set_backend_tensor_data(pe, pe_vec.data()); set_backend_tensor_data(pe, pe_vec.data());

View File

@ -412,14 +412,16 @@ namespace LLaDAImage {
GGML_ASSERT(!context_tensor.empty()); GGML_ASSERT(!context_tensor.empty());
ggml_tensor* context = make_input(context_tensor); ggml_tensor* context = make_input(context_tensor);
pe_vec = finish_rope_pe(Rope::gen_llada_image_pe(static_cast<int>(x->ne[1]), pe_vec = Rope::gen_llada_image_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
config.patch_size, config.patch_size,
static_cast<int>(x->ne[3]), static_cast<int>(x->ne[3]),
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
ZImage::SEQ_MULTI_OF, ZImage::SEQ_MULTI_OF,
config.theta, config.theta,
config.axes_dim)); circular_y_enabled,
circular_x_enabled,
config.axes_dim);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);
set_backend_tensor_data(pe, pe_vec.data()); set_backend_tensor_data(pe, pe_vec.data());
@ -459,14 +461,14 @@ namespace LLaDAImage {
ggml_tensor* source = make_input(source_tensor); ggml_tensor* source = make_input(source_tensor);
GGML_ASSERT(x->ne[3] == 1); GGML_ASSERT(x->ne[3] == 1);
pe_vec = finish_rope_pe(Rope::gen_llada_image_edit_pe(static_cast<int>(x->ne[1]), pe_vec = Rope::gen_llada_image_edit_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
config.patch_size, config.patch_size,
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
semantic != nullptr ? static_cast<int>(semantic->ne[1]) : 0, semantic != nullptr ? static_cast<int>(semantic->ne[1]) : 0,
ZImage::SEQ_MULTI_OF, ZImage::SEQ_MULTI_OF,
config.theta, config.theta,
config.axes_dim)); config.axes_dim);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);
set_backend_tensor_data(pe, pe_vec.data()); set_backend_tensor_data(pe, pe_vec.data());

View File

@ -110,13 +110,13 @@ namespace MageFlow {
} }
int batch_size = static_cast<int>(x->ne[3]); int batch_size = static_cast<int>(x->ne[3]);
pe_vec = finish_rope_pe(Rope::gen_mage_flow_pe(static_cast<int>(x->ne[1]), pe_vec = Rope::gen_mage_flow_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
batch_size, batch_size,
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
ref_latents, ref_latents,
config.theta, config.theta,
config.axes_dim)); config.axes_dim);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);
set_backend_tensor_data(pe, pe_vec.data()); set_backend_tensor_data(pe, pe_vec.data());

View File

@ -264,9 +264,6 @@ namespace MiniMaxH3 {
for (int64_t i = 0; i < num_layers; ++i) { for (int64_t i = 0; i < num_layers; ++i) {
auto block = std::dynamic_pointer_cast<TokenRefinerBlock>(blocks["blocks." + std::to_string(i)]); auto block = std::dynamic_pointer_cast<TokenRefinerBlock>(blocks["blocks." + std::to_string(i)]);
x = block->forward(ctx, x); x = block->forward(ctx, x);
sd::ggml_graph_cut::mark_graph_cut(x,
"minimax_h3.token_refiner.blocks." + std::to_string(i),
"hidden_states");
} }
return std::dynamic_pointer_cast<RMSNorm>(blocks["final_norm"])->forward(ctx, x); return std::dynamic_pointer_cast<RMSNorm>(blocks["final_norm"])->forward(ctx, x);
} }
@ -530,11 +527,7 @@ namespace MiniMaxH3 {
GGML_ASSERT(context->ne[0] == config.text_dim); GGML_ASSERT(context->ne[0] == config.text_dim);
auto condition_proj = std::dynamic_pointer_cast<Linear>(blocks["condition_proj"]); auto condition_proj = std::dynamic_pointer_cast<Linear>(blocks["condition_proj"]);
auto token_refiner = std::dynamic_pointer_cast<TokenRefiner>(blocks["token_refiner"]); auto token_refiner = std::dynamic_pointer_cast<TokenRefiner>(blocks["token_refiner"]);
auto projected = condition_proj->forward(ctx, context); return token_refiner->forward(ctx, condition_proj->forward(ctx, context));
sd::ggml_graph_cut::mark_graph_cut(projected,
"minimax_h3.condition_proj",
"hidden_states");
return token_refiner->forward(ctx, projected);
} }
ggml_tensor* time_embedding(GGMLRunnerContext* ctx, ggml_tensor* time_embedding(GGMLRunnerContext* ctx,

View File

@ -154,26 +154,18 @@ namespace MiniT2I {
return Rope::flatten(Rope::rope(Rope::linspace(0.f, static_cast<float>(length - 1), length), head_dim, 10000.f)); return Rope::flatten(Rope::rope(Rope::linspace(0.f, static_cast<float>(length - 1), length), head_dim, 10000.f));
} }
inline Rope::Embedding make_vision_rope(int side, int head_dim) { inline std::vector<float> make_vision_rope(int side, int head_dim) {
GGML_ASSERT(head_dim % 4 == 0); GGML_ASSERT(head_dim % 4 == 0);
int dim = head_dim / 2; int dim = head_dim / 2;
int quarter = dim / 2; int quarter = dim / 2;
int length = side * side; int length = side * side;
Rope::Embedding result;
result.positions.append_image(side, side);
std::vector<float> out(static_cast<size_t>(length) * (head_dim / 2) * 4); std::vector<float> out(static_cast<size_t>(length) * (head_dim / 2) * 4);
std::vector<float> freqs(quarter); std::vector<float> freqs(quarter);
for (int i = 0; i < quarter; ++i) { for (int i = 0; i < quarter; ++i) {
freqs[i] = 1.0f / std::pow(10000.0f, static_cast<float>(2 * i) / static_cast<float>(dim)); freqs[i] = 1.0f / std::pow(10000.0f, static_cast<float>(2 * i) / static_cast<float>(dim));
} }
for (int axis : {1, 2}) {
for (float frequency : freqs) {
result.frequencies.push_back({static_cast<size_t>(axis), frequency});
}
}
for (int y = 0; y < side; ++y) { for (int y = 0; y < side; ++y) {
for (int x = 0; x < side; ++x) { for (int x = 0; x < side; ++x) {
result.ids.push_back({0.f, static_cast<float>(y), static_cast<float>(x)});
int pos = y * side + x; int pos = y * side + x;
size_t base = static_cast<size_t>(pos) * (head_dim / 2) * 4; size_t base = static_cast<size_t>(pos) * (head_dim / 2) * 4;
for (int i = 0; i < quarter; ++i) { for (int i = 0; i < quarter; ++i) {
@ -190,8 +182,7 @@ namespace MiniT2I {
} }
} }
} }
result.values = std::move(out); return out;
return result;
} }
struct SwiGLUMlp : public GGMLBlock { struct SwiGLUMlp : public GGMLBlock {
@ -484,8 +475,6 @@ namespace MiniT2I {
int64_t cached_txt_len = -1; int64_t cached_txt_len = -1;
int64_t cached_hidden_size = -1; int64_t cached_hidden_size = -1;
int64_t cached_head_dim = -1; int64_t cached_head_dim = -1;
bool cached_circular_x = false;
bool cached_circular_y = false;
MiniT2IRunner(ggml_backend_t backend, MiniT2IRunner(ggml_backend_t backend,
const String2TensorStorage& tensor_storage_map = {}, const String2TensorStorage& tensor_storage_map = {},
@ -532,8 +521,6 @@ namespace MiniT2I {
cached_txt_len == txt_len && cached_txt_len == txt_len &&
cached_hidden_size == config.hidden_size && cached_hidden_size == config.hidden_size &&
cached_head_dim == config.head_dim && cached_head_dim == config.head_dim &&
cached_circular_x == circular_x_enabled &&
cached_circular_y == circular_y_enabled &&
cached_pos_embed != nullptr && cached_pos_embed != nullptr &&
cached_txt_pe != nullptr && cached_txt_pe != nullptr &&
cached_joint_pe != nullptr) { cached_joint_pe != nullptr) {
@ -544,7 +531,7 @@ namespace MiniT2I {
auto pos_embed_vec = make_2d_sincos_pos_embed(static_cast<int>(img_side), static_cast<int>(config.hidden_size)); auto pos_embed_vec = make_2d_sincos_pos_embed(static_cast<int>(img_side), static_cast<int>(config.hidden_size));
auto txt_pe_vec = make_text_rope(static_cast<int>(txt_len), static_cast<int>(config.head_dim)); auto txt_pe_vec = make_text_rope(static_cast<int>(txt_len), static_cast<int>(config.head_dim));
auto img_pe_vec = finish_rope_pe(make_vision_rope(static_cast<int>(img_side), static_cast<int>(config.head_dim))); auto img_pe_vec = make_vision_rope(static_cast<int>(img_side), static_cast<int>(config.head_dim));
auto joint_pe_vec = txt_pe_vec; auto joint_pe_vec = txt_pe_vec;
joint_pe_vec.insert(joint_pe_vec.end(), img_pe_vec.begin(), img_pe_vec.end()); joint_pe_vec.insert(joint_pe_vec.end(), img_pe_vec.begin(), img_pe_vec.end());
@ -574,8 +561,6 @@ namespace MiniT2I {
cached_txt_len = txt_len; cached_txt_len = txt_len;
cached_hidden_size = config.hidden_size; cached_hidden_size = config.hidden_size;
cached_head_dim = config.head_dim; cached_head_dim = config.head_dim;
cached_circular_x = circular_x_enabled;
cached_circular_y = circular_y_enabled;
} }
ggml_cgraph* build_graph(const sd::Tensor<float>& x_tensor, ggml_cgraph* build_graph(const sd::Tensor<float>& x_tensor,

View File

@ -7,7 +7,7 @@
#include "core/ggml_runner.h" #include "core/ggml_runner.h"
#include "core/tensor_ggml.hpp" #include "core/tensor_ggml.hpp"
#include "model/common/rope_circular.hpp" #include "model/common/rope.hpp"
#include "model_manager.h" #include "model_manager.h"
enum class RefImageResizeMode { enum class RefImageResizeMode {
@ -71,8 +71,6 @@ struct AnimaDiffusionExtra {
struct QwenImage21DiffusionExtra { struct QwenImage21DiffusionExtra {
const sd::Tensor<int32_t>* image_slots = nullptr; const sd::Tensor<int32_t>* image_slots = nullptr;
// Nonzero IDs identify immutable prefix inputs within one sampling run.
uint64_t prefix_id = 0;
}; };
struct WanDiffusionExtra { struct WanDiffusionExtra {
@ -184,11 +182,6 @@ struct DiffusionModelRunner : public GGMLRunner {
protected: protected:
std::string prefix; std::string prefix;
std::vector<float> finish_rope_pe(Rope::Embedding embedding) {
Rope::apply_circular(embedding, circular_x_enabled, circular_y_enabled);
return std::move(embedding.values);
}
public: public:
DiffusionModelRunner(ggml_backend_t backend, DiffusionModelRunner(ggml_backend_t backend,
const std::string& prefix, const std::string& prefix,

View File

@ -135,13 +135,13 @@ namespace Pid {
return Rope::flatten(Rope::rope(Rope::linspace(0.f, static_cast<float>(length - 1), length), dim, theta)); return Rope::flatten(Rope::rope(Rope::linspace(0.f, static_cast<float>(length - 1), length), dim, theta));
} }
inline Rope::Embedding make_rope_2d(int height, inline std::vector<float> make_rope_2d(int height,
int width, int width,
int dim, int dim,
float theta = 10000.f, float theta = 10000.f,
float scale = 16.f, float scale = 16.f,
int ref_grid_h = 0, int ref_grid_h = 0,
int ref_grid_w = 0) { int ref_grid_w = 0) {
GGML_ASSERT(dim % 4 == 0); GGML_ASSERT(dim % 4 == 0);
return Rope::embed_2d_interleaved(height, width, dim, theta, scale, ref_grid_h, ref_grid_w); return Rope::embed_2d_interleaved(height, width, dim, theta, scale, ref_grid_h, ref_grid_w);
} }
@ -867,13 +867,13 @@ namespace Pid {
int64_t Hs = Hp / config.patch_size; int64_t Hs = Hp / config.patch_size;
int64_t Ws = Wp / config.patch_size; int64_t Ws = Wp / config.patch_size;
pos_img_vec = finish_rope_pe(make_rope_2d(static_cast<int>(Hs), pos_img_vec = make_rope_2d(static_cast<int>(Hs),
static_cast<int>(Ws), static_cast<int>(Ws),
static_cast<int>(config.hidden_size / config.num_groups), static_cast<int>(config.hidden_size / config.num_groups),
10000.f, 10000.f,
16.f, 16.f,
static_cast<int>(config.rope_ref_grid_h), static_cast<int>(config.rope_ref_grid_h),
static_cast<int>(config.rope_ref_grid_w))); static_cast<int>(config.rope_ref_grid_w));
auto pos_img = ggml_new_tensor_4d(compute_ctx, auto pos_img = ggml_new_tensor_4d(compute_ctx,
GGML_TYPE_F32, GGML_TYPE_F32,
2, 2,
@ -904,13 +904,13 @@ namespace Pid {
1); 1);
set_backend_tensor_data(pixel_pos, pixel_pos_vec.data()); set_backend_tensor_data(pixel_pos, pixel_pos_vec.data());
pixel_pos_comp_vec = finish_rope_pe(make_rope_2d(static_cast<int>(Hs), pixel_pos_comp_vec = make_rope_2d(static_cast<int>(Hs),
static_cast<int>(Ws), static_cast<int>(Ws),
static_cast<int>(config.pixel_attn_hidden_size / config.pixel_num_groups), static_cast<int>(config.pixel_attn_hidden_size / config.pixel_num_groups),
10000.f, 10000.f,
16.f, 16.f,
static_cast<int>(config.rope_ref_grid_h), static_cast<int>(config.rope_ref_grid_h),
static_cast<int>(config.rope_ref_grid_w))); static_cast<int>(config.rope_ref_grid_w));
auto pixel_pos_comp = ggml_new_tensor_4d(compute_ctx, auto pixel_pos_comp = ggml_new_tensor_4d(compute_ctx,
GGML_TYPE_F32, GGML_TYPE_F32,
2, 2,

View File

@ -635,16 +635,18 @@ namespace Qwen {
ref_index_mode = Rope::RefIndexMode::DECREASE; ref_index_mode = Rope::RefIndexMode::DECREASE;
} }
pe_vec = finish_rope_pe(Rope::gen_qwen_image_pe(time_len, pe_vec = Rope::gen_qwen_image_pe(time_len,
static_cast<int>(x->ne[1]), static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
config.patch_size, config.patch_size,
batch_size, batch_size,
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
ref_latents, ref_latents,
ref_index_mode, ref_index_mode,
config.theta, config.theta,
config.axes_dim)); circular_y_enabled,
circular_x_enabled,
config.axes_dim);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
// LOG_VERBOSE("pos_len %d", pos_len); // LOG_VERBOSE("pos_len %d", pos_len);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);

View File

@ -68,7 +68,6 @@ namespace Qwen {
std::vector<QwenImage21Segment> segments; std::vector<QwenImage21Segment> segments;
std::vector<std::vector<float>> positions; std::vector<std::vector<float>> positions;
int64_t prefix_length = 0; int64_t prefix_length = 0;
Rope::PositionLayout rope_layout;
static QwenImage21Layout build(int64_t text_length, static QwenImage21Layout build(int64_t text_length,
const sd::Tensor<int32_t>& image_slots, const sd::Tensor<int32_t>& image_slots,
@ -83,7 +82,6 @@ namespace Qwen {
auto [height, width] = image_shapes[index]; auto [height, width] = image_shapes[index];
int64_t start = static_cast<int64_t>(layout.positions.size()); int64_t start = static_cast<int64_t>(layout.positions.size());
layout.segments.push_back({start, start + height * width, context_start, index}); layout.segments.push_back({start, start + height * width, context_start, index});
layout.rope_layout.append_image(static_cast<int>(height), static_cast<int>(width));
for (int64_t h = 0; h < height; ++h) { for (int64_t h = 0; h < height; ++h) {
for (int64_t w = 0; w < width; ++w) { for (int64_t w = 0; w < width; ++w) {
layout.positions.push_back({static_cast<float>(position), layout.positions.push_back({static_cast<float>(position),
@ -108,7 +106,6 @@ namespace Qwen {
} else { } else {
int64_t start = static_cast<int64_t>(layout.positions.size()); int64_t start = static_cast<int64_t>(layout.positions.size());
layout.segments.push_back({start, start + i - begin, begin, -1}); layout.segments.push_back({start, start + i - begin, begin, -1});
layout.rope_layout.append_tokens(i - begin);
for (int64_t j = begin; j < i; ++j, ++position) { for (int64_t j = begin; j < i; ++j, ++position) {
float p = static_cast<float>(position); float p = static_cast<float>(position);
layout.positions.push_back({p, p, p}); layout.positions.push_back({p, p, p});
@ -124,18 +121,6 @@ namespace Qwen {
} }
}; };
struct QwenImage21PrefixCache {
enum class Mode {
NONE,
STORE,
REUSE
};
Mode mode = Mode::NONE;
std::string name;
std::string cut_group;
int64_t prefix_length = 0;
};
class QwenImage21ZeroCenterRMSNorm : public RMSNorm { class QwenImage21ZeroCenterRMSNorm : public RMSNorm {
public: public:
using RMSNorm::RMSNorm; using RMSNorm::RMSNorm;
@ -175,49 +160,27 @@ namespace Qwen {
} }
} }
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x, ggml_tensor* pe, const std::vector<QwenImage21Segment>& segments, const std::vector<ggml_tensor*>& masks, const QwenImage21PrefixCache& cache) { ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x, ggml_tensor* pe, const std::vector<QwenImage21Segment>& segments, const std::vector<ggml_tensor*>& masks) {
int64_t heads = x->ne[0] / dim_head; int64_t heads = x->ne[0] / dim_head;
auto project = [&](const char* name) { auto project = [&](const char* name) {
auto h = std::dynamic_pointer_cast<Linear>(blocks[name])->forward(ctx, x); auto h = std::dynamic_pointer_cast<Linear>(blocks[name])->forward(ctx, x);
return ggml_reshape_4d(ctx->ggml_ctx, h, dim_head, heads, x->ne[1], x->ne[2]); return ggml_reshape_4d(ctx->ggml_ctx, h, dim_head, heads, x->ne[1], x->ne[2]);
}; };
auto q = project("to_q"); auto q = project("to_q");
auto k = project("to_k"); auto k = project("to_k");
auto v = project("to_v"); auto v = project("to_v");
q = std::dynamic_pointer_cast<RMSNorm>(blocks["norm_q"])->forward(ctx, q); q = std::dynamic_pointer_cast<RMSNorm>(blocks["norm_q"])->forward(ctx, q);
k = std::dynamic_pointer_cast<RMSNorm>(blocks["norm_k"])->forward(ctx, k); k = std::dynamic_pointer_cast<RMSNorm>(blocks["norm_k"])->forward(ctx, k);
q = Rope::apply_rope(ctx->ggml_ctx, q, pe); q = Rope::apply_rope(ctx->ggml_ctx, q, pe);
k = Rope::apply_rope(ctx->ggml_ctx, k, pe); k = Rope::apply_rope(ctx->ggml_ctx, k, pe);
if (cache.mode == QwenImage21PrefixCache::Mode::STORE) {
auto persist = [&](ggml_tensor* tensor, int axis, const char* name) {
auto part = ggml_ext_slice(ctx->ggml_ctx, tensor, axis, 0, cache.prefix_length);
auto copy = ggml_new_tensor(ctx->ggml_ctx, GGML_TYPE_F32, 4, part->ne);
copy = ggml_cpy(ctx->ggml_ctx, part, copy);
// Keep the copy in this layer's segment so graph cuts do not
// retain or recompute the full-sequence K/V in the final segment.
sd::ggml_graph_cut::mark_graph_cut(copy, cache.cut_group, name);
ctx->persist_cache_tensor(cache.name + "." + name, copy);
};
persist(k, 1, "k");
persist(v, 2, "v");
}
ggml_tensor* result = nullptr; ggml_tensor* result = nullptr;
if (cache.mode == QwenImage21PrefixCache::Mode::REUSE) { for (size_t i = 0; i < segments.size(); ++i) {
auto prefix_k = ctx->load_cache_tensor(cache.name + ".k"); const auto& segment = segments[i];
auto prefix_v = ctx->load_cache_tensor(cache.name + ".v"); auto sq = ggml_ext_slice(ctx->ggml_ctx, q, 1, segment.start, segment.end);
GGML_ASSERT(prefix_k != nullptr && prefix_v != nullptr); auto sk = ggml_ext_slice(ctx->ggml_ctx, k, 1, 0, segment.end);
k = ggml_concat(ctx->ggml_ctx, prefix_k, k, 1); auto sv = ggml_ext_slice(ctx->ggml_ctx, v, 2, 0, segment.end);
v = ggml_concat(ctx->ggml_ctx, prefix_v, v, 2); auto out = ggml_ext_attention_ext(ctx, sq, sk, sv, heads, masks[i], true, ctx->flash_attn_enabled);
result = ggml_ext_attention_ext(ctx, q, k, v, heads, nullptr, true, ctx->flash_attn_enabled); result = result == nullptr ? out : ggml_concat(ctx->ggml_ctx, result, out, 1);
} else {
for (size_t i = 0; i < segments.size(); ++i) {
const auto& segment = segments[i];
auto sq = ggml_ext_slice(ctx->ggml_ctx, q, 1, segment.start, segment.end);
auto sk = ggml_ext_slice(ctx->ggml_ctx, k, 1, 0, segment.end);
auto sv = ggml_ext_slice(ctx->ggml_ctx, v, 2, 0, segment.end);
auto out = ggml_ext_attention_ext(ctx, sq, sk, sv, heads, masks[i], true, ctx->flash_attn_enabled);
result = result == nullptr ? out : ggml_concat(ctx->ggml_ctx, result, out, 1);
}
} }
auto to_out = std::dynamic_pointer_cast<Linear>(blocks["to_out.0"]); auto to_out = std::dynamic_pointer_cast<Linear>(blocks["to_out.0"]);
if (sd_backend_is(ctx->backend, "Vulkan") || sd_backend_is(ctx->backend, "ROCm")) { if (sd_backend_is(ctx->backend, "Vulkan") || sd_backend_is(ctx->backend, "ROCm")) {
@ -256,14 +219,13 @@ namespace Qwen {
return ggml_concat(ctx, prefix, target, 1); return ggml_concat(ctx, prefix, target, 1);
} }
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x, const std::vector<ggml_tensor*>& modulation, ggml_tensor* pe, const QwenImage21Layout& layout, const std::vector<ggml_tensor*>& masks, const QwenImage21PrefixCache& cache) { ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x, const std::vector<ggml_tensor*>& modulation, ggml_tensor* pe, const QwenImage21Layout& layout, const std::vector<ggml_tensor*>& masks) {
const int64_t prefix_length = cache.mode == QwenImage21PrefixCache::Mode::REUSE ? 0 : layout.prefix_length; auto h = std::dynamic_pointer_cast<LayerNorm>(blocks["img_norm1"])->forward(ctx, x);
auto h = std::dynamic_pointer_cast<LayerNorm>(blocks["img_norm1"])->forward(ctx, x); h = modulate(ctx->ggml_ctx, h, modulation[0], layout.prefix_length);
h = modulate(ctx->ggml_ctx, h, modulation[0], prefix_length); h = std::dynamic_pointer_cast<QwenImage21Attention>(blocks["attn"])->forward(ctx, h, pe, layout.segments, masks);
h = std::dynamic_pointer_cast<QwenImage21Attention>(blocks["attn"])->forward(ctx, h, pe, layout.segments, masks, cache); x = ggml_add(ctx->ggml_ctx, x, modulate(ctx->ggml_ctx, h, modulation[1], layout.prefix_length, true));
x = ggml_add(ctx->ggml_ctx, x, modulate(ctx->ggml_ctx, h, modulation[1], prefix_length, true)); h = std::dynamic_pointer_cast<LayerNorm>(blocks["img_norm2"])->forward(ctx, x);
h = std::dynamic_pointer_cast<LayerNorm>(blocks["img_norm2"])->forward(ctx, x); h = modulate(ctx->ggml_ctx, h, modulation[2], layout.prefix_length);
h = modulate(ctx->ggml_ctx, h, modulation[2], prefix_length);
ggml_tensor* gate; ggml_tensor* gate;
auto fused = blocks.find("img_mlp.gate_up"); auto fused = blocks.find("img_mlp.gate_up");
if (fused != blocks.end()) { if (fused != blocks.end()) {
@ -277,7 +239,7 @@ namespace Qwen {
} }
h = ggml_mul(ctx->ggml_ctx, h, ggml_silu(ctx->ggml_ctx, gate)); h = ggml_mul(ctx->ggml_ctx, h, ggml_silu(ctx->ggml_ctx, gate));
h = std::dynamic_pointer_cast<Linear>(blocks["img_mlp.out"])->forward(ctx, h); h = std::dynamic_pointer_cast<Linear>(blocks["img_mlp.out"])->forward(ctx, h);
return ggml_add(ctx->ggml_ctx, x, modulate(ctx->ggml_ctx, h, modulation[3], prefix_length, true)); return ggml_add(ctx->ggml_ctx, x, modulate(ctx->ggml_ctx, h, modulation[3], layout.prefix_length, true));
} }
}; };
@ -299,7 +261,7 @@ namespace Qwen {
} }
} }
ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x, ggml_tensor* timestep, ggml_tensor* context, const std::vector<ggml_tensor*>& refs, ggml_tensor* pe, const QwenImage21Layout& layout, const std::vector<ggml_tensor*>& masks, const QwenImage21PrefixCache& cache) { ggml_tensor* forward(GGMLRunnerContext* ctx, ggml_tensor* x, ggml_tensor* timestep, ggml_tensor* context, const std::vector<ggml_tensor*>& refs, ggml_tensor* pe, const QwenImage21Layout& layout, const std::vector<ggml_tensor*>& masks) {
auto time = ggml_concat(ctx->ggml_ctx, timestep, ggml_ext_zeros_like(ctx->ggml_ctx, timestep), 0); auto time = ggml_concat(ctx->ggml_ctx, timestep, ggml_ext_zeros_like(ctx->ggml_ctx, timestep), 0);
// Runtime flow timesteps already use the [0, 1000] scale. // Runtime flow timesteps already use the [0, 1000] scale.
time = ggml_ext_timestep_embedding(ctx->ggml_ctx, time, 256, 10000, 1.f); time = ggml_ext_timestep_embedding(ctx->ggml_ctx, time, 256, 10000, 1.f);
@ -307,37 +269,27 @@ namespace Qwen {
time = ggml_silu(ctx->ggml_ctx, time); time = ggml_silu(ctx->ggml_ctx, time);
auto modulation = std::dynamic_pointer_cast<Linear>(blocks["modulation.1"])->forward(ctx, time); auto modulation = std::dynamic_pointer_cast<Linear>(blocks["modulation.1"])->forward(ctx, time);
auto mod = ggml_ext_chunk(ctx->ggml_ctx, modulation, 4, 0); auto mod = ggml_ext_chunk(ctx->ggml_ctx, modulation, 4, 0);
auto text = std::dynamic_pointer_cast<QwenImage21TextProjection>(blocks["txt_in"])->forward(ctx, context);
auto img_in = std::dynamic_pointer_cast<Linear>(blocks["img_in"]); auto img_in = std::dynamic_pointer_cast<Linear>(blocks["img_in"]);
ggml_tensor* joint = nullptr; ggml_tensor* joint = nullptr;
if (cache.mode == QwenImage21PrefixCache::Mode::REUSE) { for (const auto& segment : layout.segments) {
joint = img_in->forward(ctx, DiT::patchify(ctx->ggml_ctx, x, 1, 1)); ggml_tensor* h;
} else { if (segment.image_index < 0) {
auto text = std::dynamic_pointer_cast<QwenImage21TextProjection>(blocks["txt_in"])->forward(ctx, context); h = ggml_ext_slice(ctx->ggml_ctx, text, 1, segment.context_start,
for (const auto& segment : layout.segments) { segment.context_start + segment.end - segment.start);
ggml_tensor* h; } else {
if (segment.image_index < 0) { auto image = segment.image_index == static_cast<int>(refs.size()) ? x : refs[segment.image_index];
h = ggml_ext_slice(ctx->ggml_ctx, text, 1, segment.context_start, h = img_in->forward(ctx, DiT::patchify(ctx->ggml_ctx, image, 1, 1));
segment.context_start + segment.end - segment.start);
} else {
auto image = segment.image_index == static_cast<int>(refs.size()) ? x : refs[segment.image_index];
h = img_in->forward(ctx, DiT::patchify(ctx->ggml_ctx, image, 1, 1));
}
joint = joint == nullptr ? h : ggml_concat(ctx->ggml_ctx, joint, h, 1);
} }
joint = joint == nullptr ? h : ggml_concat(ctx->ggml_ctx, joint, h, 1);
} }
sd::ggml_graph_cut::mark_graph_cut(joint, "qwen_image_2_1.prelude", "joint"); sd::ggml_graph_cut::mark_graph_cut(joint, "qwen_image_2_1.prelude", "joint");
for (int i = 0; i < config.num_layers; ++i) { for (int i = 0; i < config.num_layers; ++i) {
const std::string layer = "transformer_blocks." + std::to_string(i); auto block = std::dynamic_pointer_cast<QwenImage21TransformerBlock>(blocks["transformer_blocks." + std::to_string(i)]);
auto layer_cache = cache; joint = block->forward(ctx, joint, mod, pe, layout, masks);
layer_cache.name = cache.name + "." + std::to_string(i); sd::ggml_graph_cut::mark_graph_cut(joint, "qwen_image_2_1.transformer_blocks." + std::to_string(i), "joint");
layer_cache.cut_group = "qwen_image_2_1." + layer;
auto block = std::dynamic_pointer_cast<QwenImage21TransformerBlock>(blocks[layer]);
joint = block->forward(ctx, joint, mod, pe, layout, masks, layer_cache);
sd::ggml_graph_cut::mark_graph_cut(joint, layer_cache.cut_group, "joint");
}
if (cache.mode != QwenImage21PrefixCache::Mode::REUSE) {
joint = ggml_ext_slice(ctx->ggml_ctx, joint, 1, layout.prefix_length, joint->ne[1]);
} }
joint = ggml_ext_slice(ctx->ggml_ctx, joint, 1, layout.prefix_length, joint->ne[1]);
auto scale = std::dynamic_pointer_cast<Linear>(blocks["norm_out.linear"])->forward(ctx, ggml_ext_chunk(ctx->ggml_ctx, time, 2, 1)[0]); auto scale = std::dynamic_pointer_cast<Linear>(blocks["norm_out.linear"])->forward(ctx, ggml_ext_chunk(ctx->ggml_ctx, time, 2, 1)[0]);
joint = std::dynamic_pointer_cast<LayerNorm>(blocks["norm_out.norm"])->forward(ctx, joint); joint = std::dynamic_pointer_cast<LayerNorm>(blocks["norm_out.norm"])->forward(ctx, joint);
joint = ggml_mul(ctx->ggml_ctx, joint, ggml_scale_bias(ctx->ggml_ctx, scale, 1.f, 1.f)); joint = ggml_mul(ctx->ggml_ctx, joint, ggml_scale_bias(ctx->ggml_ctx, scale, 1.f, 1.f));
@ -351,18 +303,11 @@ namespace Qwen {
QwenImage21Model model; QwenImage21Model model;
std::vector<float> pe_data; std::vector<float> pe_data;
std::vector<sd::Tensor<float>> mask_data; std::vector<sd::Tensor<float>> mask_data;
bool prefix_cache_enabled = true;
bool prefix_cache_disabled = false;
QwenImage21Runner(ggml_backend_t backend, const String2TensorStorage& weights, const std::string& prefix, std::shared_ptr<RunnerWeightManager> weight_manager = nullptr, const char* model_args = nullptr) QwenImage21Runner(ggml_backend_t backend, const String2TensorStorage& weights, const std::string& prefix, std::shared_ptr<RunnerWeightManager> weight_manager = nullptr)
: DiffusionModelRunner(backend, prefix, weight_manager), : DiffusionModelRunner(backend, prefix, weight_manager),
config(QwenImage21Config::detect_from_weights(weights, prefix)), config(QwenImage21Config::detect_from_weights(weights, prefix)),
model(config) { model(config) {
for (const auto& [key, value] : parse_key_value_args(model_args, "model arg")) {
if (key == "qwen_image_2_1_prefix_cache" && !parse_strict_bool(value, prefix_cache_enabled)) {
LOG_WARN("ignoring invalid Qwen Image 2.1 model arg '%s=%s'", key.c_str(), value.c_str());
}
}
model.init(params_ctx, weights, prefix); model.init(params_ctx, weights, prefix);
} }
@ -372,22 +317,6 @@ namespace Qwen {
model.get_param_tensors(tensors, prefix); model.get_param_tensors(tensors, prefix);
} }
bool has_prefix_cache(const QwenImage21PrefixCache& cache) {
for (int i = 0; i < config.num_layers; ++i) {
const auto name = cache.name + "." + std::to_string(i);
auto k = get_cache_tensor_by_name(name + ".k");
auto v = get_cache_tensor_by_name(name + ".v");
if (k == nullptr || v == nullptr || k->type != GGML_TYPE_F32 || v->type != GGML_TYPE_F32 ||
k->ne[0] != config.head_dim || k->ne[1] != cache.prefix_length ||
k->ne[2] != config.hidden_size / config.head_dim || k->ne[3] != 1 ||
v->ne[0] != config.head_dim || v->ne[1] != config.hidden_size / config.head_dim ||
v->ne[2] != cache.prefix_length || v->ne[3] != 1) {
return false;
}
}
return true;
}
sd::Tensor<float> compute(int n_threads, const DiffusionParams& inputs) override { sd::Tensor<float> compute(int n_threads, const DiffusionParams& inputs) override {
const auto& x = tensor_or_empty(inputs.x); const auto& x = tensor_or_empty(inputs.x);
const auto& context = tensor_or_empty(inputs.context); const auto& context = tensor_or_empty(inputs.context);
@ -416,87 +345,38 @@ namespace Qwen {
LOG_ERROR("%s", error.what()); LOG_ERROR("%s", error.what());
return {}; return {};
} }
if (!runner_started()) { pe_data = Rope::embed_nd(layout.positions, 1, 10000.f, config.axes_dim);
prefix_cache_disabled = false; mask_data.clear();
} for (const auto& segment : layout.segments) {
QwenImage21PrefixCache cache; sd::Tensor<float> mask;
if (prefix_cache_enabled && !prefix_cache_disabled && extra != nullptr && extra->prefix_id != 0 && layout.prefix_length > 0) { if (segment.image_index < 0) {
cache.name = "qwen_image_2_1.prefix." + std::to_string(extra->prefix_id) + mask = sd::Tensor<float>::zeros({segment.end, segment.end - segment.start});
".circular." + std::to_string(circular_x_enabled) + std::to_string(circular_y_enabled); for (int64_t q = segment.start; q < segment.end; ++q) {
cache.prefix_length = layout.prefix_length; for (int64_t k = q + 1; k < segment.end; ++k) {
cache.mode = has_prefix_cache(cache) ? QwenImage21PrefixCache::Mode::REUSE : QwenImage21PrefixCache::Mode::STORE; mask[k + segment.end * (q - segment.start)] = -INFINITY;
}
auto run = [&](const QwenImage21PrefixCache& active_cache) {
const bool cached = active_cache.mode == QwenImage21PrefixCache::Mode::REUSE;
const auto first_position = layout.positions.begin() + (cached ? layout.prefix_length : 0);
Rope::Embedding embedding;
embedding.ids.assign(first_position, layout.positions.end());
const size_t offset = cached ? static_cast<size_t>(layout.prefix_length) : 0;
embedding.positions.token_count = embedding.ids.size();
for (auto region : layout.rope_layout.images) {
if (region.begin >= offset) {
region.begin -= offset;
embedding.positions.images.push_back(region);
}
}
embedding.values = Rope::embed_nd(embedding.ids, 1, 10000.f, config.axes_dim, embedding.layout, &embedding.frequencies);
pe_data = finish_rope_pe(std::move(embedding));
mask_data.clear();
if (!cached) {
for (const auto& segment : layout.segments) {
sd::Tensor<float> mask;
if (segment.image_index < 0) {
mask = sd::Tensor<float>::zeros({segment.end, segment.end - segment.start});
for (int64_t q = segment.start; q < segment.end; ++q) {
for (int64_t k = q + 1; k < segment.end; ++k) {
mask[k + segment.end * (q - segment.start)] = -INFINITY;
}
}
}
mask_data.push_back(std::move(mask));
}
}
auto build = [&]() {
auto graph = new_graph_custom(QWEN_IMAGE_GRAPH_SIZE * 2);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.head_dim / 2,
layout.positions.size() - (cached ? layout.prefix_length : 0));
set_backend_tensor_data(pe, pe_data.data());
std::vector<ggml_tensor*> masks, ref_inputs;
for (const auto& mask : mask_data) {
masks.push_back(mask.empty() ? nullptr : make_input(mask));
}
if (!cached) {
for (const auto& ref : refs) {
ref_inputs.push_back(make_input(ref));
} }
} }
auto ctx = get_context(); }
auto out = model.forward(&ctx, make_input(x), make_input(*inputs.timesteps), cached ? nullptr : make_input(context), mask_data.push_back(std::move(mask));
ref_inputs, pe, layout, masks, active_cache); }
ggml_build_forward_expand(graph, out); auto build = [&]() {
return graph; auto graph = new_graph_custom(QWEN_IMAGE_GRAPH_SIZE * 2);
}; auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.head_dim / 2, layout.positions.size());
return restore_trailing_singleton_dims(GGMLRunner::compute(build, n_threads, false), x.dim()); set_backend_tensor_data(pe, pe_data.data());
std::vector<ggml_tensor*> masks, ref_inputs;
for (const auto& mask : mask_data) {
masks.push_back(mask.empty() ? nullptr : make_input(mask));
}
for (const auto& ref : refs) {
ref_inputs.push_back(make_input(ref));
}
auto ctx = get_context();
auto out = model.forward(&ctx, make_input(x), make_input(*inputs.timesteps), make_input(context),
ref_inputs, pe, layout, masks);
ggml_build_forward_expand(graph, out);
return graph;
}; };
auto result = run(cache); return restore_trailing_singleton_dims(GGMLRunner::compute(build, n_threads, false), x.dim());
if (result.empty() && last_compute_status() == GGML_STATUS_ALLOC_FAILED &&
(cache.mode != QwenImage21PrefixCache::Mode::NONE || !cache_.empty())) {
// The failed graph has ended before persistent inputs are released.
free_cache_ctx_and_buffer();
prefix_cache_disabled = true;
LOG_WARN("Qwen Image 2.1: insufficient memory for prefix caching; retrying without it for this sampling run");
return run(QwenImage21PrefixCache{});
}
if (!result.empty() && cache.mode == QwenImage21PrefixCache::Mode::STORE) {
if (!has_prefix_cache(cache)) {
free_cache_ctx_and_buffer();
prefix_cache_disabled = true;
LOG_WARN("Qwen Image 2.1: incomplete prefix cache; disabling it for this sampling run");
} else {
LOG_DEBUG("Qwen Image 2.1: cached prefix %" PRIu64 " (%" PRId64 " tokens)", extra->prefix_id, layout.prefix_length);
}
}
return result;
} }
}; };
} }

View File

@ -642,16 +642,18 @@ namespace ZImage {
ref_latents.push_back(make_input(ref_latent_tensor)); ref_latents.push_back(make_input(ref_latent_tensor));
} }
pe_vec = finish_rope_pe(Rope::gen_z_image_pe(static_cast<int>(x->ne[1]), pe_vec = Rope::gen_z_image_pe(static_cast<int>(x->ne[1]),
static_cast<int>(x->ne[0]), static_cast<int>(x->ne[0]),
config.patch_size, config.patch_size,
static_cast<int>(x->ne[3]), static_cast<int>(x->ne[3]),
static_cast<int>(context->ne[1]), static_cast<int>(context->ne[1]),
SEQ_MULTI_OF, SEQ_MULTI_OF,
ref_latents, ref_latents,
ref_index_mode, ref_index_mode,
config.theta, config.theta,
config.axes_dim)); circular_y_enabled,
circular_x_enabled,
config.axes_dim);
int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2); int pos_len = static_cast<int>(pe_vec.size() / config.axes_dim_sum / 2);
// LOG_VERBOSE("pos_len %d", pos_len); // LOG_VERBOSE("pos_len %d", pos_len);
auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len); auto pe = ggml_new_tensor_4d(compute_ctx, GGML_TYPE_F32, 2, 2, config.axes_dim_sum / 2, pos_len);

View File

@ -82,20 +82,6 @@ namespace WAN {
} }
x = ggml_ext_pad_ext(ctx->ggml_ctx, ctx->backend, x, lp0, rp0, lp1, rp1, lp2, rp2, 0, 0, ctx->circular_x_enabled, ctx->circular_y_enabled); x = ggml_ext_pad_ext(ctx->ggml_ctx, ctx->backend, x, lp0, rp0, lp1, rp1, lp2, rp2, 0, 0, ctx->circular_x_enabled, ctx->circular_y_enabled);
if (w->ne[2] == 1 && x->ne[2] == 1 && x->ne[3] == in_channels) {
// One frame through a one-frame-deep kernel is a 2D conv; backends without
// im2col_3d (Metal) otherwise fall back to a much slower direct conv_3d.
if (!ggml_is_contiguous(x)) {
x = ggml_cont(ctx->ggml_ctx, x);
}
ggml_tensor* x2 = ggml_reshape_4d(ctx->ggml_ctx, x, x->ne[0], x->ne[1], in_channels, 1);
ggml_tensor* w2 = ggml_reshape_4d(ctx->ggml_ctx, w, w->ne[0], w->ne[1], in_channels, out_channels);
x2 = ggml_ext_conv_2d(ctx->ggml_ctx, x2, w2, b,
std::get<2>(stride), std::get<1>(stride), 0, 0,
std::get<2>(dilation), std::get<1>(dilation),
ctx->conv2d_direct_enabled);
return ggml_reshape_4d(ctx->ggml_ctx, x2, x2->ne[0], x2->ne[1], 1, out_channels);
}
return ggml_ext_conv_3d(ctx->ggml_ctx, ctx->backend, x, w, b, in_channels, return ggml_ext_conv_3d(ctx->ggml_ctx, ctx->backend, x, w, b, in_channels,
std::get<2>(stride), std::get<1>(stride), std::get<0>(stride), std::get<2>(stride), std::get<1>(stride), std::get<0>(stride),
0, 0, 0, 0, 0, 0,

View File

@ -874,8 +874,7 @@ void ModelLoader::process_model_files(bool enable_mmap, bool writable_mmap) {
std::vector<MmapTensorStore> ModelLoader::mmap_tensors(std::map<std::string, ggml_tensor*>& tensors, std::vector<MmapTensorStore> ModelLoader::mmap_tensors(std::map<std::string, ggml_tensor*>& tensors,
std::set<std::string> ignore_tensors, std::set<std::string> ignore_tensors,
bool writable_mmap, bool writable_mmap) {
ggml_backend_dev_t device) {
std::set<std::string> names; std::set<std::string> names;
for (const auto& entry : tensors) { for (const auto& entry : tensors) {
names.insert(entry.first); names.insert(entry.first);
@ -897,39 +896,6 @@ std::vector<MmapTensorStore> ModelLoader::mmap_tensors(std::map<std::string, ggm
if (!fdata.mmbuffer) if (!fdata.mmbuffer)
continue; continue;
// Wrapped on first use: a device buffer makes the whole file resident on that device.
std::shared_ptr<struct ggml_backend_buffer> file_buffer = device == nullptr ? fdata.mmbuffer : nullptr;
bool file_unmappable = false;
auto buffer_for_file = [&]() -> ggml_backend_buffer_t {
if (file_buffer || file_unmappable) {
return file_buffer.get();
}
auto cached = fdata.device_mmbuffers.find(device);
if (cached != fdata.device_mmbuffers.end()) {
file_buffer = cached->second;
return file_buffer.get();
}
size_t max_tensor_size = 0;
for (const auto& ts : fdata.tensors) {
max_tensor_size = std::max(max_tensor_size, static_cast<size_t>(ts.nbytes()));
}
ggml_backend_buffer_t buf = sd_backend_dev_buffer_from_host_ptr(device,
fdata.mmapped->writable_data(),
fdata.mmapped->size(),
max_tensor_size);
if (buf == nullptr) {
LOG_WARN("mmap: %s cannot map '%s', loading it instead",
ggml_backend_dev_name(device), fdata.path.c_str());
file_unmappable = true;
return nullptr;
}
LOG_INFO("mmap: mapped '%s' for %s", fdata.path.c_str(), ggml_backend_dev_name(device));
file_buffer = std::shared_ptr<struct ggml_backend_buffer>(buf, ggml_backend_buffer_free);
fdata.device_mmbuffers[device] = file_buffer;
return file_buffer.get();
};
const std::vector<TensorStorage>& file_tensors = fdata.tensors; const std::vector<TensorStorage>& file_tensors = fdata.tensors;
size_t file_mapped_bytes = 0; size_t file_mapped_bytes = 0;
@ -978,13 +944,10 @@ std::vector<MmapTensorStore> ModelLoader::mmap_tensors(std::map<std::string, ggm
continue; continue;
} }
ggml_backend_buffer_t buf_mmap = buffer_for_file(); ggml_backend_buffer_t buf_mmap = fdata.mmbuffer.get();
if (buf_mmap == nullptr) { uint8_t* mmap_data = static_cast<uint8_t*>(ggml_backend_buffer_get_base(buf_mmap));
break; dst_tensor->buffer = buf_mmap;
} dst_tensor->data = mmap_data + tensor_offset;
uint8_t* mmap_data = static_cast<uint8_t*>(ggml_backend_buffer_get_base(buf_mmap));
dst_tensor->buffer = buf_mmap;
dst_tensor->data = mmap_data + tensor_offset;
file_mapped_bytes += tensor_size; file_mapped_bytes += tensor_size;
file_mapped_tensors++; file_mapped_tensors++;
@ -993,7 +956,7 @@ std::vector<MmapTensorStore> ModelLoader::mmap_tensors(std::map<std::string, ggm
if (file_mapped_bytes > 0) { if (file_mapped_bytes > 0) {
mapped_tensors += file_mapped_tensors; mapped_tensors += file_mapped_tensors;
mapped_bytes += file_mapped_bytes; mapped_bytes += file_mapped_bytes;
result.push_back({fdata.mmapped, file_buffer}); result.push_back({fdata.mmapped, fdata.mmbuffer});
} }
} }
@ -1009,16 +972,6 @@ std::vector<MmapTensorStore> ModelLoader::mmap_tensors(std::map<std::string, ggm
return result; return result;
} }
std::vector<ggml_backend_buffer_t> ModelLoader::get_device_mmap_buffers() const {
std::vector<ggml_backend_buffer_t> buffers;
for (const auto& fdata : file_data) {
for (const auto& entry : fdata.device_mmbuffers) {
buffers.push_back(entry.second.get());
}
}
return buffers;
}
bool ModelLoader::load_tensors(on_new_tensor_cb_t on_new_tensor_cb, bool ModelLoader::load_tensors(on_new_tensor_cb_t on_new_tensor_cb,
bool enable_mmap, bool enable_mmap,
const std::set<std::string>* target_tensor_names, const std::set<std::string>* target_tensor_names,
@ -1162,11 +1115,6 @@ bool ModelLoader::load_tensors(on_new_tensor_cb_t on_new_tensor_cb,
if (dst_tensor->buffer != nullptr && dst_tensor->buffer == fdata.mmbuffer.get()) { if (dst_tensor->buffer != nullptr && dst_tensor->buffer == fdata.mmbuffer.get()) {
continue; continue;
} }
if (dst_tensor->buffer != nullptr &&
std::any_of(fdata.device_mmbuffers.begin(), fdata.device_mmbuffers.end(),
[&](const auto& entry) { return entry.second.get() == dst_tensor->buffer; })) {
continue;
}
size_t nbytes_to_read = tensor_storage.nbytes_to_read(); size_t nbytes_to_read = tensor_storage.nbytes_to_read();

View File

@ -20,8 +20,6 @@ struct ModelFileData {
std::vector<TensorStorage> tensors; std::vector<TensorStorage> tensors;
std::shared_ptr<MmapWrapper> mmapped; std::shared_ptr<MmapWrapper> mmapped;
std::shared_ptr<struct ggml_backend_buffer> mmbuffer; std::shared_ptr<struct ggml_backend_buffer> mmbuffer;
// mmapped wrapped by devices that can use host memory in place (buffer_from_host_ptr)
std::map<ggml_backend_dev_t, std::shared_ptr<struct ggml_backend_buffer>> device_mmbuffers;
bool is_zip; bool is_zip;
}; };
@ -122,9 +120,7 @@ public:
void process_model_files(bool enable_mmap = false, bool writable_mmap = true); void process_model_files(bool enable_mmap = false, bool writable_mmap = true);
std::vector<MmapTensorStore> mmap_tensors(std::map<std::string, ggml_tensor*>& tensors, std::vector<MmapTensorStore> mmap_tensors(std::map<std::string, ggml_tensor*>& tensors,
std::set<std::string> ignore_tensors = {}, std::set<std::string> ignore_tensors = {},
bool writable = true, bool writable = true);
ggml_backend_dev_t device = nullptr);
std::vector<ggml_backend_buffer_t> get_device_mmap_buffers() const;
bool load_tensors(on_new_tensor_cb_t on_new_tensor_cb, bool load_tensors(on_new_tensor_cb_t on_new_tensor_cb,
bool use_mmap = false, bool use_mmap = false,
const std::set<std::string>* target_tensor_names = nullptr, const std::set<std::string>* target_tensor_names = nullptr,

View File

@ -780,52 +780,38 @@ bool ModelManager::validate_tensor(const TensorState& state) const {
bool ModelManager::mmap_params(const std::vector<TensorState*>& states, bool ModelManager::mmap_params(const std::vector<TensorState*>& states,
std::vector<ParamsStorageBlock*>& created_storage_blocks) { std::vector<ParamsStorageBlock*>& created_storage_blocks) {
// A GPU that computes on mmapped params in place cannot address a CPU buffer, and nothing std::map<std::string, ggml_tensor*> mmap_candidates;
// stages them for it, so they are mapped through a buffer of that GPU's device. std::map<std::string, TensorState*> mmap_states;
struct MmapGroup {
std::map<std::string, ggml_tensor*> candidates;
std::map<std::string, TensorState*> states;
};
std::map<ggml_backend_dev_t, MmapGroup> groups;
for (TensorState* state : states) { for (TensorState* state : states) {
if (state == nullptr || !can_mmap_storage(*state) || state->tensor == nullptr || if (state == nullptr || !can_mmap_storage(*state) || state->tensor == nullptr ||
state->tensor->data != nullptr || state->tensor->view_src != nullptr) { state->tensor->data != nullptr || state->tensor->view_src != nullptr) {
continue; continue;
} }
ggml_backend_dev_t device = nullptr; mmap_candidates[state->name] = state->tensor;
if (!sd_backend_is_cpu(state->compute_backend) && !sd_backend_is_cpu(state->params_backend)) { mmap_states[state->name] = state;
device = ggml_backend_get_device(state->compute_backend); }
} if (mmap_candidates.empty()) {
MmapGroup& group = groups[device]; return true;
group.candidates[state->name] = state->tensor;
group.states[state->name] = state;
} }
for (auto& [device, group] : groups) { auto mmap_store = model_loader_.mmap_tensors(mmap_candidates, {}, writable_mmap_);
// Device buffers wrap read-only mappings only; params that LoRAs are merged into in place if (mmap_store.empty()) {
// are loaded instead. return true;
if (device != nullptr && writable_mmap_) { }
continue;
}
auto mmap_store = model_loader_.mmap_tensors(group.candidates, {}, writable_mmap_, device);
if (mmap_store.empty()) {
continue;
}
auto block = std::make_unique<ParamsStorageBlock>(); auto block = std::make_unique<ParamsStorageBlock>();
block->mmap_tensor_stores = std::move(mmap_store); block->mmap_tensor_stores = std::move(mmap_store);
ParamsStorageBlock* raw = block.get(); ParamsStorageBlock* raw = block.get();
for (const auto& pair : group.states) { for (const auto& pair : mmap_states) {
TensorState* state = pair.second; TensorState* state = pair.second;
if (state != nullptr && state->tensor != nullptr && state->tensor->data != nullptr) { if (state != nullptr && state->tensor != nullptr && state->tensor->data != nullptr) {
block->states.push_back(state); block->states.push_back(state);
}
} }
}
if (!block->states.empty()) { if (!block->states.empty()) {
params_storage_blocks_.push_back(std::move(block)); params_storage_blocks_.push_back(std::move(block));
created_storage_blocks.push_back(raw); created_storage_blocks.push_back(raw);
}
} }
return true; return true;
} }
@ -1367,16 +1353,15 @@ size_t ModelManager::compute_backend_resident_bytes(ggml_backend_t compute_backe
} }
size_t total_size = 0; size_t total_size = 0;
std::unordered_set<ggml_backend_buffer_t> seen; auto add_buffer = [&](ggml_backend_buffer_t buffer) {
auto add_buffer = [&](ggml_backend_buffer_t buffer) { if (buffer == nullptr || ggml_backend_buffer_is_host(buffer)) {
if (buffer == nullptr || ggml_backend_buffer_is_host(buffer) || !seen.insert(buffer).second) {
return; return;
} }
ggml_backend_buffer_type_t buffer_type = ggml_backend_buffer_get_type(buffer); ggml_backend_buffer_type_t buffer_type = ggml_backend_buffer_get_type(buffer);
auto split_devices = split_buffer_devices_.find(buffer_type); auto split_devices = split_buffer_devices_.find(buffer_type);
const bool on_device = split_devices == split_buffer_devices_.end() const bool on_device = split_devices == split_buffer_devices_.end()
? buffer_type != nullptr && ggml_backend_buft_get_device(buffer_type) == compute_device ? buffer_type != nullptr && ggml_backend_buft_get_device(buffer_type) == compute_device
: std::any_of(split_devices->second.begin(), split_devices->second.end(), [&](const auto& entry) { : std::any_of(split_devices->second.begin(), split_devices->second.end(), [&](const auto& entry) {
return ggml_backend_get_device(entry.first) == compute_device; return ggml_backend_get_device(entry.first) == compute_device;
}); });
if (!on_device) { if (!on_device) {
@ -1386,16 +1371,9 @@ size_t ModelManager::compute_backend_resident_bytes(ggml_backend_t compute_backe
total_size = buffer_size > SIZE_MAX - total_size ? SIZE_MAX : total_size + buffer_size; total_size = buffer_size > SIZE_MAX - total_size ? SIZE_MAX : total_size + buffer_size;
}; };
// The loader may retain device mappings after their parameter blocks are released.
for (ggml_backend_buffer_t buffer : model_loader_.get_device_mmap_buffers()) {
add_buffer(buffer);
}
for (const auto& block : params_storage_blocks_) { for (const auto& block : params_storage_blocks_) {
if (block != nullptr) { if (block != nullptr) {
add_buffer(block->buffer); add_buffer(block->buffer);
for (const auto& store : block->mmap_tensor_stores) {
add_buffer(store.mmbuffer.get());
}
} }
} }
for (const auto& block : compute_staging_blocks_) { for (const auto& block : compute_staging_blocks_) {

View File

@ -7,7 +7,6 @@
#include <list> #include <list>
#include <mutex> #include <mutex>
#include <set> #include <set>
#include <tuple>
#include <type_traits> #include <type_traits>
#include <unordered_set> #include <unordered_set>
#include <utility> #include <utility>
@ -30,7 +29,6 @@
#include "stable-diffusion.h" #include "stable-diffusion.h"
#include "conditioning/conditioner.hpp" #include "conditioning/conditioner.hpp"
#include "conditioning/conditioning_cache.h"
#include "core/backend_fit.h" #include "core/backend_fit.h"
#include "extensions/generation_extension.h" #include "extensions/generation_extension.h"
#include "model/adapter/ip_adapter.hpp" #include "model/adapter/ip_adapter.hpp"
@ -137,7 +135,6 @@ static_assert(std::atomic<sd_cancel_mode_t>::is_always_lock_free,
StableDiffusionGGML::StableDiffusionGGML() StableDiffusionGGML::StableDiffusionGGML()
: rng(std::make_shared<PhiloxRNG>()), : rng(std::make_shared<PhiloxRNG>()),
conditioning_cache_(std::make_unique<ConditioningCache>()),
denoiser(std::make_shared<CompVisDenoiser>()) {} denoiser(std::make_shared<CompVisDenoiser>()) {}
StableDiffusionGGML::~StableDiffusionGGML() = default; StableDiffusionGGML::~StableDiffusionGGML() = default;
@ -207,8 +204,6 @@ void StableDiffusionGGML::end_runners() {
} }
bool StableDiffusionGGML::reset_runners(const RunnerGroups& groups) { bool StableDiffusionGGML::reset_runners(const RunnerGroups& groups) {
conditioning_cache_->clear();
conditioning_loras_.clear();
end_runners(); end_runners();
clear_lora_adapters(); clear_lora_adapters();
runtime_lora_models.clear(); runtime_lora_models.clear();
@ -920,11 +915,6 @@ bool StableDiffusionGGML::init(const sd_ctx_params_t* sd_ctx_params) {
return false; return false;
} }
} }
if (sd_ctx_params->conditioning_cache_size < 0) {
LOG_ERROR("conditioning_cache_size must be non-negative");
return false;
}
conditioning_cache_->set_capacity(static_cast<size_t>(sd_ctx_params->conditioning_cache_size));
auto configuration = std::make_unique<ModelConfig>(*sd_ctx_params); auto configuration = std::make_unique<ModelConfig>(*sd_ctx_params);
n_threads = sd_ctx_params->n_threads; n_threads = sd_ctx_params->n_threads;
tensor_executor = std::make_unique<sd::ParallelExecutor>(n_threads > 0 ? n_threads : sd_get_num_physical_cores()); tensor_executor = std::make_unique<sd::ParallelExecutor>(n_threads > 0 ? n_threads : sd_get_num_physical_cores());
@ -1772,22 +1762,13 @@ bool StableDiffusionGGML::apply_loras(const sd_lora_t* loras, uint32_t lora_coun
extension->collect_loras(all_loras); extension->collect_loras(all_loras);
} }
conditioning_cache_allowed_ = all_loras.empty();
int64_t t0 = ggml_time_ms(); int64_t t0 = ggml_time_ms();
end_runners(); end_runners();
clear_lora_adapters(); clear_lora_adapters();
if (!model_manager->prepare_lora_sources(all_loras)) { if (!model_manager->prepare_lora_sources(all_loras))
conditioning_cache_->clear();
return false; return false;
}
if (!std::equal(all_loras.begin(), all_loras.end(),
conditioning_loras_.begin(), conditioning_loras_.end(),
[](const ModelManager::LoraSpec& a, const ModelManager::LoraSpec& b) {
return a.file_id == b.file_id && a.file_revision == b.file_revision &&
a.multiplier == b.multiplier && a.is_high_noise == b.is_high_noise &&
a.tensor_name_prefix_filter == b.tensor_name_prefix_filter;
})) {
conditioning_cache_->clear();
}
runtime_lora_models.erase(std::remove_if(runtime_lora_models.begin(), runtime_lora_models.end(), [&](const RuntimeLora& entry) { runtime_lora_models.erase(std::remove_if(runtime_lora_models.begin(), runtime_lora_models.end(), [&](const RuntimeLora& entry) {
return std::none_of(all_loras.begin(), all_loras.end(), [&](const ModelManager::LoraSpec& spec) { return std::none_of(all_loras.begin(), all_loras.end(), [&](const ModelManager::LoraSpec& spec) {
return entry.matches(spec); return entry.matches(spec);
@ -1797,7 +1778,6 @@ bool StableDiffusionGGML::apply_loras(const sd_lora_t* loras, uint32_t lora_coun
const bool success = apply_lora_immediately ? apply_loras_immediately(all_loras) const bool success = apply_lora_immediately ? apply_loras_immediately(all_loras)
: apply_loras_at_runtime(all_loras); : apply_loras_at_runtime(all_loras);
if (!success) { if (!success) {
conditioning_cache_->clear();
clear_lora_adapters(); clear_lora_adapters();
runtime_lora_models.clear(); runtime_lora_models.clear();
return false; return false;
@ -1807,14 +1787,9 @@ bool StableDiffusionGGML::apply_loras(const sd_lora_t* loras, uint32_t lora_coun
if (!all_loras.empty()) { if (!all_loras.empty()) {
LOG_INFO("apply_loras completed, taking %.2fs", (t1 - t0) * 1.0f / 1000); LOG_INFO("apply_loras completed, taking %.2fs", (t1 - t0) * 1.0f / 1000);
} }
conditioning_loras_ = std::move(all_loras);
return true; return true;
} }
SDCondition StableDiffusionGGML::get_learned_condition(const ConditionerParams& params) {
return conditioning_cache_->get(*cond_stage_model, n_threads, params);
}
void StableDiffusionGGML::reset_generation_extensions() { void StableDiffusionGGML::reset_generation_extensions() {
for (auto& extension : generation_extensions) { for (auto& extension : generation_extensions) {
extension->reset_runtime_condition(); extension->reset_runtime_condition();
@ -1999,8 +1974,6 @@ void StableDiffusionGGML::preview_image(int step,
int patch_sz = 1; int patch_sz = 1;
const float(*latent_rgb_proj)[3] = nullptr; const float(*latent_rgb_proj)[3] = nullptr;
float* latent_rgb_bias = nullptr; float* latent_rgb_bias = nullptr;
const float* latent_alpha_proj = nullptr;
float latent_alpha_bias = 1.f;
if (channels == 128) { if (channels == 128) {
if (sd_version_uses_flux2_vae(version)) { if (sd_version_uses_flux2_vae(version)) {
@ -2014,16 +1987,6 @@ void StableDiffusionGGML::preview_image(int step,
LOG_WARN("No latent to RGB projection known for this model"); LOG_WARN("No latent to RGB projection known for this model");
return; return;
} }
} else if (channels == 64) {
if (version == VERSION_QWEN_IMAGE_2_1) {
latent_rgb_proj = qwen21_latent_rgb_proj;
latent_rgb_bias = qwen21_latent_rgb_bias;
latent_alpha_proj = qwen21_latent_alpha_proj;
latent_alpha_bias = qwen21_latent_alpha_bias;
} else {
LOG_WARN("No latent to RGB projection known for this model");
return;
}
} else if (channels == 48) { } else if (channels == 48) {
if (sd_version_is_wan(version)) { if (sd_version_is_wan(version)) {
latent_rgb_proj = wan_22_latent_rgb_proj; latent_rgb_proj = wan_22_latent_rgb_proj;
@ -2074,14 +2037,13 @@ void StableDiffusionGGML::preview_image(int step,
uint32_t img_width = static_cast<uint32_t>(_latents.shape()[0]) * patch_sz; uint32_t img_width = static_cast<uint32_t>(_latents.shape()[0]) * patch_sz;
uint32_t img_height = static_cast<uint32_t>(_latents.shape()[1]) * patch_sz; uint32_t img_height = static_cast<uint32_t>(_latents.shape()[1]) * patch_sz;
uint32_t img_channels = latent_alpha_proj != nullptr ? 4 : 3; uint8_t* data = (uint8_t*)malloc(frames * img_width * img_height * 3 * sizeof(uint8_t));
uint8_t* data = (uint8_t*)malloc(frames * img_width * img_height * img_channels * sizeof(uint8_t));
GGML_ASSERT(data != nullptr); GGML_ASSERT(data != nullptr);
preview_latent_video(data, _latents, latent_rgb_proj, latent_rgb_bias, patch_sz, latent_alpha_proj, latent_alpha_bias); preview_latent_video(data, _latents, latent_rgb_proj, latent_rgb_bias, patch_sz);
sd_image_t* images = (sd_image_t*)malloc(frames * sizeof(sd_image_t)); sd_image_t* images = (sd_image_t*)malloc(frames * sizeof(sd_image_t));
GGML_ASSERT(images != nullptr); GGML_ASSERT(images != nullptr);
for (uint32_t i = 0; i < frames; i++) { for (uint32_t i = 0; i < frames; i++) {
images[i] = {img_width, img_height, img_channels, data + i * img_width * img_height * img_channels}; images[i] = {img_width, img_height, 3, data + i * img_width * img_height * 3};
} }
step_callback(step, frames, images, is_noisy, step_callback_data); step_callback(step, frames, images, is_noisy, step_callback_data);
free(data); free(data);
@ -2256,15 +2218,6 @@ sd::Tensor<float> StableDiffusionGGML::sample(const std::shared_ptr<DiffusionMod
}; };
RunnerEndOnExit sample_diffusion_runner_end{work_diffusion_model.get()}; RunnerEndOnExit sample_diffusion_runner_end{work_diffusion_model.get()};
// These inputs are immutable for this sampling run. Extensions may replace or
// modify them per step, so those paths need an explicit stability contract first.
const bool cache_qwen_prefix = version == VERSION_QWEN_IMAGE_2_1 &&
std::none_of(generation_extensions.begin(), generation_extensions.end(),
[](const auto& extension) { return extension->is_enabled(); });
using QwenPrefixInputs = std::tuple<const sd::Tensor<float>*, const sd::Tensor<int32_t>*,
const std::vector<sd::Tensor<float>>*>;
std::vector<QwenPrefixInputs> qwen_prefix_inputs;
RunnerEndOnExit sample_control_runner_end{!control_image.empty() && control_net != nullptr ? control_net.get() : nullptr}; RunnerEndOnExit sample_control_runner_end{!control_image.empty() && control_net != nullptr ? control_net.get() : nullptr};
const bool apply_denoise_mask = !denoise_mask.empty() && const bool apply_denoise_mask = !denoise_mask.empty() &&
@ -2534,18 +2487,6 @@ sd::Tensor<float> StableDiffusionGGML::sample(const std::shared_ptr<DiffusionMod
extension->before_diffusion(diffusion_params, step); extension->before_diffusion(diffusion_params, step);
} }
if (cache_qwen_prefix) {
auto* extra = std::get_if<QwenImage21DiffusionExtra>(&diffusion_params.extra);
if (extra != nullptr) {
auto key = std::make_tuple(diffusion_params.context, extra->image_slots,
diffusion_params.ref_image_params.pass_to_dit ? diffusion_params.ref_latents : nullptr);
auto entry = std::find(qwen_prefix_inputs.begin(), qwen_prefix_inputs.end(), key);
extra->prefix_id = static_cast<uint64_t>(entry - qwen_prefix_inputs.begin()) + 1;
if (entry == qwen_prefix_inputs.end()) {
qwen_prefix_inputs.push_back(key);
}
}
}
auto output_opt = work_diffusion_model->compute(n_threads, diffusion_params); auto output_opt = work_diffusion_model->compute(n_threads, diffusion_params);
if (output_opt.empty()) { if (output_opt.empty()) {
LOG_ERROR("diffusion model compute failed"); LOG_ERROR("diffusion model compute failed");

View File

@ -26,7 +26,6 @@ class RNG;
struct Denoiser; struct Denoiser;
struct LoraModel; struct LoraModel;
struct ConditionerParams; struct ConditionerParams;
class ConditioningCache;
struct SDCondition; struct SDCondition;
struct RefImageParams; struct RefImageParams;
namespace Wav2Vec2 { namespace Wav2Vec2 {
@ -179,8 +178,7 @@ public:
std::recursive_mutex execution_mutex; std::recursive_mutex execution_mutex;
std::unique_ptr<ModelConfig> config_; std::unique_ptr<ModelConfig> config_;
RunnerState runner_state_; RunnerState runner_state_;
std::unique_ptr<ConditioningCache> conditioning_cache_; bool conditioning_cache_allowed_ = false;
std::vector<ModelManager::LoraSpec> conditioning_loras_;
bool executing_ = false; bool executing_ = false;
std::shared_ptr<Denoiser> denoiser; std::shared_ptr<Denoiser> denoiser;
@ -364,8 +362,6 @@ public:
bool apply_loras(const sd_lora_t* loras, uint32_t lora_count); bool apply_loras(const sd_lora_t* loras, uint32_t lora_count);
SDCondition get_learned_condition(const ConditionerParams& params);
void reset_generation_extensions(); void reset_generation_extensions();
void prepare_generation_extensions(const sd_pm_params_t& pm_params, void prepare_generation_extensions(const sd_pm_params_t& pm_params,

View File

@ -441,7 +441,8 @@ namespace sd::pipeline {
sd->compute_ip_adapter_tokens(sd_img_gen_params->ip_adapter_image, sd_img_gen_params->ip_adapter_strength); sd->compute_ip_adapter_tokens(sd_img_gen_params->ip_adapter_image, sd_img_gen_params->ip_adapter_strength);
int64_t prepare_start_ms = ggml_time_ms(); int64_t prepare_start_ms = ggml_time_ms();
condition_params.zero_out_masked = false; condition_params.zero_out_masked = false;
auto cond = sd->get_learned_condition(condition_params); auto cond = sd->cond_stage_model->get_learned_condition(sd->n_threads,
condition_params);
if (cond.empty()) { if (cond.empty()) {
LOG_ERROR("failed to encode prompt"); LOG_ERROR("failed to encode prompt");
return std::nullopt; return std::nullopt;
@ -479,7 +480,8 @@ namespace sd::pipeline {
// LLaDA-Image CFG keeps the source latent but drops its SigVQ features. // LLaDA-Image CFG keeps the source latent but drops its SigVQ features.
condition_params.ref_images = nullptr; condition_params.ref_images = nullptr;
} }
uncond = sd->get_learned_condition(condition_params); uncond = sd->cond_stage_model->get_learned_condition(sd->n_threads,
condition_params);
if (uncond.empty()) { if (uncond.empty()) {
LOG_ERROR("failed to encode negative prompt"); LOG_ERROR("failed to encode negative prompt");
return std::nullopt; return std::nullopt;
@ -507,7 +509,8 @@ namespace sd::pipeline {
if (use_ref_latent_img_cfg) { if (use_ref_latent_img_cfg) {
condition_params.ref_images = &empty_ref_images; condition_params.ref_images = &empty_ref_images;
} }
img_uncond = sd->get_learned_condition(condition_params); img_uncond = sd->cond_stage_model->get_learned_condition(sd->n_threads,
condition_params);
if (img_uncond.empty()) { if (img_uncond.empty()) {
LOG_ERROR("failed to encode image guidance prompt"); LOG_ERROR("failed to encode image guidance prompt");
return std::nullopt; return std::nullopt;

View File

@ -291,8 +291,7 @@ namespace sd::model_builders {
result.diffusion = std::make_shared<Qwen::QwenImage21Runner>(ctx.backends.runtime_backend(SDBackendModule::DIFFUSION), result.diffusion = std::make_shared<Qwen::QwenImage21Runner>(ctx.backends.runtime_backend(SDBackendModule::DIFFUSION),
tensor_storage_map, tensor_storage_map,
"model.diffusion_model", "model.diffusion_model",
weight_manager, weight_manager);
sd_ctx_params->model_args);
} else { } else {
result.diffusion = std::make_shared<Qwen::QwenImageRunner>(ctx.backends.runtime_backend(SDBackendModule::DIFFUSION), result.diffusion = std::make_shared<Qwen::QwenImageRunner>(ctx.backends.runtime_backend(SDBackendModule::DIFFUSION),
tensor_storage_map, tensor_storage_map,

View File

@ -1157,12 +1157,17 @@ namespace sd::pipeline {
condition_params.zero_out_masked = true; condition_params.zero_out_masked = true;
condition_params.ref_images = &latents.ref_images; condition_params.ref_images = &latents.ref_images;
condition_params.minimax_h3_references = &latents.minimax_presentation_refs; condition_params.minimax_h3_references = &latents.minimax_presentation_refs;
condition_params.allow_cache =
sd_version_is_minimax_h3(sd->version) &&
sd->conditioning_cache_allowed_ &&
!request.use_uncond;
if (sd_version_is_lingbot_video(sd->version) || sd_version_is_minimax_h3(sd->version)) { if (sd_version_is_lingbot_video(sd->version) || sd_version_is_minimax_h3(sd->version)) {
condition_params.ref_image_params.vlm_resize_mode = RefImageResizeMode::AREA; condition_params.ref_image_params.vlm_resize_mode = RefImageResizeMode::AREA;
} }
int64_t prepare_start_ms = ggml_time_ms(); int64_t prepare_start_ms = ggml_time_ms();
embeds.cond = sd->get_learned_condition(condition_params); embeds.cond = sd->cond_stage_model->get_learned_condition(sd->n_threads,
condition_params);
if (embeds.cond.empty()) { if (embeds.cond.empty()) {
LOG_ERROR("failed to encode video prompt"); LOG_ERROR("failed to encode video prompt");
return std::nullopt; return std::nullopt;
@ -1187,7 +1192,8 @@ namespace sd::pipeline {
} }
if (request.use_uncond) { if (request.use_uncond) {
condition_params.text = request.negative_prompt; condition_params.text = request.negative_prompt;
embeds.uncond = sd->get_learned_condition(condition_params); embeds.uncond = sd->cond_stage_model->get_learned_condition(sd->n_threads,
condition_params);
if (embeds.uncond.empty()) { if (embeds.uncond.empty()) {
LOG_ERROR("failed to encode negative video prompt"); LOG_ERROR("failed to encode negative video prompt");
return std::nullopt; return std::nullopt;

View File

@ -4,86 +4,6 @@
#include "core/tensor.hpp" #include "core/tensor.hpp"
#include "ggml.h" #include "ggml.h"
// RGB is projected to [-1, 1]; alpha is projected directly to [0, 1].
const float qwen21_latent_rgb_proj[64][3] = {
{0.00860495522f, 0.01219501462f, -0.00321337196f},
{0.01889233090f, 0.01246581216f, 0.01074959482f},
{0.1255941446f, 0.1176879344f, -0.0332212352f},
{0.0418238528f, 0.1043427754f, 0.0121666316f},
{0.02025338f, 0.01453670296f, -0.000224336044f},
{-0.01896720702f, -0.0206099030f, -0.0322728584f},
{0.00438984796f, -0.01374969766f, 0.02849196f},
{-0.0374495856f, -0.0286777126f, -0.0693192810f},
{0.01511914734f, 0.0242979386f, 0.0553878870f},
{-0.1138629518f, -0.020391466f, 0.001550520522f},
{-0.0233650696f, -0.0417292018f, -0.0362361182f},
{-0.0351603342f, -0.0243595924f, -0.00216261038f},
{0.01093355288f, -0.0373466924f, 0.00241315350f},
{0.01778704744f, -0.00401984678f, -0.0343259192f},
{0.0486059334f, 0.0253144f, 0.0672564966f},
{0.0309463558f, 0.0277963166f, 0.0520869622f},
{0.0374485008f, 0.0551753676f, 0.0225853902f},
{-0.0090809962f, -0.004756176f, 0.00636443612f},
{-0.0270455652f, -0.0384966954f, -0.00905908082f},
{-0.00553493756f, 0.01484553684f, -0.0211502468f},
{0.01319502562f, 0.00948005666f, 0.0483789212f},
{-0.00931847104f, -0.00276452734f, -0.01011985302f},
{0.0180478258f, 0.01614954356f, -0.0209424690f},
{-0.0214530434f, -0.00272961176f, 0.0217887476f},
{-0.0636772304f, -0.0208893548f, 0.0479167742f},
{-0.0250321236f, -0.0286715676f, 0.0530110146f},
{-0.01853078078f, 0.01647272818f, -0.00207747588f},
{0.0023101082f, 0.01228800748f, 0.01303505006f},
{-0.01243671408f, -0.0258638728f, -0.0379116264f},
{0.00598934710f, 0.00642563550f, -0.01234514304f},
{-0.0296733996f, -0.0234698050f, 0.00060018212f},
{-0.0322019498f, -0.0529200462f, -0.00344987414f},
{-0.00205026458f, -0.00846599446f, 0.00455971038f},
{-0.01082227064f, 0.0315661948f, -0.0677753362f},
{0.0645553474f, 0.1109666998f, 0.0674744864f},
{0.01036801108f, -0.00484841210f, -0.001529168474f},
{0.01264353566f, 0.01548126338f, -0.00966374324f},
{-0.0223892408f, -0.00871751526f, -0.000306421670f},
{0.0271322742f, 0.03496524f, -0.0089692858f},
{0.0512178672f, 0.0173080034f, 0.00804227746f},
{0.01210987192f, 0.00758025926f, -0.00281712586f},
{0.1897278390f, 0.1210261828f, 0.062603892f},
{0.0208058822f, 0.00547548182f, 0.01262955638f},
{0.00813332858f, 0.01015930914f, 0.01301771290f},
{-0.000927236014f, -0.00152540594f, -0.00599213302f},
{0.01663314616f, -0.00582789626f, 0.0163958132f},
{-0.0252546342f, -0.0604193732f, -0.1606919922f},
{-0.091722686f, -0.0409201224f, -0.0959576198f},
{0.0282963112f, -0.01387223872f, -0.01648814464f},
{0.0552316818f, 0.0967547788f, 0.0413586632f},
{0.00922849292f, 0.00451467542f, -0.0529172378f},
{0.0558600768f, 0.0122988308f, -0.01445942422f},
{0.000210660902f, -0.01295958782f, -0.01804761764f},
{0.0358136250f, -0.0472505970f, -0.1156405142f},
{-0.0506390696f, -0.0471914842f, 0.0349791468f},
{-0.0480143168f, 0.00628389868f, -0.0545163826f},
{0.0315499582f, 0.0564846606f, -0.0430850488f},
{-0.0362330316f, -0.01267788554f, 0.0061024772f},
{0.0038627542f, 0.00911055916f, -0.00758526008f},
{-0.0447103298f, -0.00835411408f, 0.01545872328f},
{-0.015006738f, 0.00270612302f, -0.00784361356f},
{-0.0221755048f, -0.0513344748f, -0.0475317424f},
{-0.01036656294f, -0.00422146068f, -0.0213499052f},
{0.01788952706f, 0.01191944190f, 0.0397205238f},
};
float qwen21_latent_rgb_bias[3] = {-0.043293118f, -0.02695978f, -0.11986706f};
const float qwen21_latent_alpha_proj[64] = {
-0.0416241114f, -0.00678954612f, -0.0169095515f, -0.0230551401f, 0.0100882595f, 0.00655586802f, 0.0401166874f, -0.0055510216f,
0.0224234441f, -0.0389640963f, -0.0114492163f, -0.00721128977f, -0.0029064082f, 0.0150300547f, -0.00321615308f, -0.0498856338f,
-0.0215251401f, 0.0240220482f, 0.0117338008f, -0.0460420624f, 0.0387872889f, 0.0131517207f, 0.0147100836f, 0.0266985286f,
0.0153097324f, -0.0418119757f, 0.0421013917f, 0.0401724499f, 0.00972515915f, 0.011718495f, 0.0117622291f, 0.0136505134f,
-0.0350017363f, -0.0100692606f, -0.0131582529f, -0.00660639315f, 0.00253611396f, -0.0195736368f, -0.04240184f, 0.0321299262f,
0.0106089414f, -0.0179845306f, -0.00806212708f, 0.0135889057f, -0.0157393098f, -0.0267791344f, 0.0109068534f, 0.0283931966f,
-0.0435370078f, 0.00187883536f, -0.0108995378f, -0.0450757676f, -0.0699481501f, 0.0123562106f, -0.0222592249f, 0.0216155907f,
0.0563755424f, -0.0073379912f, 0.0160012921f, 0.0411637742f, 0.0189607258f, -0.024025029f, -0.0161487905f, -0.016913203f};
const float qwen21_latent_alpha_bias = 0.871268134f;
const float minimax_latent_rgb_proj[24][3] = { const float minimax_latent_rgb_proj[24][3] = {
{0.19819857f, 0.11584999f, 0.07929777f}, {0.19819857f, 0.11584999f, 0.07929777f},
{-0.16047224f, -0.10601170f, -0.15996324f}, {-0.16047224f, -0.10601170f, -0.15996324f},
@ -404,7 +324,7 @@ const float sd_latent_rgb_proj[4][3] = {
{-0.178022f, -0.200862f, -0.678514f}}; {-0.178022f, -0.200862f, -0.678514f}};
float sd_latent_rgb_bias[3] = {-0.017478f, -0.055834f, -0.105825f}; float sd_latent_rgb_bias[3] = {-0.017478f, -0.055834f, -0.105825f};
void preview_latent_video(uint8_t* buffer, ggml_tensor* latents, const float (*latent_rgb_proj)[3], const float latent_rgb_bias[3], int patch_size, const float* latent_alpha_proj = nullptr, float latent_alpha_bias = 1.f) { void preview_latent_video(uint8_t* buffer, ggml_tensor* latents, const float (*latent_rgb_proj)[3], const float latent_rgb_bias[3], int patch_size) {
size_t buffer_head = 0; size_t buffer_head = 0;
uint32_t latent_width = static_cast<uint32_t>(latents->ne[0]); uint32_t latent_width = static_cast<uint32_t>(latents->ne[0]);
@ -418,8 +338,7 @@ void preview_latent_video(uint8_t* buffer, ggml_tensor* latents, const float (*l
uint32_t rgb_width = latent_width * patch_size; uint32_t rgb_width = latent_width * patch_size;
uint32_t rgb_height = latent_height * patch_size; uint32_t rgb_height = latent_height * patch_size;
uint32_t unpatched_dim = dim / (patch_size * patch_size); uint32_t unpatched_dim = dim / (patch_size * patch_size);
const uint32_t output_channels = latent_alpha_proj != nullptr ? 4 : 3;
for (uint32_t k = 0; k < frames; k++) { for (uint32_t k = 0; k < frames; k++) {
for (uint32_t rgb_x = 0; rgb_x < rgb_width; rgb_x++) { for (uint32_t rgb_x = 0; rgb_x < rgb_width; rgb_x++) {
@ -437,16 +356,13 @@ void preview_latent_video(uint8_t* buffer, ggml_tensor* latents, const float (*l
// should be incremented by 1 for each pixel // should be incremented by 1 for each pixel
size_t pixel_id = k * rgb_width * rgb_height + rgb_y * rgb_width + rgb_x; size_t pixel_id = k * rgb_width * rgb_height + rgb_y * rgb_width + rgb_x;
float r = 0, g = 0, b = 0, a = 0; float r = 0, g = 0, b = 0;
if (latent_rgb_proj != nullptr) { if (latent_rgb_proj != nullptr) {
for (uint32_t d = 0; d < unpatched_dim; d++) { for (uint32_t d = 0; d < unpatched_dim; d++) {
float value = *(float*)((char*)latents->data + latent_id + (d * patch_size * patch_size + channel_offset) * latents->nb[ggml_n_dims(latents) - 1]); float value = *(float*)((char*)latents->data + latent_id + (d * patch_size * patch_size + channel_offset) * latents->nb[ggml_n_dims(latents) - 1]);
r += value * latent_rgb_proj[d][0]; r += value * latent_rgb_proj[d][0];
g += value * latent_rgb_proj[d][1]; g += value * latent_rgb_proj[d][1];
b += value * latent_rgb_proj[d][2]; b += value * latent_rgb_proj[d][2];
if (latent_alpha_proj != nullptr) {
a += value * latent_alpha_proj[d];
}
} }
} else { } else {
// interpret first 3 channels as RGB // interpret first 3 channels as RGB
@ -470,13 +386,9 @@ void preview_latent_video(uint8_t* buffer, ggml_tensor* latents, const float (*l
g = g >= 0 ? g <= 1 ? g : 1 : 0; g = g >= 0 ? g <= 1 ? g : 1 : 0;
b = b >= 0 ? b <= 1 ? b : 1 : 0; b = b >= 0 ? b <= 1 ? b : 1 : 0;
buffer[pixel_id * output_channels + 0] = (uint8_t)(r * 255); buffer[pixel_id * 3 + 0] = (uint8_t)(r * 255);
buffer[pixel_id * output_channels + 1] = (uint8_t)(g * 255); buffer[pixel_id * 3 + 1] = (uint8_t)(g * 255);
buffer[pixel_id * output_channels + 2] = (uint8_t)(b * 255); buffer[pixel_id * 3 + 2] = (uint8_t)(b * 255);
if (latent_alpha_proj != nullptr) {
a = std::min(1.0f, std::max(0.0f, a + latent_alpha_bias));
buffer[pixel_id * output_channels + 3] = (uint8_t)(a * 255);
}
} }
} }
} }
@ -486,17 +398,16 @@ static inline bool preview_latent_tensor_is_video(const sd::Tensor<float>& laten
return latents.dim() == 5; return latents.dim() == 5;
} }
void preview_latent_video(uint8_t* buffer, const sd::Tensor<float>& latents, const float (*latent_rgb_proj)[3], const float latent_rgb_bias[3], int patch_size, const float* latent_alpha_proj = nullptr, float latent_alpha_bias = 1.f) { void preview_latent_video(uint8_t* buffer, const sd::Tensor<float>& latents, const float (*latent_rgb_proj)[3], const float latent_rgb_bias[3], int patch_size) {
uint32_t latent_width = static_cast<uint32_t>(latents.shape()[0]); uint32_t latent_width = static_cast<uint32_t>(latents.shape()[0]);
uint32_t latent_height = static_cast<uint32_t>(latents.shape()[1]); uint32_t latent_height = static_cast<uint32_t>(latents.shape()[1]);
bool is_video = preview_latent_tensor_is_video(latents); bool is_video = preview_latent_tensor_is_video(latents);
uint32_t frames = is_video ? static_cast<uint32_t>(latents.shape()[2]) : 1; uint32_t frames = is_video ? static_cast<uint32_t>(latents.shape()[2]) : 1;
uint32_t dim = is_video ? static_cast<uint32_t>(latents.shape()[3]) : static_cast<uint32_t>(latents.shape()[2]); uint32_t dim = is_video ? static_cast<uint32_t>(latents.shape()[3]) : static_cast<uint32_t>(latents.shape()[2]);
uint32_t rgb_width = latent_width * patch_size; uint32_t rgb_width = latent_width * patch_size;
uint32_t rgb_height = latent_height * patch_size; uint32_t rgb_height = latent_height * patch_size;
uint32_t unpatched_dim = dim / (patch_size * patch_size); uint32_t unpatched_dim = dim / (patch_size * patch_size);
const uint32_t output_channels = latent_alpha_proj != nullptr ? 4 : 3;
for (uint32_t k = 0; k < frames; k++) { for (uint32_t k = 0; k < frames; k++) {
for (uint32_t rgb_x = 0; rgb_x < rgb_width; rgb_x++) { for (uint32_t rgb_x = 0; rgb_x < rgb_width; rgb_x++) {
@ -516,7 +427,7 @@ void preview_latent_video(uint8_t* buffer, const sd::Tensor<float>& latents, con
: latents.values()[latent_x + latent_width * (latent_y + latent_height * latent_channel)]; : latents.values()[latent_x + latent_width * (latent_y + latent_height * latent_channel)];
}; };
float r = 0.f, g = 0.f, b = 0.f, a = 0.f; float r = 0.f, g = 0.f, b = 0.f;
if (latent_rgb_proj != nullptr) { if (latent_rgb_proj != nullptr) {
for (uint32_t d = 0; d < unpatched_dim; d++) { for (uint32_t d = 0; d < unpatched_dim; d++) {
uint32_t latent_channel = d * patch_size * patch_size + channel_offset; uint32_t latent_channel = d * patch_size * patch_size + channel_offset;
@ -524,9 +435,6 @@ void preview_latent_video(uint8_t* buffer, const sd::Tensor<float>& latents, con
r += value * latent_rgb_proj[d][0]; r += value * latent_rgb_proj[d][0];
g += value * latent_rgb_proj[d][1]; g += value * latent_rgb_proj[d][1];
b += value * latent_rgb_proj[d][2]; b += value * latent_rgb_proj[d][2];
if (latent_alpha_proj != nullptr) {
a += value * latent_alpha_proj[d];
}
} }
} else { } else {
r = latent_value(0); r = latent_value(0);
@ -542,13 +450,9 @@ void preview_latent_video(uint8_t* buffer, const sd::Tensor<float>& latents, con
g = std::min(1.0f, std::max(0.0f, g * .5f + .5f)); g = std::min(1.0f, std::max(0.0f, g * .5f + .5f));
b = std::min(1.0f, std::max(0.0f, b * .5f + .5f)); b = std::min(1.0f, std::max(0.0f, b * .5f + .5f));
buffer[pixel_id * output_channels + 0] = (uint8_t)(r * 255); buffer[pixel_id * 3 + 0] = (uint8_t)(r * 255);
buffer[pixel_id * output_channels + 1] = (uint8_t)(g * 255); buffer[pixel_id * 3 + 1] = (uint8_t)(g * 255);
buffer[pixel_id * output_channels + 2] = (uint8_t)(b * 255); buffer[pixel_id * 3 + 2] = (uint8_t)(b * 255);
if (latent_alpha_proj != nullptr) {
a = std::min(1.0f, std::max(0.0f, a + latent_alpha_bias));
buffer[pixel_id * output_channels + 3] = (uint8_t)(a * 255);
}
} }
} }
} }

View File

@ -326,7 +326,6 @@ void sd_hires_params_init(sd_hires_params_t* hires_params) {
void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params) { void sd_ctx_params_init(sd_ctx_params_t* sd_ctx_params) {
*sd_ctx_params = {}; *sd_ctx_params = {};
sd_ctx_params->n_threads = sd_get_num_physical_cores(); sd_ctx_params->n_threads = sd_get_num_physical_cores();
sd_ctx_params->conditioning_cache_size = 4;
sd_ctx_params->wtype = SD_TYPE_COUNT; sd_ctx_params->wtype = SD_TYPE_COUNT;
sd_ctx_params->rng_type = CUDA_RNG; sd_ctx_params->rng_type = CUDA_RNG;
sd_ctx_params->sampler_rng_type = RNG_TYPE_COUNT; sd_ctx_params->sampler_rng_type = RNG_TYPE_COUNT;
@ -379,7 +378,6 @@ char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params) {
"pulid_weights_path: %s\n" "pulid_weights_path: %s\n"
"tensor_type_rules: %s\n" "tensor_type_rules: %s\n"
"n_threads: %d\n" "n_threads: %d\n"
"conditioning_cache_size: %d\n"
"wtype: %s\n" "wtype: %s\n"
"rng_type: %s\n" "rng_type: %s\n"
"sampler_rng_type: %s\n" "sampler_rng_type: %s\n"
@ -420,7 +418,6 @@ char* sd_ctx_params_to_str(const sd_ctx_params_t* sd_ctx_params) {
SAFE_STR(sd_ctx_params->pulid_weights_path), SAFE_STR(sd_ctx_params->pulid_weights_path),
SAFE_STR(sd_ctx_params->tensor_type_rules), SAFE_STR(sd_ctx_params->tensor_type_rules),
sd_ctx_params->n_threads, sd_ctx_params->n_threads,
sd_ctx_params->conditioning_cache_size,
sd_type_name(sd_ctx_params->wtype), sd_type_name(sd_ctx_params->wtype),
sd_rng_type_name(sd_ctx_params->rng_type), sd_rng_type_name(sd_ctx_params->rng_type),
sd_rng_type_name(sd_ctx_params->sampler_rng_type), sd_rng_type_name(sd_ctx_params->sampler_rng_type),