-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logical axis global context support for NNX
- Loading branch information
Showing
6 changed files
with
110 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import contextlib | ||
import dataclasses | ||
import threading | ||
|
||
from flax.typing import ( | ||
LogicalRules, | ||
Sharding, | ||
) | ||
|
||
# Dynamic Axis Mapping Context | ||
# ------------------------------------------------------------------------------ | ||
|
||
|
||
@dataclasses.dataclass | ||
class _AxisRules(threading.local): | ||
"""Dynamic logical axis to mesh axis binding context.""" | ||
|
||
rules: LogicalRules = () | ||
|
||
|
||
# Global axis binding context. | ||
_axis_rules = _AxisRules() | ||
|
||
|
||
def set_logical_axis_rules(rules: LogicalRules): | ||
"""Sets the global logical axis to mesh axis binding.""" | ||
_axis_rules.rules = rules | ||
|
||
|
||
def get_logical_axis_rules() -> LogicalRules: | ||
"""Returns the global logical axis to mesh axis binding.""" | ||
return _axis_rules.rules | ||
|
||
|
||
@contextlib.contextmanager | ||
def logical_axis_rules(rules: LogicalRules): | ||
"""Context manager for setting the logical to mesh axis bindings.""" | ||
old_rules = _axis_rules.rules | ||
try: | ||
_axis_rules.rules = rules | ||
yield | ||
finally: | ||
_axis_rules.rules = old_rules | ||
|
||
|
||
def composite_rules(rule1, rule2): | ||
if not rule1 and not rule2: return () | ||
rules = {alias: value for alias, value in rule1} | ||
for alias, value in rule2: | ||
if alias in rules and rules[alias] != value: | ||
raise ValueError(f'Inconsistent logical axis annotations for {alias}: ' | ||
f'{rules[alias]} vs {value}') | ||
rules[alias] = value | ||
return tuple(rules.items()) | ||
|
||
def from_sharding_rules(sharding: Sharding, | ||
sharding_rules: LogicalRules) -> Sharding: | ||
rules = {alias: on_mesh for (alias, on_mesh) in sharding_rules} | ||
return (rules[s] if s in rules else None for s in sharding) |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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