Skip to content

Commit

Permalink
history in the making
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed May 17, 2024
1 parent 161ceb1 commit 09af24d
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 33 deletions.
7 changes: 6 additions & 1 deletion commune/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,18 @@ def determine_type(cls, x):

@classmethod
def history_module(cls, path='history'):
return c.m('history')(cls.resolve_path(path))
return c.m('history')(folder_path=cls.resolve_path(path))

@classmethod
def history(cls,**kwargs):
history = cls.history_module().history(**kwargs)
return history

@classmethod
def rm_history(cls,*args, **kwargs):
history = cls.history_module().rm_history(*args, **kwargs)
return history


@classmethod
def history_paths(cls, **kwargs):
Expand Down
6 changes: 6 additions & 0 deletions commune/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def history(self, search=None, n=100, reverse=True, idx=None):
return history[idx]
return history

def rm_history(self, search=None, n=100, reverse=True):
history_paths = self.history_paths(n=n, reverse=reverse, search=search)
for path in history_paths:
c.rm(path)
return history_paths

def last_n(self, n=1):
return self.history(n=n)

Expand Down
2 changes: 1 addition & 1 deletion commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def load_keys(cls, path='saved_keys.json', refresh=False, **kwargs):
@classmethod
def mems(cls, search=None):
mems = {}
for key in cls.keys():
for key in cls.keys(search):
try:
mems[key] = cls.getmem(key)
except Exception as e:
Expand Down
3 changes: 3 additions & 0 deletions commune/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3289,6 +3289,9 @@ def module(cls,module: Any = 'module' , **kwargs):
'''
Wraps a python class as a module
'''
shortcuts = c.shortcuts()
if module in shortcuts:
module = shortcuts[module]
module_class = c.get_module(module,**kwargs)
return module_class
m = mod = module
Expand Down
1 change: 1 addition & 0 deletions commune/remote/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ def ssh(self):

host_map = self.host_map
cols = st.columns([5,1])

cmd = cols[0].text_input('Command', 'ls')
[cols[1].write('') for i in range(2)]
if 'x' not in fn_code:
Expand Down
4 changes: 1 addition & 3 deletions commune/remote/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ def print_output():
yield line

# if there is an stderr, print it
cnt = 0

for line in stderr.readlines():
if cnt == 0:
yield '---- ERROR ----'
if verbose:
c.print(f'[bold]{host["name"]}[/bold]', line.strip('\n'))
yield line
Expand Down
44 changes: 26 additions & 18 deletions commune/subspace/subspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2517,28 +2517,19 @@ def register(
if isinstance(netuid, str):
subnet = netuid
netuid = subnet2netuid.get(netuid, 0)
c.print(f"Registering {name} with {stake} stake on {subnet} subnet")
if netuid not in netuid2subnet:

if netuid in netuid2subnet:
subnet = netuid2subnet[netuid]
else:
netuid = 0
response = input(f"Do you want to create a new subnet ({subnet}) (yes or y or dope): ")
if response.lower() not in ["yes", 'y', 'dope']:
return {'success': False, 'msg': 'Subnet not found and not created'}

# require prompt to create new subnet

stake = stake or 0
min_register_stake = self.min_register_stake(netuid=netuid, network=network)
stake = max(min_register_stake, stake)

if c.key_exists(name):
mkey = c.get_key(name)
mkey_balance = self.get_balance(key=mkey.ss58_address, network=network)
if mkey_balance > stake:
c.print(f'Using {name} key to register {name} with {stake} stake')
key = mkey

stake = stake * 1e9

if '0.0.0.0' in address:
Expand Down Expand Up @@ -3314,8 +3305,12 @@ def set_weights(
if modules == None:
modules = c.shuffle(self.uids(netuid=netuid, update=update))
# checking if the "uids" are passed as names -> strings
key2name, name2uid = None, None
for i, module in enumerate(modules):
if isinstance(module, str):
if key2name == None:
key2name = self.key2name(netuid=netuid, update=update)
name2uid = self.name2uid(netuid=netuid, update=update)
if module in key2name:
modules[i] = key2name[module]
elif module in name2uid:
Expand All @@ -3325,7 +3320,6 @@ def set_weights(

if weights is None:
weights = [1 for _ in uids]
max_weight = max(weights)
if len(uids) < subnet_params['min_allowed_weights']:
n = self.n(netuid=netuid)
while len(uids) < subnet_params['min_allowed_weights']:
Expand All @@ -3335,13 +3329,24 @@ def set_weights(
weights.append(min_value)

uid2weight = dict(sorted(zip(uids, weights), key=lambda item: item[1], reverse=True))

self_uid = self.key2uid(netuid=netuid).get(key.ss58_address, None)
uid2weight.pop(self_uid, None)

uids = list(uid2weight.keys())
weights = list(uid2weight.values())


if len(uids) > subnet_params['max_allowed_weights']:
uids = uids[:subnet_params['max_allowed_weights']]
weights = weights[:subnet_params['max_allowed_weights']]


c.print(f'Voting for {len(uids)} modules')
assert len(uids) == len(weights), f"Length of uids {len(uids)} must be equal to length of weights {len(weights)}"
uids = torch.tensor(uids)[:n]
weights = torch.tensor(weights)[:n]
uids = torch.tensor(uids)
weights = torch.tensor(weights)
weights = weights / weights.sum() # normalize the weights between 0 and 1

# STEP 2: CLAMP THE WEIGHTS BETWEEN 0 AND 1 WITH MIN AND MAX VALUES
assert min_value >= 0 and max_value <= 1, f"min_value and max_value must be between 0 and 1"
weights = torch.clamp(weights, min_value, max_value) # min_value and max_value are between 0 and 1
Expand All @@ -3350,6 +3355,8 @@ def set_weights(
weights = list(map(lambda x : int(min(x, U16_MAX)), weights.tolist()))
uids = list(map(int, uids.tolist()))



params = {'uids': uids,
'weights': weights,
'netuid': netuid}
Expand Down Expand Up @@ -3409,6 +3416,7 @@ def register_servers(self,
c.print(f"Registering {info['name']} with module_key {info['ss58_address']} using launcher {launcher_key}")
f = c.submit(c.register, kwargs=dict(name=info['name'],
address= info['address'],
netuid = netuid,
module_key=info['ss58_address'],
key=launcher_key), timeout=timeout)
futures+= [f]
Expand Down
2 changes: 1 addition & 1 deletion commune/vali/vali.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def epoch(self, batch_size = None, network=None, **kwargs):
module_addresses = c.shuffle(list(self.namespace.values()))
c.print(f'Epoch {self.epochs} with {len(module_addresses)} modules', color='yellow')
batch_size = min(self.config.batch_size, len(module_addresses))
self.executor = c.module('executor.thread')(max_workers=batch_size)
self.executor = c.module('executor.thread')(max_workers=min(1, batch_size))
batch_size = self.config.batch_size

for module_address in module_addresses:
Expand Down
2 changes: 1 addition & 1 deletion commune/vali/vali.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

# network
network: subspace # the network to connect to (bittensor, commune, local) to specify subnet, (subspace.0 or subspace.text)
netuid: 0 # (OPTIONAL) if you have a voting network with subnets [bittensor, commune]
netuid: null # (OPTIONAL) if you have a voting network with subnets [bittensor, commune]
sync_interval: 60
sample_interval: 0.005 # the sleep interval between sending samples
search: null # the search for modules in the namespace
Expand Down
16 changes: 9 additions & 7 deletions commune/watchdog/watchdog.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import commune as c

class Watchdog(c.Module):
def __init__(self):
def __init__(self, modules=['module', 'subspace']):
self.modules = {m: c.module(m) for m in modules}
self.module = c.module('watchdog')
c.thread(self.run_loop)

def sync(self):
self.modules = {m: c.module(m) for m in self.modules}
c.print('syncing...')
c.ip(update=1)
c.tree(update=1)
c.namespace(update=1)
c.print('synced')
self.subspace = c.module('subspace')
self.subspace = c.get_module('subspace')
self.subspace.stake_from(netuid='all')



def run_loop(self, sleep_time=30):
def run_loop(self, sleep_time=30):
while True:
try:
self.sync()
except Exception as e:
print(e)
pass
c.sleep(sleep_time)
print(f'sleeping for {sleep_time} seconds')
e = c.detailed_error(e)
c.print(f'Error syncing, restarting {e}')

10 changes: 10 additions & 0 deletions modules/comchat/comchat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import commune as c


class Comchat(c.Module):
def __init__(self, model='model.openrouter' , **kwargs):
self.model = c.module(model)(**kwargs)
def generate(self, text, **kwargs):
return self.model.generate(text, **kwargs)
def test(self):
return self.generate('hello world')
27 changes: 26 additions & 1 deletion sandbox.py
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
print('fam')
import commune as c


def register_servers(netuid=6, buffer = 100, timeout = 60):
subspace = c.module('subspace')()
stake = subspace.min_register_stake(netuid=netuid) + buffer
key2balance = {k:v for k,v in c.key2balance().items() if v > stake}
keys = list(key2balance.keys())
c.print(f'Keys for registration: {keys}')
servers = c.servers()
c.print(f'Servers: {servers}')
futures = []
for i, s in enumerate(c.servers()):
c.print(f'Registering {s}')
key = keys[i % len(keys)]
future = c.submit(c.register, kwargs=dict(name=s, netuid=netuid, stake=stake, key=key ), timeout=timeout)
futures.append(future)

for f in c.as_completed(futures, timeout=timeout):
c.print(f.result())


while True:
register_servers()
# for s in c.servers():
# c.register(s, netuid=)
10 changes: 10 additions & 0 deletions subnet/comchat/comchat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import commune as c


class Comchat(c.Module):
def __init__(self, model='model.openrouter' , **kwargs):
self.model = c.module(model)(**kwargs)
def generate(self, text, **kwargs):
return self.model.generate(text, **kwargs)
def test(self):
return self.generate('hello world')

0 comments on commit 09af24d

Please sign in to comment.