diffusion¶
diffusion
¶
DiffusionBatcher
¶
Bases: Batcher
Batcher for the denoiser's guidance-doubled, multi-image batch layout.
Where the base batcher assumes every batched activation is a plain dim-0 stack
of the invokes' rows, a diffusion denoiser sees an expanded batch: each prompt
is repeated num_images_per_prompt times, and under classifier-free guidance
the whole thing is doubled (an unconditional half followed by a conditional
half). This maps each invoke's plain [start, size] group onto that layout,
picking the case by the tensor's leading dim at run time — so an intervention on
model.unet reads (and writes) exactly its invoke's rows across both halves.
DiffusionModel
¶
Bases: HuggingFaceModel
A model backed by a diffusers.DiffusionPipeline.
A diffusion pipeline orchestrates several modules (unet/transformer, vae,
text_encoder, ...) around a denoising loop. This wraps the whole pipeline as one
nnsight model: the pipeline is exposed as pipeline, each of its module
components as an envoy (model.unet, model.vae, ...), and interventions
apply to any component along the way.
Both trace and generate run the whole pipeline, with
interventions firing on every component the denoising loop invokes;
model.output (and tracer.result) is the pipeline's output object.
A traced run defaults to num_inference_steps=1 — a fast one-step pass for
inspecting or editing activations — and with model.generate(...): is a
traced run, so it takes that default too; only a bare model.generate(...)
call outside a trace uses the pipeline's own default. The same call means one
denoising step inside a with and fifty outside it, so pass
num_inference_steps= whenever the image itself matters. To run one
component's forward on its own, trace that envoy directly —
with model.unet.trace(sample, timestep, encoder_hidden_states=...):.
On dispatch a real pipeline is built with real weights. The lazy meta build can't load a pipeline without weights, so each module component is constructed from its config on the meta device while the light components (scheduler, tokenizer, ...) load normally, and a meta pipeline of the same shape is assembled from them.
Requires the optional diffusers package.
| PARAMETER | DESCRIPTION |
|---|---|
repo_id
|
A
TYPE:
|
*args
|
Forwarded to the mixin chain.
TYPE:
|
**kwargs
|
Forwarded to the mixin chain and, on dispatch, to
TYPE:
|
Examples:
>>> from nnsight.modeling.diffusion import DiffusionModel
>>> model = DiffusionModel("hf-internal-testing/tiny-stable-diffusion-torch")
>>> with model.generate("a photo of a cat", num_inference_steps=2) as tracer:
... latents = model.unet.output[0].save() # per denoising step
... images = model.output.save()
>>> images.images[0] # a PIL image
trace
¶
Trace the whole pipeline, defaulting to a single denoising step.
num_inference_steps=1 unless overridden — a fast one-step pass for
inspecting or editing activations. model.output is the pipeline's
output object. with model.generate(...): routes here as well, so a
traced generation takes the one-step default too.
Examples:
generate
¶
Run the diffusion pipeline, returning its output object.
with model.generate(...): traces the whole pipeline, so the block's
interventions run against every component the denoising loop invokes (use
tracer.iter to target a particular inference step); calling it directly
just runs the pipeline. A traced run goes through trace, so it
defaults to one denoising step where a direct call takes the pipeline's own
default — name num_inference_steps to fix the count either way. The
return value is the pipeline's own output
object — read the images off its .images (or off model.output /
tracer.result inside a trace).
Examples:
>>> with model.generate("a photo of a cat", num_inference_steps=20):
... images = model.output.save()
>>> images.images[0] # a PIL image
| PARAMETER | DESCRIPTION |
|---|---|
*inputs
|
The pipeline's inputs, e.g. a text prompt.
TYPE:
|
**kwargs
|
Forwarded to the pipeline, e.g.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
The pipeline's output object; its |