Skip to content

Commit 297b9c9

Browse files
authored
hexagon: SmolVLA 1037 → 918 ms on the VENTUNO Q (GELU, patch embed, SigLIP views, time fold, gate|up) (#87)
1 parent be05482 commit 297b9c9

3 files changed

Lines changed: 130 additions & 22 deletions

File tree

‎src/backend.h‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,14 +260,15 @@ inline int64_t gemm_width(int64_t n) {
260260
* fallback wrapper refuses it and every tower layer would round-trip its MLP
261261
* activations through the CPU. The tanh form is exactly
262262
* x*sigmoid(2*sqrt(2/pi)*(x + 0.044715x^3)), since (1+tanh z)/2 = sigmoid(2z),
263-
* and HTP implements each of those ops. Elsewhere this is plain ggml_gelu.
263+
* and HTP implements each of those ops. Written as x*sigmoid(x*(k + k*0.044715x^2)),
264+
* k = 2*sqrt(2/pi), it takes five ops, since SCALE carries the offset. Elsewhere this
265+
* is plain ggml_gelu.
264266
*/
265267
inline ggml_tensor * gelu(ggml_context * ctx, ggml_tensor * x) {
266268
#ifdef GGML_USE_HEXAGON
267-
const float c = 0.7978845608028654f; // sqrt(2/pi)
268-
ggml_tensor * x3 = ggml_mul(ctx, ggml_mul(ctx, x, x), x);
269-
ggml_tensor * z = ggml_add(ctx, x, ggml_scale(ctx, x3, 0.044715f));
270-
return ggml_mul(ctx, x, ggml_sigmoid(ctx, ggml_scale(ctx, z, 2.0f * c)));
269+
const float k = 1.5957691216057308f; // 2*sqrt(2/pi)
270+
ggml_tensor * t = ggml_scale_bias(ctx, ggml_mul(ctx, x, x), k * 0.044715f, k);
271+
return ggml_mul(ctx, x, ggml_sigmoid(ctx, ggml_mul(ctx, x, t)));
271272
#else
272273
return ggml_gelu(ctx, x);
273274
#endif

‎src/models/smolvla.cpp‎

Lines changed: 109 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ struct LayerW {
239239
ggml_tensor * Wdown;
240240
// The fused graph (SmolVLAModelArch::fused_lm): q|k|v (self-attention
241241
// layers) and gate|up as one weight each; Wq..Wup are then views of them.
242+
// Hexagon builds fuse gate|up alone (attn_mlp).
242243
ggml_tensor * Wqkv = nullptr;
243244
ggml_tensor * Wgu = nullptr;
244245
// W8A8 (SmolVLAModelArch::w8a8_*): INT8 copies of those GEMMs, made after
@@ -318,6 +319,15 @@ struct SmolVLAModelArch : public ModelArchBase {
318319
graph_cache<int, MainIO> main_graph;
319320

320321
std::vector<ggml_tensor *> time_bcasts;
322+
323+
// Hexagon: action_time_mlp_in without the per-step feature concat, which
324+
// the NPU refuses and every denoise step would round-trip through the CPU.
325+
// W_at1a is its action half; at1_step[s] is its time half applied to step
326+
// s's time embedding, plus b_at1.
327+
ggml_context * ctx_at = nullptr;
328+
ggml_backend_buffer_t at_buf = nullptr;
329+
ggml_tensor * W_at1a = nullptr;
330+
std::vector<ggml_tensor *> at1_step;
321331
};
322332

323333
namespace {
@@ -330,16 +340,29 @@ namespace {
330340
// 96/100 for explicit attention. evo1 showed the same ~4-5 pp drop, so the
331341
// default stays on the accuracy-preserving path.
332342

333-
// One pre-norm SigLIP encoder block (SmolVLM2 tower), same graph as the other
334-
// in-tree models. Bidirectional attention, F32 score accumulation, tanh GELU.
335-
ggml_tensor * build_siglip_layer(ggml_context * C, const EncCfg & c, const EncBlockW & w,
336-
ggml_tensor * x, int64_t seq) {
343+
// Self-attention of one SigLIP block over its normed input n1: [hidden, seq].
344+
ggml_tensor * siglip_attention(ggml_context * C, const EncCfg & c, const EncBlockW & w, ggml_tensor * n1,
345+
int64_t seq) {
337346
const float scale = 1.0f/std::sqrt((float) c.head_dim);
338-
ggml_tensor * n1 = layer_norm(C, x, w.ln1w, w.ln1b, c.ln_eps);
347+
#ifdef GGML_USE_HEXAGON
348+
if (vla::flash_attn_enabled()) {
349+
// The NPU's flash attention reads Q and the F16 K/V through strides, so
350+
// the heads stay views of the projections: no copy into the head layout,
351+
// and K/V are cast while contiguous.
352+
const auto heads = [&](ggml_tensor * p) {
353+
return ggml_permute(C, ggml_reshape_3d(C, p, c.head_dim, c.heads, seq), 0, 2, 1, 3);
354+
};
355+
ggml_tensor * fa = ggml_flash_attn_ext(C, heads(linear(C, w.Wq, w.bq, n1)),
356+
heads(vla::fa_kv(C, linear(C, w.Wk, w.bk, n1))),
357+
heads(vla::fa_kv(C, linear(C, w.Wv, w.bv, n1))), nullptr, scale,
358+
0.0f, 0.0f);
359+
ggml_prec_set_acc(fa, GGML_PREC_F32);
360+
return ggml_reshape_2d(C, fa, c.hidden, seq);
361+
}
362+
#endif
339363
ggml_tensor * Q = to_heads(C, linear(C, w.Wq, w.bq, n1), c.head_dim, c.heads, seq);
340364
ggml_tensor * K = to_heads(C, linear(C, w.Wk, w.bk, n1), c.head_dim, c.heads, seq);
341365
ggml_tensor * v = linear(C, w.Wv, w.bv, n1);
342-
ggml_tensor * att;
343366
if (vla::flash_attn_enabled()) {
344367
// The tower runs 1024 tokens (512/16 grid) over 12 layers, so the
345368
// explicit path below materialises a 1024x1024 score matrix per head —
@@ -351,11 +374,17 @@ ggml_tensor * build_siglip_layer(ggml_context * C, const EncCfg & c, const EncBl
351374
ggml_tensor * V = to_heads(C, v, c.head_dim, c.heads, seq);
352375
ggml_tensor * fa = ggml_flash_attn_ext(C, Q, vla::fa_kv(C, K), vla::fa_kv(C, V), nullptr, scale, 0.0f, 0.0f);
353376
ggml_prec_set_acc(fa, GGML_PREC_F32);
354-
att = ggml_reshape_2d(C, fa, c.hidden, seq);
355-
} else {
356-
att = attention(C, Q, K, to_heads_v(C, v, c.head_dim, c.heads, seq), nullptr, scale, c.hidden, seq);
377+
return ggml_reshape_2d(C, fa, c.hidden, seq);
357378
}
358-
ggml_tensor * h1 = ggml_add(C, x, linear(C, w.Wo, w.bo, att));
379+
return attention(C, Q, K, to_heads_v(C, v, c.head_dim, c.heads, seq), nullptr, scale, c.hidden, seq);
380+
}
381+
382+
// One pre-norm SigLIP encoder block (SmolVLM2 tower), same graph as the other
383+
// in-tree models. Bidirectional attention, F32 score accumulation, tanh GELU.
384+
ggml_tensor * build_siglip_layer(ggml_context * C, const EncCfg & c, const EncBlockW & w,
385+
ggml_tensor * x, int64_t seq) {
386+
ggml_tensor * n1 = layer_norm(C, x, w.ln1w, w.ln1b, c.ln_eps);
387+
ggml_tensor * h1 = ggml_add(C, x, linear(C, w.Wo, w.bo, siglip_attention(C, c, w, n1, seq)));
359388
return ggml_add(C, h1, ffn_gelu(C, w.Wfc1, w.bfc1, w.Wfc2, w.bfc2, layer_norm(C, h1, w.ln2w, w.ln2b, c.ln_eps)));
360389
}
361390

@@ -705,6 +734,12 @@ ggml_tensor * attn_mlp(ggml_context * ctx, const LayerW & w, ggml_tensor * x_in,
705734
ggml_tensor * h1 = ggml_add(ctx, x_in, mm_w(ctx, w.Wo, ggml_reshape_2d(ctx, fa, cfg.q_full_dim, seq)));
706735

707736
ggml_tensor * x_norm_mlp = rms_norm(ctx, h1, w.Wln_post, cfg.rms_eps);
737+
#ifdef GGML_USE_HEXAGON
738+
// One gate|up matmul and a SwiGLU: two NPU ops where the split weights take
739+
// four, in a step loop where each op costs more to issue than to compute.
740+
if (w.Wgu)
741+
return ggml_add(ctx, h1, mm_w(ctx, w.Wdown, ggml_swiglu(ctx, mm_w(ctx, w.Wgu, x_norm_mlp))));
742+
#endif
708743
ggml_tensor * inter = ggml_mul(ctx, ggml_silu(ctx, mm_w(ctx, w.Wgate, x_norm_mlp)),
709744
mm_w(ctx, w.Wup, x_norm_mlp));
710745
return ggml_add(ctx, h1, mm_w(ctx, w.Wdown, inter));
@@ -1178,17 +1213,35 @@ std::unique_ptr<SmolVLAModelArch> smolvla_load_impl(ggml_type weight_dtype,
11781213
return W;
11791214
};
11801215

1216+
// gate|up as one weight: the fused graph's, and on Hexagon attn_mlp's. A
1217+
// part streamed into a view loads as float, so a stack whose gate the file
1218+
// packs keeps the two apart.
1219+
bool fuse_vlm_gu = m->fused_lm, fuse_exp_gu = m->fused_lm;
1220+
#ifdef GGML_USE_HEXAGON
1221+
{
1222+
const auto file_float = [&](const char * name) {
1223+
const ggml_type ft = use_gguf ? gst.file_type(hf_to_gguf(name)) : GGML_TYPE_COUNT;
1224+
return ft == GGML_TYPE_COUNT || !ggml_is_quantized(ft);
1225+
};
1226+
fuse_vlm_gu = file_float("model.vlm_with_expert.vlm.model.text_model.layers.0.mlp.gate_proj.weight");
1227+
fuse_exp_gu = file_float("model.vlm_with_expert.lm_expert.layers.0.mlp.gate_proj.weight");
1228+
}
1229+
#endif
1230+
11811231
m->vlm_layers.resize(cfg.n_layers);
11821232
for (int i=0; i<cfg.n_layers; ++i) {
11831233
LayerW & w = m->vlm_layers[i];
11841234
w.Wln_in = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, cfg.hidden);
11851235
if (m->fused_lm) {
11861236
w.Wqkv = fused_weight(cfg.hidden, {{&w.Wq, cfg.q_full_dim}, {&w.Wk, cfg.kv_full_dim}, {&w.Wv, cfg.kv_full_dim}});
1187-
w.Wgu = fused_weight(cfg.hidden, {{&w.Wgate, cfg.intermediate}, {&w.Wup, cfg.intermediate}});
11881237
} else {
11891238
w.Wq = ggml_new_tensor_2d(ctx, wdt, cfg.hidden, cfg.q_full_dim);
11901239
w.Wk = ggml_new_tensor_2d(ctx, wdt, cfg.hidden, cfg.kv_full_dim);
11911240
w.Wv = ggml_new_tensor_2d(ctx, wdt, cfg.hidden, cfg.kv_full_dim);
1241+
}
1242+
if (fuse_vlm_gu) {
1243+
w.Wgu = fused_weight(cfg.hidden, {{&w.Wgate, cfg.intermediate}, {&w.Wup, cfg.intermediate}});
1244+
} else {
11921245
w.Wgate = ggml_new_tensor_2d(ctx, wdt, cfg.hidden, cfg.intermediate);
11931246
w.Wup = ggml_new_tensor_2d(ctx, wdt, cfg.hidden, cfg.intermediate);
11941247
}
@@ -1227,7 +1280,7 @@ std::unique_ptr<SmolVLAModelArch> smolvla_load_impl(ggml_type weight_dtype,
12271280
w.Wv = ggml_new_tensor_2d(ctx, GGML_TYPE_F32, cfg.kv_full_dim, cfg.kv_full_dim);
12281281
}
12291282
}
1230-
if (m->fused_lm)
1283+
if (fuse_exp_gu)
12311284
w.Wgu = fused_weight(cfg.expert_h, {{&w.Wgate, cfg.expert_inter}, {&w.Wup, cfg.expert_inter}});
12321285
else {
12331286
w.Wgate = ggml_new_tensor_2d(ctx, wdt, cfg.expert_h, cfg.expert_inter);
@@ -1453,12 +1506,50 @@ std::unique_ptr<SmolVLAModelArch> smolvla_load_impl(ggml_type weight_dtype,
14531506
0, tile.size()*sizeof(float));
14541507
}
14551508
}
1509+
#ifdef GGML_USE_HEXAGON
1510+
if (m->W_at1->type == GGML_TYPE_F32 && m->b_at1->type == GGML_TYPE_F32) {
1511+
const int64_t H = cfg.expert_h;
1512+
const ggml_init_params ap = { ggml_tensor_overhead() * (size_t) (cfg.num_steps + 2), nullptr, true };
1513+
m->ctx_at = ggml_init(ap);
1514+
if (!m->ctx_at) { std::fprintf(stderr, "vla(smolvla): ggml_init (action-time) failed\n"); return nullptr; }
1515+
m->W_at1a = ggml_new_tensor_2d(m->ctx_at, GGML_TYPE_F32, H, H);
1516+
m->at1_step.resize((size_t) cfg.num_steps);
1517+
for (auto & t : m->at1_step)
1518+
t = ggml_new_tensor_1d(m->ctx_at, GGML_TYPE_F32, H);
1519+
m->at_buf = alloc_weights(m->ctx_at, m->backend);
1520+
if (!m->at_buf) { std::fprintf(stderr, "vla(smolvla): alloc (action-time) failed\n"); return nullptr; }
1521+
std::vector<float> W((size_t) (2 * H * H)), b((size_t) H), wa((size_t) (H * H)), bt((size_t) H);
1522+
ggml_backend_tensor_get(m->W_at1, W.data(), 0, ggml_nbytes(m->W_at1));
1523+
ggml_backend_tensor_get(m->b_at1, b.data(), 0, ggml_nbytes(m->b_at1));
1524+
for (int64_t o = 0; o < H; ++o)
1525+
std::memcpy(wa.data() + o * H, W.data() + o * 2 * H, (size_t) H * sizeof(float));
1526+
ggml_backend_tensor_set(m->W_at1a, wa.data(), 0, wa.size() * sizeof(float));
1527+
const float dt = -1.f/static_cast<float>(cfg.num_steps);
1528+
for (int step = 0; step < cfg.num_steps; ++step) {
1529+
const auto te = sinusoidal_time_emb(1.0 + double(step) * double(dt), H, cfg.min_period, cfg.max_period);
1530+
for (int64_t o = 0; o < H; ++o) {
1531+
double acc = b[(size_t) o];
1532+
for (int64_t i = 0; i < H; ++i)
1533+
acc += double(W[(size_t) (o * 2 * H + H + i)]) * double(te[(size_t) i]);
1534+
bt[(size_t) o] = (float) acc;
1535+
}
1536+
ggml_backend_tensor_set(m->at1_step[(size_t) step], bt.data(), 0, bt.size() * sizeof(float));
1537+
}
1538+
}
1539+
#endif
14561540

14571541
return m;
14581542
}
14591543
}
14601544

14611545
namespace {
1546+
// action_time_mlp_in over [action_emb; the step's time embedding].
1547+
ggml_tensor * action_time_in(SmolVLAModelArch * m, ggml_context * ctx, ggml_tensor * action_emb, int step) {
1548+
if (m->W_at1a)
1549+
return linear(ctx, m->W_at1a, m->at1_step[(size_t) step], action_emb);
1550+
return linear(ctx, m->W_at1, m->b_at1, ggml_concat(ctx, action_emb, m->time_bcasts[step], 0));
1551+
}
1552+
14621553
void build_graph(SmolVLAModelArch * m, ggml_context * ctx, SmolVLAModelArch::MainIO & io,
14631554
int n_views, int64_t n_lang, bool kv_leaves) {
14641555
Config cfg = m->cfg;
@@ -1517,8 +1608,7 @@ void build_graph(SmolVLAModelArch * m, ggml_context * ctx, SmolVLAModelArch::Mai
15171608
ggml_tensor * x_t = io.x0;
15181609
for (int step=0; step<cfg.num_steps; ++step) {
15191610
ggml_tensor * action_emb = linear(ctx, m->W_ain, m->b_ain, x_t);
1520-
ggml_tensor * action_time_in = ggml_concat(ctx, action_emb, m->time_bcasts[step], 0);
1521-
ggml_tensor * mlp1 = linear(ctx, m->W_at1, m->b_at1, action_time_in);
1611+
ggml_tensor * mlp1 = action_time_in(m, ctx, action_emb, step);
15221612
ggml_tensor * hs = linear(ctx, m->W_at2, m->b_at2, ggml_silu(ctx, mlp1));
15231613
for (int li=0; li<cfg.n_layers; ++li) {
15241614
const LayerW & w = m->expert_layers[li];
@@ -1572,8 +1662,7 @@ void build_graph(SmolVLAModelArch * m, ggml_context * ctx, SmolVLAModelArch::Mai
15721662
ggml_tensor * x_t = io.x0;
15731663
for (int step=0; step<cfg.num_steps; ++step) {
15741664
ggml_tensor * action_emb = linear(ctx, m->W_ain, m->b_ain, x_t);
1575-
ggml_tensor * action_time_in = ggml_concat(ctx, action_emb, m->time_bcasts[step], 0);
1576-
ggml_tensor * mlp1 = linear(ctx, m->W_at1, m->b_at1, action_time_in);
1665+
ggml_tensor * mlp1 = action_time_in(m, ctx, action_emb, step);
15771666
ggml_tensor * h = linear(ctx, m->W_at2, m->b_at2, ggml_silu(ctx, mlp1));
15781667
for (int li=0; li<cfg.n_layers; ++li) {
15791668
if (expert_self_attn(cfg, li)) {
@@ -1595,6 +1684,10 @@ void build_graph(SmolVLAModelArch * m, ggml_context * ctx, SmolVLAModelArch::Mai
15951684
}
15961685

15971686
SmolVLAModelArch::~SmolVLAModelArch() {
1687+
if (at_buf)
1688+
ggml_backend_buffer_free(at_buf);
1689+
if (ctx_at)
1690+
ggml_free(ctx_at);
15981691
if (q8_buf)
15991692
ggml_backend_buffer_free(q8_buf);
16001693
if (ctx_q8)

‎src/modules/siglip_vit.cpp‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,22 @@ void SigLipTower::declare(WeightLoader & L, const char * prefix, int64_t layers,
3434
}
3535

3636
ggml_tensor * SigLipTower::embed_conv(ggml_context * C, ggml_tensor * pixels, int64_t patch, int64_t grid) const {
37-
ggml_tensor * conv = ggml_conv_2d(C, patch_w, pixels, (int)patch, (int)patch, 0, 0, 1, 1);
3837
const int64_t nv = pixels->ne[3];
38+
#ifdef GGML_USE_HEXAGON
39+
// The weight times the im2col columns lands [hidden, grid*grid(, nv)]
40+
// directly. ggml_conv_2d's product is the transpose, and turning it back
41+
// takes two large transposing copies, which the NPU runs slowly.
42+
{
43+
const int64_t K = patch_w->ne[0] * patch_w->ne[1] * patch_w->ne[2];
44+
ggml_tensor * cols = ggml_im2col(C, patch_w, pixels, (int) patch, (int) patch, 0, 0, 1, 1, true, GGML_TYPE_F32);
45+
ggml_tensor * flat = ggml_mul_mat(C, ggml_reshape_2d(C, patch_w, K, patch_w->ne[3]),
46+
ggml_reshape_2d(C, cols, K, cols->ne[1] * cols->ne[2] * cols->ne[3]));
47+
if (nv > 1)
48+
flat = ggml_reshape_3d(C, flat, enc.cfg.hidden, grid * grid, nv);
49+
return ggml_add(C, ggml_add(C, flat, patch_b), pos);
50+
}
51+
#endif
52+
ggml_tensor * conv = ggml_conv_2d(C, patch_w, pixels, (int)patch, (int)patch, 0, 0, 1, 1);
3953
// One view: [hidden, grid*grid]. A batch of views ([W, H, 3, nv] pixels):
4054
// [hidden, grid*grid, nv], what build(..., nv) takes.
4155
ggml_tensor * flat = nv == 1

0 commit comments

Comments
 (0)