class LegionSync(SimpleExtension): # make sure to specify the argument "every_n_batches=T" when you instantiate this extension, # or something to that effect to determine how often we want to call it def __init__(self, params, alpha, beta, debug=False, verbose=False, **kwargs): super(LegionSync, self).__init__(**kwargs) self.client = Client() self.debug = debug self.params = params self.alpha = alpha self.beta = beta self.verbose = verbose self.initialize_values(self.client, params, verbose) self.counter = 0 def do(self, which_callback, *args): for name, val in self.params.iteritems(): self.client.push_full(name, val.get_value(), self.alpha, self.beta) val.set_value(self.client.pull_full(name)) @staticmethod def initialize_values(client, params, verbose): for name, val in params.iteritems(): val.set_value(client.create_if_not_already_created(name, val.get_value()))
def __init__(self, params, alpha, beta, debug=False, verbose=False, **kwargs): super(LegionSync, self).__init__(**kwargs) self.client = Client() self.debug = debug self.params = params self.alpha = alpha self.beta = beta self.verbose = verbose self.initialize_values(self.client, params, verbose) self.counter = 0
def run_legion(N): from legion import Client client = Client() alpha = 0.5 beta = 0.5 big_matrix = generate_big_matrix(N) current_inv_approx = np.eye(N) client.create_if_not_already_created("current_inv_approx", current_inv_approx) current_inv_approx = client.pull_full("current_inv_approx") #print "type(current_inv_approx)" #print type(current_inv_approx) current_error = get_error(current_inv_approx, big_matrix) original_noise = 0.001 current_noise = original_noise last_index_with_good_proposal = 0 for i in range(1000): (status, current_inv_approx, current_error) = try_one_guess(current_inv_approx, big_matrix, current_noise, current_error) #print "update %s, current_error %0.4f" % (status, current_error) time.sleep(1) if status == 'success': client.push_full("current_inv_approx", current_inv_approx, alpha, beta) print "Pushed current_inv_approx with error %0.12f to server." % current_error current_inv_approx = client.pull_full("current_inv_approx") last_index_with_good_proposal = i current_noise = original_noise else: current_noise = 0.9 * current_noise if last_index_with_good_proposal == i or 25 < last_index_with_good_proposal - i: current_inv_approx = client.pull_full("current_inv_approx") current_error = get_error(current_inv_approx, big_matrix) print "Pulled current_inv_approx with error %0.12f from server." % current_error """
def main(): leg = Client() test = -np.ones((10, 20, 30)) test[2:5, 2:5, 2:5] = np.zeros((3, 3, 3)) # We create the value on the server. If we try to create it from different clients at once, # it will only take the first value it receives, and send it back to the other clients so they know which one was picked. test = leg.create_if_not_already_created("test", test) test *= 2 comp_test0 = leg.pull_full("test") leg.push_as_part("test", test, [[0], [1, 4], [1, 6]], 0.5, 0.5) comp_test1 = leg.pull_full("test") print comp_test0 print comp_test1
def __init__(self, params, alpha, beta, client=None, debug=False, verbose=False, want_sync_timing_log=False, **kwargs): super(SharedParamsAutoSync, self).__init__(**kwargs) assert type(params) == dict if client is None: self.client = Client() self.debug = debug self.params = params self.alpha = alpha self.beta = beta self.verbose = verbose self.initialize_values(self.client, params, verbose) # This creates some "pollution" in the quantities reported, # but it's useful if you want to get a better understanding # of how much time was spent synching. self.want_sync_timing_log = want_sync_timing_log
class SharedParamsAutoSync(SimpleExtension): # Make sure to specify the argument "every_n_batches=T" when you instantiate this extension, # or something to that effect to determine how often we want to call it. # # If the parameters are already set up on the server, # they will get read (and not reinitialized). # If the parameters are not configured on the server, # they will be created and take on the same values # as when this extension is called for the first time. def __init__(self, params, alpha, beta, client=None, debug=False, verbose=False, want_sync_timing_log=False, **kwargs): super(SharedParamsAutoSync, self).__init__(**kwargs) assert type(params) == dict if client is None: self.client = Client() self.debug = debug self.params = params self.alpha = alpha self.beta = beta self.verbose = verbose self.initialize_values(self.client, params, verbose) # This creates some "pollution" in the quantities reported, # but it's useful if you want to get a better understanding # of how much time was spent synching. self.want_sync_timing_log = want_sync_timing_log def do(self, which_callback, *args): if self.should_we_sync_now(): self.presync_callback() # perform the actual work here for name, val in self.params.iteritems(): self.client.push_full(name, val.get_value(), self.alpha, self.beta) val.set_value(self.client.pull_full(name)) # done self.postsync_callback() @staticmethod def initialize_values(client, params, verbose): for name, val in params.iteritems(): val.set_value(client.create_if_not_already_created(name, val.get_value())) # These next functions are to support a bit of polymorphism # for other subclasses that will instrument this class a bit more. def presync_callback(self): self.sync_start_timestamp = time.time() def postsync_callback(self): self.sync_end_timestamp = time.time() if self.want_sync_timing_log: # write down how long the sync took current_row = self.main_loop.log.current_row current_row['param_sync_start'] = self.sync_start_timestamp current_row['param_sync_end'] = self.sync_end_timestamp current_row['param_sync_duration'] = time_diff def should_we_sync_now(self): return True