core: the second lane's scratch region sized by the ops it may issue

A program naming a buffer to rad_buf_concurrent got two scratch regions of the program's largest kernel
scratch each, though by Ctx::lane1_ rule 3 an op on the second lane touches only rad_buf_concurrent
buffers. Program::scratch_bytes_lane1 is the largest device scratch of such an op (or one naming no
transient buffer); the Arena takes per-region sizes, arena_plan_bytes counts them for the VRAM budget,
and Ctx::issue refuses a larger lane-1 request by name instead of overrunning the region. Qwen3.8 with
its drafter: region 1 0 B instead of 50 MiB (17 MiB without speculation).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
mici81andClaude Opus 5.5 authored and StillDeadcode committed 2026-10-24 11:10:08 +02:00
1 parent 5838c25343
commit 0c813784a0
8 files changed
+125 -15

No files matched your search

+12 -5
View File
@@ -5,15 +5,18 @@
namespace rad {
int Arena::init(int64_t plan_bytes, int64_t scratch_bytes, int kind, int regions) {
int Arena::init(int64_t plan_bytes, int64_t scratch_bytes, int kind, int regions,
int64_t scratch_bytes1) {
release();
if (plan_bytes < 0 || scratch_bytes < 0 || regions < 1) return RAD_E_INVAL;
if (scratch_bytes1 < 0 || scratch_bytes1 > scratch_bytes) scratch_bytes1 = scratch_bytes;
/* Scratch starts on a page boundary, so a workspace gets 4 KiB alignment for at most one
* page of padding. */
const int64_t off = align_up(plan_bytes, RAD_ALIGN_UNIT);
const int64_t pitch = align_up(scratch_bytes, RAD_ALIGN_UNIT);
const int64_t tail = pitch * regions;
const int64_t pitch = align_up(scratch_bytes, RAD_ALIGN_UNIT);
const int64_t pitch1 = align_up(scratch_bytes1, RAD_ALIGN_UNIT);
const int64_t tail = pitch + pitch1 * (regions - 1);
int64_t total = off + tail;
/* Never zero bytes: a null base would turn a plan bug into an anonymous segfault. */
@@ -33,6 +36,8 @@ int Arena::init(int64_t plan_bytes, int64_t scratch_bytes, int kind, int regions
scratch_off_ = off;
scratch_bytes_ = scratch_bytes;
scratch_pitch_ = pitch;
scratch_bytes1_ = regions > 1 ? scratch_bytes1 : 0;
scratch_pitch1_ = regions > 1 ? pitch1 : 0;
scratch_regions_ = regions;
return RAD_OK;
}
@@ -87,8 +92,10 @@ int64_t arena_plan_bytes(const Program& P, int64_t* dev_out, int64_t* total_out)
/* The staging region sits above every buffer, so it is a separate term. */
if (P.arena_stage_bytes > 0 && P.arena_stage_off >= 0)
dev_bytes = std::max(dev_bytes, P.arena_stage_off + P.arena_stage_bytes);
int64_t total = align_up(dev_bytes, RAD_ALIGN_UNIT) +
align_up(P.scratch_bytes, RAD_ALIGN_UNIT) * std::max<int64_t>(P.scratch_regions, 1);
const int64_t s1 = P.scratch_bytes_lane1 < 0 || P.scratch_bytes_lane1 > P.scratch_bytes
? P.scratch_bytes : P.scratch_bytes_lane1;
int64_t total = align_up(dev_bytes, RAD_ALIGN_UNIT) + align_up(P.scratch_bytes, RAD_ALIGN_UNIT) +
align_up(s1, RAD_ALIGN_UNIT) * (std::max<int64_t>(P.scratch_regions, 1) - 1);
if (total == 0) total = RAD_ALIGN_UNIT; /* Arena::init's floor, for the same reason */
if (dev_out) *dev_out = dev_bytes;
if (total_out) *total_out = total;
+13 -4
View File
@@ -25,18 +25,25 @@ public:
Arena& operator=(const Arena&) = delete;
/* `kind` is a RAD_MEM_* class: RAD_MEM_DEVICE for the device arena, RAD_MEM_HOST for the
* second arena a program with RAD_DOMAIN_HOST buffers gets. `regions` scratch regions of
* `scratch_bytes` each sit at the tail, one per lane. */
int init(int64_t plan_bytes, int64_t scratch_bytes, int kind, int regions = 1);
* second arena a program with RAD_DOMAIN_HOST buffers gets. `regions` scratch regions sit at the
* tail, one per lane: region 0 of `scratch_bytes`, the others of `scratch_bytes1` (-1: the same;
* Program::scratch_bytes_lane1). */
int init(int64_t plan_bytes, int64_t scratch_bytes, int kind, int regions = 1,
int64_t scratch_bytes1 = -1);
void release();
char* base() const { return base_; }
/* Region `r`, or region 0 when the arena holds fewer. */
void* scratch(int r = 0) const {
if (!base_) return nullptr;
return base_ + scratch_off_ + (r > 0 && r < scratch_regions_ ? r * scratch_pitch_ : 0);
return base_ + scratch_off_ +
(r > 0 && r < scratch_regions_ ? scratch_pitch_ + (r - 1) * scratch_pitch1_ : 0);
}
/* Region 0's size (the program's largest kernel), or region `r`'s. */
int64_t scratch_bytes() const { return scratch_bytes_; }
int64_t scratch_bytes(int r) const {
return r > 0 && r < scratch_regions_ ? scratch_bytes1_ : scratch_bytes_;
}
int scratch_regions() const { return scratch_regions_; }
int64_t bytes() const { return bytes_; }
bool live() const { return base_ != nullptr; }
@@ -48,6 +55,8 @@ private:
int64_t scratch_off_ = 0;
int64_t scratch_bytes_ = 0;
int64_t scratch_pitch_ = 0; /* scratch_bytes_ padded to RAD_ALIGN_UNIT */
int64_t scratch_bytes1_ = 0; /* each region past 0 */
int64_t scratch_pitch1_ = 0;
int scratch_regions_ = 1;
};
+11 -4
View File
@@ -153,13 +153,20 @@ int Ctx::bind_arena() {
int64_t dev_bytes = 0, total = 0;
arena_plan_bytes(P, &dev_bytes, &total);
RAD_TRY(arena_.init(dev_bytes, P.scratch_bytes, RAD_MEM_DEVICE, std::max(P.scratch_regions, 1)));
RAD_TRY(arena_.init(dev_bytes, P.scratch_bytes, RAD_MEM_DEVICE, std::max(P.scratch_regions, 1),
P.scratch_bytes_lane1));
/* The arena is in no pool and scales with max_tok, so core/mem/vram_budget.cpp charges it by
* name rather than leaving it to --gpu-headroom-mib. This log line must agree with that
* charge. */
RAD_INFO("arena: %s device + %d x %s scratch = %s, charged to the VRAM budget before it was "
"split", humanb(dev_bytes).c_str(), arena_.scratch_regions(),
humanb(P.scratch_bytes).c_str(), humanb(total).c_str());
if (arena_.scratch_regions() > 1)
RAD_INFO("arena: %s device + %s scratch + %s for the second lane = %s, charged to the VRAM "
"budget before it was split", humanb(dev_bytes).c_str(),
humanb(P.scratch_bytes).c_str(), humanb(arena_.scratch_bytes(1)).c_str(),
humanb(total).c_str());
else
RAD_INFO("arena: %s device + %s scratch = %s, charged to the VRAM budget before it was "
"split", humanb(dev_bytes).c_str(), humanb(P.scratch_bytes).c_str(),
humanb(total).c_str());
if (host_hw > 0) {
/* RAD_MEM_HOST_MAPPED, not RAD_MEM_HOST: nothing copies between the two arenas, so device
+13 -2
View File
@@ -874,7 +874,18 @@ int Ctx::issue_body(rad_op h, const RadOperand* opd, int n_opd, int64_t n) {
* correctness. That also covers lane 0 before lane 1 exists, since a lane built later has
* nothing ordering it behind lane 0's earlier work. */
if (ip.args.scratch_bytes > 0 && dom != RAD_DOMAIN_HOST && arena_.scratch_regions() > 1) {
/* Per-lane regions: nothing to order. */
/* Per-lane regions: nothing to order. Region 1 holds what an op the second lane may issue
* asked for at declare (Program::scratch_bytes_lane1); a larger request there comes from an
* op with a transient buffer outside rad_buf_concurrent, which breaks Ctx::lane1_'s rule 3
* in its buffers too, so it is refused by name rather than let overrun the region. */
if (ip.args.scratch_bytes > arena_.scratch_bytes(lane_)) {
return abort_step_msg(h, RAD_E_SCRATCH,
fmt("kernel %s at n=%lld asks for %lld bytes of scratch on lane %d, whose region "
"holds %lld: an op on the second lane may touch only buffers named to "
"rad_buf_concurrent (Ctx::lane1_ rule 3)",
ip.row && ip.row->info ? ip.row->info->name : "?", (long long)n,
(long long)ip.args.scratch_bytes, lane_, (long long)arena_.scratch_bytes(lane_)));
}
ip.args.scratch = scratch_take(lane_, ip.args.scratch_bytes);
} else if (ip.args.scratch_bytes > 0 && dom != RAD_DOMAIN_HOST) {
ip.args.scratch = scratch_take(0, ip.args.scratch_bytes);
@@ -1048,7 +1059,7 @@ void* Ctx::scratch_take(int region, int64_t bytes) {
constexpr int64_t kAlign = 256;
const int64_t need = (bytes + kAlign - 1) / kAlign * kAlign;
int64_t off = scratch_cur_[region];
if (off + need > arena_.scratch_bytes()) off = 0;
if (off + need > arena_.scratch_bytes(region)) off = 0;
scratch_cur_[region] = off + need;
return (char*)arena_.scratch(region) + off;
}