"src/onnx/onnx.cpp" did not exist on "846a0bb6477aac7282dc994ee2fbb4c3a6502bd8"
instruction.cpp 4.64 KB
Newer Older
Paul's avatar
Paul committed
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <migraph/instruction.hpp>
#include <migraph/builtin.hpp>
#include <migraph/erase.hpp>

namespace migraph {

    instruction::instruction(operation o, shape r, std::vector<instruction_ref> args)
        : op(std::move(o)), result(std::move(r)), arguments(std::move(args))
    {
    }

    instruction::instruction(literal l) : op(builtin::literal{}), result(l.get_shape()), lit(std::move(l)) {}

    void instruction::replace(const shape& r)
    {
        if(r != result)
        {
            result = r;
            for(auto&& ins : output)
            {
                assert(ins->name().front() != '@');
                ins->recompute_shape();
            }
        }
    }

    void instruction::recompute_shape() { replace(compute_shape(op, arguments)); }

    void instruction::clear_arguments()
    {
        for(auto&& arg : arguments)
        {
            arg->remove_output(*this);
        }
        arguments.clear();
    }

    bool operator==(const instruction& i, instruction_ref ref)
    {
        return std::addressof(i) == std::addressof(*ref);
    }

    bool instruction::valid(instruction_ref start) const
    {
        return valid() && std::all_of(arguments.begin(), arguments.end(), [&](instruction_ref i) {
                   auto self = std::find(i->outputs().begin(), i->outputs().end(), *this);
                   return self != i->outputs().end() &&
                          std::distance(start, i) < std::distance(start, *self);
               });
    }

    bool instruction::valid() const
    {
        shape computed;
        if(op.name() == "@literal")
        {
            computed = lit.get_shape();
        }
        else if(op.name() == "@param")
        {
            computed = result;
        }
        else
        {
            try
            {
                computed = compute_shape(op, arguments);
            }
            catch(migraph::exception&)
            {
                return false;
            }
        }
        return result == computed &&
               std::all_of(output.begin(), output.end(), [&](instruction_ref i) {
                   return std::find(i->inputs().begin(), i->inputs().end(), *this) !=
                          i->inputs().end();
               });
    }

    shape instruction::get_shape() const { return result; }
    const literal& instruction::get_literal() const
    {
        assert(op.name() == "@literal");
        return lit;
    }

    const operation& instruction::get_operator() const { return op; }

    std::string instruction::name() const { return op.name(); }

    const std::vector<instruction_ref>& instruction::inputs() const { return arguments; }

    const std::vector<instruction_ref>& instruction::outputs() const { return output; }

    bool operator==(instruction_ref ref, const instruction& i) { return i == ref; }

    bool operator!=(const instruction& i, instruction_ref ref) { return !(i == ref); }

    bool operator!=(instruction_ref ref, const instruction& i) { return !(i == ref); }

    void instruction::add_output(instruction_ref ins)
    {
        if(std::find(output.begin(), output.end(), ins) == output.end())
            output.push_back(ins);
    }

    template <class T>
    void instruction::remove_output(const T& ins)
    {
        migraph::erase(output, ins);
    }

    void instruction::backreference(instruction_ref ref)
    {
        for(auto&& arg : ref->inputs())
            arg->add_output(ref);
    }

    void instruction::replace_argument(instruction_ref ins, instruction_ref old, instruction_ref new_ins)
    {
        ins->replace_argument(old, new_ins);
        backreference(ins);
        ins->recompute_shape();
    }

    void
    instruction::replace(instruction_ref ins, operation o, const shape& r, std::vector<instruction_ref> args)
    {
        ins->replace(std::move(o), r, std::move(args));
        backreference(ins);
    }

    void instruction::replace(operation o, const shape& r, std::vector<instruction_ref> args)
    {
        op = std::move(o);
        replace(r);
        replace(std::move(args));
    }

    void instruction::replace(std::vector<instruction_ref> args)
    {
        clear_arguments();
        arguments = std::move(args);
    }

    void instruction::replace_argument(instruction_ref old, instruction_ref new_ins)
    {
        std::replace(arguments.begin(), arguments.end(), old, new_ins);
        old->remove_output(*this);
    }

shape compute_shape(const operation& op, const std::vector<instruction_ref>& args)
{
    std::vector<shape> shapes(args.size());
    std::transform(
        args.begin(), args.end(), shapes.begin(), [](instruction_ref i) { return i->get_shape(); });
    return op.compute_shape(shapes);
}

} // namespace migraph