AEUPH commited on
Commit
5b9f80f
·
verified ·
1 Parent(s): 1b0d3ad

Document ZeroGPU offload: which endpoints run on the GPU and how to call them

Browse files
Files changed (1) hide show
  1. README.md +203 -97
README.md CHANGED
@@ -1,110 +1,216 @@
1
  ---
2
- title: MOSS4GB ZeroGPU — NF4 4-bit + bank trainer
3
- emoji: 🐏
4
- colorFrom: green
5
- colorTo: indigo
6
- sdk: gradio
7
- app_file: app.py
8
- pinned: false
9
  license: apache-2.0
10
- models:
11
- - OpenMOSS-Team/MOSS-Image-SFT
12
- - AEUPH/moss4gb
13
- short_description: MOSS-Image 4-bit NF4 DiT + bank trainer + MCP
14
- suggested_hardware: zero-a10g
 
 
 
 
 
15
  ---
16
 
17
- # MOSS4GB on ZeroGPU — every component is a selling point
18
-
19
- A Space that is also an MCP server, running the **MOSS-Image-SFT** 5B DiT
20
- (`OpenMOSS-Team/MOSS-Image-SFT`, Apache-2.0) through the **4-bit packaging
21
- published at [`AEUPH/moss4gb`](https://huggingface.co/AEUPH/moss4gb)**.
22
-
23
- ## Unique selling points
24
-
25
- 1. **5B-class DiT at 2.77 GB VRAM.** The transformer runs as bitsandbytes
26
- **NF4** weights with fp16 compute — the entire 5.08 B-parameter image model
27
- sits in the memory budget of a small waterfall game, and the same packaging
28
- runs on a real 4 GB GTX 1050 Ti locally.
29
- 2. **No text encoder in the render path.** The 9B Qwen3.5 encoder (16.78 GB in
30
- bf16) is *absent from inference*: prompts come from an embedding bank.
31
- That is what the 4 GB build proved out, and this Space keeps the design.
32
- 3. **The bank trainer is built in.** ZeroGPU has GPU room, so instead of "your
33
- prompt must be in the bank", the Space encodes any new prompt **on the fly**
34
- with the resident Qwen3.5-9B — one extra encoder pass, then the entry stays
35
- for every later call on any machine.
36
- 4. **Take your bank home.** `bank_train`'s output is a standard
37
- `bank.safetensors` (entries keyed by prompt-hash, fp16 `[S, 4096]` + masks)
38
- opened exactly by `python -m moss4gb bank import --source <dir>` on the
39
- 4 GB box — a remote bank farm for a local renderer.
40
- 5. **A pickled NF4 load cache, produced and verified on real hardware.** The
41
- 10.5-minute streaming quantize was paid for **once**, on a 1050 Ti; the
42
- Space and every user load the twoishes-minute artifact instead
43
- (`cache/dit_nf4_float16-*.pt` in [`AEUPH/moss4gb`](https://huggingface.co/AEUPH/moss4gb)).
44
- 6. **Determinism by contract.** Same prompt + size + seed ⇒ byte-identical
45
- image, since the conditioning and the sampling are both fixed.
46
- 7. **Measured, not assumed.** The 4 GB numbers on the model card (2.77 GB
47
- resident, 2.90 GB peak, 2.91 s/step at 256² cfg 1, CPU decode bit-identical
48
- to upstream) were measured, not predicted; the Space reuses the same nf4
49
- skip-list and chat-template handling that passed a 26-test suite.
50
- 8. **MCP from minute one.** `generate`, `bank_train`, `bank_export` and
51
- `status` are each an MCP tool at `<url>/gradio_api/mcp/`, with descriptions
52
- written for agents (ranges, defaults, failure modes, quota).
53
-
54
- ## Hardware and quota
55
-
56
- Hardware: **ZeroGPU** (choose it in Settings → Hardware; `suggested_hardware`
57
- here is only a hint). Visitor quota applies (2 min/day unauthenticated,
58
- 5 min/day free, 40 min/day PRO), and the reservation is dynamic: a small
59
- 512²/20-step/cfg-3 render reserves well under 120 s; a brand-new prompt adds
60
- one encoder pass to the reservation, once.
61
-
62
- ## Where each piece runs
63
-
64
- | Piece | Where | Footprint |
65
  |---|---|---|
66
- | NF4 DiT cache (pickled, 2.77 GB) | `AEUPH/moss4gb` repo | downloaded once |
67
- | DiT at runtime | GPU (staged per call), NF4/fp16 | 2.77 GB VRAM |
68
- | VAE | GPU, fp32 | ~0.5 GB VRAM |
69
- | Qwen3.5-9B encoder | GPU, bf16, **only for bank builds** | ~16.8 GB VRAM |
70
- | Prompt bank | repo `assets/bank` + container disk | ~300 KB/entry |
71
-
72
- ## MCP and API
73
-
74
- Config for any MCP client:
75
-
76
- ```jsonc
77
- {
78
- "mcpServers": {
79
- "moss4gb": {
80
- "url": "https://AEUPH-moss4gb.hf.space/gradio_api/mcp/",
81
- "transport": "streamable_http"
82
- }
83
- }
84
- }
85
- ```
86
 
87
- | Tool | Purpose |
88
  |---|---|
89
- | `generate` | 4-bit render; returns image + metadata JSON (entry id, tokens, s/step, peak VRAM) |
90
- | `bank_train` | encode prompts (one per line) into the bank |
91
- | `bank_export` | download the current `bank.safetensors` |
92
- | `status` | pipeline state, precision and VRAM accounting per component |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
- Local development: `pip install -r requirements.txt && python app.py`
95
- (without `spaces` the decorator is a passthrough; on CPU it is correct but slow).
96
 
97
- ## Licence
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
- Apache-2.0, matching upstream `OpenMOSS-Team/MOSS-Image-SFT`. The Qwen3.5-9B
100
- chat template used by the trainer ships from `Qwen/Qwen3.5-9B` under its own
101
- licence.
 
 
 
 
 
102
 
103
  ## Trainable lane — one image in, one LoRA out
104
 
105
- The same project already ships a **single-image trainer** (one source image ->
106
- one 3 MB LoRA, resumable, VRAM-planned for a 4 GB card) and a **measured run**
107
- where a trained adapter went from RGB confetti to a prompt-bound subject once the
108
- delta multiplier was fixed to `alpha / r`. The full recipe, the numbers and the
109
- two bugs worth never repeating are in
110
- [TRAINABLE-REFERENCE.md](https://huggingface.co/spaces/AEUPH/moss4gb/blob/main/TRAINABLE-REFERENCE.md).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
 
 
 
 
 
 
 
2
  license: apache-2.0
3
+ base_model: OpenMOSS-Team/MOSS-Image-SFT
4
+ library_name: diffusers
5
+ pipeline_tag: text-to-image
6
+ tags:
7
+ - text-to-image
8
+ - low-vram
9
+ - 4-bit
10
+ - nf4
11
+ - quantization
12
+ - diffusers
13
  ---
14
 
15
+ # MOSS4GB — MOSS-Image-SFT on 4 GB cards (5B DiT, 4-bit, no text encoder)
16
+
17
+ Run `MOSS-Image-SFT` on a 4 GB card with the 9B text encoder never resident.
18
+
19
+ | Piece | Where it runs | Footprint |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  |---|---|---|
21
+ | DiT `MOSSImageTransformer` | GPU, NF4 4-bit | ~2.6 GB |
22
+ | Text encoder `Qwen3.5-9B` | nowhere at inference — replaced by cached embeddings | 0 |
23
+ | VAE decode | CPU, fp32 | 250 MB RAM |
24
+ | Latents / sampling state | GPU, fp32 | <10 MB |
25
+
26
+ The DiT is 5.08 B parameters (10.16 GB bf16) and the encoder is 8.39 B
27
+ (16.78 GB bf16). Only the encoder is genuinely impossible on this card, so the
28
+ pipeline is built around deleting it from the runtime path.
29
+
30
+ ## Measured on a 1050 Ti (4 GB, sm_61, Windows)
 
 
 
 
 
 
 
 
 
 
31
 
32
+ | Metric | Value |
33
  |---|---|
34
+ | DiT load, streaming from disk | 349-513 s (I/O bound, drive dependent) |
35
+ | VRAM resident | 2.77 GB (262 Linear NF4, 7 fp16, 5.08 B params) |
36
+ | 256x256, cfg 1 | 2.91 s/step |
37
+ | 256x256, cfg 3 (two forwards/step) | 13.8 s/step |
38
+ | Peak VRAM at 256x256 | 2.90 GB |
39
+ | VAE decode 256x256 / 512x512, CPU | 7.8-28.5 s / 51.6 s |
40
+ | Decode vs upstream pipeline | max abs diff 0.0 |
41
+
42
+ Consequence for real use: a 20-step 256x256 render at cfg 3 is about 4.5
43
+ minutes of sampling on top of the load. Keep a warm process if you are
44
+ iterating; the load-time NF4 cache (`<model-dir>/.moss4gb-cache/`) is written on
45
+ the first `generate` and read back in about a minute after that.
46
+
47
+ ## Install from this repo
48
+
49
+ ```bash
50
+ hf download AEUPH/moss4gb --local-dir moss4gb-hf
51
+ cd moss4gb-hf
52
+ pip install torch diffusers>=0.38 'bitsandbytes>=0.49' 'transformers>=5.3' safetensors huggingface_hub
53
+ # weights come from the upstream repo (transformer + vae only; ~9.7 GB)
54
+ hf download OpenMOSS-Team/MOSS-Image-SFT --include 'transformer/*' --include 'vae/*' --include 'pipeline_moss_image.py' --include 'scheduler/*' --local-dir models/MOSS-Image-SFT
55
+ python -m moss4gb doctor
56
+ ```
57
+
58
+ ## Why this works at all
59
+
60
+ `pipeline_moss_image.py` already declares
61
+ `_optional_components = ['text_encoder', 'tokenizer', 'vae']` and accepts
62
+ `prompt_embeds: [B, S, 4096]` instead of a string. The transformer only
63
+ requires `S >= 1`. So the encoder's *outputs* are the real interface, and they
64
+ are 300 KB each.
65
+
66
+ ## Quick start
67
+
68
+ ```bash
69
+ # 1. weights (~10.5 GB; the 17 GB text encoder is only needed by `bank build`)
70
+ python -m moss4gb doctor
71
+
72
+ # 2. a placeholder bank so the path is runnable immediately (no conditioning)
73
+ python -m moss4gb bank null
74
+
75
+ # 3. render (256 to stay small; 512 needs the card almost entirely free)
76
+ python -m moss4gb generate --height 256 --width 256 --steps 20 --seed 42 --out out/first.png
77
+ ```
78
+
79
+ Commands here are written as single lines on purpose: PowerShell's line
80
+ continuation is a backtick, not a trailing `\`, so a bash-style `\` becomes a
81
+ literal argument (`error: unrecognized arguments: \`).
82
+
83
+ The null bank produces an unconditioned sample: useful for verifying VRAM,
84
+ speed and that the VAE decode is correct, not for judging quality. With a
85
+ one-token zero embedding the model produces structured noise, and because the
86
+ seed and the conditioning are both constant the repeat is byte-identical.
87
+
88
+ ## Building a real prompt bank
89
+
90
+ This is the only step that does not fit on a 4 GB card. Full runbook:
91
+ [`docs/BUILD_BANK_GPU.md`](docs/BUILD_BANK_GPU.md).
92
+
93
+ ```bash
94
+ python -m moss4gb bank build --device cuda --dtype bfloat16 --prompts-file prompts/characters.txt
95
+ python -m moss4gb bank build --device cuda --dtype bfloat16 --load-in-8bit --prompt "a woman with pink hair"
96
+ ```
97
+
98
+ The DiT consumes Qwen3.5-9B hidden states, so a real bank needs that encoder
99
+ **once**. It needs `transformers>=5.3` and roughly 20 GB of RAM (or a GPU with
100
+ ~12 GB free), and 17 GB of disk. That does not fit on the 4 GB / 16 GB target
101
+ box, so build it elsewhere and copy the result.
102
 
103
+ Check the prompt formatting first — it costs seconds and no model download:
 
104
 
105
+ ```bash
106
+ python -m moss4gb bank build --dry-run --prompt "a wide view of a red bicycle"
107
+ # -> 17 tokens, <|im_start|>user\n...<|im_end|>\n<|im_start|>assistant\n thinking\n
108
+ ```
109
+
110
+ The repo's own `tokenizer/` has **no chat template** (`apply_chat_template`
111
+ raises), and the template decides exactly which tokens the DiT was trained on,
112
+ so `qwen35_chat_template.jinja` is attached automatically. Don't substitute
113
+ your own.
114
+
115
+ ```bash
116
+ huggingface-cli download OpenMOSS-Team/MOSS-Image-SFT \
117
+ --include 'text_encoder/*' --local-dir models/MOSS-Image-SFT
118
+ python -m moss4gb bank build --prompt "a wide view of a red bicycle"
119
+ python -m moss4gb bank build --prompts-file prompts.txt
120
+ ```
121
+
122
+ ```bash
123
+ # on the 4 GB box
124
+ python -m moss4gb bank import --source /path/to/bank
125
+ python -m moss4gb generate --bank-id p_157d4f7a84a3 --height 256 --width 256 --steps 20 --cfg 3 --seed 42 --out out/sample.png
126
+ ```
127
+
128
+ A bank entry is ~4 MB fp16 per 500 tokens, so a few hundred prompts is a few
129
+ hundred MB.
130
+
131
+ ## Notes and limits
132
+
133
+ * **Precision.** Pascal has no bf16, so weights are cast to fp16 and quantized
134
+ to NF4 with `compute_dtype=float16`. If a run produces NaN/black output, the
135
+ residual stream is overflowing fp16; retry with `--compute-dtype float32`
136
+ (about 2x slower), or swap in KBlueLeaf's FP16-fixed SFT transformer, whose
137
+ residual rescaling exists for exactly this class of card.
138
+ * **Resolution.** 256x256 is comfortable. 512x512 leaves only a few hundred MB
139
+ free once the DiT is resident, so it will page on a machine with a browser
140
+ open; 1024x1024 is out of reach. `--height/--width` must be multiples of 16.
141
+ * **Steps.** 15-25 steps is the intended range. With `--cfg 3` that is two
142
+ transformer forwards per step; `--cfg 1` halves the time at some quality
143
+ cost. `--uncond-skip 22` keeps the default negative-branch layer skip.
144
+ * **System RAM.** The loader is streaming: it never materializes the fp16
145
+ model, so peak RSS stays around 6.8 GB (one shard of evictable file pages)
146
+ instead of 10.2 GB. This matters on a 16 GB box with ~6 GB free.
147
+ * **Prompt bank.** `assets/bank` currently holds the null bank, so renders are
148
+ unconditioned. Building a real one needs the 9B encoder on a machine with
149
+ ~20 GB RAM; see below.
150
+ * **The conditioning is frozen.** With no text encoder there is no
151
+ instruction-following beyond what is in the bank. To get image-input
152
+ conditioning you need a trained bridge into the text slot, not a flag.
153
+
154
+ ## Tests
155
+
156
+ ```bash
157
+ python -m pytest tests -q # 26 tests, ~21 s, no GPU needed
158
+ ```
159
+
160
+ Covers the bank format, chat-template formatting and token counts, the
161
+ NF4 skip list, and byte-level parity between our CPU decode and the upstream
162
+ pipeline's own decode.
163
+
164
+ ## Files
165
 
166
+ ```
167
+ moss4gb/quant.py streaming NF4 loader (meta skeleton -> per-tensor -> GPU)
168
+ moss4gb/decode.py CPU VAE decode: BatchNorm fold + 2x2 channel unshuffle
169
+ moss4gb/bank.py embedding bank format, null bank, encoder-side builder
170
+ moss4gb/cli.py doctor / bank / generate
171
+ moss4gb/metadata_cache.py caches diffusers' import-time package scan
172
+ tests/ CPU-only suite
173
+ ```
174
 
175
  ## Trainable lane — one image in, one LoRA out
176
 
177
+ This pack renders for free, and the adapter lane is free too: a single-image
178
+ loop (one image -> one 3 MB LoRA) with a measured recipe, plus the one loader bug
179
+ (`alpha / r`, not `r`) that decides whether an adapter helps or turns the render
180
+ into confetti:
181
+
182
+ [TRAINABLE-REFERENCE.md](https://huggingface.co/AEUPH/moss4gb/blob/main/TRAINABLE-REFERENCE.md)
183
+
184
+ ## Offload to ZeroGPU — no local GPU required
185
+
186
+ Everything heavy runs on the free ZeroGPU slot. The only things computed
187
+ off-device are the 2.77 GB NF4 cache download and the prompt bank storage.
188
+
189
+ | Endpoint | Runs on the GPU | Notes |
190
+ |---|---|---|
191
+ | `/generate` | NF4 → dense bf16 expansion, sampling, VAE decode | 256²/12 steps ≈ 1 s here; reserve scales with steps × area × CFG |
192
+ | `/bank_train` | Qwen3.5-9B encoder forward (one pass per new prompt) | bank entry ≈ 300 KB, reused for every later call |
193
+ | `/bank_export` | nothing | returns `bank.safetensors` for the local 4 GB path |
194
+ | `/status` | nothing | readiness, precisions, bank size |
195
+
196
+ Python client (same auth tricks apply to any HF token):
197
+
198
+ ```python
199
+ from gradio_client import Client
200
+ c = Client("AEUPH/moss4gb", hf_token="hf_...")
201
+ img, meta = c.predict(
202
+ prompt="a wide view of a red bicycle leaning on a white wall",
203
+ negative_prompt="", height=256, width=256,
204
+ steps=12, guidance_scale=1.0, seed=42, uncond_skip="22",
205
+ api_name="/generate",
206
+ )
207
+ c.predict("a red bicycle on cobblestones", api_name="/bank_train") # encoder pass
208
+ print(c.predict(api_name="/status"))
209
+ ```
210
+
211
+ Agent/MCP: `launch(mcp_server=True)` exposes the same four calls as tools at
212
+ `https://aeuph-moss4gb.hf.space/gradio_api/mcp/`.
213
+
214
+ Economy note: reservations are computed per request (`reserve_seconds`) and
215
+ clamped to 120 s, so an anonymous visitor never burns the daily quota on a
216
+ call it did not ask for.