diff --git a/DESCRIPTION b/DESCRIPTION index dc42751..8921d9b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: diffuseR Title: Functional Interface to Diffusion Models in R -Version: 0.2.2.26 +Version: 0.2.2.27 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 cca39be..1439409 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +# diffuseR 0.2.2.27 + +* `resident_generate(res, prompt, image = )` on a `flux3` handle runs the + single-reference edit (`img2img_flux3()`) instead of text-to-image, with + the rest of `...` passed on; the handle must hold the VAE encoder + (`flux3_load_pipeline(encoder = TRUE)`, the loader's default, which + `resident_load("flux3")` passes through). An `image` on another family + still goes to that family's generator (LTX's start frame). + # diffuseR 0.2.2.26 * LTX keyframes: `ltx23_keyframe(x, frame, strength)` and diff --git a/R/resident.R b/R/resident.R index b80b0b7..48ff2fb 100644 --- a/R/resident.R +++ b/R/resident.R @@ -851,10 +851,16 @@ resident_deactivate <- function(res, release = TRUE) { #' @param res A \code{diffuseR_resident} handle. #' @param prompt Character. The text prompt. #' @param ... Passed to \code{\link{txt2img_flux}}, -#' \code{\link{txt2img_flux2}}, \code{\link{txt2img_zimage}}, -#' \code{\link{txt2vid_ltx2}} or \code{\link{txt2img_sdxl}}. For -#' \code{sdxl} the handle supplies \code{devices} matching its own -#' placement unless the caller names it. +#' \code{\link{txt2img_flux2}}, \code{\link{txt2img_flux3}}, +#' \code{\link{txt2img_zimage}}, \code{\link{txt2vid_ltx2}} or +#' \code{\link{txt2img_sdxl}}. For \code{sdxl} the handle supplies +#' \code{devices} matching its own placement unless the caller names +#' it. For \code{flux3} an \code{image} (a path or an [H, W, 3] array) +#' routes the call to \code{\link{img2img_flux3}}, the single-reference +#' edit, with the rest of \code{...} (\code{max_area}, \code{width}, +#' \code{height}, \code{seed}, ...) passed on; the handle must hold the +#' VAE encoder, which \code{\link{flux3_load_pipeline}} loads by default +#' (\code{encoder = TRUE}, passed through \code{resident_load}). #' #' @return Whatever the family generator returns, which is always a list. #' @@ -884,16 +890,34 @@ resident_generate <- function(res, prompt, ...) { stop("cannot generate from state '", res$state, "'; call resident_activate() first", call. = FALSE) } - gen <- switch(res$model, - flux1 = txt2img_flux, - flux2 = txt2img_flux2, - flux3 = txt2img_flux3, - zimage = txt2img_zimage, - ltx = txt2vid_ltx2, - sdxl = txt2img_sdxl, - sd21 = txt2img_sd21) + dots <- list(...) + gen <- .resident_generator(res, dots) do.call(gen, c(list(prompt, pipeline = res$pipeline), - .resident_gen_args(res, list(...)))) + .resident_gen_args(res, dots))) +} + +# The generator a resident call dispatches to: the family's text +# generator, or FLUX 3's single-reference edit when a flux3 call carries +# an `image`. An `image` on any other family is left to that family's +# generator (LTX takes one as its start frame). Split out so the routing +# can be asserted without a model. +.resident_generator <- function(res, dots) { + if (identical(res$model, "flux3") && !is.null(dots$image)) { + if (is.null(res$pipeline$encoder)) { + stop("this resident FLUX 3 pipeline has no VAE encoder, so it ", + "cannot edit; load it with resident_load(\"flux3\", ", + "encoder = TRUE)", call. = FALSE) + } + return(img2img_flux3) + } + switch(res$model, + flux1 = txt2img_flux, + flux2 = txt2img_flux2, + flux3 = txt2img_flux3, + zimage = txt2img_zimage, + ltx = txt2vid_ltx2, + sdxl = txt2img_sdxl, + sd21 = txt2img_sd21) } #' Family fixups for a resident generate call diff --git a/inst/tinytest/test_resident_flux3_edit.R b/inst/tinytest/test_resident_flux3_edit.R new file mode 100644 index 0000000..607e194 --- /dev/null +++ b/inst/tinytest/test_resident_flux3_edit.R @@ -0,0 +1,33 @@ +# resident_generate() on a flux3 handle routes an `image` to the +# single-reference edit; without one, and for every other family, the +# text generator stands. No model, no GPU. + +library(tinytest) +library(diffuseR) + +pick <- diffuseR:::.resident_generator +mk <- function(model, encoder = TRUE) { + h <- new.env(parent = emptyenv()) + h$model <- model + h$state <- "active" + h$pipeline <- if (encoder) list(encoder = "vae-encoder") else list() + class(h) <- "diffuseR_resident" + h +} + +expect_identical(pick(mk("flux3"), list()), txt2img_flux3) +expect_identical(pick(mk("flux3"), list(seed = 7L)), txt2img_flux3) +expect_identical(pick(mk("flux3"), list(image = "ref.png", seed = 7L)), + img2img_flux3) +expect_error(pick(mk("flux3", encoder = FALSE), list(image = "ref.png")), + "VAE encoder") +# an image on the video family is its start frame, untouched +expect_identical(pick(mk("ltx"), list(image = "frame.png")), txt2vid_ltx2) +expect_identical(pick(mk("flux2"), list()), txt2img_flux2) +expect_identical(pick(mk("zimage"), list()), txt2img_zimage) + +# the state check comes before the routing +h <- mk("flux3") +h$state <- "inactive" +expect_error(resident_generate(h, "p", image = "ref.png"), + pattern = "resident_activate") diff --git a/man/resident_generate.Rd b/man/resident_generate.Rd index b41e4d7..1bba722 100644 --- a/man/resident_generate.Rd +++ b/man/resident_generate.Rd @@ -11,10 +11,16 @@ resident_generate(res, prompt, ...) \item{prompt}{Character. The text prompt.} \item{...}{Passed to \code{\link{txt2img_flux}}, -\code{\link{txt2img_flux2}}, \code{\link{txt2img_zimage}}, -\code{\link{txt2vid_ltx2}} or \code{\link{txt2img_sdxl}}. For -\code{sdxl} the handle supplies \code{devices} matching its own -placement unless the caller names it.} +\code{\link{txt2img_flux2}}, \code{\link{txt2img_flux3}}, +\code{\link{txt2img_zimage}}, \code{\link{txt2vid_ltx2}} or +\code{\link{txt2img_sdxl}}. For \code{sdxl} the handle supplies +\code{devices} matching its own placement unless the caller names +it. For \code{flux3} an \code{image} (a path or an [H, W, 3] array) +routes the call to \code{\link{img2img_flux3}}, the single-reference +edit, with the rest of \code{...} (\code{max_area}, \code{width}, +\code{height}, \code{seed}, ...) passed on; the handle must hold the +VAE encoder, which \code{\link{flux3_load_pipeline}} loads by default +(\code{encoder = TRUE}, passed through \code{resident_load}).} } \value{ Whatever the family generator returns, which is always a list.