Refactor: replace DeviceRunner singleton with handle-based C API#483
Merged
ChaoWao merged 1 commit intohw-native-sys:mainfrom Apr 8, 2026
Conversation
There was a problem hiding this comment.
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.
ad0c8f0 to
2b4f206
Compare
ChaoWao
approved these changes
Apr 8, 2026
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
2 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
create_device_context/destroy_device_contextC API, enabling multiple ChipWorker instances per processpthread_key_tto bind DeviceRunner* per calling thread, so HostApi wrapper functions resolve the correct instance — zero changes to HostApi signatures or runtime layer codeDeviceRunner::get()singleton and make ctor/dtor public across all 4 platform variants (a5/a2a3 × sim/onboard)aicpu_executor.cpp(#elsebranch referencing undeclaredPTO2_SCHED_PROFILINGvariables)Testing
-c 882c4db)-c 882c4db)test_chip_worker.py)