diff --git a/CMakeLists.txt b/CMakeLists.txt index 70e7ccdc455c3a78ee7c08137eb76aa2cbc20f6a..10cbf7d48f97b9587aa40af98252f1feaa874eac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.15) + file(READ "${CMAKE_SOURCE_DIR}/version.txt" version) file(READ "${CMAKE_SOURCE_DIR}/project_name.txt" project) @@ -9,19 +10,28 @@ message(STATUS "Project version: ${version}") # Note : project name is {project} and python module name is also {project} set(module_name _${project}) # target name - project(${project}) ############################################## # Import utils CMakeLists set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") -include(PybindModuleCreation) +#include(PybindModuleCreation) ############################################## # Define options option(PYBIND "python binding" ON) option(WERROR "Warning as error" OFF) option(TEST "Enable tests" ON) +option(COVERAGE "Enable coverage" OFF) + +############################################## +# Import utils CMakeLists +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") +#include(PybindModuleCreation) + +if(CMAKE_COMPILER_IS_GNUCXX AND COVERAGE) + Include(CodeCoverage) +endif() ############################################## # Find system dependencies @@ -38,11 +48,11 @@ add_library(${module_name} ${src_files} ${inc_files}) target_link_libraries(${module_name} PUBLIC _aidge_core # _ is added because we link the target not the project - _aidge_backend_cpu # _ is added because we link the target not the project + _aidge_backend_cpu ) - #Set target properties set_property(TARGET ${module_name} PROPERTY POSITION_INDEPENDENT_CODE ON) + target_include_directories(${module_name} PUBLIC $<INSTALL_INTERFACE:include> @@ -52,9 +62,9 @@ target_include_directories(${module_name} ) # PYTHON BINDING -generate_python_binding(${project} ${module_name}) - if (PYBIND) + generate_python_binding(${project} ${module_name}) + # Handles Python + pybind11 headers dependencies target_link_libraries(${module_name} PUBLIC @@ -66,21 +76,15 @@ endif() target_compile_features(${module_name} PRIVATE cxx_std_14) - -if(WERROR) - target_compile_options(${module_name} PRIVATE +target_compile_options(${module_name} PRIVATE $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>: - -Wall -Wextra -fPIC -Wold-style-cast -Winline -pedantic -Werror=narrowing -Wshadow -Werror>) - target_compile_options(${module_name} PRIVATE + -Wall -Wextra -Wold-style-cast -Winline -pedantic -Werror=narrowing -Wshadow $<$<BOOL:${WERROR}>:-Werror>>) +target_compile_options(${module_name} PRIVATE $<$<CXX_COMPILER_ID:MSVC>: /W4>) -else() - target_compile_options(${module_name} PRIVATE - $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>: - -Wall -Wextra -fPIC -Wold-style-cast -Winline -pedantic -Werror=narrowing -Wshadow -Wpedantic>) - target_compile_options(${module_name} PRIVATE - $<$<CXX_COMPILER_ID:MSVC>: - /W4>) + +if(CMAKE_COMPILER_IS_GNUCXX AND COVERAGE) + append_coverage_compiler_flags() endif() ############################################## @@ -93,7 +97,6 @@ install(TARGETS ${module_name} EXPORT ${project}-targets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} - INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) @@ -103,12 +106,11 @@ install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(EXPORT ${project}-targets FILE "${project}-targets.cmake" DESTINATION ${INSTALL_CONFIGDIR} -# COMPONENT ${module_name} + COMPONENT ${module_name} ) #Create a ConfigVersion.cmake file include(CMakePackageConfigHelpers) - write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/${project}-config-version.cmake" VERSION ${version} diff --git a/aidge_quantization-config.cmake.in b/aidge_quantization-config.cmake.in new file mode 100644 index 0000000000000000000000000000000000000000..0361c06c6aa9bdcb9a2380d3f1c0b7996e77cd84 --- /dev/null +++ b/aidge_quantization-config.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include(${CMAKE_CURRENT_LIST_DIR}/aidge_quantization-config-version.cmake) + +include(${CMAKE_CURRENT_LIST_DIR}/aidge_quantization-targets.cmake) diff --git a/include/aidge/QuantPTQ.hpp b/include/aidge/QuantPTQ.hpp index 58e7cc0deea337fe1277386561a16a79c12de6fa..2967f9b0881a52400756a1778e7a98490f31f570 100644 --- a/include/aidge/QuantPTQ.hpp +++ b/include/aidge/QuantPTQ.hpp @@ -16,8 +16,8 @@ #define QuantPTQ_H_ -#include <aidge/aidge.hpp> -#include <aidge/aidge_backend_cpu.hpp> +//#include <aidge/aidge.hpp> +#include "aidge/backend/cpu.hpp" #include <numeric> #include <vector> @@ -36,6 +36,7 @@ namespace Aidge_HELPER{ long double quantizeFreeParams(std::shared_ptr<Node> node, std::size_t nbBits, std::unordered_map<std::string, long double> biasScalings, bool verbose); long double quantizeActivation(std::shared_ptr<Node> node, std::size_t nbBits, std::unordered_map<std::string, long double> biasScalings, std::unordered_map<std::string, long double> activationScalings, bool verbose); void quantizeNetwork(std::vector<std::shared_ptr<Node>> orderedGraphView, std::size_t nbBits, bool verbose); + //void quantizeNetwork(std::shared_ptr<GraphView> graphView, std::size_t nbBits, bool verbose); } #endif /* QuantPTQ_H_ */ diff --git a/src/QuantPTQ.cpp b/src/QuantPTQ.cpp index 8f776dfd79f3a687f47262fb7097044fe6ca840c..1a32faa3c703e0668250c0ebde5cede3cb4f1bd7 100644 --- a/src/QuantPTQ.cpp +++ b/src/QuantPTQ.cpp @@ -24,35 +24,7 @@ using namespace Aidge; #include "aidge/hook/outputRange.hpp" #include "aidge/operator/Scaling.hpp" - -//bias part when quantizing free params : -/* -std::unordered_map<std::string, long double> biasScalings; -long double biasScaling = getMaxParentsScaling(cell, biasScalings); -rescaleParentsToScaling(cell, biasScalings, biasScaling); - - long double bQuantScaling = DeepNetExport::isCellInputsUnsigned(*cell, mDeepNet)? - wQuantScaling*(std::pow(2, nbBits) - 1): - wQuantScaling*(std::pow(2, nbBits - 1) - 1); - -const std::pair<Float_T, Float_T> wMinMax = cell->getFreeParametersRange(Cell::Multiplicative); -const Float_T wScalingCell = Utils::max_abs(wMinMax.first, wMinMax.second); -if(wScalingCell != 0.0) { - cell->processFreeParameters([&](Float_T w) { return w*(wQuantScaling/wScalingCell); }, Cell::Multiplicative); - biasScaling *= wScalingCell; -} - -cell->processFreeParameters([&](Float_T b) { return b*(bQuantScaling/biasScaling); }, Cell::Additive); -biasScalings[cell->getName()] = biasScaling; - -fuseScalingCells(); - -return biasScalings; -*/ - - - - +#include "aidge/graph/GraphView.hpp" namespace Aidge_HELPER{ @@ -420,8 +392,6 @@ long double quantizeActivation(std::shared_ptr<Node> node, std::size_t nbBits, s rescaleParentsToScaling(node, activationScalings, prevActivationScaling); long double activationScaling; - - bool isLinearActivation = false; long double biasScaling = 1.0; if(node->type() == "ElemWise") { @@ -445,8 +415,6 @@ long double quantizeActivation(std::shared_ptr<Node> node, std::size_t nbBits, s else if(node->type() == "ReLU" || node->type() == "Linear" || node->type() == "Saturation") { - isLinearActivation = true; - //TODO :: nbOutputs > 2 is relevant for clip, check it //const bool clip = node->nbOutputs() > 2 && isLinearActivation; activationScaling = getCellThreshold(node); @@ -473,7 +441,7 @@ long double quantizeActivation(std::shared_ptr<Node> node, std::size_t nbBits, s } void quantizeNetwork(std::vector<std::shared_ptr<Node>> orderedGraphView, std::size_t nbBits, bool verbose){ -//void quantizeNetwork(std::shared_ptr<GraphView> g1, std::size_t nbBits){ +//void quantizeNetwork(std::shared_ptr<GraphView> g1, std::size_t nbBits, bool verbose){ //keep all bias scalings here std::unordered_map<std::string, long double> biasScalings; @@ -481,7 +449,6 @@ void quantizeNetwork(std::vector<std::shared_ptr<Node>> orderedGraphView, std::s std::unordered_map<std::string, long double> activationScalings; //loop on all nodes for weights/bias quantization - //for (const std::shared_ptr<Node>& nodePtr : g1->getNodes()) { for (const std::shared_ptr<Node>& nodePtr : orderedGraphView) { if(verbose){ @@ -500,13 +467,11 @@ void quantizeNetwork(std::vector<std::shared_ptr<Node>> orderedGraphView, std::s } if (nodePtr->type() == "Conv") { - //std::cout << "this is conv" << std::endl; biasScalings[nodePtr->name()] = quantizeFreeParams(nodePtr, nbBits, biasScalings, verbose); if(verbose){ std::cout << "outside quantizeFreeParams :: biasScalings[node->name()] = " << biasScalings[nodePtr->name()] << std::endl; } } else if(nodePtr->type() == "ReLU") { - //std::cout << "this is ReLU" << std::endl; activationScalings[nodePtr->name()] = quantizeActivation(nodePtr, nbBits, biasScalings, activationScalings, verbose); if(verbose){ std::cout << "outside quantizeActivation :: activationScalings[node->name()] = " << activationScalings[nodePtr->name()] << std::endl; @@ -526,7 +491,6 @@ void quantizeNetwork(std::vector<std::shared_ptr<Node>> orderedGraphView, std::s for (auto& graphPtr : nodePtr->views()) { graphPtr->addChild(scaling_node); } - //add scaling cell /* const long double actQuantScaling = getActivationQuantizationScaling(*cell, nbBits); @@ -547,17 +511,14 @@ void quantizeNetwork(std::vector<std::shared_ptr<Node>> orderedGraphView, std::s */ //add activation/bias to the maps for scaling cell ? - //activationScalings[scalingCell->getName()] = activationScalings[cell->getName()]; //biasScalings[scalingCell->getName()] = biasScaling; } else{ std::cout << "this is not Conv or ReLu, moving on" << std::endl; - } + } } - //std::cout << "the end!" << std::endl; - } } diff --git a/unit_tests/Test_QuantPTQ.cpp b/unit_tests/Test_QuantPTQ.cpp index a29d3cbd7743b93c3b084f8084eacf6322fc7fe3..b3e00739930f507075dae1dfbfd0ed3971ad7c1c 100644 --- a/unit_tests/Test_QuantPTQ.cpp +++ b/unit_tests/Test_QuantPTQ.cpp @@ -1,8 +1,8 @@ #include <catch2/catch_test_macros.hpp> #include "aidge/data/Tensor.hpp" -#include "aidge/data/TensorImpl.hpp" -#include "aidge/aidge_backend_cpu.hpp" +#include "aidge/backend/TensorImpl.hpp" +#include "aidge/backend/cpu.hpp" #include "aidge/operator/Conv.hpp" #include "aidge/operator/Scaling.hpp" #include "aidge/operator/GenericOperator.hpp" @@ -200,11 +200,16 @@ TEST_CASE("[aidge_module_template/ref_cpp/quantization] PTQ : Quantize Graph") { max_output_relu); } - scheduler.generateScheduling(false); + //no need to do this anymore, forward does it autimatically now ... + //scheduler.generateScheduling(true); std::vector<std::shared_ptr<Node>> ordered_graph_view = scheduler.getStaticScheduling(); + printf("Going to quantize network :\n"); + quantizeNetwork(ordered_graph_view, 8, verbose); + printf("After quantize network !!! \n"); + if(verbose) { printf("Graph after quantization :\n"); for (const std::shared_ptr<Node>& nodePtr : g1->getNodes()) { diff --git a/unit_tests/hook/Test_execTime.cpp b/unit_tests/hook/Test_execTime.cpp index 6b6bac7855c2642536e444150c491f107e8e5a42..26dce611bc691cd7fe6e1bc6bdf3b5c6d233541f 100644 --- a/unit_tests/hook/Test_execTime.cpp +++ b/unit_tests/hook/Test_execTime.cpp @@ -1,7 +1,7 @@ #include <catch2/catch_test_macros.hpp> -#include "aidge/aidge.hpp" -#include "aidge/aidge_backend_cpu.hpp" +//#include "aidge/aidge.hpp" +#include "aidge/backend/cpu.hpp" #include "aidge/QuantPTQ.hpp" #include "aidge/hook/execTime.hpp" diff --git a/unit_tests/hook/Test_outputRange.cpp b/unit_tests/hook/Test_outputRange.cpp index 4d8d9ddf1740ff8c82a0bc57ccc79ea79253e9a3..2d84d04a8aede72bf1e2252effd678e8a9c1b13c 100644 --- a/unit_tests/hook/Test_outputRange.cpp +++ b/unit_tests/hook/Test_outputRange.cpp @@ -4,8 +4,8 @@ #include <chrono> #include <iomanip> -#include "aidge/aidge.hpp" -#include "aidge/aidge_backend_cpu.hpp" +//#include "aidge/aidge.hpp" +#include "aidge/backend/cpu.hpp" #include "aidge/QuantPTQ.hpp" #include "aidge/hook/outputRange.hpp"