Run this model with HF Jobs

Run PrunaAI/P-Flux-2-Klein-4B-DWD on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs to fine-tune https://huggingface.co/PrunaAI/P-Flux-2-Klein-4B-DWD. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/PrunaAI/P-Flux-2-Klein-4B-DWD on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
P-Flux-2-Klein-4B-DWD / tests /test_evaluate.py
johnrachwanpruna's picture
P-Flux-2-Klein-4B-DWD
24baf68
Raw History Blame Contribute Delete
4.28 kB
"""4-step sampler with the adapted tail on a tiny CPU model: the student path returns the DWD-head image and the VAE
image from the same final step; at init (student == reference) its VAE image equals the original model's."""
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(HERE), "src"))
sys.path.insert(0, HERE)
import pytest # noqa: E402
import torch # noqa: E402
pytest.importorskip("diffusers")
def test_sampler_outputs_head_and_vae():
from diffusers import FlowMatchEulerDiscreteScheduler
from dwd_klein.evaluate import Sampler, default_prompts, psnr_u8
from dwd_klein.color import to_uint8
from dwd_klein.model import DWDKlein
from tiny import TinyText, TinyVAE, tiny_base
model = DWDKlein(tiny_base(0), k=2)
with torch.no_grad():
model.student.head.weight.normal_(0, 0.01)
sch = FlowMatchEulerDiscreteScheduler(use_dynamic_shifting=True, time_shift_type="exponential")
s = Sampler(model, TinyText(), TinyVAE(), sch, torch.device("cpu"))
o = s.generate("a cat", 48, 32, seed=3, tail=model.ref)
a = s.generate("a cat", 48, 32, seed=3, tail=model.student)
assert o["head"] is None and a["head"].shape == a["vae"].shape == (1, 3, 32, 48)
assert torch.equal(a["vae"], o["vae"]) # student == reference at init
assert a["head"].abs().max() <= 1
ts, sig = s.resolution(2, 3)
assert len(ts) == 4 and float(sig[-1]) == 0.0
x = torch.rand(1, 3, 32, 48) * 2 - 1
r = s.recon(x, TinyText()(["a cat"]), model.student, seed=0)
assert r["head"].shape == r["vae"].shape == x.shape
assert psnr_u8(to_uint8(a["vae"]), to_uint8(o["vae"])) == 99.0
assert len(default_prompts(7)) == 7
def test_final_tail_and_stock_bf16_tail():
"""final_tail swaps only the last step's velocity (at init == student, so identical images); pure_bf16_copy of the
reference tail is bit-exact with the stock bf16 model on the same prefix states."""
import copy
from diffusers import FlowMatchEulerDiscreteScheduler
from dwd_klein import schedule as S
from dwd_klein.color import latent_ids, text_ids
from dwd_klein.evaluate import Sampler
from dwd_klein.model import DWDKlein, pure_bf16_copy
from tiny import JOINT, N_TXT, TinyText, TinyVAE, tiny_base
base = tiny_base(0, n_single=3)
orig = copy.deepcopy(base).to(torch.bfloat16)
model = DWDKlein(base, k=2)
sch = FlowMatchEulerDiscreteScheduler(use_dynamic_shifting=True, time_shift_type="exponential")
s = Sampler(model, TinyText(), TinyVAE(), sch, torch.device("cpu"))
a = s.generate("a cat", 48, 32, seed=3, tail=model.student, final_tail=model.ref)
assert torch.equal(a["vae_final"], a["vae"])
with torch.no_grad(): # a student whose velocity differs: only the hybrid's last step uses the reference
model.student.proj_out.weight.mul_(1.5)
b = s.generate("a cat", 48, 32, seed=3, tail=model.student, final_tail=model.ref)
o = s.generate("a cat", 48, 32, seed=3, tail=model.ref)
assert not torch.equal(b["vae_final"], b["vae"]) and not torch.equal(b["vae_final"], o["vae"])
stock = pure_bf16_copy(model.ref)
assert all(p.dtype == torch.bfloat16 and not p.requires_grad for p in stock.parameters())
g = torch.Generator().manual_seed(0)
x = torch.randn(2, 6, 128, generator=g).to(torch.bfloat16)
enc = torch.randn(2, N_TXT, JOINT, generator=g).to(torch.bfloat16)
t = torch.tensor([0.9, 0.5])
iid, tid = latent_ids(2, 3), text_ids(N_TXT)
ts = S.model_timestep(t)
with torch.no_grad():
full = orig(hidden_states=x, encoder_hidden_states=enc, timestep=(t * 1000).to(torch.bfloat16) / 1000,
img_ids=iid, txt_ids=tid, return_dict=False)[0]
with torch.autocast("cpu", dtype=torch.bfloat16): # the caller's autocast must not matter
hs, rot, n = model.prefix(x, enc, ts, iid, tid)
v_stock = stock(hs, ts, rot, n)[0]
v_ref = model.ref(hs, ts, rot, n)[0]
assert torch.equal(v_stock, full)
# the fp32-master reference is only close (about 0.7% velocity on the real model); tiny CPU shapes may even match
assert float((v_ref.float() - full.float()).norm() / full.float().norm()) < 0.02