Repository navigation
Context change hook - #187
Open
freddieknets wants to merge 4 commits into
Open
freddieknets wants to merge 4 commits into
freddieknets wants to merge 4 commits into
Conversation
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.
Description
Add context-change lifecycle hook to
HybridClassThis PR introduces an
_on_context_change(old_context, new_context)lifecycle hook forHybridClass.The hook allows classes backed by Xobjects to react when their backing Xobject is created in, moved to, or copied to a different execution context.
Motivation
Until now, subclasses that need context-specific setup have had to implement this separately in their constructor,
move(), andcopy()methods.This is both repetitive and easy to get wrong, since moving or copying an Xobject does not rerun the subclass constructor.
A concrete example is Xcoll's
EverestCrystal. CUDA tracking of crystals requires a larger per-thread CUDA stack. Initially this setup was performed only inEverestCrystal.__init__():This worked when a crystal was constructed directly on
ContextCupy, but not when it was first constructed on CPU and subsequently moved to CUDA as part ofLine.build_tracker():HybridClass.move()replaces the backing Xobject without rerunning__init__().The same issue applies to
copy(_context=...).More generally, subclasses may need to initialise or validate resources whenever they enter a particular context. A common lifecycle hook provides one place to implement such behaviour.
API
Subclasses can implement:
where:
old_contextisNoneduring initial construction;new_contextis the context of the current backing Xobject;The base implementation is a no-op.
For example:
Behaviour
The hook is invoked:
after initial Xobject construction, as
after
move()when the context changes;on the newly created object after
copy()when the destination context differs from the source context.It is not invoked when only the backing buffer changes while remaining in the same context.
Initialisation is hooked through
xoinitialize()rather than only throughHybridClass.__init__(), since subclasses are allowed to callxoinitialize()directly.Example
Tests
The added tests verify that:
move()reports the correct source and destination contexts;copy()invokes the hook on the newly created object without modifying the source object;__setstate__()invokes the hookxoinitialize()directly receive the initial lifecycle callback.This keeps context-dependent setup in one well-defined place and avoids subclasses having to duplicate lifecycle handling across construction, move, and copy operations.
Checklist
Mandatory:
Optional: