program.hpp 4.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24
25
#ifndef MIGRAPHX_GUARD_MIGRAPHLIB_PROGRAM_HPP
#define MIGRAPHX_GUARD_MIGRAPHLIB_PROGRAM_HPP
Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
#include <list>
Paul's avatar
Paul committed
28
#include <unordered_map>
Paul's avatar
Paul committed
29
#include <migraphx/operation.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
30
#include <migraphx/module.hpp>
Paul's avatar
Paul committed
31
32
33
34
#include <migraphx/literal.hpp>
#include <migraphx/builtin.hpp>
#include <migraphx/instruction_ref.hpp>
#include <migraphx/target.hpp>
35
#include <migraphx/compile_options.hpp>
Paul's avatar
Paul committed
36
#include <migraphx/env.hpp>
Paul's avatar
Paul committed
37
#include <migraphx/config.hpp>
Paul's avatar
Paul committed
38
#include <algorithm>
Paul's avatar
Paul committed
39
#include <iostream>
Paul's avatar
Paul committed
40

Paul's avatar
Paul committed
41
namespace migraphx {
Paul's avatar
Paul committed
42
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
43

Paul's avatar
Paul committed
44
45
46
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_COMPILE)
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_EVAL)

Paul's avatar
Paul committed
47
48
struct program_impl;

49
50
struct marker;

mei-ye's avatar
mei-ye committed
51
/**
Paul's avatar
Paul committed
52
53
 * @brief Stores the instruction stream
 */
Paul's avatar
Paul committed
54
55
struct program
{
Paul's avatar
Paul committed
56
    program();
57
58

    // move constructor
Paul's avatar
Paul committed
59
    program(program&&) noexcept;
60
61

    // copy constructor
Shucai Xiao's avatar
Shucai Xiao committed
62
    program(const program&);
63
64

    // copy assignment operator
Shucai Xiao's avatar
Shucai Xiao committed
65
    program& operator=(program);
66

Paul's avatar
Paul committed
67
    ~program() noexcept;
Paul's avatar
Paul committed
68

69
70
    std::vector<std::string> get_parameter_names() const;

Paul's avatar
Paul committed
71
    shape get_parameter_shape(std::string name) const;
Paul's avatar
Paul committed
72

mei-ye's avatar
mei-ye committed
73
74
    instruction_ref get_parameter(std::string name) const;

Paul's avatar
Paul committed
75
76
    std::unordered_map<std::string, shape> get_parameter_shapes() const;

77
    std::vector<argument> eval(parameter_map params) const;
Paul's avatar
Paul committed
78

Paul's avatar
Paul committed
79
    std::size_t size() const;
80

81
    std::vector<shape> get_output_shapes() const;
Paul's avatar
Paul committed
82

Paul's avatar
Paul committed
83
84
    context& get_context() const;

Paul's avatar
Paul committed
85
86
    instruction_ref validate() const;

87
    void compile(const target& t, compile_options options = compile_options{});
Paul's avatar
Paul committed
88

89
90
    bool is_compiled() const;

Paul's avatar
Paul committed
91
    void finalize();
Paul's avatar
Paul committed
92

93
94
    void
    perf_report(std::ostream& os, std::size_t n, parameter_map params, std::size_t batch = 1) const;
Paul's avatar
Paul committed
95

96
97
    void mark(const parameter_map& params, marker&& m);

98
99
100
    value to_value() const;
    void from_value(const value& v);

Paul's avatar
Paul committed
101
102
    void debug_print() const;
    void debug_print(instruction_ref ins) const;
Shucai Xiao's avatar
Shucai Xiao committed
103
104
105
    void print(std::unordered_map<instruction_ref, std::string>& names,
               const std::function<void(instruction_ref,
                                        std::unordered_map<instruction_ref, std::string>)>&
Shucai Xiao's avatar
Shucai Xiao committed
106
                   print_func) const;
Shucai Xiao's avatar
Shucai Xiao committed
107
108
109
    void print(const std::function<void(instruction_ref ins,
                                        std::unordered_map<instruction_ref, std::string>)>&
                   print_func) const;
Shucai Xiao's avatar
Shucai Xiao committed
110

111
    void print_graph(std::ostream& os, bool brief = false) const;
112
    void print_cpp(std::ostream& os) const;
Paul's avatar
Paul committed
113

Paul's avatar
Paul committed
114
115
    void dry_run(parameter_map params) const;

Shucai Xiao's avatar
Shucai Xiao committed
116
    void annotate(std::ostream& os, const std::function<void(instruction_ref)>& a) const;
Paul's avatar
Paul committed
117

118
119
    program& sort();

Paul's avatar
Paul committed
120
121
    friend std::ostream& operator<<(std::ostream& os, const program& p);
    friend bool operator==(const program& x, const program& y);
Paul's avatar
Paul committed
122
    friend bool operator!=(const program& x, const program& y) { return !(x == y); }
Paul's avatar
Paul committed
123

Shucai Xiao's avatar
Shucai Xiao committed
124
125
126
127
128
    // module related api
    module* create_module(const std::string& name);
    module* get_module(const std::string& name);
    const module* get_module(const std::string& name) const;

Shucai Xiao's avatar
Shucai Xiao committed
129
130
    module* get_main_module();
    const module* get_main_module() const;
Shucai Xiao's avatar
Shucai Xiao committed
131

Shucai Xiao's avatar
Shucai Xiao committed
132
    std::vector<const module*> get_modules() const;
Shucai Xiao's avatar
Shucai Xiao committed
133
    std::vector<module*> get_modules();
Shucai Xiao's avatar
Shucai Xiao committed
134

135
136
    std::unordered_multimap<module_ref, module_ref> get_module_tree();

137
138
139
    void remove_module(const std::string& name);
    void remove_unused_modules();

140
    private:
Shucai Xiao's avatar
Shucai Xiao committed
141
    void assign(const program& p);
Paul's avatar
Paul committed
142
    std::unique_ptr<program_impl> impl;
Paul's avatar
Paul committed
143
};
Paul's avatar
Paul committed
144

Paul's avatar
Paul committed
145
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
146
} // namespace migraphx
Paul's avatar
Paul committed
147
148

#endif