Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
base_model: zai-org/GLM-Image
|
| 4 |
+
tags:
|
| 5 |
+
- core-ai
|
| 6 |
+
- coreai
|
| 7 |
+
- text-to-image
|
| 8 |
+
- autoregressive
|
| 9 |
+
- diffusion
|
| 10 |
+
- glm
|
| 11 |
+
- on-device
|
| 12 |
+
- apple-silicon
|
| 13 |
+
pipeline_tag: text-to-image
|
| 14 |
+
library_name: coreai
|
| 15 |
+
---
|
| 16 |
+
|
| 17 |
+
# GLM-Image — Core AI
|
| 18 |
+
|
| 19 |
+
[ZhipuAI's **GLM-Image**](https://huggingface.co/zai-org/GLM-Image) (16B, MIT) converted to
|
| 20 |
+
**Core AI** for on-device image generation on Apple Silicon (macOS 27+) — the zoo's first
|
| 21 |
+
**autoregressive + diffusion hybrid**.
|
| 22 |
+
|
| 23 |
+
GLM-Image generates in two stages: a **9B GLM-4 autoregressive model** writes the image as a
|
| 24 |
+
grid of discrete *visual prior tokens* (sampled left-to-right like an LLM, ~36 tok/s on
|
| 25 |
+
M4 Max), and a **7B flow-matching DiT** denoises the actual pixels conditioned on those
|
| 26 |
+
tokens, followed by a 16-channel VAE decode. Both native **1024×1024** and a faster
|
| 27 |
+
**512×512** variant are included.
|
| 28 |
+
|
| 29 |
+
> **macOS only.** The AR (9.6 GB) + DiT (9.2 GB) weights are resolution-independent, so the
|
| 30 |
+
> 512 variant is no smaller — an iPhone port needs int4 + sequential component loading.
|
| 31 |
+
|
| 32 |
+
## Components
|
| 33 |
+
|
| 34 |
+
| Component | Description | Size |
|
| 35 |
+
| --- | --- | --- |
|
| 36 |
+
| `glm_image_ar.aimodelc` | GLM-4-9B visual-token AR decoder (int8, S=1 decode, AOT h16c GPU) | 9.6 GB |
|
| 37 |
+
| `glm_image_dit_1024.aimodelc` | 30-block flow-matching DiT, 1024² (int8, AOT h16c GPU) | 9.2 GB |
|
| 38 |
+
| `glm_image_dit_512.aimodelc` | Same DiT exported at 512² | 9.2 GB |
|
| 39 |
+
| `glm_image_vae_1024.aimodel` | 16ch AutoencoderKL decoder, 1024² (fp32, CPU) | 0.9 GB |
|
| 40 |
+
| `glm_image_vae_512.aimodel` | Same VAE exported at 512² | 0.9 GB |
|
| 41 |
+
| `tokenizer/` | GLM tokenizer (vocab 168 064; visual tokens < 16 512) | — |
|
| 42 |
+
| `ehs.f32` | Empty-glyph text embedding [1×1×1472] — the DiT text input for glyph-free prompts (no T5 needed at runtime) | 6 KB |
|
| 43 |
+
|
| 44 |
+
Weights are int8 (per-block-32) for AR/DiT; the VAE stays fp32 (fp16 overflows its
|
| 45 |
+
activations). Compute precision float16 on GPU. One resolution needs ~19.7 GB on disk
|
| 46 |
+
(AR + one DiT + one VAE).
|
| 47 |
+
|
| 48 |
+
## Usage
|
| 49 |
+
|
| 50 |
+
### Sample app (easiest)
|
| 51 |
+
|
| 52 |
+
[**CoreAIImageGen** (macOS)](https://github.com/john-rocky/coreai-model-zoo/tree/main/apps/CoreAIImageGen)
|
| 53 |
+
— run the `CoreAIImageGenMac` scheme, pick **GLM-Image 1024 (AR+diffusion)** from the model
|
| 54 |
+
menu, tap **Download & Load**, type a prompt, **Generate**.
|
| 55 |
+
|
| 56 |
+
- **Steps 20** (default) ≈ 4.5 min/image on M4 Max — reference quality
|
| 57 |
+
- **Steps 12** ≈ 2.5 min — nearly indistinguishable
|
| 58 |
+
- The 512 variant generates in ~1 min
|
| 59 |
+
|
| 60 |
+
The AR stage is *sampled* (temperature 0.9 / top-p 0.75, the upstream `generation_config`),
|
| 61 |
+
so re-rolling the seed meaningfully changes composition.
|
| 62 |
+
|
| 63 |
+
### Pipeline contract (build your own)
|
| 64 |
+
|
| 65 |
+
The hybrid does not fit Apple's high-level `CoreAIDiffusionPipeline`; the host loop is
|
| 66 |
+
~300 lines of Swift (see the app's `GlmImagePipeline`):
|
| 67 |
+
|
| 68 |
+
1. **AR**: `tokenize(prompt) + fixed grid suffix` → S=1 prefill/decode with host-computed
|
| 69 |
+
3D-mRoPE cos/sin → sample visual tokens → 2× upsample the large grid → `prior[1, N]`
|
| 70 |
+
(N = 1024 @512, 4096 @1024).
|
| 71 |
+
2. **DiT**: 20-step CFG (guidance 1.5, `prior_scale` 1/0 for cond/uncond). Condition the
|
| 72 |
+
DiT on the **raw integer schedule** `trunc(linspace(1000,1,steps+1)) − 1`; step the
|
| 73 |
+
latent with the **mu-shifted sigmas** `σ' = μ/(μ + (1/σ − 1))`, μ = 0.75·side/256 + 0.25.
|
| 74 |
+
3. **VAE** (fp32, CPU): latent → RGB.
|
| 75 |
+
|
| 76 |
+
⚠️ Feeding the DiT the *shifted* timesteps (sigma×1000) instead of the raw schedule skews
|
| 77 |
+
the adaLN time conditioning every step and drifts colors — resolution-dependently (mild
|
| 78 |
+
at 512, strong at 1024). This is the one non-obvious contract in the port.
|
| 79 |
+
|
| 80 |
+
## Limitations
|
| 81 |
+
|
| 82 |
+
- **Glyph rendering (text in images) is not wired**: prompts containing quoted text
|
| 83 |
+
(`'…'`, `"…"`, `「…」`) route through a T5 glyph encoder that is not part of this bundle.
|
| 84 |
+
Keep prompts glyph-free.
|
| 85 |
+
- Negative prompts are a no-op (GLM's CFG drops the prior tokens instead).
|
| 86 |
+
- Image-to-image / editing (`GlmImageKVCache`) not yet exported.
|
| 87 |
+
|
| 88 |
+
## Quality
|
| 89 |
+
|
| 90 |
+
Verified against the bf16 diffusers reference: DiT single-forward cosine 0.9999, VAE
|
| 91 |
+
byte-exact, AR prior-injection reproduces the reference composition; end-to-end output is
|
| 92 |
+
visually on par with `GlmImagePipeline` at the same step count.
|
| 93 |
+
|
| 94 |
+
## License
|
| 95 |
+
|
| 96 |
+
MIT (inherited from [zai-org/GLM-Image](https://huggingface.co/zai-org/GLM-Image)).
|