Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libopenbuenos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ if (OPENBUENOS_BUILD_TESTS)
add_test(NAME openbuenos_host_test COMMAND openbuenos_host_test)
add_test(NAME tpu_soc_probe_mock COMMAND tpu_soc_probe --mock)

add_executable(openbuenos_adversarial_test tests/openbuenos_adversarial_test.cpp)
target_link_libraries(openbuenos_adversarial_test PRIVATE openbuenos)
target_include_directories(openbuenos_adversarial_test PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
${OPENBUENOS_ROOT}/docs/baremetal-vii
${OPENBUENOS_ROOT}/docs/kernel-abi
)
target_compile_features(openbuenos_adversarial_test PRIVATE cxx_std_17)
target_compile_options(openbuenos_adversarial_test PRIVATE ${OPENBUENOS_COMPILE_FLAGS})
add_test(NAME openbuenos_adversarial_test COMMAND openbuenos_adversarial_test)

add_executable(ob_quant_unpack_test tests/ob_quant_unpack_test.cpp)
target_link_libraries(ob_quant_unpack_test PRIVATE openbuenos)
target_include_directories(ob_quant_unpack_test PRIVATE
Expand Down
35 changes: 35 additions & 0 deletions libopenbuenos/include/openbuenos.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ typedef struct ob_config {
uint8_t priority; /* VII queue priority (default 0) */
uint8_t perf_mode; /* 0 = default, 1..5 = DVFS perf tier (5 = max clocks) */
uint8_t reserved[2];
/*
* Explicit ext[2] weight-cache byte cap. 0 selects MemAvailable auto-size.
* Honored only when struct_size covers this field.
* Environment (wins over this field):
* OPENBUENOS_WEIGHT_CACHE_MB — MiB cap (16..8192). 0 / non-numeric is ignored.
* OPENBUENOS_CACHE_CAP — values <= 1048576 stay the ggml graph-entry count.
* Values above that are an explicit weight-cache byte cap (>= 1.5 GiB).
* Explicit caps are clamped to leave MemAvailable headroom (512 MiB, or half
* when MemAvailable is at most 640 MiB) so a request cannot exhaust RAM.
*/
uint64_t weight_cache_cap_bytes;
} ob_config_t;

/* Execution telemetry returned by invoke / wait / matmul */
Expand Down Expand Up @@ -230,6 +241,7 @@ typedef struct ob_profile_stats {
uint64_t weight_cache_peak_bytes; /* Peak resident ext[2] DMA-BUF bytes */
uint64_t weight_cache_budget_bytes; /* Effective LMKD-safe ext[2] DMA-BUF byte cap */
uint64_t weight_cache_evictions; /* Cumulative weight cache LRU evictions */
uint64_t weight_pack_count; /* Cold ext[2] pack/repack operations */
uint64_t swiglu_calls; /* Number of ob_gemm_swiglu_* calls executed */
uint64_t swiglu_chunks_chained; /* Single-VII in-SRAM SwiGLU chunks dispatched */
uint64_t swiglu_fallbacks; /* SwiGLU calls routed via M7A batched fallback */
Expand Down Expand Up @@ -308,6 +320,29 @@ ob_status_t ob_context_set_profile(ob_context_t* ctx, int enable);
ob_status_t ob_context_get_profile(const ob_context_t* ctx, ob_profile_stats_t* out_profile);
ob_status_t ob_context_reset_profile(ob_context_t* ctx);

/*
* ext[2] weight-cache byte cap. 0 restores MemAvailable auto-size.
* OPENBUENOS_WEIGHT_CACHE_MB and a byte-sized OPENBUENOS_CACHE_CAP still win.
*/
ob_status_t ob_context_set_weight_cache_cap(ob_context_t* ctx, uint64_t cap_bytes);

/* Hit/miss/eviction + repack telemetry for the ext[2] weight cache. */
typedef struct ob_weight_cache_stats {
uint64_t hits;
uint64_t misses;
uint64_t evictions;
uint64_t bytes_resident;
uint64_t peak_bytes;
uint64_t budget_bytes;
uint64_t pack_count;
uint64_t pack_us;
uint32_t entries;
uint32_t reserved;
} ob_weight_cache_stats_t;

ob_status_t ob_context_get_weight_cache_stats(const ob_context_t* ctx,
ob_weight_cache_stats_t* out_stats);

/*
* Opt-in / defaultable FreeTile BUENOS_A0 VLIW/DGC0 emitter switch.
*
Expand Down
38 changes: 36 additions & 2 deletions libopenbuenos/src/ob_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,10 @@ ob_status_t ob_context_create(const ob_config_t* config, ob_context_t** out_ctx)
ctx->mock_mode = (eff.mock_mode != 0);
ctx->cache_capacity = eff.graph_cache_capacity;
ctx->pool_max_bytes = static_cast<size_t>(eff.buffer_pool_max_bytes);
if (config &&
config->struct_size >= offsetof(ob_config_t, weight_cache_cap_bytes) + sizeof(uint64_t)) {
ctx->weight_cache_cap_bytes = config->weight_cache_cap_bytes;
}
#if defined(__linux__)
if (!ctx->mock_mode) {
struct rlimit rl {};
Expand Down Expand Up @@ -1510,7 +1514,7 @@ void ob_context_destroy(ob_context_t* ctx) {
"submit_us=%llu wait_us=%llu dequant_us=%llu sat_retries=%llu spin_hits=%llu "
"poll_waits=%llu pool_hits=%llu pool_misses=%llu w_hits=%llu w_misses=%llu "
"w_evict=%llu w_bytes=%llu w_peak_bytes=%llu w_budget_bytes=%llu "
"w_entries=%llu w_peak_entries=%llu\n",
"w_entries=%llu w_peak_entries=%llu w_pack=%llu\n",
static_cast<unsigned long long>(ps.calls),
static_cast<unsigned long long>(ps.tiles_dispatched),
static_cast<unsigned long long>(ps.horiz_fusions),
Expand Down Expand Up @@ -1539,7 +1543,8 @@ void ob_context_destroy(ob_context_t* ctx) {
static_cast<unsigned long long>(ps.weight_cache_peak_bytes),
static_cast<unsigned long long>(ps.weight_cache_budget_bytes),
static_cast<unsigned long long>(ctx->weight_cache_map.size()),
static_cast<unsigned long long>(ctx->weight_cache_peak_entries));
static_cast<unsigned long long>(ctx->weight_cache_peak_entries),
static_cast<unsigned long long>(ps.weight_pack_count));
}
}
ctx->async_dequant.shutdown();
Expand Down Expand Up @@ -1629,6 +1634,7 @@ ob_status_t ob_context_get_profile(const ob_context_t* ctx, ob_profile_stats_t*
out_profile->weight_cache_peak_bytes = mctx->weight_cache_peak_bytes;
out_profile->weight_cache_budget_bytes = ob::compute_weight_cache_budget_bytes(mctx);
out_profile->weight_cache_evictions = mctx->weight_cache_evictions;
out_profile->weight_pack_count = mctx->weight_pack_count;
out_profile->swiglu_calls = mctx->prof.swiglu_calls;
out_profile->swiglu_chunks_chained = mctx->prof.swiglu_chunks_chained;
out_profile->swiglu_fallbacks = mctx->prof.swiglu_fallbacks;
Expand All @@ -1646,6 +1652,34 @@ ob_status_t ob_context_get_profile(const ob_context_t* ctx, ob_profile_stats_t*
return OB_OK;
}

ob_status_t ob_context_set_weight_cache_cap(ob_context_t* ctx, uint64_t cap_bytes) {
if (!ctx) return OB_ERR_INVALID_ARG;
std::lock_guard<std::mutex> lk(ctx->gemm_mu);
ctx->weight_cache_cap_bytes = cap_bytes;
ctx->weight_cache_max_bytes = 0u;
return OB_OK;
}

ob_status_t ob_context_get_weight_cache_stats(const ob_context_t* ctx,
ob_weight_cache_stats_t* out_stats) {
if (!ctx || !out_stats) return OB_ERR_INVALID_ARG;
std::memset(out_stats, 0, sizeof(*out_stats));
ob_context* mctx = const_cast<ob_context*>(ctx);
std::lock_guard<std::mutex> lk(mctx->gemm_mu);
out_stats->hits = mctx->weight_cache_hits;
out_stats->misses = mctx->weight_cache_misses;
out_stats->evictions = mctx->weight_cache_evictions;
out_stats->bytes_resident = mctx->weight_cache_bytes;
out_stats->peak_bytes = mctx->weight_cache_peak_bytes;
out_stats->budget_bytes = ob::compute_weight_cache_budget_bytes(mctx);
out_stats->pack_count = mctx->weight_pack_count;
out_stats->pack_us = mctx->prof.weight_pack_ns / 1000ULL;
const size_t entries = mctx->weight_cache_map.size();
out_stats->entries = static_cast<uint32_t>(
std::min<size_t>(entries, static_cast<size_t>(std::numeric_limits<uint32_t>::max())));
return OB_OK;
}

ob_status_t ob_context_reset_profile(ob_context_t* ctx) {
if (!ctx) return OB_ERR_INVALID_ARG;
std::lock_guard<std::mutex> glk(ctx->gemm_mu);
Expand Down
Loading
Loading