Fix: separate onboard run context cleanup from final device teardown#532
Open
ChaoZheng109 wants to merge 1 commit intohw-native-sys:mainfrom
Open
Fix: separate onboard run context cleanup from final device teardown#532ChaoZheng109 wants to merge 1 commit intohw-native-sys:mainfrom
ChaoZheng109 wants to merge 1 commit intohw-native-sys:mainfrom
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the device cleanup logic by moving the rtDeviceReset call from reset_device_context to the finalize method in DeviceRunner. It also updates the MemoryAllocator to reset its finalized_ state upon new allocations and modifies the runtime API to call finalize instead of reset_device_context. Feedback was provided to guard the rtDeviceReset call with a check for a valid device_id_ to ensure the finalize method is idempotent and avoids passing an invalid ID to the runtime.
2655f49 to
8220425
Compare
- split onboard device lifecycle into thread attach, run-context setup,
run-context release, and final device teardown
- keep run-scoped streams available through init_runtime_impl so kernel
binary upload succeeds before execution starts
- release run-scoped streams after each run without calling
rtDeviceReset, and defer the final device reset to finalize()
- preserve the guarantee that all tracked device memory is freed before
rtDeviceReset runs
- align onboard API comments and error messages with the new lifecycle
8220425 to
53f3f96
Compare
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
run_runtime()lambda — letfinalize_device()handle it viaDeviceRunner::finalize()rtSetDevice()atfinalize()entry sortFreesucceeds even when called from a thread without an existing device contextrtDeviceReset()fromreset_device_context()intofinalize(), aftermem_alloc_.finalize(), to guarantee all device memory is freed before the context is torn downMemoryAllocator::finalized_flag — an emptyptr_set_is already a natural no-op, so repeated and interleavedfinalize()calls are idempotent without an explicit guardRoot Cause
The original teardown sequence was:
run_runtime()lambda →reset_device_context()→ destroys streams +rtDeviceResetfinalize_device()→DeviceRunner::finalize()→mem_alloc_.finalize()→rtFreeStep 2's
rtFreecalls happened afterrtDeviceResetin step 1, causing error 507899 (invalid device context).The fix centralizes all resource cleanup in
finalize(), called byfinalize_device():Testing