diff --git a/include/aidge/hook/execTime.hpp b/include/aidge/hook/execTime.hpp new file mode 100644 index 0000000000000000000000000000000000000000..212fef58696be702e89c8ad973dcc0dd0fc389ae --- /dev/null +++ b/include/aidge/hook/execTime.hpp @@ -0,0 +1,59 @@ +/** + * \file execTime.hpp + * \brief execTime structure + * \version file 1.0.0 + * \date Creation 27 June 2023 + * \date 27 June 2023 + * \par ChangeLog + * \par + * v1.0.0, 27 June 2023<br> + * - Initial version. + * \author mn271187, ik243221 + * \copyright + * Copyright (c) 2023 CEA, LIST, Embedded Artificial Intelligence Laboratory. All + * rights reserved. + */ + +#ifndef execTime_H_ +#define execTime_H_ + +#include "aidge/operator/Operator.hpp" +#include "aidge/hook/hook.hpp" +#include <memory> +#include <chrono> +#include <vector> + +namespace Aidge { + +class ExecTime : public Hook { +private: + std::vector<std::chrono::high_resolution_clock::time_point> registeredTimes = std::vector<std::chrono::high_resolution_clock::time_point>(); +public: + ExecTime(const std::shared_ptr<Operator> op) : Hook(op) {} + ~ExecTime() = default; + + void call() override final { + registeredTimes.push_back(std::chrono::high_resolution_clock::now()); + } + + static std::shared_ptr<ExecTime> create(const std::shared_ptr<Operator> op) + { + return std::make_shared<ExecTime>(op); + } + + std::vector<std::chrono::high_resolution_clock::time_point> getTimes() { + return registeredTimes; + } + + std::chrono::high_resolution_clock::time_point getTime(size_t idx) { + return registeredTimes[idx]; + } + +}; + +namespace { + static Registrar<Hook> registrarHook_ExecTime({"execution_time"}, Aidge::ExecTime::create); +} +} + +#endif /* execTime_H_ */ \ No newline at end of file diff --git a/include/aidge/hook/hook.hpp b/include/aidge/hook/hook.hpp new file mode 100644 index 0000000000000000000000000000000000000000..e6d6165c3e512770a892408d3c8164ef74be7822 --- /dev/null +++ b/include/aidge/hook/hook.hpp @@ -0,0 +1,41 @@ +/** + * \file Hook.hpp + * \brief Hook structure + * \version file 1.0.0 + * \date Creation 27 June 2023 + * \date 27 June 2023 + * \par ChangeLog + * \par + * v1.0.0, 27 June 2023<br> + * - Initial version. + * \author mn271187, ik243221 + * \copyright + * Copyright (c) 2023 CEA, LIST, Embedded Artificial Intelligence Laboratory. All + * rights reserved. + */ + +#ifndef Hook_H_ +#define Hook_H_ + +#include "aidge/utils/Parameter.hpp" +#include "aidge/utils/Registrar.hpp" +#include <memory> + +namespace Aidge { + +class Operator; +class Hook : public Registrable<Hook, std::tuple<std::string>, std::shared_ptr<Hook>(const std::shared_ptr<Operator>)> { +//class Hook : public Registrable<Hook, std::tuple<std::string>, std::shared_ptr<Hook>(const std::shared_ptr<Operator>)>{ +protected: + const std::shared_ptr<Operator> mOperator; + +public: + Hook(std::shared_ptr<Operator> op) : mOperator(op) {} + ~Hook() = default; + + virtual void call() = 0; + +}; +} + +#endif /* Hook_H_ */ \ No newline at end of file diff --git a/include/aidge/hook/outputRange.hpp b/include/aidge/hook/outputRange.hpp new file mode 100644 index 0000000000000000000000000000000000000000..268918cf765f6d00d4b20f261fada86b83dade2a --- /dev/null +++ b/include/aidge/hook/outputRange.hpp @@ -0,0 +1,74 @@ +/** + * \file execTime.hpp + * \brief execTime structure + * \version file 1.0.0 + * \date Creation 27 June 2023 + * \date 27 June 2023 + * \par ChangeLog + * \par + * v1.0.0, 27 June 2023<br> + * - Initial version. + * \author ik243221 + * \copyright + * Copyright (c) 2023 CEA, LIST, Embedded Artificial Intelligence Laboratory. All + * rights reserved. + */ + +#ifndef outputRange_H_ +#define outputRange_H_ + +#include "aidge/operator/Operator.hpp" +#include "aidge/hook/hook.hpp" +#include <memory> +#include <chrono> +#include <vector> + +namespace Aidge { + +class OutputRange : public Hook { +private: + std::vector<float> registeredOutputs = std::vector<float>(); +public: + OutputRange(const std::shared_ptr<Operator> op) : Hook(op) {} + ~OutputRange() = default; + + void call() override final { + //std::cout << "call() outputRange hook " << std::endl; + //this assumes there is only 1 output possible + std::shared_ptr<Tensor> tensor = std::static_pointer_cast<Tensor>(this->mOperator->getOutput(0)); + //tensor->print(); + //std::cout << "call() outputRange hook : tensor printed" << std::endl; + float max_value = 0.; + float * casted_tensor = static_cast<float *>(tensor->getImpl()->rawPtr()); + //find the absolute max value in the tensor, save it to registered outputs + for(size_t i = 0; i<tensor->size(); i++) { + //std::cout << "call() outputRange hook : casted_tensor[i] = " << casted_tensor[i] << std::endl; + if(abs(casted_tensor[i]) > max_value){ + max_value = abs(casted_tensor[i]); + } + } + //std::cout << "call() outputRange hook : max_value = " << max_value << std::endl; + registeredOutputs.push_back(max_value); + } + + static std::shared_ptr<OutputRange> create(const std::shared_ptr<Operator> op) + { + return std::make_shared<OutputRange>(op); + } + + std::vector<float> getOutputs() { + return registeredOutputs; + } + + float getOutput(size_t idx) { + return registeredOutputs[idx]; + } + +}; + +namespace { + static Registrar<Hook> registrarHook_OutputRange({"output_range"}, Aidge::OutputRange::create); +} +} + +#endif /* outputRange_H_ */ \ No newline at end of file diff --git a/include/aidge/operator/Operator.hpp b/include/aidge/operator/Operator.hpp index 9f24ce884863776f6856ee03fb4feb089e6323e2..3dc25f2dc9e3888adfbe557d057e23a3f08a414e 100644 --- a/include/aidge/operator/Operator.hpp +++ b/include/aidge/operator/Operator.hpp @@ -20,12 +20,14 @@ #include "aidge/data/Data.hpp" #include "aidge/data/Tensor.hpp" #include "aidge/utils/Types.h" +#include "aidge/hook/hook.hpp" namespace Aidge { class Operator : public std::enable_shared_from_this<Operator> { protected: std::unique_ptr<OperatorImpl> mImpl; // implementation of the operator + std::map<std::string, std::shared_ptr<Hook>> mHooks; private: std::string mType; @@ -48,6 +50,15 @@ public: virtual std::shared_ptr<Tensor> getOutput(const IOIndex_t outputIdx) const = 0; virtual Tensor& output(const IOIndex_t /*outputIdx*/) const = 0; + std::shared_ptr<Hook>& getHook(std::string hookName) { + return mHooks[hookName]; + } + void addHook(std::string hookName) { + mHooks.insert(std::pair<std::string, std::shared_ptr<Hook>>(hookName,Registrar<Hook>::create({hookName})(shared_from_this()))); + } + + void runHooks() const; + /////////////////////////////////////////////////////// // IMPLEMENTATION /////////////////////////////////////////////////////// diff --git a/src/operator/Operator.cpp b/src/operator/Operator.cpp index 99b07235e2917527160f03af997747f02947dcf9..d776a13441d8f16446cc195c8fd893527b321ff9 100644 --- a/src/operator/Operator.cpp +++ b/src/operator/Operator.cpp @@ -39,6 +39,14 @@ Aidge::NbElts_t Aidge::Operator::getNbProducedData(Aidge::IOIndex_t outputIdx) c return mImpl->getNbProducedData(outputIdx); } -void Aidge::Operator::forward() { mImpl->forward(); } +void Aidge::Operator::runHooks() const { + for (auto& hook : mHooks) { + hook.second->call(); + } +} +void Aidge::Operator::forward() { + mImpl->forward(); + runHooks(); +} void Aidge::Operator::backward() { mImpl->backward(); }