def test_generate_output_stripe_configs_single_dimension():
    stripe_factors = 3
    subgraph = cs.TESubgraph([], None)
    part_1 = cs.InlinePart(
        subgraph,
        [
            cs.Propagator(
                [[2, 0, 0], [0, 2, 0], [0, 0, 1]],
                [0, 0],
            ),
        ],
    )
    tensor_1 = cs.Tensor([800, 800], "uint8")
    tensor_2 = cs.Tensor([400, 400], "uint8")

    part_1.set_input(0, tensor_1)
    part_1.set_output(tensor_2)
    tensor_1.add_consumer(part_1)
    tensor_2.add_producer(part_1)

    expected_stripe_configs = {
        cs.StripeConfig([400, 1], [400, 400], [400, 1], [2, 1], [1, 400], [0, 0]),
        cs.StripeConfig([400, 200], [400, 400], [400, 200], [2, 1], [1, 2], [0, 0]),
        cs.StripeConfig([1, 400], [400, 400], [1, 400], [1, 2], [400, 1], [0, 0]),
        cs.StripeConfig([200, 400], [400, 400], [200, 400], [1, 2], [2, 1], [0, 0]),
        cs.StripeConfig([400, 400], [400, 400], [400, 400], [1, 2], [1, 1], [0, 0]),
    }

    output_stripe_configs = _generate_output_stripe_configs(
        part=part_1, stripe_factors=stripe_factors, enable_striping=True, multi_dimensional=False
    )

    assert len(output_stripe_configs) == len(expected_stripe_configs)
    assert set(output_stripe_configs) == expected_stripe_configs
Beispiel #2
0
def test_inline_part():
    subgraph = cs.TESubgraph([], None)
    part = cs.InlinePart(
        subgraph,
        [
            cs.Propagator(
                [[0, 1, 0], [1, 0, 0], [0, 0, 1]],
                [0, 0],
            ),
        ],
    )
    output_stripe_config = cs.StripeConfig([2, 4], [8, 8], [2, 4], [1, 2],
                                           [4, 2], [0, 0])
    input_stripe_config = cs.StripeConfig([4, 2], [8, 8], [4, 2], [2, 1],
                                          [2, 4], [0, 0])

    assert part.input_tensors == [None]
    assert part.output_tensor == None
    assert len(part.propagators) == 1
    assert part.in_line == True
    assert part.get_stripe_align_hint() == [1, 1]
    performance_info = part.get_performance_info(output_stripe_config,
                                                 is_rolling=False)
    assert performance_info.compute_cycles == 0
    assert performance_info.read_bytes == [0]
    assert performance_info.write_bytes == 0
    input_stripe_configs = part.calculate_input_stripe_configs(
        output_stripe_config)
    assert len(input_stripe_configs) == 1
    assert input_stripe_configs[0] == input_stripe_config
Beispiel #3
0
def test_ethosu_part():
    te_subgraph = cs.TESubgraph([], None)
    output_quantum = [1, 2, 2, 8]
    propagator = cs.Propagator(
        [[1, 0, 0, 0, 2], [0, 1, 0, 0, 2], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]],
        [0, 0, 0, 0],
    )
    stripe_config = cs.StripeConfig(
        [1, 4, 4, 16], [1, 64, 72, 96], [1, 4, 4, 16], [1, 2, 3, 4], [1, 16, 13, 6], [0, 0, 0, 0]
    )
    subkernels = 3

    valid_block_configs = [cs.BlockConfig([1, 2, 4, 16], 15000, 7500)]

    part = EthosuPart(
        te_subgraph,
        [propagator],
        output_quantum,
        subkernels,
        valid_block_configs,
        1,
    )
    input_tensor = cs.Tensor(shape=[1, 66, 74, 16], dtype="int8")
    part.set_input(0, input_tensor)

    assert part.get_stripe_align_hint() == output_quantum
    # Check that the performance model runs, don't verify output
    part.get_performance_info(stripe_config, BufferMode.ROLLING)
    part.get_performance_info(stripe_config, BufferMode.RECOMPUTE)
Beispiel #4
0
def test_ethosu_part():
    te_subgraph = pl.TESubgraph([], None)
    output_quantum = [1, 2, 2, 8]
    quantum_cycles = 32
    propagator = pl.Propagator(
        [[1, 0, 0, 0, 2], [0, 1, 0, 0, 2], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0],
         [0, 0, 0, 0, 1]],
        [0, 0, 0, 0],
    )
    stripe_config = pl.StripeConfig([1, 4, 4, 16], [1, 64, 72, 96],
                                    [1, 4, 4, 16], [1, 2, 3, 4],
                                    [1, 16, 13, 6], [0, 0, 0, 0])

    part = EthosuPart(te_subgraph, [propagator], output_quantum,
                      quantum_cycles)

    assert part.get_stripe_align_hint() == output_quantum
    # Check that the performance model runs, don't verify output
    part.get_performance_info(stripe_config, False)
    part.get_performance_info(stripe_config, True)
Beispiel #5
0
def test_ethosu_unary_elementwise_matcher(ofm_shape, ifm_layout, ofm_layout, op_type):
    ifm_shape = ofm_shape.copy()
    ofm_channels = ofm_shape[3]
    nhwc_to_nhcwb16, _ = get_layout_transform_matrices(ofm_channels)
    if ifm_layout == "NHCWB16":
        ifm_shape = [
            int(math.ceil(n))
            for n in np.matmul(
                nhwc_to_nhcwb16,
                ifm_shape
                + [
                    1,
                ],
            ).tolist()[:-1]
        ]
    if ofm_layout == "NHCWB16":
        ofm_shape = [
            int(math.ceil(n))
            for n in np.matmul(
                nhwc_to_nhcwb16,
                ofm_shape
                + [
                    1,
                ],
            ).tolist()[:-1]
        ]
        order = [1, 2, 4, 3, 0]
    else:
        order = [1, 2, 3, 4]

    ifm = te.placeholder(ifm_shape, dtype="int8")
    lut = te.placeholder((), dtype="uint8")
    out = unary_elementwise_compute(
        ifm=ifm,
        lut=lut,
        operator_type=op_type,
        ifm_scale=1,
        ifm_zero_point=0,
        ofm_scale=1,
        ofm_zero_point=0,
        ofm_channels=ofm_channels,
        activation="NONE",
        clip_min=0,
        clip_max=0,
        rounding_mode="TFL",
        ifm_layout=ifm_layout,
        ofm_layout=ofm_layout,
    )
    ifm_propagator = out.op.attrs["ifm_propagator"]

    offset = [0] * len(ofm_shape)
    stripes = [0] * len(ofm_shape)
    output_stripe_config = cs.StripeConfig(ofm_shape, ofm_shape, ofm_shape, order, stripes, offset)

    ifm_transform = _make_matrices(ifm_layout, ofm_layout, ofm_channels)

    device_config = cs.EthosuDeviceConfig("ethos-u55-256")
    part = match_ethosu_unary_elementwise(out, device_config)

    assert isinstance(part, cs.EthosuPart)
    assert len(part.propagators) == 1
    assert part.propagators[0].transform == ifm_transform

    propagated_ifm = ifm_propagator.propagate(output_stripe_config).shape

    # The layout transforms that have the exact number of output channels in them
    # will lose no information about the number of channels
    assert ifm_shape == propagated_ifm
Beispiel #6
0
def test_best_block_config(
    test_id,
    op_type,
    activation,
    kernel,
    stride,
    dilation,
    padding,
    in_shape,
    out_shape,
    layouts,
    acc_config,
    expected_block_configs,
):
    nhwc_to_nhcwb16 = [
        [1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0],
        [0, 0, 0, 1 / 16, 0],
        [0, 0, 1, 0, 0],
        [0, 0, 0, 0, 16],
        [0, 0, 0, 0, 1],
    ]
    nhcwb16_to_nhwc = [
        [1, 0, 0, 0, 0, 0],
        [0, 1, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0],
        [0, 0, 16, 0, 1, -16],
        [0, 0, 0, 0, 0, 1],
    ]
    ifm_matrix, ifm_offset, weight_matrix, weight_offset, _, _ = make_matrices(
        op_type, kernel, stride, padding, layouts[0], layouts[1], dilation,
        in_shape[3])

    ofm_channels = out_shape[3]
    ifm_channels = in_shape[3]

    if layouts[0] == "NHCWB16":
        in_shape = [
            int(math.ceil(n))
            for n in np.matmul(nhwc_to_nhcwb16, in_shape + (1, )).tolist()[:-1]
        ]
    if layouts[1] == "NHCWB16":
        out_shape = [
            int(math.ceil(n)) for n in np.matmul(nhwc_to_nhcwb16, out_shape +
                                                 (1, )).tolist()[:-1]
        ]

    propagator = cs.Propagator(ifm_matrix, ifm_offset)
    weight_propagator = cs.Propagator(weight_matrix, weight_offset)

    subkernels = ((kernel[0] + 7) // 8) * ((kernel[1] + 7) // 8)

    op_attrs = {
        "op": op_type,
        "activation": activation,
        "stride_h": stride[0],
        "stride_w": stride[1],
        "dilation_h": dilation[0],
        "dilation_w": dilation[1],
    }

    device_config = cs.EthosuDeviceConfig(acc_config)
    block_configs = device_config.get_valid_block_configs(
        propagator,
        op_attrs,
        out_shape,
        ofm_channels,
        ifm_channels,
        layouts[1],
        layouts[0],
        "int8",
        "int8",
        kernel[0],
        kernel[1],
    )

    output_quantum = [1, 1, 2, 8]
    if layouts[1] == "NHCWB16":
        output_quantum = [1, 1, 1, 2, 8]

    # Create EthosUPart
    te_subgraph = cs.TESubgraph([], None)
    part = cs.EthosuPart(
        te_subgraph,
        [propagator, weight_propagator],
        output_quantum,
        subkernels,
        block_configs,
        1,
    )

    order = [1, 2, 3, 4] if layouts[1] == "NHCWB16" else [1, 2, 4, 3, 0]
    stripes = [1] * len(output_quantum)
    offset = [0] * len(output_quantum)

    stripe_config = cs.StripeConfig(out_shape, out_shape, out_shape, order,
                                    stripes, offset)

    block = part.get_block_config(stripe_config)
    block_shape = tuple(int(a) for a in block.output_shape)

    assert block_shape in expected_block_configs[test_id]
Beispiel #7
0
def test_conv_performance(
    accelerator,
    op_type,
    activation,
    kernel,
    stride,
    dilation,
    padding,
    in_shape,
    out_shape,
    block_shape,
    input_block_shape,
    expected,
):
    ifm_channels = in_shape[3]
    ifm_matrix, ifm_offset, weight_matrix, weight_offset, _, _ = make_matrices(
        op_type,
        kernel,
        stride,
        padding,
        "NHWC",
        "NHWC",
        dilation,
        ifm_channels,
    )

    propagator = cs.Propagator(ifm_matrix, ifm_offset)
    weight_propagator = cs.Propagator(weight_matrix, weight_offset)

    subkernels = ((kernel[0] + 7) // 8) * ((kernel[1] + 7) // 8)

    device_config = cs.EthosuDeviceConfig(accelerator)

    output_cycles = device_config._get_output_cycles(op_type, "", "int8", "int8", activation)
    output_cycles *= reduce(lambda a, b: a * b, block_shape, 1)
    is_partkernel = device_config.is_partkernel(
        op_type, ifm_channels, "int8", kernel[0] * kernel[1]
    )
    compute_cycles = device_config._estimate_compute_cycles_per_block(
        op_type,
        _Shape(block_shape),
        _Shape(input_block_shape),
        kernel[0],
        kernel[1],
        ifm_channels,
        "int8",
        is_partkernel,
    )
    block_configs = [
        cs.BlockConfig(input_block_shape, block_shape, compute_cycles, int(output_cycles))
    ]

    output_quantum = [1, 1, 2, 8]
    te_subgraph = cs.TESubgraph([], None)
    part = cs.EthosuPart(
        te_subgraph,
        [propagator, weight_propagator],
        output_quantum,
        subkernels,
        block_configs,
        1,
    )
    part.set_input(0, cs.Tensor(in_shape, "int8"))
    part.set_input(1, cs.Tensor([ifm_channels, kernel[0], kernel[1], out_shape[-1]], "int8"))
    part.set_output(cs.Tensor(out_shape, "int8"))

    stripes = [1] * len(output_quantum)
    offset = [0] * len(output_quantum)
    order = [1, 2, 3, 4]

    stripe_config = cs.StripeConfig(out_shape, out_shape, out_shape, order, stripes, offset)

    compute_cycles = part.get_performance_info(stripe_config, cs.BufferMode.ROLLING).compute_cycles
    tolerance = expected * 0.1

    assert expected - tolerance <= compute_cycles <= expected + tolerance
Beispiel #8
0
def test_ethosu_conv2d_block_config_from_matcher(ifm_layout, ofm_layout,
                                                 ifm_channels,
                                                 expected_cycles):
    ofm_channels = 10
    ifm_height = 123
    ifm_width = 155

    ifm_shape = ((1, ifm_height, ifm_width,
                  ifm_channels) if ifm_layout == "NHWC" else
                 (1, ifm_height, 1 + ((ifm_channels - 1) // 16), ifm_width,
                  16))
    weight_shape = (ofm_channels, 3, 3, ifm_channels)
    scale_bias_shape = (ofm_channels, 10)

    ifm = te.placeholder(ifm_shape, dtype="int8")
    weight = te.placeholder(weight_shape, dtype="int8")
    scale_bias = te.placeholder(scale_bias_shape, dtype="uint8")
    lut = te.placeholder((), dtype="uint8")
    out = conv2d_compute(
        ifm=ifm,
        weight=weight,
        scale_bias=scale_bias,
        lut=lut,
        ifm_scale=1,
        ifm_zero_point=0,
        ofm_scale=1,
        ofm_zero_point=0,
        weight_zero_point=0,
        strides=(1, 1),
        padding=(0, 0, 0, 0),
        dilation=(1, 1),
        activation="NONE",
        clip_min=0,
        clip_max=0,
        upscale="NONE",
        rounding_mode="TFL",
        ifm_layout=ifm_layout,
        ofm_layout=ofm_layout,
    )

    device_config = cs.EthosuDeviceConfig("ethos-u55-256")
    part = match_ethosu_conv2d(out, device_config)

    ofm_shape = [int(i) for i in part.subgraph.output_tensor.shape]

    # Add inputs and outputs to the part
    input_tensor = cs.Tensor(ifm_shape, "int8")
    part.set_input(0, input_tensor)
    weight_tensor = cs.Tensor(weight_shape, "int8")
    part.set_input(1, weight_tensor)
    scale_bias_tensor = cs.Tensor(scale_bias_shape, "int8")
    part.set_input(2, scale_bias_tensor)
    output_tensor = cs.Tensor(ofm_shape, "int8")
    part.set_output(output_tensor)

    # Create a stripe of a size of the output tensor
    order = [1, 2, 3, 4] if ofm_layout == "NHWC" else [1, 2, 4, 3, 0]
    stripes = [1] * len(order)
    offset = [0] * len(order)

    stripe_config = cs.StripeConfig(ofm_shape, ofm_shape, ofm_shape, order,
                                    stripes, offset)

    block = part.get_block_config(stripe_config)

    # Since we dont know the values of the variables we passed to the get_valid_block_configs in
    # the matcher, best we can do is to verify the compute cycle count since the channels have a
    # significant effect on it
    assert block.compute_cycles == expected_cycles
Beispiel #9
0
def test_ethosu_unary_elementwise_matcher(ofm_shape, ifm_layout, ofm_layout,
                                          op_type):
    ifm_shape = ofm_shape.copy()
    ofm_channels = ofm_shape[3]
    nhwc_to_nhcwb16 = [
        [1, 0, 0, 0, 0],
        [0, 1, 0, 0, 0],
        [0, 0, 0, 1 / 16, 0],
        [0, 0, 1, 0, 0],
        [0, 0, 0, 0, 16],
        [0, 0, 0, 0, 1],
    ]
    if ifm_layout == "NHCWB16":
        ifm_shape = [
            int(math.ceil(n)) for n in np.matmul(
                nhwc_to_nhcwb16,
                ifm_shape + [
                    1,
                ],
            ).tolist()[:-1]
        ]
    if ofm_layout == "NHCWB16":
        ofm_shape = [
            int(math.ceil(n)) for n in np.matmul(
                nhwc_to_nhcwb16,
                ofm_shape + [
                    1,
                ],
            ).tolist()[:-1]
        ]
        order = [1, 2, 4, 3, 0]
    else:
        order = [1, 2, 3, 4]

    ifm = te.placeholder(ifm_shape, dtype="int8")
    lut = te.placeholder((), dtype="uint8")
    out = unary_elementwise_compute(
        ifm=ifm,
        lut=lut,
        operator_type=op_type,
        ifm_scale=1,
        ifm_zero_point=0,
        ofm_scale=1,
        ofm_zero_point=0,
        ofm_channels=ofm_channels,
        activation="NONE",
        clip_min=0,
        clip_max=0,
        rounding_mode="TFL",
        ifm_layout=ifm_layout,
        ofm_layout=ofm_layout,
    )
    ifm_propagator = out.op.attrs["ifm_propagator"]

    offset = [0] * len(ofm_shape)
    stripes = [0] * len(ofm_shape)
    output_stripe_config = cs.StripeConfig(ofm_shape, ofm_shape, ofm_shape,
                                           order, stripes, offset)

    ifm_transform = _make_matrices(ifm_layout, ofm_layout)

    device_config = cs.EthosuDeviceConfig("ethos-u55-256")
    part = match_ethosu_unary_elementwise(out, device_config)

    assert isinstance(part, cs.EthosuPart)
    assert len(part.propagators) == 1
    assert part.propagators[0].transform == ifm_transform

    propagated_ifm = ifm_propagator.propagate(output_stripe_config).shape

    # Layout conversions will align the propagated IFMs to the brick, i.e. 16
    # so the expected ifm_shape needs to be rounded up to 16
    if ifm_layout != ofm_layout:
        assert ifm_shape[:-1] == propagated_ifm[:-1]
        assert ((ifm_shape[-1] + 16 - 1) // 16) * 16 == propagated_ifm[-1]
    else:
        assert ifm_shape == propagated_ifm