diff --git a/include/aidge/operator/PTQMetaOps.hpp b/include/aidge/operator/PTQMetaOps.hpp index 85bf0f6cfc876c15da1ee4f31809948c6efdbc98..33240fa2b4db0663f304cb5641bdbb8982ceaf3c 100644 --- a/include/aidge/operator/PTQMetaOps.hpp +++ b/include/aidge/operator/PTQMetaOps.hpp @@ -19,10 +19,8 @@ namespace Aidge { - // XXX XXX XXX std::shared_ptr<Aidge::Node> BaseQuantizer(double scalingFactor, const std::string& name); - /// @brief Quantizer acts as a meta-operator to handle scaling operations in the PTQ, replacing the Scaling Operator. /// This operator is composed of a sequence of [Mul] -> [Clip] -> [Round] operations. /// diff --git a/src/PTQ/PTQ.cpp b/src/PTQ/PTQ.cpp index c79cd4eba1292fd4a86531df508bae46c5f7734c..4c581feeb48c3ca0a819c47d75edb701f8b5499a 100644 --- a/src/PTQ/PTQ.cpp +++ b/src/PTQ/PTQ.cpp @@ -209,20 +209,6 @@ static int getInputIndex(std::shared_ptr<Node> node, std::shared_ptr<Node> paren void multiplyScalingFactor(std::shared_ptr<Aidge::Node> scalingNode, double coeff) { -/* - AIDGE_ASSERT(node->type() == "Mul" && hasAttr(node, "isProducerScaling") || hasAttr(node, "isScaling"), - "Cannot update the scaling factor on Node of type {} with no scaling tag", node->type()); - - auto scalingFactorTensor = std::static_pointer_cast<OperatorTensor>(node->getOperator())->getInput(1); - - std::shared_ptr<Tensor> fallback; - const Tensor& localTensor = scalingFactorTensor->refCastFrom(fallback, DataType::Float64, "cpu"); - double previousScalingFactor = localTensor.get<double>(0); - - std::shared_ptr<Tensor> resultTensor = std::make_shared<Tensor>(Array1D<double, 1> {previousScalingFactor * coeff}); - node->input(1).first->getOperator()->setOutput(0, resultTensor); -*/ - auto metaOperatorOp = std::static_pointer_cast<MetaOperator_Op> (scalingNode->getOperator()); // Get the Mul node from the microGraph @@ -416,27 +402,6 @@ static DataType getDataType(std::shared_ptr<Node> node) return op->getOutput(0)->dataType(); } -/* -static std::shared_ptr<Aidge::Node> createScalingNode(std::string name, std::vector<std::string> attributes, double value) -{ - std::shared_ptr<Node> scalingNode = Mul(name); - - for (std::string a : attributes) - addAttr(scalingNode, a); - - // Add the scaling factor as a producer of the node - - std::shared_ptr<Tensor> scalingFactorTensor = std::make_shared<Tensor>(Array1D<double, 1> {value}); - std::shared_ptr<Node> scalingFactorProducer = addProducer(scalingNode, 1, {1}, "ScalingFactor"); - - scalingFactorProducer->getOperator()->setOutput(0, scalingFactorTensor); - - // XXX graphView->add(scalingNode); - - return scalingNode; -} -*/ - // XXX double check this ! static bool nodeHasBias(std::shared_ptr<Node> node) { @@ -469,17 +434,6 @@ void insertScalingBelowProducer(std::shared_ptr<Node> producerNode, double ratio void insertScalingBelowProducer(std::shared_ptr<Node> producerNode, std::shared_ptr<GraphView> graphView) { -/* - std::string scalingNodeName = makeUniqueName(producerNode->name() + "_ProducerScaling", graphView); - std::shared_ptr<Node> scalingNode = createScalingNode(scalingNodeName, {"isProducerScaling"}, scalingFactor); - - scalingNode->getOperator()->setDataType(DataType::Float64); // getDataType(parentNode) - scalingNode->getOperator()->setBackend(determineBackend(producerNode)); - - insertChildren(producerNode, scalingNode, graphView); - graphView->add(scalingNode->getParent(1)); // add the scaling factor producer -*/ - std::string scalingNodeName = makeUniqueName(producerNode->name() + "_ProducerScaling", graphView); std::shared_ptr<Node> scalingNode = BaseQuantizer(1.0, scalingNodeName);; addAttr(scalingNode, "isProducerScaling"); @@ -489,9 +443,6 @@ void insertScalingBelowProducer(std::shared_ptr<Node> producerNode, std::shared_ scalingNode->getOperator()->setBackend(determineBackend(producerNode)); // XXX use the producer parent instead ??? insertChildren(producerNode, scalingNode, graphView); - - // XXX XXX XXX is it needed ? - // graphView->add(scalingNode->getParent(1)); } void insertProducerScalingNodes(std::shared_ptr<GraphView> graphView) @@ -536,7 +487,6 @@ void insertResidualScalingNodes(std::shared_ptr<GraphView> graphView) std::string residualNodeName = makeUniqueName(parentNode->name() + "_Res", graphView); // XXX XXX XXX - // std::shared_ptr<Node> residualNode = createScalingNode(residualNodeName, {"isScaling", "isResidual"}, 1.0); std::shared_ptr<Node> residualNode = BaseQuantizer(1.0, residualNodeName); addAttr(residualNode, "isScaling"); addAttr(residualNode, "isResidual"); @@ -545,9 +495,6 @@ void insertResidualScalingNodes(std::shared_ptr<GraphView> graphView) residualNode->getOperator()->setBackend(determineBackend(parentNode)); graphView->insertParent(node, residualNode, i, 0, 0); - - // XXX XXX XXX is it needed ? no more ! - // graphView->add(residualNode->getParent(1)); } } } @@ -570,7 +517,6 @@ void insertScalingNodes(std::shared_ptr<GraphView> graphView) std::string scalingNodeName = makeUniqueName(parentNode->name() + "_Scaling", graphView); // XXX XXX XXX - // std::shared_ptr<Node> scalingNode = createScalingNode(scalingNodeName, {"isScaling"}, 1.0); std::shared_ptr<Node> scalingNode = BaseQuantizer(1.0, scalingNodeName); addAttr(scalingNode, "isScaling"); @@ -584,9 +530,6 @@ void insertScalingNodes(std::shared_ptr<GraphView> graphView) graphView->add(scalingNode); } - // XXX XXX XXX is it needed ? no more - // graphView->add(scalingNode->getParent(1)); - // In the case the node is a non-linear operator we want to add an extra // scaling node before it to rescale it's input ... @@ -595,7 +538,6 @@ void insertScalingNodes(std::shared_ptr<GraphView> graphView) std::string prevScalingNodeName = makeUniqueName(parentNode->name() + "_PrevScaling", graphView); // XXX XXX XXX - // std::shared_ptr<Node> prevScalingNode = createScalingNode(prevScalingNodeName, {"isScaling"}, 1.0); std::shared_ptr<Node> prevScalingNode = BaseQuantizer(1.0, prevScalingNodeName); addAttr(prevScalingNode, "isScaling"); @@ -603,9 +545,6 @@ void insertScalingNodes(std::shared_ptr<GraphView> graphView) prevScalingNode->getOperator()->setBackend(determineBackend(parentNode)); graphView->insertParent(parentNode, prevScalingNode, 0, 0, 0); - - // XXX XXX XXX is it needed ? no more ! - // graphView->add(prevScalingNode->getParent(1)); } } } @@ -646,8 +585,7 @@ void normalizeParameters(std::shared_ptr<GraphView> graphView) double ratio = 1.0 / getTensorAbsoluteMax(weightTensor); - //rescaleTensor(weightTensor, ratio); - // XXX XXX XXX insertScalingBelowProducer(node->getParent(1), ratio, graphView); + // rescaleTensor(weightTensor, ratio); multiplyScalingFactor(node->getParent(1), ratio); // Accumulate the ratio @@ -665,7 +603,6 @@ void normalizeParameters(std::shared_ptr<GraphView> graphView) { std::shared_ptr<Tensor> biasTensor = getBiasTensor(node); //rescaleTensor(biasTensor, accumulatedRatios[node] ); - // XXX XXX XXX insertScalingBelowProducer(node->getParent(2), accumulatedRatios[node], graphView); multiplyScalingFactor(node->getParent(2), accumulatedRatios[node]); } }