From c84f629b9d2acf4a853a61da66f4bc81915fd568 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 21 Aug 2018 08:18:18 +0900 Subject: [PATCH] [enco] Introduce ANN OperandInventory (#1095) This commit introduces ANN OperandInventory class which serves as a factory and tracker of ANN Operand instances. Signed-off-by: Jonghyun Park --- contrib/enco/core/src/ANN/IR/OperandInventory.cpp | 41 ++++++++++++++++++++++ contrib/enco/core/src/ANN/IR/OperandInventory.h | 40 +++++++++++++++++++++ .../enco/core/src/ANN/IR/OperandInventory.test.cpp | 14 ++++++++ 3 files changed, 95 insertions(+) create mode 100644 contrib/enco/core/src/ANN/IR/OperandInventory.cpp create mode 100644 contrib/enco/core/src/ANN/IR/OperandInventory.h create mode 100644 contrib/enco/core/src/ANN/IR/OperandInventory.test.cpp diff --git a/contrib/enco/core/src/ANN/IR/OperandInventory.cpp b/contrib/enco/core/src/ANN/IR/OperandInventory.cpp new file mode 100644 index 0000000..231b305 --- /dev/null +++ b/contrib/enco/core/src/ANN/IR/OperandInventory.cpp @@ -0,0 +1,41 @@ +#include "ANN/IR/OperandInventory.h" + +#include + +using nncc::foundation::make_unique; + +namespace ann +{ + +OperandID OperandInventory::create(const DType &dtype) +{ + uint32_t id = _operands.size(); + + auto operand = make_unique(); + operand->dtype(dtype); + + _operands.emplace_back(std::move(operand)); + + return OperandID{id}; +} + +OperandID OperandInventory::create(const DType &dtype, const nncc::core::ADT::tensor::Shape &shape) +{ + uint32_t id = _operands.size(); + + auto operand = make_unique(shape); + operand->dtype(dtype); + + _operands.emplace_back(std::move(operand)); + + return OperandID{id}; +} + +Operand *OperandInventory::at(const OperandID &id) { return _operands.at(id.value()).get(); } + +const Operand *OperandInventory::at(const OperandID &id) const +{ + return _operands.at(id.value()).get(); +} + +} // namespace ann diff --git a/contrib/enco/core/src/ANN/IR/OperandInventory.h b/contrib/enco/core/src/ANN/IR/OperandInventory.h new file mode 100644 index 0000000..b058444 --- /dev/null +++ b/contrib/enco/core/src/ANN/IR/OperandInventory.h @@ -0,0 +1,40 @@ +#ifndef __ANN_IR_OPERAND_INVENTORY_H__ +#define __ANN_IR_OPERAND_INVENTORY_H__ + +#include "ANN/IR/OperandID.h" +#include "ANN/IR/Operand.h" + +#include + +#include +#include + +namespace ann +{ + +class OperandInventory +{ +public: + OperandID create(const DType &); + OperandID create(const DType &, const nncc::core::ADT::tensor::Shape &); + +public: + template void each(Callable &&cb) const + { + for (uint32_t n = 0; n < _operands.size(); ++n) + { + cb(OperandID{n}, _operands.at(n).get()); + } + } + +public: + Operand *at(const OperandID &id); + const Operand *at(const OperandID &id) const; + +private: + std::vector> _operands; +}; + +} // namespace ann + +#endif // __ANN_IR_OPERAND_INVENTORY_H__ diff --git a/contrib/enco/core/src/ANN/IR/OperandInventory.test.cpp b/contrib/enco/core/src/ANN/IR/OperandInventory.test.cpp new file mode 100644 index 0000000..ebe5324 --- /dev/null +++ b/contrib/enco/core/src/ANN/IR/OperandInventory.test.cpp @@ -0,0 +1,14 @@ +#include "OperandInventory.h" + +#include + +TEST(ANN_IR_OPERAND_INVENTORY, constructor) +{ + ann::OperandInventory inven; + + uint32_t count = 0; + + inven.each([&](const ann::OperandID &, const ann::Operand *) { ++count; }); + + ASSERT_EQ(count, 0); +} -- 2.7.4