"test/vscode:/vscode.git/clone" did not exist on "ea3872f748ba7e6bf5f4262d3336ed32e20105d9"
onnx.cpp 3.4 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    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)
    {
        if(options.default_dyn_dim_value != shape::dynamic_dimension{1, 1, 0})
        {
            MIGRAPHX_THROW("PARSE_ONNX_FROM: both default_dim_value and default_dyn_dim_value"
                           "set to non-default value");
        }
        else
        {
            parser.default_dyn_dim_value = {dim_val, dim_val, 0};
        }
    }
    else
    {
        parser.default_dyn_dim_value = options.default_dyn_dim_value;
    }
63
    parser.skip_unknown_operators = options.skip_unknown_operators;
Shucai Xiao's avatar
Shucai Xiao committed
64
    parser.max_loop_iterations    = options.max_loop_iterations;
65

66
    if(options.print_program_on_error)
Paul's avatar
Paul committed
67
    {
68
69
70
71
72
73
74
75
76
77
        // 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
78
    }
79
    else
Paul's avatar
Paul committed
80
    {
81
        parser.parse_from(std::forward<Ts>(xs)...);
Paul's avatar
Paul committed
82
83
84
85
    }
    return std::move(parser.prog);
}

86
program parse_onnx(const std::string& name, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
87
88
{
    std::fstream input(name.c_str(), std::ios::in | std::ios::binary);
89
    return parse_onnx_from(options, input, name);
Paul Fultz II's avatar
Paul Fultz II committed
90
91
}

92
program parse_onnx_buffer(const std::string& buffer, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
93
94
95
96
{
    return parse_onnx_from(options, buffer.data(), buffer.size());
}

97
program parse_onnx_buffer(const void* data, std::size_t size, const onnx_options& options)
Paul Fultz II's avatar
Paul Fultz II committed
98
99
100
101
{
    return parse_onnx_from(options, data, size);
}

102
103
std::vector<std::string> get_onnx_operators() { return onnx::get_op_parsers(); }

Paul's avatar
Paul committed
104
} // namespace MIGRAPHX_INLINE_NS
Paul's avatar
Paul committed
105
} // namespace migraphx