def check_nbla_infer(tmpdir, x, y, batch_size, on_memory): if not command_exists('nbla'): pytest.skip('An executable `nbla` is not in path.') # A. save a created graph to nnp. contents = { 'networks': [{ 'name': 'graph', 'batch_size': 1, 'outputs': { 'y': y }, 'names': { 'x': x } }], 'executors': [{ 'name': 'runtime', 'network': 'graph', 'data': ['x'], 'output': ['y'] }] } from nnabla.utils.save import save tmpdir.ensure(dir=True) tmppath = tmpdir.join('tmp.nnp') nnp_file = tmppath.strpath save(nnp_file, contents) # B. Get result with nnp_graph from nnabla.utils import nnp_graph nnp = nnp_graph.NnpLoader(nnp_file) graph = nnp.get_network('graph', batch_size=batch_size) x2 = graph.inputs['x'] y2 = graph.outputs['y'] x2.d = np.random.randn(*x2.shape).astype(np.float32) y2.forward() # C. Get result with nbla input_bin = tmpdir.join('tmp_in.bin') input_bin_file = input_bin.strpath x2.d.tofile(input_bin_file) output_bin = tmpdir.join('tmp_out') if on_memory: check_call([ 'nbla', 'infer', '-O', '-e', 'runtime', '-b', str(batch_size), '-o', output_bin.strpath, nnp_file, input_bin_file ]) else: check_call([ 'nbla', 'infer', '-e', 'runtime', '-b', str(batch_size), '-o', output_bin.strpath, nnp_file, input_bin_file ]) # D. Compare y3 = np.fromfile(output_bin.strpath + '_0.bin', dtype=np.float32).reshape(y2.shape) assert np.allclose(y2.d, y3)
def test_networkpass_on_generate_function(module): '''This test tests whether equivalency between new or old implementation of nnp_graph ''' _, inputs = module verbose = 1 callback = nnp_graph.NnpNetworkPass(verbose) @callback.on_generate_function_by_name('Convolution') def change_convolution_param(f): print('{}'.format(f.proto.convolution_param.pad.dim[:])) f.proto.convolution_param.pad.dim[:] = [1, 1] return f @callback.on_function_pass_by_type('Affine') def change_affine_param(f, variables, param_scope): param_name = f.inputs[1].proto.name input_shape = f.inputs[0].proto.shape.dim[:] w_shape = f.inputs[1].proto.shape.dim[:] rng = np.random.RandomState(388) with nn.parameter_scope('', param_scope): W = nn.Variable.from_numpy_array( rng.randn(np.prod(input_shape[1:]), w_shape[1])) nn.parameter.set_parameter(param_name, W) W.need_grad = True with get_saved_test_model(module) as nnp_file: ref_nnp = legacy_nnp_graph.NnpLoader(nnp_file) nnp = nnp_graph.NnpLoader(nnp_file) for ref_v, v in zip(nnp_check(ref_nnp, 'left', callback), nnp_check(nnp, 'right', callback)): verify_equivalence(ref_v, v)
def test_nnp_graph_reshape(tmpdir, variable_batch_size, batch_size, shape): x = nn.Variable([10, 1, 28, 28, 10, 10]) y = F.reshape(x, shape=shape) contents = { 'networks': [ {'name': 'graph', 'batch_size': 1, 'outputs': {'y': y}, 'names': {'x': x}}]} from nnabla.utils.save import save tmppath = tmpdir.join('tmp_reshape.nnp') tmppath.ensure() nnp_file = tmppath.strpath save(nnp_file, contents, variable_batch_size=variable_batch_size) from nnabla.utils import nnp_graph nnp = nnp_graph.NnpLoader(nnp_file) graph = nnp.get_network('graph', batch_size=batch_size) x2 = graph.inputs['x'] y2 = graph.outputs['y'] if not variable_batch_size: assert x2.shape == x.shape assert y2.shape == y.shape return assert x2.shape[0] == batch_size assert y2.shape[0] == batch_size x2.d = np.random.randn(*x2.shape) shape2 = list(shape) shape2[0] = batch_size shape2[1:] = y.shape[1:] y2.forward() assert np.all(y2.d == x2.d.reshape(shape2))
def check_nnp_graph_save_load(tmpdir, x, y, batch_size, variable_batch_size): # Save contents = { 'networks': [{ 'name': 'graph', 'batch_size': 1, 'outputs': { 'y': y }, 'names': { 'x': x } }] } from nnabla.utils.save import save tmpdir.ensure(dir=True) tmppath = tmpdir.join('tmp.nnp') nnp_file = tmppath.strpath save(nnp_file, contents, variable_batch_size=variable_batch_size) # Load from nnabla.utils import nnp_graph nnp = nnp_graph.NnpLoader(nnp_file) graph = nnp.get_network('graph', batch_size=batch_size) x2 = graph.inputs['x'] y2 = graph.outputs['y'] if not variable_batch_size: assert x2.shape == x.shape assert y2.shape == y.shape return x2, y2 assert x2.shape[0] == batch_size assert y2.shape[0] == batch_size return x2, y2
def test_nnp_graph_simple_load(module): '''This test tests whether equivalency between new or old implementation of nnp_graph ''' with get_saved_test_model(module) as nnp_file: ref_nnp = legacy_nnp_graph.NnpLoader(nnp_file) nnp = nnp_graph.NnpLoader(nnp_file) for ref_v, v in zip(nnp_check(ref_nnp, 'left'), nnp_check(nnp, 'right')): verify_equivalence(ref_v, v)
def build_network_from_nnp(self, nnp_file): from nnabla.utils import nnp_graph nnp = nnp_graph.NnpLoader(nnp_file) net = nnp.get_network(self.name, batch_size=1) s = net.inputs['s'] q = net.outputs['q'] Variables = namedtuple('Variables', ['s', 'q']) assert q.shape[1] == self.num_actions self.network = Variables(s, q)
def load_model_from_nnp_graph_and_forward(nnp_file, batch_size): nnp = nnp_graph.NnpLoader(nnp_file) network = nnp.get_network(nnp.get_network_names()[0], batch_size=batch_size) inputs = list(network.inputs.values()) outputs = list(network.outputs.values()) out = [] for d in forward_variable(inputs, outputs, 'left'): out.append(d) return d
def test_nnp_graph(seed): rng = np.random.RandomState(seed) def unit(i, prefix): c1 = PF.convolution(i, 4, (3, 3), pad=(1, 1), name=prefix + '-c1') c2 = PF.convolution(F.relu(c1), 4, (3, 3), pad=(1, 1), name=prefix + '-c2') c = F.add2(c2, c1, inplace=True) return c x = nn.Variable([2, 3, 4, 4]) c1 = unit(x, 'c1') c2 = unit(x, 'c2') y = PF.affine(c2, 5, name='fc') runtime_contents = { 'networks': [{ 'name': 'graph', 'batch_size': 2, 'outputs': { 'y': y }, 'names': { 'x': x } }], } import tempfile tmpdir = tempfile.mkdtemp() import os nnp_file = os.path.join(tmpdir, 'tmp.nnp') try: from nnabla.utils.save import save save(nnp_file, runtime_contents) from nnabla.utils import nnp_graph nnp = nnp_graph.NnpLoader(nnp_file) finally: import shutil shutil.rmtree(tmpdir) graph = nnp.get_network('graph') x2 = graph.inputs['x'] y2 = graph.outputs['y'] d = rng.randn(*x.shape).astype(np.float32) x.d = d x2.d = d y.forward(clear_buffer=True) y2.forward(clear_buffer=True) from nbla_test_utils import ArrayDiffStats assert np.allclose(y.d, y2.d), str(ArrayDiffStats(y.d, y2.d))
def get_nnp(contents, tmpdir, need_file_object, file_type): import io from nnabla.utils.save import save from nnabla.utils import nnp_graph if file_type == '.nntxt' or file_type == '.prototxt': include_params = True else: include_params = False if need_file_object: nnp_object = io.BytesIO() if file_type == '.nnp' else io.StringIO() save(nnp_object, contents, extension=file_type, include_params=include_params) nnp_object.seek(0) nnp = nnp_graph.NnpLoader(nnp_object, extension=file_type) else: tmpdir.ensure(dir=True) nnp_file = tmpdir.join('tmp'+file_type).strpath save(nnp_file, contents, include_params=include_params) nnp = nnp_graph.NnpLoader(nnp_file) return nnp
def test_networkpass_remove_and_rewire(module): '''This test tests whether equivalency between new or old implementation of nnp_graph ''' _, inputs = module verbose = 1 callback = nnp_graph.NnpNetworkPass(verbose) callback.remove_and_rewire('affine1_1') callback.remove_and_rewire('c1-c1') with get_saved_test_model(module) as nnp_file: ref_nnp = legacy_nnp_graph.NnpLoader(nnp_file) nnp = nnp_graph.NnpLoader(nnp_file) for ref_v, v in zip(nnp_check(ref_nnp, 'left', callback), nnp_check(nnp, 'right', callback)): verify_equivalence(ref_v, v)
def test_networkpass_fix_parameter(module): '''This test tests whether equivalency between new or old implementation of nnp_graph ''' _, inputs = module verbose = 1 callback = nnp_graph.NnpNetworkPass(verbose) callback.fix_parameters() with get_saved_test_model(module) as nnp_file: nnp = nnp_graph.NnpLoader(nnp_file) assert_parameter_scope_empty() for network_name in sorted(nnp.get_network_names()): network = nnp.get_network( network_name, batch_size=32, callback=callback) assert_parameter_scope_empty()
def test_networkpass_use_up_to(module): '''This test tests whether equivalency between new or old implementation of nnp_graph ''' _, inputs = module verbose = 1 callback = nnp_graph.NnpNetworkPass(verbose) callback.use_up_to('Tanh_out_1') callback.use_up_to('Convolution_out_3') with get_saved_test_model(module) as nnp_file: ref_nnp = legacy_nnp_graph.NnpLoader(nnp_file) nnp = nnp_graph.NnpLoader(nnp_file) for ref_v, v in zip(nnp_check(ref_nnp, 'left', callback), nnp_check(nnp, 'right', callback)): verify_equivalence(ref_v, v)
def test_nnp_graph(seed, tmpdir): rng = np.random.RandomState(seed) def unit(i, prefix): c1 = PF.convolution(i, 4, (3, 3), pad=(1, 1), name=prefix + '-c1') c2 = PF.convolution(F.relu(c1), 4, (3, 3), pad=(1, 1), name=prefix + '-c2') c = F.add2(c2, c1, inplace=True) return c x = nn.Variable([2, 3, 4, 4]) c1 = unit(x, 'c1') c2 = unit(x, 'c2') y = PF.affine(c2, 5, name='fc') runtime_contents = { 'networks': [{ 'name': 'graph', 'batch_size': 2, 'outputs': { 'y': y }, 'names': { 'x': x } }], } tmpdir.ensure(dir=True) nnp_file = tmpdir.join('tmp.nnp').strpath from nnabla.utils.save import save save(nnp_file, runtime_contents) from nnabla.utils import nnp_graph nnp = nnp_graph.NnpLoader(nnp_file) graph = nnp.get_network('graph') x2 = graph.inputs['x'] y2 = graph.outputs['y'] d = rng.randn(*x.shape).astype(np.float32) x.d = d x2.d = d y.forward(clear_buffer=True) y2.forward(clear_buffer=True) assert_allclose(y.d, y2.d)
def test_networkpass_set_variable(module): '''This test tests whether equivalency between new or old implementation of nnp_graph ''' _, inputs = module verbose = 1 callback = nnp_graph.NnpNetworkPass(verbose) ref_callback = legacy_nnp_graph.NnpNetworkPass(verbose) for inp_name, inp_shape in inputs: inp_shape = (1, *inp_shape[1:]) # change shape callback.set_variable(inp_name, nn.Variable(inp_shape)) ref_callback.set_variable(inp_name, nn.Variable(inp_shape)) with get_saved_test_model(module) as nnp_file: ref_nnp = legacy_nnp_graph.NnpLoader(nnp_file) nnp = nnp_graph.NnpLoader(nnp_file) for ref_v, v in zip(nnp_check(ref_nnp, 'left', ref_callback), nnp_check(nnp, 'right', callback)): verify_equivalence(ref_v, v)
def test_nnp_load_parameter_scope(module): '''This test tests whether equivalency between new or old implementation of nnp_graph ''' _, inputs = module verbose = 1 callback = nnp_graph.NnpNetworkPass(verbose) @callback.on_generate_function_by_name('Convolution') def change_convolution_param(f): print('{}'.format(f.proto.convolution_param.pad.dim[:])) f.proto.convolution_param.pad.dim[:] = [1, 1] return f @callback.on_function_pass_by_type('Affine') def change_affine_param(f, variables, param_scope): param_name = f.inputs[1].proto.name input_shape = f.inputs[0].proto.shape.dim[:] w_shape = f.inputs[1].proto.shape.dim[:] rng = np.random.RandomState(388) with nn.parameter_scope('', param_scope): W = nn.Variable.from_numpy_array( rng.randn(np.prod(input_shape[1:]), w_shape[1])) W.need_grad = True nn.parameter.set_parameter(param_name, W) ref_params = {} with get_saved_test_model(module) as nnp_file: nnp = legacy_nnp_graph.NnpLoader(nnp_file) for network_name in sorted(nnp.get_network_names()): network = nnp.get_network(network_name, batch_size=32, callback=callback) ref_params[network_name] = nn.get_parameters().copy() nn.clear_parameters() params = {} nnp = nnp_graph.NnpLoader(nnp_file) assert_parameter_scope_empty() for network_name in sorted(nnp.get_network_names()): network = nnp.get_network(network_name, batch_size=32, callback=callback) params[network_name] = nn.get_parameters() assert_parameters_equal(ref_params, params)
def draw_graph_command(args): import os from nnabla.logger import logger from nnabla.utils import nnp_graph import nnabla.functions as F from nnabla.experimental.viewers import SimpleGraph logger.info('Loading: %s' % args.input) nnp = nnp_graph.NnpLoader(args.input) names = nnp.get_network_names() logger.info('Available networks: %s' % repr(names)) if not args.network: logger.info( 'Drawing all networks as `--network` option is not passed.') draw_networks = names else: draw_networks = [] for n in args.network: assert n in names, "Specified a network `%s` that doesn't exist." % n draw_networks.append(n) if not os.path.isdir(args.output_dir): os.makedirs(args.output_dir) logger.info("Save drawn network(s) into `%s`." % args.output_dir) for n in draw_networks: logger.info("Drawing: %s" % n) graph = nnp.get_network(n) if not graph.outputs: logger.info("No output in `%s`. Skipping." % n) continue elif len(graph.outputs) > 1: out = F.sink(*graph.outputs.values()) else: out = list(graph.outputs.values())[0] sg = SimpleGraph(format=args.format) sg.save(out, os.path.join(args.output_dir, n.replace(' ', '_')), cleanup=True) return True
def test_load_twice(): from nnabla.ext_utils import list_extensions, get_extension_context exts = list_extensions() if "cudnn" not in exts: pytest.skip("This test is only for cudnn context!") with create_temp_with_dir("network.nntxt") as fn: with open(fn, "w") as f: f.write(nntxt) ctx = get_extension_context('cudnn') nn.set_default_context(ctx) nnp = nnp_graph.NnpLoader(fn) for network_name in sorted(nnp.get_network_names()): print(network_name) network = nnp.get_network(network_name, batch_size=32) for network_name in sorted(nnp.get_network_names()): print(network_name) network = nnp.get_network(network_name, batch_size=32)
def test_examples_cpp_mnist_runtime(tmpdir, nnabla_examples_root, batch_size): pytest.skip('Temporarily skip due to mnist training data server trouble.') nn.clear_parameters() # A. Check this test can run if not nnabla_examples_root.available: pytest.skip('`nnabla-examples` can not be found.') if not command_exists('mnist_runtime'): pytest.skip('An executable `mnist_runtime` is not in path.') tmpdir.chdir() # B. Run mnist training. script = os.path.join(nnabla_examples_root.path, 'image-classification/mnist-collection', 'classification.py') check_call(['python', script, '-i', '100']) # C. Get mnist_runtime results. nnp_file = tmpdir.join('tmp.monitor', 'lenet_result.nnp').strpath assert os.path.isfile(nnp_file) pgm_file = os.path.join(os.path.dirname(__file__), '../../../examples/cpp/mnist_runtime/1.pgm') assert os.path.isfile(pgm_file) output = check_output(['mnist_runtime', nnp_file, pgm_file, 'Runtime']) output.decode('ascii').splitlines()[1].split(':')[1].strip() cpp_result = np.asarray(output.decode('ascii').splitlines()[1].split(':') [1].strip().split(' '), dtype=np.float32) # D. Get nnp_graph results and compare. from nnabla.utils import nnp_graph nnp = nnp_graph.NnpLoader(nnp_file) graph = nnp.get_network('Validation', batch_size=batch_size) x = graph.inputs['x'] y = graph.outputs['y'] from nnabla.utils.image_utils import imread img = imread(pgm_file, grayscale=True) x.d = img y.forward() assert_allclose(y.d.flatten(), cpp_result)
def prepare_model(model): with get_saved_test_model(model) as nnp_file: nnp = nnp_graph.NnpLoader(nnp_file) return nnp