-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
355 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Copyright 2023 ETH Zurich and University of Bologna. | ||
# Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Luca Colagrande <[email protected]> | ||
|
||
# Usage of absolute paths is required to externally include this Makefile | ||
MK_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) | ||
|
||
DATA_DIR := $(realpath $(MK_DIR)/$(APP)/data) | ||
SRC_DIR := $(realpath $(MK_DIR)/$(APP)/src) | ||
COMMON_SRC_DIR := $(realpath $(MK_DIR)/src) | ||
|
||
DATA_CFG ?= $(DATA_DIR)/params.hjson | ||
SECTION ?= | ||
|
||
SRCS ?= $(realpath $(SRC_DIR)/main.c) | ||
INCDIRS ?= $(DATA_DIR) $(SRC_DIR) $(COMMON_SRC_DIR) | ||
|
||
DATAGEN_PY := $(DATA_DIR)/datagen.py | ||
DATA_H := $(DATA_DIR)/data.h | ||
|
||
$(DATA_H): $(DATAGEN_PY) $(DATA_CFG) | ||
$< -c $(DATA_CFG) --section="$(SECTION)" > $@ | ||
|
||
.PHONY: clean-data clean | ||
|
||
clean-data: | ||
rm -f $(DATA_H) | ||
|
||
clean: clean-data |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
data/data.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2023 ETH Zurich and University of Bologna. | ||
# Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Luca Colagrande <[email protected]> | ||
|
||
import argparse | ||
import pathlib | ||
import hjson | ||
import sys | ||
import os | ||
import torch | ||
|
||
sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) | ||
from data_utils import emit_license, format_scalar_definition, \ | ||
format_struct_definition, format_array_definition, \ | ||
format_array_declaration, format_ifdef_wrapper # noqa: E402 | ||
import data_utils | ||
|
||
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 | ||
|
||
|
||
def golden_model(ifmap, eps, shape, prec): | ||
dtype = data_utils.floating_point_torch_type(prec) | ||
ln = torch.nn.LayerNorm(shape, eps=eps)#, dtype=dtype) | ||
return ln(ifmap) | ||
|
||
|
||
def emit_header(**kwargs): | ||
batch_size = kwargs['input_dim']['batch_size'] | ||
seq_len = kwargs['input_dim']['seq_len'] | ||
embeddings = kwargs['input_dim']['embeddings'] | ||
eps = kwargs['eps'] | ||
prec = str(kwargs['prec']) | ||
|
||
torch_type = data_utils.floating_point_torch_type(prec) | ||
ifmap = torch.randn(batch_size, seq_len, embeddings, requires_grad=False, dtype=torch_type) | ||
|
||
ofmap = golden_model(ifmap, eps, embeddings, prec) | ||
ofmap = ofmap.detach().numpy() | ||
|
||
ctype = data_utils.floating_point_ctype(prec) | ||
checksum = torch.sum(ifmap, dim=-1) | ||
|
||
ifmap_uid = 'ifmap' | ||
ofmap_uid = 'ofmap' | ||
|
||
layer_cfg = { | ||
**kwargs['input_dim'], | ||
'ifmap': ifmap_uid, | ||
'ofmap': ofmap_uid | ||
} | ||
|
||
data_str = [emit_license()] | ||
data_str += [format_array_declaration(ctype, ifmap_uid, ifmap.shape, | ||
alignment=BURST_ALIGNMENT)] | ||
data_str += [format_array_declaration(ctype, ofmap_uid, ofmap.shape, | ||
alignment=BURST_ALIGNMENT)] | ||
data_str += [format_struct_definition('layernorm_layer_t', 'layer', layer_cfg)] | ||
data_str += [format_array_definition(ctype, ifmap_uid, ifmap, | ||
alignment=BURST_ALIGNMENT)] | ||
result_def = format_array_definition(ctype, 'golden', ofmap, alignment=BURST_ALIGNMENT) | ||
data_str += [format_ifdef_wrapper('BIST', result_def)] | ||
data_str = '\n\n'.join(data_str) | ||
|
||
return data_str | ||
|
||
|
||
def main(): | ||
|
||
parser = argparse.ArgumentParser(description='Generate data for layernorm kernel') | ||
parser.add_argument( | ||
"-c", "--cfg", | ||
type=pathlib.Path, | ||
required=True, | ||
help='Select param config file kernel' | ||
) | ||
parser.add_argument( | ||
'--section', | ||
type=str, | ||
help='Section to store matrices in') | ||
args = parser.parse_args() | ||
|
||
# Load param config file | ||
with args.cfg.open() as f: | ||
param = hjson.loads(f.read()) | ||
param['section'] = args.section | ||
|
||
# Emit header file | ||
print(emit_header(**param)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
5 changes: 1 addition & 4 deletions
5
...er/sw/apps/dnn/layernorm/src/params.hjson → sw/dnn/layernorm/data/params.hjson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.