Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
MIGraphX
Commits
b8c8d09b
Unverified
Commit
b8c8d09b
authored
Dec 14, 2022
by
kahmed10
Committed by
GitHub
Dec 13, 2022
Browse files
Update file reading function to fix external data loading (#1460)
parent
a9d6071a
Changes
7
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
462 additions
and
396 deletions
+462
-396
src/file_buffer.cpp
src/file_buffer.cpp
+16
-8
src/include/migraphx/file_buffer.hpp
src/include/migraphx/file_buffer.hpp
+1
-1
src/onnx/onnx_parser.cpp
src/onnx/onnx_parser.cpp
+18
-5
test/onnx/external_constant_test.onnx
test/onnx/external_constant_test.onnx
+10
-0
test/onnx/external_constant_test.weight
test/onnx/external_constant_test.weight
+0
-0
test/onnx/gen_onnx.py
test/onnx/gen_onnx.py
+407
-382
test/onnx/onnx_test.cpp
test/onnx/onnx_test.cpp
+10
-0
No files found.
src/file_buffer.cpp
View file @
b8c8d09b
...
@@ -30,23 +30,31 @@ namespace migraphx {
...
@@ -30,23 +30,31 @@ namespace migraphx {
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
namespace
MIGRAPHX_INLINE_NS
{
template
<
class
T
>
template
<
class
T
>
T
generic_read_file
(
const
std
::
string
&
filename
)
T
generic_read_file
(
const
std
::
string
&
filename
,
size_t
offset
=
0
,
size_t
nbytes
=
0
)
{
{
std
::
ifstream
is
(
filename
,
std
::
ios
::
binary
|
std
::
ios
::
ate
);
std
::
ifstream
is
(
filename
,
std
::
ios
::
binary
|
std
::
ios
::
ate
);
std
::
streamsize
size
=
is
.
tellg
();
if
(
nbytes
==
0
)
if
(
size
<
1
)
{
// if there is a non-zero offset and nbytes is not set,
// calculate size of remaining bytes to read
nbytes
=
is
.
tellg
();
if
(
offset
>
nbytes
)
MIGRAPHX_THROW
(
"offset is larger than file size"
);
nbytes
-=
offset
;
}
if
(
nbytes
<
1
)
MIGRAPHX_THROW
(
"Invalid size for: "
+
filename
);
MIGRAPHX_THROW
(
"Invalid size for: "
+
filename
);
is
.
seekg
(
0
,
std
::
ios
::
beg
);
is
.
seekg
(
offset
,
std
::
ios
::
beg
);
T
buffer
(
size
,
0
);
T
buffer
(
nbytes
,
0
);
if
(
not
is
.
read
(
&
buffer
[
0
],
size
))
if
(
not
is
.
read
(
&
buffer
[
0
],
nbytes
))
MIGRAPHX_THROW
(
"Error reading file: "
+
filename
);
MIGRAPHX_THROW
(
"Error reading file: "
+
filename
);
return
buffer
;
return
buffer
;
}
}
std
::
vector
<
char
>
read_buffer
(
const
std
::
string
&
filename
)
std
::
vector
<
char
>
read_buffer
(
const
std
::
string
&
filename
,
size_t
offset
,
size_t
nbytes
)
{
{
return
generic_read_file
<
std
::
vector
<
char
>>
(
filename
);
return
generic_read_file
<
std
::
vector
<
char
>>
(
filename
,
offset
,
nbytes
);
}
}
std
::
string
read_string
(
const
std
::
string
&
filename
)
std
::
string
read_string
(
const
std
::
string
&
filename
)
...
...
src/include/migraphx/file_buffer.hpp
View file @
b8c8d09b
...
@@ -31,7 +31,7 @@
...
@@ -31,7 +31,7 @@
namespace
migraphx
{
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
namespace
MIGRAPHX_INLINE_NS
{
std
::
vector
<
char
>
read_buffer
(
const
std
::
string
&
filename
);
std
::
vector
<
char
>
read_buffer
(
const
std
::
string
&
filename
,
size_t
offset
=
0
,
size_t
nbytes
=
0
);
std
::
string
read_string
(
const
std
::
string
&
filename
);
std
::
string
read_string
(
const
std
::
string
&
filename
);
void
write_buffer
(
const
std
::
string
&
filename
,
const
char
*
buffer
,
std
::
size_t
size
);
void
write_buffer
(
const
std
::
string
&
filename
,
const
char
*
buffer
,
std
::
size_t
size
);
...
...
src/onnx/onnx_parser.cpp
View file @
b8c8d09b
...
@@ -393,18 +393,31 @@ literal onnx_parser::parse_value(const onnx::AttributeProto& attr) const
...
@@ -393,18 +393,31 @@ literal onnx_parser::parse_value(const onnx::AttributeProto& attr) const
literal
onnx_parser
::
parse_tensor
(
const
onnx
::
TensorProto
&
t
)
const
literal
onnx_parser
::
parse_tensor
(
const
onnx
::
TensorProto
&
t
)
const
{
{
std
::
vector
<
std
::
size_t
>
dims
(
t
.
dims
().
begin
(),
t
.
dims
().
end
());
std
::
vector
<
std
::
size_t
>
dims
(
t
.
dims
().
begin
(),
t
.
dims
().
end
());
if
(
not
t
.
external_data
().
empty
())
auto
type
=
get_type
(
t
.
data_type
());
shape
tensor_shape
(
type
,
dims
);
auto
external_data
=
t
.
external_data
();
if
(
not
external_data
.
empty
())
{
const
std
::
string
&
data_file
=
external_data
.
at
(
0
).
value
();
size_t
num_data_fields
=
external_data
.
size
();
size_t
offset
=
0
;
size_t
nbytes
=
tensor_shape
.
bytes
();
if
(
num_data_fields
>
1
)
// if offset field is present
{
offset
=
std
::
stoul
(
t
.
external_data
().
at
(
1
).
value
());
}
if
(
num_data_fields
>
2
)
// if nbytes field is present
{
{
const
std
::
string
&
data_file
=
t
.
external_data
().
at
(
0
).
value
();
nbytes
=
std
::
stoul
(
t
.
external_data
().
at
(
2
).
value
());
auto
raw_buffer
=
read_buffer
(
path
+
"/"
+
data_file
);
}
auto
raw_buffer
=
read_buffer
(
path
+
"/"
+
data_file
,
offset
,
nbytes
);
std
::
string
s
(
raw_buffer
.
begin
(),
raw_buffer
.
end
());
std
::
string
s
(
raw_buffer
.
begin
(),
raw_buffer
.
end
());
auto
type
=
get_type
(
t
.
data_type
());
return
create_literal
(
type
,
dims
,
s
.
data
());
return
create_literal
(
type
,
dims
,
s
.
data
());
}
}
if
(
t
.
has_raw_data
())
if
(
t
.
has_raw_data
())
{
{
const
std
::
string
&
s
=
t
.
raw_data
();
const
std
::
string
&
s
=
t
.
raw_data
();
auto
type
=
get_type
(
t
.
data_type
());
return
create_literal
(
type
,
dims
,
s
.
data
());
return
create_literal
(
type
,
dims
,
s
.
data
());
}
}
...
...
test/onnx/external_constant_test.onnx
0 → 100644
View file @
b8c8d09b
external_constant_test:¡
v0"Constant*g
value*[Bconst_tensorj)
locationexternal_constant_test.weightj
offset48j
length24p external_constant_testb
0
B
\ No newline at end of file
test/onnx/external_constant_test.weight
0 → 100644
View file @
b8c8d09b
File added
test/onnx/gen_onnx.py
View file @
b8c8d09b
This diff is collapsed.
Click to expand it.
test/onnx/onnx_test.cpp
View file @
b8c8d09b
...
@@ -1818,6 +1818,16 @@ migraphx::program create_external_data_prog()
...
@@ -1818,6 +1818,16 @@ migraphx::program create_external_data_prog()
return p;
return p;
}
}
TEST_CASE(external_constant_test)
{
migraphx::program p;
auto* mm = p.get_main_module();
mm->add_literal(migraphx::literal{{migraphx::shape::int64_type, {3}}, {0, 1, 2}});
auto prog = optimize_onnx("external_constant_test.onnx");
EXPECT(p == prog);
}
TEST_CASE(external_data_test)
TEST_CASE(external_data_test)
{
{
migraphx::program p = create_external_data_prog();
migraphx::program p = create_external_data_prog();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment