Skip to content

npu_rt_api.h: declare the dma-buf bo constructor the private runtime already defines (refs #753) - #1526

Merged
bong-water-water-bong merged 1 commit into
mainfrom
1bit/npurtapi-dmabuf-ctor
Nov 8, 2026
Merged

bong-water-water-bong merged 1 commit into
mainfrom
1bit/npurtapi-dmabuf-ctor

Conversation

@bong-water-water-bong

Copy link
Copy Markdown
Collaborator

Refs #753. The private runtime tree's npu_rt_api.cpp defines

ext::bo::bo(const device& d, int dma_buf_fd, size_t size, void* host)

and no header declared it, so that tree does not compile — which is also what stops 1bit and the ORT plugin EP from building with ONEBIT_EP_NPU=ON, the only way the ONNX route's "NPU / HRX / CPU per op" claim can be measured instead of asserted.

I found this while trying to measure that (the TGPNPU vision program): the EP's NPU backend needs the private runtime, and the runtime refused to build. The blocker was not in the private code — it was a missing declaration in this public header.

Measured, before and after (no device needed)

step before with this declaration
<rt>/build-api-lib.sh <engine> <builddir> rc 2 — npu_rt_api.cpp:101:1: error: no declaration matches 'npu_rt_api::ext::bo::bo(const npu_rt_api::device&, int, size_t, void*)' rc 0, libnpu_rt_api.a 321,030 B
cmake -DONEBIT_EP_NPU=ON -DONEBIT_NPU_RT_DIR=<rt> same error (the EP compiles those sources itself) builds, libonebit_ep.so with the NPU backend, 414,416 B, 0 errors

Why it is safe to land ahead of the private work

A declaration with no definition is only an error if it is used, so this is inert for every build that does not compile the private sources — which is every public build and every CI job. The comment in the header says exactly that, and states the host == nullptr semantics from the runtime's own import_bo, so the next reader does not have to work out why a public header names a private implementation.

What this does not do — so "builds" is not read as "measured"

With no ELF configured, the EP now claims nothing at all on a graph it previously claimed 8 matmuls from: the NPU backend declines at capability time when no full ELF matches its (K, N). So the ONNX-on-NPU row is still unmeasured; it needs one ELF per (K, N) and a device window, and the device lock was held by another lane when this was written. That is recorded in the commit message and in my state file rather than left to look like progress on the row itself.

tools/copyright.py --check rc 0. The owning lane (the dma-buf import work) has been told: their tree builds now, and if their constructor's final signature differs this declaration is two lines to adjust.

…already defines (refs #753)

The private runtime tree's `npu_rt_api.cpp` defines

    ext::bo::bo(const device& d, int dma_buf_fd, size_t size, void* host)

and no header declared it, so that tree does not compile -- which is also what stops `1bit` and the ORT
EP from building with `ONEBIT_EP_NPU=ON`, the only way the ONNX route's "NPU / HRX / CPU per op" claim
can be measured rather than asserted.

Measured before/after on this box, no device needed:
  * `<rt>/build-api-lib.sh <engine> <builddir>`  ->  rc 2,
    `npu_rt_api.cpp:101:1: error: no declaration matches 'npu_rt_api::ext::bo::bo(const npu_rt_api::device&, int, size_t, void*)'`
  * with this declaration: `libnpu_rt_api.a` builds (321,030 B), and `cmake -DONEBIT_EP_NPU=ON
    -DONEBIT_NPU_RT_DIR=<rt>` builds `libonebit_ep.so` with the NPU backend (414,416 B, 0 errors).

The declaration is inert for every build that does not compile the private sources (a declaration with no
definition is only an error if it is used), which is why it can land ahead of them, and the comment says so
rather than leaving the next reader to work out why a public header names a private implementation.

What it does NOT do: with no ELF configured the EP now claims nothing at all on a graph it used to claim 8
matmuls from (the NPU backend declines at capability time when no full ELF matches), so the NPU row is still
unmeasured -- it needs one ELF per (K,N) and a device window, and the device lock was held by another lane
when this was written. Recorded so the next lane does not read "builds" as "measured".

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at c805ee8aa1593566625a5a2ff174857b455fe212

A header-only declaration that the private runtime already defines, and I could confirm it against a tree that builds it rather than taking the comment's word:

this PR                      npu/npu_rt_api.h:115
  bo(const device& d, int dma_buf_fd, size_t size, void* host = nullptr);

wt/onebp-1110/npu/npu_rt_api.h:115   (another lane's tree, same declaration)
  bo(const device& d, int dma_buf_fd, size_t size, void* host = nullptr);

Byte-identical, including the default argument and parameter order, so the signature here is the one a lane is already compiling against. wt/zc-753 carries an earlier, shorter-commented variant of the same line — the PR is the third generation and the most complete comment.

The header parses standalone (g++ -std=c++17 -fsyntax-only over a TU that includes it → rc 0), and the declaration sits in namespace ext { class bo : public npu_rt_api::bo } beside bo(const device&, size_t), which is where a device-attached buffer constructor belongs.

The comment's two claims are the right ones to make and both are checkable by the reader: dma_buf_fd is imported via DRM PRIME, pinned and mapped so the NPU addresses those pages with no copy, and host is the exporter's own mapping (amdgpu's or HRX's) — with nullptr meaning "give me the driver's mapping", which "the driver then marks invalid (npu_rt.cpp, import_bo)". That last clause is the one that saves a caller from passing nullptr and quietly getting a buffer they must not touch.

"And why the RT tree does not build without it: its npu_rt_api.cpp defines this constructor and no header declared it" is exactly the kind of sentence a declaration-ahead-of-implementation PR needs — it says why this can land now and what it unblocks, so nobody has to disassemble the history later.

Not run: a build of the private runtime (out of reach here). What I verified is the signature match against a tree that uses it, the standalone parse, and the placement.

@bong-water-water-bong bong-water-water-bong left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VERDICT: READY at c805ee8aa1593566625a5a2ff174857b455fe212

I reproduced the before/after the body describes, using the runtime tree's build-api-lib.sh under thermal-run and building into my own scratch dir, so nothing was written into the owning lane's tree:

engine checkout rc result
origin/main 2 error: no declaration matches 'npu_rt_api::ext::bo::bo(const npu_rt_api::device&, int, size_t, void*)'
this head 0 libnpu_rt_api.a, 321,030 B, the same size the body reports

Checked for correctness:

  • Signature. The definition is ext::bo::bo(const device& d, int dma_buf_fd, size_t size, void* host), with no default argument. So the header's = nullptr is the only default, which is legal; a default on both sides would be an error.
  • Overloads. bo(d, size) needs 2 arguments and the new one needs at least 3, so no existing call can become ambiguous.
  • The nullptr semantics in the comment. They match the runtime's import_bo: with host set, that address is used; without it, the driver's own mapping is used, and the driver marks that mapping invalid.
  • Inertness. git grep finds no caller in the public tree, so public builds never odr-use the declaration, and nothing public can fail to link.
  • tools/copyright.py --check exits 0.

Not reproduced: the -DONEBIT_EP_NPU=ON EP build. It compiles the same runtime source, so the header is the same gate, but that row is the author's.

One small note, not blocking. The public header's comment names private implementation details: "npu_rt.cpp, import_bo" and "npu_rt_api.cpp in the RT tree". The API contract (fd imported, host is the caller's mapping, nullptr means the driver's) is fine to state publicly, but the private file and function names add nothing for a public reader. Shortening that to "the private runtime" would keep the public tree free of internal file names.

I agree with "builds, not measured": with no ELF configured, the ONNX-on-NPU row is still unmeasured.

@bong-water-water-bong
bong-water-water-bong merged commit fc0c183 into main Nov 8, 2026
9 checks passed
@bong-water-water-bong
bong-water-water-bong deleted the 1bit/npurtapi-dmabuf-ctor branch November 8, 2026 19:07
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

🎫 Ticket compliance analysis 🔶

753 - Partially compliant

Compliant requirements:

  • Declared the dma-buf bo constructor in the public header

Non-compliant requirements:

  • Does not implement the actual dma-buf import functionality in the NPU submission path
  • Does not enable the zero-copy hybrid/split path

Requires further human verification:

  • The actual implementation of the dma-buf import functionality in the private runtime needs to be verified
  • The zero-copy hybrid/split path functionality needs to be validated on the hardware
⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Missing Implementation

The PR only declares the dma-buf bo constructor in the public header but does not implement the actual functionality to import dma-buf handles for NPU execution. The implementation exists in the private runtime but is not yet connected to the public API, which means the declared function is currently inert and cannot be used.

bo(const device& d, int dma_buf_fd, size_t size, void* host = nullptr);

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant