MCPcopy Create free account
hub / github.com/apple/axlearn / InvocationContext

Class InvocationContext

axlearn/common/module.py:367–551  ·  view source on GitHub ↗

The invocation context for `Module.__call__()`. Attributes: name: The context name. Must be unique among sibling contexts. parent: The parent context, or None if `self` is the root context. module: The Module associated with the context. state: The state of the m

Source from the content-addressed store, hash-verified

365
366
367@typing.runtime_checkable # Needed for isinstance checks to work.
368class Summable(Protocol):
369 # Objects of the same type which adhere to this protocol may be added.
370 def __add__(self, other: T) -> T: ...
371
372
373# TODO(markblee): Link to docs on invocation contexts.
374@functools.partial(flax_struct.dataclass, frozen=False)
375# pylint: disable-next=too-many-instance-attributes
376class InvocationContext: # pytype: disable=invalid-annotation
377 """The invocation context for `Module.__call__()`.
378
379 Attributes:
380 name: The context name. Must be unique among sibling contexts.
381 parent: The parent context, or None if `self` is the root context.
382 module: The Module associated with the context.
383 state: The state of the module.
384 is_training: Whether the invocation should run in the training mode.
385 prng_key: The pseudo-random number generator key (can be None if the computation does not
386 require random numbers).
387 output_collection: See `OutputCollection`.
388 """
389
390 name: str = flax_struct.field(pytree_node=False)
391 parent: Optional["InvocationContext"] = flax_struct.field(pytree_node=True)
392 module: Optional["Module"] = flax_struct.field(pytree_node=False)
393 state: NestedTensor = flax_struct.field(pytree_node=True)
394 is_training: bool = flax_struct.field(pytree_node=False)
395 prng_key: Optional[Tensor] = flax_struct.field(pytree_node=True)
396 output_collection: OutputCollection = flax_struct.field(pytree_node=True)
397
398 def path(self):
399 if self.parent is None:
400 return self.name
401 return self.parent.path() + "." + self.name
402
403 # pylint: disable-next=too-many-branches
404 def add_child(self, name: str, **override_kwargs) -> "InvocationContext":
405 """Creates a child context with the given `name`.
406
407 Args:
408 name: The child context name. Must be unique among the siblings.
409 override_kwargs: Overrides of the child context fields.
410
411 Returns:
412 The child context. Default values for fields not specified in `override_kwargs`:
413 - `module` defaults to self.module.children[name];
414 - `state` defaults to the state corresponding to child_context.module, that is:
415 self.state[child_module.name] if child_context.module is a a child of self.module, or
416 self.state if child_context.module is self.module;
417 - `is_training` defaults to self.is_training;
418 - `prng_key` defaults to fold_in(self.prng_key, hash(name));
419 - `output_collection` defaults to self.output_collection.add_child(name).
420
421 Raises:
422 ValueError: if "parent" is specified in `override_kwargs`.
423 NotImplementedError: if a field doesn't have a default value.
424 """

Calls

no outgoing calls