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
1decc5fc
Commit
1decc5fc
authored
Dec 08, 2022
by
charlie
Browse files
Initial
parent
75b854bf
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
190 additions
and
13 deletions
+190
-13
src/include/migraphx/op/pad.hpp
src/include/migraphx/op/pad.hpp
+21
-10
src/include/migraphx/shape.hpp
src/include/migraphx/shape.hpp
+6
-0
src/onnx/parse_pad.cpp
src/onnx/parse_pad.cpp
+6
-0
src/shape.cpp
src/shape.cpp
+17
-0
src/targets/ref/lowering.cpp
src/targets/ref/lowering.cpp
+3
-3
test/onnx/gen_onnx.py
test/onnx/gen_onnx.py
+45
-0
test/onnx/onnx_test.cpp
test/onnx/onnx_test.cpp
+36
-0
test/onnx/pad_attr_dyn_test.onnx
test/onnx/pad_attr_dyn_test.onnx
+0
-0
test/onnx/pad_cnst_dyn_test.onnx
test/onnx/pad_cnst_dyn_test.onnx
+0
-0
test/onnx/pad_dyn_reflect_error.onnx
test/onnx/pad_dyn_reflect_error.onnx
+0
-0
test/op_shape_test.cpp
test/op_shape_test.cpp
+36
-0
test/ref_ops_test.cpp
test/ref_ops_test.cpp
+20
-0
No files found.
src/include/migraphx/op/pad.hpp
View file @
1decc5fc
...
...
@@ -59,18 +59,29 @@ struct pad
std
::
string
name
()
const
{
return
"pad"
;
}
shape
compute_shape
(
std
::
vector
<
shape
>
inputs
)
const
{
check_shapes
{
inputs
,
*
this
}.
has
(
1
);
auto
&&
idims
=
inputs
.
front
().
lens
();
std
::
vector
<
std
::
size_t
>
rdims
(
idims
.
begin
(),
idims
.
end
());
std
::
size_t
num_dims
=
rdims
.
size
();
for
(
std
::
size_t
i
=
0
;
i
<
num_dims
;
i
++
)
check_shapes
{
inputs
,
*
this
,
true
}.
has
(
1
);
const
auto
&
s0
=
inputs
.
front
();
if
(
s0
.
dynamic
())
{
rdims
[
i
]
+=
pads
[
i
]
+
pads
[
i
+
num_dims
];
auto
out_dyn_dims
=
s0
.
dyn_dims
();
for
(
std
::
size_t
i
=
0
;
i
<
s0
.
ndim
();
++
i
)
{
out_dyn_dims
[
i
]
=
out_dyn_dims
[
i
]
+
pads
[
i
]
+
pads
[
i
+
s0
.
ndim
()];
}
return
{
s0
.
type
(),
out_dyn_dims
};
}
else
{
auto
&&
idims
=
s0
.
lens
();
std
::
vector
<
std
::
size_t
>
rdims
(
idims
.
begin
(),
idims
.
end
());
std
::
size_t
num_dims
=
rdims
.
size
();
for
(
std
::
size_t
i
=
0
;
i
<
num_dims
;
i
++
)
{
rdims
[
i
]
+=
pads
[
i
]
+
pads
[
i
+
num_dims
];
}
shape
s
{
s0
.
type
(),
rdims
};
return
s
;
}
shape
s
{
inputs
.
front
().
type
(),
rdims
};
return
s
;
}
std
::
size_t
pad_ndims
()
const
...
...
src/include/migraphx/shape.hpp
View file @
1decc5fc
...
...
@@ -107,6 +107,12 @@ struct shape
friend
bool
operator
==
(
const
std
::
size_t
&
x
,
const
dynamic_dimension
&
y
);
friend
bool
operator
!=
(
const
dynamic_dimension
&
x
,
const
std
::
size_t
&
y
);
friend
bool
operator
!=
(
const
std
::
size_t
&
x
,
const
dynamic_dimension
&
y
);
// add and subtract fixed std::size_t dimension
friend
dynamic_dimension
operator
+
(
const
dynamic_dimension
&
x
,
const
std
::
size_t
&
y
);
friend
dynamic_dimension
operator
+
(
const
std
::
size_t
&
x
,
const
dynamic_dimension
&
y
);
friend
dynamic_dimension
operator
-
(
const
dynamic_dimension
&
x
,
const
std
::
size_t
&
y
);
friend
dynamic_dimension
operator
-
(
const
std
::
size_t
&
x
,
const
dynamic_dimension
&
y
);
};
static
const
std
::
vector
<
type_t
>&
types
();
...
...
src/onnx/parse_pad.cpp
View file @
1decc5fc
...
...
@@ -147,7 +147,13 @@ struct parse_pad : op_parser<parse_pad>
{
auto
mode
=
info
.
attributes
.
at
(
"mode"
).
s
();
if
(
mode
==
"reflect"
)
{
if
(
args
.
front
()
->
get_shape
().
dynamic
())
{
MIGRAPHX_THROW
(
"PARSE_PAD: reflect padding with dynamic shape not supported"
);
}
return
reflect_pad
(
info
,
pads
,
args
.
front
());
}
if
(
mode
!=
"constant"
)
{
MIGRAPHX_THROW
(
...
...
src/shape.cpp
View file @
1decc5fc
...
...
@@ -529,6 +529,23 @@ bool operator==(const std::size_t& x, const shape::dynamic_dimension& y) { retur
bool
operator
!=
(
const
shape
::
dynamic_dimension
&
x
,
const
std
::
size_t
&
y
)
{
return
not
(
x
==
y
);
}
bool
operator
!=
(
const
std
::
size_t
&
x
,
const
shape
::
dynamic_dimension
&
y
)
{
return
not
(
x
==
y
);
}
shape
::
dynamic_dimension
operator
+
(
const
shape
::
dynamic_dimension
&
x
,
const
std
::
size_t
&
y
)
{
return
{
x
.
min
+
y
,
x
.
max
+
y
,
x
.
opt
+
y
};
}
shape
::
dynamic_dimension
operator
+
(
const
std
::
size_t
&
x
,
const
shape
::
dynamic_dimension
&
y
)
{
return
y
+
x
;
}
shape
::
dynamic_dimension
operator
-
(
const
shape
::
dynamic_dimension
&
x
,
const
std
::
size_t
&
y
)
{
return
{
x
.
min
-
y
,
x
.
max
-
y
,
x
.
opt
-
y
};
}
shape
::
dynamic_dimension
operator
-
(
const
std
::
size_t
&
x
,
const
shape
::
dynamic_dimension
&
y
)
{
return
{
x
-
y
.
min
,
x
-
y
.
max
,
x
-
y
.
opt
};
}
bool
operator
==
(
const
shape
&
x
,
const
shape
&
y
)
{
if
(
x
.
dynamic
()
and
y
.
dynamic
())
...
...
src/targets/ref/lowering.cpp
View file @
1decc5fc
...
...
@@ -346,10 +346,10 @@ struct ref_pad
std
::
string
name
()
const
{
return
"ref::pad"
;
}
shape
compute_shape
(
const
std
::
vector
<
shape
>&
inputs
)
const
{
return
op
.
compute_shape
(
inputs
);
}
argument
compute
(
context
&
,
const
shape
&
output_shape
,
std
::
vector
<
argument
>
args
)
const
argument
compute
(
context
&
,
const
dyn_output
&
dyn_out
,
std
::
vector
<
argument
>
args
)
const
{
assert
(
out
put_shape
.
standard
());
argument
result
{
out
put_shape
};
assert
(
dyn_out
.
com
put
ed
_shape
.
standard
());
argument
result
{
dyn_out
.
com
put
ed
_shape
};
result
.
visit
([
&
](
auto
output
)
{
using
type
=
typename
decltype
(
output
)
::
value_type
;
std
::
fill
(
output
.
begin
(),
output
.
end
(),
pad_clamp
<
type
>
(
op
.
value
));
...
...
test/onnx/gen_onnx.py
View file @
1decc5fc
...
...
@@ -4172,6 +4172,51 @@ def pad_reflect_multiaxis_test():
return
([
arg_pad
,
node
],
[
x
],
[
y
])
@
onnx_test
def
pad_attr_dyn_test
():
x
=
helper
.
make_tensor_value_info
(
'0'
,
TensorProto
.
FLOAT
,
[
None
,
None
])
y
=
helper
.
make_tensor_value_info
(
'1'
,
TensorProto
.
FLOAT
,
[
None
,
None
])
node
=
onnx
.
helper
.
make_node
(
'Pad'
,
inputs
=
[
'0'
],
pads
=
[
1
,
1
,
1
,
1
],
outputs
=
[
'1'
])
return
([
node
],
[
x
],
[
y
])
@
onnx_test
def
pad_cnst_dyn_test
():
x
=
helper
.
make_tensor_value_info
(
'0'
,
TensorProto
.
FLOAT
,
[
None
,
None
])
y
=
helper
.
make_tensor_value_info
(
'1'
,
TensorProto
.
FLOAT
,
[
None
,
None
])
sizes
=
np
.
array
([
0
,
2
,
0
,
1
])
pad_tensor
=
helper
.
make_tensor
(
name
=
'pad_size'
,
data_type
=
TensorProto
.
INT32
,
dims
=
sizes
.
shape
,
vals
=
sizes
.
astype
(
int
))
arg_pad
=
onnx
.
helper
.
make_node
(
'Constant'
,
inputs
=
[],
outputs
=
[
'arg_pad'
],
value
=
pad_tensor
)
node
=
onnx
.
helper
.
make_node
(
'Pad'
,
inputs
=
[
'0'
,
'arg_pad'
],
outputs
=
[
'1'
])
return
([
arg_pad
,
node
],
[
x
],
[
y
])
@
onnx_test
def
pad_dyn_reflect_error
():
x
=
helper
.
make_tensor_value_info
(
'0'
,
TensorProto
.
FLOAT
,
[
None
,
None
])
y
=
helper
.
make_tensor_value_info
(
'1'
,
TensorProto
.
FLOAT
,
[
None
,
None
])
node
=
onnx
.
helper
.
make_node
(
'Pad'
,
mode
=
'reflect'
,
inputs
=
[
'0'
],
pads
=
[
0
,
2
,
0
,
1
],
outputs
=
[
'1'
])
return
([
node
],
[
x
],
[
y
])
@
onnx_test
def
pow_test
():
...
...
test/onnx/onnx_test.cpp
View file @
1decc5fc
...
...
@@ -4006,6 +4006,42 @@ TEST_CASE(pad_3arg_test)
EXPECT
(
p
==
prog
);
}
TEST_CASE
(
pad_attr_dyn_test
)
{
migraphx
::
program
p
;
auto
*
mm
=
p
.
get_main_module
();
auto
x
=
mm
->
add_parameter
(
"0"
,
migraphx
::
shape
{
migraphx
::
shape
::
float_type
,
{{
2
,
4
,
2
},
{
2
,
4
,
2
}}});
auto
ret
=
mm
->
add_instruction
(
migraphx
::
make_op
(
"pad"
,
{{
"pads"
,
{
1
,
1
,
1
,
1
}}}),
x
);
mm
->
add_return
({
ret
});
migraphx
::
onnx_options
options
;
options
.
map_dyn_input_dims
[
"0"
]
=
{{
2
,
4
,
2
},
{
2
,
4
,
2
}};
auto
prog
=
parse_onnx
(
"pad_attr_dyn_test.onnx"
,
options
);
EXPECT
(
p
==
prog
);
}
TEST_CASE
(
pad_cnst_dyn_test
)
{
migraphx
::
program
p
;
auto
*
mm
=
p
.
get_main_module
();
auto
x
=
mm
->
add_parameter
(
"0"
,
migraphx
::
shape
{
migraphx
::
shape
::
float_type
,
{{
2
,
4
,
2
},
{
2
,
4
,
2
}}});
mm
->
add_literal
({
migraphx
::
shape
{
migraphx
::
shape
::
int32_type
,
{
4
}},
{
0
,
2
,
0
,
1
}});
auto
ret
=
mm
->
add_instruction
(
migraphx
::
make_op
(
"pad"
,
{{
"pads"
,
{
0
,
2
,
0
,
1
}}}),
x
);
mm
->
add_return
({
ret
});
migraphx
::
onnx_options
options
;
options
.
map_dyn_input_dims
[
"0"
]
=
{{
2
,
4
,
2
},
{
2
,
4
,
2
}};
auto
prog
=
parse_onnx
(
"pad_cnst_dyn_test.onnx"
,
options
);
EXPECT
(
p
==
prog
);
}
TEST_CASE
(
pad_dyn_reflect_error
)
{
migraphx
::
onnx_options
options
;
options
.
default_dyn_dim_value
=
{
2
,
4
,
2
};
EXPECT
(
test
::
throws
([
&
]
{
migraphx
::
parse_onnx
(
"pad_dyn_reflect_error.onnx"
,
options
);
}));
}
TEST_CASE
(
pad_reflect_test
)
{
migraphx
::
program
p
;
...
...
test/onnx/pad_attr_dyn_test.onnx
0 → 100644
View file @
1decc5fc
File added
test/onnx/pad_cnst_dyn_test.onnx
0 → 100644
View file @
1decc5fc
File added
test/onnx/pad_dyn_reflect_error.onnx
0 → 100644
View file @
1decc5fc
File added
test/op_shape_test.cpp
View file @
1decc5fc
...
...
@@ -1663,6 +1663,42 @@ TEST_CASE(nms_shape)
score_thres_s
);
}
TEST_CASE
(
pad_shape0
)
{
migraphx
::
shape
input
{
migraphx
::
shape
::
float_type
,
{
2
,
3
,
3
,
3
}};
migraphx
::
shape
output
{
migraphx
::
shape
::
float_type
,
{
2
,
3
,
5
,
5
}};
expect_shape
(
output
,
migraphx
::
make_op
(
"pad"
,
{{
"pads"
,
{
0
,
0
,
1
,
1
,
0
,
0
,
1
,
1
}}}),
input
);
}
TEST_CASE
(
pad_shape1
)
{
migraphx
::
shape
input
{
migraphx
::
shape
::
float_type
,
{
2
,
3
,
3
,
3
}};
migraphx
::
shape
output
{
migraphx
::
shape
::
float_type
,
{
2
,
3
,
6
,
6
}};
expect_shape
(
output
,
migraphx
::
make_op
(
"pad"
,
{{
"pads"
,
{
0
,
0
,
2
,
2
,
0
,
0
,
1
,
1
}}}),
input
);
}
TEST_CASE
(
pad_dyn_shape0
)
{
migraphx
::
shape
input
{
migraphx
::
shape
::
float_type
,
{{
1
,
4
,
2
},
{
3
,
3
,
0
},
{
3
,
3
,
0
},
{
3
,
3
,
0
}}};
migraphx
::
shape
output
{
migraphx
::
shape
::
float_type
,
{{
1
,
4
,
2
},
{
3
,
3
,
0
},
{
5
,
5
,
0
},
{
5
,
5
,
0
}}};
expect_shape
(
output
,
migraphx
::
make_op
(
"pad"
,
{{
"pads"
,
{
0
,
0
,
1
,
1
,
0
,
0
,
1
,
1
}}}),
input
);
}
TEST_CASE
(
pad_dyn_shape1
)
{
migraphx
::
shape
input
{
migraphx
::
shape
::
float_type
,
{{
1
,
4
,
2
},
{
3
,
3
,
0
},
{
3
,
5
,
5
},
{
3
,
5
,
5
}}};
migraphx
::
shape
output
{
migraphx
::
shape
::
float_type
,
{{
1
,
4
,
2
},
{
3
,
3
,
0
},
{
5
,
7
,
7
},
{
5
,
7
,
7
}}};
expect_shape
(
output
,
migraphx
::
make_op
(
"pad"
,
{{
"pads"
,
{
0
,
0
,
1
,
1
,
0
,
0
,
1
,
1
}}}),
input
);
}
TEST_CASE
(
pooling_shape0
)
{
migraphx
::
shape
input
{
migraphx
::
shape
::
float_type
,
{
4
,
3
,
3
,
3
}};
...
...
test/ref_ops_test.cpp
View file @
1decc5fc
...
...
@@ -5071,6 +5071,26 @@ TEST_CASE(pad_test_lowest_half)
EXPECT(migraphx::verify_range(results_vector, gold));
}
TEST_CASE(pad_dyn_test)
{
migraphx::program p;
auto* mm = p.get_main_module();
migraphx::shape s{migraphx::shape::float_type, {{2, 4, 2}, {2, 4, 2}}};
auto x = mm->add_parameter("x", s);
mm->add_instruction(migraphx::make_op("pad", {{"pads", {1, 1, 1, 1}}}), x);
p.compile(migraphx::ref::target{});
std::vector<float> data = {1, 2, 3, 4};
migraphx::parameter_map params;
migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 2}};
params["x"] = migraphx::argument(input_fixed_shape, data.data());
auto result = p.eval(params).back();
std::vector<float> results_vector(16);
result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
std::vector<float> gold{0, 0, 0, 0, 0, 1, 2, 0, 0, 3, 4, 0, 0, 0, 0, 0};
EXPECT(migraphx::verify_range(results_vector, gold));
}
TEST_CASE(pointwise_test)
{
migraphx::program p;
...
...
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