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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
9 changes: 9 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
50 changes: 37 additions & 13 deletions R/resident.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
Expand Down Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions inst/tinytest/test_resident_flux3_edit.R
Original file line number Diff line number Diff line change
@@ -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")
14 changes: 10 additions & 4 deletions man/resident_generate.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading