Skip to content

minicpm-v-4.7-bf16:1b: unquantized MiniCPM-V-4.7-1B on the NPU - #1220

Open
michyu-amd wants to merge 18 commits into
mainfrom
minicpm-v4.7-bf16
Open

michyu-amd wants to merge 18 commits into
mainfrom
minicpm-v4.7-bf16

Conversation

@michyu-amd

Copy link
Copy Markdown
Collaborator

Adds minicpm-v-4.7-bf16:1b: MiniCPM-V-4.7-1B with every weight in bf16 (no quantization), as a new model next to minicpm-v-4.7:1b. Weights: https://huggingface.co/FastFlowLM/MiniCPM-V-4.7-1B-BF16-NPU2 (bit-identical to openbmb/MiniCPM-V-4.7-1B). Needs the CI pull request ci/pr-tier-minicpm-v-4.7-bf16 merged first (PR_CHAT line).

What it adds (all new; the shipped minicpm-v-4.7 model, engine and kernels are unchanged):

  • Converter: configs/minicpm_v_1b_bf16.json, q4nx/models/minicpm_v_bf16.py, arch MINICPM_V_1B_BF16 (-f minicpm-v-4.7-1b-bf16).
  • Engine flm-dev-kit/FLM_DLL/detail/minicpm_v_4_7_bf16_npu/ (+ engines.json): decode on the NPU with a bf16 decode kernel (lm_head inside the layer xclbin), prefill matmuls on the bfp16 GEMM, vision attention round trip from vision_mm_p's context; optional round-to-nearest for the prefill attention tiles (FLM_MINICPM_V_BF16_ATTN_RNE=1, off by default).
  • Kernels flm-dev-kit/FLM_Xclbin/MiniCPM_V_BF16/: decoding (new __BF16__ weight path, QWEN3_5_08B_BF16_SL), and copies of the units it loads (bfp16_mm, gated_attention_prefill_chunked, vision_attn, vision_attn_kv, vision_mm, vision_mm_p, test-only vision_mm_c). Kernel assignment, build units and RTP lock stanzas for the model.
  • App: modeling_minicpm_v_bf16* (copied wrapper; no presence penalty; optional FLM_MINICPM_V_THINK_IN_PREFILL=1), the engine's app header, registration, model_list.json + model_info.json entries, Windows installer entries, test harness src/test/minicpm_v_4_7_bf16_npu/.

Shared files touched (other models may be rebuilt/retested): flm-dev-kit/FLM_DLL/engines.json, flm-dev-kit/FLM_Xclbin/{build_units.json,kernel_assignment.json,kernel_assignment.md,rtp_addresses.lock.txt}, flm-dev-kit/FLM_Q4NX_Converter/q4nx/{constants.py,models/__init__.py}, src/common/AutoModel/{builtin_backends.cpp,builtin_frontends.cpp,model_frontend.cpp}, src/server/rest_handler.cpp (raw-RGB8 image check), src/CMakeLists.txt, src/inno/flm.iss, src/wix/flm.wxs, src/model_list.json, src/model_info.json, .github/script/ci/changes.py.

Libraries and xclbins are not in this pull request: they come in the dev-kit swap commit after CI.

Measured on Strix Halo (local builds): decode 33.8 / 31.8 / 28.7 tok/s at 128 / 4K / 10K context; language prefill 98 / 202 / 598 / 1209 ms at 128 / 1K / 4K / 8K tokens.

🤖 Generated with Claude Code

michyu-amd and others added 13 commits December 1, 2026 12:32
Add a converter path for an unquantized MiniCPM-V-4.7-1B package, from
llama.cpp's BF16 GGUF (no llama-quantize):

- configs/minicpm_v_1b_bf16.json: the 1B name map with every tensor BF16.
- q4nx/constants.py: ModelArch.MINICPM_V_1B_BF16, selected with
  -f minicpm-v-4.7-1b-bf16 (listed before the 1B entry because -f
  matching is by prefix).
- q4nx/models/minicpm_v_bf16.py: MiniCPM_V_1B_BF16 writes every 2-D
  language weight as plain row-major bf16 (N, K), keeps ssm_alpha/beta in
  bf16 (plus the .bf16 twins the host prefill reads), zero-pads
  output.weight to a multiple of 512 rows, and refuses quantized input.
  The vision tower uses the parent's path unchanged.

Every language and vision tensor of the resulting package matches the HF
checkpoint bit for bit, after llama.cpp's own value conventions (+1 folded
into RMSNorm weights, -exp(A_log), conv1d transpose, q/gate split).

Co-Authored-By: Claude <noreply@anthropic.com>
First step of the unquantized MiniCPM-V-4.7-1B engine: a pure copy of
detail/minicpm_v_4_7_npu (and its public header) under new names, still
running the Q4 logic. The bf16 weight path comes in later commits.

- detail/minicpm_v_4_7_bf16_npu/, include/models/minicpm_v_4_7_bf16/:
  class, library, file names, namespaces (mcv, mcv_npu, minicpm_reconfig,
  minicpm_wait, cpu_func), the C state API and every header-level global
  renamed, so the two engines share no symbol when one process loads both
  (same-named inline statics would otherwise be one GNU-unique copy).
  Undoing the renames gives the original files byte for byte.
- engines.json: the engine with family minicpm-v-4.7-bf16 and tag
  minicpm-v-4.7-bf16:1b (kernel assignment and catalog entry follow).
- build_minicpm_v_4_7_bf16.sh: builds it.

Checked: both engines build (XRT, host-only); no engine-own symbol is
exported by both libraries; with both libraries in one process the copy
gives the same vision hashes, prompt ids and answers as the original.

Co-Authored-By: Claude <noreply@anthropic.com>
…del uses

Copy the six FLM_Xclbin build units the unquantized MiniCPM-V-4.7-1B will
load into their own folder, so its kernels can change without touching
the units the shipped models build from:

- MiniCPM_V_BF16/decoding                        <- MiniCPM_V/decoding
- MiniCPM_V_BF16/gated_attention_prefill_chunked <- Qwen3_5/gated_attention_prefill_chunked
- MiniCPM_V_BF16/vision_attn_kv                  <- MiniCPM_V/vision_attn_kv
- MiniCPM_V_BF16/vision_mm_p                     <- MiniCPM_V/vision_mm_p
- MiniCPM_V_BF16/vision_mm                       <- Qwen3_8/vision_mm
- MiniCPM_V_BF16/bfp16_mm                        <- bfp16_gemm

Tracked files only, unchanged except five path lines: bfp16_mm sits one
level deeper (HOST_COMMON_DIR), a build path in the decoding host README,
and the vision_mm_p host test, which keeps using the original
MiniCPM_V/vision_mm_c as its bit-exact reference.

Built with Peano 22: the IRON .mlir and RTP address files are identical to
the originals' builds, and every unit's host test gives the same result
as the original's. Build units, the kernel assignment and the RTP lock
stanzas follow with the engine.

Co-Authored-By: Claude <noreply@anthropic.com>
The copied engine now loads the unquantized MiniCPM-V-4.7-1B package and
runs it fully on the host (build with PREFILL_MM=cpu PREFILL_ATTN=cpu
PREFILL_GDN=cpu PREFILL_CONV=cpu DECODE=cpu):

- Every projection, ssm_alpha/beta and the lm_head are flm_bf16, stored
  as plain row-major (N, K) and copied as is (copy_weight); the order of
  the *_all() vectors is unchanged.
- bf16_row_block(): the flm_bf16 case of dequant_row_block, so the host
  matmuls, the decode-on-CPU path and the host lm_head keep their fp32
  accumulation unchanged.
- The embedding must be in the package; the Q8_0 fallback
  (embedding_q8_0.hpp) is removed.
- An NPU build compiles but stops at load with a clear message until the
  bf16 decode and prefill kernels exist.

Checked against Hugging Face on the host: prompt ids equal on 9 prompts
(text, long, multi-turn, tool call, two pictures); greedy answers equal
the fp32 reference except at near-ties; per-layer agreement at the level
of HF's own bf16 run.

Co-Authored-By: Claude <noreply@anthropic.com>
Copy MiniCPM_V/vision_mm_c into MiniCPM_V_BF16/ as a test-only unit (no
model loads it): the vision_mm_p host test runs design C as its bit-exact
reference. With C next to it, the vision_mm_p copy's three test paths go
back to the original text, so both units are now exact copies of their
originals and the bf16 kernel folder no longer reads from MiniCPM_V/.

The C copy builds with Peano 22 into the same .mlir and RTP files as the
original, and the vision_mm_p host test passes against it (P and C
bit-identical in all six shapes).

Co-Authored-By: Claude <noreply@anthropic.com>
…on tiles

The host packs the prefill attention's q / K / V^T tiles into bfp16 with
an arithmetic shift and a truncating byte convert, which rounds every
value toward minus infinity. That leaves a constant -ulp/2 bias on the
attention output at every KV length.

- cpu_functions.hpp: FLM_MINICPM_V_BF16_ATTN_RNE=1 makes
  _bfp16_tile_group / _bfp16_vt_tile_group round to nearest even with
  signed saturation (the rule pack_bfp16 already uses). Off by default;
  with it off the packers are unchanged byte for byte.
- minicpm_v_bf16_prefill.hpp: the debug cross-check no longer reads the
  up/gate buffer when the GLU is fused (it is not allocated then).
- minicpm_v_bf16_prefill_xref.hpp: debug xref lines also print a
  double-precision cosine and the mean error, so a bias is visible.
- gated_attention_prefill_chunked host test: --round floor|rne, and the
  README's hash list replaced by today's floor / rne results.

With the flag on: kernel test cosine >= 0.9997 at every KV length up to
28,672 (floor: down to 0.78 on random data); real 3-turn chat, attention
mean error ~0 in every layer; answers vs Hugging Face no worse.

Co-Authored-By: Claude <noreply@anthropic.com>
Add a __BF16__ weight format to MiniCPM_V_BF16/decoding, built as two
variants: QWEN3_5_08B_BF16 (stateful, driven by the unit's run.exe) and
QWEN3_5_08B_BF16_SL (every run self-contained, for the engine).

- Weights stream as 32 x 64 bf16 blocks (4 KB, column-major), so every L1
  buffer and pinned RTP address stays where it is; four blocks per 256-wide
  x slice. kernels/bf16_core.h: bf16 x bf16 into fp32 accumulators, no
  dequantization. The lm_head is a plain projection on the same blocks;
  alpha/beta use them too.
- The projection output rounds to nearest even for the BF16 types (Q4
  types keep their rounding).
- host/: run.exe takes the weight format from the package; bf16 device
  layout, CPU reference on the bf16 weights, --check prints mean error.
- Makefile: link now builds the IRON .mlir (it had an unset target).
- Removes the copied ANALYSIS.md and PLAN_LM_HEAD_IN_LAYER.md.

On the bf16 package: worst per-layer cosine vs the fp32 CPU reference
0.99930 (Q4 kernel on its package 0.99909), lm_head logits 0.999997 with
the same top-5, repeat runs bit-identical; one token (24 layers + lm_head)
29.65 ms = 33.7 tok/s at 128 tokens of context, the lm_head phase at the
DDR ceiling.

Co-Authored-By: Claude <noreply@anthropic.com>
… kernel

Decode (24 layers + lm_head) now runs on the NPU with the bf16 decode
kernel (MiniCPM_V_BF16/decoding, QWEN3_5_08B_BF16_SL), loaded from the
model folder's layer.xclbin.

- Weights and the lm_head go to the device in the kernel's 32 x 64 bf16
  block order; the weight DMAs are sized by dtype. The host matmuls read
  the same blocked buffers (no second row-major copy), so the host prefill
  is unchanged byte for byte and host decode got faster.
- The layer_lm.xclbin / layer_lm_sl.xclbin / two-xclbin decode paths and
  the single-context decode are removed from this engine, with their env
  switches (FLM_MINICPM_LM_IN_LAYER, FLM_MINICPM_LM_STATELESS,
  FLM_MINICPM_DEC_SINGLE_CTX).
- The load-time refusal now covers only the NPU prefill matmuls.

Decode speed in the engine: 33.8 / 31.8 / 28.7 tok/s at 128 / 4K / 10K
tokens of context (lm_head at the DDR ceiling, ~0.04 ms host work per
token). The NPU lm_head matches the host's exactly; the decode layers
agree with the host path at the same level as the Q4 decode kernel does
with its own host path; repeated runs are identical.

Co-Authored-By: Claude <noreply@anthropic.com>
The prefill projections of the bf16 engine now run on the NPU through the
bfp16 GEMM (FLM_Xclbin/MiniCPM_V_BF16/bfp16_mm, slot mm_bfp16.xclbin).

- minicpm_v_bf16_mm_bfp16.hpp: the unit's run-time sequence and bfp16
  packer under the engine's own namespace; the weights are packed once at
  load time (round to nearest even, ~0.38 s, +567 MB device memory) and
  each call's input is packed the same way.
- Every projection of both layer kinds runs on the NPU (fused where the
  kernel's 1024-column blocks need it); GLU, norms, residuals and
  alpha/beta stay on the host; the block pipeline stays on.
- Removed from this engine: the Q4 mm paths, the fused GLU, the attention
  single-context round trip and their env switches (FLM_MINICPM_MM_RTM,
  FLM_MINICPM_GLU_FUSED, FLM_MINICPM_GLU_MIN_ROWS, FLM_MINICPM_SINGLE_CTX).
- A PREFILL_MM=cpu build keeps the host matmuls (logits unchanged).

Language prefill: 98 / 202 / 598 / 1209 ms at 128 / 1K / 4K / 8K tokens
(host matmuls: 169 / 989 / 4273 / 8999 ms). Each NPU call matches fp32 of
its packed operands; each projection matches the host fp32 matmul at
cosine >= 0.9998 with no bias.

Co-Authored-By: Claude <noreply@anthropic.com>
…text

With vision_mm_p loaded, every vision matmul runs in P's hw_context, but
the in-stream attention round trip was hard-wired to vision_mm_a's, so
each of the 27 blocks still switched P -> A -> P. The round trip now uses
the context of the matmul design in use (P when loaded, else A as before),
and the load-time self-test checks a matmul in that same context after
the round trips.

54 fewer hw_context switches per picture: vision tower 137-151 ms faster
(448x448 318 -> 181 ms, 1080p 897 -> 758 ms), image_embeds and answers
unchanged; the two-context fallback (FLM_MINICPM_V_VISION_SINGLE_CTX=0 or
another process on the NPU) gives the same results.

Co-Authored-By: Claude <noreply@anthropic.com>
Wire the bf16 engine (minicpm_v_4_7_bf16_npu) into the app as family
minicpm-v-4.7-bf16, tag minicpm-v-4.7-bf16:1b, weights folder
MiniCPM-V-4.7-1B-BF16-NPU2.

- modeling_minicpm_v_bf16.{hpp,cpp}, modeling_minicpm_v_bf16_image.cpp: a
  copy of the MiniCPM-V wrapper for the bf16 engine class and its state
  API; sampling defaults with no presence penalty (as Hugging Face);
  optional FLM_MINICPM_V_THINK_IN_PREFILL=1 prefills the empty think block
  instead of feeding it in decode; inference errors print the exception.
- The engine's app header, the frontend / backend registration, the
  engine link list, the server's raw-RGB8 image check, the Windows
  installers and the CI engine table (changes.py).
- model_list.json: the catalog entry (upload fields are placeholders).
- src/test/minicpm_v_4_7_bf16_npu: the test harness (prompt, cached
  follow-up, clear_context, picture), saving the prefix on turn 1 so the
  follow-up hits the prompt cache.

Co-Authored-By: Claude <noreply@anthropic.com>
Tell CI which xclbins to build for the unquantized MiniCPM-V-4.7-1B and
where they go (MiniCPM-V-4.7-1B-BF16-NPU2), all from MiniCPM_V_BF16/:

  layer.xclbin            decoding, QWEN3_5_08B_BF16_SL
  mm_bfp16.xclbin         bfp16_mm
  attn_bfp16c.xclbin      gated_attention_prefill_chunked
  vision_attn.xclbin      vision_attn
  vision_attn_kv.xclbin   vision_attn_kv
  vision_mm_a/_b.xclbin   vision_mm
  vision_mm_p.xclbin      vision_mm_p

- build_units.json: the eight MiniCPM_V_BF16 units (decoding builds only
  QWEN3_5_08B_BF16_SL; vision_mm_c is a test-only unit no model uses).
- kernel_assignment.json / .md: the model's entry and section.
- rtp_addresses.lock.txt: stanzas for the bf16 decode layer and bfp16_mm.
- MiniCPM_V_BF16/vision_attn: exact copy of Qwen3_8/vision_attn (the
  engine requires vision_attn.xclbin to exist).

Co-Authored-By: Claude <noreply@anthropic.com>
The package is now public at FastFlowLM/MiniCPM-V-4.7-1B-BF16-NPU2
(commit b4a673d5).

- model_list.json: flm_min_version 1.0.15 (the uploaded config.json's
  flm_version) and modified_at for the upload.
- model_info.json: the repo's file list with sizes and hashes for the
  tag, which flm pull checks every download against.

Co-Authored-By: Claude <noreply@anthropic.com>
@michyu-amd
michyu-amd requested a review from zaneni6 December 2, 2026 02:21
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Security scan found no critical or high findings on lines changed by this pull request

53 medium, low, or unrated finding(s) are not listed; the scan run has them.

View the security scan run for complete reports.

“New” means the finding is located in a changed file on an added or modified line; it is not a base-versus-head findings delta.

michyu-amd and others added 4 commits December 2, 2026 14:17
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
…still read

The pipelined GDN prefill layer starts the bfp16 matmul runs of one row
group before it packs the next. pack_x() grew an X staging slot by creating
a new buffer object in place, so on a prompt longer than any earlier one the
runs started before the grow still read the replaced buffer. Linux keeps that
memory intact until the runs finish; Windows reuses it, so layer 0 became NaN
and every answer turned into "/" repeated to max_tokens (customer video demo,
4 of 443 shots: exactly the requests that were longer than all before them).

reserve_x(L) now sizes all four X slots for the whole prefill at layer 0,
before any run starts, and a slot that still has to grow mid-prefill keeps
its old buffer alive until the next prefill (with a warning). Answers are
byte-identical to before on 27 demo shots; prefill time unchanged.

Co-Authored-By: Claude <noreply@anthropic.com>
…tage that turns NaN/inf

Off by default (one cached getenv, answers byte-identical). With
FLM_MINICPM_V_NAN_TRACE=1 the engine checks each request stage by stage
(vision blocks, image embeds, every prefill layer and GDN state, logits, the
first decode steps) with a bit-pattern NaN/inf test that survives /fp:fast
and -ffast-math, prints one line per stage and stops at the first bad one.
Also logs every bfp16 mm X slot grow. Found the "/" bug of the previous
commit; kept as a diagnostic for Windows users who report broken answers.

Co-Authored-By: Claude <noreply@anthropic.com>
@github-actions

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Dev-kit bench

Results of the dev-kit benches in Build and Test run 47001780528 for f1e209286d, as that run reported them (its artifacts hold the per-model reports).

Summary

Dev-kit bench (pr) on f1e209286d

the pull request's staged packages: this run's numbers only, no comparison (to be designed later)

✅ No hard-gate failure and no soft-gate warning.

Linux XRT

Perf (tok/s; dev-kit against the base package on the same runner)

Model Machine Context Decode base → dev-kit Δ Prefill base → dev-kit Δ Decode Δ vs nightly (same kind)
minicpm-v-4.7-bf16:1b strix 1k — → 30.7 — — → 3781.5 — no baseline for this kind
minicpm-v-4.7-bf16:1b strix 2k — → 30.0 — — → 4248.1 — no baseline for this kind
minicpm-v-4.7-bf16:1b strix 4k — → 29.3 — — → 4442.2 — no baseline for this kind
minicpm-v-4.7-bf16:1b strix 8k — → 27.6 — — → 4533.3 — no baseline for this kind

GSM8K accuracy (%)

Model Nightly baseline This run Δ (points) Notes
minicpm-v-4.7-bf16:1b — 80.0 —

Linux HRX

Perf (tok/s; dev-kit against the base package on the same runner)

Model Machine Context Decode base → dev-kit Δ Prefill base → dev-kit Δ Decode Δ vs nightly (same kind)
minicpm-v-4.7-bf16:1b krackan 1k — → 36.2 — — → 2506.5 — no baseline for this kind
minicpm-v-4.7-bf16:1b krackan 2k — → 35.5 — — → 2634.9 — no baseline for this kind
minicpm-v-4.7-bf16:1b krackan 4k — → 34.5 — — → 2819.7 — no baseline for this kind
minicpm-v-4.7-bf16:1b krackan 8k — → 31.6 — — → 3200.6 — no baseline for this kind

GSM8K accuracy (%)

Model Nightly baseline This run Δ (points) Notes
minicpm-v-4.7-bf16:1b — 80.0 —

Perf: flm bench, decode and prefill average tok/s per context. A/B is the one perf signal that judges a change; the runners differ by machine kind, so the nightly comparison is informational and only made for the same kind. Accuracy: GSM8K (first 50 questions) and DocVQA (when enabled), greedy; unanswered questions count as wrong. Per-question reports, server logs and CSVs: the devkit-bench-result-* artifacts.

@zaneni6

zaneni6 commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the detailed write-up, and great to see the bf16 engine building with MSVC in CI! One correction on the cause: #1232's "Place package DLLs" step only runs in release builds (when dlls is set), and it's skipped in this run. The MSI fails because src/wix/flm.wxs now lists minicpm_v_4_7_bf16_npu.dll, which isn't in src\lib\xrt until the swap:

src\wix\flm.wxs(196) : error WIX0103: Cannot find the File file '.\package\minicpm_v_4_7_bf16_npu.dll'

Inkling-2 hit the same thing and added its flm.wxs entry only after the swap commit (7ad84973). Could you drop the flm.wxs/flm.iss lines for now and add them back right after the swap lands? The next run should then fail only on "swap pending", which is the one the swap needs.

…ands

The MSI build takes the engine DLLs from src\lib\xrt, which gets this DLL
only with the dev-kit swap commit, so listing it before the swap fails
"Windows XRT / msi" (WIX0103). Same order as inkling-2 (7ad8497): drop the
flm.wxs / flm.iss entries now, add them back right after the swap.

Co-Authored-By: Claude <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants