api.cpp 8.64 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 Fultz II's avatar
Paul Fultz II committed
24
25
26
27
28
#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
29
#include <migraphx/tf.hpp>
30
#include <migraphx/instruction_ref.hpp>
31
#include <migraphx/register_target.hpp>
Paul Fultz II's avatar
Paul Fultz II committed
32
#include <migraphx/generate.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
33
#include <migraphx/quantization.hpp>
34
#include <migraphx/ref/target.hpp>
35
#include <migraphx/load_save.hpp>
36
#include <migraphx/make_op.hpp>
37
#include <migraphx/register_op.hpp>
38
39
40
#include <migraphx/json.hpp>
#include <migraphx/convert_to_json.hpp>
#include <algorithm>
41
#include <cstdarg>
Paul Fultz II's avatar
Paul Fultz II committed
42
43
namespace migraphx {

44
45
46
47
48
49
50
static thread_local bool disable_exception_catch = false; // NOLINT

extern "C" void migraphx_test_private_disable_exception_catch(bool b)
{
    disable_exception_catch = b;
}

Paul Fultz II's avatar
Paul Fultz II committed
51
52
53
template <class F>
migraphx_status try_(F f, bool output = true) // NOLINT
{
54
    if(disable_exception_catch)
Paul Fultz II's avatar
Paul Fultz II committed
55
56
57
    {
        f();
    }
58
    else
Paul Fultz II's avatar
Paul Fultz II committed
59
    {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        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;
Paul Fultz II's avatar
Paul Fultz II committed
77
            return migraphx_status_unknown_error;
78
79
80
81
82
        }
        catch(...)
        {
            return migraphx_status_unknown_error;
        }
Paul Fultz II's avatar
Paul Fultz II committed
83
84
85
86
87
88
89
90
    }
    return migraphx_status_success;
}

shape::type_t to_shape_type(migraphx_shape_datatype_t t)
{
    switch(t)
    {
Paul Fultz II's avatar
Paul Fultz II committed
91
    case migraphx_shape_tuple_type: return shape::tuple_type;
Paul Fultz II's avatar
Paul Fultz II committed
92
93
94
95
96
97
98
99
100
101
102
103
#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)
    {
Paul Fultz II's avatar
Paul Fultz II committed
104
    case shape::tuple_type: return migraphx_shape_tuple_type;
Paul Fultz II's avatar
Paul Fultz II committed
105
106
107
108
109
110
111
112
#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");
}

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
template <class T>
auto to_obj_vector(const T* x, std::size_t n)
{
    std::vector<decltype((*x)->object)> result;
    std::transform(x, x + n, std::back_inserter(result), [&](auto&& y) { return y->object; });
    return result;
}

template <class T, class U>
auto to_objptr_vector(const U* x, std::size_t n)
{
    std::vector<T> result;
    std::transform(
        x, x + n, std::back_inserter(result), [&](auto&& y) { return std::addressof(y->object); });
    return result;
}

130
target get_target(const std::string& name) { return make_target(name); }
Paul Fultz II's avatar
Paul Fultz II committed
131

132
133
134
void set_offload_copy(compile_options& options, bool value) { options.offload_copy = value; }

void set_fast_math(compile_options& options, bool value) { options.fast_math = value; }
Paul Fultz II's avatar
Paul Fultz II committed
135

136
void set_file_format(file_options& options, const char* format) { options.format = format; }
137

138
void set_default_dim_value(onnx_options& options, size_t value)
Paul Fultz II's avatar
Paul Fultz II committed
139
{
140
141
142
    options.default_dim_value = value;
}

Shucai Xiao's avatar
Shucai Xiao committed
143
144
145
146
147
void set_default_loop_iterations(onnx_options& options, int64_t value)
{
    options.max_loop_iterations = value;
}

kahmed10's avatar
kahmed10 committed
148
149
150
151
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; }

152
153
void set_input_parameter_shape(onnx_options& options,
                               const char* name,
154
                               std::vector<std::size_t> dims)
155
{
156
    options.map_input_dims[std::string(name)] = std::move(dims);
Paul Fultz II's avatar
Paul Fultz II committed
157
158
}

kahmed10's avatar
kahmed10 committed
159
160
161
162
163
164
165
166
167
168
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
169
170
171
172
173
174
175
176
177
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
178
179
180
181
182
183
184
185
186
187
188
189
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
{
190
191
    std::vector<parameter_map> calibration = {};
    std::vector<std::string> op_names      = {};
Shucai Xiao's avatar
Shucai Xiao committed
192
193
194
195
196
197
198
};

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

199
void add_calibration_data(quantize_int8_options& options, parameter_map& data)
Shucai Xiao's avatar
Shucai Xiao committed
200
201
202
203
204
205
206
207
208
209
210
211
212
213
{
    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);
}

214
215
216
217
218
219
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
#endif

operation create_op(const char* name, const char* attributes, va_list vlist)
220
{
221
222
223
    std::string sattributes = attributes == nullptr ? "" : attributes;
    std::vector<char> buffer(sattributes.size() * 2);
    std::vsnprintf(buffer.data(), buffer.size(), sattributes.c_str(), vlist);
224
225
226
    value v = value::object{};
    if(attributes != nullptr)
    {
227
        v = from_json_string(convert_to_json(std::string(buffer.data())));
228
229
230
231
232
233
    }
    auto op = make_op(name, v);

    return op;
}

234
235
236
237
#ifdef __clang__
#pragma clang diagnostic pop
#endif

Paul Fultz II's avatar
Paul Fultz II committed
238
239
240
241
242
243
template <class T>
bool equal(const T& x, const T& y)
{
    return x == y;
}

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

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

Shucai Xiao's avatar
Shucai Xiao committed
248
249
250
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
251

252
253
254
255
256
migraphx::instruction_ref add_allocation(module& m, const migraphx::shape& s)
{
    return m.add_instruction(migraphx::make_op("allocate", {{"shape", migraphx::to_value(s)}}), {});
}

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
struct experimental_custom_op
{
    std::string name;
    experimental_custom_op() = default;

    experimental_custom_op(std::string pname) : name(std::move(pname)) {}
};

template <class CustomOp>
struct custom_operation
{
    template <class Self, class F>
    static auto reflect(Self&, F)
    {
        return pack();
    }
    CustomOp op;
    std::string name() const { return op.xobject.name; }

    shape compute_shape(std::vector<shape> inputs) const
    {
        return op.compute_shape(std::move(inputs));
    }

281
282
283
284
285
286
    // TODO: Compute method with module_args
    argument
    compute(migraphx::context ctx, migraphx::shape output_shape, std::vector<argument> inputs) const
    {
        return op.compute(std::move(ctx), std::move(output_shape), std::move(inputs));
    }
287
288
289
290
291
292
293
294
};

template <class CustomOp>
void register_custom_op(const CustomOp& op)
{
    register_op(custom_operation<CustomOp>{op});
}

kahmed10's avatar
kahmed10 committed
295
296
migraphx::context get_context(const program& p) { return p.get_context(); }

Paul Fultz II's avatar
Paul Fultz II committed
297
298
299
} // namespace migraphx

<% generate_c_api_body() %>