Ejemplo n.º 1
0
def conv2d_leaky_relu_nhwc_oihw_test(
    in_shape,
    w_shape,
    conv_padding,
    conv_strides,
    conv_dilation,
    kernel_layout="OIHW",
    targets=["DPUCZDX8G-zcu104"],
) -> None:

    for target in targets:
        xgraph = _create_conv2d_leaky_relu_nhwc_oihw(
            in_shape,
            w_shape,
            conv_padding,
            conv_strides,
            conv_dilation,
            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") == {"lr1": "lr1:0"}
        assert len(c_output.get_code_files("xp0")) == 1

        shutil.rmtree(work_dir)
        shutil.rmtree(build_dir)
Ejemplo n.º 2
0
def xcompiler_conv2d_leaky_relu_nhwc_oihw_test(
    in_shape,
    w_shape,
    conv_padding,
    conv_strides,
    conv_dilation,
    kernel_layout="OIHW",
    targets=["DPUCZDX8G-zcu104"],
    expected_nb_subgraphs=3,
) -> None:

    for target in targets:
        xgraph = _create_conv2d_leaky_relu_nhwc_oihw(
            in_shape,
            w_shape,
            conv_padding,
            conv_strides,
            conv_dilation,
            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)

        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))
Ejemplo n.º 3
0
                 bn_handling=BatchNormHandling.MERGE_AND_QUANTIZE)

# Finetune the model
# . . .

# Export to ONNX
onnx_filename = 'dpuv2_resnet18.onnx'
export_dpuv2_onnx(model,
                  input_shape=IN_SIZE,
                  input_t=inp,
                  export_path=onnx_filename)

# Load ONNX into PyXIR
onnx_model = onnx.load(onnx_filename)
xgraph = from_onnx(onnx_model)
xgraph = pyxir.partition(xgraph, [target])
xgraph = pyxir.optimize(xgraph, target)
work_dir = os.path.join(file_dir, f'{target}_quant_trained_resnet18_workdir')
inputs = np.random.randn(*IN_SIZE)


def inputs_func(iter):
    return {'inp.1': inputs}


xgraph = pyxir.quantize(xgraph, target, inputs_func, work_dir=work_dir)
pyxir.build(xgraph,
            target,
            work_dir=work_dir,
            build_dir=work_dir,
            runtime='cpu-np')
Ejemplo n.º 4
0
def xcompiler_resnetv1_block_test(
    in_shape,
    pool_size,
    pool_strides,
    w1_shape,
    w2_shape,
    w3_shape,
    w4_shape,
    c1_padding=[0, 0, 0, 0],
    c2_padding=[0, 0, 0, 0],
    c3_padding=[0, 0, 0, 0],
    c4_padding=[0, 0, 0, 0],
    c1_strides=[1, 1],
    c2_strides=[1, 1],
    c3_strides=[1, 1],
    c4_strides=[1, 1],
    c1_dilation=[1, 1],
    c2_dilation=[1, 1],
    c3_dilation=[1, 1],
    c4_dilation=[1, 1],
    kernel_layout="OIHW",
    target="DPUCAHX8H-u50",
    expected_nb_subgraphs=3,
):

    xgraph = _create_resnetv1_block(
        in_shape,
        pool_size,
        pool_strides,
        w1_shape,
        w2_shape,
        w3_shape,
        w4_shape,
        c1_padding,
        c2_padding,
        c3_padding,
        c4_padding,
        c1_strides,
        c2_strides,
        c3_strides,
        c4_strides,
        c1_dilation,
        c2_dilation,
        c3_dilation,
        c4_dilation,
        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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
def prequantize_onnx_model(onnx_model, target, inputs_func, out_file,
                           **kwargs):

    xgraph = _from_onnx(onnx_model)

    xgraph = px.partition(xgraph, [target])
    xgraph = px.optimize(xgraph, target)

    q_xgraph = px.quantize(xgraph, target, inputs_func, **kwargs)

    # Move quant_info information from XGraph to ONNX model
    tensor_quant_info = {}
    for X in q_xgraph.get_layers():
        if "vai_quant" in X.attrs and X.attrs["vai_quant"] != []:
            tensor_name = X.attrs['onnx_id']
            if tensor_name in tensor_quant_info:
                raise NotImplementedError("Quantization for ONNX tensor: {}"
                                          " already provided. Merging of"
                                          " multiple tensor quantization info"
                                          " parameters not supported yet")
            tensor_quant_info[tensor_name] = {
                "vai_quant": X.attrs["vai_quant"]
            }

            for vai_quant_elem in X.attrs['vai_quant']:
                tensor_quant_info[tensor_name][vai_quant_elem] = \
                    X.attrs[vai_quant_elem]

    for node in onnx_model.graph.node:
        node_w = NodeWrapper(node)
        tensor_name = node_w.get_outputs()[0]
        if tensor_name in tensor_quant_info:
            node_w.add_attribute(
                'vai_quant',
                list(tensor_quant_info[tensor_name]['vai_quant']),
                'STRINGS'
            )
            for vai_quant_elem in tensor_quant_info[tensor_name]['vai_quant']:
                node_w.add_attribute(
                    vai_quant_elem,
                    tensor_quant_info[tensor_name][vai_quant_elem],
                    'INTS'
                )

    # q_output = q_xgraph.get_quantizer_output()

    # for qkey in q_output.keys():
    #     quant_file = q_output.get_q_file(qkey)
    #     quant_info_file = q_output.get_q_info(qkey)
    #     quant_orig_pb = q_output.get_orig_pb(qkey)

    #     if not os.path.isfile(quant_info_file):
    #         raise ValueError("quant file: {} for qkey: {} does not exist"
    #                          .format(quant_info_file, qkey))

    #     meta = onnx_model.metadata_props.add()
    #     meta.key = "vitis_ai_quant--q_file--" + qkey
    #     meta.value = str(quant_file)

    #     meta = onnx_model.metadata_props.add()
    #     meta.key = "vitis_ai_quant--q_info--" + qkey
    #     meta.value = str(quant_info_file)

    #     meta = onnx_model.metadata_props.add()
    #     meta.key = "vitis_ai_quant--orig_pb--" + qkey
    #     meta.value = str(quant_orig_pb)

    onnx.save(onnx_model, out_file)