* Sort the `SymbolicTensor`s topologically, for an array of fetches. * * This function calls getTopologicalSortAndRecipientCountsForOneFetch and * merges their results. * * @param fetch The array of fetches requested. Must be a non-empty array. * @param feedDict The dictionary of fed values.
(
fetches: SymbolicTensor[], feedDict: FeedDict)
| 393 | * recipientCounts: Recipient counts for all SymbolicTensors in `sorted`. |
| 394 | */ |
| 395 | function getTopologicalSortAndRecipientCounts( |
| 396 | fetches: SymbolicTensor[], feedDict: FeedDict): |
| 397 | {sorted: SymbolicTensor[], recipientCounts: RecipientCounts} { |
| 398 | util.assert( |
| 399 | fetches != null && fetches.length > 0, |
| 400 | () => `Expected at least one fetch, got none`); |
| 401 | |
| 402 | let finalSorted: SymbolicTensor[] = []; |
| 403 | let finalRecipientMap: RecipientMap = {}; |
| 404 | if (fetches.length === 1) { |
| 405 | // Special-casing 1 fetch for efficiency. |
| 406 | const out = |
| 407 | getTopologicalSortAndRecipientCountsForOneFetch(fetches[0], feedDict); |
| 408 | finalSorted = out.sorted; |
| 409 | finalRecipientMap = out.recipientMap; |
| 410 | } else { |
| 411 | const visited = new Set<string>(); |
| 412 | for (const fetch of fetches) { |
| 413 | const {sorted, recipientMap} = |
| 414 | getTopologicalSortAndRecipientCountsForOneFetch(fetch, feedDict); |
| 415 | |
| 416 | // Merge sorted SymbolicTensor Arrays. |
| 417 | for (const symbolicTensor of sorted) { |
| 418 | if (!visited.has(symbolicTensor.name)) { |
| 419 | finalSorted.push(symbolicTensor); |
| 420 | visited.add(symbolicTensor.name); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // Merge recipient maps. |
| 425 | for (const name in recipientMap) { |
| 426 | if (finalRecipientMap[name] == null) { |
| 427 | finalRecipientMap[name] = new Set<string>(); |
| 428 | } |
| 429 | recipientMap[name].forEach( |
| 430 | recipient => finalRecipientMap[name].add(recipient)); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | return { |
| 435 | sorted: finalSorted, |
| 436 | recipientCounts: recipientMap2Counts(finalRecipientMap) |
| 437 | }; |
| 438 | } |
| 439 | |
| 440 | function recipientMap2Counts(recipientMap: RecipientMap): RecipientCounts { |
| 441 | const recipientCounts: RecipientCounts = {}; |
no test coverage detected
searching dependent graphs…