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.
tlm_adjoint, by design, allows application code to directly construct and work with tape objects (
Equation
objects). This makes caching easier, as the application code can reuse tape objects, the tape objects can cache intermediate results (e.g. finite element assembly or linear solvers), and multiple references to the same object are held on the tape. It makes analyzing the computational graph more difficult, as the tape is not an exact representation of the computational DAG, and this is instead inferred elsewhere.It also makes managing variable references more difficult. In particular we want to be able to record two entries on the tape via
i.e. without having to pass any values to the
solve
method calls. However we also wantto not leak variable values, as that would defeat checkpointing algorithms. This is resolved by instead recording
WeakAlias
objects on the tape, which hold references only to the attributes of an object. When the original object (the originalEquation
) is destroyed the manager knows it is safe to drop references to variable values, by calling thedrop_references
method of theWeakAlias
.The
WeakAlias
works by holding a reference only to the__dict__
attribute of an original aliased object. The original object can then later be destroyed while retaining attributes. This works as methods are dynamically bound (test = []; assert test.append is test.append # Fails!
) and so the original object doesn't usually reference bound methods (see alsoweakref_method
, used to avoid creating reference cycles via manually bound methods).This PR simplifies the
WeakAlias
implementation by simply setting the__dict__
attribute of theWeakAlias
.