InitGraph creates a graph based on the graph_def, that may then be converted to an xla::XlaComputation via ConvertGraphToXla. The graph is rewritten with _Arg and _Retval nodes, representing the inputs and outputs of the function that will be compiled. Each feed id causes a new _Arg node to be created, where we first collect all existing edges pointing from the named node's output index, and the
| 360 | // with a new edge pointing from the named node's output index to that _Retval |
| 361 | // node. |
| 362 | Status InitGraph(const GraphDef& graph_def, const tf2xla::Config& config, |
| 363 | std::unique_ptr<Graph>* graph) { |
| 364 | TF_RETURN_IF_ERROR(ValidateConfig(config)); |
| 365 | |
| 366 | FunctionLibraryDefinition flib_def(OpRegistry::Global(), graph_def.library()); |
| 367 | std::unique_ptr<Graph> g(new Graph(flib_def)); |
| 368 | |
| 369 | // Replace references to fed tensors with references to newly added |
| 370 | // placeholders. |
| 371 | GraphDef first_copy_def = graph_def; |
| 372 | |
| 373 | // Maps from name:port of a feed to the name:port of the placeholder to use. |
| 374 | std::unordered_map<string, string> feed_remapping; |
| 375 | TF_RETURN_IF_ERROR(AddPlaceholdersForFeeds(config, g->op_registry(), |
| 376 | &feed_remapping, &first_copy_def)); |
| 377 | |
| 378 | // Prune the GraphDef first so that unknown ops that we aren't compiling get |
| 379 | // filtered out. |
| 380 | GraphDef second_copy_def; |
| 381 | TF_RETURN_IF_ERROR( |
| 382 | PruneGraphDefInto(config, first_copy_def, &second_copy_def)); |
| 383 | |
| 384 | TF_RETURN_IF_ERROR(AddDefaultAttrsToGraphDef( |
| 385 | &second_copy_def, *g->op_registry(), /*node_offset=*/0)); |
| 386 | |
| 387 | TF_RETURN_IF_ERROR(ConvertGraphDefToGraph( |
| 388 | GraphConstructorOptions(), std::move(second_copy_def), g.get())); |
| 389 | TF_RETURN_IF_ERROR(RewriteAndPruneGraph(g.get(), config, feed_remapping)); |
| 390 | |
| 391 | // Functionalize control flow. |
| 392 | TF_RETURN_IF_ERROR(FunctionalizeControlFlow(g.get(), &flib_def)); |
| 393 | // After control flow functionalization, we might have more FunctionDef's |
| 394 | // (then/else branch, loop body). Add them to the graph. |
| 395 | TF_RETURN_IF_ERROR(g->AddFunctionLibrary(flib_def.ToProto())); |
| 396 | |
| 397 | *graph = std::move(g); |
| 398 | return Status::OK(); |
| 399 | } |
| 400 | |
| 401 | } // namespace |
| 402 |
no test coverage detected