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
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include <getopt.h>
#include <iostream>
#include <memory>
#include "ngraph/file_util.hpp"
#include "ngraph/runtime/plaidml/plaidml_backend.hpp"
#include "ngraph/serializer.hpp"
static const struct option opts[] = {{"backend", required_argument, nullptr, 'b'},
{"format", required_argument, nullptr, 'f'},
{"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, '\0'}};
int main(int argc, char** argv)
{
int opt;
bool err = false;
bool usage = false;
std::string model;
std::string output;
std::string backend_name = "PlaidML";
plaidml_file_format format = PLAIDML_FILE_FORMAT_TILE;
while ((opt = getopt_long(argc, argv, "f:b:h", opts, nullptr)) != -1)
{
switch (opt)
{
case 'b': backend_name = optarg; break;
case 'h': usage = true; break;
case 'f':
if (!strcmp(optarg, "tile"))
{
format = PLAIDML_FILE_FORMAT_TILE;
}
else if (!strcmp(optarg, "human"))
{
format = PLAIDML_FILE_FORMAT_STRIPE_HUMAN;
}
else if (!strcmp(optarg, "prototxt"))
{
format = PLAIDML_FILE_FORMAT_STRIPE_PROTOTXT;
}
else if (!strcmp(optarg, "binary"))
{
format = PLAIDML_FILE_FORMAT_STRIPE_BINARY;
}
else
{
err = true;
}
break;
case '?':
default: err = true; break;
}
}
if (optind + 2 != argc)
{
err = true;
}
else
{
model = argv[optind];
output = argv[optind + 1];
if (model.empty())
{
err = true;
}
else if (!ngraph::file_util::exists(model))
{
std::cerr << "File " << model << " not found\n";
err = true;
}
if (output.empty())
{
err = true;
}
else if (ngraph::file_util::exists(output))
{
std::cerr << "File " << output << " already exists; not overwriting\n";
err = true;
}
}
if (backend_name.substr(0, backend_name.find(':')) != "PlaidML")
{
std::cerr << "Unsupported backend: " << backend_name << "\n";
err = true;
}
if (err || usage)
{
std::cerr << R"###(
DESCRIPTION
Convert an ngraph JSON model to one of PlaidML's file formats.
SYNOPSIS
ngraph-to-plaidml [--backend|-b <backend>] MODEL OUTPUT
OPTIONS
-b|--backend Backend to use (default: PlaidML)
-f|--format Format to use (tile, human, prototxt, binary, or json; default: tile)
)###";
}
if (err)
{
return EXIT_FAILURE;
}
if (usage)
{
return EXIT_SUCCESS;
}
std::cerr << "Reading nGraph model from " << model << "\n";
std::shared_ptr<ngraph::Function> f = ngraph::deserialize(model);
std::shared_ptr<ngraph::runtime::Backend> base_backend =
ngraph::runtime::Backend::create(backend_name);
std::shared_ptr<ngraph::runtime::plaidml::PlaidML_Backend> backend =
std::dynamic_pointer_cast<ngraph::runtime::plaidml::PlaidML_Backend>(base_backend);
if (!backend)
{
std::cerr << "Failed to load PlaidML backend\n";
return EXIT_FAILURE;
}
auto exec = backend->compile(f);
static_cast<ngraph::runtime::plaidml::PlaidML_Executable*>(exec.get())
->save_as_format(output, format);
std::cerr << "Wrote output to " << output << "\n";
return EXIT_SUCCESS;
}