#include "Usage.h"
#include <stdexcept>
+#include <functional>
#include <nncc/core/ADT/kernel/NHWCLayout.h>
namespace
{
+using Appender = std::function<void(ANNBinder *binder)>;
+
class ANNOpBuilder : public coco::Instr::Visitor<void>
{
public:
public:
void visit(const coco::UnitF *unit)
{
- if (auto conv = unit->op()->asConv2D())
+ if (unit->op()->asConv2D())
+ {
+ auto f = conv2d(unit);
+ f(_binder);
+ }
+ else if (unit->op()->asReLU())
+ {
+ auto f = relu(unit);
+ f(_binder);
+ }
+ else if (unit->op()->asMaxPool2D())
+ {
+ auto f = maxpool2d(unit);
+ f(_binder);
+ }
+ else if (unit->op()->asAvgPool2D())
+ {
+ auto f = avgpool2d(unit);
+ f(_binder);
+ }
+ else if (unit->op()->asPadF())
{
+ auto f = pad(unit);
+ f(_binder);
+ }
+ else
+ {
+ throw std::runtime_error{"Not supported, yet"};
+ }
+ }
+
+public:
+ void visit(const coco::Shuffle *) { throw std::runtime_error{"Not supported, yet"}; }
+
+private:
+ Appender conv2d(const coco::UnitF *unit) const
+ {
+ auto _data = this->_data;
+ assert(unit->op()->asConv2D());
+ // TODO Rename "_binder"
+ return [unit, _data](ANNBinder *_binder) {
+ auto conv = unit->op()->asConv2D();
auto ifm = _binder->addOperand<float>(unit->ifm());
auto ker = _binder->addOperand<float>(conv->ker());
_binder->addOperation(ann::Operation::Code::CONV_2D,
{ifm, ker, bias, left, right, top, bottom, hstride, vstride, fuse},
{ofm});
- }
- else if (unit->op()->asReLU())
- {
+ };
+ }
+
+ Appender relu(const coco::UnitF *unit)
+ {
+ assert(unit->op()->asReLU());
+ // TODO Rename "_binder"
+ return [unit](ANNBinder *_binder) {
auto ifm = _binder->addOperand<float>(unit->ifm());
auto ofm = _binder->addOperand<float>(unit->ofm());
_binder->addOperation(ann::Operation::Code::RELU, {ifm}, {ofm});
- }
- else if (auto maxpool = unit->op()->asMaxPool2D())
- {
+ };
+ }
+
+ Appender maxpool2d(const coco::UnitF *unit)
+ {
+ assert(unit->op()->asMaxPool2D());
+ // TODO Rename "_binder"
+ return [unit](ANNBinder *_binder) {
+ auto maxpool = unit->op()->asMaxPool2D();
auto ifm = _binder->addOperand<float>(unit->ifm());
// Set padding
_binder->addOperation(ann::Operation::Code::MAX_POOL_2D,
{ifm, left, right, top, bottom, hstride, vstride, width, height, fuse},
{ofm});
- }
- else if (auto avgpool = unit->op()->asAvgPool2D())
- {
+ };
+ }
+
+ Appender avgpool2d(const coco::UnitF *unit)
+ {
+ assert(unit->op()->asAvgPool2D());
+ // TODO Rename "_binder"
+ return [unit](ANNBinder *_binder) {
+ auto avgpool = unit->op()->asAvgPool2D();
auto ifm = _binder->addOperand<float>(unit->ifm());
auto left = _binder->addOperand<int32_t>();
_binder->addOperation(ann::Operation::Code::AVG_POOL_2D,
{ifm, left, right, top, bottom, hstride, vstride, width, height, fuse},
{ofm});
- }
- else if (auto op = unit->op()->asPadF())
- {
+ };
+ }
+
+ Appender pad(const coco::UnitF *unit)
+ {
+ assert(unit->op()->asPadF());
+ // TODO Rename "_binder"
+ return [unit](ANNBinder *_binder) {
+ auto op = unit->op()->asPadF();
+
using nncc::core::ADT::tensor::Shape;
auto ifm = _binder->addOperand<float>(unit->ifm());
auto ofm = _binder->addOperand<float>(unit->ofm());
_binder->addOperation(ann::Operation::Code::PAD, {ifm, pad}, {ofm});
- }
- else
- {
- throw std::runtime_error{"Not supported, yet"};
- }
- }
-public:
- void visit(const coco::Shuffle *) { throw std::runtime_error{"Not supported, yet"}; }
+ };
+ }
private:
ANNBinder *const _binder;