This repository has been archived by the owner on Jul 1, 2023. It is now read-only.
[Layer] Remove the 'context' argument from 'Layer.applied(to:in:)'. #87
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.
This PR removes the
context
argument fromLayer
'sapplied(to:in:)
method to simplify the call sites of layer application. Instead of passing contexts as an argument, we make contexts available as thread-local information like how our device placement APIs (withDevice(_:execute:)
) work.We made this decision based on user feedback and API design principles, and would like our APIs to be clear and fluent. The team is aware of potential slight concerns in the context of concurrency, but has decided to start from first principles to make this APIs easier to use for everyone.
Internal changes
Add
ContextManager
class, each of whose instance manages a stack of contexts for one thread. Each thread-local singleton can be accessed through thread-safe type propertylocal
. The most recent context can be accessed through computed propertycurrentContext
. Pushing and popping can be done viapush(_:)
andpopContext()
.I chose not to reuse the thread-local stack for device placement in the compiler runtime code base because it is engineered differently and is tied to the TensorFlow backend.
User-visible changes
Remove the
context
argument. Changeapplied(to:in:)
toapplied(to:)
in theLayer
protocol and all layers.Change
Context
from aclass
to astruct
, since access to thread-local contexts is already by reference and involves no copies.Add thread-safe computed property
.local
toContext
for retrieving the current context.For example, here's how you can check the current learning phase as part of your layer.
There is a default
Context
, wherelearningPhase == .inference
. To train a model, instead of creating aContext
and passing it toapplied(to:in:)
, users can now set the local learning phase and callapplied(to:)
directly.Add APIs
withContext(_:_:)
, which calls a closure under a temporary context, andwithLearningPhase(_:_:)
, which calls a closure under a temporary learning phase.For example,
Layer.inferring(from:)
is now implemented asFuture PRs will migrate all existing models and tutorials to the new API.