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
8 changes: 8 additions & 0 deletions src/fs_backend_openbuenos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,14 @@ static fs_status_t fs_openbuenos_execute(void* user_data,
return FS_ERR_UNSUPPORTED;
}

/* Reject undersized tensors before the hardware path indexes host_ptr. */
{
fs_status_t bounds = fs_ref_check_op_bounds(op);
if (bounds != FS_OK) {
return bounds;
}
}

if (st->ob_ctx && op->act_dtype == FS_DTYPE_F32 &&
(op->weight_dtype == FS_DTYPE_Q8_0 || op->weight_dtype == FS_DTYPE_Q4_0)) {
std::lock_guard<std::mutex> lk(st->mu);
Expand Down
227 changes: 227 additions & 0 deletions src/fs_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,232 @@ static fs_status_t fs_ref_execute_elementwise(const fs_op_desc_t* op) {
return FS_OK;
}

static int fs_mul_size(size_t a, size_t b, size_t* out) {
if (a != 0u && b > (SIZE_MAX / a)) {
return 0;
}
*out = a * b;
return 1;
}

static size_t fs_act_elem_size(fs_dtype_t dtype) {
return dtype == FS_DTYPE_F16 ? 2u : 4u;
}

static fs_status_t fs_need_bytes(const fs_dmabuf_t* buf, size_t bytes) {
if (!buf || !buf->host_ptr) {
return FS_OK;
}
if (bytes > buf->size) {
return FS_ERR_OVERFLOW;
}
return FS_OK;
}

static fs_status_t fs_need_act_matrix(const fs_dmabuf_t* buf,
uint32_t rows,
uint32_t cols,
fs_dtype_t dtype) {
size_t n = 0u;
size_t bytes = 0u;
if (!fs_mul_size(rows, cols, &n) || !fs_mul_size(n, fs_act_elem_size(dtype), &bytes)) {
return FS_ERR_OVERFLOW;
}
return fs_need_bytes(buf, bytes);
}

/* unread=1 when this dtype is not indexed (or quant K is misaligned). */
static int fs_weight_bytes(uint32_t rows, uint32_t k, fs_dtype_t dtype, size_t* out, int* unread) {
*unread = 0;
size_t row_bytes = 0u;
if (dtype == FS_DTYPE_Q8_0 || dtype == FS_DTYPE_Q4_0) {
if ((k % FS_QK8_0) != 0u) {
*unread = 1;
return 1;
}
const size_t blk = (dtype == FS_DTYPE_Q8_0) ? sizeof(fs_block_q8_0_t) : sizeof(fs_block_q4_0_t);
if (!fs_mul_size(static_cast<size_t>(k / FS_QK8_0), blk, &row_bytes)) {
return 0;
}
} else if (dtype == FS_DTYPE_F16) {
if (!fs_mul_size(k, 2u, &row_bytes)) {
return 0;
}
} else if (dtype == FS_DTYPE_I8) {
row_bytes = k;
} else if (dtype == FS_DTYPE_F32) {
if (!fs_mul_size(k, 4u, &row_bytes)) {
return 0;
}
} else {
*unread = 1;
return 1;
}
return fs_mul_size(row_bytes, rows, out);
}

static fs_status_t fs_need_weight_matrix(const fs_dmabuf_t* buf,
uint32_t rows,
uint32_t k,
fs_dtype_t dtype) {
int unread = 0;
size_t bytes = 0u;
if (!fs_weight_bytes(rows, k, dtype, &bytes, &unread)) {
return FS_ERR_OVERFLOW;
}
if (unread) {
return FS_OK;
}
return fs_need_bytes(buf, bytes);
}

static fs_status_t fs_check_gemm_bounds(const fs_op_desc_t* op) {
if (!op->input_a || !op->input_b || !op->output || !op->input_a->host_ptr ||
!op->input_b->host_ptr || !op->output->host_ptr) {
return FS_OK;
}
if ((op->weight_dtype == FS_DTYPE_Q8_0 || op->weight_dtype == FS_DTYPE_Q4_0) &&
(op->k_dim % FS_QK8_0) != 0u) {
return FS_OK;
}
if (op->m_dim == 0u || op->n_dim == 0u) {
return FS_OK;
}
fs_status_t st = fs_need_act_matrix(op->output, op->m_dim, op->n_dim, op->act_dtype);
if (st != FS_OK) {
return st;
}
if (op->k_dim == 0u) {
return FS_OK;
}
st = fs_need_act_matrix(op->input_a, op->m_dim, op->k_dim, op->act_dtype);
if (st != FS_OK) {
return st;
}
return fs_need_weight_matrix(op->input_b, op->n_dim, op->k_dim, op->weight_dtype);
}

static fs_status_t fs_check_row_bounds(const fs_op_desc_t* op, int optional_weight) {
if (!op->input_a || !op->output || !op->input_a->host_ptr || !op->output->host_ptr ||
op->n_dim == 0u) {
return FS_OK;
}
const uint32_t rows = op->m_dim > 0u ? op->m_dim : 1u;
fs_status_t st = fs_need_act_matrix(op->input_a, rows, op->n_dim, op->act_dtype);
if (st != FS_OK) {
return st;
}
st = fs_need_act_matrix(op->output, rows, op->n_dim, op->act_dtype);
if (st != FS_OK) {
return st;
}
if (optional_weight && op->input_b && op->input_b->host_ptr) {
const size_t elem = op->weight_dtype == FS_DTYPE_F16 ? 2u : 4u;
size_t bytes = 0u;
if (!fs_mul_size(op->n_dim, elem, &bytes)) {
return FS_ERR_OVERFLOW;
}
st = fs_need_bytes(op->input_b, bytes);
}
return st;
}

static fs_status_t fs_check_elementwise_bounds(const fs_op_desc_t* op) {
if (!op->input_a || !op->output || !op->input_a->host_ptr || !op->output->host_ptr) {
return FS_OK;
}
const uint32_t rows = op->m_dim > 0u ? op->m_dim : 1u;
const uint32_t cols = op->n_dim > 0u ? op->n_dim : 1u;
fs_status_t st = fs_need_act_matrix(op->input_a, rows, cols, op->act_dtype);
if (st != FS_OK) {
return st;
}
st = fs_need_act_matrix(op->output, rows, cols, op->act_dtype);
if (st != FS_OK) {
return st;
}
if (op->input_b && op->input_b->host_ptr) {
st = fs_need_act_matrix(op->input_b, rows, cols, op->act_dtype);
}
return st;
}

static fs_status_t fs_check_swiglu_bounds(const fs_op_desc_t* op) {
if (!op->input_a || !op->input_b || !op->input_c || !op->input_d || !op->output ||
!op->input_a->host_ptr || !op->input_b->host_ptr || !op->input_c->host_ptr ||
!op->input_d->host_ptr || !op->output->host_ptr) {
return FS_OK;
}
if (op->h_dim == 0u) {
return FS_OK;
}
if ((op->weight_dtype == FS_DTYPE_Q8_0 || op->weight_dtype == FS_DTYPE_Q4_0) &&
((op->k_dim % FS_QK8_0) != 0u || (op->h_dim % FS_QK8_0) != 0u)) {
return FS_OK;
}
if (op->m_dim == 0u) {
return FS_OK;
}
if (op->k_dim > 0u) {
fs_status_t st = fs_need_act_matrix(op->input_a, op->m_dim, op->k_dim, op->act_dtype);
if (st != FS_OK) {
return st;
}
st = fs_need_weight_matrix(op->input_b, op->h_dim, op->k_dim, op->weight_dtype);
if (st != FS_OK) {
return st;
}
st = fs_need_weight_matrix(op->input_c, op->h_dim, op->k_dim, op->weight_dtype);
if (st != FS_OK) {
return st;
}
}
if (op->n_dim > 0u) {
fs_status_t st = fs_need_weight_matrix(op->input_d, op->n_dim, op->h_dim, op->weight_dtype);
if (st != FS_OK) {
return st;
}
st = fs_need_act_matrix(op->output, op->m_dim, op->n_dim, op->act_dtype);
if (st != FS_OK) {
return st;
}
}
return FS_OK;
}

fs_status_t fs_ref_check_op_bounds(const fs_op_desc_t* op) {
if (!op) {
return FS_OK;
}
switch (op->type) {
case FS_OP_GEMM:
case FS_OP_PREFILL_GEMM:
case FS_OP_DECODE_GEMV:
case FS_OP_VISION_CONV:
return fs_check_gemm_bounds(op);
case FS_OP_SWIGLU:
return fs_check_swiglu_bounds(op);
case FS_OP_ATTN_SOFTMAX:
return fs_check_row_bounds(op, 0);
case FS_OP_RMS_NORM:
return fs_check_row_bounds(op, 1);
case FS_OP_ROPE:
if (op->n_dim == 0u || (op->n_dim % 2u) != 0u) {
return FS_OK;
}
return fs_check_row_bounds(op, 0);
case FS_OP_ELEMENTWISE:
return fs_check_elementwise_bounds(op);
default:
return FS_OK;
}
}

static fs_status_t fs_ref_execute_any(const fs_op_desc_t* op) {
fs_status_t bounds = fs_ref_check_op_bounds(op);
if (bounds != FS_OK) {
return bounds;
}
switch (op->type) {
case FS_OP_GEMM:
case FS_OP_PREFILL_GEMM:
Expand Down Expand Up @@ -629,6 +854,8 @@ static fs_status_t fs_execute_single_step(fs_context_t* ctx,
if (op->output) {
fs_buffer_release_access(ctx, op->output, target_dev);
}
/* Backend-exported sync fds live only in this fence. Close both paths. */
fs_fence_close(&sig_fence);

if (exec_st != FS_OK) {
fs_set_error(ctx, "backend %s execute_op failed (%s)", fs_dev_name(target_dev),
Expand Down
1 change: 1 addition & 0 deletions src/fs_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fs_status_t fs_dmabuf_ensure_mapped(fs_context_t* ctx, fs_dmabuf_t* buf, fs_dev_
fs_status_t fs_install_builtin_backends(fs_context_t* ctx);
void fs_shutdown_backends(fs_context_t* ctx);
fs_status_t fs_ref_execute_any_internal(const fs_op_desc_t* op);
fs_status_t fs_ref_check_op_bounds(const fs_op_desc_t* op);

/* Router & topology internal helpers */
fs_status_t fs_topology_probe_mode(fs_hw_profile_t profile_hint,
Expand Down
42 changes: 41 additions & 1 deletion src/fs_topology.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,37 @@ const char* fs_op_type_name(fs_op_type_t type) {
}
}

/* Uncalibrated GPU peak/handoff for a known profile. A repeat apply must not
* compound the previous call's fp16 peak or no-DMA-BUF handoff penalty. */
static void fs_profile_gpu_baseline(fs_hw_profile_t profile,
float* fp16,
float* handoff,
int* known) {
switch (profile) {
case FS_PROFILE_PIXEL10_LAGUNA:
*fp16 = 2400.0f;
*handoff = 10.0f;
*known = 1;
break;
case FS_PROFILE_PIXEL11_MALIBU:
case FS_PROFILE_HOST_MOCK:
*fp16 = 3600.0f;
*handoff = 8.0f;
*known = 1;
break;
case FS_PROFILE_QCOM_IQ9075:
*fp16 = 1536.0f;
*handoff = 8.0f;
*known = 1;
break;
default:
*fp16 = 0.0f;
*handoff = 0.0f;
*known = 0;
break;
}
}

fs_status_t fs_topology_apply_vk_caps(fs_topology_t* topo, const fs_vk_gpu_caps_t* caps) {
if (!topo || !caps) {
return FS_ERR_INVALID_ARG;
Expand All @@ -583,7 +614,14 @@ fs_status_t fs_topology_apply_vk_caps(fs_topology_t* topo, const fs_vk_gpu_caps_
fs_copy_str(gpu.name, sizeof(gpu.name), caps->device_name);
}

const float base_fp16 = (topo->profile == FS_PROFILE_QCOM_IQ9075) ? 1536.0f : gpu.peak_gflops_fp16;
float base_fp16 = 0.0f;
float base_handoff = 0.0f;
int profile_known = 0;
fs_profile_gpu_baseline(topo->profile, &base_fp16, &base_handoff, &profile_known);
if (!profile_known) {
base_fp16 = gpu.peak_gflops_fp16;
}

if ((mapped & FS_CAP_VK_COOP_MATRIX) != 0u) {
gpu.peak_gflops_fp16 = base_fp16 * 1.5f;
gpu.peak_tops_int8 = 3.072f;
Expand All @@ -600,6 +638,8 @@ fs_status_t fs_topology_apply_vk_caps(fs_topology_t* topo, const fs_vk_gpu_caps_

if (caps->fence_handoff_p50_us > 0.0f) {
gpu.handoff_overhead_us = caps->fence_handoff_p50_us;
} else if (profile_known) {
gpu.handoff_overhead_us = base_handoff;
}

if ((mapped & FS_CAP_DMABUF) == 0u) {
Expand Down
Loading
Loading