mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-05-02 21:48:15 +00:00
* init shardformer code structure * add implement of sharder (inject and replace) * add implement of replace layer to colossal layer * separate different layer policy, add some notion * implement 1d and 2d slicer, can tell col or row * fix bug when slicing and inject model * fix some bug; add inference test example * add share weight and train example * add train * add docstring and readme * add docstring for other files * pre-commit
59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
def hasattr_(obj, attr: str):
|
|
r"""
|
|
Check whether the object has the multi sublevel attr
|
|
|
|
Args:
|
|
obj (object): The object to check
|
|
attr (str): The multi level attr to check
|
|
"""
|
|
attrs = attr.split('.')
|
|
for a in attrs:
|
|
try:
|
|
obj = getattr(obj, a)
|
|
except AttributeError:
|
|
return False
|
|
return True
|
|
|
|
|
|
def setattr_(obj, attr: str, value, ignore: bool = False):
|
|
r"""
|
|
Set the object's multi sublevel attr to value, if ignore, ignore when it doesn't exist
|
|
|
|
Args:
|
|
obj (object): The object to set
|
|
attr (str): The multi level attr to set
|
|
value (Any): The value to set
|
|
ignore (bool): Whether to ignore when the attr doesn't exist
|
|
"""
|
|
|
|
attrs = attr.split('.')
|
|
for a in attrs[:-1]:
|
|
try:
|
|
obj = getattr(obj, a)
|
|
except AttributeError:
|
|
if ignore:
|
|
return
|
|
raise AttributeError(f"Object {obj} has no attribute {attr}")
|
|
setattr(obj, attrs[-1], value)
|
|
|
|
|
|
def getattr_(obj, attr: str, ignore: bool = None):
|
|
r"""
|
|
Get the object's multi sublevel attr
|
|
|
|
Args:
|
|
obj (object): The object to set
|
|
attr (str): The multi level attr to set
|
|
ignore (bool): Whether to ignore when the attr doesn't exist
|
|
"""
|
|
|
|
attrs = attr.split('.')
|
|
for a in attrs:
|
|
try:
|
|
obj = getattr(obj, a)
|
|
except AttributeError:
|
|
if ignore:
|
|
return None
|
|
raise AttributeError(f"Object {obj} has no attribute {attr}")
|
|
return obj
|