Skip to content

Commit

Permalink
fam
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Sep 7, 2024
1 parent 4a21591 commit 63975a5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 123 deletions.
53 changes: 52 additions & 1 deletion commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -2780,7 +2780,7 @@ def simple2path(cls,
"""
# if cls.libname in simple and '/' not in simple and cls.can_import_module(simple):
# return simple
shortcuts = cls.shortcuts()
shortcuts = c.shortcuts()
simple = shortcuts.get(simple, simple)

if simple.endswith(extension):
Expand Down Expand Up @@ -3134,6 +3134,7 @@ def import_object(cls, key:str, verbose: bool = 0, trials=3)-> Any:
Import an object from a string with the format of {module_path}.{object}
Examples: import_object("torch.nn"): imports nn from torch
'''
assert key != None, key
module = '.'.join(key.split('.')[:-1])
object_name = key.split('.')[-1]
if verbose:
Expand Down Expand Up @@ -4111,6 +4112,56 @@ def explain_myself(self):



@classmethod
def remote_fn(cls,
fn: str='train',
module: str = None,
args : list = None,
kwargs : dict = None,
name : str =None,
refresh : bool =True,
interpreter = 'python3',
autorestart : bool = True,
force : bool = False,
cwd = None,
**extra_launch_kwargs
):

kwargs = c.locals2kwargs(kwargs)
kwargs = kwargs if kwargs else {}
args = args if args else []
if 'remote' in kwargs:
kwargs['remote'] = False
assert fn != None, 'fn must be specified for pm2 launch'
kwargs = {
'module': module,
'fn': fn,
'args': args,
'kwargs': kwargs
}
name = name or module
if refresh:
c.pm2_kill(name)

module = c.module(module)
kwargs_str = json.dumps(kwargs).replace('"', "'")

# build command to run pm2
filepath = module.filepath()
cwd = os.path.dirname(filepath)
root_filepath = c.module('module').filepath()
command = f"pm2 start {root_filepath} --name {name} --interpreter {interpreter}"
if not autorestart:
command += ' --no-autorestart'
if force:
command += ' -f '
command = command + f' -- --fn module_fn --kwargs "{kwargs_str}"'
return c.cmd(command, cwd=cwd)

def fuckkkk(self):
return "fuckkkk"


c.add_routes()
Module = c # Module is alias of c
Module.run(__name__)
Expand Down
2 changes: 0 additions & 2 deletions commune/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ pm2:
- [exists, pm2_exists]
- [servers, pm2_servers]
- [logs, pm2_logs]
- launch
- remote_fn
vali:
- run_epoch
- setup_vali
Expand Down
125 changes: 5 additions & 120 deletions commune/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def serve(cls,
if remote:
remote = False
remote_kwargs = c.locals2kwargs(locals()) # GET THE LOCAL KWARGS FOR SENDING TO THE REMOTE
for _ in ['extra_kwargs', 'address', 'response']:
for _ in ['extra_kwargs', 'address', 'response', 'namespace']:
remote_kwargs.pop(_, None) # WE INTRODUCED THE ADDRES
cls.remote_fn('serve', name=name, kwargs=remote_kwargs)
return response
Expand All @@ -231,126 +231,11 @@ def serve(cls,
key=key)
return response

@classmethod
def launch(cls,
module:str = None,
fn: str = 'serve',
name:Optional[str]=None,
tag : str = None,
args : list = None,
kwargs: dict = None,
device:str=None,
interpreter:str='python3',
autorestart: bool = True,
verbose: bool = False ,
force:bool = True,
meta_fn: str = 'module_fn',
tag_seperator:str = '::',
cwd = None,
refresh:bool=True ):
import commune as c

if hasattr(module, 'module_name'):
module = module.module_name()

# avoid these references fucking shit up
args = args if args else []
kwargs = kwargs if kwargs else {}

# convert args and kwargs to json strings
kwargs = {
'module': module ,
'fn': fn,
'args': args,
'kwargs': kwargs
}

kwargs_str = json.dumps(kwargs).replace('"', "'")

name = name or module
if refresh:
cls.pm2_kill(name)
module = c.module()
# build command to run pm2
filepath = c.filepath()
cwd = cwd or module.dirpath()
command = f"pm2 start {filepath} --name {name} --interpreter {interpreter}"

if not autorestart:
command += ' --no-autorestart'
if force:
command += ' -f '
command = command + f' -- --fn {meta_fn} --kwargs "{kwargs_str}"'
env = {}
if device != None:
if isinstance(device, int):
env['CUDA_VISIBLE_DEVICES']=str(device)
if isinstance(device, list):
env['CUDA_VISIBLE_DEVICES']=','.join(list(map(str, device)))
if refresh:
cls.pm2_kill(name)

cwd = cwd or module.dirpath()

stdout = c.cmd(command, env=env, verbose=verbose, cwd=cwd)
return {'success':True, 'message':f'Launched {module}', 'command': command, 'stdout':stdout}

@classmethod
def remote_fn(cls,
fn: str='train',
module: str = None,
args : list = None,
kwargs : dict = None,
name : str =None,
tag: str = None,
refresh : bool =True,
mode = 'pm2',
tag_seperator : str = '::',
cwd = None,
**extra_launch_kwargs
):
import commune as c

kwargs = c.locals2kwargs(kwargs)
if 'remote' in kwargs:
kwargs['remote'] = False
if len(fn.split('.'))>1:
module = '.'.join(fn.split('.')[:-1])
fn = fn.split('.')[-1]

kwargs = kwargs if kwargs else {}
args = args if args else []
if 'remote' in kwargs:
kwargs['remote'] = False

cwd = cwd or cls.dirpath()
kwargs = kwargs or {}
args = args or []
module = cls.resolve_object(module)
# resolve the name
if name == None:
# if the module has a module_path function, use that as the name
if hasattr(module, 'module_path'):
name = module.module_name()
else:
name = module.__name__.lower()

c.print(f'[bold cyan]Launching --> <<[/bold cyan][bold yellow]class:{module.__name__}[/bold yellow] [bold white]name[/bold white]:{name} [bold white]fn[/bold white]:{fn} [bold white]mode[/bold white]:{mode}>>', color='green')

launch_kwargs = dict(
module=module,
fn = fn,
name=name,
tag=tag,
args = args,
kwargs = kwargs,
refresh=refresh,
**extra_launch_kwargs
)
assert fn != None, 'fn must be specified for pm2 launch'

return cls.launch(**launch_kwargs)





sync_time = 0
timescale_map = {'sec': 1, 'min': 60, 'hour': 3600, 'day': 86400, 'minute': 60, 'second': 1}

Expand Down

0 comments on commit 63975a5

Please sign in to comment.