def make_printer_module(nvmnet, layer_names): first = layer_names[0] if len(layer_names) > 0 else "" def print_callback(ID, size, ptr): layer_name = layer_names[ID] if layer_name == first: print("") syn_v = FloatArray(size, ptr).to_np_array() syn_tok = nvmnet.layers[layer_name].coder.decode(syn_v) print("%4s: %12s" % (layer_name, syn_tok)) create_io_callback("nvm_print", print_callback) return { "type": "callback", "layers": [{ "structure": "nvm", "layer": layer_name, "output": True, "function": "nvm_print", "id": i } for i, layer_name in enumerate(layer_names)] }
def make_custom_output_module(structure, layer_names, name, cb): def custom_callback(ID, size, ptr): cb(layer_names[ID], FloatArray(size, ptr).to_np_array()) create_io_callback(name, custom_callback) return { "type": "callback", "layers": [{ "structure": structure, "layer": layer_name, "output": True, "function": name, "id": i } for i, layer_name in enumerate(layer_names)] }
def make_exit_module(): def exit_callback(ID, size, ptr): if FloatArray(size, ptr)[0] > 0.0: interrupt_engine() create_io_callback("nvm_exit", exit_callback) return { "type": "callback", "layers": [{ "structure": "nvm", "layer": "exit", "output": True, "function": "nvm_exit", "id": 0 }] }
def make_checker_module(nvmnet): layer_names = list(nvmnet.layers.keys()) def checker_callback(ID, size, ptr): if ID == 0: nvmnet.tick() layer_name = layer_names[ID] coder = nvmnet.layers[layer_name].coder syn_v = FloatArray(size, ptr).to_np_array() syn_tok = coder.decode(syn_v) py_v = nvmnet.activity[layer_name] py_tok = coder.decode(py_v) if py_tok != syn_tok: residual = np.fabs(syn_v.reshape(py_v.shape) - py_v).max() print("Mismatch detected in nvm_checker!") print("%4s: %12s | %12s (res=%f)" % (layer_name, syn_tok, py_tok, residual)) interrupt_engine() create_io_callback("nvm_checker", checker_callback) return { "type": "callback", "layers": [{ "structure": "nvm", "layer": layer_name, "output": True, "function": "nvm_checker", "id": i } for i, layer_name in enumerate(layer_names)] }