Archived
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:
1 parent
5838c25343
commit
0c813784a0
8 files changed
+125
-15
No files matched your search
@@ -2210,6 +2210,25 @@ void Builder::finish_buffer_liveness() {
|
||||
* output likewise: the engine reads it after the last op. */
|
||||
if ((rad_buf)i == prog_.logits_buf || (rad_buf)i == prog_.pooled_buf) b.last_use = last_op;
|
||||
}
|
||||
|
||||
/* Region 1 holds what an op the second lane may issue asks for (Program::scratch_bytes_lane1):
|
||||
* every transient buffer of such an op is concurrent (Ctx::lane1_ rule 3). Ctx::issue refuses a
|
||||
* larger request on lane 1 by name, so an op that breaks the rule fails rather than overruns. */
|
||||
prog_.scratch_bytes_lane1 = 0;
|
||||
if (prog_.scratch_regions > 1)
|
||||
for (size_t o = 1; o < prog_.ops.size() && o < op_bufs_.size(); ++o) {
|
||||
bool lane1 = true;
|
||||
for (const std::vector<rad_buf>* v : { &op_bufs_[o].reads, &op_bufs_[o].writes })
|
||||
for (rad_buf h : *v)
|
||||
if (prog_.buffers[h].decl.kind == RAD_BUF_TRANSIENT && !concurrent_bufs_.count(h))
|
||||
lane1 = false;
|
||||
if (!lane1) continue;
|
||||
for (const Band& bd : prog_.ops[o].bands)
|
||||
if (bd.dom[RAD_DOMAIN_DEVICE] &&
|
||||
bd.dom[RAD_DOMAIN_DEVICE].scratch_bytes > prog_.scratch_bytes_lane1)
|
||||
prog_.scratch_bytes_lane1 = bd.dom[RAD_DOMAIN_DEVICE].scratch_bytes;
|
||||
}
|
||||
if (prog_.scratch_bytes_lane1 > prog_.scratch_bytes) prog_.scratch_bytes_lane1 = prog_.scratch_bytes;
|
||||
}
|
||||
|
||||
int Builder::finish() {
|
||||
|
||||
@@ -458,6 +458,11 @@ public:
|
||||
* region safely through in-order launch; a second lane is a second stream and needs its own.
|
||||
* Two when the architecture declared a buffer rad_buf_concurrent. */
|
||||
int32_t scratch_regions = 1;
|
||||
/* Region 1's size when there are two: the largest device scratch of an op the second lane may
|
||||
* issue. By Ctx::lane1_'s rule 3 such an op touches only rad_buf_concurrent buffers, so an op
|
||||
* with any other transient buffer never runs there; one naming no transient buffer counts.
|
||||
* At most scratch_bytes; -1 (a program not built by the Builder) means scratch_bytes. */
|
||||
int64_t scratch_bytes_lane1 = -1;
|
||||
|
||||
/* Everything that resolved to nothing, reported whole rather than stopping at the first
|
||||
* failure (spec §3.1). Structured because the domains are reported differently: a device miss
|
||||
|
||||
+12
-5
@@ -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
@@ -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
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1895,6 +1895,8 @@ TEST(the_logits_pooled_and_concurrent_buffers_live_past_their_declared_uses) {
|
||||
CHECK_EQ(by->first_def, 0);
|
||||
CHECK_EQ(by->last_use, 4);
|
||||
CHECK_EQ(p.scratch_regions, 2);
|
||||
/* The second lane's region is bounded by the ops it may issue, never the larger. */
|
||||
CHECK(p.scratch_bytes_lane1 >= 0 && p.scratch_bytes_lane1 <= p.scratch_bytes);
|
||||
/* Without the extension y (op 4) would take the bytes the output (op 1) left. */
|
||||
CHECK(by->arena_offset >= bo->arena_offset + bo->bytes ||
|
||||
bo->arena_offset >= by->arena_offset + by->bytes);
|
||||
|
||||
@@ -911,6 +911,32 @@ TEST(a_declared_second_lane_has_its_own_scratch_region) {
|
||||
}
|
||||
}
|
||||
|
||||
/* A request on the second lane larger than its region (an op whose buffers are not all
|
||||
* rad_buf_concurrent, breaking Ctx::lane1_ rule 3) is refused by name; on lane 0 the same op runs. */
|
||||
TEST(a_second_lane_scratch_request_past_its_region_is_refused_by_name) {
|
||||
Fixture f;
|
||||
f.P.scratch_bytes = kScrBytes;
|
||||
f.P.scratch_bytes_lane1 = kScrBytes / 4;
|
||||
f.P.scratch_regions = 2;
|
||||
rad_buf x = f.add_buffer("x", RAD_BF16, 64, 4, 0);
|
||||
rad_op a0 = f.add_op("gemm_nt", "M", { 8192 }, { k_scr_seen0 }, { "s0" }, nullptr, {});
|
||||
f.P.ops[a0].bands[0].dom[RAD_DOMAIN_DEVICE].scratch_bytes = kScrBytes;
|
||||
CHECK_OK(f.start());
|
||||
|
||||
CHECK_OK(f.step(4, 0, [&](Ctx& c) {
|
||||
RadOperand opd[1] = { RAD_B(x) };
|
||||
CHECK_OK(c.issue(a0, opd, 1, RAD_N_BATCH));
|
||||
}));
|
||||
const int s = f.step(4, 1, [&](Ctx& c) {
|
||||
RadOperand opd[1] = { RAD_B(x) };
|
||||
CHECK_OK(c.set_lane(1));
|
||||
c.issue(a0, opd, 1, RAD_N_BATCH);
|
||||
CHECK_OK(c.set_lane(0));
|
||||
});
|
||||
CHECK_EQ(s, RAD_E_SCRATCH);
|
||||
CHECK(std::strstr(f.ctx.step_error(), "rad_buf_concurrent") != nullptr);
|
||||
}
|
||||
|
||||
/* Operands are positional (spec §2.3), so a miscount is silent numerical garbage at the kernel,
|
||||
* not a diagnosable failure. The runtime checks the given arity against the schema. */
|
||||
TEST(operand_arity_is_checked_against_the_schema) {
|
||||
@@ -1255,6 +1281,30 @@ TEST(the_predicted_arena_counts_every_scratch_region) {
|
||||
a.release();
|
||||
}
|
||||
|
||||
/* Region 1 sized apart (Program::scratch_bytes_lane1): the prediction and the allocation agree,
|
||||
* region 1 starts right after region 0 and ends inside the arena. */
|
||||
TEST(the_second_lanes_scratch_region_takes_its_own_size) {
|
||||
Fixture f;
|
||||
f.add_buffer("x", RAD_BF16, 16, 16, 0);
|
||||
f.P.arena_bytes = 512;
|
||||
f.P.scratch_bytes = 3 * RAD_ALIGN_UNIT;
|
||||
f.P.scratch_bytes_lane1 = 300;
|
||||
f.P.scratch_regions = 2;
|
||||
int64_t dev = 0, total = 0;
|
||||
rad::arena_plan_bytes(f.P, &dev, &total);
|
||||
CHECK_EQ(total, rad::align_up(dev, RAD_ALIGN_UNIT) + 3 * RAD_ALIGN_UNIT +
|
||||
rad::align_up(300, RAD_ALIGN_UNIT));
|
||||
rad::Arena a;
|
||||
CHECK_OK(a.init(dev, f.P.scratch_bytes, RAD_MEM_DEVICE, f.P.scratch_regions,
|
||||
f.P.scratch_bytes_lane1));
|
||||
CHECK_EQ(a.bytes(), total);
|
||||
CHECK_EQ(a.scratch_bytes(0), 3 * RAD_ALIGN_UNIT);
|
||||
CHECK_EQ(a.scratch_bytes(1), 300);
|
||||
CHECK_EQ((char*)a.scratch(1) - (char*)a.scratch(0), 3 * RAD_ALIGN_UNIT);
|
||||
CHECK((char*)a.scratch(1) + 300 <= a.base() + a.bytes());
|
||||
a.release();
|
||||
}
|
||||
|
||||
/* The high-water mark is over offset + size, not a sum of sizes. The buffer plan reuses offsets
|
||||
* for buffers whose lifetimes do not overlap, so summing would reserve several times the needed
|
||||
* arena, which comes out of the weight slab. */
|
||||
|
||||
Reference in new issue
Block a user