-
Notifications
You must be signed in to change notification settings - Fork 363
Adding limited support for aten::Int #870
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
908340f
feat(aten::Int): Lowers out aten::Int
narendasan 46ac757
feat(aten::Int): Adding a new pass to remove single use
narendasan e63908b
refactor: Apply linting
narendasan 8139da9
chore: Add BERT to the model set
narendasan 72c7b76
refactor(//core/lowering/passes/remove_unnecessary_casts):
narendasan 7996a10
feat(//tests): Adding BERT to the test suite
narendasan 83ae991
refactor: apply linting
narendasan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <stack> | ||
#include <unordered_set> | ||
|
||
#include "torch/csrc/jit/passes/subgraph_rewrite.h" | ||
|
||
#include "core/lowering/passes/passes.h" | ||
#include "core/util/prelude.h" | ||
|
||
namespace torch_tensorrt { | ||
namespace core { | ||
namespace lowering { | ||
namespace passes { | ||
|
||
void RemoveSetAttrs(const torch::jit::Module& mod, std::string method_name) { | ||
auto g = mod.get_method(method_name).graph(); | ||
|
||
std::string set_attr_pattern = R"IR( | ||
graph(%self, %0): | ||
None = prim::SetAttr[name="_has_warned"](%self, %0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you mention in a comment about this specific attribute |
||
return ())IR"; | ||
std::string no_set_attr_pattern = R"IR( | ||
graph(%self, %0): | ||
return ())IR"; | ||
|
||
// remove contiguous | ||
torch::jit::SubgraphRewriter remove_set_attr; | ||
remove_set_attr.RegisterRewritePattern(set_attr_pattern, no_set_attr_pattern); | ||
remove_set_attr.runOnGraph(g); | ||
LOG_GRAPH("Post remove contiguous: " << *g); | ||
} | ||
|
||
} // namespace passes | ||
} // namespace lowering | ||
} // namespace core | ||
} // namespace torch_tensorrt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
#include "torch/csrc/jit/ir/constants.h" | ||
#include "torch/csrc/jit/passes/subgraph_rewrite.h" | ||
|
||
#include "core/util/prelude.h" | ||
|
||
#include <vector> | ||
|
||
namespace torch_tensorrt { | ||
namespace core { | ||
namespace lowering { | ||
namespace passes { | ||
|
||
// Presumably this is safe since torch::jit::EraseNumberTypesOnBlock exists which just | ||
// removes prim::TensorToNum, aten::Float, aten::Int and prim::NumToTensor nodes outright | ||
void RemoveUnnecessaryCasts(std::shared_ptr<torch::jit::Graph>& graph) { | ||
std::string int_cast_pattern = R"IR( | ||
graph(%1: int): | ||
%2: Tensor = aten::NumToTensor(%1) | ||
%3: int = aten::Int(%2) | ||
return (%3))IR"; | ||
peri044 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
std::string int_clean_pattern = R"IR( | ||
graph(%1: int): | ||
return (%1))IR"; | ||
|
||
std::string float_cast_pattern = R"IR( | ||
graph(%1: float): | ||
%2: Tensor = aten::NumToTensor(%1) | ||
%3: float = aten::Float(%2) | ||
return (%3))IR"; | ||
std::string float_clean_pattern = R"IR( | ||
graph(%1: float): | ||
return (%1))IR"; | ||
|
||
std::string bool_cast_pattern = R"IR( | ||
graph(%1: bool): | ||
%2: Tensor = aten::NumToTensor(%1) | ||
%3: bool = aten::Bool(%2) | ||
return (%3))IR"; | ||
std::string bool_clean_pattern = R"IR( | ||
graph(%1: bool): | ||
return (%1))IR"; | ||
|
||
torch::jit::SubgraphRewriter int_cast_rewriter; | ||
int_cast_rewriter.RegisterRewritePattern(int_cast_pattern, int_clean_pattern); | ||
int_cast_rewriter.runOnGraph(graph); | ||
|
||
torch::jit::SubgraphRewriter float_cast_rewriter; | ||
float_cast_rewriter.RegisterRewritePattern(float_cast_pattern, float_clean_pattern); | ||
float_cast_rewriter.runOnGraph(graph); | ||
|
||
torch::jit::SubgraphRewriter bool_cast_rewriter; | ||
bool_cast_rewriter.RegisterRewritePattern(bool_cast_pattern, bool_clean_pattern); | ||
bool_cast_rewriter.runOnGraph(graph); | ||
|
||
LOG_GRAPH("After RemoveUnnecessaryCasts: " << *graph); | ||
} | ||
|
||
void RemoveSingleUse0DTensors(std::shared_ptr<torch::jit::Graph>& g) { | ||
for (auto it = g->block()->nodes().begin(), end = g->block()->nodes().end(); it != end; ++it) { | ||
if (it->kind() == torch::jit::prim::Constant) { | ||
// Going from a constant and is single use means we can fuse | ||
if (it->output()->type()->isSubtypeOf(c10::TensorType::get())) { | ||
// Get the tensor stored in constant | ||
at::Tensor t = *torch::jit::constant_as<at::Tensor>(it->output()); | ||
// If shape is 0D | ||
if (t.sizes() == std::vector<int64_t>({})) { | ||
LOG_GRAPH("Found a 0D Tensor: " << it->output()->debugName()); | ||
LOG_GRAPH("Number of uses: " << it->output()->uses().size()); | ||
// If the tensor is only used once | ||
if (it->output()->uses().size() == 1) { | ||
auto use = it->output()->uses()[0]; | ||
auto user = use.user; | ||
|
||
// Is a NumToTensor / aten::[Int/Float] case | ||
if (user->outputs().size() == 1 && user->outputs()[0]->type()->isSubtypeOf(c10::TensorType::get())) { | ||
if (user->output()->uses().size() == 1) { | ||
auto potential_cast = user->output()->uses()[0].user; | ||
// The downstream user is aten::Int | ||
if (potential_cast->kind() == c10::Symbol::fromQualString("aten::Int") || | ||
potential_cast->kind() == c10::Symbol::fromQualString("aten::Float")) { | ||
LOG_GRAPH("Downstream user is aten::Int/aten::Float"); | ||
auto arg = use.offset; | ||
|
||
for (size_t k = 0; k < user->inputs().size(); ++k) { | ||
if (k != arg) { | ||
if (user->inputs()[k]->type()->isSubtypeOf(c10::TensorType::get())) { | ||
LOG_GRAPH("Input " << k << " is a Tensor"); | ||
if (user->inputs()[k]->node()->kind() == c10::Symbol::fromQualString("prim::NumToTensor")) { | ||
auto num_to_tensor = user->inputs()[k]->node(); | ||
|
||
LOG_GRAPH( | ||
"Found a prim::NumToTensor / aten::[Int/Float] pair with an intermediate operation:\n " | ||
<< *(*it) << *num_to_tensor << *user << *potential_cast); | ||
|
||
// Replace the Tensor Constant with a scalar constant | ||
LOG_GRAPH("Deleting 0-dim Tensor: " << **it); | ||
torch::jit::WithInsertPoint gaurd(*it); | ||
|
||
auto new_const_val = g->insertConstant(t.item(), c10::nullopt, it->scope()); | ||
new_const_val->copyMetadata(it->output()); | ||
// How to determine the internal scalar type instead of assuming? | ||
if (potential_cast->kind() == c10::aten::Int) { | ||
new_const_val->setType(c10::IntType::get()); | ||
} else if (potential_cast->kind() == c10::aten::Float) { | ||
new_const_val->setType(c10::FloatType::get()); | ||
} | ||
it->output()->replaceAllUsesWith(new_const_val); | ||
it.destroyCurrent(); | ||
|
||
LOG_GRAPH("New constant: " << *new_const_val->node()); | ||
|
||
// Delete NumToTensor | ||
LOG_GRAPH("Deleting NumToTensor: " << *num_to_tensor); | ||
num_to_tensor->output()->replaceAllUsesWith(num_to_tensor->inputs()[0]); | ||
num_to_tensor->destroy(); | ||
|
||
// Change intermediate op output type | ||
LOG_GRAPH(user->schema()); | ||
|
||
torch::jit::Node* new_node; | ||
switch (user->kind()) { | ||
// Use this to handle special cases where the scalar version of the intermediate operator | ||
// has a different schema than the original | ||
case c10::aten::add: | ||
new_node = g->create( | ||
user->kind(), | ||
torch::jit::ArrayRef<torch::jit::Value*>({user->inputs()[0], user->inputs()[1]}), | ||
1); | ||
new_node->insertAfter(user); | ||
new_node->outputs()[0]->setType(c10::IntType::get()); | ||
user->outputs()[0]->replaceAllUsesWith(new_node->outputs()[0]); | ||
user->destroy(); | ||
break; | ||
default: | ||
new_node = g->create(user->kind(), user->inputs(), 1); | ||
new_node->insertAfter(user); | ||
new_node->outputs()[0]->setType(c10::IntType::get()); | ||
user->outputs()[0]->replaceAllUsesWith(new_node->outputs()[0]); | ||
user->destroy(); | ||
break; | ||
} | ||
|
||
LOG_GRAPH("New intermediate operation: " << *new_node); | ||
LOG_GRAPH(new_node->schema()); | ||
|
||
// Delete aten::Int | ||
LOG_GRAPH("Deleting aten::[Int/Float]: " << *potential_cast); | ||
potential_cast->output()->replaceAllUsesWith(potential_cast->inputs()[0]); | ||
potential_cast->destroy(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
LOG_GRAPH("Post removing single use 0-dim Tensor operations: " << *g); | ||
} | ||
|
||
} // namespace passes | ||
} // namespace lowering | ||
} // namespace core | ||
} // namespace torch_tensorrt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this lowering pass used ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a version of transformers that had this and it was breaking the conversion process since setattr does not have a schema. But later versions dont use this so I removed it from the set of active passes