onnx.cpp 3.69 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
#include <migraphx/onnx/onnx_parser.hpp>
25
#include <migraphx/onnx/op_parser.hpp>
Paul's avatar
Paul committed
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <functional>
#include <array>
Paul's avatar
Paul committed
31
#include <utility>
32
#include <vector>
Paul's avatar
Paul committed
33

Paul's avatar
Paul committed
34
#include <migraphx/program.hpp>
35
#include <migraphx/onnx.hpp>
36

Paul's avatar
Paul committed
37
namespace migraphx {
Paul's avatar
Paul committed
38
inline namespace MIGRAPHX_INLINE_NS {
Paul's avatar
Paul committed
39

Paul Fultz II's avatar
Paul Fultz II committed
40
template <class... Ts>
41
program parse_onnx_from(const onnx_options& options, Ts&&... xs)
Paul's avatar
Paul committed
42
{
Paul Fultz II's avatar
Paul Fultz II committed
43
    onnx::onnx_parser parser;
44
45
46
47
48
    parser.map_input_dims     = options.map_input_dims;
    parser.map_dyn_input_dims = options.map_dyn_input_dims;
    auto dim_val              = options.default_dim_value;
    if(dim_val != 0)
    {
49
        if(options.default_dyn_dim_value != shape::dynamic_dimension{1, 1})
50
51
52
53
54
55
        {
            MIGRAPHX_THROW("PARSE_ONNX_FROM: both default_dim_value and default_dyn_dim_value"
                           "set to non-default value");
        }
        else
        {
56
            parser.default_dyn_dim_value = {dim_val, dim_val};
57
58
59
60
61
62
        }
    }
    else
    {
        parser.default_dyn_dim_value = options.default_dyn_dim_value;
    }
Charlie Lin's avatar
Charlie Lin committed
63
64
65
66
67
    if(not options.map_input_dims.empty() and not options.map_dyn_input_dims.empty())
    {
        MIGRAPHX_THROW("PARSE_ONNX_FROM: both map_input_dims and map_dyn_input_dims non-empty, only"
                       "one should be used");
    }
68
    parser.skip_unknown_operators = options.skip_unknown_operators;
Shucai Xiao's avatar
Shucai Xiao committed
69
    parser.max_loop_iterations    = options.max_loop_iterations;
Charlie Lin's avatar
Charlie Lin committed
70
    parser.use_dyn_output         = options.use_dyn_output;
71

72
    if(options.print_program_on_error)
Paul's avatar
Paul committed
73
    {
74
75
76
77
78
79
80
81
82
83
        // Log the program when it can't be parsed
        try
        {
            parser.parse_from(std::forward<Ts>(xs)...);
        }
        catch(...)
        {
            std::cerr << parser.prog << std::endl;
            throw;
        }
Paul's avatar
Paul committed
84
    }
85
    else
Paul's avatar
Paul committed
86
    {
87
        parser.parse_from(std::forward<Ts>(xs)...);
Paul's avatar
Paul committed
88
    }
Charlie Lin's avatar
Charlie Lin committed
89

Paul's avatar
Paul committed
90
91
92
    return std::move(parser.prog);
}

93
program parse_onnx(const std::string& name, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
94
95
{
    std::fstream input(name.c_str(), std::ios::in | std::ios::binary);
96
    return parse_onnx_from(options, input, name);
Paul Fultz II's avatar
Paul Fultz II committed
97
98
}

99
program parse_onnx_buffer(const std::string& buffer, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
100
101
102
103
{
    return parse_onnx_from(options, buffer.data(), buffer.size());
}

104
program parse_onnx_buffer(const void* data, std::size_t size, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
105
106
107
108
{
    return parse_onnx_from(options, data, size);
}

109
110
std::vector<std::string> get_onnx_operators() { return onnx::get_op_parsers(); }

Paul's avatar
Paul committed
111
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
112
} // namespace migraphx