Skip to content
Snippets Groups Projects
Commit 5aa49ccf authored by Olivier BICHLER's avatar Olivier BICHLER
Browse files

Unified interface for parameters

parent 44afa3b4
No related branches found
No related tags found
1 merge request!16Unified interface for attributes
Pipeline #32210 failed
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <vector> #include <vector>
#include <array> #include <array>
#include "aidge/utils/Parameter.hpp"
#include "aidge/backend/OperatorImpl.hpp" #include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/Conv.hpp" #include "aidge/operator/Conv.hpp"
#include "aidge/operator/Operator.hpp" #include "aidge/operator/Operator.hpp"
...@@ -26,7 +25,7 @@ namespace py = pybind11; ...@@ -26,7 +25,7 @@ namespace py = pybind11;
namespace Aidge { namespace Aidge {
template <DimIdx_t DIM> void declare_ConvOp(py::module &m) { template <DimIdx_t DIM> void declare_ConvOp(py::module &m) {
py::class_<Conv_Op<DIM>, std::shared_ptr<Conv_Op<DIM>>, Operator, PyAbstractParametrizable>( py::class_<Conv_Op<DIM>, std::shared_ptr<Conv_Op<DIM>>, Operator, Parameters>(
m, ("ConvOp" + std::to_string(DIM) + "D").c_str(), m, ("ConvOp" + std::to_string(DIM) + "D").c_str(),
py::multiple_inheritance()) py::multiple_inheritance())
.def(py::init<DimSize_t, .def(py::init<DimSize_t,
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <vector> #include <vector>
#include <array> #include <array>
#include "aidge/utils/Parameter.hpp"
#include "aidge/backend/OperatorImpl.hpp" #include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/ConvDepthWise.hpp" #include "aidge/operator/ConvDepthWise.hpp"
#include "aidge/operator/Operator.hpp" #include "aidge/operator/Operator.hpp"
...@@ -27,7 +26,7 @@ namespace py = pybind11; ...@@ -27,7 +26,7 @@ namespace py = pybind11;
namespace Aidge { namespace Aidge {
template <DimIdx_t DIM> void declare_ConvDepthWiseOp(py::module &m) { template <DimIdx_t DIM> void declare_ConvDepthWiseOp(py::module &m) {
py::class_<ConvDepthWise_Op<DIM>, std::shared_ptr<ConvDepthWise_Op<DIM>>, Operator, PyAbstractParametrizable>( py::class_<ConvDepthWise_Op<DIM>, std::shared_ptr<ConvDepthWise_Op<DIM>>, Operator, Parameters>(
m, ("ConvDepthWiseOp" + std::to_string(DIM) + "D").c_str(), m, ("ConvDepthWiseOp" + std::to_string(DIM) + "D").c_str(),
py::multiple_inheritance()) py::multiple_inheritance())
.def(py::init<const std::array<DimSize_t, DIM> &, .def(py::init<const std::array<DimSize_t, DIM> &,
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include "aidge/operator/FC.hpp" #include "aidge/operator/FC.hpp"
#include "aidge/utils/Parameter.hpp"
#include "aidge/backend/OperatorImpl.hpp" #include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/Operator.hpp" #include "aidge/operator/Operator.hpp"
#include "aidge/utils/Types.h" #include "aidge/utils/Types.h"
...@@ -21,7 +20,7 @@ namespace py = pybind11; ...@@ -21,7 +20,7 @@ namespace py = pybind11;
namespace Aidge { namespace Aidge {
void declare_FC(py::module &m) { void declare_FC(py::module &m) {
py::class_<FC_Op, std::shared_ptr<FC_Op>, Operator, PyAbstractParametrizable>(m, "FC_Op", py::multiple_inheritance()); py::class_<FC_Op, std::shared_ptr<FC_Op>, Operator, Parameters>(m, "FC_Op", py::multiple_inheritance());
m.def("FC", &FC, py::arg("out_channels"), py::arg("nobias") = false, py::arg("name") = ""); m.def("FC", &FC, py::arg("out_channels"), py::arg("nobias") = false, py::arg("name") = "");
} }
......
...@@ -21,46 +21,7 @@ namespace Aidge { ...@@ -21,46 +21,7 @@ namespace Aidge {
void init_GenericOperator(py::module& m) { void init_GenericOperator(py::module& m) {
py::class_<GenericOperator_Op, std::shared_ptr<GenericOperator_Op>, Operator>(m, "GenericOperatorOp", py::class_<GenericOperator_Op, std::shared_ptr<GenericOperator_Op>, Operator>(m, "GenericOperatorOp",
py::multiple_inheritance()) py::multiple_inheritance());
.def("get_parameter_type", &GenericOperator_Op::getParameterType)
.def("get_parameters_name", &GenericOperator_Op::getParametersName)
.def("add_parameter", &GenericOperator_Op::addParameter<bool>)
.def("add_parameter", &GenericOperator_Op::addParameter<int>)
.def("add_parameter", &GenericOperator_Op::addParameter<float>)
.def("add_parameter", &GenericOperator_Op::addParameter<std::string>)
.def("add_parameter", &GenericOperator_Op::addParameter<std::vector<bool>>)
.def("add_parameter", &GenericOperator_Op::addParameter<std::vector<int>>)
.def("add_parameter", &GenericOperator_Op::addParameter<std::vector<float>>)
.def("add_parameter", &GenericOperator_Op::addParameter<std::vector<std::string>>)
.def("get_parameter", [](GenericOperator_Op& self, std::string key) -> py::object {
/*
This getParameter method returns the good python type without having to have
prior knowledge of the parameter type.
*/
py::object res = py::none();
std::string paramType = self.getParameterType(key);
if(paramType == typeid(int).name())
res = py::cast(self.getParameter<int>(key));
else if(paramType == typeid(float).name())
res = py::cast(self.getParameter<float>(key));
else if(paramType == typeid(bool).name())
res = py::cast(self.getParameter<bool>(key));
else if(paramType == typeid(std::string).name())
res = py::cast(self.getParameter<std::string>(key));
else if(paramType == typeid(std::vector<bool>).name())
res = py::cast(self.getParameter<std::vector<bool>>(key));
else if(paramType == typeid(std::vector<int>).name())
res = py::cast(self.getParameter<std::vector<int>>(key));
else if(paramType == typeid(std::vector<float>).name())
res = py::cast(self.getParameter<std::vector<float>>(key));
else if(paramType == typeid(std::vector<std::string>).name())
res = py::cast(self.getParameter<std::vector<std::string>>(key));
else {
throw py::key_error("Failed to convert parameter type " + key + ", this issue may come from typeid function which gave an unknown key : [" + paramType + "]. Please open an issue asking to add the support for this key.");
}
return res;
});
m.def("GenericOperator", &GenericOperator, py::arg("type"), py::arg("nbDataIn"), py::arg("nbIn"), py::arg("nbOut"), m.def("GenericOperator", &GenericOperator, py::arg("type"), py::arg("nbDataIn"), py::arg("nbIn"), py::arg("nbOut"),
py::arg("name") = ""); py::arg("name") = "");
} }
......
...@@ -13,13 +13,12 @@ ...@@ -13,13 +13,12 @@
#include "aidge/operator/LeakyReLU.hpp" #include "aidge/operator/LeakyReLU.hpp"
#include "aidge/operator/Operator.hpp" #include "aidge/operator/Operator.hpp"
#include "aidge/utils/Parameter.hpp"
namespace py = pybind11; namespace py = pybind11;
namespace Aidge { namespace Aidge {
void init_LeakyReLU(py::module& m) { void init_LeakyReLU(py::module& m) {
py::class_<LeakyReLU_Op, std::shared_ptr<LeakyReLU_Op>, Operator, PyAbstractParametrizable>(m, "LeakyReLU_Op", py::multiple_inheritance()); py::class_<LeakyReLU_Op, std::shared_ptr<LeakyReLU_Op>, Operator, Parameters>(m, "LeakyReLU_Op", py::multiple_inheritance());
m.def("LeakyReLU", &LeakyReLU, py::arg("negative_slope") = 0.0f, py::arg("name") = ""); m.def("LeakyReLU", &LeakyReLU, py::arg("negative_slope") = 0.0f, py::arg("name") = "");
} }
......
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include "aidge/operator/Matmul.hpp" #include "aidge/operator/Matmul.hpp"
#include "aidge/utils/Parameter.hpp"
#include "aidge/backend/OperatorImpl.hpp" #include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/Operator.hpp" #include "aidge/operator/Operator.hpp"
#include "aidge/utils/Types.h" #include "aidge/utils/Types.h"
...@@ -21,7 +20,7 @@ namespace py = pybind11; ...@@ -21,7 +20,7 @@ namespace py = pybind11;
namespace Aidge { namespace Aidge {
void declare_Matmul(py::module &m) { void declare_Matmul(py::module &m) {
py::class_<Matmul_Op, std::shared_ptr<Matmul_Op>, Operator, PyAbstractParametrizable>(m, "Matmul_Op", py::multiple_inheritance()); py::class_<Matmul_Op, std::shared_ptr<Matmul_Op>, Operator, Parameters>(m, "Matmul_Op", py::multiple_inheritance());
m.def("Matmul", &Matmul, py::arg("out_channels"), py::arg("name") = ""); m.def("Matmul", &Matmul, py::arg("out_channels"), py::arg("name") = "");
} }
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <vector> #include <vector>
#include <array> #include <array>
#include "aidge/utils/Parameter.hpp"
#include "aidge/backend/OperatorImpl.hpp" #include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/MaxPooling.hpp" #include "aidge/operator/MaxPooling.hpp"
#include "aidge/operator/Operator.hpp" #include "aidge/operator/Operator.hpp"
...@@ -27,7 +26,7 @@ namespace py = pybind11; ...@@ -27,7 +26,7 @@ namespace py = pybind11;
namespace Aidge { namespace Aidge {
template <DimIdx_t DIM> void declare_MaxPoolingOp(py::module &m) { template <DimIdx_t DIM> void declare_MaxPoolingOp(py::module &m) {
py::class_<MaxPooling_Op<DIM>, std::shared_ptr<MaxPooling_Op<DIM>>, Operator, PyAbstractParametrizable>( py::class_<MaxPooling_Op<DIM>, std::shared_ptr<MaxPooling_Op<DIM>>, Operator, Parameters>(
m, ("MaxPoolingOp" + std::to_string(DIM) + "D").c_str(), m, ("MaxPoolingOp" + std::to_string(DIM) + "D").c_str(),
py::multiple_inheritance()) py::multiple_inheritance())
.def(py::init<const std::array<DimSize_t, DIM> &, .def(py::init<const std::array<DimSize_t, DIM> &,
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include <pybind11/stl.h> #include <pybind11/stl.h>
#include "aidge/utils/Types.h" #include "aidge/utils/Types.h"
#include "aidge/utils/Parameter.hpp"
// #include "aidge/backend/OperatorImpl.hpp" // #include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/Operator.hpp" #include "aidge/operator/Operator.hpp"
#include "aidge/operator/Producer.hpp" #include "aidge/operator/Producer.hpp"
......
...@@ -17,7 +17,7 @@ namespace Aidge { ...@@ -17,7 +17,7 @@ namespace Aidge {
void init_Data(py::module&); void init_Data(py::module&);
void init_Tensor(py::module&); void init_Tensor(py::module&);
void init_OperatorImpl(py::module&); void init_OperatorImpl(py::module&);
void init_Parameterizable(py::module&); void init_Parameters(py::module&);
void init_Operator(py::module&); void init_Operator(py::module&);
void init_Add(py::module&); void init_Add(py::module&);
...@@ -65,7 +65,7 @@ void init_Aidge(py::module& m){ ...@@ -65,7 +65,7 @@ void init_Aidge(py::module& m){
init_Connector(m); init_Connector(m);
init_OperatorImpl(m); init_OperatorImpl(m);
init_Parameterizable(m); init_Parameters(m);
init_Operator(m); init_Operator(m);
init_Add(m); init_Add(m);
init_AvgPooling(m); init_AvgPooling(m);
......
#include <pybind11/pybind11.h> #include <pybind11/pybind11.h>
#include "aidge/utils/Parameter.hpp" #include "aidge/utils/Parameters.hpp"
#include "aidge/utils/DynamicParameters.hpp"
namespace py = pybind11; namespace py = pybind11;
namespace Aidge { namespace Aidge {
void init_Parameterizable(py::module& m){ void init_Parameters(py::module& m){
py::class_<PyAbstractParametrizable, std::shared_ptr<PyAbstractParametrizable>>(m, "PyAbstractParametrizable") py::class_<Parameters, std::shared_ptr<Parameters>>(m, "Parameters")
.def("get", &PyAbstractParametrizable::getPy, py::arg("name")) .def("is_parameter", &Parameters::isParameter)
; .def("get_parameter_type", &Parameters::getParameterType)
.def("get_parameters_name", &Parameters::getParametersName)
.def("get_parameter", &Parameters::getPy, py::arg("name"));
py::class_<DynamicParameters, std::shared_ptr<DynamicParameters>>(m, "DynamicParameters")
.def("add_parameter", &DynamicParameters::addParameter<bool>)
.def("add_parameter", &DynamicParameters::addParameter<int>)
.def("add_parameter", &DynamicParameters::addParameter<float>)
.def("add_parameter", &DynamicParameters::addParameter<std::string>)
.def("add_parameter", &DynamicParameters::addParameter<std::vector<bool>>)
.def("add_parameter", &DynamicParameters::addParameter<std::vector<int>>)
.def("add_parameter", &DynamicParameters::addParameter<std::vector<float>>)
.def("add_parameter", &DynamicParameters::addParameter<std::vector<std::string>>);
} }
} }
...@@ -39,7 +39,7 @@ bool Aidge::NodeRegex::isA(std::string NodeType){ ...@@ -39,7 +39,7 @@ bool Aidge::NodeRegex::isA(std::string NodeType){
/**bool NodeRegex::_is(string &Node_op){ /**bool NodeRegex::_is(string &Node_op){
// Parsing the condition is done in the initialization of the NodeRegex // Parsing the condition is done in the initialization of the NodeRegex
// assert parameters exist in the node with the parameter function isParam() // assert parameters exist in the node with the parameter function isParameter()
// get the parameters // get the parameters
......
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