Skip to content

Commit

Permalink
danger zone
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Sep 13, 2024
1 parent 6fe73d9 commit de113da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
60 changes: 15 additions & 45 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,48 +665,13 @@ def routes(cls, cache=True):
#### THE FINAL TOUCH , ROUTE ALL OF THE MODULES TO THE CURRENT MODULE BASED ON THE routes CONFIG

@classmethod
def route_fns(cls):
routes = cls.routes()
route_fns = []
for module, fns in routes.items():
for fn in fns:
if isinstance(fn, dict):
fn = fn['to']
elif isinstance(fn, list):
fn = fn[1]
elif isinstance(fn, str):
fn
else:
raise ValueError(f'Invalid route {fn}')
route_fns.append(fn)
return route_fns

@staticmethod
def resolve_to_from_fn_routes(fn):
'''
resolve the from and to function names from the routes
option 1:
{fn: 'fn_name', name: 'name_in_current_module'}
option 2:
{from: 'fn_name', to: 'name_in_current_module'}
'''

if type(fn) in [list, set, tuple] and len(fn) == 2:
# option 1: ['fn_name', 'name_in_current_module']
from_fn = fn[0]
to_fn = fn[1]
elif isinstance(fn, dict) and all([k in fn for k in ['fn', 'name']]):
if 'fn' in fn and 'name' in fn:
to_fn = fn['name']
from_fn = fn['fn']
elif 'from' in fn and 'to' in fn:
from_fn = fn['from']
to_fn = fn['to']
def resolve_routes(cls, routes:dict=None):
if hasattr(cls, 'routes'):
routes = cls.routes() if callable(cls.routes) else cls.routes
else:
from_fn = fn
to_fn = fn

return from_fn, to_fn
routes = {}
assert isinstance(routes, dict), f'routes must be a dictionary, not {type(routes)}'
return routes

@classmethod
def add_routes(cls, routes:dict=None, verbose=False):
Expand Down Expand Up @@ -746,8 +711,14 @@ def fn_generator( *args, module_ph, fn_ph, **kwargs):
if fns in ['all', '*']:
fns = c.functions(m)
for fn in fns:
# resolve the from and to function names
from_fn, to_fn = cls.resolve_to_from_fn_routes(fn)

if type(fn) in [list, set, tuple] and len(fn) == 2:
# option 1: ['fn_name', 'name_in_current_module']
from_fn = fn[0]
to_fn = fn[1]
else:
from_fn = fn
to_fn = fn
# create a partial function that is bound to the module
fn_obj = partial(fn_generator, fn_ph=from_fn, module_ph=m )
# make sure the funciton is as close to the original function as possible
Expand Down Expand Up @@ -4187,8 +4158,7 @@ def vscode(self, path = None):

def path(self):
return self.filepath()



c.add_routes()
Module = c # Module is alias of c
Module.run(__name__)
Expand Down
1 change: 1 addition & 0 deletions commune/routes.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
vali:
- run_epoch
- leaderboard
cli:
- parse_args
streamlit:
Expand Down
2 changes: 2 additions & 0 deletions scripts/install_commune.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# install commune

0 comments on commit de113da

Please sign in to comment.