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)
import pytest import nnabla as nn import nnabla.utils.dlpack import numpy as np from nnabla.ext_utils import list_extensions, \ get_extension_context from nnabla.testing import assert_allclose # Try to import PyTorch try: import torch import torch.utils.dlpack except ImportError: pytest.skip('PyTorch is not installed.', allow_module_level=True) ext_names = list_extensions() types = [[np.float32, torch.float32]] # PyTorch to NNabla @pytest.mark.parametrize("ext_name", ext_names) @pytest.mark.parametrize("numpy_type, torch_type", types) def test_from_dlpack_new(ext_name, numpy_type, torch_type): ctx = get_extension_context(ext_name) device_name = ctx.backend[0].split(':')[0] if device_name == 'cudnn': device_name = 'cuda' # for PyTorch nn.set_default_context(ctx) # Init PyTorch Tensor t = torch.ones((5, 5), dtype=torch_type, device=torch.device(device_name))
# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import pytest from nnabla import ext_utils ext_names = ext_utils.list_extensions() @pytest.mark.parametrize('ext_name', ext_names) def test_import_extension_module(ext_name): ext = ext_utils.import_extension_module(ext_name) @pytest.mark.parametrize('ext_name', ext_names) def test_get_extension_context(ext_name): ctx = ext_utils.get_extension_context(ext_name) @pytest.mark.parametrize('ext_name', ext_names) def test_ext_utils_misc(ext_name): ext = ext_utils.import_extension_module(ext_name)
import pytest import nnabla as nn import numpy as np from nnabla.ext_utils import get_extension_context, list_extensions from nnabla.utils.inspection import TimeProfiler from .models import simple_cnn @pytest.mark.parametrize("batch_size", [8]) @pytest.mark.parametrize("n_class", [5]) @pytest.mark.parametrize( "ext_name", [x for x in list_extensions() if x in ["cpu", "cudnn"]]) def test_time_profiler(batch_size, n_class, ext_name, tmpdir): nn.clear_parameters() ctx = get_extension_context(ext_name) nn.set_default_context(ctx) x = nn.Variable.from_numpy_array( np.random.normal(size=(batch_size, 3, 16, 16))) t = nn.Variable.from_numpy_array( np.random.randint(low=0, high=n_class, size=(batch_size, 1))) y = simple_cnn(x, t, n_class) tp = TimeProfiler(ext_name, device_id=ctx.device_id) for i in range(5):
import nnabla.functions as F from nnabla.ext_utils import get_extension_context, list_extensions from nnabla.utils.inspection import NanInfTracer from .models import simple_cnn def _refresh_inputs_grad(f): for i in f.inputs: i.grad.zero() @pytest.mark.parametrize("batch_size", [8]) @pytest.mark.parametrize("n_class", [5]) @pytest.mark.parametrize("ext_name", list_extensions()) @pytest.mark.parametrize("trace_nan", [False, True]) @pytest.mark.parametrize("trace_inf", [False, True]) def test_nan_inf_tracer(batch_size, n_class, ext_name, trace_nan, trace_inf): nn.clear_parameters() ctx = get_extension_context(ext_name) nn.set_default_context(ctx) x = nn.Variable.from_numpy_array( np.random.normal(size=(batch_size, 3, 16, 16))) t = nn.Variable.from_numpy_array( np.random.randint(low=0, high=n_class, size=(batch_size, 1))) y = simple_cnn(x, t, n_class)