mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-07 03:52:01 +00:00
[fx/meta/rpc] move _meta_registration.py to fx folder / register fx functions with compatibility checks / remove color debug (#1710)
* [fx] move meta registration * [fx] fix tests. * [fx] fix test. * [fx] fix. * [meta] refactor meta registration.py. * [fx] add compatibility descriptions. * [fx] polish import. * [fx] add a decorator. * [fx] fix tests. * [fx] remove print. * [fx] edit raise error. * [fx] edit raise error. * [fx] add type hint. * [fx] fix import in experimental. * [rpc] remove color debug. * [meta] fix naming.
This commit is contained in:
46
colossalai/fx/_compatibility.py
Normal file
46
colossalai/fx/_compatibility.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from typing import Callable
|
||||
|
||||
import torch
|
||||
|
||||
try:
|
||||
from . import _meta_registrations
|
||||
META_COMPATIBILITY = True
|
||||
except:
|
||||
META_COMPATIBILITY = False
|
||||
|
||||
|
||||
def compatibility(is_backward_compatible: bool = False) -> Callable:
|
||||
"""A decorator to make a function compatible with different versions of PyTorch.
|
||||
|
||||
Args:
|
||||
is_backward_compatible (bool, optional): Whether the function is backward compatible. Defaults to False.
|
||||
|
||||
Returns:
|
||||
Callable: The decorated function
|
||||
"""
|
||||
|
||||
def decorator(func):
|
||||
if META_COMPATIBILITY:
|
||||
return func
|
||||
else:
|
||||
if is_backward_compatible:
|
||||
return func
|
||||
else:
|
||||
|
||||
def wrapper(*args, **kwargs):
|
||||
raise RuntimeError(f'Function `{func.__name__}` is not compatible with PyTorch {torch.__version__}')
|
||||
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
def is_compatible_with_meta() -> bool:
|
||||
"""Check the meta compatibility. Normally it should be called before importing some of the `colossalai.fx`
|
||||
modules. If the meta compatibility is not satisfied, the `colossalai.fx` modules will be replaced by its
|
||||
experimental counterparts.
|
||||
|
||||
Returns:
|
||||
bool: The meta compatibility
|
||||
"""
|
||||
return META_COMPATIBILITY
|
Reference in New Issue
Block a user