def __init__(self, jsonFile): with open(jsonFile) as f: data = json.load(f) self.parameters = {"name": {"output": data['comm']['ports']['output'], "input": data['comm']['ports']['input']}, "mac_table": data['comm']['mac_table'], "rank": data['comm']['src_rank'], "config_hardware": data['config_hardware'], "neural_net" : data['neural_net'], "mode" : data['mode'] } GalapagosNet.__init__(self, self.parameters) for rank_id, rank in enumerate(self.parameters['neural_net']):
def __init__(self, jsonFile): with open(jsonFile) as f: data = json.load(f) self.parameters = { "name": { "output": data['comm']['ports']['output'], "input": data['comm']['ports']['input'] }, "mac_table": data['comm']['mac_table'], "rank": data['comm']['src_rank'], "config_hardware": data['config_hardware'], "neural_net": data['neural_net'], "mode": data['mode'] } GalapagosNet.__init__(self, self.parameters) for rank_id, rank in enumerate(self.parameters['neural_net']): self.parameters['neural_net'][rank_id]['rank_info'][ 'batch_size'] = 1 self.parameters['neural_net'][rank_id]['rank_info'][ 'num_ranks'] = 1 self.parameters['neural_net'][rank_id]['rank_info'][ 'ifm_baseaddr'] = int( self.parameters['neural_net'][rank_id]['rank_info'] ['ifm_baseaddr'], 16) self.parameters['neural_net'][rank_id]['rank_info'][ 'ofm_baseaddr'] = int( self.parameters['neural_net'][rank_id]['rank_info'] ['ofm_baseaddr'], 16) self.parameters['neural_net'][rank_id]['rank_info'][ 'biases_weights_baseaddr'] = int( self.parameters['neural_net'][rank_id]['rank_info'] ['biases_weights_baseaddr'], 16) for cmd_id, cmd in enumerate( self.parameters['neural_net'][rank_id]['command']): num_weights = 0 weights = bytearray(0) with open(rank['command'][cmd_id]['weight_file'], 'r') as fobj: for line in fobj: for num in line.split(): weights = bytearray(struct.pack( '>h', int(num))) + weights num_weights = num_weights + 1 #weights = [[struct.pack('>I',int(num)) for num in line.split()] for line in fobj] #print binascii.hexlify(weights) #self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"weights": np.array(weights, dtype="int16")}) self.parameters['neural_net'][rank_id]['command'][cmd_id][ "weights"] = weights print "num of weights " + str(num_weights) if self.parameters['neural_net'][rank_id]['command'][cmd_id][ 'bias'] == 1: biases = bytearray(0) with open( self.parameters['neural_net'][rank_id]['command'] [cmd_id]['bias_file'], 'r') as fobj: for line in fobj: for num in line.split(): biases = bytearray(struct.pack( '>h', int(num))) + biases self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({"biases": biases}) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "configParameters": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['biases'] + self.parameters['neural_net'][rank_id]['weights'] }) else: self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "configParameters": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['weights'] }) print "length of weights2 " + str( len(self.parameters['neural_net'][rank_id]['command'] [cmd_id]['weights'])) print "length of configparam " + str( len(self.parameters['neural_net'][rank_id]['command'] [cmd_id]['configParameters'])) # if(cmd_id == (0)): # #self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"biases": np.array(biases, dtype="int16").byteswap()}) # self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"biases": biases}) # #self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"configParameters" :self.parameters['neural_net'][rank_id]['command'][cmd_id]['biases'].tobytes() + self.parameters['neural_net'][rank_id]['weights'].tobytes()}) # self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"configParameters" :self.parameters['neural_net'][rank_id]['command'][cmd_id]['biases'] + self.parameters['neural_net'][rank_id]['weights']}) # # # else: # self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"configParameters" : self.parameters['neural_net'][rank_id]['command'][cmd_id]['weights']}) ##A self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"weights_baseaddr": self.parameters['neural_net'][rank_id]['command'][cmd_id]["biases_weights_baseaddr"]}) # if (cmd_id == (0)): if self.parameters['neural_net'][rank_id]['command'][ cmd_id]['bias'] == 1: self.parameters['neural_net'][rank_id]['command'][ cmd_id][ 'biases_weights_baseaddr'] = self.parameters[ 'neural_net'][rank_id]['rank_info'][ 'biases_weights_baseaddr'] self.parameters['neural_net'][rank_id]['command'][ cmd_id]['weights_baseaddr'] = self.parameters[ 'neural_net'][rank_id]['rank_info'][ 'biases_weights_baseaddr'] + len( self.parameters['neural_net'][rank_id] ['command'][cmd_id]['biases']) else: self.parameters['neural_net'][rank_id]['command'][ cmd_id][ 'biases_weights_baseaddr'] = self.parameters[ 'neural_net'][rank_id]['rank_info'][ 'biases_weights_baseaddr'] self.parameters['neural_net'][rank_id]['command'][ cmd_id]['weights_baseaddr'] = self.parameters[ 'neural_net'][rank_id]['rank_info'][ 'biases_weights_baseaddr'] if (cmd_id != (len(self.parameters['neural_net'][rank_id]['command']) - 1)): if self.parameters['neural_net'][rank_id]['command'][ cmd_id]['bias'] == 1: self.parameters['neural_net'][rank_id]['command'][ cmd_id + 1]['biases_weights_baseaddr'] = self.parameters[ 'neural_net'][rank_id]['command'][cmd_id][ 'biases_weights_baseaddr'] + len( self.parameters['neural_net'][rank_id] ['command'][cmd_id] ['configParameters']) self.parameters['neural_net'][rank_id]['command'][ cmd_id + 1]['weights_baseaddr'] = self.parameters[ 'neural_net'][rank_id]['command'][ cmd_id + 1]['biases_weights_baseaddr'] + len( self.parameters['neural_net'][rank_id] ['command'][cmd_id + 1]['biases']) else: self.parameters['neural_net'][rank_id]['command'][ cmd_id + 1]['biases_weights_baseaddr'] = self.parameters[ 'neural_net'][rank_id]['command'][cmd_id][ 'biases_weights_baseaddr'] + len( self.parameters['neural_net'][rank_id] ['command'][cmd_id] ['configParameters']) self.parameters['neural_net'][rank_id]['command'][ cmd_id + 1]['weights_baseaddr'] = self.parameters[ 'neural_net'][rank_id]['command'][ cmd_id + 1]['biases_weights_baseaddr'] #print self.parameters['neural_net'][rank_id]['command'][cmd_id]["biases_weights_baseaddr"] self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "ofm_height": math.ceil((self.parameters['neural_net'][rank_id] ['command'][cmd_id]['ifm_height'] + 2 * self.parameters['neural_net'][rank_id] ['command'][cmd_id]['pad'] - self.parameters['neural_net'][rank_id] ['command'][cmd_id]['kernel_height']) / self.parameters['neural_net'][rank_id] ['command'][cmd_id]['stride'] + 1) }) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "ofm_width": math.ceil((self.parameters['neural_net'][rank_id] ['command'][cmd_id]['ifm_width'] + 2 * self.parameters['neural_net'][rank_id] ['command'][cmd_id]['pad'] - self.parameters['neural_net'][rank_id] ['command'][cmd_id]['kernel_width']) / self.parameters['neural_net'][rank_id] ['command'][cmd_id]['stride'] + 1) }) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "ifm_slices": math.ceil( self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ifm_depth'] / self.parameters['config_hardware']['C_NUM_OF_ROWS'] ) }) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "ofm_slices": math.ceil( self.parameters['neural_net'][rank_id]['command'] [cmd_id]['output_channels'] / self.parameters['config_hardware']['C_NUM_OF_ROWS'] ) }) print "pool kernel height " + str( self.parameters['neural_net'][rank_id]['command'][cmd_id] ['pool_kernel_height']) print "pool kernel width " + str( self.parameters['neural_net'][rank_id]['command'][cmd_id] ['pool_kernel_width']) print "pool stride " + str( self.parameters['neural_net'][rank_id]['command'][cmd_id] ['pool_stride']) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "ifm_packet_length": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ifm_width'] * self.parameters['neural_net'] [rank_id]['command'][cmd_id]['ifm_height'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ifm_slices'] }) print "ifm slices " + str( self.parameters['neural_net'][rank_id]['command'][cmd_id] ['ifm_slices']) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "ifm_depth_offset": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ifm_width'] * self.parameters['neural_net'] [rank_id]['command'][cmd_id]['ifm_height'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ifm_depth'] }) if (cmd_id == 0): self.parameters['neural_net'][rank_id]['command'][0][ 'ifm_baseaddr'] = self.parameters['neural_net'][ rank_id]['rank_info']['ifm_baseaddr'] else: self.parameters['neural_net'][rank_id]['command'][cmd_id][ 'ifm_baseaddr'] = self.parameters['neural_net'][ rank_id]['command'][cmd_id - 1]['ofm_baseaddr'] #self.parameters['neural_net'][rank_id]['command'][cmd_id]['ofm_baseaddr'] = int(self.parameters['neural_net'][rank_id]['command'][cmd_id]['ifm_baseaddr'],16) + self.parameters['neural_net'][rank_id]['command'][cmd_id]['ifm_packet_length']*2 self.parameters['neural_net'][rank_id]['command'][cmd_id][ 'ofm_baseaddr'] = self.parameters['neural_net'][rank_id][ 'command'][cmd_id]['ifm_baseaddr'] + self.parameters[ 'neural_net'][rank_id]['command'][cmd_id][ 'ifm_height'] * self.parameters['neural_net'][ rank_id]['command'][cmd_id][ 'ifm_width'] * self.parameters[ 'neural_net'][rank_id]['command'][ cmd_id]['ifm_depth'] * 2 if (cmd_id == len( self.parameters['neural_net'][rank_id]['command']) - 1): self.parameters['neural_net'][rank_id]['rank_info'][ 'ofm_baseaddr'] = self.parameters['neural_net'][ rank_id]['command'][cmd_id]['ofm_baseaddr'] self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "weights_packet_length": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['kernel_height'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['kernel_width'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ifm_depth'] }) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "weight_depth_offset": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['kernel_height'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['kernel_width'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ifm_depth'] * self.parameters['config_hardware']['C_NUM_OF_COLS'] }) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "pool_input_height": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ofm_height'] }) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "pool_input_width": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ofm_width'] }) print "pool input height " + str( self.parameters['neural_net'][rank_id]['command'][cmd_id] ['pool_input_height']) print "pool input width " + str( self.parameters['neural_net'][rank_id]['command'][cmd_id] ['pool_input_width']) #self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"pool_input_height" : self.parameters['neural_net'][rank_id]['command'][cmd_id]['ofm_height']}) #self.parameters['neural_net'][rank_id]['command'][cmd_id].update({"pool_input_width" : self.parameters['neural_net'][rank_id]['command'][cmd_id]['ofm_width']}) try: self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "pool_output_height": math.ceil( ((self.parameters['neural_net'][rank_id] ['command'][cmd_id]['pool_input_height'] - self.parameters['neural_net'][rank_id] ['command'][cmd_id]['pool_kernel_height']) / 1.0) / self.parameters['neural_net'][rank_id] ['command'][cmd_id]['pool_stride'] / 1.0 + 1) }) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "pool_output_width": math.ceil( ((self.parameters['neural_net'][rank_id] ['command'][cmd_id]['pool_input_width'] - self.parameters['neural_net'][rank_id] ['command'][cmd_id]['pool_kernel_width']) / 1.0) / self.parameters['neural_net'][rank_id] ['command'][cmd_id]['pool_stride'] / 1.0 + 1) }) except ZeroDivisionError: self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({"pool_output_height": 0}) self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({"pool_output_width": 0}) # pool_stride has to be a multiple of 2 if (self.parameters['neural_net'][rank_id]['command'][cmd_id]['pool_stride'] != 0 \ and self.parameters['neural_net'][rank_id]['command'][cmd_id]['pool_output_height'] > 5 and self.parameters['neural_net'][rank_id]['command'][cmd_id]['pool_output_width'] > 5 \ and self.parameters['neural_net'][rank_id]['command'][cmd_id]['pool_output_width'] * self.parameters['neural_net'][rank_id]['command'][cmd_id]['pool_kernel_width'] < 1 << 9 \ and self.parameters['neural_net'][rank_id]['command'][cmd_id]['pool_output_width'] < 1 << 8): # WHEN MAXPOOL IS ENABLED, THE OUTPUT SIZE WILL BE SMALLER # THERFORE, THE OFM_PACKET_LENGTH HAS TO BE ADJUSTED ACCORDINGLY self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "ofm_packet_length": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['pool_output_height'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['pool_output_width'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ofm_slices'] }) else: self.parameters['neural_net'][rank_id]['command'][cmd_id][ 'pool_output_height'] = 0 self.parameters['neural_net'][rank_id]['command'][cmd_id][ 'pool_output_width'] = 0 self.parameters['neural_net'][rank_id]['command'][cmd_id][ 'pool_kernel_height'] = 0 self.parameters['neural_net'][rank_id]['command'][cmd_id][ 'pool_kernel_width'] = 0 self.parameters['neural_net'][rank_id]['command'][cmd_id][ 'pool_stride'] = 0 self.parameters['neural_net'][rank_id]['command'][ cmd_id].update({ "ofm_packet_length": self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ofm_height'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ofm_width'] * self.parameters['neural_net'][rank_id]['command'] [cmd_id]['ofm_slices'] }) self.configParametersFPGA(rank_id) self.configDMAFPGA(rank_id) self.configCmdFPGA(rank_id)