Instructions to use OzzyGT/ideogram4_custom_blocks with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Diffusers
How to use OzzyGT/ideogram4_custom_blocks with Diffusers:
pip install -U diffusers transformers accelerate
import torch from diffusers import DiffusionPipeline # switch to "mps" for apple devices pipe = DiffusionPipeline.from_pretrained("OzzyGT/ideogram4_custom_blocks", dtype=torch.bfloat16, device_map="cuda") prompt = "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k" image = pipe(prompt).images[0] - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- Draw Things
- DiffusionBee
Delete example_img2img.py
Browse files- example_img2img.py +0 -129
example_img2img.py
DELETED
|
@@ -1,129 +0,0 @@
|
|
| 1 |
-
"""Run the Ideogram4 img2img custom blocks on the SDNQ 4-bit checkpoint with diffusers-mm.
|
| 2 |
-
|
| 3 |
-
Loads the standard SDNQ-4bit pipeline (to get the quantized components + diffusers-mm memory
|
| 4 |
-
management), then builds `Ideogram4Img2ImgAutoBlocks` and injects those same module objects into
|
| 5 |
-
the modular pipeline. The diffusers-mm offload hooks live on the modules, so they keep working when
|
| 6 |
-
the modular pipeline drives the forwards.
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
import sys
|
| 10 |
-
|
| 11 |
-
import torch
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Make the custom-blocks package importable when run from anywhere.
|
| 15 |
-
sys.path.insert(0, r"C:\projects\modular_diffusers")
|
| 16 |
-
|
| 17 |
-
from sdnq import SDNQConfig # noqa: E402,F401 (registers sdnq into diffusers/transformers)
|
| 18 |
-
from sdnq.common import use_torch_compile as triton_is_available # noqa: E402
|
| 19 |
-
from sdnq.loader import apply_sdnq_options_to_model # noqa: E402
|
| 20 |
-
|
| 21 |
-
import diffusers # noqa: E402
|
| 22 |
-
from diffusers.utils import load_image # noqa: E402
|
| 23 |
-
from ideogram4_custom_blocks import Ideogram4Img2ImgAutoBlocks # noqa: E402
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
MODEL_ID = "Disty0/Ideogram-4-SDNQ-4bit-dynamic-hadamard"
|
| 27 |
-
HEIGHT = 1024
|
| 28 |
-
WIDTH = 1024
|
| 29 |
-
STEPS = 20
|
| 30 |
-
STRENGTH = 0.6
|
| 31 |
-
SEED = 0
|
| 32 |
-
REFERENCE = r"C:\projects\python\diffusers\ideogram4-sdnq-4bit-diffusers_mm.png"
|
| 33 |
-
|
| 34 |
-
# Warm sunset re-color of the original cool-blue sailboat scene; strength=0.6 keeps the composition.
|
| 35 |
-
PROMPT = """
|
| 36 |
-
{
|
| 37 |
-
"high_level_description": "A cinematic 35mm film photograph of a lone wooden sailboat on a glassy lake at golden-hour sunset, warm amber light, the boat on a right-third vertical with the horizon at the lower third.",
|
| 38 |
-
"style_description": {
|
| 39 |
-
"aesthetics": "Cinematic, minimal, serene, warm and glowing.",
|
| 40 |
-
"lighting": "Warm golden-hour sun low at the horizon casting long amber reflections; soft and glowing.",
|
| 41 |
-
"photo": "35mm motion-picture film still, 16:9 framing, subtle grain, warm saturated tones.",
|
| 42 |
-
"medium": "Photograph.",
|
| 43 |
-
"color_palette": ["#E8912F", "#F4C77B"]
|
| 44 |
-
},
|
| 45 |
-
"compositional_deconstruction": {
|
| 46 |
-
"background": "Windless golden-hour evening on a wide lake; horizon at the lower third. Warm amber-orange sky with a glowing sun at the horizon and a bright gold streak across the glassy water. Subtle 35mm grain.",
|
| 47 |
-
"elements": [
|
| 48 |
-
{ "type": "obj", "bbox": [380, 590, 660, 720],
|
| 49 |
-
"desc": "Lone wooden sailboat on the right-third vertical, dark varnished hull and a single tall mast with a slack white sail. No visible crew." },
|
| 50 |
-
{ "type": "obj",
|
| 51 |
-
"desc": "Mirror-perfect reflection of the sailboat doubling straight down into the glassy amber water beneath the hull." }
|
| 52 |
-
]
|
| 53 |
-
}
|
| 54 |
-
}
|
| 55 |
-
"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def main():
|
| 59 |
-
pipe_std = diffusers.Ideogram4Pipeline.from_pretrained(MODEL_ID, torch_dtype=torch.bfloat16)
|
| 60 |
-
|
| 61 |
-
if triton_is_available and (torch.cuda.is_available() or torch.xpu.is_available()):
|
| 62 |
-
pipe_std.transformer = apply_sdnq_options_to_model(pipe_std.transformer, use_quantized_matmul=True)
|
| 63 |
-
pipe_std.unconditional_transformer = apply_sdnq_options_to_model(
|
| 64 |
-
pipe_std.unconditional_transformer, use_quantized_matmul=True
|
| 65 |
-
)
|
| 66 |
-
pipe_std.text_encoder = apply_sdnq_options_to_model(pipe_std.text_encoder, use_quantized_matmul=True)
|
| 67 |
-
|
| 68 |
-
# diffusers-mm smart memory management; hooks are installed on the module objects.
|
| 69 |
-
from diffusers_mm import managed
|
| 70 |
-
|
| 71 |
-
pipe_std = managed(pipe_std, strategy="auto", device="cuda")
|
| 72 |
-
print(f"diffusers-mm applied strategy: {getattr(pipe_std.mm, 'applied_strategy', None)}")
|
| 73 |
-
|
| 74 |
-
# Build the modular pipeline from the custom img2img blocks and inject the managed components.
|
| 75 |
-
pipe = Ideogram4Img2ImgAutoBlocks().init_pipeline()
|
| 76 |
-
pipe.update_components(
|
| 77 |
-
text_encoder=pipe_std.text_encoder,
|
| 78 |
-
tokenizer=pipe_std.tokenizer,
|
| 79 |
-
transformer=pipe_std.transformer,
|
| 80 |
-
unconditional_transformer=pipe_std.unconditional_transformer,
|
| 81 |
-
vae=pipe_std.vae,
|
| 82 |
-
scheduler=pipe_std.scheduler,
|
| 83 |
-
)
|
| 84 |
-
|
| 85 |
-
# The blocks create latents/move inputs onto `components._execution_device`; managed onloads the
|
| 86 |
-
# modules to cuda per-forward, so pin the target device to cuda for the modular run.
|
| 87 |
-
if torch.cuda.is_available():
|
| 88 |
-
type(pipe)._execution_device = property(lambda self: torch.device("cuda"))
|
| 89 |
-
print("modular execution device:", pipe._execution_device)
|
| 90 |
-
|
| 91 |
-
reference = load_image(REFERENCE)
|
| 92 |
-
|
| 93 |
-
if torch.cuda.is_available():
|
| 94 |
-
torch.cuda.reset_peak_memory_stats()
|
| 95 |
-
|
| 96 |
-
# text2image (no `image`) -> resolves to the text2image workflow.
|
| 97 |
-
t2i = pipe(
|
| 98 |
-
prompt=PROMPT,
|
| 99 |
-
height=HEIGHT,
|
| 100 |
-
width=WIDTH,
|
| 101 |
-
num_inference_steps=STEPS,
|
| 102 |
-
guidance_schedule=[7.0] * STEPS,
|
| 103 |
-
generator=torch.manual_seed(SEED),
|
| 104 |
-
output="images",
|
| 105 |
-
)[0]
|
| 106 |
-
t2i.save("ideogram4-modular-t2i.png")
|
| 107 |
-
print("saved ideogram4-modular-t2i.png")
|
| 108 |
-
|
| 109 |
-
# image2image (`image` provided) -> resolves to the img2img workflow.
|
| 110 |
-
img = pipe(
|
| 111 |
-
prompt=PROMPT,
|
| 112 |
-
image=reference,
|
| 113 |
-
strength=STRENGTH,
|
| 114 |
-
height=HEIGHT,
|
| 115 |
-
width=WIDTH,
|
| 116 |
-
num_inference_steps=STEPS,
|
| 117 |
-
guidance_schedule=[7.0] * STEPS,
|
| 118 |
-
generator=torch.manual_seed(SEED),
|
| 119 |
-
output="images",
|
| 120 |
-
)[0]
|
| 121 |
-
img.save("ideogram4-modular-img2img.png")
|
| 122 |
-
print("saved ideogram4-modular-img2img.png")
|
| 123 |
-
|
| 124 |
-
if torch.cuda.is_available():
|
| 125 |
-
print(f"peak VRAM: {torch.cuda.max_memory_allocated() / 1024**3:.2f} GB")
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
if __name__ == "__main__":
|
| 129 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|