program.hpp 3.35 KB
Newer Older
Paul's avatar
Paul committed
1
2
#ifndef MIGRAPH_GUARD_MIGRAPHLIB_PROGRAM_HPP
#define MIGRAPH_GUARD_MIGRAPHLIB_PROGRAM_HPP
Paul's avatar
Paul committed
3

Paul's avatar
Paul committed
4
#include <list>
Paul's avatar
Paul committed
5
#include <unordered_map>
Paul's avatar
Paul committed
6
7
8
9
10
#include <migraph/operation.hpp>
#include <migraph/literal.hpp>
#include <migraph/builtin.hpp>
#include <migraph/instruction_ref.hpp>
#include <migraph/target.hpp>
Paul's avatar
Paul committed
11
#include <migraph/tracer.hpp>
12
#include <migraph/config.hpp>
Paul's avatar
Paul committed
13
#include <algorithm>
Paul's avatar
Paul committed
14
#include <iostream>
Paul's avatar
Paul committed
15

16
17
namespace migraph {
inline namespace MIGRAPH_INLINE_NS {
Paul's avatar
Paul committed
18

Paul's avatar
Paul committed
19
20
struct program_impl;

Paul's avatar
Paul committed
21
22
const operation& get_operation(instruction_ref ins);

mei-ye's avatar
mei-ye committed
23
/**
Paul's avatar
Paul committed
24
25
 * @brief Stores the instruction stream
 */
Paul's avatar
Paul committed
26
27
struct program
{
Paul's avatar
Paul committed
28
29
    program();
    program(program&&) noexcept;
Paul's avatar
Paul committed
30
31
    program& operator=(program&&) noexcept;
    ~program() noexcept;
Paul's avatar
Paul committed
32

mei-ye's avatar
mei-ye committed
33
    using parameter_map = std::unordered_map<std::string, argument>;
Paul's avatar
Paul committed
34

Paul's avatar
Paul committed
35
    template <class... Ts>
Paul's avatar
Paul committed
36
    instruction_ref add_instruction(operation op, Ts... args)
Paul's avatar
Paul committed
37
    {
Paul's avatar
Paul committed
38
        return add_instruction(op, {args...});
Paul's avatar
Paul committed
39
    }
Paul's avatar
Paul committed
40
    instruction_ref add_instruction(const operation& op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
41

Paul's avatar
Paul committed
42
43
44
45
46
    template <class... Ts>
    instruction_ref insert_instruction(instruction_ref ins, operation op, Ts... args)
    {
        return insert_instruction(ins, op, {args...});
    }
Paul's avatar
Paul committed
47
    instruction_ref
Paul's avatar
Paul committed
48
    insert_instruction(instruction_ref ins, const operation& op, std::vector<instruction_ref> args);
Paul's avatar
Paul committed
49

Paul's avatar
Paul committed
50
51
52
53
54
    template <class... Ts>
    instruction_ref replace_instruction(instruction_ref ins, operation op, Ts... args)
    {
        return replace_instruction(ins, op, {args...});
    }
Paul's avatar
Paul committed
55
56
57
    instruction_ref replace_instruction(instruction_ref ins,
                                        const operation& op,
                                        std::vector<instruction_ref> args);
Paul's avatar
Paul committed
58

Paul's avatar
Paul committed
59
    instruction_ref replace_instruction(instruction_ref ins, instruction_ref rep);
Paul's avatar
Paul committed
60

Paul's avatar
Paul committed
61
    instruction_ref remove_instruction(instruction_ref ins);
62
    instruction_ref remove_instructions(instruction_ref first, instruction_ref last);
Paul's avatar
Paul committed
63

64
    instruction_ref move_instruction(instruction_ref src, instruction_ref dst);
Paul's avatar
Paul committed
65

Paul's avatar
Paul committed
66
    template <class... Ts>
Paul's avatar
Paul committed
67
    instruction_ref add_literal(Ts&&... xs)
Paul's avatar
Paul committed
68
    {
Paul's avatar
Paul committed
69
        return add_literal(literal{std::forward<Ts>(xs)...});
Paul's avatar
Paul committed
70
71
    }

Paul's avatar
Paul committed
72
    instruction_ref add_literal(literal l);
Paul's avatar
Paul committed
73

Paul's avatar
Paul committed
74
    instruction_ref add_outline(const shape& s);
Paul's avatar
Paul committed
75

Paul's avatar
Paul committed
76
    instruction_ref add_parameter(std::string name, shape s);
Paul's avatar
Paul committed
77

Paul's avatar
Paul committed
78
    shape get_parameter_shape(std::string name) const;
Paul's avatar
Paul committed
79

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

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

Paul's avatar
Paul committed
84
    argument eval(parameter_map params) const;
Paul's avatar
Paul committed
85

Paul's avatar
Paul committed
86
    bool has_instruction(instruction_ref ins) const;
Paul's avatar
Paul committed
87

Paul's avatar
Paul committed
88
    std::size_t size() const;
Paul's avatar
Paul committed
89
90
    instruction_ref begin() const;
    instruction_ref end() const;
91

Paul's avatar
Paul committed
92
93
    shape get_shape() const;

Paul's avatar
Paul committed
94
95
    instruction_ref validate() const;

mei-ye's avatar
mei-ye committed
96
    void compile(const target& t, tracer trace = tracer{});
Paul's avatar
Paul committed
97

Paul's avatar
Paul committed
98
99
    void perf_report(std::ostream& os, std::size_t n, parameter_map params) const;

Paul's avatar
Paul committed
100
101
102
    void debug_print() const;
    void debug_print(instruction_ref ins) const;
    void debug_print(const std::vector<instruction_ref>& inss) const;
Paul's avatar
Paul committed
103

Paul's avatar
Paul committed
104
105
    friend std::ostream& operator<<(std::ostream& os, const program& p);
    friend bool operator==(const program& x, const program& y);
Paul's avatar
Paul committed
106
    friend bool operator!=(const program& x, const program& y) { return !(x == y); }
Paul's avatar
Paul committed
107

Paul's avatar
Paul committed
108
    private:
Paul's avatar
Paul committed
109
    std::unique_ptr<program_impl> impl;
Paul's avatar
Paul committed
110
};
111

112
} // namespace MIGRAPH_INLINE_NS
Paul's avatar
Paul committed
113
} // namespace migraph
Paul's avatar
Paul committed
114
115

#endif