From 542b5d1d6d473d26757e68e5175e7b2540b960ef Mon Sep 17 00:00:00 2001 From: telbayad Date: Wed, 8 Jan 2025 16:28:25 +0100 Subject: [PATCH] Revert "apply Victor's comment" This reverts commit 1034298382719fec692c7130029b1cce9212521f. --- Deeploy/Targets/PULPOpen/Bindings.py | 8 +-- .../Templates/RQAddTemplate.py | 4 +- Deeploy/Targets/Snitch/Bindings.py | 6 +- .../Targets/Snitch/{Parsers.py => Parser.py} | 0 .../Targets/Snitch/Templates/RQAddTemplate.py | 71 +++++++++++++++++++ 5 files changed, 80 insertions(+), 9 deletions(-) rename Deeploy/Targets/{Generic => PULPOpen}/Templates/RQAddTemplate.py (96%) rename Deeploy/Targets/Snitch/{Parsers.py => Parser.py} (100%) create mode 100644 Deeploy/Targets/Snitch/Templates/RQAddTemplate.py diff --git a/Deeploy/Targets/PULPOpen/Bindings.py b/Deeploy/Targets/PULPOpen/Bindings.py index da232f6f..4026faa7 100644 --- a/Deeploy/Targets/PULPOpen/Bindings.py +++ b/Deeploy/Targets/PULPOpen/Bindings.py @@ -36,7 +36,7 @@ from Deeploy.DeeployTypes import CodeTransformation, NodeBinding, NodeTemplate from Deeploy.FutureExtension.Bindings.AutoFutureBinding import AutoFutureBinding from Deeploy.FutureExtension.CodeTransformationPasses.FutureCodeTransformation import FutureGeneration -from Deeploy.Targets.Generic.Templates import ConcatTemplate, RQAddTemplate, RQSiGELUTemplate, iHardswishTemplate +from Deeploy.Targets.Generic.Templates import ConcatTemplate, RQSiGELUTemplate, iHardswishTemplate from Deeploy.Targets.Generic.TypeCheckers import ConcatChecker, GELUChecker, HardswishChecker, MatMulChecker, \ MulChecker, ReduceMeanChecker, RQAddChecker, RQHardswishChecker, SliceChecker, SoftmaxChecker, TransposeChecker, \ iLayerNormChecker @@ -45,8 +45,8 @@ from Deeploy.Targets.PULPOpen.CodeTransformationPasses.PULPL3Tiling import PULPL3Tiling from Deeploy.Targets.PULPOpen.DataTypes import PULPDMAFuture from Deeploy.Targets.PULPOpen.Templates import ConvTemplate, GEMMTemplate, MatrixVectorTemplate, MaxPool2DTemplate, \ - MulTemplate, ReduceMeanTemplate, RequantShiftTemplate, RQSiHardswishTemplate, SliceTemplate, TallGEMMTemplate, \ - TransposeTemplate, UniformRequantShiftTemplate, iRMSNormTemplate, iSoftmaxTemplate + MulTemplate, ReduceMeanTemplate, RequantShiftTemplate, RQAddTemplate, RQSiHardswishTemplate, SliceTemplate, \ + TallGEMMTemplate, TransposeTemplate, UniformRequantShiftTemplate, iRMSNormTemplate, iSoftmaxTemplate from Deeploy.Targets.PULPOpen.TypeCheckers import PULPConvChecker, PULPLinearChecker, PULPMaxPoolChecker, \ PULPRequantShiftChecker from Deeploy.TilingExtension.CodeTransformationPasses.TilingVariableReplacement import TilingVariableReplacement @@ -157,7 +157,7 @@ PULPRQAddBindings = [ NodeBinding(RQAddChecker([PointerClass(_type), PointerClass(_type2)], [PointerClass(_type3)]), - RQAddTemplate.referenceTemplate, ForkTransformer) + RQAddTemplate.RQAddTemplate, ForkTransformer) for _type in [int8_t, uint8_t] for _type2 in [int8_t, uint8_t] for _type3 in [int8_t, uint8_t] diff --git a/Deeploy/Targets/Generic/Templates/RQAddTemplate.py b/Deeploy/Targets/PULPOpen/Templates/RQAddTemplate.py similarity index 96% rename from Deeploy/Targets/Generic/Templates/RQAddTemplate.py rename to Deeploy/Targets/PULPOpen/Templates/RQAddTemplate.py index e9f82959..49ede2b6 100644 --- a/Deeploy/Targets/Generic/Templates/RQAddTemplate.py +++ b/Deeploy/Targets/PULPOpen/Templates/RQAddTemplate.py @@ -28,7 +28,7 @@ from Deeploy.DeeployTypes import NetworkContext, NodeTemplate, OperatorRepresentation -class _RQAddTemplate(NodeTemplate): +class PULPRQAddTemplate(NodeTemplate): def __init__(self, templateStr): super().__init__(templateStr) @@ -46,7 +46,7 @@ def alignToContext(self, ctxt: NetworkContext, return ctxt, operatorRepresentation, [] -referenceTemplate = _RQAddTemplate(""" +RQAddTemplate = PULPRQAddTemplate(""" <% signatureString = '' diff --git a/Deeploy/Targets/Snitch/Bindings.py b/Deeploy/Targets/Snitch/Bindings.py index 17bfe1b5..6b793a64 100644 --- a/Deeploy/Targets/Snitch/Bindings.py +++ b/Deeploy/Targets/Snitch/Bindings.py @@ -32,11 +32,11 @@ from Deeploy.CommonExtensions.DataTypes import int8_t, int32_t, uint8_t from Deeploy.DeeployTypes import CodeTransformation, NodeBinding from Deeploy.FutureExtension.CodeTransformationPasses.FutureCodeTransformation import FutureGeneration -from Deeploy.Targets.Generic.Templates import RQAddTemplate, iNoNormTemplate -from Deeploy.Targets.Generic.TypeCheckers import AddChecker, GEMMChecker, RQAddChecker, SoftmaxChecker, iNoNormChecker +from Deeploy.Targets.Generic.Templates import iNoNormTemplate +from Deeploy.Targets.Generic.TypeCheckers import AddChecker, GEMMChecker, RQAddChecker, SoftmaxChecker, iNoNormChecker from Deeploy.Targets.Snitch.CodeTransformationPasses import SnitchClusterTiling, SnitchCoreFilterPass, \ SnitchProfileExecutionBlockPass, SnitchSynchCoresPass -from Deeploy.Targets.Snitch.Templates import AddTemplate, iSoftmaxTemplate +from Deeploy.Targets.Snitch.Templates import AddTemplate, RQAddTemplate, iSoftmaxTemplate from Deeploy.Targets.Snitch.Templates.GemmTemplate import SnitchGemm_Template from Deeploy.Targets.Snitch.Templates.RqGemmTemplate import SnitchRqGemm_Template from Deeploy.TilingExtension.CodeTransformationPasses.TilingVariableReplacement import TilingVariableReplacement diff --git a/Deeploy/Targets/Snitch/Parsers.py b/Deeploy/Targets/Snitch/Parser.py similarity index 100% rename from Deeploy/Targets/Snitch/Parsers.py rename to Deeploy/Targets/Snitch/Parser.py diff --git a/Deeploy/Targets/Snitch/Templates/RQAddTemplate.py b/Deeploy/Targets/Snitch/Templates/RQAddTemplate.py new file mode 100644 index 00000000..2a49a3b4 --- /dev/null +++ b/Deeploy/Targets/Snitch/Templates/RQAddTemplate.py @@ -0,0 +1,71 @@ +# ---------------------------------------------------------------------- +# +# File: RQAddTemplate.py +# +# Last edited: 11.11.2023 +# +# Copyright (C) 2023, ETH Zurich and University of Bologna. +# +# Author: +# - Moritz Scherer, ETH Zurich +# - Victor Jung, ETH Zurich +# +# ---------------------------------------------------------------------- +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the License); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an AS IS BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from typing import Dict, List, Tuple + +from Deeploy.DeeployTypes import NetworkContext, NodeTemplate, OperatorRepresentation + + +class SnitchRQAddTemplate(NodeTemplate): + + def __init__(self, templateStr): + super().__init__(templateStr) + + def alignToContext(self, ctxt: NetworkContext, + operatorRepresentation: OperatorRepresentation) -> Tuple[NetworkContext, Dict, List[str]]: + # Extract signedness information of input, weights and output + signedI2 = ctxt.lookup(operatorRepresentation['data_in_2'])._type.referencedType.typeMin < 0 + signedI = ctxt.lookup(operatorRepresentation['data_in_1'])._type.referencedType.typeMin < 0 + signedO = ctxt.lookup(operatorRepresentation['data_out'])._type.referencedType.typeMin < 0 + operatorRepresentation['input_2_signed'] = signedI2 + operatorRepresentation['input_signed'] = signedI + operatorRepresentation['output_signed'] = signedO + + return ctxt, operatorRepresentation, [] + + +referenceTemplate = SnitchRQAddTemplate(""" + +<% +signatureString = '' +if input_signed: + signatureString += '_i8' +else: + signatureString += '_u8' +if input_2_signed: + signatureString += '_i8' +else: + signatureString += '_u8' +if output_signed: + signatureString += '_i8' +else: + signatureString += '_u8' +%> + +// PULP NN RQADD +pulp_nn_add${signatureString}(${data_in_1}, ${data_in_2}, ${data_out}, ${rqs1_mul}, ${rqs1_add}, ${rqs1_log2D}, ${rqs2_mul}, ${rqs2_add}, ${rqs2_log2D}, ${rqsOut_mul}, ${rqsOut_add}, ${rqsOut_log2D}, 1, ${size}, 1, 1); +""")