Skip to content

Commit bc5c0cb

Browse files
committed
Standardize logging to max_logging, update reference images, and verify 4B/9B smoke tests
1 parent 0eb3a58 commit bc5c0cb

7 files changed

Lines changed: 34 additions & 18 deletions

File tree

src/maxdiffusion/generate_flux2klein.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def partition_prompts(prompt_str: str, batch_size: int) -> List[str]:
5858
active.extend([raw_prompts[-1]] * (batch_size - len(active)))
5959
return active
6060
else:
61-
print(f"⚠️ Warning: Found {num_prompts} prompts, but batch_size is {batch_size}. Truncating to the first {batch_size}.")
61+
max_logging.log(
62+
f"⚠️ Warning: Found {num_prompts} prompts, but batch_size is {batch_size}. Truncating to the first {batch_size}."
63+
)
6264
return raw_prompts[:batch_size]
6365

6466

@@ -405,7 +407,7 @@ def unbox_fn(x):
405407
)
406408

407409
# 10. Instantiate and invoke FlaxFlux2KleinPipeline
408-
print("Instantiating JAX FlaxFlux2KleinPipeline...")
410+
max_logging.log("Instantiating JAX FlaxFlux2KleinPipeline...")
409411
pipeline = FlaxFlux2KleinPipeline(
410412
transformer=transformer,
411413
vae=vae,

src/maxdiffusion/models/attention_flax.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,11 +1697,21 @@ def apply_rope(xq: Array, xk: Array, freqs_cis: Any) -> tuple[Array, Array]:
16971697
if isinstance(freqs_cis, (tuple, list)):
16981698
cos, sin = freqs_cis
16991699
if cos.ndim == 2:
1700-
cos = cos[None, :, None, :]
1701-
sin = sin[None, :, None, :]
1700+
seq_len = cos.shape[0]
1701+
if xq.ndim == 4 and xq.shape[2] == seq_len:
1702+
cos = cos[None, None, :, :]
1703+
sin = sin[None, None, :, :]
1704+
else:
1705+
cos = cos[None, :, None, :]
1706+
sin = sin[None, :, None, :]
17021707
elif cos.ndim == 3 and cos.shape[0] == 1:
1703-
cos = cos[:, :, None, :]
1704-
sin = sin[:, :, None, :]
1708+
seq_len = cos.shape[1]
1709+
if xq.ndim == 4 and xq.shape[2] == seq_len:
1710+
cos = cos[:, None, :, :]
1711+
sin = sin[:, None, :, :]
1712+
else:
1713+
cos = cos[:, :, None, :]
1714+
sin = sin[:, :, None, :]
17051715

17061716
def _rotate(x):
17071717
x_reshaped = x.reshape(*x.shape[:-1], -1, 2)

src/maxdiffusion/models/embeddings_flax.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .modeling_flax_utils import get_activation
2121
from ..models.attention_flax import NNXSimpleFeedForward
2222
from ..models.normalization_flax import FP32LayerNorm
23+
from maxdiffusion import max_logging
2324
from maxdiffusion.tpu_utils import get_tpu_type, TpuType
2425
from maxdiffusion.max_utils import safe_getattr
2526

@@ -296,7 +297,7 @@ def __call__(self, encoder_hidden_states_image: jax.Array) -> tuple[jax.Array, j
296297
# Apply pos_embed to the original sequence length
297298
hidden_states = hidden_states.at[:, :add_len, :].add(self.pos_embed.value[:, :add_len, :])
298299
if current_seq_len > pe_len:
299-
print(f"[WARN] Input seq_len {current_seq_len} > pos_embed len {pe_len}")
300+
max_logging.log(f"[WARN] Input seq_len {current_seq_len} > pos_embed len {pe_len}")
300301

301302
hidden_states = self.norm1(hidden_states)
302303
hidden_states = self.ff(hidden_states)

src/maxdiffusion/pipelines/flux/flux2klein_pipeline.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def _prepare_latents(self, config, batch_size, height, width):
110110
seed_val = getattr(config, "seed", None)
111111
if seed_val is None:
112112
seed_val = int(time.time()) & 0x7FFFFFFF
113-
print(
113+
max_logging.log(
114114
f"Generating random gaussian noise in unpacked space (32 channels) with seed: {seed_val} and shape: {latent_shape}..."
115115
)
116116
np.random.seed(seed_val)
@@ -215,7 +215,10 @@ def __call__(
215215
tokenizer = Qwen2TokenizerFast.from_pretrained(tokenizer_path, subfolder="tokenizer", local_files_only=True)
216216

217217
# Tokenize using deterministic explicit template string (version-agnostic across transformers versions)
218-
templated_texts = [f"<|im_start|>user\n{p}<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n" for p in prompts]
218+
templated_texts = [
219+
f"<|im_start|>user\n{p}<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\n"
220+
for p in prompts
221+
]
219222
max_logging.log(f"DEBUG: templated_texts[0] = {repr(templated_texts[0])}")
220223
inputs = tokenizer(templated_texts, return_tensors="np", padding="max_length", truncation=True, max_length=seq_len_txt)
221224
prompt_ids = jnp.array(inputs["input_ids"])
@@ -243,9 +246,7 @@ def __call__(
243246
# ---------------------------------------------------------------------
244247
# PHASE B: Denoising Loop (Flux Transformer)
245248
# ---------------------------------------------------------------------
246-
max_logging.log(
247-
f"[PHASE B] Running {num_inference_steps}-step E2E Denoising Loop on a batch of {batch_size} images..."
248-
)
249+
max_logging.log(f"[PHASE B] Running {num_inference_steps}-step E2E Denoising Loop on a batch of {batch_size} images...")
249250
t0 = time.perf_counter()
250251

251252
guidance_vec_val = None
@@ -270,9 +271,7 @@ def __call__(
270271

271272
# Print progress
272273
sigma_val = scheduler_state.sigmas[step_idx]
273-
max_logging.log(
274-
f" -> Step {step_idx}: Timestep = {scheduler_state.timesteps[step_idx]:.4f}, Sigma = {sigma_val:.4f}"
275-
)
274+
max_logging.log(f" -> Step {step_idx}: Timestep = {scheduler_state.timesteps[step_idx]:.4f}, Sigma = {sigma_val:.4f}")
276275

277276
latents_jax.block_until_ready()
278277

src/maxdiffusion/tests/generate_flux2klein_e2e_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def main():
156156
"python3",
157157
"src/maxdiffusion/generate_flux2klein.py",
158158
"src/maxdiffusion/configs/base_flux2klein.yml",
159+
"skip_jax_distributed_system=True",
159160
f"prompt={prompt}",
160161
"output_dir=/tmp/",
161162
f"seed={seed}",
@@ -175,14 +176,13 @@ def main():
175176
jax_4b_paths = [f"/tmp/jax_4b_dancing_b{b}.png" for b in range(batch_size)]
176177
print(f"[JAX 4B] Execution complete! Verifying outputs at: {jax_4b_paths}")
177178

179+
# =========================================================================
178180
# =========================================================================
179181
# PART II: FLUX.2-KLEIN-9B PARITY
180182
# =========================================================================
181183
print("\n" + "#" * 80)
182184
print("🎬 STARTING PARITY EVALUATION FOR FLUX.2-KLEIN-9B")
183185
print("#" * 80)
184-
185-
# 1. Run PyTorch 9B (FP32 & BF16)
186186
pt_9b_fp32_paths, pt_9b_bf16_paths = run_pytorch_pipeline(
187187
model_id="black-forest-labs/FLUX.2-klein-9B",
188188
prompt=prompt,
@@ -195,12 +195,12 @@ def main():
195195
prefix="9b",
196196
)
197197

198-
# 2. Run JAX 9B (via generate_flux2klein.py)
199198
print("\n[JAX 9B] Executing pipeline script generate_flux2klein.py...")
200199
cmd_jax_9b = [
201200
"python3",
202201
"src/maxdiffusion/generate_flux2klein.py",
203202
"src/maxdiffusion/configs/base_flux2klein_9B.yml",
203+
"skip_jax_distributed_system=True",
204204
f"prompt={prompt}",
205205
"output_dir=/tmp/",
206206
f"seed={seed}",

src/maxdiffusion/tests/generate_flux2klein_smoke_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,7 @@ def test_flux2klein_9b_smoke(self):
118118
ssim_compare = ssim(base_image, test_image, channel_axis=-1, data_range=255)
119119
print(f"\n[SMOKE TEST 9B] SSIM Score: {ssim_compare:.6f}")
120120
self.assertGreaterEqual(ssim_compare, 0.80)
121+
122+
123+
if __name__ == "__main__":
124+
unittest.main()
-1.93 KB
Loading

0 commit comments

Comments
 (0)