Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/development/pe_cache.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Position embedding cache

`sd::PECache` stores CPU-side `sd::Tensor<float>` data. Each model runner owns
its cache and calls the existing `make_input()` on every graph build. Cache
hits skip PE generation; allocation and upload remain ordinary graph-input
operations. `GGMLRunner` has no PE-specific code or resource hooks.
its cache. A cache hit skips PE generation, and `make_input()` submits the CPU
data through ordinary graph-input allocation and upload. `GGMLRunner` has no
PE-specific code or resource hooks.

The cache keeps the most recent key and tensor for each named PE input. A
matching key returns the existing tensor by reference. Changing its parameters
Expand All @@ -13,6 +13,12 @@ there is no LRU, device-buffer cache, memory budget, or graph lifecycle callback
CPU data lives with the model runner, including across `runner_end()` calls,
and is released when its named entry is replaced or the cache is destroyed.

On CUDA, HIP, and MUSA, LTXAV retains large video RoPE tensors in the existing
device runner cache during sampling. It stores each rotation as an FP32 2×2 matrix.
The tensors count against the runner's device memory budget.
The runner clears them when the position key changes or sampling ends. If
allocation fails, it clears the device cache and retries with the CPU inputs.

## Adding a model

Include `core/pe_cache.h` and declare a member:
Expand Down
79 changes: 57 additions & 22 deletions src/core/cuda/convrot.cu
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ namespace SD_CUDA_NAMESPACE {
NORM,
MOD,
RMS_MOD,
RMS_MOD_VEC,
SWIGLU,
SITU,
GELU,
SILU,
SIGMOID };
struct Operation {
Expand All @@ -43,6 +45,17 @@ namespace SD_CUDA_NAMESPACE {
int64_t k, rows, stride, up_stride, gate_offset = 0, up_offset = 0;
float eps = 0.f, scale = 1.f, bias = 0.f;
};
static bool vector_rms_input(const ggml_tensor* input, int64_t width) {
if (width != 1024 && width != 2048 && width != 4096) {
return false;
}
size_t offset = 0;
while (!input->data && input->view_src) {
offset += input->view_offs;
input = input->view_src;
}
return (reinterpret_cast<uintptr_t>(input->data) + offset) % 16 == 0;
}
static bool describe(ggml_tensor* const* nodes, int count, Operation& op) {
int group;
const auto* first = nodes[0];
Expand Down Expand Up @@ -73,14 +86,20 @@ namespace SD_CUDA_NAMESPACE {
op.shift = first->src[2];
if (!vector(op.modulation, op.k) || (op.shift && !vector(op.shift, op.k)))
return false;
} else if (count == 4 && first->op == GGML_OP_RMS_NORM && nodes[1]->op == GGML_OP_MUL && nodes[1]->src[0] == first &&
sd_ggml_modulate_describe(nodes[2]) && nodes[2]->src[0] == nodes[1] && value == nodes[2]) {
op.kind = RMS_MOD;
op.weight = nodes[1]->src[1];
op.modulation = nodes[2]->src[1];
op.shift = nodes[2]->src[2];
} else if (first->op == GGML_OP_RMS_NORM && value == nodes[count - 2] && sd_ggml_modulate_describe(value)) {
if (count == 3 && value->src[0] == first && vector_rms_input(op.input, op.k)) {
op.kind = RMS_MOD_VEC;
} else if (count == 4 && nodes[1]->op == GGML_OP_MUL && nodes[1]->src[0] == first &&
value->src[0] == nodes[1] && op.k >= 1024 && vector(nodes[1]->src[1], op.k)) {
op.kind = RMS_MOD;
op.weight = nodes[1]->src[1];
} else {
return false;
}
op.modulation = value->src[1];
op.shift = value->src[2];
op.eps = param_f32(first, 0);
if (op.k < 1024 || !vector(op.weight, op.k) || !vector(op.modulation, op.k) || (op.shift && !vector(op.shift, op.k)))
if (!vector(op.modulation, op.k) || (op.shift && !vector(op.shift, op.k)))
return false;
} else if (count == 2 && first->op == GGML_OP_GLU && value == first) {
const auto glu = ggml_get_glu_op(first);
Expand All @@ -97,6 +116,8 @@ namespace SD_CUDA_NAMESPACE {
else
op.up_offset = op.k;
}
} else if (count == 2 && first->op == GGML_OP_UNARY && ggml_get_unary_op(first) == GGML_UNARY_OP_GELU && value == first) {
op.kind = GELU;
} else if (count == 3 && first->op == GGML_OP_UNARY && nodes[1]->op == GGML_OP_MUL && value == nodes[1] &&
(nodes[1]->src[0] == first || nodes[1]->src[1] == first)) {
auto unary = ggml_get_unary_op(first);
Expand All @@ -118,17 +139,20 @@ namespace SD_CUDA_NAMESPACE {
return true;
}
static bool supported(const Operation& op, const ggml_ext_gpu_device_info& device) {
const size_t extra = op.kind == NORM ? 384 : op.kind == RMS_MOD ? 256
: 128;
const size_t extra = op.kind == NORM ? 384 : op.kind == RMS_MOD || op.kind == RMS_MOD_VEC ? 256
: 128;
return device.device >= 0 && size_t(op.k) * sizeof(float) + extra <= device.shared_memory;
}
static int match(ggml_backend_dev_t device, const ggml_cgraph* graph, int first, void*) {
if (fusion_disabled())
return 0;
auto* g = const_cast<ggml_cgraph*>(graph);
const int available = ggml_graph_n_nodes(g) - first;
const auto* node = ggml_graph_node(g, first);
const int count = node->op == GGML_OP_RMS_NORM ? 4 : node->op == GGML_OP_NORM || node->op == GGML_OP_UNARY ? 3
auto* g = const_cast<ggml_cgraph*>(graph);
const int available = ggml_graph_n_nodes(g) - first;
const auto* node = ggml_graph_node(g, first);
const bool unary_mul = node->op == GGML_OP_UNARY && ggml_get_unary_op(node) != GGML_UNARY_OP_GELU;
const bool affine_rms = node->op == GGML_OP_RMS_NORM && available >= 2 &&
ggml_graph_node(g, first + 1)->op == GGML_OP_MUL;
const int count = affine_rms ? 4 : node->op == GGML_OP_RMS_NORM || node->op == GGML_OP_NORM || unary_mul ? 3
: 2;
if (available < count)
return 0;
Expand Down Expand Up @@ -157,6 +181,11 @@ namespace SD_CUDA_NAMESPACE {
chain[2] = value;
chain[3] = quant;
count = 4;
} else if (weight->op == GGML_OP_RMS_NORM && vector_rms_input(weight->src[0], value->ne[0])) {
chain[0] = weight;
chain[1] = value;
chain[2] = quant;
count = 3;
} else {
chain[0] = value;
chain[1] = quant;
Expand Down Expand Up @@ -221,26 +250,26 @@ namespace SD_CUDA_NAMESPACE {
const auto x = reinterpret_cast<uintptr_t>(a->data), y = reinterpret_cast<uintptr_t>(b->data);
return x <= y ? y - x < ggml_nbytes(a) : x - y < ggml_nbytes(b);
}
template <bool Swiglu = false, bool Normalize = false, bool Scaled = false, bool Sigmoid = false, bool RMS = false, bool Modulate = false, bool Situ = false>
template <bool Swiglu = false, bool Normalize = false, bool Scaled = false, bool Sigmoid = false, bool RMS = false, bool Modulate = false, bool Situ = false, bool Gelu = false, bool RMSVec = false>
static CUDAError launch(Plan& p, const Operation& op, int8_t* output, CUDAStream stream) {
constexpr bool warp = Scaled || RMS || Modulate;
constexpr bool warp = Scaled || RMS || Modulate || Gelu;
constexpr int extra = Normalize ? 384 : RMS ? 256
: 128;
if (!p.configured) {
auto status = cuda_set_shared_memory(quantize<Swiglu, false, Normalize, Scaled, warp, float, Sigmoid, RMS, Modulate, Situ>,
auto status = cuda_set_shared_memory(quantize<Swiglu, false, Normalize, Scaled, warp, float, Sigmoid, RMS, Modulate, Situ, Gelu, RMSVec>,
int(p.device.shared_memory - extra));
if (status != cuda_success)
return status;
p.configured = true;
}
const int threads = Normalize ? 1024 : RMS ? (op.k <= 1024 ? 128 : op.k <= 6144 ? 256
: op.k <= 8192 ? 512
: 1024)
: (Situ ? op.k >= 8192 : Swiglu && !Sigmoid && op.k >= 16384) ? 512
: 256;
const int threads = Normalize ? 1024 : RMS ? (op.k <= 1024 ? 128 : op.k <= 6144 ? 256
: op.k <= 8192 ? 512
: 1024)
: (Situ ? op.k >= 8192 : (Gelu || (Swiglu && !Sigmoid)) && op.k >= 16384) ? 512
: 256;
const int64_t padded = (op.rows + 3) / 4 * 4;
auto data = [](const ggml_tensor* t) { return t ? static_cast<const float*>(t->data) : nullptr; };
quantize<Swiglu, false, Normalize, Scaled, warp, float, Sigmoid, RMS, Modulate, Situ><<<unsigned(op.rows), threads, size_t(op.k) * sizeof(float), stream>>>(
quantize<Swiglu, false, Normalize, Scaled, warp, float, Sigmoid, RMS, Modulate, Situ, Gelu, RMSVec><<<unsigned(op.rows), threads, size_t(op.k) * sizeof(float), stream>>>(
data(op.input) + op.gate_offset, output, reinterpret_cast<float*>(output + op.k * padded), op.k, op.rows, op.k,
op.up ? data(op.up) + op.up_offset : nullptr, op.stride, op.up_stride, nullptr, nullptr, data(op.modulation), op.eps, op.scale, op.bias, data(op.weight), data(op.shift));
return cuda_last_error();
Expand Down Expand Up @@ -271,6 +300,9 @@ namespace SD_CUDA_NAMESPACE {
case RMS_MOD:
error = launch<false, false, false, false, true>(plan, op, output, stream);
break;
case RMS_MOD_VEC:
error = launch<false, false, false, false, true, false, false, false, true>(plan, op, output, stream);
break;
case MOD:
error = launch<false, false, false, false, false, true>(plan, op, output, stream);
break;
Expand All @@ -281,6 +313,9 @@ namespace SD_CUDA_NAMESPACE {
case SITU:
error = launch<true, false, false, false, false, false, true>(plan, op, output, stream);
break;
case GELU:
error = launch<false, false, false, false, false, false, false, true>(plan, op, output, stream);
break;
case SIGMOID:
error = launch<true, false, false, true>(plan, op, output, stream);
break;
Expand Down
30 changes: 21 additions & 9 deletions src/core/cuda/convrot_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace SD_CUDA_NAMESPACE {
return __fmul_rn(__fmul_rn(a, 1.0f / (1.0f + expf(-gate))), b);
}

template <bool swiglu, bool dequantize = false, bool normalize = false, bool scaled = false, bool warp_rotation = false, typename T = float, bool sigmoid_gate = false, bool rms_modulation = false, bool modulate = false, bool situ_gate = false>
template <bool swiglu, bool dequantize = false, bool normalize = false, bool scaled = false, bool warp_rotation = false, typename T = float, bool sigmoid_gate = false, bool rms_modulation = false, bool modulate = false, bool situ_gate = false, bool gelu = false, bool rms_vec = false>
__global__ void quantize(
const T* src,
int8_t* dst,
Expand Down Expand Up @@ -81,20 +81,27 @@ namespace SD_CUDA_NAMESPACE {
[[maybe_unused]] float rms_scale = 1.0f;
if constexpr (rms_modulation) {
__shared__ float sums[32];
// Preserve the unfused RMSNorm's 1024-thread reduction order with fewer physical warps.
for (int virtual_tid = tid; virtual_tid < 1024; virtual_tid += blockDim.x) {
// Match the unfused RMSNorm reduction across a smaller physical block.
const int reduction_threads = rms_vec ? k / 4 : 1024;
for (int virtual_tid = tid; virtual_tid < reduction_threads; virtual_tid += blockDim.x) {
float partial = 0.0f;
for (int64_t i = virtual_tid; i < k; i += 1024) {
const float value = src_row[i];
partial += value * value;
if constexpr (rms_vec) {
const float4 a = reinterpret_cast<const float4*>(src_row)[virtual_tid];
partial = a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
} else {
for (int64_t i = virtual_tid; i < k; i += 1024) {
const float value = src_row[i];
partial += value * value;
}
}
partial = warp_reduce_sum(partial);
if (tid % 32 == 0) {
sums[virtual_tid / 32] = partial;
}
}
__syncthreads();
const float sum = warp_reduce_sum(sums[tid % 32]);
const int lane = tid % 32;
const float sum = warp_reduce_sum(lane < reduction_threads / 32 ? sums[lane] : 0.f);
rms_scale = rsqrtf(sum / k + eps);
}

Expand All @@ -108,7 +115,10 @@ namespace SD_CUDA_NAMESPACE {
if constexpr (!swiglu) {
float value = src_row[i];
if constexpr (rms_modulation) {
value = __fmul_rn(__fmul_rn(value, rms_scale), rms_weight[i]);
value = __fmul_rn(value, rms_scale);
if constexpr (!rms_vec) {
value = __fmul_rn(value, rms_weight[i]);
}
}
if constexpr (rms_modulation || modulate) {
value = __fadd_rn(value, __fmul_rn(value, modulation[i]));
Expand All @@ -119,6 +129,9 @@ namespace SD_CUDA_NAMESPACE {
if constexpr (scaled) {
value = __fmaf_rn(value, input_scale, input_bias);
}
if constexpr (gelu) {
value = 0.5f * value * (1.0f + tanhf(0.7978845608028654f * value * (1.0f + 0.044715f * value * value)));
}
v[j] = value * (1.0f / 16.0f);
} else {
float gate_value, up_value;
Expand Down Expand Up @@ -260,7 +273,6 @@ namespace SD_CUDA_NAMESPACE {
dst_row[i] = (int8_t)value;
}
}

}
}
#endif
Loading
Loading