diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6f5efe205..a26f3f64c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,6 +48,8 @@ jobs: - uses: actions/checkout@v2 with: submodules: 'recursive' + - name: Install local Python packages + run: pip install . - name: Build Software working-directory: target/snitch_cluster run: | @@ -81,6 +83,8 @@ jobs: - uses: actions/checkout@v2 with: submodules: 'recursive' + - name: Install local Python packages + run: pip install . - name: Build Software run: | bender vendor init diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index cbe6372b7..71495dc2e 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -48,6 +48,8 @@ jobs: - uses: actions/checkout@v4 with: submodules: true + - name: Install local Python packages + run: pip install . - name: Generate opcodes run: | ./util/generate-opcodes.sh diff --git a/.gitignore b/.gitignore index 2fc617106..5f7019e12 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ gmon.out # Installation directories /.venv/ /tools/ +/build/ +/snitch.egg-info/ diff --git a/iis-setup.sh b/iis-setup.sh index cf376b1a1..1fd813f66 100755 --- a/iis-setup.sh +++ b/iis-setup.sh @@ -23,6 +23,9 @@ TMPDIR=tmp pip install -r python-requirements.txt TMPDIR=tmp pip install -r $($BENDER path idma)/requirements.txt rm -rf tmp +# Install local packages in editable mode. +pip install -e . + # Install spike-dasm mkdir tools/ cd tools/ diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 000000000..9e1fc9597 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,19 @@ +# Copyright 2023 ETH Zurich and University of Bologna. +# Solderpad Hardware License, Version 0.51, see LICENSE for details. +# SPDX-License-Identifier: SHL-0.51 + +[build-system] +requires = ["setuptools>=42"] +build-backend = "setuptools.build_meta" + +[project] +name = "snitch" +authors = [ + {name = "Luca Colagrande", email = "colluca@iis.ee.ethz.ch"} +] +dynamic = ["version"] + +[tool.setuptools.package-dir] +"snitch.dnn" = "sw/dnn" +"snitch.blas" = "sw/blas" +"snitch.util" = "util" \ No newline at end of file diff --git a/sw/apps/atax/scripts/datagen.py b/sw/apps/atax/scripts/datagen.py index 4332ecc31..51317c70e 100755 --- a/sw/apps/atax/scripts/datagen.py +++ b/sw/apps/atax/scripts/datagen.py @@ -7,12 +7,9 @@ # Luca Colagrande import numpy as np -import os -import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -from data_utils import format_scalar_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper, DataGen # noqa: E402 +from snitch.util.sim.data_utils import format_scalar_definition, format_array_definition, \ + format_array_declaration, format_ifdef_wrapper, DataGen # AXI splits bursts crossing 4KB address boundaries. To minimize diff --git a/sw/apps/atax/scripts/verify.py b/sw/apps/atax/scripts/verify.py index cb5fe21f0..8a81033b0 100755 --- a/sw/apps/atax/scripts/verify.py +++ b/sw/apps/atax/scripts/verify.py @@ -7,11 +7,9 @@ import numpy as np import sys -from pathlib import Path from datagen import AtaxDataGen -sys.path.append(str(Path(__file__).parent / '../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 +from snitch.util.sim.verif_utils import Verifier class AtaxVerifier(Verifier): diff --git a/sw/apps/correlation/scripts/datagen.py b/sw/apps/correlation/scripts/datagen.py index a42fdfe33..b2047d5eb 100755 --- a/sw/apps/correlation/scripts/datagen.py +++ b/sw/apps/correlation/scripts/datagen.py @@ -7,12 +7,9 @@ # Luca Colagrande import numpy as np -import os -import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -from data_utils import format_scalar_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper, DataGen # noqa: E402 +from snitch.util.sim.data_utils import format_scalar_definition, format_array_definition, \ + format_array_declaration, format_ifdef_wrapper, DataGen # AXI splits bursts crossing 4KB address boundaries. To minimize diff --git a/sw/apps/correlation/scripts/verify.py b/sw/apps/correlation/scripts/verify.py index 67746af7f..7849f94e3 100755 --- a/sw/apps/correlation/scripts/verify.py +++ b/sw/apps/correlation/scripts/verify.py @@ -7,11 +7,9 @@ import numpy as np import sys -from pathlib import Path from datagen import CorrelationDataGen -sys.path.append(str(Path(__file__).parent / '../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 +from snitch.util.sim.verif_utils import Verifier class CorrelationVerifier(Verifier): diff --git a/sw/apps/covariance/scripts/datagen.py b/sw/apps/covariance/scripts/datagen.py index e62586a6d..44e20d55e 100755 --- a/sw/apps/covariance/scripts/datagen.py +++ b/sw/apps/covariance/scripts/datagen.py @@ -7,12 +7,9 @@ # Luca Colagrande import numpy as np -import os -import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -from data_utils import format_scalar_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper, DataGen # noqa: E402 +from snitch.util.sim.data_utils import format_scalar_definition, format_array_definition, \ + format_array_declaration, format_ifdef_wrapper, DataGen # AXI splits bursts crossing 4KB address boundaries. To minimize diff --git a/sw/apps/covariance/scripts/verify.py b/sw/apps/covariance/scripts/verify.py index aacbccdb1..4c5b0cdd1 100755 --- a/sw/apps/covariance/scripts/verify.py +++ b/sw/apps/covariance/scripts/verify.py @@ -7,11 +7,9 @@ import numpy as np import sys -from pathlib import Path from datagen import CovarianceDataGen -sys.path.append(str(Path(__file__).parent / '../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 +from snitch.util.sim.verif_utils import Verifier class CovarianceVerifier(Verifier): diff --git a/sw/blas/__init__.py b/sw/blas/__init__.py new file mode 100644 index 000000000..c3acdebaa --- /dev/null +++ b/sw/blas/__init__.py @@ -0,0 +1,9 @@ +# Copyright 2024 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 + +from . import gemm + +__all__ = ['gemm'] diff --git a/sw/blas/axpy/scripts/datagen.py b/sw/blas/axpy/scripts/datagen.py index 96b950194..117495391 100755 --- a/sw/blas/axpy/scripts/datagen.py +++ b/sw/blas/axpy/scripts/datagen.py @@ -6,12 +6,10 @@ # Author: Luca Colagrande import numpy as np -import os import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -from data_utils import format_scalar_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper, DataGen # noqa: E402 +from snitch.util.sim.data_utils import format_scalar_definition, format_array_definition, \ + format_array_declaration, format_ifdef_wrapper, DataGen class AxpyDataGen(DataGen): diff --git a/sw/blas/axpy/scripts/verify.py b/sw/blas/axpy/scripts/verify.py index 4568f9397..7c8d0819e 100755 --- a/sw/blas/axpy/scripts/verify.py +++ b/sw/blas/axpy/scripts/verify.py @@ -6,11 +6,9 @@ # Luca Colagrande import sys -from pathlib import Path from datagen import AxpyDataGen -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 +from snitch.util.sim.verif_utils import Verifier class AxpyVerifier(Verifier): diff --git a/sw/blas/dot/scripts/datagen.py b/sw/blas/dot/scripts/datagen.py index 01f36899e..01560c51f 100755 --- a/sw/blas/dot/scripts/datagen.py +++ b/sw/blas/dot/scripts/datagen.py @@ -4,12 +4,10 @@ # SPDX-License-Identifier: Apache-2.0 import numpy as np -import os import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -from data_utils import format_scalar_definition, format_array_definition, \ - format_scalar_declaration, format_ifdef_wrapper, DataGen # noqa: E402 +from snitch.util.sim.data_utils import format_scalar_definition, format_array_definition, \ + format_scalar_declaration, format_ifdef_wrapper, DataGen class DotDataGen(DataGen): diff --git a/sw/blas/dot/scripts/verify.py b/sw/blas/dot/scripts/verify.py index 9d61ff466..cc83af8e8 100755 --- a/sw/blas/dot/scripts/verify.py +++ b/sw/blas/dot/scripts/verify.py @@ -4,11 +4,9 @@ # SPDX-License-Identifier: Apache-2.0 import sys -from pathlib import Path from datagen import DotDataGen -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 +from snitch.util.sim.verif_utils import Verifier class DotVerifier(Verifier): diff --git a/sw/blas/gemm/__init__.py b/sw/blas/gemm/__init__.py index 19c2e2f01..8432d35c7 100644 --- a/sw/blas/gemm/__init__.py +++ b/sw/blas/gemm/__init__.py @@ -4,6 +4,6 @@ # # Luca Colagrande -from .scripts import datagen +from .scripts.datagen import GemmDataGen -__all__ = ['datagen'] +__all__ = ['GemmDataGen'] diff --git a/sw/blas/gemm/scripts/datagen.py b/sw/blas/gemm/scripts/datagen.py index 602a596ad..da7f8ba57 100755 --- a/sw/blas/gemm/scripts/datagen.py +++ b/sw/blas/gemm/scripts/datagen.py @@ -9,15 +9,13 @@ # Luca Colagrande import numpy as np -import os import re import pyflexfloat as ff import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import DataGen, format_array_declaration, format_struct_definition, \ - format_array_definition, format_ifdef_wrapper # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import DataGen, format_array_declaration, \ + format_struct_definition, format_array_definition, format_ifdef_wrapper np.random.seed(42) diff --git a/sw/blas/gemm/scripts/verify.py b/sw/blas/gemm/scripts/verify.py index b98e3854d..40840b327 100755 --- a/sw/blas/gemm/scripts/verify.py +++ b/sw/blas/gemm/scripts/verify.py @@ -7,12 +7,10 @@ import numpy as np import sys -from pathlib import Path from datagen import GemmDataGen -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 -from data_utils import ctype_from_precision_t # noqa: E402 +from snitch.util.sim.verif_utils import Verifier +from snitch.util.sim.data_utils import ctype_from_precision_t class GemmVerifier(Verifier): diff --git a/sw/dnn/batchnorm/scripts/datagen.py b/sw/dnn/batchnorm/scripts/datagen.py index 53dce77b4..61a236ab9 100755 --- a/sw/dnn/batchnorm/scripts/datagen.py +++ b/sw/dnn/batchnorm/scripts/datagen.py @@ -10,15 +10,11 @@ import argparse import pathlib import json5 -import sys -import os import torch -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import emit_license, format_struct_definition, \ + format_array_definition, format_array_declaration, format_ifdef_wrapper torch.manual_seed(42) diff --git a/sw/dnn/concat/scripts/datagen.py b/sw/dnn/concat/scripts/datagen.py index 4978a8ad7..a2b64795a 100755 --- a/sw/dnn/concat/scripts/datagen.py +++ b/sw/dnn/concat/scripts/datagen.py @@ -9,15 +9,11 @@ import numpy as np import pathlib import json5 -import sys -import os import torch -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import emit_license, format_struct_definition, \ + format_array_definition, format_array_declaration, format_ifdef_wrapper torch.manual_seed(42) diff --git a/sw/dnn/concat/scripts/verify.py b/sw/dnn/concat/scripts/verify.py index b5fdc9e83..f22ec9352 100755 --- a/sw/dnn/concat/scripts/verify.py +++ b/sw/dnn/concat/scripts/verify.py @@ -7,12 +7,10 @@ import sys import torch -from pathlib import Path from datagen import golden_model -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 -from data_utils import ctype_from_precision_t # noqa: E402 +from snitch.util.sim.verif_utils import Verifier +from snitch.util.sim.data_utils import ctype_from_precision_t class ConcatVerifier(Verifier): diff --git a/sw/dnn/conv2d/scripts/datagen.py b/sw/dnn/conv2d/scripts/datagen.py index 95ae015d0..0b93f1c59 100755 --- a/sw/dnn/conv2d/scripts/datagen.py +++ b/sw/dnn/conv2d/scripts/datagen.py @@ -9,15 +9,11 @@ import numpy as np import pathlib import hjson -import sys -import os import torch -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_array_declaration # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import emit_license, format_struct_definition, \ + format_array_definition, format_array_declaration torch.manual_seed(42) diff --git a/sw/dnn/conv2d/scripts/verify.py b/sw/dnn/conv2d/scripts/verify.py index 92a8b167f..28b5bfb16 100755 --- a/sw/dnn/conv2d/scripts/verify.py +++ b/sw/dnn/conv2d/scripts/verify.py @@ -11,10 +11,9 @@ import torch from data.datagen import golden_model -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -import verification # noqa: E402 -from elf import Elf # noqa: E402 -from data_utils import from_buffer, ctype_from_precision_t, check_result # noqa: E402 +from snitch.util.sim import verification +from snitch.util.sim.elf import Elf +from snitch.util.sim.data_utils import from_buffer, ctype_from_precision_t, check_result ERR_THRESHOLD = 1E-6 @@ -77,8 +76,8 @@ def main(): fail, rel_err = check_result(output_golden, output_actual, rtol=ERR_THRESHOLD) if fail: verification.dump_results_to_csv( - [output_golden, output_actual, rel_err], - Path.cwd() / 'results.csv') + [output_golden, output_actual, rel_err], + Path.cwd() / 'results.csv') print('Maximum relative error:', np.max(rel_err)) return int(fail) diff --git a/sw/dnn/flashattention_2/scripts/datagen.py b/sw/dnn/flashattention_2/scripts/datagen.py index 841fa3110..f54ff7a08 100755 --- a/sw/dnn/flashattention_2/scripts/datagen.py +++ b/sw/dnn/flashattention_2/scripts/datagen.py @@ -10,18 +10,13 @@ import numpy as np import pathlib import json5 -import sys -import os import torch import pyflexfloat as ff -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../blas/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_array_declaration # noqa: E402 -import gemm # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import format_struct_definition, \ + format_array_definition, format_array_declaration, emit_license +from snitch.blas import gemm np.random.seed(42) torch.manual_seed(42) @@ -117,13 +112,13 @@ def exact_flexfloat_golden_model(Q, K, V, B_r, B_c, desc): V_j = V[start_col:end_col,] # Compute O tile update S_ij = ff.array(np.zeros((B_r, B_c)), desc) - S_ij = gemm.datagen.GemmDataGen().exact_golden_model(1, Q_i, K_t_j, 0, S_ij) + S_ij = gemm.GemmDataGen().exact_golden_model(1, Q_i, K_t_j, 0, S_ij) m_i_prev = m_i m_i = np.maximum(m_i_prev, np.max(S_ij, 1, keepdims=True)) shifted_exp = np.exp((m_i_prev.astype(np.float32) - m_i.astype(np.float32))) P_ij = np.exp((S_ij - m_i).astype(np.float32)) PxV = ff.array(np.zeros((B_r, d)), desc) - PxV = gemm.datagen.GemmDataGen().exact_golden_model(1, P_ij, V_j, 0, PxV) + PxV = gemm.GemmDataGen().exact_golden_model(1, P_ij, V_j, 0, PxV) row_sum = np.sum(P_ij.astype(np.float32), 1, keepdims=True) if j == 0: l_i = row_sum @@ -169,20 +164,20 @@ def validate_config(L, S, d, B_r, B_c, dtype, baseline, gemm_impl): data_utils.validate_tcdm_footprint(total_size) # Q*K^t - gemm.datagen.GemmDataGen().validate_config( + gemm.GemmDataGen().validate_config( gemm_fp=gemm_impl, parallelize_m=0, parallelize_k=0, m_tiles=1, n_tiles=1, k_tiles=1, transa=0, transb=1, M=B_r, N=B_c, K=d, beta=0 ) # P*V if baseline: - gemm.datagen.GemmDataGen().validate_config( + gemm.GemmDataGen().validate_config( gemm_fp=gemm_impl, parallelize_m=0, parallelize_k=0, m_tiles=1, n_tiles=1, k_tiles=1, transa=0, transb=0, M=B_r, N=d, K=B_c, beta=1 ) else: # P*(V^t)^t - gemm.datagen.GemmDataGen().validate_config( + gemm.GemmDataGen().validate_config( gemm_fp=gemm_impl, parallelize_m=0, parallelize_k=0, m_tiles=1, n_tiles=1, k_tiles=1, transa=0, transb=1, M=B_r, N=d, K=B_c, beta=1 ) diff --git a/sw/dnn/flashattention_2/scripts/verify.py b/sw/dnn/flashattention_2/scripts/verify.py index 4b979286d..27e268d2a 100755 --- a/sw/dnn/flashattention_2/scripts/verify.py +++ b/sw/dnn/flashattention_2/scripts/verify.py @@ -7,13 +7,11 @@ import numpy as np import sys -from pathlib import Path from datagen import exact_flexfloat_golden_model import pyflexfloat as ff -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 -from data_utils import ctype_from_precision_t, ff_desc_from_precision_t # noqa: E402 +from snitch.util.sim.verif_utils import Verifier +from snitch.util.sim.data_utils import ctype_from_precision_t, ff_desc_from_precision_t class FlashAttention2Verifier(Verifier): diff --git a/sw/dnn/fused_concat_linear/scripts/datagen.py b/sw/dnn/fused_concat_linear/scripts/datagen.py index 1631eb6a4..5c7fdc62e 100755 --- a/sw/dnn/fused_concat_linear/scripts/datagen.py +++ b/sw/dnn/fused_concat_linear/scripts/datagen.py @@ -4,22 +4,19 @@ # SPDX-License-Identifier: Apache-2.0 # # Luca Colagrande +# Viviane Potocnik import argparse import numpy as np import pathlib import json5 -import sys -import os import torch -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import format_struct_definition, \ + format_array_definition, format_array_declaration, format_ifdef_wrapper, \ + emit_license -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 diff --git a/sw/dnn/fused_concat_linear/scripts/verify.py b/sw/dnn/fused_concat_linear/scripts/verify.py index 1b9a37535..0bb898a0c 100755 --- a/sw/dnn/fused_concat_linear/scripts/verify.py +++ b/sw/dnn/fused_concat_linear/scripts/verify.py @@ -4,15 +4,14 @@ # SPDX-License-Identifier: Apache-2.0 # # Luca Colagrande +# Viviane Potocnik import sys import torch -from pathlib import Path from datagen import golden_model -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 -from data_utils import ctype_from_precision_t # noqa: E402 +from snitch.util.sim.verif_utils import Verifier +from snitch.util.sim.data_utils import ctype_from_precision_t class FusedConcatLinearVerifier(Verifier): diff --git a/sw/dnn/fusedconv/scripts/datagen.py b/sw/dnn/fusedconv/scripts/datagen.py index 3b06c5095..f3f3c718c 100755 --- a/sw/dnn/fusedconv/scripts/datagen.py +++ b/sw/dnn/fusedconv/scripts/datagen.py @@ -8,15 +8,11 @@ import argparse import pathlib import hjson -import sys -import os import torch -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_scalar_definition, format_array_declaration # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import emit_license, format_struct_definition, \ + format_array_definition, format_scalar_definition, format_array_declaration torch.manual_seed(42) @@ -62,8 +58,8 @@ def golden_model(ifmap, weights, bn_k, bn_l, padding, stride, bn, relu, accumula for c in range(co): ofmap[h//stride['stride_y'], w//stride['stride_x'], c] = torch.dot( - ifmap_padded[h:h+fh, w:w+fw, c].flatten(), - weights[:, :, c].flatten()) + ifmap_padded[h:h+fh, w:w+fw, c].flatten(), + weights[:, :, c].flatten()) else: # Conv2d for h in range(0, ifmap_padded.shape[0] - (fh - 1), stride['stride_y']): @@ -71,8 +67,8 @@ def golden_model(ifmap, weights, bn_k, bn_l, padding, stride, bn, relu, accumula for c in range(co): ofmap[h//stride['stride_y'], w//stride['stride_x'], c] = torch.dot( - ifmap_padded[h:h+fh, w:w+fw].flatten(), - weights[c].flatten()) + ifmap_padded[h:h+fh, w:w+fw].flatten(), + weights[c].flatten()) ofmap += ofmap_before diff --git a/sw/dnn/fusedconv/scripts/verify.py b/sw/dnn/fusedconv/scripts/verify.py index ef37a4178..171ec02d1 100755 --- a/sw/dnn/fusedconv/scripts/verify.py +++ b/sw/dnn/fusedconv/scripts/verify.py @@ -11,10 +11,9 @@ import torch from data.datagen import golden_model -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -import verification # noqa: E402 -from elf import Elf # noqa: E402 -from data_utils import from_buffer, ctype_from_precision_t, check_result # noqa: E402 +from snitch.util.sim import verification +from snitch.util.sim.elf import Elf +from snitch.util.sim.data_utils import from_buffer, ctype_from_precision_t, check_result ERR_THRESHOLD = 1E-6 @@ -110,8 +109,8 @@ def main(): fail, rel_err = check_result(output_golden, output_actual, rtol=ERR_THRESHOLD) if fail: verification.dump_results_to_csv( - [output_golden, output_actual, rel_err], - Path.cwd() / 'results.csv') + [output_golden, output_actual, rel_err], + Path.cwd() / 'results.csv') print('Maximum relative error:', np.max(rel_err)) return int(fail) diff --git a/sw/dnn/gelu/scripts/datagen.py b/sw/dnn/gelu/scripts/datagen.py index a875288ca..5a4084fb0 100755 --- a/sw/dnn/gelu/scripts/datagen.py +++ b/sw/dnn/gelu/scripts/datagen.py @@ -10,15 +10,12 @@ import argparse import pathlib import json5 -import sys -import os import torch -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import format_struct_definition, \ + format_array_definition, format_array_declaration, format_ifdef_wrapper, \ + emit_license torch.manual_seed(42) diff --git a/sw/dnn/gelu/scripts/verify.py b/sw/dnn/gelu/scripts/verify.py index a3a3aad99..d58bdf8af 100755 --- a/sw/dnn/gelu/scripts/verify.py +++ b/sw/dnn/gelu/scripts/verify.py @@ -7,12 +7,10 @@ import sys import torch -from pathlib import Path from datagen import golden_model -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 -from data_utils import ctype_from_precision_t # noqa: E402 +from snitch.util.sim.verif_utils import Verifier +from snitch.util.sim.data_utils import ctype_from_precision_t class GeluVerifier(Verifier): diff --git a/sw/dnn/layernorm/scripts/datagen.py b/sw/dnn/layernorm/scripts/datagen.py index 8aa854d8d..37f1fb3b5 100755 --- a/sw/dnn/layernorm/scripts/datagen.py +++ b/sw/dnn/layernorm/scripts/datagen.py @@ -10,17 +10,15 @@ import argparse import pathlib import json5 -import sys -import os import torch import numpy as np import pyflexfloat as ff -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, format_array_declaration, format_struct_definition, \ - format_array_definition, format_ifdef_wrapper # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import format_array_declaration, \ + format_struct_definition, format_array_definition, format_ifdef_wrapper, \ + emit_license torch.manual_seed(42) diff --git a/sw/dnn/layernorm/scripts/verify.py b/sw/dnn/layernorm/scripts/verify.py index 235edcf44..46d0efe60 100755 --- a/sw/dnn/layernorm/scripts/verify.py +++ b/sw/dnn/layernorm/scripts/verify.py @@ -7,12 +7,10 @@ # Viviane Potocnik import sys -from pathlib import Path from datagen import golden_model -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 -from data_utils import ctype_from_precision_t # noqa: E402 +from snitch.util.sim.verif_utils import Verifier +from snitch.util.sim.data_utils import ctype_from_precision_t class LayernormVerifier(Verifier): diff --git a/sw/dnn/maxpool/scripts/datagen.py b/sw/dnn/maxpool/scripts/datagen.py index e83f733b8..4cda2fc4a 100755 --- a/sw/dnn/maxpool/scripts/datagen.py +++ b/sw/dnn/maxpool/scripts/datagen.py @@ -10,15 +10,11 @@ import argparse import pathlib import json5 -import sys -import os import torch -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import emit_license, format_struct_definition, \ + format_array_definition, format_array_declaration, format_ifdef_wrapper torch.manual_seed(42) diff --git a/sw/dnn/softmax/scripts/datagen.py b/sw/dnn/softmax/scripts/datagen.py index 59c96b9f2..60d4dc62c 100755 --- a/sw/dnn/softmax/scripts/datagen.py +++ b/sw/dnn/softmax/scripts/datagen.py @@ -10,15 +10,11 @@ import argparse import pathlib import json5 -import sys -import os import torch -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -import data_utils # noqa: E402 -from data_utils import emit_license, \ - format_struct_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper # noqa: E402 +from snitch.util.sim import data_utils +from snitch.util.sim.data_utils import emit_license, format_struct_definition, \ + format_array_definition, format_array_declaration, format_ifdef_wrapper torch.manual_seed(42) diff --git a/sw/dnn/softmax/scripts/verify.py b/sw/dnn/softmax/scripts/verify.py index 0ad1df653..718a9e33c 100755 --- a/sw/dnn/softmax/scripts/verify.py +++ b/sw/dnn/softmax/scripts/verify.py @@ -4,15 +4,14 @@ # SPDX-License-Identifier: Apache-2.0 # # Luca Colagrande +# Viviane Potocnik import sys import torch -from pathlib import Path from datagen import golden_model -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 -from data_utils import ctype_from_precision_t # noqa: E402 +from snitch.util.sim.verif_utils import Verifier +from snitch.util.sim.data_utils import ctype_from_precision_t class SoftmaxVerifier(Verifier): diff --git a/sw/dnn/transpose/scripts/datagen.py b/sw/dnn/transpose/scripts/datagen.py index 2ea51eea2..64ab02cad 100755 --- a/sw/dnn/transpose/scripts/datagen.py +++ b/sw/dnn/transpose/scripts/datagen.py @@ -8,14 +8,12 @@ # Luca Colagrande import numpy as np -import os import pyflexfloat as ff import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../../../util/sim/")) -from data_utils import ctype_from_precision_t, ff_desc_from_precision_t, \ - format_struct_definition, format_array_definition, \ - format_array_declaration, format_ifdef_wrapper, DataGen # noqa: E402 +from snitch.util.sim.data_utils import ctype_from_precision_t, ff_desc_from_precision_t, \ + format_struct_definition, format_array_definition, format_array_declaration, \ + format_ifdef_wrapper, DataGen np.random.seed(42) diff --git a/sw/dnn/transpose/scripts/verify.py b/sw/dnn/transpose/scripts/verify.py index 823e397a0..145a6fb7f 100755 --- a/sw/dnn/transpose/scripts/verify.py +++ b/sw/dnn/transpose/scripts/verify.py @@ -6,12 +6,10 @@ # Luca Colagrande import sys -from pathlib import Path from datagen import TransposeDataGen -sys.path.append(str(Path(__file__).parent / '../../../../util/sim/')) -from verif_utils import Verifier # noqa: E402 -from data_utils import ctype_from_precision_t # noqa: E402 +from snitch.util.sim.verif_utils import Verifier +from snitch.util.sim.data_utils import ctype_from_precision_t class TransposeVerifier(Verifier): diff --git a/util/sim/Elf.py b/util/sim/Elf.py index 0e2f530df..27c73074b 100644 --- a/util/sim/Elf.py +++ b/util/sim/Elf.py @@ -6,7 +6,7 @@ from elftools.elf.elffile import ELFFile from elftools.elf.sections import SymbolTableSection -from data_utils import from_buffer +from snitch.util.sim.data_utils import from_buffer class Elf(object): diff --git a/target/common/test/SnitchSim.py b/util/sim/SnitchSim.py similarity index 100% rename from target/common/test/SnitchSim.py rename to util/sim/SnitchSim.py diff --git a/util/sim/__init__.py b/util/sim/__init__.py new file mode 100644 index 000000000..73aca9ddc --- /dev/null +++ b/util/sim/__init__.py @@ -0,0 +1,9 @@ +# Copyright 2024 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 + +from .SnitchSim import SnitchSim + +__all__ = ['SnitchSim'] diff --git a/util/sim/verif_utils.py b/util/sim/verif_utils.py index 2a948fbfa..90f4c6129 100644 --- a/util/sim/verif_utils.py +++ b/util/sim/verif_utils.py @@ -7,16 +7,14 @@ """Convenience functions and classes for verification scripts.""" -import sys import argparse import numpy as np import csv -from Elf import Elf from pathlib import Path -from data_utils import flatten, from_buffer -sys.path.append(str(Path(__file__).parent / '../../target/common/test/')) -from SnitchSim import SnitchSim # noqa: E402 +from snitch.util.sim.Elf import Elf +from snitch.util.sim.data_utils import flatten, from_buffer +from snitch.util.sim import SnitchSim def dump_results_to_csv(expected_results, actual_results, error, max_error, path): diff --git a/util/trace/a2l.py b/util/trace/a2l.py index c62633739..ad345a011 100644 --- a/util/trace/a2l.py +++ b/util/trace/a2l.py @@ -103,7 +103,7 @@ def __init__(self, elf, a2l_binary='addr2line'): @lru_cache(maxsize=1024) def addr2line(self, addr): - if type(addr) == str: + if isinstance(addr, str): addr = int(addr, 16) cmd = f'{self.a2l} -e {self.elf} -f -i {addr:x}' return Addr2LineOutput(os.popen(cmd).read())