text
stringlengths
0
2.2M
// Copyright 2015 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------- END-OF-FILE ----------------------------------
#include <torch/csrc/jit/passes/bailout_graph.h>
#include <ATen/core/function.h>
#include <c10/util/irange.h>
#include <torch/csrc/jit/ir/alias_analysis.h>
#include <torch/csrc/jit/ir/ir_views.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/passes/clear_profiling.h>
#include <torch/csrc/jit/passes/constant_pooling.h>
#include <torch/csrc/jit/passes/liveness.h>
#include <memory>
#include <unordered_set>
namespace torch {
namespace jit {
static bool shouldBeCapturedInByBailOut(Node* n) {
return n->kind() != prim::Constant;
}
struct BailOutGraphBuilderForNode {
explicit BailOutGraphBuilderForNode(
std::shared_ptr<Graph> graph,
std::shared_ptr<Graph> target)
: graph_(std::move(graph)), copy_graph_(std::move(target)) {}
// capture `old_value` into the bailout graph
// by creating a new input and mapping
// `old_value` to it
Value* addNewInputForValue(Value* old_value) {
auto node = old_value->node();
// this reduces the number of inputs to a bailout graph significantly
// making it easier to debug
if (node->kind() == prim::Constant) {
TORCH_INTERNAL_ASSERT(!shouldBeCapturedInByBailOut(node));
auto new_const = copy_graph_->createClone(node, {nullptr});
copy_graph_->block()->prependNode(new_const);
return new_const->output();
}
live_inputs_.push_back(old_value);
auto new_value = copy_graph_->block()->addInput();
GRAPH_DEBUG(
"Adding a new value %",
new_value->debugName(),
" for %",
old_value->debugName());
return mapValueAndCopyMetadata(old_value, new_value);
}
Value* mapValueAndCopyMetadata(Value* old_value, Value* new_value) {
this->old_to_new_[old_value] = new_value;
new_value->copyMetadata(old_value);
return new_value;
}
Value* getOrAddInputForValue(Value* v) {
if (this->old_to_new_.count(v) == 0) {
return addNewInputForValue(v);
} else {
return this->old_to_new_[v];
}
}
Value* getInputForValue(Value* v) {
TORCH_INTERNAL_ASSERT(this->old_to_new_.count(v));
return this->old_to_new_[v];
}
Node* cloneNode(Node* node) {
auto* block = copy_graph_->block();
auto env = [this](Value* v) { return getOrAddInputForValue(v); };
auto new_node = block->appendNode(copy_graph_->createClone(node, env));
for (size_t i = 0; i < node->outputs().size(); ++i) {
auto oo = node->outputs()[i];
auto no = new_node->outputs()[i];
old_to_new_[oo] = no;
}
return new_node;
}
// buildBailOutBlockFrom builds a bailout graph from