Skip to main content

ZImageDit

Struct ZImageDit 

Source
pub struct ZImageDit {
    pub cfg: ZConfig,
    pub model: Option<Arc<CmfModel>>,
    /* private fields */
}
Expand description

The Z-Image transformer. Tensor names are the ORIGINAL diffusers names under dit. (no rename map).

Fields§

§cfg: ZConfig§model: Option<Arc<CmfModel>>

The container this was loaded from (None for load_dir) — device paths need tensor indices into it.

Implementations§

Source§

impl ZImageDit

Source

pub fn load_dir(dir: &Path) -> Result<Self, String>

Load from a diffusers transformer/ directory (bf16/fp32 safetensors, read whole and widened to f32 — a dev/parity path).

Source

pub fn from_cmf(model: &Arc<CmfModel>) -> Result<Self, String>

Load from a packaged .cmf (dit.* tensors + dit.config_json). Quantized projections stay mmap-resident.

Source

pub fn geom(&self) -> ZGeom

Source

pub fn temb(&self, t_model: f32) -> Vec<f32>

Timestep embedding for t_model = (1000 − 1000σ)/1000: sinusoid of t·1000 (cos first, f32 args) → mlp.0 → SiLU → mlp.2. Returns [256].

Source

pub fn mods_for_steps(&self, t_models: &[f32]) -> Vec<f32>

Raw adaLN_modulation.0(temb) for every step and block: [steps][2 + n_layers][4 · dim], chunks [scale_msa, gate_msa, scale_mlp, gate_mlp] (no +1, no tanh). f64 accumulation.

Source

pub fn final_scale_for_steps(&self, t_models: &[f32]) -> Vec<f32>

1 + all_final_layer.2-1.adaLN_modulation.1(SiLU(temb)) per step: [steps][dim].

Source

pub fn embed_caption(&self, cap_feats: &[f32], l: usize) -> Vec<f32>

Caption features [l, cap_feat_dim] → [l_p, dim]: pad rows are copies of the last row, RMSNorm(w, 1e-5) → Linear + b, rows ≥ l := cap_pad_token.

Source

pub fn refine_caption_cpu(&self, cap: &mut [f32], rope_cap: (&[f32], &[f32]))

The two unmodulated context-refiner blocks on the host, in place on cap [l_p, dim].

Source

pub fn block_cpu( &self, blk: ZBlockId, x: &mut [f32], n: usize, rope: (&[f32], &[f32]), m: Option<&[f32]>, )

One block on the host, in place on x [n, dim]. m = the block’s raw modulation [4 · dim] (None = unmodulated: s = 0, gate = 1).

Source

pub fn block_refs(&self) -> Option<ZBlockRefs<'_>>

Device views of all blocks (requires from_cmf).

Source

pub fn prepare( &self, cap_feats: &[f32], shape: ZShape, key: u64, mods_all: Option<(&[f32], &[f32])>, ) -> Result<ZPrepared, String>

Once per (prompt, resolution): caption embed → context refiner (gpu::zimage_refine_caption, else CPU) → rope tables → gpu::zimage_prepare (sets ZPrepared::device). mods_all = (mods_for_steps, final_scale_for_steps) forwarded to the backend.

Source

pub fn prepare_with( &self, cap_feats: &[f32], shape: ZShape, key: u64, mods_all: Option<(&[f32], &[f32])>, device: bool, ) -> Result<ZPrepared, String>

prepare with the device use explicit: device = false builds a pure host state (CPU context refiner, no gpu::zimage_prepare) — the reference a device test diffs step against.

Source

pub fn prepare_host( &self, cap_feats: &[f32], shape: ZShape, key: u64, device_refine: bool, ) -> Result<ZPrepared, String>

The host half of prepare: caption embed → context refiner (gpu::zimage_refine_caption when device_refine, else CPU) → rope tables. device is false until attach_device.

Source

pub fn preload_device(&self) -> bool

Upload every device plane now (gpu::zimage_preload); independent of the caption, so it can run beside the text encoder.

Source

pub fn attach_device( &self, p: &mut ZPrepared, mods_all: Option<(&[f32], &[f32])>, ) -> bool

gpu::zimage_prepare for a host state (sets p.device).

Source

pub fn attach_device_pair( &self, pos: &ZPrepared, neg: &ZPrepared, key: u64, mods_all: Option<(&[f32], &[f32])>, ) -> bool

One batch-2 device program for a CFG pair (item 0 = pos, item 1 = neg, both at the same resolution) under key. false = the backend has no batch 2 here; the caller steps the items one by one.

Source

pub fn step_pair_device( &self, key: u64, n_img: usize, step: usize, x_tok: &[f32], mods: &[f32], final_scale: &[f32], ) -> Option<(Vec<f32>, Vec<f32>)>

Both items of a CFG pair prepared by attach_device_pair under key, in one device forward: returns (v_pos, v_neg), or None when the backend declined (the caller steps the items separately).

Source

pub fn step( &self, p: &ZPrepared, step: usize, x_tok: &[f32], mods: &[f32], final_scale: &[f32], ) -> Vec<f32>

One DiT forward: gpu::zimage_step if p.device, else step_cpu. x_tok [n_img_p, 64] (pad rows = copies of the last row), mods [(2 + n_layers) · 4 · dim] of this step, final_scale [dim]. Returns v [n_img, 64] (before the pipeline’s negation).

Source

pub fn tokens(&self, latent: &[f32], shape: &ZShape) -> Vec<f32>

The step input for a latent [16, h_lat, w_lat]: patchify, then pad to n_img_p rows by repeating the last row.

Source

pub fn embed_image( &self, x_tok: &[f32], n_img: usize, n_img_p: usize, ) -> Vec<f32>

x_tok [n_img_p, 64] → [n_img_p, dim]: Linear + b, rows ≥ n_img := x_pad_token.

Source

pub fn final_layer(&self, u: &[f32], n: usize, final_scale: &[f32]) -> Vec<f32>

Final layer on the first n rows of u: LayerNorm(eps 1e-6, no affine) · final_scale → Linear(dim → 64) + b.

Source

pub fn step_cpu( &self, p: &ZPrepared, x_tok: &[f32], mods: &[f32], final_scale: &[f32], ) -> Vec<f32>

The host reference forward (WP2/WP3 gate against this): embed → pad rows := x_pad_token → noise refiner (img only) → concat [img, cap] → layers → LayerNorm(1e-6)·final_scale → Linear → image rows.

Source

pub fn step_cpu_taps( &self, p: &ZPrepared, x_tok: &[f32], mods: &[f32], final_scale: &[f32], tap: &mut dyn FnMut(&str, &[f32]), ) -> Vec<f32>

step_cpu with a tap callback: (name, tensor) at the oracle’s tap points (x_seq, nr{i}_out, u_in, l{i}_out, final_out).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more