Skip to content
Snippets Groups Projects
Commit b69da666 authored by Inna Kucher's avatar Inna Kucher
Browse files

adding missing pybind and fix namespaces

parent 89181403
No related branches found
No related tags found
No related merge requests found
......@@ -15,7 +15,7 @@ project(${project})
##############################################
# Import utils CMakeLists
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
#include(PybindModuleCreation)
include(PybindModuleCreation)
##############################################
# Define options
......@@ -27,7 +27,7 @@ option(COVERAGE "Enable coverage" OFF)
##############################################
# Import utils CMakeLists
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
#include(PybindModuleCreation)
include(PybindModuleCreation)
if(CMAKE_COMPILER_IS_GNUCXX AND COVERAGE)
Include(CodeCoverage)
......
......@@ -71,4 +71,6 @@ for outNode in model.get_output_nodes():
output_aidge = np.array(outNode.get_operator().output(0))
assert(np.allclose(output_aidge, output_model,rtol=1e-04))
#add PTQ part for testing
\ No newline at end of file
#add PTQ part for testing
#created ordered graph, as in cpp and do
#quantizeNetwork(ordered_graph_view, 8, verbose);
\ No newline at end of file
This diff is collapsed.
function(generate_python_binding name target_to_bind)
add_definitions(-DPYBIND)
Include(FetchContent)
FetchContent_Declare(
PyBind11
GIT_REPOSITORY https://github.com/pybind/pybind11.git
GIT_TAG v2.10.4 # or a later release
)
# Use the New FindPython mode, recommanded. Requires CMake 3.15+
find_package(Python COMPONENTS Interpreter Development)
FetchContent_MakeAvailable(PyBind11)
message(STATUS "Creating binding for module ${name}")
file(GLOB_RECURSE pybind_src_files "python_binding/*.cpp")
pybind11_add_module(${name} MODULE ${pybind_src_files} "NO_EXTRAS") # NO EXTRA recquired for pip install
target_include_directories(${name} PUBLIC "python_binding")
target_link_libraries(${name} PUBLIC ${target_to_bind})
endfunction()
......@@ -25,9 +25,9 @@
#include <cstdint>
#include <unordered_map>
using namespace Aidge;
//using namespace Aidge;
namespace Aidge_HELPER{
namespace Aidge{
float getCellThreshold(std::shared_ptr<Node> node);
float getMaxParentsScaling(std::shared_ptr<Node> node);
......@@ -36,7 +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);
// void quantizeNetwork(std::shared_ptr<GraphView> graphView, std::size_t nbBits, bool verbose);
}
#endif /* QuantPTQ_H_ */
/********************************************************************************
* Copyright (c) 2023 CEA-List
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
********************************************************************************/
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <string>
#include "aidge/QuantPTQ.hpp"
namespace py = pybind11;
namespace Aidge {
void init_QuantPTQ(py::module &m) {
m.def("quantize_network", &quantizeNetwork, py::arg("ordered_graph_view"), py::arg("nb_bits")=8, py::arg("verbose")=false, R"mydelimiter(
void quantizeNetwork(std::vector<std::shared_ptr<Node>> orderedGraphView, std::size_t nbBits, bool verbose){
Parameters
----------
std::vector<std::shared_ptr<Node>> orderedGraphView: Ordered Graph View,
std::size_t nbBits : number of bits
bool verbose : print debug messages
)mydelimiter");
}
PYBIND11_MODULE(aidge_quantization, m) {
init_QuantPTQ(m);
}
} // namespace Aidge
......@@ -20,13 +20,14 @@
#include <unordered_map>
#include "aidge/QuantPTQ.hpp"
using namespace Aidge;
//using namespace Aidge;
#include "aidge/hook/outputRange.hpp"
#include "aidge/operator/Scaling.hpp"
#include "aidge/graph/GraphView.hpp"
namespace Aidge_HELPER{
//namespace Aidge_HELPER{
namespace Aidge{
//getMaxParentsScaling
long double getMaxParentsScaling(std::shared_ptr<Node> node,
......@@ -385,8 +386,43 @@ void N2D2::DeepNetQuantization::quantizeActivations(
fuseScalingCells();
}
//TODO: add this !!!
double N2D2::DeepNetQuantization::getActivationQuantizationScaling(const Cell& cell, std::size_t nbBits) const {
const double unsignedMax = std::pow(2, nbBits) - 1;
const double signedMax = std::pow(2, nbBits - 1) - 1;
const Cell_Frame_Top& cellFrame = dynamic_cast<const Cell_Frame_Top&>(cell);
const std::shared_ptr<Activation>& activation = cellFrame.getActivation();
if(!activation || cell.getType() == ElemWiseCell::Type || cell.getType() == ActivationCell::Type) {
return 1.0;
}
const std::string activationType = activation->getType();
if(activationType == LogisticActivation::Type ||
activationType == LogisticActivation::TypeWithLoss)
{
return 2*(DeepNetExport::isCellInputsUnsigned(cell, mDeepNet)?
signedMax*unsignedMax/signedMax:
signedMax*signedMax/signedMax);
}
else if(DeepNetExport::isCellOutputUnsigned(cell, mDeepNet)) {
return DeepNetExport::isCellInputsUnsigned(cell, mDeepNet)?
signedMax*unsignedMax/unsignedMax:
signedMax*signedMax/unsignedMax;
}
else {
return DeepNetExport::isCellInputsUnsigned(cell, mDeepNet)?
signedMax*unsignedMax/signedMax:
signedMax*signedMax/signedMax;
}
}
*/
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){
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*/){
const long double prevActivationScaling = getMaxParentsScaling(node, activationScalings);
rescaleParentsToScaling(node, activationScalings, prevActivationScaling);
......
......@@ -15,7 +15,7 @@
#include <unordered_map>
using namespace Aidge;
using namespace Aidge_HELPER;
//using namespace Aidge_HELPER;
TEST_CASE("[aidge_module_template/ref_cpp/quantization] PTQ : Quantize Graph") {
......
......@@ -6,7 +6,7 @@
#include "aidge/hook/execTime.hpp"
using namespace Aidge;
using namespace Aidge_HELPER;
//using namespace Aidge_HELPER;
#include <iostream>
#include <ctime>
......
......@@ -10,7 +10,7 @@
#include "aidge/hook/outputRange.hpp"
using namespace Aidge;
using namespace Aidge_HELPER;
//using namespace Aidge_HELPER;
TEST_CASE("[hook] OutputRange(forward)") {
std::shared_ptr<Node> myConv1 = Conv(3,4,{3,3}, "myConv1");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment