|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <memory> |
| 4 | +#include <vector> |
| 5 | + |
| 6 | +#include "../tensor.hpp" |
| 7 | + |
| 8 | +namespace infinicore::graph { |
| 9 | +// Forward declarations |
| 10 | +class GraphManager; |
| 11 | + |
| 12 | +class GraphTensor : public Tensor { |
| 13 | +public: |
| 14 | + GraphTensor(const Tensor &); |
| 15 | +}; |
| 16 | + |
| 17 | +class GraphOperator { |
| 18 | + |
| 19 | +public: |
| 20 | + void run() const; |
| 21 | + ~GraphOperator(); |
| 22 | + |
| 23 | +protected: |
| 24 | + using run_schema = void (*)(void *); |
| 25 | + using cleanup_schema = void (*)(void **); |
| 26 | + void *planned_meta_; |
| 27 | + run_schema runner_; |
| 28 | + cleanup_schema deleter_; |
| 29 | +}; |
| 30 | + |
| 31 | +class Graph { |
| 32 | +public: |
| 33 | + Graph() = default; |
| 34 | + ~Graph() = default; |
| 35 | + |
| 36 | + void run() const; |
| 37 | + |
| 38 | +protected: |
| 39 | + void add_operator(std::shared_ptr<GraphOperator> op); |
| 40 | + |
| 41 | + std::vector<std::shared_ptr<GraphOperator>> op_list_; |
| 42 | + |
| 43 | + friend class GraphManager; |
| 44 | +}; |
| 45 | +} // namespace infinicore::graph |
| 46 | + |
| 47 | +#define INFINICORE_GRAPH_OP_CLASS(__OP_NAME__, ...) \ |
| 48 | + class __OP_NAME__ : public graph::GraphOperator { \ |
| 49 | + public: \ |
| 50 | + using schema = void (*)(__VA_ARGS__); \ |
| 51 | + using plan_schema = void *(*)(__VA_ARGS__); \ |
| 52 | + static common::OpDispatcher<plan_schema> &plan_dispatcher(); \ |
| 53 | + static common::OpDispatcher<run_schema> &run_dispatcher(); \ |
| 54 | + static common::OpDispatcher<cleanup_schema> &cleanup_dispatcher(); \ |
| 55 | + __OP_NAME__(__VA_ARGS__); \ |
| 56 | + static void execute(__VA_ARGS__); \ |
| 57 | + }; |
| 58 | + |
| 59 | +#define INFINICORE_GRAPH_OP_DISPATCHERS_IMPL(__OP_NAME__) \ |
| 60 | + common::OpDispatcher<__OP_NAME__::plan_schema> &__OP_NAME__::plan_dispatcher() { \ |
| 61 | + static common::OpDispatcher<__OP_NAME__::plan_schema> dispatcher_; \ |
| 62 | + return dispatcher_; \ |
| 63 | + } \ |
| 64 | + common::OpDispatcher<__OP_NAME__::run_schema> &__OP_NAME__::run_dispatcher() { \ |
| 65 | + static common::OpDispatcher<__OP_NAME__::run_schema> dispatcher_; \ |
| 66 | + return dispatcher_; \ |
| 67 | + } \ |
| 68 | + common::OpDispatcher<__OP_NAME__::cleanup_schema> &__OP_NAME__::cleanup_dispatcher() { \ |
| 69 | + static common::OpDispatcher<__OP_NAME__::cleanup_schema> dispatcher_; \ |
| 70 | + return dispatcher_; \ |
| 71 | + } |
| 72 | + |
| 73 | +#define INFINICORE_GRAPH_OP_DISPATCH(__DEVICE_TYPE__, ...) \ |
| 74 | + planned_meta_ = plan_dispatcher().lookup(__DEVICE_TYPE__)(__VA_ARGS__); \ |
| 75 | + runner_ = run_dispatcher().lookup(__DEVICE_TYPE__); \ |
| 76 | + deleter_ = cleanup_dispatcher().lookup(__DEVICE_TYPE__); |
| 77 | + |
| 78 | +#define INFINICORE_GRAPH_OP_RECORD_OR_RUN(__OP_NAME__, ...) \ |
| 79 | + auto op = std::make_shared<__OP_NAME__>(__VA_ARGS__); \ |
| 80 | + if (context::isGraphRecording()) { \ |
| 81 | + context::addGraphOperator(op); \ |
| 82 | + } else { \ |
| 83 | + op->run(); \ |
| 84 | + } |
| 85 | + |
| 86 | +#define INFINICORE_GRAPH_OP_REGISTER_ALLDEVICE(__OP_NAME__, __PLAN_F__, __RUN_F__, __CLEANUP_F__) \ |
| 87 | + static bool registered = []() { \ |
| 88 | + __OP_NAME__::plan_dispatcher().registerAll(__PLAN_F__, false); \ |
| 89 | + __OP_NAME__::run_dispatcher().registerAll(__RUN_F__, false); \ |
| 90 | + __OP_NAME__::cleanup_dispatcher().registerAll(__CLEANUP_F__, false); \ |
| 91 | + return true; \ |
| 92 | + }(); |
0 commit comments