def test_qemu_make_fail(workspace_dir, board, west_cmd, microtvm_debug):
    """Testing QEMU make fail."""
    if board not in ["qemu_x86", "mps2_an521", "mps3_an547"]:
        pytest.skip(msg="Only for QEMU targets.")

    model = test_utils.ZEPHYR_BOARDS[board]
    build_config = {"debug": microtvm_debug}
    shape = (10, )
    dtype = "float32"

    # Construct Relay program.
    x = relay.var("x", relay.TensorType(shape=shape, dtype=dtype))
    xx = relay.multiply(x, x)
    z = relay.add(xx, relay.const(np.ones(shape=shape, dtype=dtype)))
    func = relay.Function([x], z)
    ir_mod = tvm.IRModule.from_expr(func)

    target = tvm.target.target.micro(model)
    executor = Executor("aot")
    runtime = Runtime("crt")
    with tvm.transform.PassContext(opt_level=3,
                                   config={"tir.disable_vectorize": True}):
        lowered = relay.build(ir_mod,
                              target,
                              executor=executor,
                              runtime=runtime)

    sample = np.zeros(shape=shape, dtype=dtype)
    project, project_dir = test_utils.generate_project(
        workspace_dir,
        board,
        west_cmd,
        lowered,
        build_config,
        sample,
        shape,
        dtype,
        load_cmsis=False,
    )

    file_path = pathlib.Path(project_dir) / "build" / "build.ninja"
    assert file_path.is_file(), f"[{file_path}] does not exist."

    # Remove a file to create make failure.
    os.remove(file_path)
    project.flash()
    with pytest.raises(server.JSONRPCError) as excinfo:
        project.transport().open()
    assert "QEMU setup failed" in str(excinfo.value)
def test_armv7m_intrinsic(temp_dir, board, west_cmd, tvm_debug):
    """Testing a ARM v7m SIMD extension."""

    if board not in [
        "mps2_an521",
        "stm32f746xx_disco",
        "nucleo_f746zg",
        "nucleo_l4r5zi",
        "nrf5340dk_nrf5340_cpuapp",
    ]:
        pytest.skip(msg="Platform does not support ARM v7m SIMD extension.")

    model = test_utils.ZEPHYR_BOARDS[board]

    build_config = {"debug": tvm_debug}

    this_dir = pathlib.Path(os.path.dirname(__file__))
    testdata_dir = this_dir.parent / "testdata" / "mnist"

    relay_mod, params = _open_tflite_model()

    sample, output_shape = _get_test_data(testdata_dir)

    relay_mod_simd = _apply_desired_layout_simd(relay_mod)
    # kernel layout "HWIO" is not supported by arm_cpu SIMD extension (see tvm\python\relay\op\strategy\arm_cpu.py)
    relay_mod_no_simd = _apply_desired_layout_no_simd(relay_mod)

    target = tvm.target.target.micro(model, options=["-keys=cpu"])
    target_simd = tvm.target.target.micro(model, options=["-keys=arm_cpu,cpu"])

    executor = Executor("aot", {"unpacked-api": True, "interface-api": "c"})
    runtime = Runtime("crt")

    temp_dir_simd = temp_dir / "simd"
    temp_dir_no_simd = temp_dir / "nosimd"

    os.makedirs(temp_dir_simd, exist_ok=True)
    os.makedirs(temp_dir_no_simd, exist_ok=True)

    with tvm.transform.PassContext(opt_level=3, config={"tir.disable_vectorize": True}):
        lowered_simd = relay.build(
            relay_mod_simd, target_simd, params=params, runtime=runtime, executor=executor
        )
        lowered_no_simd = relay.build(
            relay_mod_no_simd, target, params=params, runtime=runtime, executor=executor
        )

        simd_project, _ = test_utils.generate_project(
            temp_dir_simd,
            board,
            west_cmd,
            lowered_simd,
            build_config,
            sample,
            output_shape,
            "float32",
            load_cmsis=True,
        )
        result_simd, time_simd = test_utils.run_model(simd_project)

        no_simd_project, _ = test_utils.generate_project(
            temp_dir_no_simd,
            board,
            west_cmd,
            lowered_no_simd,
            build_config,
            sample,
            output_shape,
            "float32",
            load_cmsis=False,
        )
        result_no_simd, time_no_simd = test_utils.run_model(no_simd_project)

    assert result_no_simd == result_simd == 2

    # Time performance measurements on QEMU emulator are always equal to zero.
    if board not in [
        "mps2_an521",
        "mps3_an547",
    ]:
        assert time_no_simd > time_simd
Exemple #3
0
def test_tflite(temp_dir, board, west_cmd, tvm_debug):
    """Testing a TFLite model."""
    model = test_utils.ZEPHYR_BOARDS[board]
    input_shape = (1, 49, 10, 1)
    output_shape = (1, 12)
    build_config = {"debug": tvm_debug}

    model_url = "https://github.com/tlc-pack/web-data/raw/25fe99fb00329a26bd37d3dca723da94316fd34c/testdata/microTVM/model/keyword_spotting_quant.tflite"
    model_path = download_testdata(model_url,
                                   "keyword_spotting_quant.tflite",
                                   module="model")

    # Import TFLite model
    tflite_model_buf = open(model_path, "rb").read()
    try:
        import tflite

        tflite_model = tflite.Model.GetRootAsModel(tflite_model_buf, 0)
    except AttributeError:
        import tflite.Model

        tflite_model = tflite.Model.Model.GetRootAsModel(tflite_model_buf, 0)

    # Load TFLite model and convert to Relay
    relay_mod, params = relay.frontend.from_tflite(
        tflite_model,
        shape_dict={"input_1": input_shape},
        dtype_dict={"input_1 ": "int8"})

    target = tvm.target.target.micro(model)
    executor = Executor("aot", {
        "unpacked-api": True,
        "interface-api": "c",
        "workspace-byte-alignment": 4
    })
    runtime = Runtime("crt")
    with tvm.transform.PassContext(opt_level=3,
                                   config={"tir.disable_vectorize": True}):
        lowered = relay.build(relay_mod,
                              target,
                              params=params,
                              runtime=runtime,
                              executor=executor)

    sample_url = "https://github.com/tlc-pack/web-data/raw/967fc387dadb272c5a7f8c3461d34c060100dbf1/testdata/microTVM/data/keyword_spotting_int8_6.pyc.npy"
    sample_path = download_testdata(sample_url,
                                    "keyword_spotting_int8_6.pyc.npy",
                                    module="data")
    sample = np.load(sample_path)

    project, _ = test_utils.generate_project(
        temp_dir,
        board,
        west_cmd,
        lowered,
        build_config,
        sample,
        output_shape,
        "int8",
        load_cmsis=False,
    )

    result, time = test_utils.run_model(project)
    assert result == 6