|
| 1 | +#include <unordered_map> |
| 2 | + |
| 3 | +#include <c10/util/Logging.h> |
| 4 | + |
| 5 | +#include <c10/util/Enumerate.h> |
| 6 | +#include <torch/nativert/executor/ExecutionPlanner.h> |
| 7 | + |
| 8 | +namespace torch::nativert { |
| 9 | + |
| 10 | +std::unique_ptr<ExecutionPlan> ExecutionPlanner::createPlan() { |
| 11 | + auto plan = std::make_unique<ExecutionPlan>(); |
| 12 | + |
| 13 | + // Current implementation assume that nodes will be executed |
| 14 | + // in the same order as the thrift graph. |
| 15 | + // In the future, we can do execution order plan, as long as it's |
| 16 | + // comply with topological order |
| 17 | + |
| 18 | + generateDeallocationPlan(*plan); |
| 19 | + return plan; |
| 20 | +} |
| 21 | + |
| 22 | +/* static */ c10::FastSet<ValueId> ExecutionPlanner::staticValues( |
| 23 | + const Graph& graph) { |
| 24 | + c10::FastSet<ValueId> staticValues; |
| 25 | + // Filter lastUsedBy by graph inputs |
| 26 | + // parameters/buffer values should not be freed |
| 27 | + // It's a policy decision to whether to free user inputs. For now, we don't |
| 28 | + // free user inputs. |
| 29 | + // TODO: It should be fine to "free" the user inputs. If the user holds a ref |
| 30 | + // to it, it won't be deallocated. |
| 31 | + for (const auto* input : graph.inputs()) { |
| 32 | + if (input) { |
| 33 | + const auto& id = input->id(); |
| 34 | + staticValues.insert(id); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // Filter lastUsedBy by graph outputs, as they are still needed to be returned |
| 39 | + for (const auto& output : graph.outputs()) { |
| 40 | + const auto& id = output->id(); |
| 41 | + staticValues.insert(id); |
| 42 | + } |
| 43 | + |
| 44 | + for (const auto& [id, _] : graph.getConstantSymIntValues()) { |
| 45 | + staticValues.insert(id); |
| 46 | + } |
| 47 | + |
| 48 | + for (const Node& node : graph.nodes()) { |
| 49 | + if (node.target() == "torch.ops.higher_order.run_const_graph") { |
| 50 | + for (const auto& output : node.outputs()) { |
| 51 | + // Do not free the outputs of run_const_graph, as they are newly |
| 52 | + // produced folded constants |
| 53 | + staticValues.insert(output->id()); |
| 54 | + } |
| 55 | + } else { |
| 56 | + for (const auto& input : node.inputs()) { |
| 57 | + if (input.value->isFolded()) { |
| 58 | + staticValues.insert(input.value->id()); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return staticValues; |
| 65 | +} |
| 66 | + |
| 67 | +void ExecutionPlanner::generateDeallocationPlan(ExecutionPlan& plan) { |
| 68 | + const auto& nodes = graph_.nodes(); |
| 69 | + size_t numNodes = nodes.size(); |
| 70 | + |
| 71 | + std::unordered_map<ValueId, NodeIndex> lastUsedBy; |
| 72 | + |
| 73 | + // Traverse from the last node to the first node |
| 74 | + // For each Value, find out which is the last node that uses it |
| 75 | + // the Value can freed after executing the node |
| 76 | + size_t nodeIdx = nodes.size() - 1; |
| 77 | + for (auto it = std::rbegin(nodes); it != std::rend(nodes); it++) { |
| 78 | + const auto& inputs = it->inputs(); |
| 79 | + for (const auto& input : inputs) { |
| 80 | + const auto& id = input.value->id(); |
| 81 | + if (lastUsedBy.find(id) == lastUsedBy.end()) { |
| 82 | + lastUsedBy.insert({id, nodeIdx}); |
| 83 | + } |
| 84 | + } |
| 85 | + nodeIdx--; |
| 86 | + } |
| 87 | + |
| 88 | + std::vector<std::vector<ValueId>> valuesToFree(numNodes); |
| 89 | + |
| 90 | + const auto& statics = staticValues(graph_); |
| 91 | + for (auto& [id, nodeIndex] : lastUsedBy) { |
| 92 | + if (statics.find(id) == statics.end()) { |
| 93 | + valuesToFree[nodeIndex].push_back(id); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + plan.valuesToFree = std::move(valuesToFree); |
| 98 | + |
| 99 | + // print allocation plan |
| 100 | + VLOG(2) << plan; |
| 101 | + |
| 102 | + return; |
| 103 | +} |
| 104 | + |
| 105 | +std::ostream& operator<<(std::ostream& out, const ExecutionPlan& plan) { |
| 106 | + out << "****** Deallocation Plan ******\n"; |
| 107 | + for (auto&& [i, values] : c10::enumerate(plan.valuesToFree)) { |
| 108 | + out << "Node #" << i << ", valuesToFree = ["; |
| 109 | + for (const auto& value : values) { |
| 110 | + out << value << ", "; |
| 111 | + } |
| 112 | + out << "]\n"; |
| 113 | + } |
| 114 | + return out; |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace torch::nativert |
0 commit comments