-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4350 from IvyZX:logical-axis
PiperOrigin-RevId: 694704983
- Loading branch information
Showing
9 changed files
with
227 additions
and
148 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,80 @@ | ||
# Copyright 2024 The Flax Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
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 tuple( | ||
rules[str(s)] if (s and str(s) in rules) else s 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
Oops, something went wrong.