Skip to content
Snippets Groups Projects
Commit 8f849fc9 authored by Houssem ROUIS's avatar Houssem ROUIS
Browse files

add And op

parent 4754941b
No related branches found
No related tags found
2 merge requests!212Version 0.3.0,!178Learning backend cuda
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
#include "aidge/nodeTester/ConditionalInterpreter.hpp" #include "aidge/nodeTester/ConditionalInterpreter.hpp"
#include "aidge/operator/Add.hpp" #include "aidge/operator/Add.hpp"
#include "aidge/operator/And.hpp"
#include "aidge/operator/ArgMax.hpp" #include "aidge/operator/ArgMax.hpp"
#include "aidge/operator/AvgPooling.hpp" #include "aidge/operator/AvgPooling.hpp"
#include "aidge/operator/BatchNorm.hpp" #include "aidge/operator/BatchNorm.hpp"
......
/********************************************************************************
* Copyright (c) 2024 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
*
********************************************************************************/
#ifndef AIDGE_CORE_OPERATOR_AND_H_
#define AIDGE_CORE_OPERATOR_AND_H_
#include <memory>
#include <string>
#include <vector>
#include "aidge/utils/Registrar.hpp"
#include "aidge/operator/OperatorTensor.hpp"
#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/graph/Node.hpp"
#include "aidge/utils/Types.h"
namespace Aidge {
/**
* @brief Tensor element-wise logical and operation.
*/
class And_Op : public OperatorTensor,
public Registrable<And_Op, std::string, std::shared_ptr<OperatorImpl>(const And_Op&)> {
public:
static const std::string Type;
And_Op() : OperatorTensor(Type, {InputCategory::Data, InputCategory::Data}, 1) {}
/**
* @brief Copy-constructor. Copy the operator attributes and its output tensor(s),
* but not its input tensors (the new operator has no input associated).
* @param op Operator to copy.
*/
And_Op(const And_Op& op)
: OperatorTensor(op)
{
if (op.mImpl) {
SET_IMPL_MACRO(And_Op, *this, op.backend());
} else {
mImpl = nullptr;
}
}
/**
* @brief Clone the operator using its copy-constructor.
* @see Operator::And_Op
*/
std::shared_ptr<Operator> clone() const override {
return std::make_shared<And_Op>(*this);
}
bool forwardDims(bool allowDataDependency = false) override final;
void setBackend(const std::string& name, DeviceIdx_t device = 0) override;
static const std::vector<std::string> getInputsName(){
return {"data_input_1", "data_input_2"};
}
static const std::vector<std::string> getOutputsName(){
return {"data_output"};
}
};
inline std::shared_ptr<Node> And(const std::string& name = "") {
return std::make_shared<Node>(std::make_shared<And_Op>(), name);
}
} // namespace Aidge
#endif /* AIDGE_CORE_OPERATOR_AND_H_ */
/********************************************************************************
* Copyright (c) 2024 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 "aidge/data/Tensor.hpp"
#include "aidge/operator/And.hpp"
#include "aidge/operator/OperatorTensor.hpp"
namespace py = pybind11;
namespace Aidge {
void init_And(py::module& m) {
py::class_<And_Op, std::shared_ptr<And_Op>, OperatorTensor>(m, "AndOp", py::multiple_inheritance())
.def(py::init<>())
.def_static("get_inputs_name", &And_Op::getInputsName)
.def_static("get_outputs_name", &And_Op::getOutputsName);
declare_registrable<And_Op>(m, "AndOp");
m.def("And", &And, py::arg("name") = "");
}
} // namespace Aidge
...@@ -28,6 +28,7 @@ void init_Operator(py::module&); ...@@ -28,6 +28,7 @@ void init_Operator(py::module&);
void init_OperatorTensor(py::module&); void init_OperatorTensor(py::module&);
void init_Add(py::module&); void init_Add(py::module&);
void init_And(py::module&);
void init_ArgMax(py::module&); void init_ArgMax(py::module&);
void init_AvgPooling(py::module&); void init_AvgPooling(py::module&);
void init_BatchNorm(py::module&); void init_BatchNorm(py::module&);
...@@ -101,6 +102,7 @@ void init_Aidge(py::module& m) { ...@@ -101,6 +102,7 @@ void init_Aidge(py::module& m) {
init_Operator(m); init_Operator(m);
init_OperatorTensor(m); init_OperatorTensor(m);
init_Add(m); init_Add(m);
init_And(m);
init_ArgMax(m); init_ArgMax(m);
init_AvgPooling(m); init_AvgPooling(m);
init_BatchNorm(m); init_BatchNorm(m);
......
/********************************************************************************
* Copyright (c) 2024 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 <cstddef> // std::size_t
#include <memory>
#include <stdexcept> // std::runtime_error
#include <string>
#include <vector>
#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/data/Tensor.hpp"
#include "aidge/operator/And.hpp"
#include "aidge/utils/ErrorHandling.hpp"
#include "aidge/utils/Types.h"
const std::string Aidge::And_Op::Type = "And";
bool Aidge::And_Op::forwardDims(bool /*allowDataDependency*/) {
if (inputsAssociated()) {
const std::vector<std::size_t>& inputsDims0 = getInput(0)->dims();
const std::vector<std::size_t>& inputsDims1 = getInput(1)->dims();
std::vector<std::size_t> outDims = (inputsDims0.size() >= inputsDims1.size()) ? inputsDims0 : inputsDims1;
const std::vector<std::size_t>& lowDims = (inputsDims0.size() < inputsDims1.size()) ? inputsDims0 : inputsDims1;
std::size_t out_id = outDims.size() - 1;
std::size_t low_id = lowDims.size() - 1;
std::size_t i = 0;
while (i++ < lowDims.size()) {
if (outDims[out_id] == 1) {
outDims[out_id] = lowDims[low_id];
}
else if ((lowDims[low_id] != 1) && (lowDims[low_id] != outDims[out_id])) {
AIDGE_THROW_OR_ABORT(std::runtime_error, "Incompatible Tensor shape for And Operation: {} for input#0 vs {} for input#1",
inputsDims0, inputsDims1);
}
--out_id;
--low_id;
}
mOutputs[0]->resize(outDims);
return true;
}
return false;
}
void Aidge::And_Op::setBackend(const std::string& name, Aidge::DeviceIdx_t device) {
SET_IMPL_MACRO(And_Op, *this, name);
mOutputs[0]->setBackend(name, device);
}
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