1 /** 2 * Copyright 2022 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_C_API_INCLUDE_GRAPH_H_ 18 #define MINDSPORE_CCSRC_C_API_INCLUDE_GRAPH_H_ 19 20 #include <stdbool.h> 21 #include <stdlib.h> 22 #include "include/c_api/ms/node.h" 23 #include "include/c_api/ms/base/macros.h" 24 #include "include/c_api/ms/base/status.h" 25 #include "include/c_api/ms/context.h" 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif 30 31 /// \brief Creates an empty function graph. 32 /// 33 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 34 /// 35 /// \return The created function graph. 36 MIND_C_API GraphHandle MSFuncGraphCreate(ResMgrHandle res_mgr); 37 38 /// \brief Load function graph from MINDIR. 39 /// 40 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 41 /// \param[in] file_path The path of MINDIR. 42 /// 43 /// \return The loaded function graph. 44 MIND_C_API GraphHandle MSFuncGraphLoad(ResMgrHandle res_mgr, const char *file_path); 45 46 /// \brief Get the input node of the function graph. 47 /// 48 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 49 /// \param[in] graph The given function graph pointer handle. 50 /// \param[in] i Index of the input node. 51 /// 52 /// \return The created function graph. 53 MIND_C_API NodeHandle MSFuncGraphGetInput(ResMgrHandle res_mgr, ConstGraphHandle graph, size_t i); 54 55 /// \brief Get the inputs number of the function graph. 56 /// 57 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 58 /// \param[in] graph The given function graph pointer handle. 59 /// \param[in] error Records error code that indicate whether the functions executed successfully. 60 /// 61 /// \return the inputs number of the function graph. 62 MIND_C_API size_t MSFuncGraphGetInputNum(ResMgrHandle res_mgr, ConstGraphHandle graph, STATUS *error); 63 64 /// \brief Get all inputs of the function graph. 65 /// 66 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 67 /// \param[in] graph The given function graph pointer handle. 68 /// \param[in] inputs The array to contain input nodes. 69 /// \param[in] input_num The length of the array. 70 /// 71 /// \return Error code that indicate whether the functions executed successfully. 72 MIND_C_API STATUS MSFuncGraphGetInputs(ResMgrHandle res_mgr, ConstGraphHandle graph, NodeHandle inputs[], 73 size_t input_num); 74 75 /// \brief Set the output node. 76 /// 77 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 78 /// \param[in] graph The given function graph pointer handle. 79 /// \param[in] op_node The output operator node to be set. 80 /// \param[in] force_new_ret If true, a new return node is always created. 81 /// 82 /// \return Error code that indicate whether the functions executed successfully. 83 MIND_C_API STATUS MSFuncGraphSetOutput(ResMgrHandle res_mgr, GraphHandle graph, ConstNodeHandle op_node, 84 bool force_new_ret); 85 86 /// \brief Set the output node. 87 /// 88 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 89 /// \param[in] graph The given function graph pointer handle. 90 /// \param[in] outputs The array of output operator nodes. 91 /// \param[in] force_new_ret If true, a new return node is always created. 92 /// 93 /// \return Error code that indicate whether the functions executed successfully. 94 MIND_C_API STATUS MSFuncGraphSetOutputs(ResMgrHandle res_mgr, GraphHandle graph, Handle const outputs[], 95 size_t output_num, bool force_new_ret); 96 97 /// \brief Get the output node according to the index. 98 /// 99 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 100 /// \param[in] graph The given function graph pointer handle. 101 /// \param[in] i The index to get the output. If there is only one output for graph, the i should be 0; 102 /// 103 /// \return The output node, nullptr if output not set. 104 MIND_C_API NodeHandle MSFuncGraphGetOutput(ResMgrHandle res_mgr, ConstGraphHandle graph, size_t i); 105 106 /// \brief Get the outputs number of the function graph. 107 /// 108 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 109 /// \param[in] graph The given function graph pointer handle. 110 /// \param[in] error Records error code that indicate whether the functions executed successfully. 111 /// 112 /// \return the outputs number of the function graph. 113 MIND_C_API size_t MSFuncGraphGetOutputNum(ResMgrHandle res_mgr, ConstGraphHandle graph, STATUS *error); 114 115 /// \brief Get all outputs of the function graph. 116 /// 117 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 118 /// \param[in] graph The given function graph pointer handle. 119 /// \param[in] outputs The array to contain input nodes. 120 /// \param[in] output_num The length of the array. 121 /// 122 /// \return Error code that indicate whether the functions executed successfully. 123 MIND_C_API STATUS MSFuncGraphGetOutputs(ResMgrHandle res_mgr, ConstGraphHandle graph, NodeHandle outputs[], 124 size_t output_num); 125 126 /// \brief Replace a node in a function graph. 127 /// 128 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 129 /// \param[in] graph The given function graph pointer handle. 130 /// \param[in] new_node The node needs to be replaced. 131 /// \param[in] new_node The replace node. 132 /// 133 /// \return Error code that indicate whether the functions executed successfully. 134 MIND_C_API STATUS MSFuncGraphReplace(ResMgrHandle res_mgr, GraphHandle graph, ConstNodeHandle old_node, 135 ConstNodeHandle new_node); 136 137 /// \brief Compile the function graph. 138 /// 139 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 140 /// \param[in] graph The given function graph pointer handle. 141 /// 142 /// \return Error code that indicate whether the function graph compiled successfully. 143 MIND_C_API STATUS MSFuncGraphCompile(ResMgrHandle res_mgr, GraphHandle graph, OptPassID *opt_pass, size_t pass_num); 144 145 /// \brief Run the function graph. 146 /// 147 /// \param[in] res_mgr Resource manager that saves allocated instance resources. 148 /// \param[in] graph The given function graph pointer handle. 149 /// \param[in] inputs The function graph (model) inputs which can be Values or Tensors. 150 /// \param[in] input_num The input size. 151 /// \param[in] outputs The function graph (model) outputs in Tensor form. 152 /// \param[in] outputs_num The output size. 153 /// 154 /// \return Error code that indicate whether the function graph executed successfully. 155 MIND_C_API STATUS MSFuncGraphRun(ResMgrHandle res_mgr, GraphHandle graph, Handle const inputs[], size_t input_num, 156 TensorHandle outputs[], size_t outputs_num); 157 #ifdef __cplusplus 158 } 159 #endif 160 #endif // MINDSPORE_CCSRC_C_API_INCLUDE_GRAPH_H_ 161