text
stringlengths 0
2.2M
|
---|
// a given node `n` until the end of the owning block
|
// If `n` belongs to `prim::If` or `prim::Loop`
|
// buildBailOutLoop/If continue
|
// from block's owning node (e.g. `prim::If` or
|
// `prim::Loop`)
|
void buildBailOutBlockFrom(Node* n) {
|
auto b = n->owningBlock();
|
for (auto it = n->iterator(); it != b->nodes().end(); it++) {
|
cloneNode(*it);
|
}
|
// we are either in `prim::If` or `prim::Loop`
|
// bailout graph building will continue from `outer_node` next
|
auto outer_node = n->owningBlock()->owningNode();
|
if (outer_node) {
|
if (outer_node->kind() == prim::Loop) {
|
buildBailOutLoop(outer_node);
|
} else if (outer_node->kind() == prim::If) {
|
buildBailOutIf(b->outputs(), outer_node);
|
} else {
|
AT_ERROR("Unexpected outer node");
|
}
|
}
|
}
|
void mapValues(
|
const at::ArrayRef<Value*> block_outputs,
|
const at::ArrayRef<Value*> carried_deps) {
|
TORCH_INTERNAL_ASSERT(block_outputs.size() == carried_deps.size());
|
for (const auto i : c10::irange(block_outputs.size())) {
|
auto nv = getOrAddInputForValue(block_outputs[i]);
|
old_to_new_[carried_deps[i]] = nv;
|
}
|
}
|
void buildBailOutLoop(Node* outer_node) {
|
LoopView lv(outer_node);
|
auto old_max_count = getOrAddInputForValue(lv.maxTripCount());
|
auto cur_iter = getInputForValue(lv.currentTripCount());
|
auto block_outputs = lv.bodyBlock()->outputs();
|
auto* block = copy_graph_->block();
|
// subtract the number of iterations
|
WithInsertPoint guard(*block->nodes().end());
|
auto updated_max_trip_count =
|
copy_graph_->insert(aten::sub, {old_max_count, cur_iter});
|
auto one = copy_graph_->insertConstant({1});
|
updated_max_trip_count =
|
copy_graph_->insert(aten::sub, {updated_max_trip_count, one});
|
auto cur_plus_one = copy_graph_->insert(aten::add, {one, cur_iter});
|
// We need to be careful when mapping `block_outputs` to continuation
|
// loop's inputs since `cloneFrom` will replace `%4` with the same value
|
// in both, `prim::Loop` and `aten::cat` in the example below:
|
//
|
// ... : Tensor = prim::Loop(%MAX_TRIP_COUNT, %COND, ..., %4)
|
// block0(%i.2 : int, ...):
|
// ...
|
// %y.5 : Double(3) = aten::cat(%22, %4)
|
// ...
|
//
|
// However for the cloned loop node, the values should be different.
|
// Namely, the value in `prim::Loop` should come from
|
// `lv.bodyBlock()->outputs()` which are mapped to the outputs of the
|
// current iteration whereas `%4` in `aten::cat` needs to be mapped to the
|
// cloned value of `%4` in a bailout graph. To work around this, we manually
|
// clone loop nodes
|
// map the residual loop's inputs to the outputs of the current iteration
|
// (i.e. `block_outputs`)
|
auto new_loop =
|
copy_graph_->insertNode(copy_graph_->create(prim::Loop, {}, 0))
|
->setSourceRange(outer_node->sourceRange());
|
new_loop->addInput(updated_max_trip_count);
|
for (auto bo : block_outputs) {
|
new_loop->addInput(getOrAddInputForValue(bo));
|
}
|
// clone the loop body and map old loop's outputs to new loop's outputs
|
auto new_loop_body = new_loop->addBlock();
|
auto env = [this](Value* v) { return getOrAddInputForValue(v); };
|
new_loop_body->cloneFrom(lv.bodyBlock(), env);
|
for (auto ov : lv.carriedOutputs()) {
|
auto no = new_loop->addOutput();
|
mapValueAndCopyMetadata(ov, no);
|
}
|
LoopView new_lv(new_loop);
|
{
|
WithInsertPoint guard_in_loop(*new_lv.bodyBlock()->nodes().begin());
|
// `one` will be replaced with new_lv.currentTripCount()
|
// but it needs to be done after
|
// new_lv.currentTripCount()->replaceAllUsesWith(adj_iter_ctr);
|
// to avoid cyclical references
|
auto adj_iter_ctr = copy_graph_->insert(aten::add, {cur_plus_one, one});
|
new_lv.currentTripCount()->replaceAllUsesWith(adj_iter_ctr);
|
adj_iter_ctr->node()->replaceInputWith(one, new_lv.currentTripCount());
|
}
|
if (outer_node->next()) {
|
buildBailOutBlockFrom(outer_node->next());
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.