From 89f24d189857e2aaedc0a7c6036660e3b46eacea Mon Sep 17 00:00:00 2001 From: tahaelbayad Date: Tue, 7 Jan 2025 16:10:26 +0100 Subject: [PATCH] apply Victor's comment --- Deeploy/Targets/Generic/TypeCheckers.py | 22 +++++++++ Deeploy/Targets/PULPOpen/Bindings.py | 6 +-- Deeploy/Targets/PULPOpen/Parsers.py | 58 +----------------------- Deeploy/Targets/PULPOpen/Platform.py | 6 +-- Deeploy/Targets/PULPOpen/TypeCheckers.py | 23 ---------- Deeploy/Targets/Snitch/Bindings.py | 5 +- Deeploy/Targets/Snitch/TypeCheckers.py | 53 ---------------------- 7 files changed, 31 insertions(+), 142 deletions(-) delete mode 100644 Deeploy/Targets/Snitch/TypeCheckers.py diff --git a/Deeploy/Targets/Generic/TypeCheckers.py b/Deeploy/Targets/Generic/TypeCheckers.py index e1de80e9..d6011379 100644 --- a/Deeploy/Targets/Generic/TypeCheckers.py +++ b/Deeploy/Targets/Generic/TypeCheckers.py @@ -124,6 +124,28 @@ def _inferSignedness(self, inputs: List[VariableBuffer], else: return [False] +class RQAddChecker(SignPropTypeChecker): + + def __init__(self, input_types: Sequence[Type[Pointer]], output_types: Sequence[Type[Pointer]]): + super().__init__(input_types, output_types) + + def _inferNumLevels(self, inputs: List[VariableBuffer], + operatorRepresentation: OperatorRepresentation) -> List[int]: + return [operatorRepresentation['rqsOut_n_levels']] + + def _inferSignedness(self, inputs: List[VariableBuffer], + operatorRepresentation: OperatorRepresentation) -> List[bool]: + return [bool(operatorRepresentation["rqsOut_signed"])] + + # Override this. This should compute the signednes of each output node of the Layer + def checkOutputType(self, inputs: List[VariableBuffer], operatorRepresentation: OperatorRepresentation) -> bool: + outputTypeSigned = self.output_types[0].referencedType.typeMin < 0 + if operatorRepresentation['rqsOut_signed'] and outputTypeSigned: + return True + if (not operatorRepresentation['rqsOut_signed']) and (not outputTypeSigned): + return True + return False + class FloatAddChecker(SignPropTypeChecker): diff --git a/Deeploy/Targets/PULPOpen/Bindings.py b/Deeploy/Targets/PULPOpen/Bindings.py index ec27e8f3..2dcc6470 100644 --- a/Deeploy/Targets/PULPOpen/Bindings.py +++ b/Deeploy/Targets/PULPOpen/Bindings.py @@ -39,7 +39,7 @@ from Deeploy.Targets.Generic.Templates import ConcatTemplate, RQSiGELUTemplate, iHardswishTemplate, RQAddTemplate from Deeploy.Targets.Generic.TypeCheckers import ConcatChecker, GELUChecker, HardswishChecker, MatMulChecker, \ MulChecker, ReduceMeanChecker, RQHardswishChecker, SliceChecker, SoftmaxChecker, TransposeChecker, \ - iLayerNormChecker + iLayerNormChecker, RQAddChecker from Deeploy.Targets.PULPOpen.CodeTransformationPasses.PULPClusterSynch import PULPSynchCoresPass from Deeploy.Targets.PULPOpen.CodeTransformationPasses.PULPClusterTiling import PULPClusterTiling from Deeploy.Targets.PULPOpen.CodeTransformationPasses.PULPL3Tiling import PULPL3Tiling @@ -48,7 +48,7 @@ MulTemplate, ReduceMeanTemplate, RequantShiftTemplate, RQSiHardswishTemplate, SliceTemplate, \ TallGEMMTemplate, TransposeTemplate, UniformRequantShiftTemplate, iRMSNormTemplate, iSoftmaxTemplate from Deeploy.Targets.PULPOpen.TypeCheckers import PULPConvChecker, PULPLinearChecker, PULPMaxPoolChecker, \ - PULPRequantShiftChecker, PULPRQAddChecker + PULPRequantShiftChecker from Deeploy.TilingExtension.CodeTransformationPasses.TilingVariableReplacement import TilingVariableReplacement _clusterEntryClosureCallTemplate = NodeTemplate(""" @@ -156,7 +156,7 @@ ] PULPRQAddBindings = [ - NodeBinding(PULPRQAddChecker([PointerClass(_type), PointerClass(_type2)], [PointerClass(_type3)]), + NodeBinding(RQAddChecker([PointerClass(_type), PointerClass(_type2)], [PointerClass(_type3)]), RQAddTemplate.referenceTemplate, ForkTransformer) for _type in [int8_t, uint8_t] for _type2 in [int8_t, uint8_t] diff --git a/Deeploy/Targets/PULPOpen/Parsers.py b/Deeploy/Targets/PULPOpen/Parsers.py index 68782378..33b4abec 100644 --- a/Deeploy/Targets/PULPOpen/Parsers.py +++ b/Deeploy/Targets/PULPOpen/Parsers.py @@ -29,63 +29,7 @@ import onnx_graphsurgeon as gs from Deeploy.DeeployTypes import NetworkContext -from Deeploy.Targets.Generic.Parsers import AddParser, GEMMParser, RQSConv1DParser, RQSConv2DParser, RQSParserInterface - - -class PULPRQAddParser(AddParser): - - def parseNode(self, node: gs.Node) -> bool: - - if not super().parseNode(node): - return False - - ret = all([ - 'rqs1_mul' in node.attrs, - 'rqs1_add' in node.attrs, - 'rqs1_div' in node.attrs, - 'rqs1_signed' in node.attrs, - any(['rqs1_n_levels' in node.attrs, 'rqs1_n_levels_out' in node.attrs]), - 'rqs2_mul' in node.attrs, - 'rqs2_add' in node.attrs, - 'rqs2_div' in node.attrs, - 'rqs2_signed' in node.attrs, - any(['rqs2_n_levels' in node.attrs, 'rqs2_n_levels_out' in node.attrs]), - 'rqsOut_mul' in node.attrs, - 'rqsOut_add' in node.attrs, - 'rqsOut_div' in node.attrs, - 'rqsOut_signed' in node.attrs, - any(['rqsOut_n_levels' in node.attrs, 'rqsOut_n_levels_out' in node.attrs]), - ]) - - if ret: - if 'rqs1_n_levels' in node.attrs: - self.operatorRepresentation['rqs1_n_levels'] = int(node.attrs['rqs1_n_levels'].values) - else: - self.operatorRepresentation['rqs1_n_levels'] = int(node.attrs['rqs1_n_levels_out'].values) - self.operatorRepresentation['rqs1_mul'] = int(node.attrs['rqs1_mul']) - self.operatorRepresentation['rqs1_add'] = int(node.attrs['rqs1_add']) - self.operatorRepresentation['rqs1_signed'] = int(node.attrs['rqs1_signed'].values) - self.operatorRepresentation['rqs1_log2D'] = int(math.log2(node.attrs['rqs1_div'].values)) - - if 'rqs2_n_levels' in node.attrs: - self.operatorRepresentation['rqs2_n_levels'] = int(node.attrs['rqs2_n_levels'].values) - else: - self.operatorRepresentation['rqs2_n_levels'] = int(node.attrs['rqs2_n_levels_out'].values) - self.operatorRepresentation['rqs2_mul'] = int(node.attrs['rqs2_mul']) - self.operatorRepresentation['rqs2_add'] = int(node.attrs['rqs2_add']) - self.operatorRepresentation['rqs2_signed'] = int(node.attrs['rqs2_signed'].values) - self.operatorRepresentation['rqs2_log2D'] = int(math.log2(node.attrs['rqs2_div'].values)) - - if 'rqsOut_n_levels' in node.attrs: - self.operatorRepresentation['rqsOut_n_levels'] = int(node.attrs['rqsOut_n_levels'].values) - else: - self.operatorRepresentation['rqsOut_n_levels'] = int(node.attrs['rqsOut_n_levels_out'].values) - self.operatorRepresentation['rqsOut_mul'] = int(node.attrs['rqsOut_mul']) - self.operatorRepresentation['rqsOut_add'] = int(node.attrs['rqsOut_add']) - self.operatorRepresentation['rqsOut_signed'] = int(node.attrs['rqsOut_signed'].values) - self.operatorRepresentation['rqsOut_log2D'] = int(math.log2(node.attrs['rqsOut_div'].values)) - - return ret +from Deeploy.Targets.Generic.Parsers import GEMMParser, RQSConv1DParser, RQSConv2DParser, RQSParserInterface class PULPConv2DParser(RQSConv2DParser): diff --git a/Deeploy/Targets/PULPOpen/Platform.py b/Deeploy/Targets/PULPOpen/Platform.py index c32f29dc..398a9056 100644 --- a/Deeploy/Targets/PULPOpen/Platform.py +++ b/Deeploy/Targets/PULPOpen/Platform.py @@ -41,7 +41,7 @@ from Deeploy.Targets.Generic.Parsers import AddParser, ConcatParser, FlattenParser, GatherParser, MatMulParser, \ MulParser, Pad1DParser, Pad2DParser, ReduceMeanParser, RequantShiftParser, ReshapeParser, RQIntegerDivParser, \ RQSiGELUParser, RQSiHardswishParser, SliceParser, TransposeParser, UniformRequantShiftParser, UnsqueezeParser, \ - iHardswishParser, iRMSNormParser, iSoftmaxParser + iHardswishParser, iRMSNormParser, iSoftmaxParser, RQAddParser from Deeploy.Targets.Generic.Templates import AllocateTemplate as BasicAllocateTemplate from Deeploy.Targets.Generic.TopologyOptimizationPasses.Passes import IntegerDivRequantMergePass, \ MergeConstAddAndRequantPass, MergeTrueIntegerDivRequantShiftPass, RQSSplitPass, SkipEmptyConcatPass, \ @@ -50,7 +50,7 @@ PULPReduceMeanBindings from Deeploy.Targets.PULPOpen.Layers import PULPRQSConvLayer, PULPRQSGEMMLayer from Deeploy.Targets.PULPOpen.Parsers import PULPConv1DParser, PULPConv2DParser, PULPDWConv1DParser, \ - PULPDWConv2DParser, PULPGEMMParser, PULPMatrixVecParser, PULPRQAddParser, PULPTallGEMMParser + PULPDWConv2DParser, PULPGEMMParser, PULPMatrixVecParser, PULPTallGEMMParser from Deeploy.Targets.PULPOpen.Templates import AllocateTemplate, FreeTemplate from Deeploy.Targets.PULPOpen.Tiler import PULPAddTilingReadyBindings, PULPConcatTilingReadyBindings, \ PULPFlattenTilingReadyBindings, PULPiHardswishTilingReadyBindings, PULPiRMSNormTilingReadyBindings, \ @@ -62,7 +62,7 @@ from Deeploy.Targets.PULPOpen.TopologyOptimizationPasses.Passes import PULPAddRequantMergePass, \ PULPConvRequantMergePass, PULPGEMMRequantMergePass, PULPMatMulRequantMergePass -RQAddMapper = NodeMapper(PULPRQAddParser(), PULPRQAddTilingReadyBindings) +RQAddMapper = NodeMapper(RQAddParser(), PULPRQAddTilingReadyBindings) AddMapper = NodeMapper(AddParser(), PULPAddTilingReadyBindings) FlattenMapper = NodeMapper(FlattenParser(), PULPFlattenTilingReadyBindings) GatherMapper = NodeMapper(GatherParser(), BasicGatherBindings) diff --git a/Deeploy/Targets/PULPOpen/TypeCheckers.py b/Deeploy/Targets/PULPOpen/TypeCheckers.py index 2685f4d7..9f55d5bd 100644 --- a/Deeploy/Targets/PULPOpen/TypeCheckers.py +++ b/Deeploy/Targets/PULPOpen/TypeCheckers.py @@ -47,29 +47,6 @@ def _inferSignedness(self, inputs: List[VariableBuffer], return [False] -class PULPRQAddChecker(SignPropTypeChecker): - - def __init__(self, input_types: Sequence[Type[Pointer]], output_types: Sequence[Type[Pointer]]): - super().__init__(input_types, output_types) - - def _inferNumLevels(self, inputs: List[VariableBuffer], - operatorRepresentation: OperatorRepresentation) -> List[int]: - return [operatorRepresentation['rqsOut_n_levels']] - - def _inferSignedness(self, inputs: List[VariableBuffer], - operatorRepresentation: OperatorRepresentation) -> List[bool]: - return [bool(operatorRepresentation["rqsOut_signed"])] - - # Override this. This should compute the signednes of each output node of the Layer - def checkOutputType(self, inputs: List[VariableBuffer], operatorRepresentation: OperatorRepresentation) -> bool: - outputTypeSigned = self.output_types[0].referencedType.typeMin < 0 - if operatorRepresentation['rqsOut_signed'] and outputTypeSigned: - return True - if (not operatorRepresentation['rqsOut_signed']) and (not outputTypeSigned): - return True - return False - - class PULPRequantShiftChecker(SignPropTypeChecker): def __init__(self, input_types: Sequence[Type[Pointer]], output_types: Sequence[Type[Pointer]]): diff --git a/Deeploy/Targets/Snitch/Bindings.py b/Deeploy/Targets/Snitch/Bindings.py index 49de9106..24429780 100644 --- a/Deeploy/Targets/Snitch/Bindings.py +++ b/Deeploy/Targets/Snitch/Bindings.py @@ -33,13 +33,12 @@ from Deeploy.DeeployTypes import CodeTransformation, NodeBinding from Deeploy.FutureExtension.CodeTransformationPasses.FutureCodeTransformation import FutureGeneration from Deeploy.Targets.Generic.Templates import iNoNormTemplate, RQAddTemplate -from Deeploy.Targets.Generic.TypeCheckers import AddChecker, GEMMChecker, SoftmaxChecker, iNoNormChecker +from Deeploy.Targets.Generic.TypeCheckers import AddChecker, GEMMChecker, SoftmaxChecker, iNoNormChecker, RQAddChecker from Deeploy.Targets.Snitch.CodeTransformationPasses import SnitchClusterTiling, SnitchCoreFilterPass, \ SnitchProfileExecutionBlockPass, SnitchSynchCoresPass from Deeploy.Targets.Snitch.Templates import AddTemplate, iSoftmaxTemplate from Deeploy.Targets.Snitch.Templates.GemmTemplate import SnitchGemm_Template from Deeploy.Targets.Snitch.Templates.RqGemmTemplate import SnitchRqGemm_Template -from Deeploy.Targets.Snitch.TypeCheckers import SnitchRQAddChecker from Deeploy.TilingExtension.CodeTransformationPasses.TilingVariableReplacement import TilingVariableReplacement TilingCallClosure = partial(ClosureGeneration, closureSuffix = "_tiling_closure") @@ -78,7 +77,7 @@ TiledTransformer) for _type in [int8_t] ] SnitchRQAddBindings = [ - NodeBinding(SnitchRQAddChecker([PointerClass(_type), PointerClass(_type)], [PointerClass(_type)]), + NodeBinding(RQAddChecker([PointerClass(_type), PointerClass(_type)], [PointerClass(_type)]), RQAddTemplate.referenceTemplate, TiledTransformer) for _type in [int8_t] ] SnitchAddBindings = [ diff --git a/Deeploy/Targets/Snitch/TypeCheckers.py b/Deeploy/Targets/Snitch/TypeCheckers.py deleted file mode 100644 index fa428aff..00000000 --- a/Deeploy/Targets/Snitch/TypeCheckers.py +++ /dev/null @@ -1,53 +0,0 @@ -# ---------------------------------------------------------------------- -# -# File: SnitchCheckers.py -# -# Last edited: 07.06.2024 -# -# Copyright (C) 2024, ETH Zurich and University of Bologna. -# -# Author: -# - Victor Jung, jungvi@iis.ee.ethz.ch, 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 -# -# -# 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 List, Sequence, Type - -from Deeploy.AbstractDataTypes import Pointer -from Deeploy.CommonExtensions.TypeCheckers.SignPropTypeChecker import SignPropTypeChecker -from Deeploy.DeeployTypes import OperatorRepresentation, VariableBuffer - - -class SnitchRQAddChecker(SignPropTypeChecker): - - def __init__(self, input_types: Sequence[Type[Pointer]], output_types: Sequence[Type[Pointer]]): - super().__init__(input_types, output_types) - - def _inferNumLevels(self, inputs: List[VariableBuffer], - operatorRepresentation: OperatorRepresentation) -> List[int]: - return [operatorRepresentation['rqsOut_n_levels']] - - def _inferSignedness(self, inputs: List[VariableBuffer], - operatorRepresentation: OperatorRepresentation) -> List[bool]: - return [bool(operatorRepresentation["rqsOut_signed"])] - - # Override this. This should compute the signednes of each output node of the Layer - def checkOutputType(self, inputs: List[VariableBuffer], operatorRepresentation: OperatorRepresentation) -> bool: - outputTypeSigned = self.output_types[0].referencedType.typeMin < 0 - if operatorRepresentation['rqsOut_signed'] and outputTypeSigned: - return True - if (not operatorRepresentation['rqsOut_signed']) and (not outputTypeSigned): - return True - return False