| 164 | } |
| 165 | |
| 166 | void OpsUsedByGraph(const GraphDef& graph_def, |
| 167 | std::set<string>* ops_used_in_graph) { |
| 168 | // Map function names to definitions. |
| 169 | std::unordered_map<string, const FunctionDef*> name_to_function; |
| 170 | for (const auto& function : graph_def.library().function()) { |
| 171 | name_to_function.insert( |
| 172 | std::make_pair(function.signature().name(), &function)); |
| 173 | } |
| 174 | |
| 175 | // Collect the sorted list of op names. Since functions can reference |
| 176 | // functions, we need a recursive traversal. |
| 177 | std::set<string> used_ops; // Includes both primitive ops and functions |
| 178 | std::vector<const FunctionDef*> functions_to_process; // A subset of used_ops |
| 179 | // Collect the logic to mark an op in a lambda; it'll be used twice below. |
| 180 | const auto mark_op_as_used = [&used_ops, &functions_to_process, |
| 181 | &name_to_function](const string& op) { |
| 182 | if (used_ops.insert(op).second) { |
| 183 | // If it's a function, we'll need to process further |
| 184 | const auto it = name_to_function.find(op); |
| 185 | if (it != name_to_function.end()) { |
| 186 | functions_to_process.push_back(it->second); |
| 187 | } |
| 188 | } |
| 189 | }; |
| 190 | for (const auto& node : graph_def.node()) { |
| 191 | mark_op_as_used(node.op()); |
| 192 | } |
| 193 | while (!functions_to_process.empty()) { |
| 194 | const FunctionDef* fun = functions_to_process.back(); |
| 195 | functions_to_process.pop_back(); |
| 196 | for (const auto& node : fun->node_def()) { |
| 197 | mark_op_as_used(node.op()); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Filter out function names to produce output. |
| 202 | // TODO(josh11b): Change the above code to produce this directly. |
| 203 | ops_used_in_graph->clear(); |
| 204 | for (const string& op_name : used_ops) { |
| 205 | if (name_to_function.find(op_name) == name_to_function.end()) { |
| 206 | ops_used_in_graph->insert(op_name); |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | Status StrippedOpListForGraph(const GraphDef& graph_def, |
| 212 | const OpRegistryInterface& op_registry, |