File: peephole_list_idioms.h

package info (click to toggle)
pytorch-cuda 2.6.0%2Bdfsg-7
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 161,620 kB
  • sloc: python: 1,278,832; cpp: 900,322; ansic: 82,710; asm: 7,754; java: 3,363; sh: 2,811; javascript: 2,443; makefile: 597; ruby: 195; xml: 84; objc: 68
file content (70 lines) | stat: -rw-r--r-- 1,978 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#pragma once

#include <torch/csrc/jit/ir/ir.h>

namespace torch::jit {

// Peephole Optimizes List ops such as len(li) and li[1].
// 1. Construct/Unpack optimizations
// Given a function like this:
//    def foo(a, b):
//        li = [a, b]
//        x, y = li
//        return x, y
// This pass produces (after dead code elimination):
//    def foo(a, b):
//        return a, b
//
// This is only applied to lists that are not modified.
//
// 2. getitem optimizations
// Given a function like this:
//     def foo(a, b):
//         li = [a, b]
//         x = li[0]
//         return x
// This pass produces (after dead code elimination):
//     def foo(a, b):
//         return a
//
// This optimization can only happen if the list is not modified.
//
// 3. len optimizations
// Given a function like this:
//     def foo():
//         li = [1, 2]
//         return len(li)
// This pass produces (after dead code elimination):
//     def foo():
//         return 2
//
// This has the same requirements as the getitem optimizations.
//
// 4. ListConstruct + ListConstruct
// Given a function like this:
//     def foo():
//         return [1, 2] + [3, 4]
// This pass produces (after dead code elimination):
//     def foo():
//         return [1, 2, 3, 4]
//
// This is only applied to lists that are not modified.
//
// 5. Slice
// Given a function like this:
//     def foo():
//         return [1, 2, 3, 4, 5][0:2]
// This pass produces (after deadcode elimination):
//     def foo():
//         return [1, 2]
//
// Currently this is invoked as part of PeepholeOptimize
// return true if graph is modified.
// If `refine_list_len` is true will attempt to refine the len of lists through
// len comparisons and assertions. This does not generally optimize pytorch
// programs so it is not called by default in PeepholeOptimize.
TORCH_API bool PeepholeOptimizeListIdioms(
    const std::shared_ptr<Graph>& graph,
    bool refine_list_len = false);

} // namespace torch::jit