forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Torch] Assume strict symbolic shapes (iree-org#15107)
In nearly all real world models, dynamic numpy style broadcasting never occurs, and managing such cases leads to troublingly pessimistic lowerings and restricts later optimization. This defaults all dynamic symbols coming from pytorch to be interpreted strictly, meaning it must represent an actual size (not something that can optionally be 1).
- Loading branch information
Showing
8 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
43 changes: 43 additions & 0 deletions
43
compiler/plugins/input/Torch/torch-iree/InputConversion/SetStrictSymbolicShapes.cpp
This file contains 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,43 @@ | ||
// Copyright 2023 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
//===- SetStrictSymbolicShapes.cpp - Pass to set strict symbolic shapes -=====// | ||
// | ||
// Adds an attribute to all functions in the module indicating all contained | ||
// operations can be treated as if the symbolic shapes are strict, thereby | ||
// eliminating the need for special dynamic size-1 broadcast handling. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "torch-iree/InputConversion/PassDetail.h" | ||
#include "torch-iree/InputConversion/Passes.h" | ||
|
||
static const llvm::StringLiteral kStrictSymbolsMarker = | ||
"torch.assume_strict_symbolic_shapes"; | ||
|
||
namespace mlir { | ||
namespace iree_compiler { | ||
namespace TorchInput { | ||
|
||
namespace { | ||
struct SetStrictSymbolicShapesPass | ||
: public SetStrictSymbolicShapesPassBase<SetStrictSymbolicShapesPass> { | ||
|
||
void runOnOperation() override { | ||
getOperation()->setAttr(kStrictSymbolsMarker, UnitAttr::get(&getContext())); | ||
} | ||
}; | ||
} // namespace | ||
|
||
std::unique_ptr<OperationPass<func::FuncOp>> | ||
createSetStrictSymbolicShapesPass() { | ||
return std::make_unique<SetStrictSymbolicShapesPass>(); | ||
} | ||
|
||
} // namespace TorchInput | ||
} // namespace iree_compiler | ||
} // namespace mlir |
This file contains 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
14 changes: 14 additions & 0 deletions
14
compiler/plugins/input/Torch/torch-iree/InputConversion/test/assume_strict_symbols.mlir
This file contains 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,14 @@ | ||
// RUN: iree-opt --split-input-file --torch-iree-set-strict-symbolic-shapes %s | FileCheck %s | ||
|
||
module { | ||
// CHECK: func @forward() {{.*}} attributes {torch.assume_strict_symbolic_shapes} | ||
func.func @forward() -> !torch.int { | ||
%int0 = torch.constant.int 0 | ||
return %int0 : !torch.int | ||
} | ||
// CHECK: func @other_forward() {{.*}} attributes {torch.assume_strict_symbolic_shapes} | ||
func.func @other_forward() -> !torch.int { | ||
%int1 = torch.constant.int 1 | ||
return %int1 : !torch.int | ||
} | ||
} |
This file contains 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