From fa927912597b8783c4cdd0daf125fd8cfd59d656 Mon Sep 17 00:00:00 2001 From: Bobby Bones Date: Fri, 4 Dec 2026 20:38:50 -0500 Subject: [PATCH] chore(freetpu): align freesched C API and backend with freetpu_* symbols --- include/freesched.h | 8 +- include/pocketcoder_runner.h | 22 +- src/fs_backend_freetpu.cpp | 342 ++++++++++++------------ src/fs_backend_vulkan_dmabuf.cpp | 2 +- src/pc_dmabuf_graph.cpp | 22 +- tests/test_c11_abi.c | 4 +- tests/test_pocketcoder.cpp | 18 +- tools/pocketcoder_run.cpp | 40 +-- tools/volcanic_tpu_dmabuf_bench.cpp | 70 ++--- tools/volcanic_tpu_token_loop_bench.cpp | 66 ++--- 10 files changed, 297 insertions(+), 297 deletions(-) diff --git a/include/freesched.h b/include/freesched.h index 5fc75da..f4b3dd0 100644 --- a/include/freesched.h +++ b/include/freesched.h @@ -153,7 +153,7 @@ typedef struct fs_dev_info { float handoff_overhead_us; /* Baseline sync_file / cache-sync handoff latency */ float typical_watts; uint32_t sram_kb; - uint32_t max_inflight; /* Matches OB_MAX_CREDITS (16) on EdgeTPU */ + uint32_t max_inflight; /* Matches FREETPU_MAX_CREDITS (16) on EdgeTPU */ fs_thermal_power_model_t thermal_model; /* Zero-init (NONE) falls back to typical_watts */ float junction_temp_c; /* Default T_j (degC); 0 or NaN -> ref_temp_c */ uint32_t opp_count; /* 0 = use device peaks */ @@ -203,7 +203,7 @@ typedef enum fs_dtype { FS_DTYPE_I32 = 5 } fs_dtype_t; -/* Matches enum ob_activation in freetpu.h */ +/* Matches enum freetpu_activation in freetpu.h */ typedef enum fs_activation { FS_ACT_NONE = 0, FS_ACT_RELU = 1, @@ -212,7 +212,7 @@ typedef enum fs_activation { FS_ACT_GELU = 5 } fs_activation_t; -/* Matches ob_block_q8_0_t (34 B) and ob_block_q4_0_t (18 B) byte-for-byte */ +/* Matches freetpu_block_q8_0_t (34 B) and freetpu_block_q4_0_t (18 B) byte-for-byte */ #if defined(__GNUC__) || defined(__clang__) typedef struct __attribute__((packed)) fs_block_q8_0 { uint16_t d; @@ -520,7 +520,7 @@ typedef struct fs_freetpu_config { const char* device_path; /* NULL -> "/dev/edgetpu-soc" */ const char* dma_heap_path; /* NULL -> "/dev/dma_heap/system" */ int allow_mock_fallback; /* 1 = run exact host reference if TPU/lib absent */ - uint8_t perf_mode; /* 0..5 DVFS tier matching ob_config_t::perf_mode */ + uint8_t perf_mode; /* 0..5 DVFS tier matching freetpu_config_t::perf_mode */ uint8_t reserved[3]; const char* dive_interp_path; /* NULL -> FREETPU_DIVE_INTERP_PATH / firmware probe */ int enable_freetile; /* 1 = enable FreeTile BUENOS_A0 VLIW emitter (default 1) */ diff --git a/include/pocketcoder_runner.h b/include/pocketcoder_runner.h index 228ace1..f3f0e17 100644 --- a/include/pocketcoder_runner.h +++ b/include/pocketcoder_runner.h @@ -324,17 +324,17 @@ typedef struct pc_graph_stats { uint64_t dma_sync_calls; double max_abs_err; double nmse; - int ob_hw_active; - int ob_emitter_active; - int ob_freetile_active; - uint64_t ob_calls; - uint64_t ob_tiles_dispatched; - uint64_t ob_hw_us; - uint64_t ob_w_hits; - uint64_t ob_w_misses; - uint64_t ob_w_evictions; - uint64_t ob_weight_pack_us; - uint64_t ob_weight_cache_bytes; + int freetpu_hw_active; + int freetpu_emitter_active; + int freetpu_freetile_active; + uint64_t freetpu_calls; + uint64_t freetpu_tiles_dispatched; + uint64_t freetpu_hw_us; + uint64_t freetpu_w_hits; + uint64_t freetpu_w_misses; + uint64_t freetpu_w_evictions; + uint64_t freetpu_weight_pack_us; + uint64_t freetpu_weight_cache_bytes; } pc_graph_stats_t; typedef struct pc_graph pc_graph_t; diff --git a/src/fs_backend_freetpu.cpp b/src/fs_backend_freetpu.cpp index c72df99..4009a34 100644 --- a/src/fs_backend_freetpu.cpp +++ b/src/fs_backend_freetpu.cpp @@ -56,7 +56,7 @@ static_assert(sizeof(fs_edgetpu_map_dmabuf_ioctl) == 40u, "edgetpu_map_dmabuf_io #endif /* __linux__ */ /* 1:1 ABI-compatible mirrors of libfreetpu/include/freetpu.h */ -struct fs_ob_config { +struct fs_ft_config { uint32_t struct_size; int mock_mode; const char* device_path; @@ -74,7 +74,7 @@ struct fs_ob_config { uint8_t reserved[2]; }; -struct fs_ob_invoke_stats { +struct fs_ft_invoke_stats { uint64_t hw_execution_us; uint64_t wall_time_us; uint64_t seq; @@ -85,7 +85,7 @@ struct fs_ob_invoke_stats { uint32_t cache_hits; }; -struct fs_ob_profile_stats { +struct fs_ft_profile_stats { uint64_t quant_us; uint64_t hash_us; uint64_t weight_pack_us; @@ -115,7 +115,7 @@ struct fs_ob_profile_stats { uint64_t swiglu_fallbacks; }; -struct fs_ob_prepack_stats { +struct fs_ft_prepack_stats { uint32_t tiles_checked; uint32_t tiles_prepacked; uint32_t tiles_already_cached; @@ -124,7 +124,7 @@ struct fs_ob_prepack_stats { uint64_t bytes_resident; }; -struct fs_ob_gemm_f32_quant_job { +struct fs_ft_gemm_f32_quant_job { const void* b_nk; float* c_mn; uint32_t n_dim; @@ -133,7 +133,7 @@ struct fs_ob_gemm_f32_quant_job { uint8_t reserved[3]; }; -struct fs_ob_gemm_swiglu_quant_desc { +struct fs_ft_gemm_swiglu_quant_desc { const void* w_gate_hk; const void* w_up_hk; const void* w_down_nh; @@ -148,115 +148,115 @@ struct fs_ob_gemm_swiglu_quant_desc { }; #ifdef FREESCHED_HAVE_LIBFREETPU -static_assert(sizeof(fs_ob_config) == sizeof(ob_config_t), "ob_config_t ABI mismatch"); -static_assert(sizeof(fs_ob_invoke_stats) == sizeof(ob_invoke_stats_t), "ob_invoke_stats_t ABI mismatch"); -static_assert(sizeof(fs_ob_profile_stats) == sizeof(ob_profile_stats_t), "ob_profile_stats_t ABI mismatch"); -static_assert(sizeof(fs_ob_prepack_stats) == sizeof(ob_prepack_stats_t), "ob_prepack_stats_t ABI mismatch"); -static_assert(sizeof(fs_ob_gemm_f32_quant_job) == sizeof(ob_gemm_f32_quant_job_t), - "ob_gemm_f32_quant_job_t ABI mismatch"); -static_assert(sizeof(fs_ob_gemm_swiglu_quant_desc) == sizeof(ob_gemm_swiglu_quant_desc_t), - "ob_gemm_swiglu_quant_desc_t ABI mismatch"); +static_assert(sizeof(fs_ft_config) == sizeof(freetpu_config_t), "freetpu_config_t ABI mismatch"); +static_assert(sizeof(fs_ft_invoke_stats) == sizeof(freetpu_invoke_stats_t), "freetpu_invoke_stats_t ABI mismatch"); +static_assert(sizeof(fs_ft_profile_stats) == sizeof(freetpu_profile_stats_t), "freetpu_profile_stats_t ABI mismatch"); +static_assert(sizeof(fs_ft_prepack_stats) == sizeof(freetpu_prepack_stats_t), "freetpu_prepack_stats_t ABI mismatch"); +static_assert(sizeof(fs_ft_gemm_f32_quant_job) == sizeof(freetpu_gemm_f32_quant_job_t), + "freetpu_gemm_f32_quant_job_t ABI mismatch"); +static_assert(sizeof(fs_ft_gemm_swiglu_quant_desc) == sizeof(freetpu_gemm_swiglu_quant_desc_t), + "freetpu_gemm_swiglu_quant_desc_t ABI mismatch"); #endif -struct fs_ob_api { - int (*ctx_create)(const fs_ob_config*, void**); +struct fs_ft_api { + int (*ctx_create)(const fs_ft_config*, void**); void (*ctx_destroy)(void*); int (*set_profile)(void*, int); - int (*get_profile)(const void*, fs_ob_profile_stats*); + int (*get_profile)(const void*, fs_ft_profile_stats*); int (*set_freetile)(void*, int); int (*get_freetile)(const void*); int (*reg_builtin_emitter)(void*, const char*); int (*set_tile_limits)(void*, uint32_t, uint32_t, uint32_t); int (*reg_immutable)(void*, const void*, size_t, uint64_t); int (*unreg_immutable)(void*, const void*, size_t); - int (*prepack_batch)(void*, uint32_t, uint32_t, const fs_ob_gemm_f32_quant_job*, uint32_t, fs_ob_prepack_stats*); - int (*matmul_batch)(void*, const float*, uint32_t, uint32_t, const fs_ob_gemm_f32_quant_job*, uint32_t, fs_ob_invoke_stats*); - int (*swiglu)(void*, const float*, const fs_ob_gemm_swiglu_quant_desc*, float*, fs_ob_invoke_stats*); + int (*prepack_batch)(void*, uint32_t, uint32_t, const fs_ft_gemm_f32_quant_job*, uint32_t, fs_ft_prepack_stats*); + int (*matmul_batch)(void*, const float*, uint32_t, uint32_t, const fs_ft_gemm_f32_quant_job*, uint32_t, fs_ft_invoke_stats*); + int (*swiglu)(void*, const float*, const fs_ft_gemm_swiglu_quant_desc*, float*, fs_ft_invoke_stats*); int (*dmabuf_map)(void*, int, uint64_t, uint64_t, uint32_t, uint64_t*); int (*dmabuf_unmap)(void*, uint64_t); }; #ifdef FREESCHED_HAVE_LIBFREETPU -static int fs_wrap_ob_ctx_create(const fs_ob_config* c, void** out) { - return static_cast(ob_context_create(reinterpret_cast(c), - reinterpret_cast(out))); +static int fs_wrap_ft_ctx_create(const fs_ft_config* c, void** out) { + return static_cast(freetpu_context_create(reinterpret_cast(c), + reinterpret_cast(out))); } -static void fs_wrap_ob_ctx_destroy(void* ctx) { - ob_context_destroy(static_cast(ctx)); +static void fs_wrap_ft_ctx_destroy(void* ctx) { + freetpu_context_destroy(static_cast(ctx)); } -static int fs_wrap_ob_set_profile(void* ctx, int enable) { - return static_cast(ob_context_set_profile(static_cast(ctx), enable)); +static int fs_wrap_ft_set_profile(void* ctx, int enable) { + return static_cast(freetpu_context_set_profile(static_cast(ctx), enable)); } -static int fs_wrap_ob_get_profile(const void* ctx, fs_ob_profile_stats* out) { - return static_cast(ob_context_get_profile(static_cast(ctx), - reinterpret_cast(out))); +static int fs_wrap_ft_get_profile(const void* ctx, fs_ft_profile_stats* out) { + return static_cast(freetpu_context_get_profile(static_cast(ctx), + reinterpret_cast(out))); } -static int fs_wrap_ob_set_freetile(void* ctx, int enable) { - return static_cast(ob_context_set_freetile(static_cast(ctx), enable)); +static int fs_wrap_ft_set_freetile(void* ctx, int enable) { + return static_cast(freetpu_context_set_freetile(static_cast(ctx), enable)); } -static int fs_wrap_ob_get_freetile(const void* ctx) { - return ob_context_get_freetile(static_cast(ctx)); +static int fs_wrap_ft_get_freetile(const void* ctx) { + return freetpu_context_get_freetile(static_cast(ctx)); } -static int fs_wrap_ob_reg_builtin_emitter(void* ctx, const char* path) { - return static_cast(ob_gemm_register_builtin_emitter(static_cast(ctx), path)); +static int fs_wrap_ft_reg_builtin_emitter(void* ctx, const char* path) { + return static_cast(freetpu_gemm_register_builtin_emitter(static_cast(ctx), path)); } -static int fs_wrap_ob_set_tile_limits(void* ctx, uint32_t m, uint32_t k, uint32_t n) { - return static_cast(ob_gemm_set_tile_limits(static_cast(ctx), m, k, n)); +static int fs_wrap_ft_set_tile_limits(void* ctx, uint32_t m, uint32_t k, uint32_t n) { + return static_cast(freetpu_gemm_set_tile_limits(static_cast(ctx), m, k, n)); } -static int fs_wrap_ob_reg_immutable(void* ctx, const void* base, size_t size, uint64_t ver) { - return static_cast(ob_gemm_register_immutable_weights(static_cast(ctx), base, size, ver)); +static int fs_wrap_ft_reg_immutable(void* ctx, const void* base, size_t size, uint64_t ver) { + return static_cast(freetpu_gemm_register_immutable_weights(static_cast(ctx), base, size, ver)); } -static int fs_wrap_ob_unreg_immutable(void* ctx, const void* base, size_t size) { - return static_cast(ob_gemm_unregister_immutable_weights(static_cast(ctx), base, size)); +static int fs_wrap_ft_unreg_immutable(void* ctx, const void* base, size_t size) { + return static_cast(freetpu_gemm_unregister_immutable_weights(static_cast(ctx), base, size)); } -static int fs_wrap_ob_prepack_batch(void* ctx, +static int fs_wrap_ft_prepack_batch(void* ctx, uint32_t m_hint, uint32_t k_dim, - const fs_ob_gemm_f32_quant_job* jobs, + const fs_ft_gemm_f32_quant_job* jobs, uint32_t num_jobs, - fs_ob_prepack_stats* out) { - return static_cast(ob_gemm_prepack_quant_batch( - static_cast(ctx), m_hint, k_dim, - reinterpret_cast(jobs), num_jobs, - reinterpret_cast(out))); + fs_ft_prepack_stats* out) { + return static_cast(freetpu_gemm_prepack_quant_batch( + static_cast(ctx), m_hint, k_dim, + reinterpret_cast(jobs), num_jobs, + reinterpret_cast(out))); } -static int fs_wrap_ob_matmul_batch(void* ctx, +static int fs_wrap_ft_matmul_batch(void* ctx, const float* a_mk, uint32_t m_dim, uint32_t k_dim, - const fs_ob_gemm_f32_quant_job* jobs, + const fs_ft_gemm_f32_quant_job* jobs, uint32_t num_jobs, - fs_ob_invoke_stats* out) { - return static_cast(ob_gemm_matmul_f32_quant_batch( - static_cast(ctx), a_mk, m_dim, k_dim, - reinterpret_cast(jobs), num_jobs, - reinterpret_cast(out))); + fs_ft_invoke_stats* out) { + return static_cast(freetpu_gemm_matmul_f32_quant_batch( + static_cast(ctx), a_mk, m_dim, k_dim, + reinterpret_cast(jobs), num_jobs, + reinterpret_cast(out))); } -static int fs_wrap_ob_swiglu(void* ctx, +static int fs_wrap_ft_swiglu(void* ctx, const float* a_mk, - const fs_ob_gemm_swiglu_quant_desc* desc, + const fs_ft_gemm_swiglu_quant_desc* desc, float* c_mn, - fs_ob_invoke_stats* out) { - return static_cast(ob_gemm_swiglu_f32_quant( - static_cast(ctx), a_mk, - reinterpret_cast(desc), c_mn, - reinterpret_cast(out))); + fs_ft_invoke_stats* out) { + return static_cast(freetpu_gemm_swiglu_f32_quant( + static_cast(ctx), a_mk, + reinterpret_cast(desc), c_mn, + reinterpret_cast(out))); } -static int fs_wrap_ob_dmabuf_map(void* ctx, +static int fs_wrap_ft_dmabuf_map(void* ctx, int dmabuf_fd, uint64_t offset, uint64_t size, uint32_t flags, uint64_t* out_iova) { return static_cast( - ob_dmabuf_map(static_cast(ctx), dmabuf_fd, offset, size, flags, out_iova)); + freetpu_dmabuf_map(static_cast(ctx), dmabuf_fd, offset, size, flags, out_iova)); } -static int fs_wrap_ob_dmabuf_unmap(void* ctx, uint64_t iova) { - return static_cast(ob_dmabuf_unmap(static_cast(ctx), iova)); +static int fs_wrap_ft_dmabuf_unmap(void* ctx, uint64_t iova) { + return static_cast(freetpu_dmabuf_unmap(static_cast(ctx), iova)); } #endif /* FREESCHED_HAVE_LIBFREETPU */ -struct fs_ob_immutable_reg { +struct fs_ft_immutable_reg { const void* base; size_t size; uint64_t version; @@ -266,13 +266,13 @@ struct fs_freetpu_state { fs_freetpu_config_t cfg; int tpu_dev_fd; void* dl_handle; - void* ob_ctx; - fs_ob_api api; + void* ft_ctx; + fs_ft_api api; int hw_active; int emitter_active; int freetile_active; std::mutex mu; - std::vector imm_regs; + std::vector imm_regs; }; static fs_freetpu_config_t fs_normalize_freetpu_cfg(const fs_freetpu_config_t* cfg) { @@ -320,21 +320,21 @@ static void fs_freetpu_resolve_api(fs_freetpu_state* st, int allow_dlopen) { std::memset(&st->api, 0, sizeof(st->api)); #ifdef FREESCHED_HAVE_LIBFREETPU (void)allow_dlopen; - st->api.ctx_create = fs_wrap_ob_ctx_create; - st->api.ctx_destroy = fs_wrap_ob_ctx_destroy; - st->api.set_profile = fs_wrap_ob_set_profile; - st->api.get_profile = fs_wrap_ob_get_profile; - st->api.set_freetile = fs_wrap_ob_set_freetile; - st->api.get_freetile = fs_wrap_ob_get_freetile; - st->api.reg_builtin_emitter = fs_wrap_ob_reg_builtin_emitter; - st->api.set_tile_limits = fs_wrap_ob_set_tile_limits; - st->api.reg_immutable = fs_wrap_ob_reg_immutable; - st->api.unreg_immutable = fs_wrap_ob_unreg_immutable; - st->api.prepack_batch = fs_wrap_ob_prepack_batch; - st->api.matmul_batch = fs_wrap_ob_matmul_batch; - st->api.swiglu = fs_wrap_ob_swiglu; - st->api.dmabuf_map = fs_wrap_ob_dmabuf_map; - st->api.dmabuf_unmap = fs_wrap_ob_dmabuf_unmap; + st->api.ctx_create = fs_wrap_ft_ctx_create; + st->api.ctx_destroy = fs_wrap_ft_ctx_destroy; + st->api.set_profile = fs_wrap_ft_set_profile; + st->api.get_profile = fs_wrap_ft_get_profile; + st->api.set_freetile = fs_wrap_ft_set_freetile; + st->api.get_freetile = fs_wrap_ft_get_freetile; + st->api.reg_builtin_emitter = fs_wrap_ft_reg_builtin_emitter; + st->api.set_tile_limits = fs_wrap_ft_set_tile_limits; + st->api.reg_immutable = fs_wrap_ft_reg_immutable; + st->api.unreg_immutable = fs_wrap_ft_unreg_immutable; + st->api.prepack_batch = fs_wrap_ft_prepack_batch; + st->api.matmul_batch = fs_wrap_ft_matmul_batch; + st->api.swiglu = fs_wrap_ft_swiglu; + st->api.dmabuf_map = fs_wrap_ft_dmabuf_map; + st->api.dmabuf_unmap = fs_wrap_ft_dmabuf_unmap; #elif defined(__linux__) void* sym_handle = nullptr; if (allow_dlopen) { @@ -349,59 +349,59 @@ static void fs_freetpu_resolve_api(fs_freetpu_state* st, int allow_dlopen) { using FnT = std::remove_reference_t; dst = reinterpret_cast(dlsym(sym_handle, name)); }; - load_sym(st->api.ctx_create, "ob_context_create"); - load_sym(st->api.ctx_destroy, "ob_context_destroy"); - load_sym(st->api.set_profile, "ob_context_set_profile"); - load_sym(st->api.get_profile, "ob_context_get_profile"); - load_sym(st->api.set_freetile, "ob_context_set_freetile"); - load_sym(st->api.get_freetile, "ob_context_get_freetile"); - load_sym(st->api.reg_builtin_emitter, "ob_gemm_register_builtin_emitter"); - load_sym(st->api.set_tile_limits, "ob_gemm_set_tile_limits"); - load_sym(st->api.reg_immutable, "ob_gemm_register_immutable_weights"); - load_sym(st->api.unreg_immutable, "ob_gemm_unregister_immutable_weights"); - load_sym(st->api.prepack_batch, "ob_gemm_prepack_quant_batch"); - load_sym(st->api.matmul_batch, "ob_gemm_matmul_f32_quant_batch"); - load_sym(st->api.swiglu, "ob_gemm_swiglu_f32_quant"); - load_sym(st->api.dmabuf_map, "ob_dmabuf_map"); - load_sym(st->api.dmabuf_unmap, "ob_dmabuf_unmap"); + load_sym(st->api.ctx_create, "freetpu_context_create"); + load_sym(st->api.ctx_destroy, "freetpu_context_destroy"); + load_sym(st->api.set_profile, "freetpu_context_set_profile"); + load_sym(st->api.get_profile, "freetpu_context_get_profile"); + load_sym(st->api.set_freetile, "freetpu_context_set_freetile"); + load_sym(st->api.get_freetile, "freetpu_context_get_freetile"); + load_sym(st->api.reg_builtin_emitter, "freetpu_gemm_register_builtin_emitter"); + load_sym(st->api.set_tile_limits, "freetpu_gemm_set_tile_limits"); + load_sym(st->api.reg_immutable, "freetpu_gemm_register_immutable_weights"); + load_sym(st->api.unreg_immutable, "freetpu_gemm_unregister_immutable_weights"); + load_sym(st->api.prepack_batch, "freetpu_gemm_prepack_quant_batch"); + load_sym(st->api.matmul_batch, "freetpu_gemm_matmul_f32_quant_batch"); + load_sym(st->api.swiglu, "freetpu_gemm_swiglu_f32_quant"); + load_sym(st->api.dmabuf_map, "freetpu_dmabuf_map"); + load_sym(st->api.dmabuf_unmap, "freetpu_dmabuf_unmap"); #else (void)st; (void)allow_dlopen; #endif } -static void fs_ob_ensure_immutable_locked(fs_freetpu_state* st, const fs_dmabuf_t* buf) { - if (!st || !buf || !st->ob_ctx || !st->api.reg_immutable || +static void fs_ft_ensure_immutable_locked(fs_freetpu_state* st, const fs_dmabuf_t* buf) { + if (!st || !buf || !st->ft_ctx || !st->api.reg_immutable || (buf->flags & FS_BUF_IMMUTABLE_WEIGHT) == 0u || !buf->host_ptr || buf->size == 0u) { return; } const uint64_t ver = (buf->generation > 0u) ? buf->generation : 1u; const void* untagged_base = fs_mte_untag_ptr(buf->host_ptr); - for (fs_ob_immutable_reg& reg : st->imm_regs) { + for (fs_ft_immutable_reg& reg : st->imm_regs) { if (fs_mte_untag_ptr(reg.base) == untagged_base && reg.size == buf->size) { if (reg.version == ver) { return; } - if (st->api.reg_immutable(st->ob_ctx, buf->host_ptr, buf->size, ver) == 0) { + if (st->api.reg_immutable(st->ft_ctx, buf->host_ptr, buf->size, ver) == 0) { reg.base = buf->host_ptr; reg.version = ver; } return; } } - if (st->api.reg_immutable(st->ob_ctx, buf->host_ptr, buf->size, ver) == 0) { + if (st->api.reg_immutable(st->ft_ctx, buf->host_ptr, buf->size, ver) == 0) { st->imm_regs.push_back({buf->host_ptr, buf->size, ver}); } } -static void fs_ob_unreg_immutable_locked(fs_freetpu_state* st, const fs_dmabuf_t* buf) { - if (!st || !buf || !st->ob_ctx || !st->api.unreg_immutable || !buf->host_ptr) { +static void fs_ft_unreg_immutable_locked(fs_freetpu_state* st, const fs_dmabuf_t* buf) { + if (!st || !buf || !st->ft_ctx || !st->api.unreg_immutable || !buf->host_ptr) { return; } const void* untagged_base = fs_mte_untag_ptr(buf->host_ptr); for (size_t i = 0u; i < st->imm_regs.size(); ++i) { if (fs_mte_untag_ptr(st->imm_regs[i].base) == untagged_base && st->imm_regs[i].size == buf->size) { - (void)st->api.unreg_immutable(st->ob_ctx, st->imm_regs[i].base, buf->size); + (void)st->api.unreg_immutable(st->ft_ctx, st->imm_regs[i].base, buf->size); st->imm_regs[i] = st->imm_regs.back(); st->imm_regs.pop_back(); break; @@ -431,35 +431,35 @@ static fs_status_t fs_freetpu_init(void* user_data, const fs_topology_t* topo) { const int has_explicit_dive = (dive_path && dive_path[0] != '\0') ? 1 : 0; if (st->api.ctx_create && st->api.ctx_destroy) { - fs_ob_config ob_cfg; - std::memset(&ob_cfg, 0, sizeof(ob_cfg)); - ob_cfg.struct_size = static_cast(sizeof(fs_ob_config)); - ob_cfg.mock_mode = is_mock_topo ? 1 : 0; - ob_cfg.device_path = dev_path; - ob_cfg.dma_heap_path = st->cfg.dma_heap_path; - ob_cfg.max_inflight = 16u; - ob_cfg.graph_cache_capacity = 2048u; - ob_cfg.perf_mode = st->cfg.perf_mode; + fs_ft_config ft_cfg; + std::memset(&ft_cfg, 0, sizeof(ft_cfg)); + ft_cfg.struct_size = static_cast(sizeof(fs_ft_config)); + ft_cfg.mock_mode = is_mock_topo ? 1 : 0; + ft_cfg.device_path = dev_path; + ft_cfg.dma_heap_path = st->cfg.dma_heap_path; + ft_cfg.max_inflight = 16u; + ft_cfg.graph_cache_capacity = 2048u; + ft_cfg.perf_mode = st->cfg.perf_mode; /* - * Let ob_context_create() own the single /dev/edgetpu-soc descriptor with + * Let freetpu_context_create() own the single /dev/edgetpu-soc descriptor with * EDGETPU_CREATE_GROUP / shared PASID instead of opening a duplicate * ungrouped fd in freesched. */ - int ctx_rc = st->api.ctx_create(&ob_cfg, &st->ob_ctx); - if ((ctx_rc != 0 || !st->ob_ctx) && !ob_cfg.mock_mode) { - st->ob_ctx = nullptr; + int ctx_rc = st->api.ctx_create(&ft_cfg, &st->ft_ctx); + if ((ctx_rc != 0 || !st->ft_ctx) && !ft_cfg.mock_mode) { + st->ft_ctx = nullptr; if (!st->cfg.allow_mock_fallback) { return FS_ERR_NO_BACKEND; } - ob_cfg.mock_mode = 1; - ctx_rc = st->api.ctx_create(&ob_cfg, &st->ob_ctx); + ft_cfg.mock_mode = 1; + ctx_rc = st->api.ctx_create(&ft_cfg, &st->ft_ctx); } - if (ctx_rc == 0 && st->ob_ctx) { - st->hw_active = (!ob_cfg.mock_mode) ? 1 : 0; + if (ctx_rc == 0 && st->ft_ctx) { + st->hw_active = (!ft_cfg.mock_mode) ? 1 : 0; if (st->api.set_profile) { - (void)st->api.set_profile(st->ob_ctx, 1); + (void)st->api.set_profile(st->ft_ctx, 1); } int ft_req = st->cfg.enable_freetile ? 1 : 0; @@ -467,22 +467,22 @@ static fs_status_t fs_freetpu_init(void* user_data, const fs_topology_t* topo) { ft_req = (std::atoi(ft_env) != 0) ? 1 : 0; } if (st->api.set_freetile) { - (void)st->api.set_freetile(st->ob_ctx, ft_req); + (void)st->api.set_freetile(st->ft_ctx, ft_req); } if (st->api.get_freetile) { - st->freetile_active = st->api.get_freetile(st->ob_ctx) ? 1 : 0; + st->freetile_active = st->api.get_freetile(st->ft_ctx) ? 1 : 0; } if (st->api.set_tile_limits) { - (void)st->api.set_tile_limits(st->ob_ctx, 64u, 2048u, 2048u); + (void)st->api.set_tile_limits(st->ft_ctx, 64u, 2048u, 2048u); } - if ((ob_cfg.mock_mode == 0 || has_explicit_dive) && st->api.reg_builtin_emitter) { + if ((ft_cfg.mock_mode == 0 || has_explicit_dive) && st->api.reg_builtin_emitter) { const char* p = has_explicit_dive ? dive_path : nullptr; - if (st->api.reg_builtin_emitter(st->ob_ctx, p) == 0) { + if (st->api.reg_builtin_emitter(st->ft_ctx, p) == 0) { st->emitter_active = 1; - } else if (ob_cfg.mock_mode == 0 && !st->cfg.allow_mock_fallback) { - st->api.ctx_destroy(st->ob_ctx); - st->ob_ctx = nullptr; + } else if (ft_cfg.mock_mode == 0 && !st->cfg.allow_mock_fallback) { + st->api.ctx_destroy(st->ft_ctx); + st->ft_ctx = nullptr; st->hw_active = 0; return FS_ERR_NO_BACKEND; } @@ -502,7 +502,7 @@ static fs_status_t fs_freetpu_init(void* user_data, const fs_topology_t* topo) { if (!st->hw_active && !st->cfg.allow_mock_fallback) { return FS_ERR_NO_BACKEND; } - if (!st->ob_ctx && !st->cfg.allow_mock_fallback) { + if (!st->ft_ctx && !st->cfg.allow_mock_fallback) { return FS_ERR_NO_BACKEND; } return FS_OK; @@ -515,15 +515,15 @@ static void fs_freetpu_shutdown(void* user_data) { } { std::lock_guard lk(st->mu); - if (st->ob_ctx && st->api.unreg_immutable) { - for (const fs_ob_immutable_reg& reg : st->imm_regs) { - (void)st->api.unreg_immutable(st->ob_ctx, reg.base, reg.size); + if (st->ft_ctx && st->api.unreg_immutable) { + for (const fs_ft_immutable_reg& reg : st->imm_regs) { + (void)st->api.unreg_immutable(st->ft_ctx, reg.base, reg.size); } st->imm_regs.clear(); } - if (st->ob_ctx && st->api.ctx_destroy) { - st->api.ctx_destroy(st->ob_ctx); - st->ob_ctx = nullptr; + if (st->ft_ctx && st->api.ctx_destroy) { + st->api.ctx_destroy(st->ft_ctx); + st->ft_ctx = nullptr; } } #ifdef __linux__ @@ -574,12 +574,12 @@ static fs_status_t fs_freetpu_import_dmabuf(void* user_data, fs_dmabuf_t* buf) { { std::lock_guard lk(st->mu); - fs_ob_ensure_immutable_locked(st, buf); + fs_ft_ensure_immutable_locked(st, buf); } if ((buf->backing == FS_BACKING_DMA_HEAP || buf->backing == FS_BACKING_IMPORTED) && buf->dmabuf_fd >= 0) { - if (st->ob_ctx) { + if (st->ft_ctx) { if (!st->api.dmabuf_map) { if (!st->cfg.allow_mock_fallback) { return FS_ERR_IOCTL; @@ -587,7 +587,7 @@ static fs_status_t fs_freetpu_import_dmabuf(void* user_data, fs_dmabuf_t* buf) { } else { uint64_t mapped_iova = 0u; const int map_rc = st->api.dmabuf_map( - st->ob_ctx, buf->dmabuf_fd, static_cast(buf->offset), + st->ft_ctx, buf->dmabuf_fd, static_cast(buf->offset), static_cast(buf->size), 0u, &mapped_iova); if (map_rc == 0 && mapped_iova != 0u) { buf->tpu_iova = mapped_iova; @@ -636,12 +636,12 @@ static void fs_freetpu_release_dmabuf(void* user_data, fs_dmabuf_t* buf) { } { std::lock_guard lk(st->mu); - fs_ob_unreg_immutable_locked(st, buf); + fs_ft_unreg_immutable_locked(st, buf); } if ((buf->backing == FS_BACKING_DMA_HEAP || buf->backing == FS_BACKING_IMPORTED) && buf->dmabuf_fd >= 0 && buf->tpu_iova != 0u) { - if (st->ob_ctx && st->api.dmabuf_unmap) { - (void)st->api.dmabuf_unmap(st->ob_ctx, buf->tpu_iova); + if (st->ft_ctx && st->api.dmabuf_unmap) { + (void)st->api.dmabuf_unmap(st->ft_ctx, buf->tpu_iova); } #ifdef __linux__ else if (st->tpu_dev_fd >= 0) { @@ -690,30 +690,30 @@ static fs_status_t fs_freetpu_execute(void* user_data, } } - if (st->ob_ctx && op->act_dtype == FS_DTYPE_F32 && + if (st->ft_ctx && op->act_dtype == FS_DTYPE_F32 && (op->weight_dtype == FS_DTYPE_Q8_0 || op->weight_dtype == FS_DTYPE_Q4_0)) { std::lock_guard lk(st->mu); - fs_ob_ensure_immutable_locked(st, op->input_b); - fs_ob_ensure_immutable_locked(st, op->input_c); - fs_ob_ensure_immutable_locked(st, op->input_d); + fs_ft_ensure_immutable_locked(st, op->input_b); + fs_ft_ensure_immutable_locked(st, op->input_c); + fs_ft_ensure_immutable_locked(st, op->input_d); - fs_ob_invoke_stats ob_stats; - std::memset(&ob_stats, 0, sizeof(ob_stats)); + fs_ft_invoke_stats ft_stats; + std::memset(&ft_stats, 0, sizeof(ft_stats)); const uint8_t is_q4 = (op->weight_dtype == FS_DTYPE_Q4_0) ? 1u : 0u; if ((op->type == FS_OP_DECODE_GEMV || op->type == FS_OP_GEMM || op->type == FS_OP_PREFILL_GEMM) && st->api.matmul_batch && op->input_a && op->input_b && op->output) { - fs_ob_gemm_f32_quant_job job; + fs_ft_gemm_f32_quant_job job; std::memset(&job, 0, sizeof(job)); job.b_nk = op->input_b->host_ptr; job.c_mn = static_cast(op->output->host_ptr); job.n_dim = op->n_dim; job.activation = static_cast(op->activation); job.is_q4_0 = is_q4; - if (st->api.matmul_batch(st->ob_ctx, static_cast(op->input_a->host_ptr), - op->m_dim, op->k_dim, &job, 1u, &ob_stats) == 0) { + if (st->api.matmul_batch(st->ft_ctx, static_cast(op->input_a->host_ptr), + op->m_dim, op->k_dim, &job, 1u, &ft_stats) == 0) { if (out_hw_us) { - *out_hw_us = ob_stats.hw_execution_us > 0u ? ob_stats.hw_execution_us : ob_stats.wall_time_us; + *out_hw_us = ft_stats.hw_execution_us > 0u ? ft_stats.hw_execution_us : ft_stats.wall_time_us; } if (out_signal_fence) { *out_signal_fence = fs_fence_init(FS_DEV_TPU); @@ -726,7 +726,7 @@ static fs_status_t fs_freetpu_execute(void* user_data, } } else if (op->type == FS_OP_SWIGLU && st->api.swiglu && op->input_a && op->input_b && op->input_c && op->input_d && op->output) { - fs_ob_gemm_swiglu_quant_desc desc; + fs_ft_gemm_swiglu_quant_desc desc; std::memset(&desc, 0, sizeof(desc)); desc.w_gate_hk = op->input_b->host_ptr; desc.w_up_hk = op->input_c->host_ptr; @@ -738,10 +738,10 @@ static fs_status_t fs_freetpu_execute(void* user_data, desc.gate_is_q4_0 = is_q4; desc.up_is_q4_0 = is_q4; desc.down_is_q4_0 = is_q4; - if (st->api.swiglu(st->ob_ctx, static_cast(op->input_a->host_ptr), &desc, - static_cast(op->output->host_ptr), &ob_stats) == 0) { + if (st->api.swiglu(st->ft_ctx, static_cast(op->input_a->host_ptr), &desc, + static_cast(op->output->host_ptr), &ft_stats) == 0) { if (out_hw_us) { - *out_hw_us = ob_stats.hw_execution_us > 0u ? ob_stats.hw_execution_us : ob_stats.wall_time_us; + *out_hw_us = ft_stats.hw_execution_us > 0u ? ft_stats.hw_execution_us : ft_stats.wall_time_us; } if (out_signal_fence) { *out_signal_fence = fs_fence_init(FS_DEV_TPU); @@ -816,7 +816,7 @@ fs_status_t fs_backend_register_freetpu(fs_context_t* ctx, st->cfg = fs_normalize_freetpu_cfg(cfg); st->tpu_dev_fd = -1; st->dl_handle = nullptr; - st->ob_ctx = nullptr; + st->ft_ctx = nullptr; st->hw_active = 0; st->emitter_active = 0; st->freetile_active = 0; @@ -859,15 +859,15 @@ fs_status_t fs_backend_freetpu_prepack_op(fs_context_t* ctx, const fs_op_desc_t* } std::lock_guard lk(st->mu); - if (!st->ob_ctx) { + if (!st->ft_ctx) { return FS_OK; } - fs_ob_ensure_immutable_locked(st, op->input_b); + fs_ft_ensure_immutable_locked(st, op->input_b); if (!st->emitter_active || !st->api.prepack_batch) { return FS_OK; } - fs_ob_gemm_f32_quant_job job; + fs_ft_gemm_f32_quant_job job; std::memset(&job, 0, sizeof(job)); job.b_nk = op->input_b->host_ptr; job.c_mn = nullptr; @@ -875,9 +875,9 @@ fs_status_t fs_backend_freetpu_prepack_op(fs_context_t* ctx, const fs_op_desc_t* job.activation = static_cast(op->activation); job.is_q4_0 = (op->weight_dtype == FS_DTYPE_Q4_0) ? 1u : 0u; - fs_ob_prepack_stats pstats; + fs_ft_prepack_stats pstats; std::memset(&pstats, 0, sizeof(pstats)); - const int rc = st->api.prepack_batch(st->ob_ctx, op->m_dim, op->k_dim, &job, 1u, &pstats); + const int rc = st->api.prepack_batch(st->ft_ctx, op->m_dim, op->k_dim, &job, 1u, &pstats); return (rc == 0) ? FS_OK : FS_ERR_DEVICE; } @@ -896,10 +896,10 @@ fs_status_t fs_backend_freetpu_get_profile(const fs_context_t* ctx, out_profile->hw_active = st->hw_active; out_profile->emitter_active = st->emitter_active; out_profile->freetile_active = st->freetile_active; - if (st->ob_ctx && st->api.get_profile) { - fs_ob_profile_stats ps; + if (st->ft_ctx && st->api.get_profile) { + fs_ft_profile_stats ps; std::memset(&ps, 0, sizeof(ps)); - if (st->api.get_profile(st->ob_ctx, &ps) == 0) { + if (st->api.get_profile(st->ft_ctx, &ps) == 0) { out_profile->calls = ps.calls; out_profile->tiles_dispatched = ps.tiles_dispatched; out_profile->hw_us = ps.hw_us; diff --git a/src/fs_backend_vulkan_dmabuf.cpp b/src/fs_backend_vulkan_dmabuf.cpp index 7b2c513..6a88e39 100644 --- a/src/fs_backend_vulkan_dmabuf.cpp +++ b/src/fs_backend_vulkan_dmabuf.cpp @@ -874,7 +874,7 @@ static fs_status_t fs_vulkan_dmabuf_import(void* user_data, fs_dmabuf_t* buf) { /* * CRITICAL: vkAllocateMemory with VkImportMemoryFdInfoKHR takes ownership * of `fd` ONLY on VK_SUCCESS. Dup with CLOEXEC so Vulkan never closes - * freesched's shared buf->dmabuf_fd (needed by EdgeTPU ob_dmabuf_map). + * freesched's shared buf->dmabuf_fd (needed by EdgeTPU freetpu_dmabuf_map). */ const int dup_fd = fs_vk_dup_fd_cloexec(buf->dmabuf_fd); if (dup_fd >= 0) { diff --git a/src/pc_dmabuf_graph.cpp b/src/pc_dmabuf_graph.cpp index 4aa6664..94a0359 100644 --- a/src/pc_dmabuf_graph.cpp +++ b/src/pc_dmabuf_graph.cpp @@ -344,17 +344,17 @@ static fs_status_t pc_fill_graph_stats(const pc_graph_t* graph, out_stats->dma_sync_calls = tel.dma_sync_calls; out_stats->max_abs_err = parity.max_abs_err; out_stats->nmse = parity.nmse; - out_stats->ob_hw_active = ob_prof.hw_active; - out_stats->ob_emitter_active = ob_prof.emitter_active; - out_stats->ob_freetile_active = ob_prof.freetile_active; - out_stats->ob_calls = ob_prof.calls; - out_stats->ob_tiles_dispatched = ob_prof.tiles_dispatched; - out_stats->ob_hw_us = ob_prof.hw_us; - out_stats->ob_w_hits = ob_prof.weight_cache_hits; - out_stats->ob_w_misses = ob_prof.weight_cache_misses; - out_stats->ob_w_evictions = ob_prof.weight_cache_evictions; - out_stats->ob_weight_pack_us = ob_prof.weight_pack_us; - out_stats->ob_weight_cache_bytes = ob_prof.weight_cache_bytes; + out_stats->freetpu_hw_active = ob_prof.hw_active; + out_stats->freetpu_emitter_active = ob_prof.emitter_active; + out_stats->freetpu_freetile_active = ob_prof.freetile_active; + out_stats->freetpu_calls = ob_prof.calls; + out_stats->freetpu_tiles_dispatched = ob_prof.tiles_dispatched; + out_stats->freetpu_hw_us = ob_prof.hw_us; + out_stats->freetpu_w_hits = ob_prof.weight_cache_hits; + out_stats->freetpu_w_misses = ob_prof.weight_cache_misses; + out_stats->freetpu_w_evictions = ob_prof.weight_cache_evictions; + out_stats->freetpu_weight_pack_us = ob_prof.weight_pack_us; + out_stats->freetpu_weight_cache_bytes = ob_prof.weight_cache_bytes; return FS_OK; } diff --git a/tests/test_c11_abi.c b/tests/test_c11_abi.c index 35c119c..5718da4 100644 --- a/tests/test_c11_abi.c +++ b/tests/test_c11_abi.c @@ -21,8 +21,8 @@ _Static_assert(sizeof(fs_block_q8_0_t) == 34u, "fs_block_q8_0_t must be 34 bytes"); _Static_assert(sizeof(fs_block_q4_0_t) == 18u, "fs_block_q4_0_t must be 18 bytes"); -_Static_assert(FS_ACT_SILU == 4, "FS_ACT_SILU must match libfreetpu OB_ACT_SILU"); -_Static_assert(FS_ACT_GELU == 5, "FS_ACT_GELU must match libfreetpu OB_ACT_GELU"); +_Static_assert(FS_ACT_SILU == 4, "FS_ACT_SILU must match libfreetpu FREETPU_ACT_SILU"); +_Static_assert(FS_ACT_GELU == 5, "FS_ACT_GELU must match libfreetpu FREETPU_ACT_GELU"); #define CHECK(cond) \ do { \ diff --git a/tests/test_pocketcoder.cpp b/tests/test_pocketcoder.cpp index c29eed8..905d29a 100644 --- a/tests/test_pocketcoder.cpp +++ b/tests/test_pocketcoder.cpp @@ -532,15 +532,15 @@ bool test_dspark_dedup_and_dmabuf_graph() { } CHECK(gstats8.fallback_copies == 0u); CHECK(gstats8.nmse < 1e-10); - CHECK(gstats8.ob_w_misses == gstats0.ob_w_misses); - CHECK(gstats8.ob_w_evictions == gstats0.ob_w_evictions); - CHECK(gstats8.ob_weight_pack_us == gstats0.ob_weight_pack_us); - if (gstats8.ob_emitter_active == 1) { - CHECK(gstats8.ob_freetile_active == 1); - CHECK(gstats8.ob_calls >= 9u); - CHECK(gstats8.ob_tiles_dispatched >= 9u); - CHECK(gstats8.ob_w_hits > gstats0.ob_w_hits); - CHECK(gstats8.ob_weight_cache_bytes > 0u); + CHECK(gstats8.freetpu_w_misses == gstats0.freetpu_w_misses); + CHECK(gstats8.freetpu_w_evictions == gstats0.freetpu_w_evictions); + CHECK(gstats8.freetpu_weight_pack_us == gstats0.freetpu_weight_pack_us); + if (gstats8.freetpu_emitter_active == 1) { + CHECK(gstats8.freetpu_freetile_active == 1); + CHECK(gstats8.freetpu_calls >= 9u); + CHECK(gstats8.freetpu_tiles_dispatched >= 9u); + CHECK(gstats8.freetpu_w_hits > gstats0.freetpu_w_hits); + CHECK(gstats8.freetpu_weight_cache_bytes > 0u); } /* Verify weight re-binding updates generation without stale cache hits */ diff --git a/tools/pocketcoder_run.cpp b/tools/pocketcoder_run.cpp index d1a187d..a677488 100644 --- a/tools/pocketcoder_run.cpp +++ b/tools/pocketcoder_run.cpp @@ -329,19 +329,19 @@ int run_bench(const pc_run_options& opts) { (void)pc_graph_step_bound(graph, x_step.data(), y_graph.data(), &gstats_end); pc_graph_destroy(graph); - const uint64_t delta_misses = (gstats_end.ob_w_misses >= gstats_first.ob_w_misses) - ? (gstats_end.ob_w_misses - gstats_first.ob_w_misses) + const uint64_t delta_misses = (gstats_end.freetpu_w_misses >= gstats_first.freetpu_w_misses) + ? (gstats_end.freetpu_w_misses - gstats_first.freetpu_w_misses) : 0u; - const uint64_t delta_evict = (gstats_end.ob_w_evictions >= gstats_first.ob_w_evictions) - ? (gstats_end.ob_w_evictions - gstats_first.ob_w_evictions) + const uint64_t delta_evict = (gstats_end.freetpu_w_evictions >= gstats_first.freetpu_w_evictions) + ? (gstats_end.freetpu_w_evictions - gstats_first.freetpu_w_evictions) : 0u; - const uint64_t delta_pack_us = (gstats_end.ob_weight_pack_us >= gstats_first.ob_weight_pack_us) - ? (gstats_end.ob_weight_pack_us - gstats_first.ob_weight_pack_us) + const uint64_t delta_pack_us = (gstats_end.freetpu_weight_pack_us >= gstats_first.freetpu_weight_pack_us) + ? (gstats_end.freetpu_weight_pack_us - gstats_first.freetpu_weight_pack_us) : 0u; std::printf("PocketCoder-13B-A3B Micro-Benchmark [%s, %ux%u, backend=%s, iters=%u, mode=%s]\n", src_label, n_dim, k_dim, pc_soa_kernel_backend_name(), iters, - opts.mock_mode ? "mock" : (gstats_end.ob_hw_active ? "live-hw" : "live-fallback")); + opts.mock_mode ? "mock" : (gstats_end.freetpu_hw_active ? "live-hw" : "live-fallback")); std::printf("Parity vs FP32 ref: SoA-F32 max_abs=%.3e nmse=%.3e | SoA-SDOT max_abs=%.3e nmse=%.3e\n", p_f32.max_abs_err, p_f32.nmse, p_sdot.max_abs_err, p_sdot.nmse); std::printf("DSpark (MAX_DUP=4): %u -> %u unique experts (%.2fx dedup, %.2f MB saved/layer)\n", @@ -349,24 +349,24 @@ int run_bench(const pc_run_options& opts) { static_cast(dplan.bytes_saved) / (1024.0 * 1024.0)); std::printf( "DMA-BUF graph: zero_copy=%d min_align=%zu handoffs=%llu fallback_copies=%llu " - "hw_active=%d emitter=%d freetile=%d ob_calls=%llu ob_tiles=%llu ob_hw_us=%llu " + "hw_active=%d emitter=%d freetile=%d freetpu_calls=%llu freetpu_tiles=%llu freetpu_hw_us=%llu " "w_hits=%llu w_misses=%llu(delta=%llu) w_evict=%llu(delta=%llu) pack_us=%llu(delta=%llu) " "w_bytes=%llu nmse=%.3e\n\n", gstats_end.reconciled_zero_copy, gstats_end.reconciled_min_align, static_cast(gstats_end.zero_copy_handoffs), static_cast(gstats_end.fallback_copies), - gstats_end.ob_hw_active, gstats_end.ob_emitter_active, gstats_end.ob_freetile_active, - static_cast(gstats_end.ob_calls), - static_cast(gstats_end.ob_tiles_dispatched), - static_cast(gstats_end.ob_hw_us), - static_cast(gstats_end.ob_w_hits), - static_cast(gstats_end.ob_w_misses), + gstats_end.freetpu_hw_active, gstats_end.freetpu_emitter_active, gstats_end.freetpu_freetile_active, + static_cast(gstats_end.freetpu_calls), + static_cast(gstats_end.freetpu_tiles_dispatched), + static_cast(gstats_end.freetpu_hw_us), + static_cast(gstats_end.freetpu_w_hits), + static_cast(gstats_end.freetpu_w_misses), static_cast(delta_misses), - static_cast(gstats_end.ob_w_evictions), + static_cast(gstats_end.freetpu_w_evictions), static_cast(delta_evict), - static_cast(gstats_end.ob_weight_pack_us), + static_cast(gstats_end.freetpu_weight_pack_us), static_cast(delta_pack_us), - static_cast(gstats_end.ob_weight_cache_bytes), + static_cast(gstats_end.freetpu_weight_cache_bytes), gstats_end.nmse); std::printf("| Metric | Baseline (Scalar SoA) | Now (NEON SoA F32) | Now (NEON SoA sdot) | How measured |\n"); @@ -378,16 +378,16 @@ int run_bench(const pc_run_options& opts) { std::printf("| Effective Weight BW | %.2f GB/s | %.2f GB/s | %.2f GB/s | (scales+qs+x)/median_us |\n", bw_ctrl, bw_f32, bw_sdot); std::printf("| Pre-baked Bound Step %ux%u | - | %.2f us median | hw=%d emitter=%d | pc_graph_step_bound (%u iters) |\n", - n_dim, k_dim, graph_bound_us, gstats_end.ob_hw_active, gstats_end.ob_emitter_active, + n_dim, k_dim, graph_bound_us, gstats_end.freetpu_hw_active, gstats_end.freetpu_emitter_active, std::min(iters, 20u)); pc_soa_free(&mat); if (gf) { pc_gguf_close(gf); } - const double graph_nmse_limit = gstats_end.ob_hw_active ? 5e-3 : 1e-10; + const double graph_nmse_limit = gstats_end.freetpu_hw_active ? 5e-3 : 1e-10; const bool dmabuf_ok = (gstats_end.fallback_copies == 0u) || - (!opts.mock_mode && !opts.strict_hw && gstats_end.ob_hw_active == 0); + (!opts.mock_mode && !opts.strict_hw && gstats_end.freetpu_hw_active == 0); const bool zero_repack_ok = (delta_misses == 0u && delta_evict == 0u && delta_pack_us == 0u); const bool graph_parity_ok = (gstats_first.nmse < graph_nmse_limit && gstats_end.nmse < graph_nmse_limit); return (p_f32.nmse < 1e-10 && p_sdot.nmse < 1e-2 && dmabuf_ok && diff --git a/tools/volcanic_tpu_dmabuf_bench.cpp b/tools/volcanic_tpu_dmabuf_bench.cpp index 8eefe84..7657cc2 100644 --- a/tools/volcanic_tpu_dmabuf_bench.cpp +++ b/tools/volcanic_tpu_dmabuf_bench.cpp @@ -9,7 +9,7 @@ * Volcanic GPU, EdgeTPU, and CPU (linear layout, 128-byte aligned). * 2. Imports the EXACT SAME DMA-BUF file descriptors into BOTH: * - PowerVR Volcanic Vulkan via VK_EXT_external_memory_dma_buf (dup CLOEXEC fd) - * - Tensor G5 EdgeTPU via libfreetpu ob_dmabuf_map (36-bit IOVA, pinned once) + * - Tensor G5 EdgeTPU via libfreetpu freetpu_dmabuf_map (36-bit IOVA, pinned once) * 3. Executes a 3-stage round-trip with 0 host copies and 0 hot-loop IOVA maps: * Stage 1 (Volcanic GPU SPIR-V) : Prefill GEMM H[M, K] = X @ W_gpu^T -> DecodeIn[K] * Stage 2 (EdgeTPU W8A16 IOVA) : Decode MUL_MAT Y_raw[N] = FP16(DecodeIn) @ W_tpu^T @@ -1264,22 +1264,22 @@ int main(int argc, char** argv) { } /* 3. Initialize libfreetpu EdgeTPU and map the EXACT SAME b_mid & b_tpu_out FDs ONCE! */ - ob_config_t ob_cfg = ob_config_default(); - ob_cfg.mock_mode = 0; - ob_cfg.dma_heap_path = heap_path; - ob_cfg.perf_mode = 5u; - - ob_context_t* ob_ctx = nullptr; - if (ob_context_create(&ob_cfg, &ob_ctx) != OB_OK || !ob_ctx) { - std::fprintf(stderr, "ob_context_create failed\n"); + freetpu_config_t ft_cfg = freetpu_config_default(); + ft_cfg.mock_mode = 0; + ft_cfg.dma_heap_path = heap_path; + ft_cfg.perf_mode = 5u; + + freetpu_context_t* ft_ctx = nullptr; + if (freetpu_context_create(&ft_cfg, &ft_ctx) != FREETPU_OK || !ft_ctx) { + std::fprintf(stderr, "freetpu_context_create failed\n"); vk.shutdown(); fs_context_destroy(fctx); return 1; } - if (ob_gemm_register_builtin_emitter(ob_ctx, dive_path) != OB_OK) { - std::fprintf(stderr, "ob_gemm_register_builtin_emitter failed: %s\n", - ob_context_last_error(ob_ctx)); - ob_context_destroy(ob_ctx); + if (freetpu_gemm_register_builtin_emitter(ft_ctx, dive_path) != FREETPU_OK) { + std::fprintf(stderr, "freetpu_gemm_register_builtin_emitter failed: %s\n", + freetpu_context_last_error(ft_ctx)); + freetpu_context_destroy(ft_ctx); vk.shutdown(); fs_context_destroy(fctx); return 1; @@ -1297,10 +1297,10 @@ int main(int argc, char** argv) { (out_fd_len > 0 && static_cast(out_fd_len) >= b_tpu_out->size) ? static_cast(out_fd_len) : static_cast(b_tpu_out->size); - if (ob_dmabuf_map(ob_ctx, b_mid->dmabuf_fd, 0u, mid_map_sz, OB_BUF_BIDIRECTIONAL, &mid_iova) != OB_OK || - ob_dmabuf_map(ob_ctx, b_tpu_out->dmabuf_fd, 0u, out_map_sz, OB_BUF_BIDIRECTIONAL, &tpu_out_iova) != OB_OK) { - std::fprintf(stderr, "ob_dmabuf_map failed: %s\n", ob_context_last_error(ob_ctx)); - ob_context_destroy(ob_ctx); + if (freetpu_dmabuf_map(ft_ctx, b_mid->dmabuf_fd, 0u, mid_map_sz, FREETPU_BUF_BIDIRECTIONAL, &mid_iova) != FREETPU_OK || + freetpu_dmabuf_map(ft_ctx, b_tpu_out->dmabuf_fd, 0u, out_map_sz, FREETPU_BUF_BIDIRECTIONAL, &tpu_out_iova) != FREETPU_OK) { + std::fprintf(stderr, "freetpu_dmabuf_map failed: %s\n", freetpu_context_last_error(ft_ctx)); + freetpu_context_destroy(ft_ctx); vk.shutdown(); fs_context_destroy(fctx); return 1; @@ -1347,12 +1347,12 @@ int main(int argc, char** argv) { const uint64_t t2 = now_ns(); /* Stage 2: EdgeTPU W8A16 MUL_MAT directly on mid_iova -> tpu_out_iova (0 host copies!) */ - ob_invoke_stats_t tpu_st{}; - ob_status_t ost = ob_gemm_matmul_f32_w8a16_dmabuf( - ob_ctx, mid_iova, sz_mid, h_w_tpu.data(), k_dim, n_dim, tpu_out_iova, sz_tpu_out, &tpu_st); - if (ost != OB_OK) { - std::fprintf(stderr, "ob_gemm_matmul_f32_w8a16_dmabuf failed: %s (%s)\n", - ob_status_string(ost), ob_context_last_error(ob_ctx)); + freetpu_invoke_stats_t tpu_st{}; + freetpu_status_t ost = freetpu_gemm_matmul_f32_w8a16_dmabuf( + ft_ctx, mid_iova, sz_mid, h_w_tpu.data(), k_dim, n_dim, tpu_out_iova, sz_tpu_out, &tpu_st); + if (ost != FREETPU_OK) { + std::fprintf(stderr, "freetpu_gemm_matmul_f32_w8a16_dmabuf failed: %s (%s)\n", + freetpu_status_string(ost), freetpu_context_last_error(ft_ctx)); goto fail_live; } const uint64_t t3 = now_ns(); @@ -1403,7 +1403,7 @@ int main(int argc, char** argv) { const ParityMetrics par_tpu = compute_parity(got_tpu.data(), ref_tpu.data(), n_dim); const ParityMetrics par_final = compute_parity(got_final.data(), ref_final.data(), n_dim); - /* 5. Host-Staged memcpy Baseline (exact same math via CPU staging copies + ob_gemm_matmul_f32_w8a16_tile) */ + /* 5. Host-Staged memcpy Baseline (exact same math via CPU staging copies + freetpu_gemm_matmul_f32_w8a16_tile) */ std::vector stage_mid(k_dim), stage_tpu_out(n_dim); std::vector base_handoff_us, base_tpu_wall_us, base_total_us; base_handoff_us.reserve(iters); @@ -1425,11 +1425,11 @@ int main(int argc, char** argv) { const uint64_t t2 = now_ns(); /* Standard host-pointer EdgeTPU call (internally copies host -> TPU DMA-BUF -> host) */ - ob_invoke_stats_t bst{}; - if (ob_gemm_matmul_f32_w8a16_tile(ob_ctx, stage_mid.data(), h_w_tpu.data(), h_ones.data(), - k_dim, n_dim, stage_tpu_out.data(), &bst) != OB_OK) { - std::fprintf(stderr, "Baseline ob_gemm_matmul_f32_w8a16_tile failed: %s\n", - ob_context_last_error(ob_ctx)); + freetpu_invoke_stats_t bst{}; + if (freetpu_gemm_matmul_f32_w8a16_tile(ft_ctx, stage_mid.data(), h_w_tpu.data(), h_ones.data(), + k_dim, n_dim, stage_tpu_out.data(), &bst) != FREETPU_OK) { + std::fprintf(stderr, "Baseline freetpu_gemm_matmul_f32_w8a16_tile failed: %s\n", + freetpu_context_last_error(ft_ctx)); goto fail_live; } const uint64_t t3 = now_ns(); @@ -1538,9 +1538,9 @@ int main(int argc, char** argv) { (has_pwr_before || has_pwr_after) ? 1 : 0, avg_pwr_mw, zc_mj_step); } - (void)ob_dmabuf_unmap(ob_ctx, mid_iova); - (void)ob_dmabuf_unmap(ob_ctx, tpu_out_iova); - ob_context_destroy(ob_ctx); + (void)freetpu_dmabuf_unmap(ft_ctx, mid_iova); + (void)freetpu_dmabuf_unmap(ft_ctx, tpu_out_iova); + freetpu_context_destroy(ft_ctx); vk.destroy_buffer(&vk_x); vk.destroy_buffer(&vk_w_gpu); vk.destroy_buffer(&vk_prefill); @@ -1554,9 +1554,9 @@ int main(int argc, char** argv) { } fail_live: - (void)ob_dmabuf_unmap(ob_ctx, mid_iova); - (void)ob_dmabuf_unmap(ob_ctx, tpu_out_iova); - ob_context_destroy(ob_ctx); + (void)freetpu_dmabuf_unmap(ft_ctx, mid_iova); + (void)freetpu_dmabuf_unmap(ft_ctx, tpu_out_iova); + freetpu_context_destroy(ft_ctx); vk.destroy_buffer(&vk_x); vk.destroy_buffer(&vk_w_gpu); vk.destroy_buffer(&vk_prefill); diff --git a/tools/volcanic_tpu_token_loop_bench.cpp b/tools/volcanic_tpu_token_loop_bench.cpp index 4abbfb6..f0072ba 100644 --- a/tools/volcanic_tpu_token_loop_bench.cpp +++ b/tools/volcanic_tpu_token_loop_bench.cpp @@ -2122,29 +2122,29 @@ class LiveExecutor : public Executor { } bool init_ob(const char* heap, const char* dive, std::string* err) { - ob_config_t cfg = ob_config_default(); + freetpu_config_t cfg = freetpu_config_default(); cfg.mock_mode = 0; cfg.dma_heap_path = heap; cfg.perf_mode = 5u; - if (ob_context_create(&cfg, &ob_) != OB_OK || !ob_) { - *err = "ob_context_create failed (no Tensor G5 EdgeTPU node)"; + if (freetpu_context_create(&cfg, &ob_) != FREETPU_OK || !ob_) { + *err = "freetpu_context_create failed (no Tensor G5 EdgeTPU node)"; ob_ = nullptr; return false; } - if (ob_gemm_register_builtin_emitter(ob_, dive) != OB_OK) { - *err = sanitize_ob_error(std::string("ob_gemm_register_builtin_emitter failed: ") + ob_context_last_error(ob_)); + if (freetpu_gemm_register_builtin_emitter(ob_, dive) != FREETPU_OK) { + *err = sanitize_ob_error(std::string("freetpu_gemm_register_builtin_emitter failed: ") + freetpu_context_last_error(ob_)); return false; } for (uint32_t pp = 0u; pp < 2u; ++pp) { if (!ob_map(p_->sbuf(p_->mid_slot(pp)), &mid_iova_[pp]) || !ob_map(p_->sbuf(p_->out_slot(pp)), &out_iova_[pp])) { - *err = sanitize_ob_error(std::string("ob_dmabuf_map failed: ") + ob_context_last_error(ob_)); + *err = sanitize_ob_error(std::string("freetpu_dmabuf_map failed: ") + freetpu_context_last_error(ob_)); return false; } } /* * Pre-warm every layer's libfreetpu packed weight cache strictly BEFORE - * fs_iova_pool_enter_hot_loop() (even when --warmup 0): ob_gemm_matmul_f32_w8a16_dmabuf - * lazily packs and IOVA-maps per-layer weight buffers inside ob_context_t on + * fs_iova_pool_enter_hot_loop() (even when --warmup 0): freetpu_gemm_matmul_f32_w8a16_dmabuf + * lazily packs and IOVA-maps per-layer weight buffers inside freetpu_context_t on * first use, so one full pass across all L layers keeps those allocations and * mappings out of the hot loop. */ @@ -2158,12 +2158,12 @@ class LiveExecutor : public Executor { ob_prewarmed_layers_ = 0u; for (uint32_t l = 0u; l < m_.layers; ++l) { const int8_t* wf = m_.w_ffn.data() + static_cast(l) * d * d; - ob_invoke_stats_t st{}; - const ob_status_t rc = ob_gemm_matmul_f32_w8a16_dmabuf(ob_, mid_iova_[0u], d * sizeof(float), wf, d, d, - out_iova_[0u], d * sizeof(float), &st); - if (rc != OB_OK) { + freetpu_invoke_stats_t st{}; + const freetpu_status_t rc = freetpu_gemm_matmul_f32_w8a16_dmabuf(ob_, mid_iova_[0u], d * sizeof(float), wf, d, d, + out_iova_[0u], d * sizeof(float), &st); + if (rc != FREETPU_OK) { *err = sanitize_ob_error(std::string("libfreetpu weight pre-warm failed at layer ") + std::to_string(l) + - ": " + ob_status_string(rc) + " (" + ob_context_last_error(ob_) + ")"); + ": " + freetpu_status_string(rc) + " (" + freetpu_context_last_error(ob_) + ")"); return false; } ob_prewarmed_layers_ += 1u; @@ -2174,11 +2174,11 @@ class LiveExecutor : public Executor { void shutdown_ob() { if (ob_) { for (uint32_t pp = 0u; pp < 2u; ++pp) { - if (mid_iova_[pp]) (void)ob_dmabuf_unmap(ob_, mid_iova_[pp]); - if (out_iova_[pp]) (void)ob_dmabuf_unmap(ob_, out_iova_[pp]); + if (mid_iova_[pp]) (void)freetpu_dmabuf_unmap(ob_, mid_iova_[pp]); + if (out_iova_[pp]) (void)freetpu_dmabuf_unmap(ob_, out_iova_[pp]); mid_iova_[pp] = out_iova_[pp] = 0u; } - ob_context_destroy(ob_); + freetpu_context_destroy(ob_); ob_ = nullptr; } ob_upfront_maps_ = 0u; @@ -2189,7 +2189,7 @@ class LiveExecutor : public Executor { const off_t len = lseek(b->dmabuf_fd, 0, SEEK_END); const uint64_t sz = (len > 0 && static_cast(len) >= b->size) ? static_cast(len) : static_cast(b->size); - if (ob_dmabuf_map(ob_, b->dmabuf_fd, 0u, sz, OB_BUF_BIDIRECTIONAL, out_iova) != OB_OK) return false; + if (freetpu_dmabuf_map(ob_, b->dmabuf_fd, 0u, sz, FREETPU_BUF_BIDIRECTIONAL, out_iova) != FREETPU_OK) return false; ob_upfront_maps_ += 1u; return true; } @@ -2863,11 +2863,11 @@ class LiveExecutor : public Executor { } #ifdef FREESCHED_HAVE_LIBFREETPU if (ob_ != nullptr) { - const ob_status_t orc = ob_graph_wait_all(ob_, 0u); - if (orc != OB_OK) { - const char* st = ob_status_string(orc); - const char* last = ob_context_last_error(ob_); - *why += std::string(why->empty() ? "" : "; ") + "ob_graph_wait_all returned " + (st ? st : "error") + + const freetpu_status_t orc = freetpu_graph_wait_all(ob_, 0u); + if (orc != FREETPU_OK) { + const char* st = freetpu_status_string(orc); + const char* last = freetpu_context_last_error(ob_); + *why += std::string(why->empty() ? "" : "; ") + "freetpu_graph_wait_all returned " + (st ? st : "error") + (last && last[0] ? std::string(" (") + last + ")" : std::string()); ok = false; } @@ -2890,17 +2890,17 @@ class LiveExecutor : public Executor { const uint32_t d = m_.d; #ifdef FREESCHED_HAVE_LIBFREETPU if (tpu_ == TPU_OB) { - ob_invoke_stats_t st{}; - ob_status_t rc; + freetpu_invoke_stats_t st{}; + freetpu_status_t rc; if (staged) { - rc = ob_gemm_matmul_f32_w8a16_tile(ob_, stage_in_.data(), wf, ones_.data(), d, d, stage_out_.data(), &st); + rc = freetpu_gemm_matmul_f32_w8a16_tile(ob_, stage_in_.data(), wf, ones_.data(), d, d, stage_out_.data(), &st); } else { - rc = ob_gemm_matmul_f32_w8a16_dmabuf(ob_, mid_iova_[pp], d * sizeof(float), wf, d, d, out_iova_[pp], - d * sizeof(float), &st); + rc = freetpu_gemm_matmul_f32_w8a16_dmabuf(ob_, mid_iova_[pp], d * sizeof(float), wf, d, d, out_iova_[pp], + d * sizeof(float), &st); } - if (rc != OB_OK) { - *err = sanitize_ob_error(std::string("EdgeTPU W8A16 failed: ") + ob_status_string(rc) + " (" + - ob_context_last_error(ob_) + ")"); + if (rc != FREETPU_OK) { + *err = sanitize_ob_error(std::string("EdgeTPU W8A16 failed: ") + freetpu_status_string(rc) + " (" + + freetpu_context_last_error(ob_) + ")"); return false; } c->tpu_hw_us += static_cast(st.hw_execution_us); @@ -2966,9 +2966,9 @@ class LiveExecutor : public Executor { bool nn_zero_copy_bound_ = true; /* false: dmabuf_fd blocked from NNAPI (EdgeTPU) */ uint64_t nn_outstanding_ = 0u; /* started NNAPI hops not yet released */ #ifdef FREESCHED_HAVE_LIBFREETPU - ob_context_t* ob_ = nullptr; - uint64_t mid_iova_[2] = {0u, 0u}; - uint64_t out_iova_[2] = {0u, 0u}; + freetpu_context_t* ob_ = nullptr; + uint64_t mid_iova_[2] = {0u, 0u}; + uint64_t out_iova_[2] = {0u, 0u}; #endif }; #endif /* FS_TL_HAVE_VULKAN */