Skip to content

Commit

Permalink
sw: correct verification script (kernel still failing)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viviane Potocnik committed Feb 7, 2024
1 parent b012338 commit 8af1e87
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 17 deletions.
38 changes: 26 additions & 12 deletions sw/dnn/conv2d/data/datagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,37 @@
import data_utils # noqa: E402
from data_utils import emit_license, \
format_struct_definition, format_array_definition, \
format_array_declaration, format_ifdef_wrapper, NUMPY_T # noqa: E402
format_array_declaration # noqa: E402

torch.manual_seed(42)

# AXI splits bursts crossing 4KB address boundaries. To minimize
# the occurrence of these splits the data should be aligned to 4KB
BURST_ALIGNMENT = 4096


# Conv2D
def golden_model(inputs, filters, **kwargs):
return torch.nn.functional.conv2d(inputs, filters[0], **kwargs)
return torch.nn.functional.conv2d(inputs, filters, **kwargs)


def emit_header(**kwargs):
in_channels = kwargs['channels']['in']
out_channels = kwargs['channels']['out']
input_dim = kwargs['input_dim'] # [mini_batch, height, width]
filter = kwargs['filter'] # [height, width, padding, stride]
input_dim = kwargs['input_dim'] # [mini_batch, height, width]
filter = kwargs['filter'] # [height, width, padding, stride]
prec = kwargs['prec']

torch_type = data_utils.floating_point_torch_type(prec)

inputs = [torch.rand(in_channels, input_dim['height'], input_dim['width'],
requires_grad=False, dtype=torch_type)][0]

filters = [torch.rand(out_channels, in_channels, filter['height'], filter['width'],
requires_grad=False, dtype=torch_type)]
filters = [torch.rand(out_channels, in_channels, filter['height'],
filter['width'], requires_grad=False, dtype=torch_type)]

conv2d_output = golden_model(inputs, filters, padding=filter['padding'], stride=filter['stride'])
conv2d_output = golden_model(inputs, filters[0],
padding=filter['padding'], stride=filter['stride'])

# compute checksum row-wise
conv2d_checksum = np.sum(conv2d_output.numpy(), axis=1)
Expand All @@ -61,17 +63,29 @@ def emit_header(**kwargs):
'OW': conv2d_output.shape[2],
'FH': filter['height'],
'FW': filter['width'],
'ifmap': 'conv2d_ifmap_dram',
'weights': 'conv2d_weights_dram',
'ofmap': 'conv2d_ofmap_dram',
'dtype': 'FP' + prec
}

data_str = [emit_license()]
data_str += [format_array_declaration(ctype, 'conv2d_ifmap_dram',
inputs.numpy().shape, BURST_ALIGNMENT)]
data_str += [format_array_declaration(ctype, 'conv2d_weights_dram',
filters[0].numpy().shape, BURST_ALIGNMENT)]
data_str += [format_array_declaration(ctype, 'conv2d_ofmap_dram',
conv2d_output.numpy().shape, BURST_ALIGNMENT)]
data_str += [format_struct_definition('conv_layer', 'layer', layer_cfg)]
data_str += [format_array_definition(ctype, f'conv2d_ifmap_dram', inputs.numpy(), BURST_ALIGNMENT)]
data_str += [format_array_definition(ctype, f'conv2d_weights_dram', filters[0].numpy(), BURST_ALIGNMENT)]
data_str += [format_array_definition(ctype, f'conv2d_ofmap_dram', conv2d_output.numpy(), BURST_ALIGNMENT)]
data_str += [format_array_definition(ctype, f'conv2d_checksum', conv2d_checksum, BURST_ALIGNMENT)]
data_str += [format_array_definition(ctype, 'conv2d_ifmap_dram',
inputs.numpy(), BURST_ALIGNMENT)]
data_str += [format_array_definition(ctype, 'conv2d_weights_dram',
filters[0].numpy(), BURST_ALIGNMENT)]
data_str += [format_array_definition(ctype, 'conv2d_ofmap_dram',
conv2d_output.numpy(), BURST_ALIGNMENT)]
data_str += [format_array_definition(ctype, 'conv2d_checksum',
conv2d_checksum, BURST_ALIGNMENT)]


data_str = '\n\n'.join(data_str)

return data_str
Expand Down
2 changes: 1 addition & 1 deletion sw/dnn/conv2d/src/conv2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -2113,7 +2113,7 @@ void conv2d_layer(const conv_layer *l) {
uint32_t cluster_num = snrt_cluster_num();
uint32_t cluster_id = snrt_cluster_idx();
uint32_t compute_num = snrt_cluster_compute_core_num();
uint32_t compute_id = snrt_cluster_compute_core_num();
uint32_t compute_id = snrt_global_core_idx();

const uint32_t cluster_per_quadrant = min(4, cluster_num);

Expand Down
22 changes: 18 additions & 4 deletions sw/dnn/conv2d/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
from pathlib import Path
import numpy as np
import torch
from data.datagen import golden_model

sys.path.append(str(Path(__file__).parent / '../../../util/sim/'))
Expand All @@ -27,7 +28,7 @@ def main():
snitch_bin=args.snitch_bin,
symbols_bin=args.symbols_bin,
log=args.log,
output_uids=['linear_output'])
output_uids=['conv2d_ofmap_dram'])

# Extract input operands from ELF file
if args.symbols_bin:
Expand All @@ -44,6 +45,15 @@ def main():
'OW': 'I',
'FH': 'I',
'FW': 'I',
'pad': 'I',
'ifmap': 'I',
'weights': 'I',
'ofmap': 'I',
'TILE_CI': 'I',
'cluster2cluster': 'I',
'im2col': 'I',
'gamma': 'I',
'beta': 'I',
'dtype': 'I'
}

Expand All @@ -52,17 +62,21 @@ def main():
elf.get_symbol_contents('conv2d_ifmap_dram'),
PRECISION_T[layer['dtype']]),
dtype=NUMPY_T[PRECISION_T[layer['dtype']]])]
inputs = torch.from_numpy(
inputs[0].reshape(layer['CI'], layer['IH'], layer['IW']))
filters = [np.array(bytes_to_float(
elf.get_symbol_contents('conv2d_weights_dram'),
PRECISION_T[layer['dtype']]),
dtype=NUMPY_T[PRECISION_T[layer['dtype']]])]

filters = torch.from_numpy(
filters[0].reshape(
layer['CO'], layer['CI'], layer['FH'], layer['FW']))
# Verify results
output_actual = np.array(bytes_to_float(
raw_results['ofmap'],
raw_results['conv2d_ofmap_dram'],
PRECISION_T[layer['dtype']]),
dtype=NUMPY_T[PRECISION_T[layer['dtype']]])
output_golden, _ = golden_model(inputs, filters, padding=0, stride=1)
output_golden = golden_model(inputs, filters, padding=1, stride=1)
output_golden = output_golden.detach().numpy().flatten()

relative_err = np.absolute((output_golden - output_actual) / output_golden)
Expand Down

0 comments on commit 8af1e87

Please sign in to comment.