diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-06-28 07:17:33 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-06-28 07:17:33 +0200 |
| commit | 9807b449c66b06b708766753a3d38ea9022537d8 (patch) | |
| tree | 21488bfb28e9adf151c6acb861dde0f4e37d73b2 /examples | |
| parent | db5c2134ff03c459f226aeb17b00b659c90e0543 (diff) | |
| download | colabctl-9807b449c66b06b708766753a3d38ea9022537d8.tar.gz colabctl-9807b449c66b06b708766753a3d38ea9022537d8.zip | |
Add examples
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/a_hyper_detailed_ultra_fluffy_fantasy_cr.png | bin | 0 -> 534653 bytes | |||
| -rw-r--r-- | examples/torch_gpu_benchmark.py | 31 | ||||
| -rw-r--r-- | examples/txt2img.py | 38 |
3 files changed, 69 insertions, 0 deletions
diff --git a/examples/a_hyper_detailed_ultra_fluffy_fantasy_cr.png b/examples/a_hyper_detailed_ultra_fluffy_fantasy_cr.png Binary files differnew file mode 100644 index 0000000..b49a40f --- /dev/null +++ b/examples/a_hyper_detailed_ultra_fluffy_fantasy_cr.png diff --git a/examples/torch_gpu_benchmark.py b/examples/torch_gpu_benchmark.py new file mode 100644 index 0000000..777ea3b --- /dev/null +++ b/examples/torch_gpu_benchmark.py @@ -0,0 +1,31 @@ +import torch + +assert torch.cuda.is_available(), "no CUDA device" +dev = torch.device("cuda") +print("Device:", torch.cuda.get_device_name(0)) +print("Torch :", torch.__version__) + +# Measured matrix-multiply throughput in TFLOP/s. +n = 4096 +a = torch.randn(n, n, device=dev, dtype=torch.float32) +b = torch.randn(n, n, device=dev, dtype=torch.float32) + +# Warm up so the timed loop excludes one-time CUDA setup. +for _ in range(3): + a @ b +torch.cuda.synchronize() + +start = torch.cuda.Event(enable_timing=True) +end = torch.cuda.Event(enable_timing=True) + +iters = 20 +start.record() +for _ in range(iters): + c = a @ b +end.record() +torch.cuda.synchronize() + +ms = start.elapsed_time(end) / iters +flops = 2 * n ** 3 # one multiply and one add per output element +tflops = flops / (ms * 1e-3) / 1e12 +print(f"{n}x{n} matmul: {ms:.2f} ms/iter, {tflops:.1f} TFLOP/s (fp32)") diff --git a/examples/txt2img.py b/examples/txt2img.py new file mode 100644 index 0000000..0ad5e6d --- /dev/null +++ b/examples/txt2img.py @@ -0,0 +1,38 @@ +"""Generate an image from a text prompt with SD-Turbo. + +Pipe this to `colabctl exec ID --accelerator T4 -o examples`. The +generated PNG streams back through display() and is saved on your machine. +""" + +import re + +import torch +from IPython.display import display + +PROMPT = "A hyper-detailed, ultra-fluffy fantasy creature sitting in a glowing mossy forest hollow. The creature is a mix between a baby red panda and a tiny cloud, with oversized, sparkling cosmic violet eyes, floppy bunny ears, and a soft, pastel-pink and cream fur coat that looks impossibly plush. It is holding a tiny, glowing star-shaped wildflower in its small paws, looking up with an innocent, curious expression. Magical fireflies and soft, warm bokeh lights float in the background. Whimsical, cinematic lighting, 8k resolution, photorealistic fur texture, enchanted forest aesthetic, Pixar-meets-Studio-Ghibli style." + +# Load once, then keep the pipeline in the kernel for subsequent runs. +if "pipe" not in globals(): + import subprocess + import sys + + subprocess.run( + [sys.executable, "-m", "pip", "install", "-q", + "diffusers", "transformers", "accelerate", "safetensors"], + check=True, + ) + from diffusers import AutoPipelineForText2Image + + pipe = AutoPipelineForText2Image.from_pretrained( + "stabilityai/sd-turbo", + torch_dtype=torch.float16, + variant="fp16", + ).to("cuda") + print("Model loaded.") + +# SD-Turbo is a distilled model: one or two steps and no classifier-free guidance. +image = pipe(prompt=PROMPT, num_inference_steps=2, guidance_scale=0.0).images[0] + +slug = re.sub(r"[^a-z0-9]+", "_", PROMPT.lower())[:40].strip("_") or "image" +display(image, metadata={"filename": f"{slug}.png"}) +print("Generated:", PROMPT) |
