Пример #1
0
def multi_output_conv2d_naming_test(out_names) -> None:

    in_shape = (1, 20, 20, 10)
    target = "DPUCZDX8G-zcu104"
    xgraph = _create_multi_output_conv2d_nhwc_oihw(
        in_shape, (10, 10, 2, 2), [0, 0], [1, 1], [1, 1], "OIHW", target, out_names,
    )

    def inputs_func(iter):
        inputs = np.ones(in_shape, dtype=np.float32)
        return {"in1": inputs}

    work_dir = os.path.join(FILE_PATH, "work")
    build_dir = os.path.join(FILE_PATH, "build")
    quantize_func = TARGET_REGISTRY.get_target_quantizer(target)
    q_xgraph = quantize_func(xgraph, inputs_func, work_dir=work_dir)
    opt_xgraph = px.optimize(q_xgraph, target)
    c_xgraph = px.compile(opt_xgraph, target, work_dir=work_dir, build_dir=build_dir)
    c_output = c_xgraph.get_compiler_output()

    assert list(c_output.keys()) == ["xp0"]
    assert c_output.get_in_map("xp0") == {"xinput0": "xinput0:0"}
    # assert c_output.get_out_map("xp0") == {pool_name: "pool1:0"}
    assert len(c_output.get_code_files("xp0")) == 1

    shutil.rmtree(work_dir)
    shutil.rmtree(build_dir)
Пример #2
0
def conv2d_pool2d_nhwc_oihw_test(
    in_shape,
    w_shape,
    conv_padding,
    conv_strides,
    conv_dilation,
    pool_type,
    pool_size,
    pool_padding=[0, 0],
    pool_strides=[1, 1],
    conv_groups=1,
    conv_invalid=False,
    kernel_layout="OIHW",
    targets=["DPUCZDX8G-zcu104"],
) -> None:

    for target in targets:
        xgraph = _create_conv2d_pool2d_nhwc_oihw(
            in_shape,
            w_shape,
            conv_padding,
            conv_strides,
            conv_dilation,
            pool_type,
            pool_size,
            pool_padding,
            pool_strides,
            conv_groups,
            conv_invalid,
            kernel_layout,
            target,
        )

        def inputs_func(iter):
            inputs = np.ones(in_shape, dtype=np.float32)
            return {"in1": inputs}

        work_dir = os.path.join(FILE_PATH, "work")
        build_dir = os.path.join(FILE_PATH, "build")
        quantize_func = TARGET_REGISTRY.get_target_quantizer(target)
        q_xgraph = quantize_func(xgraph, inputs_func, work_dir=work_dir)
        opt_xgraph = px.optimize(q_xgraph, target)
        c_xgraph = px.compile(opt_xgraph,
                              target,
                              work_dir=work_dir,
                              build_dir=build_dir)
        c_output = c_xgraph.get_compiler_output()

        assert list(c_output.keys()) == ["xp0"]
        assert c_output.get_in_map("xp0") == {"xinput0": "xinput0:0"}
        assert c_output.get_out_map("xp0") == {"pool1": "pool1:0"}
        assert len(c_output.get_code_files("xp0")) == 1

        shutil.rmtree(work_dir)
        shutil.rmtree(build_dir)
Пример #3
0
def xcompiler_scale_conv2d_nhwc_oihw_test(
    in_shape,
    w_shape,
    conv_padding,
    conv_strides,
    conv_dilation,
    conv_groups=1,
    kernel_layout="OIHW",
    target="DPUCAHX8H-u50",
    expected_nb_subgraphs=3,
):

    xgraph = _create_scale_conv2d_nhwc_oihw(
        in_shape,
        w_shape,
        conv_padding,
        conv_strides,
        conv_dilation,
        conv_groups,
        kernel_layout,
        target,
    )

    def inputs_func(iter):
        inputs = np.ones(in_shape, dtype=np.float32)
        return {"in1": inputs}

    work_dir = os.path.join(FILE_PATH, "work")
    build_dir = os.path.join(FILE_PATH, "build")
    quantize_func = TARGET_REGISTRY.get_target_quantizer(target)
    q_xgraph = quantize_func(xgraph, inputs_func, work_dir=work_dir)
    opt_xgraph = px.optimize(q_xgraph, target)
    c_xgraph = px.compile(opt_xgraph,
                          target,
                          work_dir=work_dir,
                          build_dir=build_dir)
    c_output = c_xgraph.get_compiler_output()

    g = xir.Graph.deserialize(os.path.join(build_dir, "xp0.xmodel"))
    # TODO subgraphs[1].get_attr("device") -> *** RuntimeError: bad any_cast
    subgraphs = get_child_subgraphs(g)
    assert (len(subgraphs) == expected_nb_subgraphs
            ), "Expected {0} subgraphs but got: {1}".format(
                expected_nb_subgraphs, len(subgraphs))

    shutil.rmtree(work_dir)
    shutil.rmtree(build_dir)
Пример #4
0
def xcompiler_conv2d_pool2d_nhwc_oihw_test(
    in_shape,
    w_shape,
    conv_padding,
    conv_strides,
    conv_dilation,
    pool_type,
    pool_size,
    pool_padding=[0, 0],
    pool_strides=[1, 1],
    conv_groups=1,
    conv_invalid=False,
    kernel_layout="OIHW",
    targets=["DPUCAHX8H-u50"],
    expected_nb_subgraphs=3,
):

    for target in targets:
        xgraph = _create_conv2d_pool2d_nhwc_oihw(
            in_shape,
            w_shape,
            conv_padding,
            conv_strides,
            conv_dilation,
            pool_type,
            pool_size,
            pool_padding,
            pool_strides,
            conv_groups,
            conv_invalid,
            kernel_layout,
            target,
        )

        def inputs_func(iter):
            inputs = np.ones(in_shape, dtype=np.float32)
            return {"in1": inputs}

        work_dir = os.path.join(FILE_PATH, "work")
        build_dir = os.path.join(FILE_PATH, "build")
        quantize_func = TARGET_REGISTRY.get_target_quantizer(target)
        q_xgraph = quantize_func(xgraph, inputs_func, work_dir=work_dir)
        opt_xgraph = px.optimize(q_xgraph, target)
        c_xgraph = px.compile(
            opt_xgraph, target, work_dir=work_dir, build_dir=build_dir
        )
        c_output = c_xgraph.get_compiler_output()

        assert list(c_output.keys()) == ["xp0"]
        assert c_output.get_in_map("xp0") == {"xinput0": "xinput0"}
        assert c_output.get_out_map("xp0") == {"pool1": "pool1"}
        assert len(c_output.get_code_files("xp0")) == 1

        g = xir.Graph.deserialize(os.path.join(build_dir, "xp0.xmodel"))
        # TODO subgraphs[1].get_attr("device") -> *** RuntimeError: bad any_cast
        subgraphs = get_child_subgraphs(g)
        assert len(subgraphs) == expected_nb_subgraphs
        dpu_subgraph = subgraphs[1]
        # import pdb; pdb.set_trace()
        # assert len(dpu_subgraph.get_children()) == 3

        shutil.rmtree(work_dir)
        shutil.rmtree(build_dir)