Skip to content
Merged
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
40 changes: 40 additions & 0 deletions libopenbuenos/include/openbuenos.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,23 @@ ob_status_t ob_context_buffer_pool_stats(const ob_context_t* ctx,
uint64_t* out_pool_hits,
uint64_t* out_pool_misses);

/*
* Maps an external Linux DMA-BUF file descriptor into ctx's grouped EdgeTPU
* IOMMU domain (shared PASID with ctx->tpu_fd) via EDGETPU_MAP_DMABUF.
*
* `flags` accepts OB_BUF_TO_DEVICE / OB_BUF_FROM_DEVICE / OB_BUF_BIDIRECTIONAL
* (defaulting to bidirectional 36-bit IOVA). In mock_mode, validates arguments
* and returns a deterministic non-zero 36-bit synthetic IOVA.
*/
ob_status_t ob_dmabuf_map(ob_context_t* ctx,
int dmabuf_fd,
uint64_t offset,
uint64_t size,
uint32_t flags,
uint64_t* out_iova);

ob_status_t ob_dmabuf_unmap(ob_context_t* ctx, uint64_t iova);

/* =========================================================================
* Graph Registration (Persistent LRU Handle Cache) & Sync/Async Execution
* ========================================================================= */
Expand Down Expand Up @@ -694,6 +711,29 @@ ob_status_t ob_gemm_unregister_immutable_weights(ob_context_t* ctx,
const void* base,
size_t size);

typedef struct ob_prepack_stats {
uint32_t tiles_checked;
uint32_t tiles_prepacked;
uint32_t tiles_already_cached;
uint32_t tiles_skipped_budget;
uint32_t graphs_precompiled;
uint64_t bytes_resident;
} ob_prepack_stats_t;

/*
* Pre-packs a batched group of static quantized weights (e.g. Q+K+V or single projection)
* into ext[2] DMA-BUFs and pre-compiles their DIVE shape executables ONCE at model load
* time, eliminating runtime cache misses, weight packing, and first-token graph compile
* stalls. Stops cleanly without evicting resident entries if the weight-cache budget is full.
* Note: jobs[j].c_mn may be NULL during prepack.
*/
ob_status_t ob_gemm_prepack_quant_batch(ob_context_t* ctx,
uint32_t m_hint,
uint32_t k_dim,
const ob_gemm_f32_quant_job_t* jobs,
uint32_t num_jobs,
ob_prepack_stats_t* out_stats);

ob_status_t ob_gemm_swiglu_int8(ob_context_t* ctx,
const int8_t* x_mk,
const ob_swiglu_int8_desc_t* desc,
Expand Down
129 changes: 129 additions & 0 deletions libopenbuenos/src/ob_buffer_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,27 @@ void buffer_pool_clear(ob_context* ctx) {
ctx->pool_current_count = 0;
}

void ext_dmabuf_clear(ob_context* ctx) {
if (!ctx) return;
std::lock_guard<std::mutex> lk(ctx->pool_mu);
#if defined(__linux__)
if (!ctx->mock_mode && ctx->tpu_fd >= 0) {
for (const auto& kv : ctx->ext_dmabuf_maps) {
const ExternalDmabufMap& rec = kv.second;
struct edgetpu_map_dmabuf_ioctl u {};
u.offset = rec.offset;
u.size = rec.size;
u.device_address = rec.iova;
u.dmabuf_fd = rec.dmabuf_fd;
u.flags = rec.map_flags;
u.die_index = rec.die_index;
::ioctl(ctx->tpu_fd, EDGETPU_UNMAP_DMABUF, &u);
}
}
#endif
ctx->ext_dmabuf_maps.clear();
}

ob_status_t buffer_sync_begin_read(ob_buffer* buf) {
if (!buf || !buf->ctx) return OB_ERR_INVALID_ARG;
if (buf->ctx->mock_mode) return OB_OK;
Expand Down Expand Up @@ -543,4 +564,112 @@ ob_status_t ob_context_buffer_pool_stats(const ob_context_t* ctx,
return OB_OK;
}

ob_status_t ob_dmabuf_map(ob_context_t* ctx,
int dmabuf_fd,
uint64_t offset,
uint64_t size,
uint32_t flags,
uint64_t* out_iova) {
if (out_iova) *out_iova = 0u;
if (!ctx || dmabuf_fd < 0 || size == 0u || !out_iova) {
return OB_ERR_INVALID_ARG;
}
if (offset > (UINT64_MAX - size) || size > (UINT64_MAX - 4095ULL)) {
return OB_ERR_INVALID_ARG;
}

const uint32_t map_flags = ob::map_flags_from_ob_flags(flags);
std::lock_guard<std::mutex> lk(ctx->pool_mu);

if (ctx->mock_mode) {
const uint64_t aligned = (size + 4095ULL) & ~4095ULL;
const uint64_t iova = ctx->next_mock_iova;
ctx->next_mock_iova += (aligned > 0u) ? aligned : 4096ULL;
ob::ExternalDmabufMap rec{};
rec.dmabuf_fd = dmabuf_fd;
rec.offset = offset;
rec.size = size;
rec.iova = iova;
rec.map_flags = map_flags;
rec.die_index = 0xffffffffu;
ctx->ext_dmabuf_maps[iova] = rec;
*out_iova = iova;
return OB_OK;
}

#if defined(__linux__)
if (ctx->tpu_fd < 0) {
ob::set_last_error(ctx, "ob_dmabuf_map: EdgeTPU device fd is not open");
return OB_ERR_DEVICE;
}
struct edgetpu_map_dmabuf_ioctl m {};
m.offset = offset;
m.size = size;
m.dmabuf_fd = dmabuf_fd;
m.flags = map_flags;
m.die_index = 0xffffffffu;
if (::ioctl(ctx->tpu_fd, EDGETPU_MAP_DMABUF, &m) != 0) {
const int e = errno;
ob::set_last_error(ctx, "EDGETPU_MAP_DMABUF failed errno=" + std::to_string(e));
return OB_ERR_IOCTL;
}
ob::ExternalDmabufMap rec{};
rec.dmabuf_fd = dmabuf_fd;
rec.offset = offset;
rec.size = size;
rec.iova = m.device_address;
rec.map_flags = map_flags;
rec.die_index = m.die_index;
ctx->ext_dmabuf_maps[m.device_address] = rec;
*out_iova = m.device_address;
return OB_OK;
#else
ob::set_last_error(ctx, "Non-mock DMA-BUF mapping requires Linux/Android");
return OB_ERR_UNSUPPORTED;
#endif
}

ob_status_t ob_dmabuf_unmap(ob_context_t* ctx, uint64_t iova) {
if (!ctx || iova == 0u) {
return OB_ERR_INVALID_ARG;
}

std::lock_guard<std::mutex> lk(ctx->pool_mu);
auto it = ctx->ext_dmabuf_maps.find(iova);
if (it == ctx->ext_dmabuf_maps.end()) {
return OB_ERR_NOT_FOUND;
}
#if defined(__linux__)
const ob::ExternalDmabufMap rec = it->second;
#endif
ctx->ext_dmabuf_maps.erase(it);

if (ctx->mock_mode) {
return OB_OK;
}

#if defined(__linux__)
if (ctx->tpu_fd < 0) {
ob::set_last_error(ctx, "ob_dmabuf_unmap: EdgeTPU device fd is not open");
return OB_ERR_DEVICE;
}
struct edgetpu_map_dmabuf_ioctl u {};
u.offset = rec.offset;
u.size = rec.size;
u.device_address = rec.iova;
u.dmabuf_fd = rec.dmabuf_fd;
u.flags = rec.map_flags;
u.die_index = rec.die_index;
if (::ioctl(ctx->tpu_fd, EDGETPU_UNMAP_DMABUF, &u) != 0) {
const int e = errno;
ob::set_last_error(ctx, "EDGETPU_UNMAP_DMABUF failed errno=" + std::to_string(e));
return OB_ERR_IOCTL;
}
return OB_OK;
#else
ob::set_last_error(ctx, "Non-mock DMA-BUF unmapping requires Linux/Android");
return OB_ERR_UNSUPPORTED;
#endif
}

} // extern "C"
8 changes: 6 additions & 2 deletions libopenbuenos/src/ob_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,8 @@ void ob_context_destroy(ob_context_t* ctx) {
"hw_us=%llu wall_us=%llu quant_us=%llu hash_us=%llu pack_us=%llu dma_sync_us=%llu "
"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\n",
"w_evict=%llu w_bytes=%llu w_peak_bytes=%llu w_budget_bytes=%llu "
"w_entries=%llu w_peak_entries=%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 All @@ -1536,14 +1537,17 @@ void ob_context_destroy(ob_context_t* ctx) {
static_cast<unsigned long long>(ps.weight_cache_evictions),
static_cast<unsigned long long>(ps.weight_cache_bytes),
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>(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));
}
}
ctx->async_dequant.shutdown();
ob::gemm_release_pipeline_buffers(ctx);
ctx->worker_pool.shutdown();
ob::graph_cache_clear(ctx);
ob::shared_image_clear(ctx);
ob::ext_dmabuf_clear(ctx);
ob::device_close(ctx);
ob::buffer_pool_clear(ctx);
delete ctx;
Expand Down
Loading
Loading