Skip to content

Add: prepare and freeze kernel context device resources - #2173

Draft
TaoZQY wants to merge 4 commits into
hw-native-sys:mainfrom
TaoZQY:codex/hbg-pr-context-resources
Draft

Add: prepare and freeze kernel context device resources#2173
TaoZQY wants to merge 4 commits into
hw-native-sys:mainfrom
TaoZQY:codex/hbg-pr-context-resources

Conversation

@TaoZQY

@TaoZQY TaoZQY commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Move kernel device capacity ownership into context resource preparation. Allocate one aligned heap and one packed runtime arena, with Definition and scheduler slices, then freeze their addresses and capacities. Launch binding only checks context identity and required sizes before borrowing existing addresses.

Preserve platform allocator accounting and retain ownership after rollback or close failures so cleanup can be retried. Context stream creation distinguishes dedicated non-hidden AICPU from hidden AICore; caller remains a separate borrowed stream.

Validation

  • Both HBG C++ suites passed (32 cases per architecture), together with context and contract tests at this milestone.
  • Full rebased stack at H3 Add: seal and validate HBG execution slots on AICPU #2175: 147 C++ test executables and 159 Python tests passed (4 hardware-marked skips), covering ABI, task interfaces and simulator DMA workspace.
  • Full-stack simulation with the pinned PTO ISA required: A2/A3 74 passed, 8 skipped; A5 71 passed.
  • Runtime libraries rebuilt; repository pre-commit checks passed. Each earlier milestone was independently compiled and tested.
  • mkdocs build --strict passed separately at all five PR heads. The HBG contract page is included in navigation from H1; the execution-slot page is included from H3.

Public K1 owner integration remains pending. Context close requires external quiescence and captured-graph lifetime ordering; these resource helpers do not introduce hidden synchronization.

Dependency and review scope

Draft 3/5: Context prepare/freeze. Depends on #2172.

The stack was rebased onto main at f2478fadaf56a6fefacb8768f62a431b124a749f. The simulator runner retains both the SDMA communication header and K1 context-control header. Runtime and test changes are preserved; documentation navigation fixes are folded into the milestones that introduce the pages. The subsequent main documentation-theme update also merges cleanly with these branches.

All five upstream drafts target main, so this branch includes K1 and prior milestones (4 cumulative commits). The isolated diff above contains only this milestone. Rebase in dependency order after prerequisites land.

The original K1 snapshot 21fb15c0 is carried as rebased commit 5612d48473592269b2d08a43a771afcedeb07594. Upstream #2064 remains open and has advanced; alignment with its final version remains public execution integration work.

No idle onboard devices were available. Simulation golden checks cover existing program-mode regression; mixed ACLGraph capture/replay, DSV4 CSA device accuracy and comparative performance remain unvalidated.

@coderabbitai

coderabbitai Bot commented Sep 9, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Kernel mode is simpler's second execution identity: instead of owning
the device, a context borrows the caller's already-current device and
stream to enqueue one bounded asynchronous operator per launch, so a
PyPTO program is capturable by ACLGraph as an ordinary node. This
change freezes the public surface that identity hangs off; it creates
no resources and changes no program-path behavior.

- runtime_c_api.h declares the lifecycle entries
  simpler_kernel_mode_{supported,init,prepare_callable,launch} plus
  simpler_kernel_mode_ctx_control with its versioned
  SimplerKernelCtxControl POD (mode SIMPLER_MODE_PROGRAM/KERNEL,
  CONFIGURE/FREEZE actions, exact struct_size match, layout pinned by
  static_asserts) and adds the host-band codes
  PTO_RUNTIME_ERR_INVALID_STATE / _CAPACITY_EXCEEDED. The existing
  finalize_device stays the fifth lifecycle entry. FREEZE requires a
  previously accepted kernel-mode CONFIGURE and rejects a second
  FREEZE, so a successful FREEZE is synonymous with "the capacity
  guard will enforce this" and program-mode contexts stay structurally
  outside that guard's reach.
- ensure_acl_ready() and force_reset_device() refuse with UNSUPPORTED
  on a context configured for kernel mode: the ACL lifecycle belongs
  to the caller, and every call site of the five ACL lifecycle APIs
  (aclInit, aclrtSetDevice, aclrtResetDevice, aclrtResetDeviceForce,
  aclFinalize) is either inside force_reset_device() or gated on
  acl_ready_, which only ensure_acl_ready() sets — so poison recovery
  can never reset the device out from under the host process.
- kernel_invocation_header.h pins the 64-byte envelope every kernel
  launch ships to the AICPU (version / mode / callable / generation /
  int32_t arg counts / payload length). Runtime payload formats are
  defined under it by each runtime, and its fail-closed validation
  belongs to the AICPU-side consumers rather than this header.
- KernelCtxControlState implements the CONFIGURE/FREEZE ordering table
  once for all eight host-runtime components: unsupported stubs run
  identical structural and ordering validation and diverge only after
  every check passes, so an argument a stub accepts is one the real
  implementation accepts.
- KernelExecutionState and ExecutionModeClaimState carry the kernel
  context phase machine (New/Collecting/ReadyEnqueued/Poisoned/
  Closing/Closed with sticky, retriable Closing and separate
  runtime-error and teardown-error slots) and the two restricted
  operation vocabularies; synchronize, allocation, capture queries,
  and model attachment stay unrepresentable in those tables.
- ChipWorker dlsyms the five new symbols from every runtime, so a
  component missing one fails at load. test_host_runtime_abi.py
  asserts the export across all eight components, and table-driven
  UTs cover the control table, the phase machine, and both wire
  layouts.
- ChipCallable additionally records scalar_count in four bytes of
  historical header tail padding, the callable-side comparandum for
  the invocation header's scalar_count. Every historical offset,
  sizeof, and the kernel-cache ABI token are unchanged; a legacy blob
  reads 0. make_callable validates [0, CHIP_MAX_SCALAR_ARGS];
  ChipCallable.build gains a trailing scalar_count=0 keyword and a
  read-only property; static_asserts pin both ChipCallable and
  CoreCallable layouts (CoreCallable's leaf header has no padding, so
  it carries no such field).
Expose a leased Host graph build result without device allocation or H2D.
Keep program execution on an explicit upload path that compacts from the
virtual-address source, and rebind retained Definition staging after growth
so repeated uploads and failed-copy retries keep a valid source.

Add dual-architecture build/upload tests and document workspace ownership.
Validate both HBG C++ suites and both architecture simulation suites.
Query graph-dependent heap, runtime image, Definition and scheduler sizes
without binding or allocating device memory. Aggregate compatible graphs
by region and recompute aligned offsets for one execution slot.

Align common resource validation with TMR while retaining the current K1
wire ABI and program byte semantics. Validate three distinct stream roles
and reject unsupported configuration-only HBG sizing queries.

Cover size overflow, Definition framing, concurrent queries, stream
aliases and loader admission; document Host tensor-data capability rules.
Make the kernel context own an aligned heap and packed runtime arena,
including Definition and scheduler regions. Prepare capacity once and
freeze it explicitly; launch binding only validates identity and sizes
before borrowing existing addresses.

Preserve ownership on failed allocation rollback or close for retry, and
retain platform allocator accounting. Distinguish dedicated AICPU and
hidden AICore stream creation and their completion events.

Test capacity boundaries, lifecycle, allocator failures, close retry and
concurrent preparation on both HBG architectures.
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