| 251 | } |
| 252 | |
| 253 | Status AnalyzeResourceUsage( |
| 254 | const Graph* graph, const absl::optional<std::string>& function_name, |
| 255 | const int call_depth, const absl::flat_hash_set<int>& resource_arg_indices, |
| 256 | FunctionLibraryRuntime* lib_runtime, |
| 257 | absl::flat_hash_map<ResourceUsageAnalysis::NodeInfo, |
| 258 | absl::flat_hash_set<ResourceUsageAnalysis::NodeInfo>>* |
| 259 | source_to_path) { |
| 260 | source_to_path->clear(); |
| 261 | |
| 262 | std::vector<Node*> reverse_post_order; |
| 263 | GetReversePostOrder(*graph, &reverse_post_order, NodeComparatorName{}); |
| 264 | |
| 265 | // user_to_source maps from an edge carrying a Stack or TensorArray resource |
| 266 | // to the node that created this resource. |
| 267 | absl::flat_hash_map<const Edge*, ResourceUsageAnalysis::NodeInfo> |
| 268 | user_to_source; |
| 269 | for (const Node* n : reverse_post_order) { |
| 270 | if (IsControlFlowV1Node(n)) { |
| 271 | return errors::InvalidArgument( |
| 272 | "AnalyzeResourceUsage does not support control flow v1 node: ", |
| 273 | n->DebugString()); |
| 274 | } |
| 275 | |
| 276 | // TODO(ycao): Support pass-through functional while/if nodes. |
| 277 | if (n->type_string() == kIfOp || n->type_string() == kWhileOp) { |
| 278 | return errors::InvalidArgument( |
| 279 | "AnalyzeResourceUsage does not yet support control flow v2 " |
| 280 | "node: ", |
| 281 | n->DebugString()); |
| 282 | } |
| 283 | |
| 284 | // Record a resource source edge. |
| 285 | if (IsStackOrTensorArraySource(*n)) { |
| 286 | PropagateFromStackOrTensorArraySourceOp(*n, function_name, |
| 287 | &user_to_source); |
| 288 | continue; |
| 289 | } |
| 290 | |
| 291 | // Arguments that are listed in resource_arg_indices are also considered as |
| 292 | // resource sources. |
| 293 | if (n->IsArg()) { |
| 294 | TF_RETURN_IF_ERROR(PropagateFromArgOp( |
| 295 | *n, function_name, resource_arg_indices, &user_to_source)); |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | // Recursively analyze function call ops. |
| 300 | if (IsFunctionCall(*lib_runtime->GetFunctionLibraryDefinition(), *n)) { |
| 301 | TF_RETURN_IF_ERROR(PropagateThroughCallOp(*n, function_name, call_depth, |
| 302 | lib_runtime, &user_to_source, |
| 303 | source_to_path)); |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | if (n->IsIdentity() || n->type_string() == kIdentityNOp) { |
| 308 | TF_RETURN_IF_ERROR(PropagateThroughIdentityOp(*n, &user_to_source)); |
| 309 | } |
| 310 | } |
no test coverage detected