From 4ca54762ff527d50bf0b4a38d407b65c806824c6 Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Mon, 20 Jul 2026 08:43:15 -0500 Subject: [PATCH 1/2] Build the Qwen3 attention mask in the query dtype Since #33 the Qwen3 encoder's additive mask goes through the fused SDPA kernel, whose CUDA memory-efficient backend requires the bias dtype to match the query; the mask was always float32 against bf16 q/k/v, so every FLUX.2 prompt encode on GPU failed with 'invalid dtype for bias'. Build the mask in the embedding dtype instead: -10000 fill (the dit_ltx23 convention; -3.4e38 overflows bfloat16 to -Inf) and the $neg()$add(1) chain, since the Ops dispatch materializes R scalars as Float tensors and promotes the result. --- R/qwen3_text_encoder.R | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/R/qwen3_text_encoder.R b/R/qwen3_text_encoder.R index 7d09e32..c68390a 100644 --- a/R/qwen3_text_encoder.R +++ b/R/qwen3_text_encoder.R @@ -164,17 +164,22 @@ qwen3_encoder <- torch::nn_module( b <- input_ids$shape[1] s <- input_ids$shape[2] device <- x$device - f32 <- torch::torch_float32() + dt <- x$dtype rope <- .qwen3_rope_tables(s, self$head_dim, self$rope_theta, device) - # Additive causal (+ padding) mask [B, 1, S, S] in float32 - neg <- -3.4e38 - causal <- torch::torch_full(c(s, s), neg, dtype = f32, + # Additive causal (+ padding) mask [B, 1, S, S] in the compute dtype: + # the fused SDPA kernel requires the bias dtype to match the query. + # -10000 (the dit_ltx23 convention) stays finite in every float + # dtype, including where -3.4e38 would overflow bfloat16 to -Inf. + # The $neg()$add(1) chain (not `1 - x`) keeps torch Scalars, since + # the Ops dispatch materializes R scalars as Float and promotes. + neg <- -10000 + causal <- torch::torch_full(c(s, s), neg, dtype = dt, device = device)$triu(diagonal = 1L) mask <- causal$unsqueeze(1L)$unsqueeze(1L)$expand(c(b, 1L, s, s)) if (!is.null(attention_mask)) { - pad <- (1 - attention_mask$to(dtype = f32, device = device))$mul(neg) + pad <- attention_mask$to(dtype = dt, device = device)$neg()$add(1)$mul(neg) mask <- mask + pad$unsqueeze(2L)$unsqueeze(2L) } From 13d08230a808caec99e456e5832b49f93c3192eb Mon Sep 17 00:00:00 2001 From: TroyHernandez Date: Mon, 20 Jul 2026 10:06:18 -0500 Subject: [PATCH 2/2] Bump version to 0.1.0.8 --- DESCRIPTION | 2 +- NEWS.md | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index 7a727e5..97214f7 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: diffuseR Title: Functional Interface to Diffusion Models in R -Version: 0.1.0.7 +Version: 0.1.0.8 Authors@R: c( person("Troy", "Hernandez", email = "troy@cornball.ai", role = c("aut", "cre"), comment = c(ORCID = "0009-0005-4248-604X")), diff --git a/NEWS.md b/NEWS.md index e03a58a..c80bb61 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# diffuseR 0.1.0.8 (development) + +* The Qwen3 encoder builds its additive attention mask in the query + dtype, fixing the "invalid dtype for bias" CUDA error every FLUX.2 + prompt encode hit through the fused SDPA path (since #33). + # diffuseR 0.1.0.7 (development) ## SDXL native pipeline from safetensors