Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow more inserts before reIndexTopology #102312

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 10 additions & 3 deletions torch/csrc/jit/ir/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,13 +1283,20 @@ void Node::assignTopoPosition() {

// insert between two existing nodes
} else {
const auto posBetween = prevPos + (nextPos - prevPos) / 2;
if (posBetween == prevPos) {
int64_t remaining = nextPos - prevPos;
AT_ASSERT(remaining > 0);
if (remaining == 1) {
// There was no room
owningBlock()->reIndexTopology();
return;
}
topo_position_ = posBetween;
int64_t predicted_future_insertions = 0;
if (next() == graph_->insertPoint()) {
predicted_future_insertions = graph_->predicted_insert_count_++;
}
topo_position_ = prevPos +
std::max(int64_t(1), remaining / (2 + predicted_future_insertions));
AT_ASSERT(prevPos < topo_position_ && topo_position_ < nextPos);
}
}

Expand Down
4 changes: 3 additions & 1 deletion torch/csrc/jit/ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ struct Graph : std::enable_shared_from_this<Graph> {
// when insertNode() is called, the node is inserted before this node
// by default this is set to append to the top level block
Node* insert_before_;
int64_t predicted_insert_count_ = 0;

c10::optional<size_t> op_version_;

Expand Down Expand Up @@ -1403,14 +1404,15 @@ struct Graph : std::enable_shared_from_this<Graph> {
// set where nodes are inserted to append to the end of this block
void setInsertPoint(Block* b) {
AT_ASSERT(b->owningGraph() == this);
insert_before_ = b->return_node();
setInsertPoint(b->return_node());
}
// set where nodes are inserted to insert _before_ this node
// for implementation simplicity we only support inserting before a node for
// now
void setInsertPoint(Node* n) {
AT_ASSERT(n->owningGraph() == this && n->inBlockList());
insert_before_ = n;
predicted_insert_count_ = 0;
}
Node* insertPoint() {
return insert_before_;
Expand Down