2 #ifndef _NNDEPLOY_DAG_GRAPH_H_
3 #define _NNDEPLOY_DAG_GRAPH_H_
45 Graph(
const std::string &name, std::vector<Edge *> inputs,
46 std::vector<Edge *> outputs);
241 std::vector<std::shared_ptr<Edge>> inputs);
249 std::vector<std::shared_ptr<Edge>> outputs);
314 bool is_external =
true);
340 template <
typename T,
typename... Args,
341 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
352 template <
typename T,
typename... Args,
353 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
354 Node *
createNode(
const NodeDesc &desc, Args &...args);
485 int predecessor_port = 0,
int successor_port = 0);
496 int predecessor_port = 0,
int successor_port = 0);
509 std::map<std::string, std::shared_ptr<RunStatus>>
535 std::shared_ptr<base::Param> param);
543 const std::string &node_name);
552 std::shared_ptr<base::Param> param);
623 std::vector<Edge *> outputs);
683 virtual std::vector<Edge *>
forward(std::vector<Edge *> inputs);
690 virtual std::vector<Edge *>
operator()(std::vector<Edge *> inputs);
736 std::vector<Edge *>
trace(std::vector<Edge *> inputs);
771 const base::Any &value);
804 std::vector<Edge *> inputs,
805 std::vector<Edge *> outputs);
844 rapidjson::Document::AllocatorType &allocator);
917 const std::string &key,
const std::string &value);
924 std::map<std::string, std::map<std::string, std::string>> node_value_map);
930 virtual std::map<std::string, std::map<std::string, std::string>>
944 template <
typename T,
typename... Args,
945 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
959 template <
typename T,
typename... Args,
960 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
961 Node *
createNode(
const std::string &name,
const std::string &input_name,
962 const std::string &output_name, Args &...args);
974 template <
typename T,
typename... Args,
975 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
976 Node *
createNode(
const std::string &name, Edge *input,
977 const std::string &output_name, Args &...args);
989 template <
typename T,
typename... Args,
990 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
991 Node *
createNode(
const std::string &name,
const std::string &input_name,
992 Edge *output, Args &...args);
1004 template <
typename T,
typename... Args,
1005 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1006 Node *
createNode(
const std::string &name, std::vector<Edge *> inputs,
1007 std::vector<Edge *> outputs, Args &...args);
1019 template <
typename T,
typename... Args,
1020 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1022 std::vector<std::string> input_names,
1023 std::vector<std::string> output_names, Args &...args);
1035 template <
typename T,
typename... Args,
1036 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1038 std::vector<std::string> input_names,
1039 std::vector<Edge *> outputs, Args &...args);
1051 template <
typename T,
typename... Args,
1052 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1053 Node *
createNode(
const std::string &name, std::vector<Edge *> inputs,
1054 std::vector<std::string> output_names, Args &...args);
1066 template <
typename T,
typename... Args,
1067 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1069 std::initializer_list<Edge *> inputs,
1070 std::initializer_list<Edge *> outputs, Args &...args);
1082 template <
typename T,
typename... Args,
1083 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1085 std::initializer_list<std::string> input_names,
1086 std::initializer_list<std::string> output_names,
1099 template <
typename T,
typename... Args,
1100 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1102 std::initializer_list<Edge *> inputs,
1103 std::initializer_list<std::string> output_names,
1116 template <
typename T,
typename... Args,
1117 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1119 std::initializer_list<std::string> input_names,
1120 std::initializer_list<Edge *> outputs, Args &...args);
1132 template <
typename T,
typename... Args,
1133 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1135 Edge *input, Edge *output);
1147 template <
typename T,
typename... Args,
1148 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1150 const std::string &input_name,
1151 const std::string &output_name);
1163 template <
typename T,
typename... Args,
1164 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1166 Edge *input,
const std::string &output_name);
1178 template <
typename T,
typename... Args,
1179 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1181 const std::string &input_name, Edge *output);
1193 template <
typename T,
typename... Args,
1194 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1196 std::vector<Edge *> inputs, std::vector<Edge *> outputs);
1208 template <
typename T,
typename... Args,
1209 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1211 std::vector<std::string> input_names,
1212 std::vector<std::string> output_names);
1224 template <
typename T,
typename... Args,
1225 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1227 std::vector<Edge *> inputs,
1228 std::vector<std::string> output_names);
1240 template <
typename T,
typename... Args,
1241 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1243 std::vector<std::string> input_names,
1244 std::vector<Edge *> outputs);
1256 template <
typename T,
typename... Args,
1257 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1259 std::initializer_list<Edge *> inputs,
1260 std::initializer_list<Edge *> outputs);
1272 template <
typename T,
typename... Args,
1273 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1275 std::initializer_list<std::string> input_names,
1276 std::initializer_list<std::string> output_names);
1288 template <
typename T,
typename... Args,
1289 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1291 std::initializer_list<Edge *> inputs,
1292 std::initializer_list<std::string> output_names);
1304 template <
typename T,
typename... Args,
1305 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1307 std::initializer_list<std::string> input_names,
1308 std::initializer_list<Edge *> outputs);
1318 template <
typename T,
typename... Args,
1319 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type = 0>
1364 bool is_graph_node_share_stream_ =
true;
1373 int queue_max_size_ = 16;
1376 int queue_drop_count_ = 1;
1377 std::map<std::string, std::shared_ptr<base::Param>>
1379 bool is_loop_max_flag_ =
true;
1380 bool is_forward_api_ok_ =
true;
1382 bool is_remove_in_out_node_ =
false;
1418 template <
typename T,
typename... Args,
1419 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1422 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1425 std::vector<Edge *> inputs;
1426 std::vector<Edge *> outputs;
1427 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1430 node_wrapper->
node_ = node;
1431 node_wrapper->
name_ = name;
1441 template <
typename T,
typename... Args,
1442 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1446 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1449 Node *node =
dynamic_cast<Node *
>(
new T(name, {input}, {output}, args...));
1452 node_wrapper->
node_ = node;
1453 node_wrapper->
name_ = name;
1455 if (input_wrapper ==
nullptr) {
1456 input_wrapper = this->
addEdge(input);
1458 input_wrapper->
consumers_.emplace_back(node_wrapper);
1460 if (output_wrapper ==
nullptr) {
1461 output_wrapper = this->
addEdge(output);
1463 output_wrapper->
producers_.emplace_back(node_wrapper);
1475 template <
typename T,
typename... Args,
1476 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1478 const std::string &output_name, Args &...args) {
1480 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1484 if (input ==
nullptr) {
1488 if (output ==
nullptr) {
1491 Node *node =
dynamic_cast<Node *
>(
new T(name, {input}, {output}, args...));
1494 node_wrapper->
node_ = node;
1495 node_wrapper->
name_ = name;
1497 if (input_wrapper ==
nullptr) {
1498 input_wrapper = this->
addEdge(input);
1500 input_wrapper->
consumers_.emplace_back(node_wrapper);
1502 if (output_wrapper ==
nullptr) {
1503 output_wrapper = this->
addEdge(output);
1505 output_wrapper->
producers_.emplace_back(node_wrapper);
1516 template <
typename T,
typename... Args,
1517 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1519 const std::string &output_name, Args &...args) {
1521 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1525 if (output ==
nullptr) {
1528 Node *node =
dynamic_cast<Node *
>(
new T(name, {input}, {output}, args...));
1531 node_wrapper->
node_ = node;
1532 node_wrapper->
name_ = name;
1534 if (input_wrapper ==
nullptr) {
1535 input_wrapper = this->
addEdge(input);
1537 input_wrapper->
consumers_.emplace_back(node_wrapper);
1539 if (output_wrapper ==
nullptr) {
1540 output_wrapper = this->
addEdge(output);
1542 output_wrapper->
producers_.emplace_back(node_wrapper);
1554 template <
typename T,
typename... Args,
1555 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1557 Edge *output, Args &...args) {
1559 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1563 if (input ==
nullptr) {
1566 Node *node =
dynamic_cast<Node *
>(
new T(name, {input}, {output}, args...));
1569 node_wrapper->
node_ = node;
1570 node_wrapper->
name_ = name;
1572 if (input_wrapper ==
nullptr) {
1573 input_wrapper = this->
addEdge(input);
1575 input_wrapper->
consumers_.emplace_back(node_wrapper);
1577 if (output_wrapper ==
nullptr) {
1578 output_wrapper = this->
addEdge(output);
1580 output_wrapper->
producers_.emplace_back(node_wrapper);
1592 template <
typename T,
typename... Args,
1593 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1595 std::vector<Edge *> outputs, Args &...args) {
1597 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1600 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1603 node_wrapper->
node_ = node;
1604 node_wrapper->
name_ = name;
1605 for (
auto input : inputs) {
1607 if (input_wrapper ==
nullptr) {
1608 input_wrapper = this->
addEdge(input);
1610 input_wrapper->
consumers_.emplace_back(node_wrapper);
1612 for (
auto output : outputs) {
1614 if (output_wrapper ==
nullptr) {
1615 output_wrapper = this->
addEdge(output);
1617 output_wrapper->
producers_.emplace_back(node_wrapper);
1630 template <
typename T,
typename... Args,
1631 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1633 std::vector<std::string> input_names,
1634 std::vector<std::string> output_names, Args &...args) {
1636 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1639 std::vector<Edge *> inputs;
1640 for (
auto input_name : input_names) {
1642 if (input ==
nullptr) {
1645 inputs.emplace_back(input);
1647 std::vector<Edge *> outputs;
1648 for (
auto output_name : output_names) {
1650 if (output ==
nullptr) {
1653 outputs.emplace_back(output);
1655 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1658 node_wrapper->
node_ = node;
1659 node_wrapper->
name_ = name;
1660 for (
auto input : inputs) {
1662 if (input_wrapper ==
nullptr) {
1663 input_wrapper = this->
addEdge(input);
1665 input_wrapper->
consumers_.emplace_back(node_wrapper);
1667 for (
auto output : outputs) {
1669 if (output_wrapper ==
nullptr) {
1670 output_wrapper = this->
addEdge(output);
1672 output_wrapper->
producers_.emplace_back(node_wrapper);
1684 template <
typename T,
typename... Args,
1685 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1687 std::vector<std::string> input_names,
1688 std::vector<Edge *> outputs, Args &...args) {
1690 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1693 std::vector<Edge *> inputs;
1694 for (
auto input_name : input_names) {
1696 if (input ==
nullptr) {
1699 inputs.emplace_back(input);
1701 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1704 node_wrapper->
node_ = node;
1705 node_wrapper->
name_ = name;
1706 for (
auto input : inputs) {
1708 if (input_wrapper ==
nullptr) {
1709 input_wrapper = this->
addEdge(input);
1711 input_wrapper->
consumers_.emplace_back(node_wrapper);
1713 for (
auto output : outputs) {
1715 if (output_wrapper ==
nullptr) {
1716 output_wrapper = this->
addEdge(output);
1718 output_wrapper->
producers_.emplace_back(node_wrapper);
1730 template <
typename T,
typename... Args,
1731 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1733 std::vector<std::string> output_names, Args &...args) {
1735 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1738 std::vector<Edge *> outputs;
1739 for (
auto output_name : output_names) {
1741 if (output ==
nullptr) {
1744 outputs.emplace_back(output);
1746 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1749 node_wrapper->
node_ = node;
1750 node_wrapper->
name_ = name;
1751 for (
auto input : inputs) {
1753 if (input_wrapper ==
nullptr) {
1754 input_wrapper = this->
addEdge(input);
1756 input_wrapper->
consumers_.emplace_back(node_wrapper);
1758 for (
auto output : outputs) {
1760 if (output_wrapper ==
nullptr) {
1761 output_wrapper = this->
addEdge(output);
1763 output_wrapper->
producers_.emplace_back(node_wrapper);
1776 template <
typename T,
typename... Args,
1777 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1779 std::initializer_list<Edge *> inputs,
1780 std::initializer_list<Edge *> outputs, Args &...args) {
1782 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1785 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1788 node_wrapper->
node_ = node;
1789 node_wrapper->
name_ = name;
1790 for (
auto input : inputs) {
1792 if (input_wrapper ==
nullptr) {
1793 input_wrapper = this->
addEdge(input);
1795 input_wrapper->
consumers_.emplace_back(node_wrapper);
1797 for (
auto output : outputs) {
1799 if (output_wrapper ==
nullptr) {
1800 output_wrapper = this->
addEdge(output);
1802 output_wrapper->
producers_.emplace_back(node_wrapper);
1814 template <
typename T,
typename... Args,
1815 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1817 std::initializer_list<std::string> input_names,
1818 std::initializer_list<std::string> output_names,
1821 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1824 std::vector<Edge *> inputs;
1825 for (
auto input_name : input_names) {
1827 if (input ==
nullptr) {
1830 inputs.emplace_back(input);
1832 std::vector<Edge *> outputs;
1833 for (
auto output_name : output_names) {
1835 if (output ==
nullptr) {
1838 outputs.emplace_back(output);
1840 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1843 node_wrapper->
node_ = node;
1844 node_wrapper->
name_ = name;
1845 for (
auto input : inputs) {
1847 if (input_wrapper ==
nullptr) {
1848 input_wrapper = this->
addEdge(input);
1850 input_wrapper->
consumers_.emplace_back(node_wrapper);
1852 for (
auto output : outputs) {
1854 if (output_wrapper ==
nullptr) {
1855 output_wrapper = this->
addEdge(output);
1857 output_wrapper->
producers_.emplace_back(node_wrapper);
1870 template <
typename T,
typename... Args,
1871 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1873 std::initializer_list<Edge *> inputs,
1874 std::initializer_list<std::string> output_names,
1877 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1880 std::vector<Edge *> outputs;
1881 for (
auto output_name : output_names) {
1883 if (output ==
nullptr) {
1886 outputs.emplace_back(output);
1888 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1891 node_wrapper->
node_ = node;
1892 node_wrapper->
name_ = name;
1893 for (
auto input : inputs) {
1895 if (input_wrapper ==
nullptr) {
1896 input_wrapper = this->
addEdge(input);
1898 input_wrapper->
consumers_.emplace_back(node_wrapper);
1900 for (
auto output : outputs) {
1902 if (output_wrapper ==
nullptr) {
1903 output_wrapper = this->
addEdge(output);
1905 output_wrapper->
producers_.emplace_back(node_wrapper);
1918 template <
typename T,
typename... Args,
1919 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1921 std::initializer_list<std::string> input_names,
1922 std::initializer_list<Edge *> outputs, Args &...args) {
1924 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1927 std::vector<Edge *> inputs;
1928 for (
auto input_name : input_names) {
1930 if (input ==
nullptr) {
1933 inputs.emplace_back(input);
1935 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, args...));
1938 node_wrapper->
node_ = node;
1939 node_wrapper->
name_ = name;
1940 for (
auto input : inputs) {
1942 if (input_wrapper ==
nullptr) {
1943 input_wrapper = this->
addEdge(input);
1945 input_wrapper->
consumers_.emplace_back(node_wrapper);
1947 for (
auto output : outputs) {
1949 if (output_wrapper ==
nullptr) {
1950 output_wrapper = this->
addEdge(output);
1952 output_wrapper->
producers_.emplace_back(node_wrapper);
1965 template <
typename T,
typename... Args,
1966 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
1970 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
1973 Node *node =
dynamic_cast<Node *
>(
new T(name, {input}, {output}, type));
1976 node_wrapper->
node_ = node;
1977 node_wrapper->
name_ = name;
1979 if (input_wrapper ==
nullptr) {
1980 input_wrapper = this->
addEdge(input);
1982 input_wrapper->
consumers_.emplace_back(node_wrapper);
1984 if (output_wrapper ==
nullptr) {
1985 output_wrapper = this->
addEdge(output);
1987 output_wrapper->
producers_.emplace_back(node_wrapper);
1999 template <
typename T,
typename... Args,
2000 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2002 const std::string &input_name,
2003 const std::string &output_name) {
2005 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2009 if (input ==
nullptr) {
2013 if (output ==
nullptr) {
2016 Node *node =
dynamic_cast<Node *
>(
new T(name, {input}, {output}, type));
2019 node_wrapper->
node_ = node;
2020 node_wrapper->
name_ = name;
2022 if (input_wrapper ==
nullptr) {
2023 input_wrapper = this->
addEdge(input);
2025 input_wrapper->
consumers_.emplace_back(node_wrapper);
2027 if (output_wrapper ==
nullptr) {
2028 output_wrapper = this->
addEdge(output);
2030 output_wrapper->
producers_.emplace_back(node_wrapper);
2042 template <
typename T,
typename... Args,
2043 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2045 Edge *input,
const std::string &output_name) {
2047 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2051 if (output ==
nullptr) {
2054 Node *node =
dynamic_cast<Node *
>(
new T(name, {input}, {output}, type));
2057 node_wrapper->
node_ = node;
2058 node_wrapper->
name_ = name;
2060 if (input_wrapper ==
nullptr) {
2061 input_wrapper = this->
addEdge(input);
2063 input_wrapper->
consumers_.emplace_back(node_wrapper);
2065 if (output_wrapper ==
nullptr) {
2066 output_wrapper = this->
addEdge(output);
2068 output_wrapper->
producers_.emplace_back(node_wrapper);
2080 template <
typename T,
typename... Args,
2081 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2083 const std::string &input_name,
Edge *output) {
2085 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2089 if (input ==
nullptr) {
2092 Node *node =
dynamic_cast<Node *
>(
new T(name, {input}, {output}, type));
2095 node_wrapper->
node_ = node;
2096 node_wrapper->
name_ = name;
2098 if (input_wrapper ==
nullptr) {
2099 input_wrapper = this->
addEdge(input);
2101 input_wrapper->
consumers_.emplace_back(node_wrapper);
2103 if (output_wrapper ==
nullptr) {
2104 output_wrapper = this->
addEdge(output);
2106 output_wrapper->
producers_.emplace_back(node_wrapper);
2115 template <
typename T,
typename... Args,
2116 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2118 std::vector<Edge *> inputs,
2119 std::vector<Edge *> outputs) {
2121 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2124 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, type));
2127 node_wrapper->
node_ = node;
2128 node_wrapper->
name_ = name;
2129 for (
auto input : inputs) {
2131 if (input_wrapper ==
nullptr) {
2132 input_wrapper = this->
addEdge(input);
2134 input_wrapper->
consumers_.emplace_back(node_wrapper);
2136 for (
auto output : outputs) {
2138 if (output_wrapper ==
nullptr) {
2139 output_wrapper = this->
addEdge(output);
2141 output_wrapper->
producers_.emplace_back(node_wrapper);
2151 template <
typename T,
typename... Args,
2152 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2154 std::vector<std::string> input_names,
2155 std::vector<std::string> output_names) {
2157 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2160 std::vector<Edge *> inputs;
2161 for (
auto input_name : input_names) {
2163 if (input ==
nullptr) {
2166 inputs.emplace_back(input);
2168 std::vector<Edge *> outputs;
2169 for (
auto output_name : output_names) {
2171 if (output ==
nullptr) {
2174 outputs.emplace_back(output);
2176 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, type));
2179 node_wrapper->
node_ = node;
2180 node_wrapper->
name_ = name;
2181 for (
auto input : inputs) {
2183 if (input_wrapper ==
nullptr) {
2184 input_wrapper = this->
addEdge(input);
2186 input_wrapper->
consumers_.emplace_back(node_wrapper);
2188 for (
auto output : outputs) {
2190 if (output_wrapper ==
nullptr) {
2191 output_wrapper = this->
addEdge(output);
2193 output_wrapper->
producers_.emplace_back(node_wrapper);
2203 template <
typename T,
typename... Args,
2204 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2206 std::vector<Edge *> inputs,
2207 std::vector<std::string> output_names) {
2209 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2212 std::vector<Edge *> outputs;
2213 for (
auto output_name : output_names) {
2215 if (output ==
nullptr) {
2218 outputs.emplace_back(output);
2220 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, type));
2223 node_wrapper->
node_ = node;
2224 node_wrapper->
name_ = name;
2225 for (
auto input : inputs) {
2227 if (input_wrapper ==
nullptr) {
2228 input_wrapper = this->
addEdge(input);
2230 input_wrapper->
consumers_.emplace_back(node_wrapper);
2232 for (
auto output : outputs) {
2234 if (output_wrapper ==
nullptr) {
2235 output_wrapper = this->
addEdge(output);
2237 output_wrapper->
producers_.emplace_back(node_wrapper);
2247 template <
typename T,
typename... Args,
2248 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2250 std::vector<std::string> input_names,
2251 std::vector<Edge *> outputs) {
2253 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2256 std::vector<Edge *> inputs;
2257 for (
auto input_name : input_names) {
2259 if (input ==
nullptr) {
2262 inputs.emplace_back(input);
2264 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, type));
2267 node_wrapper->
node_ = node;
2268 node_wrapper->
name_ = name;
2269 for (
auto input : inputs) {
2271 if (input_wrapper ==
nullptr) {
2272 input_wrapper = this->
addEdge(input);
2274 input_wrapper->
consumers_.emplace_back(node_wrapper);
2276 for (
auto output : outputs) {
2278 if (output_wrapper ==
nullptr) {
2279 output_wrapper = this->
addEdge(output);
2281 output_wrapper->
producers_.emplace_back(node_wrapper);
2291 template <
typename T,
typename... Args,
2292 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2294 std::initializer_list<Edge *> inputs,
2295 std::initializer_list<Edge *> outputs) {
2297 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2300 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, type));
2303 node_wrapper->
node_ = node;
2304 node_wrapper->
name_ = name;
2305 for (
auto input : inputs) {
2307 if (input_wrapper ==
nullptr) {
2308 input_wrapper = this->
addEdge(input);
2310 input_wrapper->
consumers_.emplace_back(node_wrapper);
2312 for (
auto output : outputs) {
2314 if (output_wrapper ==
nullptr) {
2315 output_wrapper = this->
addEdge(output);
2317 output_wrapper->
producers_.emplace_back(node_wrapper);
2327 template <
typename T,
typename... Args,
2328 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2330 std::initializer_list<std::string> input_names,
2331 std::initializer_list<std::string> output_names) {
2333 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2336 std::vector<Edge *> inputs;
2337 for (
auto input_name : input_names) {
2339 if (input ==
nullptr) {
2342 inputs.emplace_back(input);
2344 std::vector<Edge *> outputs;
2345 for (
auto output_name : output_names) {
2347 if (output ==
nullptr) {
2350 outputs.emplace_back(output);
2352 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, type));
2355 node_wrapper->
node_ = node;
2356 node_wrapper->
name_ = name;
2357 for (
auto input : inputs) {
2359 if (input_wrapper ==
nullptr) {
2360 input_wrapper = this->
addEdge(input);
2362 input_wrapper->
consumers_.emplace_back(node_wrapper);
2364 for (
auto output : outputs) {
2366 if (output_wrapper ==
nullptr) {
2367 output_wrapper = this->
addEdge(output);
2369 output_wrapper->
producers_.emplace_back(node_wrapper);
2379 template <
typename T,
typename... Args,
2380 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2382 std::initializer_list<Edge *> inputs,
2383 std::initializer_list<std::string> output_names) {
2385 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2388 std::vector<Edge *> outputs;
2389 for (
auto output_name : output_names) {
2391 if (output ==
nullptr) {
2394 outputs.emplace_back(output);
2396 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs, type));
2399 node_wrapper->
node_ = node;
2400 node_wrapper->
name_ = name;
2401 for (
auto input : inputs) {
2403 if (input_wrapper ==
nullptr) {
2404 input_wrapper = this->
addEdge(input);
2406 input_wrapper->
consumers_.emplace_back(node_wrapper);
2408 for (
auto output : outputs) {
2410 if (output_wrapper ==
nullptr) {
2411 output_wrapper = this->
addEdge(output);
2413 output_wrapper->
producers_.emplace_back(node_wrapper);
2423 template <
typename T,
typename... Args,
2424 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2426 std::initializer_list<std::string> input_names,
2427 std::initializer_list<Edge *> outputs) {
2429 NNDEPLOY_LOGE(
"node name[%s] is already used!\n", name.c_str());
2432 std::vector<Edge *> inputs;
2433 for (
auto input_name : input_names) {
2435 if (input ==
nullptr) {
2438 inputs.emplace_back(input);
2440 std::vector<Edge *> outputs_vec;
2441 for (
auto output : outputs) {
2442 outputs_vec.emplace_back(output);
2444 Node *node =
dynamic_cast<Node *
>(
new T(name, inputs, outputs_vec, type));
2447 node_wrapper->
node_ = node;
2448 node_wrapper->
name_ = name;
2449 for (
auto input : inputs) {
2451 if (input_wrapper ==
nullptr) {
2452 input_wrapper = this->
addEdge(input);
2454 input_wrapper->
consumers_.emplace_back(node_wrapper);
2456 for (
auto output : outputs) {
2458 if (output_wrapper ==
nullptr) {
2459 output_wrapper = this->
addEdge(output);
2461 output_wrapper->
producers_.emplace_back(node_wrapper);
2471 template <
typename T,
typename... Args,
2472 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2548 template <
typename T,
typename... Args,
2549 typename std::enable_if<std::is_base_of<Node, T>{},
int>::type>
2553 if (node ==
nullptr) {
2565 std::vector<std::string> model_value)>;
2571 const std::string &name,
2582 std::vector<std::string> model_value);
2586 Graph *graph, rapidjson::Value &json,
2587 rapidjson::Document::AllocatorType &allocator);
2590 const std::string &path);
std::vector< NodeWrapper * > producers_
std::vector< NodeWrapper * > consumers_
Edge class in DAG graph for connecting nodes and transferring data.
Directed Acyclic Graph Node.
base::Status removeOtherUrl(const std::string &url)
Remove other type URL.
std::vector< Edge * > trace(Edge *input)
Trace execution (single input version)
virtual std::vector< Edge * > operator()(std::vector< Edge * > inputs)
Operator overload (multiple inputs version)
virtual base::Status setInputsSharedPtr(std::vector< std::shared_ptr< Edge >> inputs)
Set input edge list (shared pointer version)
base::Status connect(Node *predecessor, Node *successor, int predecessor_port=0, int successor_port=0)
Connect two nodes.
Node * createNode4Py(const std::string &key, const std::string &name="")
Create node for Python.
std::vector< EdgeWrapper * > edge_repository_
Edge repository.
bool isForwardApiOk()
Check if forward API is working properly.
std::vector< std::string > getVideoUrl() const
Get all video URL list.
virtual base::Status defaultParam()
Set default parameters.
EdgeWrapper * addEdge(Edge *edge, bool is_external=true)
Add edge to graph.
virtual base::Status init()
Initialize graph.
virtual std::map< std::string, int > getLoopCountMap()
Get loop count map.
base::Status addNodeSharedPtr(std::shared_ptr< Node > node)
Add node to graph (shared pointer version)
base::Status addImageUrl(const std::string &url)
Add image URL.
Node * getNode(const std::string &name)
Get node by name.
std::map< std::string, std::shared_ptr< RunStatus > > getNodesRunStatusRecursive()
Recursively get run status of all nodes (including nodes in subgraphs)
std::vector< std::string > getModelUrl() const
Get all model URL list.
std::shared_ptr< base::Param > getExternalParam(const std::string &key)
Get external parameter.
virtual std::set< std::string > getUnusedNodeNames()
Get unused node name set.
virtual base::Any & getResourceWithoutState(const std::string &key)
Get stateless global resource.
virtual base::Status deserialize(const std::string &json_str)
Deserialize from JSON string.
Graph(const std::string &name)
Constructor.
Edge * createEdge(const std::string &name)
Create edge.
base::Status updateNodeIO(Node *node, std::vector< Edge * > inputs, std::vector< Edge * > outputs)
Update node's input and output.
std::vector< Edge * > trace()
Trace execution (no input version)
std::vector< Node * > getNodesRecursive()
Recursively get all nodes (including nodes in subgraphs)
std::vector< std::string > getNodesName()
Get all node names.
int getEdgeQueueDropCount()
Get edge queue drop count.
std::vector< NodeWrapper * > run_node_repository_
Run node repository.
std::shared_ptr< Node > getNodeSharedPtr(const std::string &name)
Get node by name (shared pointer version)
virtual base::Status setOutputs(std::vector< Edge * > outputs)
Set output edge list.
bool getGraphNodeShareStream()
Get whether graph nodes share stream.
std::vector< std::string > other_url_
Other URL list.
Node * createNode4Py(const NodeDesc &desc)
Create node for Python by node description.
base::Status setEdgeQueueOverflowPolicy(base::QueueOverflowPolicy policy, int drop_count=1)
Set edge queue overflow policy.
base::Status setNodeDesc(Node *node, const NodeDesc &desc)
Set node description.
base::Status removeAudioUrl(const std::string &url)
Remove audio URL.
base::Status addOtherUrl(const std::string &url)
Add other type URL.
Node * getNode(int index)
Get node by index.
base::Status addNode(Node *node, bool is_external=true)
Add node to graph.
NodeWrapper * getNodeWrapper(const std::string &name)
Get node wrapper by name.
EdgeWrapper * getEdgeWrapper(const std::string &name)
Get edge wrapper by name.
virtual void removeInOutNode()
Remove input/output nodes.
std::map< std::string, std::map< std::string, std::string > > node_value_map_
base::Status markInputEdge(std::vector< Edge * > inputs)
Mark input edges.
void setGraphNodeShareStream(bool flag)
Set whether graph nodes share stream.
base::QueueOverflowPolicy getEdgeQueueOverflowPolicy()
Get edge queue overflow policy.
std::map< std::string, base::Any > resource_without_state_
Global resources (stateless)
Node * createNode(const std::string &key, const std::string &name="")
Create node by key.
std::vector< NodeWrapper * > node_repository_
Node repository.
virtual void setLoopMaxFlag(bool is_loop_max_flag)
Set loop max flag.
std::vector< std::string > getAudioUrl() const
Get all audio URL list.
base::Status removeVideoUrl(const std::string &url)
Remove video URL.
base::Status addNodeInputAndOutput(NodeWrapper *node_wrapper, std::vector< Edge * > inputs, std::vector< Edge * > outputs)
Add node's input and output.
Node * getOutputNode(int index)
Get output node by index.
virtual base::Status setInputSharedPtr(std::shared_ptr< Edge > input, int index=-1)
Set input edge (shared pointer version)
Node * getNodeByKey(const std::string &key)
Get node by key.
virtual std::vector< Edge * > forward(std::vector< Edge * > inputs)
Forward propagation (multiple inputs version)
virtual base::Status addResourceWithoutState(const std::string &key, const base::Any &value)
Add stateless global resource.
virtual base::Status setOutputSharedPtr(std::shared_ptr< Edge > output, int index=-1)
Set output edge (shared pointer version)
virtual base::Status executor()
Execute graph.
base::Status setNodeParam(const std::string &node_name, base::Param *param)
Set node parameter.
std::vector< std::string > getImageUrl() const
Get all image URL list.
virtual std::vector< Edge * > forward()
Forward propagation (no input version)
virtual base::Status setOutputsSharedPtr(std::vector< std::shared_ptr< Edge >> outputs)
Set output edge list (shared pointer version)
virtual void setNodeValue(const std::string &node_value_str)
Set node value (string format)
base::Status addModelUrl(const std::string &url)
Add model URL.
std::vector< std::shared_ptr< Node > > shared_node_repository_
Shared node repository.
base::Status updteEdge(EdgeWrapper *edge_wrapper, Edge *edge, bool is_external=true)
Update edge.
std::shared_ptr< Edge > getEdgeSharedPtr(const std::string &name)
Get edge by name (shared pointer version)
virtual bool getLoopMaxFlag()
Get loop max flag.
base::Status disconnect(Node *predecessor, Node *successor, int predecessor_port=0, int successor_port=0)
Disconnect two nodes.
virtual int getLoopCount()
Get loop count.
Node * getInputNode(int index)
Get input node by index.
base::Status setNodeParamSharedPtr(const std::string &node_name, std::shared_ptr< base::Param > param)
Set node parameter (shared pointer version)
virtual std::map< std::string, std::map< std::string, std::string > > getNodeValue()
Get node value map.
base::Status removeModelUrl(const std::string &url)
Remove model URL.
std::vector< Node * > getNodes()
Get all nodes.
virtual void setUnusedNodeNames(const std::string &node_name)
Set unused node name.
virtual void removeUnusedNodeNames(const std::set< std::string > &node_names)
Remove unused node name set.
virtual void setLoopCount(int loop_count)
Set loop count.
Edge * getEdge(const std::string &name)
Get edge by name.
Graph(const std::string &name, std::vector< Edge * > inputs, std::vector< Edge * > outputs)
Constructor.
virtual base::Status serialize(rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
Serialize to JSON.
std::map< std::string, std::shared_ptr< RunStatus > > getNodesRunStatus()
Get run status of all nodes.
virtual base::Status run()
Run graph.
virtual void setTraceFlag(bool flag)
Set trace flag.
virtual bool synchronize()
Synchronize execution.
base::Status addAudioUrl(const std::string &url)
Add audio URL.
std::vector< std::shared_ptr< Edge > > shared_edge_repository_
Shared edge repository.
virtual base::Status addResourceWithState(const std::string &key, Edge *edge)
Add stateful global resource.
base::Status deleteEdge(Edge *edge)
Delete edge from graph.
base::Status setNodeParallelType(const std::string &node_name, base::ParallelType parallel_type)
Set node parallel type.
virtual base::Status construct()
Construct graph.
virtual base::Status setOutput(Edge *output, int index=-1)
Set output edge.
std::shared_ptr< Edge > createEdgeSharedPtr(const std::string &name)
Create edge (shared pointer version)
virtual base::Status deserialize(rapidjson::Value &json)
Deserialize from JSON.
virtual bool interrupt()
Interrupt execution.
std::vector< std::string > image_url_
Image URL list.
base::Status setExternalParam(const std::string &key, std::shared_ptr< base::Param > param)
Set external parameter.
virtual base::Status setInputs(std::vector< Edge * > inputs)
Set input edge list.
NodeWrapper * getNodeWrapper(Node *node)
Get node wrapper.
virtual ~Graph()
Destructor.
std::vector< std::string > getOtherUrl() const
Get all other type URL list.
base::Status dump(std::ostream &oss=std::cout)
Print graph information.
std::vector< Edge * > trace(std::vector< Edge * > inputs)
Trace execution (multiple inputs version)
base::Status removeImageUrl(const std::string &url)
Remove image URL.
EdgeWrapper * getEdgeWrapper(Edge *edge)
Get edge wrapper.
virtual std::string serialize()
Serialize to JSON string.
virtual std::vector< Edge * > operator()(Edge *input)
Operator overload (single input version)
std::shared_ptr< base::Param > getNodeParamSharedPtr(const std::string &node_name)
Get node parameter (shared pointer version)
base::Status markOutputEdge(std::vector< Edge * > outputs)
Mark output edges.
std::vector< std::string > video_url_
Video URL list.
std::map< std::string, std::shared_ptr< base::Param > > external_param_repository_
External parameter repository.
std::vector< std::string > audio_url_
Audio URL list.
std::map< std::string, Edge * > resource_with_state_
Global resources (stateful)
virtual base::Status toStaticGraph()
Convert to static graph.
std::vector< std::string > getNodesNameRecursive()
Recursively get all node names (including nodes in subgraphs)
virtual std::vector< Edge * > forward(Edge *input)
Forward propagation (single input version)
int getNodeCount()
Get total number of nodes.
virtual base::Status deinit()
Deinitialize graph.
EdgeWrapper * addEdgeSharedPtr(std::shared_ptr< Edge > edge)
Add edge to graph (shared pointer version)
virtual Edge * getResourceWithState(const std::string &key)
Get stateful global resource.
std::set< std::string > unused_node_names_
Unused node name set.
virtual std::vector< Edge * > operator()()
Operator overload (no input version)
std::set< std::string > used_node_names_
Used node name set.
base::Status deleteNode(Node *node)
Delete node from graph.
Node * createInfer(const std::string &name, base::InferenceType type, Edge *input, Edge *output)
Template method: Create inference node (single input single output)
std::vector< std::string > model_url_
Model URL list.
base::Status setEdgeQueueMaxSize(int queue_max_size)
Set maximum size of edge queue.
Node * createNode(const NodeDesc &desc)
Create node by node description.
virtual void setNodeValue(std::map< std::string, std::map< std::string, std::string >> node_value_map)
Set node value map.
base::Status addVideoUrl(const std::string &url)
Add video URL.
int getEdgeQueueMaxSize()
Get maximum size of edge queue.
std::vector< Node * > getNodesByKey(const std::string &key)
Get all nodes matching the key.
base::Param * getNodeParam(const std::string &node_name)
Get node parameter.
virtual base::Status setInput(Edge *input, int index=-1)
Set input edge.
virtual void setNodeValue(const std::string &node_name, const std::string &key, const std::string &value)
Set node value.
virtual base::Status removeUnusedNodeAndEdge()
Remove unused nodes and edges.
virtual void setUnusedNodeNames(const std::set< std::string > &node_names)
Set unused node name set.
Node * getInferNode(int index)
Get inference node by index.
std::shared_ptr< Executor > executor_
Executor.
virtual void removeUnusedNodeNames(const std::string &node_name)
Remove unused node name.
std::set< std::string > used_edge_names_
Used edge name set.
std::vector< std::string > getInputs() const
Get input edge name list.
std::string getName() const
Get node name.
std::vector< std::string > getOutputs() const
Get output edge name list.
base::Status setGraph(Graph *graph)
Set parent graph.
TypeGraphRegister(const std::string &name, createGraphFunc func)
#define NNDEPLOY_LOGE(fmt,...)
#define NNDEPLOY_CC_API
api
@ kQueueOverflowPolicyNodeBackpressure
std::map< std::string, createGraphFunc > & getGlobalGraphCreatorMap()
std::function< Graph *(const std::string &name, base::InferenceType inference_type, base::DeviceType device_type, Edge *input, Edge *output, base::ModelType model_type, bool is_path, std::vector< std::string > model_value)> createGraphFunc
Graph * loadFile(const std::string &path)
base::Status serialize(Graph *graph, rapidjson::Value &json, rapidjson::Document::AllocatorType &allocator)
EdgeWrapper * findEdgeWrapper(std::vector< EdgeWrapper * > &edge_repository, const std::string &edge_name)
Node * createNode(const std::string &node_key, const std::string &node_name)
Create node.
Graph * createGraph(const std::string &name, base::InferenceType inference_type, base::DeviceType device_type, Edge *input, Edge *output, base::ModelType model_type, bool is_path, std::vector< std::string > model_value)
Graph * deserialize(rapidjson::Value &json)
base::Status saveFile(Graph *graph, const std::string &path)