Archived
- video: MiniMax-H3 and FastVideo's FastH3 8-Step V2, a video with its stereo soundtrack from text on OpenAI's /v1/videos, block fp8 on two cards - music: MiniMax-Music3, songs from lyrics and a description on /v1/audio/speech - speech: Breeze-TTS-2 on /v1/audio/speech and vLLM-Omni's realtime WebSocket - runtime LoRA adapters beside the base model, picked per request by `model` (baKon, #105) - several agent streams keep their cached history: --checkpoint-slots auto, the new default, sizes the snapshot store by --max-num-seqs (#108) - the Swift 1.5 27B W4 releases convert and serve: compressed-tensors AWQ and GPTQ int4, AutoRound, ModelOpt NVFP4 (#110) - the image's host tests pass where perf_event_open is refused, as in BuildKit's sandbox (#106) - plugin ABI 21: out-of-tree plugins must be rebuilt
179 lines
7.3 KiB
C++
179 lines
7.3 KiB
C++
/* driver.h -- the image generator's pass driver (gen.h describes the passes).
|
|
*
|
|
* Driven by the engine thread: `next()` picks and stages a pass, `done()` folds its result back
|
|
* once every rank has run it. Requests arrive from HTTP threads through `submit()` and leave
|
|
* through their Ticket; the job queue is the only shared state, behind one mutex.
|
|
*
|
|
* Pass priority, one pass at a time:
|
|
*
|
|
* 1. DECODE an image is owed: a finished image, or a preview its last step marked. Goes first
|
|
* because a finished image frees its lane and answers its caller, and a preview must be read
|
|
* before the lane's next step overwrites the preview latent.
|
|
* 2. COND an admitted request is owed, so its images can join the next denoise pass.
|
|
* 3. DENOISE over as many in-flight images as fit the step's rows, oldest request first, each
|
|
* at its own step.
|
|
*
|
|
* A request is admitted once it can get lanes for every image and KV for every conditioning
|
|
* sequence. Admission is in order: a request that does not fit blocks those behind it, so a large
|
|
* request is not starved by a stream of small ones.
|
|
*
|
|
* Staging is one device region per rank plus one pinned host region with the same layout. Every
|
|
* pass is synchronised before the next is built (Engine::gen_loop), so one host region suffices:
|
|
* a pass takes tens of milliseconds, the host work between passes microseconds.
|
|
*/
|
|
#pragma once
|
|
#include "gen.h"
|
|
#include "pass.h"
|
|
#include "device/device.h"
|
|
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
|
|
namespace rad {
|
|
namespace gen {
|
|
|
|
class Driver final : public IPassDriver {
|
|
public:
|
|
struct RankIO {
|
|
int device = 0;
|
|
RadStream stream = nullptr;
|
|
};
|
|
|
|
/* `kv` is rank 0's KV manager: one block table serves every rank. */
|
|
int init(const Program& prog, IKVManager* kv, const std::vector<RankIO>& ranks);
|
|
void fini();
|
|
~Driver() { fini(); }
|
|
|
|
/* The bytes init() allocates on each rank's card, for the VRAM budget. */
|
|
static int64_t device_bytes(const Program& prog);
|
|
|
|
/* ---------------------------------------------------------- HTTP threads */
|
|
/* Queue a request. Refused at once, with `why`, when it can never run: a size past the
|
|
* largest image, more pictures than the model takes, a conditioning longer than a pass. */
|
|
int submit(std::unique_ptr<Request> r, std::string* why);
|
|
/* The model's limits, for the server to check before the expensive part of preparing a
|
|
* request. */
|
|
const RadGeneratorDecl& decl() const { return decl_; }
|
|
int64_t max_cond_tokens() const { return max_tok_; }
|
|
void shutdown(const char* why, int status) override;
|
|
|
|
/* ---------------------------------------------------------- the engine thread */
|
|
const RadBatch* next(int step) override;
|
|
const RadBatch* batch_for(int rank) const override;
|
|
/* A decode pass's pixels: `image_out` into the pinned image buffer. */
|
|
const std::vector<Readback>& readbacks() const override { return rb_; }
|
|
int done() override;
|
|
bool busy() const;
|
|
|
|
int64_t queued() const override;
|
|
int64_t running() const override;
|
|
|
|
private:
|
|
struct Cond {
|
|
Layout lay;
|
|
uint64_t seq = 0; /* the KV manager's key */
|
|
bool have_kv = false;
|
|
bool encoded = false;
|
|
std::vector<int32_t> rope_img; /* [3, image tokens]: the image's rows at this sequence */
|
|
};
|
|
struct Job {
|
|
uint64_t id = 0;
|
|
std::unique_ptr<Request> req;
|
|
int n_branch = 1;
|
|
Cond cond[2];
|
|
int32_t h_lat = 0, w_lat = 0;
|
|
int64_t tokens = 0;
|
|
std::vector<float> sigmas;
|
|
std::vector<int32_t> lanes;
|
|
std::vector<std::pair<int32_t, int32_t>> ref_hw;
|
|
int32_t images_done = 0;
|
|
bool admitted = false;
|
|
bool failed = false;
|
|
};
|
|
struct Lane {
|
|
bool busy = false; /* held by request job_id */
|
|
uint64_t job_id = 0;
|
|
int32_t index = 0;
|
|
int32_t step = 0;
|
|
uint64_t seed = 0;
|
|
std::vector<int32_t> preview_at; /* the steps after which a preview is decoded */
|
|
int32_t previews_done = 0;
|
|
bool preview_due = false;
|
|
bool final_due = false;
|
|
bool finished = false;
|
|
};
|
|
/* The pass in flight, for done(). */
|
|
struct Pass {
|
|
int kind = 0;
|
|
uint64_t job = 0;
|
|
int branch = 0;
|
|
int32_t lane = -1;
|
|
bool preview = false;
|
|
std::vector<int32_t> lanes; /* DENOISE: the lanes it stepped, one per image */
|
|
std::vector<char> marks; /* ... and whether each was marked for a preview */
|
|
};
|
|
|
|
/* Staging regions: their offsets in the host span and every rank's slab. */
|
|
struct Region { int64_t off = 0, bytes = 0, used = 0; };
|
|
enum { R_TOK, R_POS, R_ROPE, R_CU, R_MMROW, R_ENCPIX, R_ENCCRD, R_ENCCU, R_SRC, R_GROPE,
|
|
R_SEG, R_SLOT, R_PX, R_MASK, R_SIG, R_SIGN, R_CFG, R_PAIR, R_LANE, R_FLAGS, R_SEED,
|
|
R_BT, R_USED, R_N };
|
|
|
|
int layout_regions();
|
|
template <class T> T* hptr(int r) const { return reinterpret_cast<T*>(host_ + reg_[r].off); }
|
|
template <class T> T* dptr(int rank, int r) const {
|
|
return reinterpret_cast<T*>((char*)dev_[(size_t)rank] + reg_[r].off);
|
|
}
|
|
int upload();
|
|
void finish_batches(int64_t n_tok, int64_t n_seq, int32_t max_q, int step);
|
|
|
|
int admit_locked();
|
|
void release_kv_locked(Job& j);
|
|
void release_locked(Job& j);
|
|
void fail_locked(Job& j, int status, const std::string& why);
|
|
Job* job_locked(uint64_t id);
|
|
|
|
const RadBatch* build_cond(Job& j, int branch, int step);
|
|
const RadBatch* build_denoise(const std::vector<int32_t>& lanes, int step);
|
|
const RadBatch* build_decode(int32_t lane, bool preview, int step);
|
|
|
|
RadGeneratorDecl decl_{};
|
|
std::vector<double> fixed_;
|
|
int64_t max_tok_ = 0, max_seqs_ = 0, max_enc_ = 0, patch_dim_ = 0;
|
|
int64_t max_rows_ = 0; /* the longest conditioning sequence */
|
|
int64_t max_px_ = 0; /* pixels the staging holds */
|
|
int64_t block_size_ = 0, max_blocks_ = 0;
|
|
int32_t kv_group_ = 0;
|
|
std::vector<RankIO> ranks_;
|
|
IKVManager* kv_ = nullptr;
|
|
|
|
char* host_ = nullptr; /* pinned, same layout as each slab */
|
|
int64_t bytes_ = 0;
|
|
std::vector<void*> dev_;
|
|
Region reg_[R_N];
|
|
void* img_host_ = nullptr;
|
|
int64_t img_bytes_ = 0;
|
|
int64_t dec_bytes_ = 0;
|
|
std::vector<Readback> rb_;
|
|
|
|
std::vector<RadBatch> out_; /* one per rank */
|
|
std::vector<RadGenBatch> gout_;
|
|
std::vector<RadKVGroupBatch> kout_;
|
|
Pass pass_;
|
|
/* Host arrays a COND pass's plugin reads on the host (RadGenBatch's _h fields). */
|
|
std::vector<int32_t> host_bidi_, host_px_, host_lanes_;
|
|
|
|
mutable std::mutex mu_;
|
|
std::deque<std::unique_ptr<Job>> jobs_; /* arrival order */
|
|
std::vector<Lane> lanes_;
|
|
std::vector<int32_t> free_lanes_;
|
|
uint64_t next_id_ = 1;
|
|
uint64_t next_seq_ = 1;
|
|
bool closed_ = false;
|
|
std::string closed_why_;
|
|
int closed_status_ = RAD_E_STATE;
|
|
};
|
|
|
|
} /* namespace gen */
|
|
} /* namespace rad */
|