def _make_matrices(broadcast, ifm_layout, ifm2_layout, ofm_layout, ofm_channels): broadcast_h, broadcast_w, broadcast_c = broadcast nhwc_to_nhcwb16, nhcwb16_to_nhwc = get_layout_transform_matrices( ofm_channels) ifm_matrix = [ [1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], ] ifm2_matrix = [ [1, 0, 0, 0, 0], [0, (1 - broadcast_h), 0, 0, broadcast_h], [0, 0, (1 - broadcast_w), 0, broadcast_w], [0, 0, 0, (1 - broadcast_c), broadcast_c], [0, 0, 0, 0, 1], ] if ofm_layout == "NHCWB16": ifm_matrix = np.matmul(ifm_matrix, nhcwb16_to_nhwc).tolist() ifm2_matrix = np.matmul(ifm2_matrix, nhcwb16_to_nhwc).tolist() if ifm_layout == "NHCWB16": ifm_matrix = np.matmul(nhwc_to_nhcwb16, ifm_matrix).tolist() if ifm2_layout == "NHCWB16": ifm2_matrix = np.matmul(nhwc_to_nhcwb16, ifm2_matrix).tolist() return (ifm_matrix, ifm2_matrix)
def _make_matrices(ifm_layout, ofm_layout, ofm_channels): nhwc_to_nhcwb16, nhcwb16_to_nhwc = get_layout_transform_matrices(ofm_channels) ifm_matrix = [ [1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], ] if ofm_layout == "NHCWB16": ifm_matrix = np.matmul(ifm_matrix, nhcwb16_to_nhwc).tolist() if ifm_layout == "NHCWB16": ifm_matrix = np.matmul(nhwc_to_nhcwb16, ifm_matrix).tolist() return ifm_matrix
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
def make_matrices( op_type, kernel, stride, padding, ifm_layout, ofm_layout, dilation=(1, 1), ifm_channels=1, ofm_channels=1, ): kernel_h, kernel_w = kernel stride_h, stride_w = stride dilation_h, dilation_w = dilation dilated_kernel_h = (kernel_h - 1) * dilation_h + 1 dilated_kernel_w = (kernel_w - 1) * dilation_w + 1 nhwc_to_nhcwb16, nhcwb16_to_nhwc = get_layout_transform_matrices(ofm_channels) if op_type == "ethosu_conv2d": ifm_matrix = [ [1, 0, 0, 0, 0], [0, stride_h, 0, 0, (dilated_kernel_h - stride_h)], [0, 0, stride_w, 0, (dilated_kernel_w - stride_w)], [0, 0, 0, 0, ifm_channels], [0, 0, 0, 0, 1], ] weight_matrix = [ [0, 0, 0, 1, 0], [0, 0, 0, 0, kernel_h], [0, 0, 0, 0, kernel_w], [0, 0, 0, 0, ifm_channels], [0, 0, 0, 0, 1], ] elif op_type == "ethosu_depthwise_conv2d": ifm_matrix = [ [1, 0, 0, 0, 0], [0, stride_h, 0, 0, (dilated_kernel_h - stride_h)], [0, 0, stride_w, 0, (dilated_kernel_w - stride_w)], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], ] weight_matrix = [ [0, 0, 0, 1, 0], [0, 0, 0, 0, kernel_h], [0, 0, 0, 0, kernel_w], [0, 0, 0, 0, 1], [0, 0, 0, 0, 1], ] elif op_type == "ethosu_pooling": ifm_matrix = [ [1, 0, 0, 0, 0], [0, stride_h, 0, 0, (dilated_kernel_h - stride_h)], [0, 0, stride_w, 0, (dilated_kernel_w - stride_w)], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1], ] weight_matrix = [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], ] scale_bias_matrix = [ [0, 0, 0, 1, 0], [0, 0, 0, 0, 10], [0, 0, 0, 0, 1], ] if ofm_layout == "NHCWB16": ifm_matrix = np.matmul(ifm_matrix, nhcwb16_to_nhwc).tolist() weight_matrix = np.matmul(weight_matrix, nhcwb16_to_nhwc).tolist() scale_bias_matrix = np.matmul(scale_bias_matrix, nhcwb16_to_nhwc).tolist() if ifm_layout == "NHCWB16": ifm_matrix = np.matmul(nhwc_to_nhcwb16, ifm_matrix).tolist() ifm_offset = ( [0, -padding[0], -padding[1], 0] if ifm_layout == "NHWC" else [0, -padding[0], 0, -padding[1], 0] ) weight_offset = [0, 0, 0, 0] scale_bias_offset = [0, 0] return ( ifm_matrix, ifm_offset, weight_matrix, weight_offset, scale_bias_matrix, scale_bias_offset, )
def test_best_block_config( test_id, op_type, activation, kernel, stride, dilation, padding, in_shape, out_shape, layouts, acc_config, expected_block_configs, ): ofm_channels = out_shape[3] ifm_channels = in_shape[3] nhwc_to_nhcwb16, _ = get_layout_transform_matrices(ofm_channels) ifm_matrix, ifm_offset, weight_matrix, weight_offset, _, _ = make_matrices( op_type, kernel, stride, padding, layouts[0], layouts[1], dilation, ifm_channels, ofm_channels, ) 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, ) # Add tensors input_tensor = cs.Tensor(in_shape, "int8") part.set_input(0, input_tensor) if op_type == "ethosu_conv2d": weight_tensor = cs.Tensor( [ofm_channels, kernel[0], kernel[1], ifm_channels], "int8") part.set_input(1, weight_tensor) elif op_type == "ethosu_depthwise_conv2d": weight_tensor = cs.Tensor([ofm_channels, kernel[0], kernel[1], 1], "int8") part.set_input(1, weight_tensor) output_tensor = cs.Tensor(out_shape, "int8") part.set_output(output_tensor) 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]