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
e0646252
"vscode:/vscode.git/clone" did not exist on "ac739092f50051e08c00377c3595b225b4daa51e"
Commit
e0646252
authored
Aug 30, 2019
by
Shucai Xiao
Browse files
add more unit tests for better code coverage
parent
377021cd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
173 additions
and
6 deletions
+173
-6
src/quantization.cpp
src/quantization.cpp
+2
-2
test/quantization.cpp
test/quantization.cpp
+171
-4
No files found.
src/quantization.cpp
View file @
e0646252
...
@@ -261,7 +261,7 @@ static void ins_quantize_int8(program& prog,
...
@@ -261,7 +261,7 @@ static void ins_quantize_int8(program& prog,
auto
dilation
=
conv_op
.
dilation
;
auto
dilation
=
conv_op
.
dilation
;
auto
padding_mode
=
conv_op
.
padding_mode
;
auto
padding_mode
=
conv_op
.
padding_mode
;
auto
group
=
conv_op
.
group
;
auto
group
=
conv_op
.
group
;
auto
adjust_factor
=
1.0
f
/
(
ins_quant_params
[
0
].
first
*
ins_quant_params
[
1
].
first
);
auto
adjust_factor
=
std
::
round
(
1.0
f
/
(
ins_quant_params
[
0
].
first
*
ins_quant_params
[
1
].
first
)
)
;
auto
quant_conv
=
prog
.
insert_instruction
(
auto
quant_conv
=
prog
.
insert_instruction
(
ins
,
ins
,
...
@@ -295,7 +295,7 @@ static void ins_quantize_int8(program& prog,
...
@@ -295,7 +295,7 @@ static void ins_quantize_int8(program& prog,
}
}
else
else
{
{
MIGRAPHX_THROW
(
"QUANTIZE_INT8: does not support operator"
+
ins
->
name
());
MIGRAPHX_THROW
(
"QUANTIZE_INT8: does not support operator
"
+
ins
->
name
());
}
}
}
}
...
...
test/quantization.cpp
View file @
e0646252
...
@@ -296,10 +296,6 @@ TEST_CASE(dot_float)
...
@@ -296,10 +296,6 @@ TEST_CASE(dot_float)
auto
qb
=
auto
qb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cb
);
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cb
);
// quantize parameter c to int32 type
auto
qc
=
p
.
insert_instruction
(
std
::
next
(
pc
),
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int32_type
},
pc
);
auto
qdot
=
p
.
add_instruction
(
migraphx
::
op
::
quant_dot
{
1
,
0
},
qa
,
qb
);
auto
qdot
=
p
.
add_instruction
(
migraphx
::
op
::
quant_dot
{
1
,
0
},
qa
,
qb
);
auto
fdot
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
qdot
);
auto
fdot
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
qdot
);
std
::
vector
<
float
>
v_alpha
(
fdot
->
get_shape
().
elements
(),
200.0
f
);
std
::
vector
<
float
>
v_alpha
(
fdot
->
get_shape
().
elements
(),
200.0
f
);
...
@@ -317,6 +313,8 @@ TEST_CASE(dot_float)
...
@@ -317,6 +313,8 @@ TEST_CASE(dot_float)
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{
{
0.1
f
,
0.0
f
},
{
0.1
f
,
0.0
f
},
{
0.1
f
,
100.0
f
}};
{
0.1
f
,
0.0
f
},
{
0.1
f
,
0.0
f
},
{
0.1
f
,
100.0
f
}};
migraphx
::
quantize_int8
(
p
,
{
"dot"
},
quant_params
);
migraphx
::
quantize_int8
(
p
,
{
"dot"
},
quant_params
);
migraphx
::
run_passes
(
p
,
{
migraphx
::
dead_code_elimination
{}});
auto
qp
=
create_int8_quantized_prog
();
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
EXPECT
(
p
==
qp
);
...
@@ -585,4 +583,173 @@ TEST_CASE(dot_int32)
...
@@ -585,4 +583,173 @@ TEST_CASE(dot_int32)
EXPECT
(
p
==
qp
);
EXPECT
(
p
==
qp
);
}
}
TEST_CASE
(
conv_float
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
auto
input
=
p
.
add_parameter
(
"x"
,
migraphx
::
shape
{
migraphx
::
shape
::
float_type
,
{
4
,
3
,
3
,
3
}});
auto
weights
=
p
.
add_parameter
(
"w"
,
migraphx
::
shape
{
migraphx
::
shape
::
float_type
,
{
4
,
3
,
3
,
3
}});
p
.
add_instruction
(
migraphx
::
op
::
convolution
{},
input
,
weights
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sx
{
migraphx
::
shape
::
float_type
,
{
4
,
3
,
3
,
3
}};
migraphx
::
shape
sw
{
migraphx
::
shape
::
float_type
,
{
4
,
3
,
3
,
3
}};
auto
px
=
p
.
add_parameter
(
"x"
,
sx
);
auto
pw
=
p
.
add_parameter
(
"w"
,
sw
);
// quantize parameter a to int8 type, multiply the scale
std
::
vector
<
float
>
vfx
(
sx
.
elements
(),
0.1
f
);
auto
fx
=
p
.
add_literal
(
migraphx
::
literal
(
sx
,
vfx
));
auto
mx
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fx
,
px
);
auto
rx
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
mx
);
auto
cx
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rx
);
auto
qx
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cx
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pw
);
std
::
vector
<
float
>
vfw
(
sw
.
elements
(),
0.1
f
);
auto
fw
=
p
.
add_literal
(
migraphx
::
literal
(
sw
,
vfw
));
auto
mw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fw
,
pw
);
auto
rw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mw
);
auto
cw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rw
);
auto
qw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cw
);
auto
q_conv
=
p
.
add_instruction
(
migraphx
::
op
::
quant_convolution
{},
qx
,
qw
);
auto
f_conv
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
q_conv
);
std
::
vector
<
float
>
v_adj
(
f_conv
->
get_shape
().
elements
(),
100.0
f
);
auto
adj
=
p
.
add_literal
(
migraphx
::
literal
(
f_conv
->
get_shape
(),
v_adj
));
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
adj
,
f_conv
);
return
p
;
};
auto
p
=
create_program
();
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{{
0.1
f
,
0.0
f
},
{
0.1
f
,
0.0
f
}};
migraphx
::
quantize_int8
(
p
,
{
"convolution"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
TEST_CASE
(
conv_int32
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
auto
input
=
p
.
add_parameter
(
"x"
,
migraphx
::
shape
{
migraphx
::
shape
::
int32_type
,
{
4
,
3
,
3
,
3
}});
auto
weights
=
p
.
add_parameter
(
"w"
,
migraphx
::
shape
{
migraphx
::
shape
::
int32_type
,
{
4
,
3
,
3
,
3
}});
p
.
add_instruction
(
migraphx
::
op
::
convolution
{},
input
,
weights
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sx
{
migraphx
::
shape
::
int32_type
,
{
4
,
3
,
3
,
3
}};
migraphx
::
shape
sw
{
migraphx
::
shape
::
int32_type
,
{
4
,
3
,
3
,
3
}};
auto
px
=
p
.
add_parameter
(
"x"
,
sx
);
auto
pw
=
p
.
add_parameter
(
"w"
,
sw
);
// quantize parameter a to int8 type, multiply the scale
auto
fpx
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
px
);
std
::
vector
<
float
>
vfx
(
sx
.
elements
(),
0.1
f
);
auto
fx
=
p
.
add_literal
(
migraphx
::
literal
(
fpx
->
get_shape
(),
vfx
));
auto
mx
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fx
,
fpx
);
auto
rx
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
mx
);
auto
cx
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rx
);
auto
qx
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cx
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pw
);
auto
fpw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pw
);
std
::
vector
<
float
>
vfw
(
sw
.
elements
(),
0.1
f
);
auto
fw
=
p
.
add_literal
(
migraphx
::
literal
(
fpw
->
get_shape
(),
vfw
));
auto
mw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fw
,
fpw
);
auto
rw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mw
);
auto
cw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rw
);
auto
qw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cw
);
auto
q_conv
=
p
.
add_instruction
(
migraphx
::
op
::
quant_convolution
{},
qx
,
qw
);
std
::
vector
<
float
>
v_adj
(
q_conv
->
get_shape
().
elements
(),
100.0
f
);
auto
adj
=
p
.
add_literal
(
migraphx
::
literal
(
q_conv
->
get_shape
(),
v_adj
));
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
q_conv
,
adj
);
return
p
;
};
auto
p
=
create_program
();
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{{
0.1
f
,
0.0
f
},
{
0.1
f
,
0.0
f
}};
migraphx
::
quantize_int8
(
p
,
{
"convolution"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
TEST_CASE
(
conv_half
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
auto
input
=
p
.
add_parameter
(
"x"
,
migraphx
::
shape
{
migraphx
::
shape
::
half_type
,
{
4
,
3
,
3
,
3
}});
auto
weights
=
p
.
add_parameter
(
"w"
,
migraphx
::
shape
{
migraphx
::
shape
::
half_type
,
{
4
,
3
,
3
,
3
}});
p
.
add_instruction
(
migraphx
::
op
::
convolution
{},
input
,
weights
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sx
{
migraphx
::
shape
::
half_type
,
{
4
,
3
,
3
,
3
}};
migraphx
::
shape
sw
{
migraphx
::
shape
::
half_type
,
{
4
,
3
,
3
,
3
}};
auto
px
=
p
.
add_parameter
(
"x"
,
sx
);
auto
pw
=
p
.
add_parameter
(
"w"
,
sw
);
// quantize parameter a to int8 type, multiply the scale
auto
fpx
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
px
);
std
::
vector
<
float
>
vfx
(
sx
.
elements
(),
0.1
f
);
auto
fx
=
p
.
add_literal
(
migraphx
::
literal
(
fpx
->
get_shape
(),
vfx
));
auto
mx
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fx
,
fpx
);
auto
rx
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
mx
);
auto
cx
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rx
);
auto
qx
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cx
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pw
);
auto
fpw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pw
);
std
::
vector
<
float
>
vfw
(
sw
.
elements
(),
0.1
f
);
auto
fw
=
p
.
add_literal
(
migraphx
::
literal
(
fpw
->
get_shape
(),
vfw
));
auto
mw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fw
,
fpw
);
auto
rw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mw
);
auto
cw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rw
);
auto
qw
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cw
);
auto
q_conv
=
p
.
add_instruction
(
migraphx
::
op
::
quant_convolution
{},
qx
,
qw
);
auto
f_conv
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
q_conv
);
std
::
vector
<
float
>
v_adj
(
f_conv
->
get_shape
().
elements
(),
100.0
f
);
auto
adj
=
p
.
add_literal
(
migraphx
::
literal
(
f_conv
->
get_shape
(),
v_adj
));
auto
f_res
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
adj
,
f_conv
);
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
half_type
},
f_res
);
return
p
;
};
auto
p
=
create_program
();
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{{
0.1
f
,
0.0
f
},
{
0.1
f
,
0.0
f
}};
migraphx
::
quantize_int8
(
p
,
{
"convolution"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
test
::
run
(
argc
,
argv
);
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
test
::
run
(
argc
,
argv
);
}
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