File: peephole_list_idioms.h

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (72 lines) | stat: -rw-r--r-- 2,003 bytes parent folder | download
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
71
72
#pragma once

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

namespace torch {
namespace 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 jit
} // namespace torch