program.hpp 4.96 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>
36
37
#include <migraphx/target_assignments.hpp>
#include <migraphx/assignment_options.hpp>
Paul's avatar
Paul committed
38
#include <migraphx/env.hpp>
Paul's avatar
Paul committed
39
#include <migraphx/config.hpp>
40
#include <migraphx/execution_environment.hpp>
Paul's avatar
Paul committed
41
#include <algorithm>
Paul's avatar
Paul committed
42
#include <iostream>
Paul's avatar
Paul committed
43

Paul's avatar
Paul committed
44
namespace migraphx {
Paul's avatar
Paul committed
45
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
46

Paul's avatar
Paul committed
47
48
49
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_COMPILE)
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_EVAL)

Paul's avatar
Paul committed
50
51
struct program_impl;

52
53
struct marker;

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

    // move constructor
Paul's avatar
Paul committed
62
    program(program&&) noexcept;
63
64

    // copy constructor
Shucai Xiao's avatar
Shucai Xiao committed
65
    program(const program&);
66
67

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

Paul's avatar
Paul committed
70
    ~program() noexcept;
Paul's avatar
Paul committed
71

72
73
    std::vector<std::string> get_parameter_names() const;

Paul's avatar
Paul committed
74
    shape get_parameter_shape(std::string name) const;
Paul's avatar
Paul committed
75

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

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

80
81
    std::vector<argument> eval(parameter_map params,
                               execution_environment exec_env = execution_environment{}) const;
Paul's avatar
Paul committed
82
    std::size_t size() const;
83

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

Paul's avatar
Paul committed
86
87
    context& get_context() const;

Paul's avatar
Paul committed
88
89
    instruction_ref validate() const;

90
91
92
    target_assignments get_target_assignments(const std::vector<target>& targets,
                                              assignment_options options = assignment_options{});

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

95
96
    bool is_compiled() const;

Paul's avatar
Paul committed
97
    void finalize();
Paul's avatar
Paul committed
98

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

102
103
    void mark(const parameter_map& params, marker&& m);

104
105
106
    value to_value() const;
    void from_value(const value& v);

Paul's avatar
Paul committed
107
108
    void debug_print() const;
    void debug_print(instruction_ref ins) const;
Shucai Xiao's avatar
Shucai Xiao committed
109
110
111
    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
112
                   print_func) const;
Shucai Xiao's avatar
Shucai Xiao committed
113
114
115
    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
116

117
    void print_graph(std::ostream& os, bool brief = false) const;
118
    void print_cpp(std::ostream& os) const;
Paul's avatar
Paul committed
119

Paul's avatar
Paul committed
120
121
    void dry_run(parameter_map params) const;

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

124
125
    program& sort();

Paul's avatar
Paul committed
126
127
    friend std::ostream& operator<<(std::ostream& os, const program& p);
    friend bool operator==(const program& x, const program& y);
128
    friend bool operator!=(const program& x, const program& y) { return not(x == y); }
Paul's avatar
Paul committed
129

Shucai Xiao's avatar
Shucai Xiao committed
130
131
132
133
134
    // 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
135
136
    module* get_main_module();
    const module* get_main_module() const;
Shucai Xiao's avatar
Shucai Xiao committed
137

Shucai Xiao's avatar
Shucai Xiao committed
138
    std::vector<const module*> get_modules() const;
Shucai Xiao's avatar
Shucai Xiao committed
139
    std::vector<module*> get_modules();
Shucai Xiao's avatar
Shucai Xiao committed
140

141
142
    std::unordered_multimap<module_ref, module_ref> get_module_tree();

143
144
145
    void remove_module(const std::string& name);
    void remove_unused_modules();

146
    private:
Shucai Xiao's avatar
Shucai Xiao committed
147
    void assign(const program& p);
Paul's avatar
Paul committed
148
    std::unique_ptr<program_impl> impl;
Paul's avatar
Paul committed
149
};
Paul's avatar
Paul committed
150

Paul's avatar
Paul committed
151
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
152
} // namespace migraphx
Paul's avatar
Paul committed
153
154

#endif