krea2-sref-adapter / README.md
lyraaaa's picture
Update README.md
519c74b verified
|
Raw History Blame Contribute Delete
6.19 kB
---
license: mit
tags:
- krea-2
- style
---
# Krea 2 srefs
Style references for the Krea 2 image model. A style is a whole number. The same number gives the same style on any prompt and any seed. Pick a number, plug it in, and have fun.
Code: https://github.com/lyramakesmusic/krea2-sref-adapter
![64 codes with no prompt](examples/srefs-null-prompt.jpeg)
by [lyraaaa](https://x.com/_lyraaaa_) and [xkm](https://x.com/kxm43)
[![Try the interactive demo](https://img.shields.io/badge/πŸ€—%20Spaces-Try%20the%20interactive%20demo-ff9d00?style=for-the-badge)](https://huggingface.co/spaces/lyraaaa/krea2-sref-adapter)
## How it works
### How Krea 2 reads a prompt
Krea 2 reads your prompt in two stages. First, a text encoder (Qwen3-VL) turns the words into hidden states. Next, a small stage inside Krea 2 called text fusion (`txtfusion`, followed by a small MLP) turns those hidden states into a row of prompt tokens. Each token is a vector of 6,144 numbers.
Krea 2 places that row of prompt tokens in front of the image patches. Its 28 blocks then look at both together while they denoise the image.
Prompt tokens carry no position. They are a bag, not a sentence. Because of that, extra tokens can go straight into the bag, and Krea 2 reads them alongside the prompt.
### What an sref is
An sref is 108 extra tokens added to that bag: a block of `108 x 6144` numbers. Krea 2 reads the block the way it reads more prompt, though it was never text.
### From a number to 108 tokens
Three small steps turn any whole number into that `108 x 6144` block.
1. **Noise injection.** Your code is the seed of a random number generator. It draws 64 numbers from a bell curve and scales each one by `sd`, a list of 64 values that sets how far styles typically spread along each direction. Those 64 numbers are the style's coordinates. Every whole number gives a style, and the same number always gives the same 64 coordinates.
2. **Projection.** `sref_space.npz` holds a fixed linear map called `basis` with shape `[64, 108, 6144]`. You can think of it as 64 base blocks of `108 x 6144`. Each coordinate weights its matching slice of `basis`, and summing all 64 weighted slices turns 64 numbers into one `108 x 6144` block.
3. **Weighting.** A single number, `strength`, multiplies the whole block:
```text
block = strength * sum_i z_i * basis_i
```
Here is the whole path from a prompt and a code into Krea 2:
```text
prompt text ─> Qwen3-VL ─> txtfusion + MLP ─> prompt tokens [T, 6144] ─┐
β”œβ”€> Krea 2
style code ─> RNG * sd ─> 64 coords ─> basis * strength β”‚ (28 blocks)
─> style block [108, 6144] β”€β”˜
```
For example, take code `768519172` at strength `6.0`. Seeding the generator with `768519172` produces 64 scaled coordinates `z`. Multiplying each coordinate by its slice of `basis`, summing them, and scaling by `6.0` gives a `[108, 6144]` block. That block sits at position zero right behind your prompt tokens, and Krea 2's 28 blocks read both together as the image denoises.
## Using it in code
Generate a style block with `sref.py`:
```python
from sref import style_block
block = style_block(768519172, strength=6.0) # numpy float32 [108, 6144]
```
In your Krea 2 pipeline, append the block right after text fusion and its MLP, and give the 108 tokens position zero like the prompt's:
```python
context = txtmlp_layers(txtmlp_norm(txtfusion(states, mask))) # [B, T, 6144] prompt tokens
block = torch.from_numpy(style_block(code, strength)).to(context)
context = torch.cat([context, block[None].expand(context.shape[0], -1, -1)], dim=1)
pos = torch.cat([pos.new_zeros(B, 108, 3), pos], dim=1) # the block's tokens sit at position zero, like the prompt's
```
Run this on every denoising step. With classifier-free guidance, add the block on the conditional pass. No model weights change. It adds 108 tokens to the sequence.
## Variants
Three style spaces ship side by side, `sref_space.npz`, `sref_space_lg.npz` and `sref_space_variant.npz`: they're roughly equal, but slightly different, and the same code gives a different style in each.
```python
block = style_block(768519172, strength=6.0, space_name="lg") # or "variant"; "default" when omitted
```
## Strength
- `6` is a full restyle for most codes.
- `3` is light.
- Above `10` overpowers the prompt.
- `0` is off.
The same code can want a different strength at different image sizes.
## Blending styles
Every code lives in the same 64-number coordinate space. To blend two styles, average their coordinates and build a block from the result:
```python
from sref import coords, block_from_coords
z = (coords(768519172) + coords(3395250012)) / 2
block = block_from_coords(z, strength=6.0)
```
## Optional: loosening the prompt
Krea 2 settles a picture's layout and medium during its first few denoising steps. When a style only decorates the background, you can add a small "loosen" step to your sampler.
During the first three denoising steps, apply an additive bias to attention on the prompt tokens so the prompt's words count for less at the start, fading out across those three steps: full, half, quarter. That lets the style shape the picture before the prompt fixes it. Higher values can let prompt details slip.
This takes a few lines in the sampler and is not part of `sref.py`.
## What to expect
- Colors named in the prompt survive. A red raincoat stays red inside an ink drawing.
- Some codes bring a recurring object along with the look.
- Some codes are photographic looks.
- On portrait prompts, some codes restyle the background more than the subject.
## More examples:
Each sheet shows 64 codes at strength 6, labelled with their number. The first tile of each sheet has the style off.
![64 codes on "an astronaut riding a horse"](examples/srefs-astronaut-horse.jpeg)
![64 codes on "a woman"](examples/srefs-woman.jpeg)
![64 codes on "an old man reading a newspaper in a cafe"](examples/srefs-man-reading.jpeg)
![64 codes on "a lighthouse on a rocky coast"](examples/srefs-lighthouse.jpeg)