linoyts HF Staff commited on
Commit
6bbf35c
·
verified ·
1 Parent(s): 4c8a1ed

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ package/submodules/injection/package.pt2 filter=lfs diff=lfs merge=lfs -text
37
+ package/submodules/plain/package.pt2 filter=lfs diff=lfs merge=lfs -text
38
+ package/submodules/refiner/package.pt2 filter=lfs diff=lfs merge=lfs -text
39
+ samples/aoti.webp filter=lfs diff=lfs merge=lfs -text
40
+ samples/eager.webp filter=lfs diff=lfs merge=lfs -text
config.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "base": "diffusers-internal-dev/Fibo-2.0-dev",
3
+ "arch": "sm120",
4
+ "torch": "2.11.0+cu130",
5
+ "cuda": "13.0",
6
+ "device": "NVIDIA RTX PRO 6000 Blackwell Server Edition",
7
+ "resolution": 1024,
8
+ "steps": 8,
9
+ "guidance": 5.0,
10
+ "injection_layer_ids": [
11
+ 6,
12
+ 14,
13
+ 22,
14
+ 30,
15
+ 38
16
+ ],
17
+ "n_layers": 44,
18
+ "n_refiner_layers": 2,
19
+ "stacks": {
20
+ "plain": "dynamic",
21
+ "injection": "dynamic",
22
+ "refiner": "dynamic"
23
+ },
24
+ "stack_targets": {
25
+ "plain": "layers[i] for i not in injection_layer_ids",
26
+ "injection": "layers[i] for i in injection_layer_ids",
27
+ "refiner": "noise_refiner[*]"
28
+ },
29
+ "errors": {}
30
+ }
job.py ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # /// script
2
+ # requires-python = ">=3.11"
3
+ # dependencies = [
4
+ # "aokit",
5
+ # "torch==2.11.0",
6
+ # "torchvision==0.26.0",
7
+ # "diffusers @ https://github.com/muhammadali-afridi/diffusers/archive/60f95274bf07ed705d675eb09d99b6ae125b601e.tar.gz",
8
+ # "transformers==5.20.1",
9
+ # "accelerate",
10
+ # "safetensors",
11
+ # "sentencepiece",
12
+ # "huggingface_hub>=1.0",
13
+ # "Pillow",
14
+ # ]
15
+ #
16
+ # [[tool.uv.index]]
17
+ # url = "https://download.pytorch.org/whl/cu130"
18
+ # ///
19
+ #
20
+ # Ahead-of-time compile the repeated transformer blocks of BriaFibo2Transformer2DModel
21
+ # with aokit (regional compilation), verify correctness, and push a PRIVATE package repo.
22
+ #
23
+ # Launch:
24
+ # hf jobs uv run job_fibo2_aoti.py \
25
+ # --flavor rtx-pro-6000 \
26
+ # --image pytorch/pytorch:2.9.1-cuda13.0-cudnn9-devel \
27
+ # --namespace diffusers-internal-dev \
28
+ # --secrets HF_TOKEN \
29
+ # -e FIBO2_MODEL=diffusers-internal-dev/Fibo-2.0-dev \
30
+ # -e OUTPUT_REPO_ID=diffusers-internal-dev/Fibo-2.0-dev-aoti-sm120 \
31
+ # -e STEPS=8
32
+
33
+ import inspect
34
+ import json
35
+ import os
36
+ import time
37
+ import traceback
38
+ from pathlib import Path
39
+
40
+ import torch
41
+ from huggingface_hub import HfApi
42
+ from PIL import Image
43
+
44
+ import aokit
45
+ from aokit.exporting import capture
46
+
47
+ MODEL = os.environ.get("FIBO2_MODEL", "diffusers-internal-dev/Fibo-2.0-dev")
48
+ OUT_REPO = os.environ["OUTPUT_REPO_ID"]
49
+ TOKEN = os.environ.get("HF_TOKEN")
50
+ STEPS = int(os.environ.get("STEPS", "8"))
51
+ GUIDANCE = float(os.environ.get("GUIDANCE", "5.0"))
52
+ PROMPT = os.environ.get("PROMPT", "a red bicycle leaning against a white brick wall on a sunny morning.")
53
+ H = W = 1024
54
+ DTYPE = torch.bfloat16
55
+
56
+ print(f"torch {torch.__version__} | cuda {torch.version.cuda} | device {torch.cuda.get_device_name()}", flush=True)
57
+
58
+ from diffusers import BriaFibo2Pipeline
59
+
60
+ pipe = BriaFibo2Pipeline.from_pretrained(MODEL, dtype=DTYPE, token=TOKEN).to("cuda")
61
+ pipe.set_progress_bar_config(disable=True)
62
+ t = pipe.transformer
63
+ INJ = list(getattr(t.config, "injection_layer_ids", (6, 14, 22, 30, 38)))
64
+ n_layers = len(t.layers)
65
+ plain_idx = next(i for i in range(n_layers) if i not in INJ)
66
+ inj_idx = INJ[0]
67
+ print(f"n_layers={n_layers} injection={INJ} plain_rep={plain_idx} inj_rep={inj_idx}", flush=True)
68
+
69
+
70
+ def gen(path, label):
71
+ img = pipe(PROMPT, num_inference_steps=STEPS, guidance_scale=GUIDANCE,
72
+ height=H, width=W, generator=torch.manual_seed(0)).images[0]
73
+ img.save(path)
74
+ print(f"sample[{label}] -> {path}", flush=True)
75
+ return img
76
+
77
+
78
+ def export_block(block, name):
79
+ """Capture inputs for `block` from a real pipeline step, then export with the
80
+ sequence axes marked dynamic (batch kept static at the captured size)."""
81
+ with capture(block) as call:
82
+ pipe(PROMPT, num_inference_steps=1, guidance_scale=GUIDANCE,
83
+ height=H, width=W, generator=torch.manual_seed(0))
84
+ # Preserve the EXACT calling convention the transformer uses (positional args stay
85
+ # positional) so the saved graph's in_spec matches the runtime block(...) call.
86
+ args = tuple(call.args)
87
+ kwargs = dict(call.kwargs)
88
+ params = list(inspect.signature(block.forward).parameters)
89
+ bound = {}
90
+ for pname, val in zip(params, args): # name the positional args for spec + dynamic_shapes
91
+ bound[pname] = val
92
+ bound.update(kwargs)
93
+ x = bound["x"]
94
+ B, S = x.shape[0], x.shape[1]
95
+ enc = bound.get("encoder_hidden_states")
96
+ E = enc.shape[1] if torch.is_tensor(enc) else None
97
+ print(f" [{name}] captured B={B} S={S} E={E} "
98
+ f"args={[type(a).__name__ for a in args]} kw={[k for k in kwargs]}", flush=True)
99
+
100
+ seq = torch.export.Dim("seq", min=16, max=16384)
101
+ enc_dim = torch.export.Dim("enc", min=16, max=16384) if E and E != S else None
102
+ size_to_dim = {S: seq}
103
+ if enc_dim is not None:
104
+ size_to_dim[E] = enc_dim
105
+
106
+ def spec(v):
107
+ if not torch.is_tensor(v):
108
+ return None
109
+ d = {i: size_to_dim[sz] for i, sz in enumerate(v.shape) if i != 0 and sz in size_to_dim}
110
+ return d or None
111
+
112
+ # dynamic_shapes keyed by argument name (torch.export maps names to args+kwargs).
113
+ dynamic_shapes = {pname: spec(val) for pname, val in bound.items()}
114
+ with torch.no_grad():
115
+ ep = torch.export.export(block, args, kwargs, dynamic_shapes=dynamic_shapes)
116
+ return ep
117
+
118
+
119
+ STACKS = {
120
+ "plain": t.layers[plain_idx],
121
+ "injection": t.layers[inj_idx],
122
+ "refiner": t.noise_refiner[0],
123
+ }
124
+
125
+ api = HfApi(token=TOKEN)
126
+ work = Path("/tmp/fibo2_aoti")
127
+ pkg = work / "package"
128
+ samples = work / "samples"
129
+ pkg.mkdir(parents=True, exist_ok=True)
130
+ samples.mkdir(parents=True, exist_ok=True)
131
+
132
+ gen(samples / "eager.webp", "eager")
133
+
134
+ compiled = {}
135
+ errors = {}
136
+ for name, block in STACKS.items():
137
+ print(f"=== export+compile stack '{name}' ===", flush=True)
138
+ try:
139
+ t0 = time.perf_counter()
140
+ ep = export_block(block, name)
141
+ aokit.compile_and_save(package_dir=str(pkg), exported_program=ep, submodule=name)
142
+ compiled[name] = True
143
+ print(f" OK '{name}' in {time.perf_counter()-t0:.0f}s", flush=True)
144
+ except Exception as e:
145
+ errors[name] = repr(e)
146
+ print(f" FAILED '{name}': {e!r}", flush=True)
147
+ traceback.print_exc()
148
+
149
+ if not compiled:
150
+ raise RuntimeError(f"No stack compiled. Errors: {errors}")
151
+
152
+ # Load compiled stacks back with a per-index custom mapping and verify.
153
+ from aokit import LazyAOTIModel, patch
154
+
155
+ models = {name: LazyAOTIModel(pkg / "submodules" / name / "package.pt2") for name in compiled}
156
+ if "plain" in models or "injection" in models:
157
+ for i, blk in enumerate(t.layers):
158
+ m = models.get("injection") if i in INJ else models.get("plain")
159
+ if m is not None:
160
+ patch(blk, m)
161
+ if "refiner" in models:
162
+ for blk in t.noise_refiner:
163
+ patch(blk, models["refiner"])
164
+
165
+ gen(samples / "aoti.webp", "aoti")
166
+
167
+ cap = "".join(map(str, torch.cuda.get_device_capability()))
168
+ cu = "".join((torch.version.cuda or "0.0").split(".")[:2])
169
+ config = {
170
+ "base": MODEL,
171
+ "arch": f"sm{cap}",
172
+ "torch": torch.__version__,
173
+ "cuda": torch.version.cuda,
174
+ "device": torch.cuda.get_device_name(),
175
+ "resolution": H,
176
+ "steps": STEPS,
177
+ "guidance": GUIDANCE,
178
+ "injection_layer_ids": INJ,
179
+ "n_layers": n_layers,
180
+ "n_refiner_layers": len(t.noise_refiner),
181
+ "stacks": {name: "dynamic" for name in compiled},
182
+ "stack_targets": {
183
+ "plain": "layers[i] for i not in injection_layer_ids",
184
+ "injection": "layers[i] for i in injection_layer_ids",
185
+ "refiner": "noise_refiner[*]",
186
+ },
187
+ "errors": errors,
188
+ }
189
+ (work / "config.json").write_text(json.dumps(config, indent=2))
190
+ print("CONFIG:\n" + json.dumps(config, indent=2), flush=True)
191
+
192
+ # self-include this script
193
+ import shutil
194
+ main_file = getattr(__import__("__main__"), "__file__", None)
195
+ if main_file:
196
+ shutil.copyfile(main_file, work / "job.py")
197
+
198
+ api.create_repo(OUT_REPO, repo_type="model", private=True, exist_ok=True)
199
+ api.upload_folder(repo_id=OUT_REPO, folder_path=str(work), repo_type="model")
200
+ print(f"DONE -> https://huggingface.co/{OUT_REPO} (private) compiled={list(compiled)} errors={list(errors)}", flush=True)
package/submodules/injection/package.pt2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:102cf5098dae0d2b9ed7798a35aa0a2be3a8abb89cbe9bcedf8ae880c371db38
3
+ size 901648
package/submodules/plain/package.pt2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dae7509af8209dc9644d6f219b13c11b7b1cccdcff349b3f6c188b85f6b8e39b
3
+ size 752187
package/submodules/refiner/package.pt2 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bd40634be42dbad43167eb1bdc50f5ab3a5fc413e61b5c089e9a9686c18dcfa0
3
+ size 740155
samples/aoti.webp ADDED

Git LFS Details

  • SHA256: 4790d19a7035d31a8fa836d193590c98ee06502a74045d2015563c5a89589069
  • Pointer size: 131 Bytes
  • Size of remote file: 136 kB
samples/eager.webp ADDED

Git LFS Details

  • SHA256: d1763671c34ef9c8709e42060305c33b5a2e4062b5c02223bb049a8f60d27f54
  • Pointer size: 131 Bytes
  • Size of remote file: 135 kB