This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/core/gen/gen.h
T
Deadcode 685ec5bd85 radiance 1.9.0
New models:
- LTX-2.5 (base and distilled): video with a stereo soundtrack from text or from text and a first
  frame, on one card, with its duration head, tiled VAE decoding, the diffusion decoder and the x2
  latent upsampler
- Krea 2 Raw and Turbo, and FLUX.3 [klein] and its base: text to image, and for FLUX.3 edits from up
  to ten reference pictures, on one card
- GLM-5.3-Flash from AMD's Quark MXFP4 export, on eight cards (John McDonough, Nunze)
- Gemma 3 / TranslateGemma 27B with an optional DFlash2 drafter (mici81)

Engine:
- --kv-cache-dtype fp8_v4 and k6_v4 for Gemma 3 and Qwen3.8-Flash-Next (mici81)
- the rotated int8 trunk (i8a8h) for Qwen3.8-27B (eddie168168)
- eight-rank all-reduce bands, weighted shards and pieces (Nunze); --host-headroom-mib (sfjes)
- the dashboard shows the single-stream decode floor and its most recent window (Memoria)

Fixes: prefix-cache reservations kept until their save lands (baKon), tool calls in Responses
namespaces (#119), system messages merged when a template refuses several (#137), a prefill
continued with no open step (#127), calibration of a Gram accumulation (#122), JSON Schema patterns
read unanchored (davetha), GLM-style tool calls, the KV floor counting every sequence's state.
Plugin ABI 24.
2026-10-19 21:58:07 +02:00

158 lines
7.5 KiB
C++

/* gen.h -- image generation: the request, what comes back, and the driver that turns a request
* into the passes an image generator declares (rad_declare_generator, abi/rad_builder.h).
*
* A generator is not driven by token steps: an image is tens of denoising passes over thousands of
* latent rows, all read whole, none sampled. Token positions, prefix reuse, preemption and
* speculation have no meaning here, so the generator has its own driver and shares only what lies
* below the step: the ranks, the plugin's step function and the KV manager holding conditioning.
*
* Passes, as the driver runs them:
*
* COND once per conditioning sequence (the prompt, plus the negative prompt with guidance):
* the prompt and its reference pictures encoded into the generator's KV group. A
* request is admitted once its lanes and its KV are free.
* DENOISE one step for each in-flight image that fits the pass, any mix of requests, sizes and
* step counts; each image carries its own sigma. With guidance an image is two entries,
* positive and negative conditioning, which the plugin combines.
* DECODE one image's latent to pixels, read back and handed to its request.
*
* Images run in lanes, the plugin's own latent state (`max_lanes` of them): a request of n images
* takes n lanes and shares one conditioning.
*/
#pragma once
#include "rad_core.h"
#include "mm/processor.h"
#include <atomic>
#include <condition_variable>
#include <deque>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
namespace rad {
struct IKVManager;
namespace gen {
/* A picture as the generator's own encoder reads it: RGBA (or `ch` channels), row-major, each value
* bf16 in [-1, 1]. */
struct Pixels {
int32_t w = 0, h = 0, ch = 4;
std::vector<uint16_t> bf16;
};
/* One conditioning sequence: the prompt tokens with their media placed, and how many leading
* tokens the generator does not see (RadGeneratorDecl::drop_prefix). */
struct Branch {
std::vector<int32_t> tokens;
std::shared_ptr<mm::PromptMedia> media; /* null for a prompt with no pictures */
int32_t n_drop = 0;
};
/* One finished image, or a preview of one. */
struct Image {
int32_t index = 0; /* which of the request's n */
int32_t w = 0, h = 0, ch = 4;
std::vector<uint8_t> px; /* [h, w, ch] */
bool preview = false;
int32_t preview_no = 0;
uint64_t seed = 0;
};
/* The channel back to the caller: images as they finish, then the end. The driver thread writes,
* the caller reads; a mutex suffices for a handful of megabyte images per request. `cancel` is set
* by the caller and read by the driver at its next pass. */
class Ticket {
public:
/* driver side */
void push(Image&& im);
void finish(int status, const std::string& why = std::string());
/* caller side: the next image, or false once the request has ended and every image was taken.
* Waits at most `ms` milliseconds; `*timed_out` marks a false that was a timeout, not the end. */
bool next(Image* out, int ms, bool* timed_out);
bool finished() const;
int status() const;
std::string why() const;
std::atomic<bool> cancel{false};
/* Usage for the response: conditioning tokens, latent rows made, and per-phase times.
* Written by the driver before finish(). */
int64_t in_tokens = 0, out_tokens = 0;
int64_t t_submit_ns = 0, t_cond_ns = 0, t_first_step_ns = 0, t_denoised_ns = 0, t_done_ns = 0;
int32_t steps = 0;
private:
mutable std::mutex mu_;
std::condition_variable cv_;
std::deque<Image> q_;
bool done_ = false;
int status_ = RAD_OK;
std::string why_;
};
struct Request {
Branch pos, neg;
bool has_neg = false;
float cfg = 1.0f;
int32_t width = 0, height = 0; /* pixels, whole multiples of the declared multiple */
int32_t steps = 0; /* 0: the model's default (or its fixed schedule) */
int32_t n = 1;
uint64_t seed = 0; /* image i draws its noise from seed + i */
int32_t previews = 0; /* partial images per image, 0..3 */
/* The reference pictures' encoder view, one per media item of `pos`, in order. */
std::vector<Pixels> refs;
/* Inpainting (an edit with a mask): the source picture at the output size, and per latent
* token how much is repainted (1) versus kept (0). */
bool inpaint = false;
Pixels inp_src;
std::vector<float> inp_mask;
std::shared_ptr<Ticket> ticket;
};
/* ================================================================== the sampler's schedule */
/* The n + 1 sigmas a request steps through, the last 0: the decl's fixed schedule, or `steps`
* (0: the default) shifted as the decl says. `tokens` is the image's latent token count, read by a
* dynamic shift. f32, matching the reference. */
std::vector<float> schedule(const RadGeneratorDecl& d, const std::vector<double>& fixed,
int32_t steps, int64_t tokens);
/* ================================================================== the generator's sequence */
/* One conditioning sequence as the generator sees it (RadGeneratorDecl::layout): the prompt row or
* reference latent row behind each row, rotary positions, segments, and the output image's frame. */
struct Layout {
int64_t rows = 0;
std::vector<int32_t> src; /* [rows] */
std::vector<int32_t> rope; /* [3, rows] component-major */
std::vector<int32_t> seg; /* [n_seg + 1] */
std::vector<int32_t> bidi; /* [n_seg] */
int32_t next_pos = 0; /* the rotary count after the last row: the image's frame */
/* JOINT_STEP only: the text's rows (padding included), the padding rows among them, and the
* f32 positions, [4, rows] component-major (`rope` is then empty). */
int64_t text = 0;
int32_t n_pad = 0;
std::vector<float> rope_f;
};
/* `ref_hw` is each reference's latent grid (h, w), in media order. RAD_E_INVAL with `why` when the
* prompt does not start with its drop prefix or a picture's slots do not tile its latent grid. */
int build_layout(const Branch& b, int64_t slot_rows, const std::vector<std::pair<int32_t, int32_t>>& ref_hw,
Layout* out, std::string* why);
/* The rotary positions of an h x w image at frame `pos`: [3, h * w] component-major. */
void image_rope(int32_t pos, int32_t h, int32_t w, int32_t* planes, int64_t n_total);
/* A conditioning in the JOINT_STEP layout (RadGeneratorDecl): the prompt's rows after its drop
* prefix, padding to a multiple of `pad_multiple` (0: none), then each reference picture's latent
* tokens. `src` is the prompt row behind a text row -- a padding row's is past the prompt's last,
* where the COND pass appends the padding tokens -- or -(1 + i) for reference latent row i. One
* text segment (causal) and a segment per picture (bidirectional). */
int build_joint_layout(const Branch& b, int32_t pad_multiple, double hw_scale, bool text_l,
const std::vector<std::pair<int32_t, int32_t>>& ref_hw, Layout* out,
std::string* why);
/* The f32 positions (0, s h, s w, l) of an h x w grid: [4, h * w] component-major. */
void grid_rope_f(double hw_scale, float l, int32_t h, int32_t w, float* planes, int64_t n_total);
} /* namespace gen */
} /* namespace rad */