Run this model with HF Jobs

Run masterofaudio2077/sana-manga-coloring-onnx-int8 on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.

Paste one of these prompts into your coding agent:

Fine-tune on my dataset

Use HF Jobs to fine-tune https://huggingface.co/masterofaudio2077/sana-manga-coloring-onnx-int8. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.

Run batch inference

Use HF Jobs to run batch inference with https://huggingface.co/masterofaudio2077/sana-manga-coloring-onnx-int8 on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.

Sana Coloring: ONNX int8

An int8 ONNX version of sana-manga-coloring that colors black-and-white manga pages on the CPU. It needs only onnxruntime, numpy and Pillow, with no PyTorch, and it gives the same colors as the original model.

original model (left of each pair) and int8 (right)

In each pair, left is the original model and right is this int8 version. Same page, tags and noise.

Quick start

pip install onnxruntime numpy pillow
huggingface-cli download masterofaudio2077/sana-manga-coloring-onnx-int8 --local-dir sana-int8
python sana-int8/run_onnx.py page.png --out colored.png
python sana-int8/run_onnx.py page.png --mood calm --time_of_day evening --palette "warm amber and brown"
python sana-int8/run_onnx.py page.png --cache 0.15   # about 1.6x faster, see "Step cache"

The download includes config.json, the Hub's download-tracking query file; keep it next to the ONNX files.

Each tag can be left out to let the model choose. The valid values are listed in the base model card and in run_onnx.py --help. Other options: --steps (default 20), --cfg (2.0), --seed, --threads (default: all cores) and --cache (default 0, off). Pages are colored with the shorter side at 512 px.

From Python:

import numpy as np
from PIL import Image
from run_onnx import Colorizer, prepare_page, tag_ids, to_pil

model = Colorizer("sana-int8")
bw = prepare_page(Image.open("page.png"))
noise = np.random.default_rng(0).standard_normal((1, 32, bw.shape[2] // 32, bw.shape[3] // 32), dtype=np.float32)
cond = tag_ids({"saturation": "vivid", "palette": "bright primary colors"})
to_pil(model(bw, noise, cond, np.zeros_like(cond))).save("colored.png")

Files

File What it does Precision
encode.onnx (+ .data) Sana's VAE encoder and the model's B/W line-feature encoder. Runs once per page. fp32
head.onnx The fine-tuned Sana transformer, part 1: input layers and block 0. Runs every step, twice (with and without the tags). int8 weights
blocks.onnx Part 2: transformer blocks 1–19, most of the work. Skipped on some steps when the step cache is on. int8 weights
out.onnx Part 3: the final layer. Runs every step. int8 weights
decode.onnx (+ .data) Sana's VAE decoder, then puts the page's line art back on the colors. Runs once per page. fp32
run_onnx.py Preprocessing, the 20-step Euler flow sampler with CFG, the optional step cache, and the command line.
config.json Model and component metadata. Also the Hub's download-tracking query file.

The transformer is split in three so the step cache can skip the middle. Run together, the three parts give exactly the same result as the whole transformer.

What is quantized

The transformer (all three parts) uses dynamic int8 quantization with per-channel weight scales (onnxruntime.quantization.quantize_dynamic, per_channel=True). This covers all 258 weight matrices of its linear layers: attention, cross-attention on the tags, timestep conditioning, the B/W feature injections, and the output projection. Activations are quantized on the fly at run time.

Kept in fp32, because quantizing them made the result worse:

  • The transformer's feed-forward 1Γ—1 convolutions. With them in int8 the colors drifted (average Ξ”E up to 4.8), and ONNX Runtime's int8 convolution was about 2Γ— slower than fp32.
  • The VAE (encode and decode). It runs only once per page, so int8 would save little time, and VAE quantization tends to damage fine detail.

Per-channel scales matter here. With one scale per layer, the colors drifted visibly warmer on some pages (average Ξ”E up to 5.1). With per-channel scales the largest average Ξ”E is 0.58.

Quality

Compared with the original PyTorch model (bfloat16 weights computed in float32, the way its Space runs it) on 10 pages: 8 from the base model's demo gallery plus 2 full-size pages. The 10 runs cover 5 different tag settings, and each page uses the same input, tags and noise in every version. Ξ”E is the CIE76 color difference in Lab. Around 2.3 is the smallest difference people can notice, and 0 is identical.

Page Ξ”E mean Ξ”E 99th pct PSNR
1 0.36 1.0 52.6 dB
2 0.44 1.3 50.7 dB
3 0.56 1.5 48.6 dB
4 0.52 1.2 49.5 dB
5 0.58 1.2 47.8 dB
6 0.38 1.0 52.4 dB
7 0.45 1.5 51.0 dB
8 0.34 1.4 53.1 dB
9 0.48 1.3 51.4 dB
10 0.45 1.1 52.0 dB

These numbers are with the step cache off (the default). The fp32 ONNX export, before quantization, matches PyTorch exactly at 8-bit output (Ξ”E 0.00, PSNR above 83 dB). All of the small difference above comes from int8.

Step cache

--cache 0.15 turns on a first-block cache (the TeaCache / FBCache idea), the same one the demo Space uses. Every step runs block 0. If block 0's output barely changed since the last full step, blocks 1–19 are skipped and the change they made last time is reused, at most 2 steps in a row. With 0.15, 11–12 of the 20 steps are skipped.

It changes the colors a little more than int8 does: average Ξ”E 1.5–4.0 against the original model with no cache, on the pages tested. That is the same difference the original PyTorch model shows with the same cache (1.6–3.9), so it comes from the cache, not from int8. Leave it off for the closest match to the original model.

Speed

20 steps per page, 512Γ—640 to 512Γ—768 pages, on an AMD Ryzen AI 7 350.

All 16 threads, cache off:

Seconds per page
PyTorch, original model 57–97
ONNX fp32 49–59
ONNX int8 (this repo) 36–44

2 threads (like a free Hugging Face Space):

Seconds per page
PyTorch, original model, cache 0.15 68–78
ONNX int8, cache off 61–69
ONNX int8, cache 0.15 39–44

The download is about 2.7 GB: the transformer is 1.4 GB in all, and each VAE half is about 0.6 GB. Peak memory is about 4 GB.

Notes

  • run_onnx.py resizes the page with Pillow, while the PyTorch code uses torch.nn.functional.interpolate. Both use antialiased bilinear filtering, so results match very closely but not bit for bit.
  • The noise comes from NumPy, so for the same --seed the colors differ from the PyTorch model. Given the same noise array, the colors match (see Quality).
  • Limitations, training details and the tag list are in the base model card.
Downloads last month
21
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support

Model tree for masterofaudio2077/sana-manga-coloring-onnx-int8

Unable to build the model tree, the base model loops to the model itself. Learn more.

Space using masterofaudio2077/sana-manga-coloring-onnx-int8 1