Skip to content

Refactor: replace DeviceRunner singleton with handle-based C API#483

Merged
ChaoWao merged 1 commit intohw-native-sys:mainfrom
hw-native-sys-bot:refactor/device-runner-handle-based-api
Apr 8, 2026
Merged

Refactor: replace DeviceRunner singleton with handle-based C API#483
ChaoWao merged 1 commit intohw-native-sys:mainfrom
hw-native-sys-bot:refactor/device-runner-handle-based-api

Conversation

@hw-native-sys-bot
Copy link
Copy Markdown
Collaborator

Summary

  • Replace DeviceRunner process-level singleton with handle-based create_device_context / destroy_device_context C API, enabling multiple ChipWorker instances per process
  • Use pthread_key_t to bind DeviceRunner* per calling thread, so HostApi wrapper functions resolve the correct instance — zero changes to HostApi signatures or runtime layer code
  • Remove DeviceRunner::get() singleton and make ctor/dtor public across all 4 platform variants (a5/a2a3 × sim/onboard)
  • Fix pre-existing build error in a2a3 tensormap_and_ringbuffer aicpu_executor.cpp (#else branch referencing undeclared PTO2_SCHED_PROFILING variables)

Testing

  • a5sim CI: 12/12 pass (-c 882c4db)
  • a2a3sim CI: 20/20 pass (-c 882c4db)
  • Unit tests: 11/11 pass (test_chip_worker.py)

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the DeviceRunner from a singleton to a context-based architecture, introducing explicit lifecycle management through new C API functions for creating and destroying device contexts. ChipWorker has been updated to own and pass these contexts to the runtime. The review feedback highlights a consistent need across platform implementations to clear thread-local DeviceRunner pointers after execution to ensure safety in scenarios where threads might be reused.

DeviceRunner was a process-level singleton, preventing multiple
ChipWorker instances from coexisting in a single process. Replace
with a handle-based design:

- Add create_device_context/destroy_device_context to C API; each
  ChipWorker owns an independent heap-allocated DeviceRunner
- Add DeviceContextHandle as first param to set_device, run_runtime,
  finalize_device
- Use pthread_key_t to bind DeviceRunner* per calling thread so
  HostApi wrapper functions (device_malloc, etc.) can resolve the
  correct instance without changing HostApi signatures or runtime code
- Remove DeviceRunner::get() singleton and make ctor/dtor public

Also fixes pre-existing build error in a2a3 tensormap_and_ringbuffer
aicpu_executor.cpp where an #else branch referenced variables only
declared under #if PTO2_SCHED_PROFILING.
@hw-native-sys-bot hw-native-sys-bot force-pushed the refactor/device-runner-handle-based-api branch from ad0c8f0 to 2b4f206 Compare April 8, 2026 13:01
@ChaoWao ChaoWao merged commit 87f961c into hw-native-sys:main Apr 8, 2026
10 checks passed
@ChaoWao ChaoWao deleted the refactor/device-runner-handle-based-api branch April 8, 2026 13:22
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 9, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Pin retry reads partial results from the killed subprocess and
  re-runs only failed/unfinished tasks
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 9, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Pin retry reads partial results from the killed subprocess and
  re-runs only failed/unfinished tasks
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 9, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 9, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 9, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 9, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 9, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 10, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 10, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)
hw-native-sys-bot pushed a commit to hw-native-sys-bot/simpler that referenced this pull request Apr 10, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(hw-native-sys#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)
ChaoWao added a commit that referenced this pull request Apr 10, 2026
Previously sim launched one subprocess per runtime group to avoid
host SO symbol collisions. With the handle-based DeviceRunner API
(#483), multiple runtimes can coexist in a single process.

- Replace run_sim_tasks_subprocess (per-runtime subprocesses) with
  a single _run_device_worker_subprocess call for all tasks
- Add parallel sim execution: tasks distributed across cpu_count/20
  virtual device IDs, each with its own ChipWorker in a thread
- ChipWorker::run() uses std::thread internally so the real work
  runs outside the Python GIL, enabling true parallelism
- Add timeout parameter to _run_device_worker_subprocess using
  subprocess.run(timeout=) for clean process kill on deadlock
- Thread-safe progress: [devN] [M/total] PASS/FAIL: task (Xs)

Co-authored-by: Chao Wang <26245345+ChaoWao@users.noreply.github.com>
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