api.cpp 4.96 KB
Newer Older
Paul Fultz II's avatar
Paul Fultz II committed
1
2
3
4
5
#include <migraphx/migraphx.h>
#include <migraphx/rank.hpp>
#include <migraphx/shape.hpp>
#include <migraphx/program.hpp>
#include <migraphx/onnx.hpp>
kahmed10's avatar
kahmed10 committed
6
#include <migraphx/tf.hpp>
7
#include <migraphx/register_target.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
8
#include <migraphx/generate.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
9
#include <migraphx/quantization.hpp>
10
#include <migraphx/ref/target.hpp>
11
#include <migraphx/load_save.hpp>
12
13
14
15
#include <migraphx/make_op.hpp>
#include <migraphx/json.hpp>
#include <migraphx/convert_to_json.hpp>
#include <algorithm>
Paul Fultz II's avatar
Paul Fultz II committed
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

namespace migraphx {

template <class F>
migraphx_status try_(F f, bool output = true) // NOLINT
{
    try
    {
        f();
    }
    catch(const migraphx::exception& ex)
    {
        if(output)
            std::cerr << "MIGraphX Error: " << ex.what() << std::endl;
        if(ex.error > 0)
            return migraphx_status(ex.error);
        else
            return migraphx_status_unknown_error;
    }
    catch(const std::exception& ex)
    {
        if(output)
            std::cerr << "MIGraphX Error: " << ex.what() << std::endl;
        return migraphx_status_unknown_error;
    }
    catch(...)
    {
        return migraphx_status_unknown_error;
    }
    return migraphx_status_success;
}

shape::type_t to_shape_type(migraphx_shape_datatype_t t)
{
    switch(t)
    {
#define MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT(x, y) \
    case migraphx_shape_##x: return shape::x;
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT)
#undef MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT
    }
    MIGRAPHX_THROW(migraphx_status_bad_param, "Unknown type");
}

migraphx_shape_datatype_t to_shape_type(shape::type_t t)
{
    switch(t)
    {
#define MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT(x, y) \
    case shape::x: return migraphx_shape_##x;
        MIGRAPHX_SHAPE_VISIT_TYPES(MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT)
#undef MIGRAPHX_DETAIL_SHAPE_CASE_CONVERT
    }
    MIGRAPHX_THROW(migraphx_status_bad_param, "Unknown type");
}

72
target get_target(const std::string& name) { return make_target(name); }
Paul Fultz II's avatar
Paul Fultz II committed
73
74
75
76
77

migraphx::compile_options to_compile_options(const migraphx_compile_options& options)
{
    migraphx::compile_options result{};
    result.offload_copy = options.offload_copy;
kahmed10's avatar
kahmed10 committed
78
    result.fast_math    = options.fast_math;
Paul Fultz II's avatar
Paul Fultz II committed
79
80
81
    return result;
}

82
83
84
85
86
87
88
migraphx::file_options to_file_options(const migraphx_file_options& options)
{
    migraphx::file_options result{};
    result.format = options.format;
    return result;
}

89
void set_default_dim_value(onnx_options& options, size_t value)
Paul Fultz II's avatar
Paul Fultz II committed
90
{
91
92
93
    options.default_dim_value = value;
}

kahmed10's avatar
kahmed10 committed
94
95
96
97
void set_nhwc(tf_options& options, bool is_nhwc) { options.is_nhwc = is_nhwc; }

void set_default_dim_value(tf_options& options, size_t value) { options.batch_size = value; }

98
99
void set_input_parameter_shape(onnx_options& options,
                               const char* name,
100
                               std::vector<std::size_t> dims)
101
{
102
    options.map_input_dims[std::string(name)] = std::move(dims);
Paul Fultz II's avatar
Paul Fultz II committed
103
104
}

kahmed10's avatar
kahmed10 committed
105
106
107
108
109
110
111
112
113
114
void set_input_parameter_shape(tf_options& options, const char* name, std::vector<std::size_t> dims)
{
    options.map_input_dims[std::string(name)] = std::move(dims);
}

void set_output_names(tf_options& options, std::vector<const char*> names)
{
    options.output_node_names = std::vector<std::string>(names.begin(), names.end());
}

Paul Fultz II's avatar
Paul Fultz II committed
115
116
117
118
119
120
121
122
123
template <class Value>
std::vector<const char*> get_names(const std::unordered_map<std::string, Value>& m)
{
    std::vector<const char*> result;
    std::transform(
        m.begin(), m.end(), std::back_inserter(result), [](auto&& p) { return p.first.c_str(); });
    return result;
}

Shucai Xiao's avatar
Shucai Xiao committed
124
125
126
127
128
129
130
131
132
133
134
135
void quantize_fp16_with_op_names(program& prog, std::vector<std::string>& names)
{
    if(names.empty())
    {
        names = {"all"};
    }

    migraphx::quantize_fp16(prog, names);
}

struct quantize_int8_options
{
136
137
    std::vector<parameter_map> calibration = {};
    std::vector<std::string> op_names      = {};
Shucai Xiao's avatar
Shucai Xiao committed
138
139
140
141
142
143
144
};

void add_op_name(quantize_int8_options& options, const char* name)
{
    options.op_names.push_back(name);
}

145
void add_calibration_data(quantize_int8_options& options, parameter_map& data)
Shucai Xiao's avatar
Shucai Xiao committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
    options.calibration.push_back(data);
}

void quantize_int8_wrap(program& prog, const target& t, quantize_int8_options& options)
{
    if(options.op_names.empty())
    {
        options.op_names = {"dot", "convolution"};
    }

    migraphx::quantize_int8(prog, t, options.calibration, options.op_names);
}

160
161
162
163
164
165
166
167
168
169
170
171
operation create_op(const char* name, const char* attributes)
{
    value v = value::object{};
    if(attributes != nullptr)
    {
        v = from_json_string(convert_to_json(std::string(attributes)));
    }
    auto op = make_op(name, v);

    return op;
}

Paul Fultz II's avatar
Paul Fultz II committed
172
173
174
175
176
177
template <class T>
bool equal(const T& x, const T& y)
{
    return x == y;
}

178
std::vector<argument> run(program& p, const parameter_map& params) { return p.eval(params); }
Paul Fultz II's avatar
Paul Fultz II committed
179

180
std::vector<shape> get_output_shapes(program& p) { return p.get_output_shapes(); }
Paul Fultz II's avatar
Paul Fultz II committed
181

Shucai Xiao's avatar
Shucai Xiao committed
182
183
184
void print_program(const program& p) { std::cout << p << std::endl; }

void print_module(const module& m) { std::cout << m << std::endl; }
Paul Fultz II's avatar
Paul Fultz II committed
185
186
187
188

} // namespace migraphx

<% generate_c_api_body() %>