|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +import unittest |
| 19 | +import numpy as np |
| 20 | +from PIL import Image |
| 21 | +from skimage.metrics import structural_similarity as ssim |
| 22 | + |
| 23 | +class FluxKleinSmokeTest(unittest.TestCase): |
| 24 | + |
| 25 | + def setUp(self): |
| 26 | + self.output_dir = "/tmp/flux_smoke_test_output/" |
| 27 | + os.makedirs(self.output_dir, exist_ok=True) |
| 28 | + # Locate the local references directory |
| 29 | + self.ref_dir = os.path.join(os.path.dirname(os.path.abspath(__file__))) |
| 30 | + |
| 31 | + def test_flux_4b_smoke(self): |
| 32 | + """ |
| 33 | + Runs Flux.2-klein-4B generator end-to-end on TPU VM for a batch of 4 images |
| 34 | + at 1024x1024 resolution, and validates SSIM consistency against golden references. |
| 35 | + """ |
| 36 | + cmd = [ |
| 37 | + sys.executable, "src/maxdiffusion/generate_flux2klein.py", |
| 38 | + "src/maxdiffusion/configs/base_flux2klein.yml", |
| 39 | + "run_name=flux4b_smoke", |
| 40 | + "batch_size=4", |
| 41 | + "height=1024", |
| 42 | + "width=1024", |
| 43 | + "use_latents=False", |
| 44 | + f"output_dir={self.output_dir}", |
| 45 | + "prompt=A detailed vector illustration of a robotic hummingbird" |
| 46 | + ] |
| 47 | + print(f"\n[4B SMOKE] Running command: {' '.join(cmd)}") |
| 48 | + res = subprocess.run(cmd, capture_output=True, text=True) |
| 49 | + self.assertEqual(res.returncode, 0, f"4B Generation failed with error:\nSTDOUT:\n{res.stdout}\nSTDERR:\n{res.stderr}") |
| 50 | + |
| 51 | + print("[4B SMOKE] Generation complete. Verifying image SSIM values...") |
| 52 | + for b in range(4): |
| 53 | + gen_path = os.path.join(self.output_dir, f"flux2klein_generated_image_b{b}.png") |
| 54 | + ref_path = os.path.join(self.ref_dir, f"reference_flux4b_b{b}.png") |
| 55 | + |
| 56 | + self.assertTrue(os.path.exists(gen_path), f"Generated file not found: {gen_path}") |
| 57 | + self.assertTrue(os.path.exists(ref_path), f"Reference file not found: {ref_path}") |
| 58 | + |
| 59 | + gen_img = np.array(Image.open(gen_path)) |
| 60 | + ref_img = np.array(Image.open(ref_path)) |
| 61 | + |
| 62 | + val_ssim = ssim(gen_img, ref_img, channel_axis=-1) |
| 63 | + print(f" -> Batch {b} SSIM vs Reference: {val_ssim:.6f}") |
| 64 | + self.assertGreater(val_ssim, 0.8, f"SSIM for batch element {b} is below threshold: {val_ssim:.6f}") |
| 65 | + print("✅ Flux 4B Smoke Test Passed successfully!") |
| 66 | + |
| 67 | + def test_flux_9b_smoke(self): |
| 68 | + """ |
| 69 | + Runs Flux.2-klein-9B generator end-to-end on TPU VM for a batch of 4 images |
| 70 | + at 1024x1024 resolution, and validates SSIM consistency against golden references. |
| 71 | + """ |
| 72 | + cmd = [ |
| 73 | + sys.executable, "src/maxdiffusion/generate_flux2klein_9B.py", |
| 74 | + "src/maxdiffusion/configs/base_flux2klein_9B.yml", |
| 75 | + "run_name=flux9b_smoke", |
| 76 | + "batch_size=4", |
| 77 | + "height=1024", |
| 78 | + "width=1024", |
| 79 | + "use_latents=False", |
| 80 | + f"output_dir={self.output_dir}", |
| 81 | + "prompt=A detailed vector illustration of a robotic hummingbird" |
| 82 | + ] |
| 83 | + print(f"\n[9B SMOKE] Running command: {' '.join(cmd)}") |
| 84 | + res = subprocess.run(cmd, capture_output=True, text=True) |
| 85 | + self.assertEqual(res.returncode, 0, f"9B Generation failed with error:\nSTDOUT:\n{res.stdout}\nSTDERR:\n{res.stderr}") |
| 86 | + |
| 87 | + print("[9B SMOKE] Generation complete. Verifying image SSIM values...") |
| 88 | + for b in range(4): |
| 89 | + gen_path = os.path.join(self.output_dir, f"flux2klein_generated_image_b{b}.png") |
| 90 | + ref_path = os.path.join(self.ref_dir, f"reference_flux9b_b{b}.png") |
| 91 | + |
| 92 | + self.assertTrue(os.path.exists(gen_path), f"Generated file not found: {gen_path}") |
| 93 | + self.assertTrue(os.path.exists(ref_path), f"Reference file not found: {ref_path}") |
| 94 | + |
| 95 | + gen_img = np.array(Image.open(gen_path)) |
| 96 | + ref_img = np.array(Image.open(ref_path)) |
| 97 | + |
| 98 | + val_ssim = ssim(gen_img, ref_img, channel_axis=-1) |
| 99 | + print(f" -> Batch {b} SSIM vs Reference: {val_ssim:.6f}") |
| 100 | + self.assertGreater(val_ssim, 0.8, f"SSIM for batch element {b} is below threshold: {val_ssim:.6f}") |
| 101 | + print("✅ Flux 9B Smoke Test Passed successfully!") |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + unittest.main() |
0 commit comments