|
4 | 4 | namespace trtorch {
|
5 | 5 | namespace core {
|
6 | 6 | namespace lowering {
|
7 |
| -namespace irfusers { |
| 7 | +namespace passes { |
8 | 8 |
|
9 | 9 | void FuseFlattenLinear(std::shared_ptr<torch::jit::Graph>& graph) {
|
10 | 10 | //TensorRT implicitly adds a flatten layer infront of FC layers if necessary
|
@@ -33,13 +33,47 @@ void FuseFlattenLinear(std::shared_ptr<torch::jit::Graph>& graph) {
|
33 | 33 | torch::jit::SubgraphRewriter flatten_linear_to_linear;
|
34 | 34 | flatten_linear_to_linear.RegisterRewritePattern(flatten_linear_pattern, fused_linear);
|
35 | 35 | flatten_linear_to_linear.runOnGraph(graph);
|
36 |
| - |
| 36 | + |
| 37 | + torch::jit::SubgraphRewriter flatten_linear_bias_none_to_linear; |
| 38 | + flatten_linear_bias_none_to_linear.RegisterRewritePattern( |
| 39 | + flatten_linear_bias_none_pattern, fused_linear_bias_none); |
| 40 | + flatten_linear_bias_none_to_linear.runOnGraph(graph); |
| 41 | +} |
| 42 | + |
| 43 | +void FuseFlattenAddMM(std::shared_ptr<torch::jit::Graph>& graph) { |
| 44 | + //TensorRT implicitly adds a flatten layer infront of FC layers if necessary |
| 45 | + std::string flatten_linear_pattern = R"IR( |
| 46 | + graph(%input, %6, %7, %weight, %bias): |
| 47 | + %flat = aten::flatten(%input, %6, %7) |
| 48 | + %res = aten::linear(%flat, %weight, %bias) |
| 49 | + return (%res))IR"; |
| 50 | + std::string flatten_linear_bias_none_pattern = R"IR( |
| 51 | + graph(%input, %6, %7, %weight): |
| 52 | + %flat = aten::flatten(%input, %6, %7) |
| 53 | + %bias: Tensor? = prim::Constant() |
| 54 | + %res = aten::linear(%flat, %weight, %bias) |
| 55 | + return (%res))IR"; |
| 56 | + std::string fused_linear = R"IR( |
| 57 | + graph(%input, %6, %7, %weight, %bias): |
| 58 | + %res = aten::linear(%input, %weight, %bias) |
| 59 | + return (%res))IR"; |
| 60 | + |
| 61 | + std::string fused_linear_bias_none = R"IR( |
| 62 | + graph(%input, %6, %7, %weight): |
| 63 | + %bias: Tensor? = prim::Constant() |
| 64 | + %res = aten::linear(%input, %weight, %bias) |
| 65 | + return (%res))IR"; |
| 66 | + |
| 67 | + torch::jit::SubgraphRewriter flatten_linear_to_linear; |
| 68 | + flatten_linear_to_linear.RegisterRewritePattern(flatten_linear_pattern, fused_linear); |
| 69 | + flatten_linear_to_linear.runOnGraph(graph); |
| 70 | + |
37 | 71 | torch::jit::SubgraphRewriter flatten_linear_bias_none_to_linear;
|
38 | 72 | flatten_linear_bias_none_to_linear.RegisterRewritePattern(
|
39 | 73 | flatten_linear_bias_none_pattern, fused_linear_bias_none);
|
40 | 74 | flatten_linear_bias_none_to_linear.runOnGraph(graph);
|
41 | 75 | }
|
42 |
| -} // namespace irfusers |
| 76 | +} // namespace passes |
43 | 77 | } // namespace lowering
|
44 | 78 | } // namespace core
|
45 | 79 | } // namespace trtorch
|
0 commit comments