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
343c4cac
Commit
343c4cac
authored
Aug 30, 2019
by
Shucai Xiao
Browse files
add more unit test for better code coverage
parent
4ce41ebc
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
343 additions
and
85 deletions
+343
-85
src/quantization.cpp
src/quantization.cpp
+4
-5
test/gpu/miopen.cpp
test/gpu/miopen.cpp
+12
-12
test/gpu/quantization.cpp
test/gpu/quantization.cpp
+0
-68
test/quantization.cpp
test/quantization.cpp
+327
-0
No files found.
src/quantization.cpp
View file @
343c4cac
...
...
@@ -57,7 +57,7 @@ instruction_ref insert_quant_ins(program& prog,
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
shape
::
float_type
},
scaled_ins
);
}
std
::
vector
<
float
>
vec_scale
(
scaled_ins
->
get_shape
().
elements
(),
scale
);
auto
l_scale
=
prog
.
add_literal
(
literal
(
scaled
_ins
->
get_shape
(),
vec_scale
));
auto
l_scale
=
prog
.
add_literal
(
literal
(
float
_ins
->
get_shape
(),
vec_scale
));
scaled_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
mul
{},
l_scale
,
float_ins
);
}
...
...
@@ -71,7 +71,7 @@ instruction_ref insert_quant_ins(program& prog,
insert_loc
,
op
::
convert
{
shape
::
float_type
},
shifted_ins
);
}
std
::
vector
<
float
>
vec_shift
(
shifted_ins
->
get_shape
().
elements
(),
shift
);
auto
l_shift
=
prog
.
add_literal
(
literal
(
shifted
_ins
->
get_shape
(),
vec_shift
));
auto
l_shift
=
prog
.
add_literal
(
literal
(
float
_ins
->
get_shape
(),
vec_shift
));
shifted_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
add
{},
l_shift
,
float_ins
);
}
...
...
@@ -220,8 +220,7 @@ static void ins_quantize_int8(program& prog,
{
auto
fp32_c
=
prog
.
insert_instruction
(
ins
,
op
::
convert
{
shape
::
float_type
},
inputs
.
back
());
auto
fp32_beta_c
=
prog
.
insert_instruction
(
ins
,
op
::
mul
{},
l_beta
,
fp32_c
);
beta_c
=
prog
.
insert_instruction
(
ins
,
op
::
convert
{
orig_type
},
fp32_beta_c
);
beta_c
=
prog
.
insert_instruction
(
ins
,
op
::
mul
{},
l_beta
,
fp32_c
);
}
else
{
...
...
@@ -357,7 +356,7 @@ void quantize_int8(program& prog,
auto
s
=
input
->
get_shape
();
if
((
s
.
type
()
==
shape
::
float_type
or
s
.
type
()
==
shape
::
double_type
or
s
.
type
()
==
shape
::
int32_type
)
and
s
.
type
()
==
shape
::
half_type
or
s
.
type
()
==
shape
::
int32_type
)
and
s
.
type
()
!=
quant_type
)
{
// if the input is a convert operator, uses its input
...
...
test/gpu/miopen.cpp
View file @
343c4cac
...
...
@@ -3846,32 +3846,32 @@ struct test_reduce_mean_half : verify_program<test_reduce_mean_half>
};
};
struct
test_
convert
:
verify_program
<
test_
convert
>
struct
test_
round
:
verify_program
<
test_
round
>
{
migraphx
::
program
create_program
()
const
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
float_type
,
{
8
,
24
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
float_type
,
{
24
,
6
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
ia
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
pa
);
auto
ib
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
pb
);
p
.
add_instruction
(
migraphx
::
op
::
quant_dot
{},
ia
,
ib
);
migraphx
::
shape
s
{
migraphx
::
shape
::
float_type
,
{
2
,
3
,
4
,
6
}};
auto
param
=
p
.
add_parameter
(
"x"
,
s
);
p
.
add_instruction
(
migraphx
::
op
::
round
{},
param
);
return
p
;
};
};
struct
test_
round
:
verify_program
<
test_
round
>
struct
test_
convert
:
verify_program
<
test_
convert
>
{
migraphx
::
program
create_program
()
const
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
float_type
,
{
8
,
24
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
float_type
,
{
24
,
6
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
ia
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
pa
);
auto
ib
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
pb
);
p
.
add_instruction
(
migraphx
::
op
::
quant_dot
{},
ia
,
ib
);
migraphx
::
shape
s
{
migraphx
::
shape
::
float_type
,
{
2
,
3
,
4
,
6
}};
auto
param
=
p
.
add_parameter
(
"x"
,
s
);
p
.
add_instruction
(
migraphx
::
op
::
round
{},
param
);
return
p
;
};
};
...
...
test/gpu/quantization.cpp
View file @
343c4cac
...
...
@@ -65,72 +65,4 @@ TEST_CASE(target_copy)
}
}
TEST_CASE
(
dot_large_alpha_beta_float
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
float_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
float_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
float_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
p
.
add_instruction
(
migraphx
::
op
::
dot
{
20.0
f
,
50.5
f
},
pa
,
pb
,
pc
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
float_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
float_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
float_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
// quantize parameter a to int8 type, multiply the scale
std
::
vector
<
float
>
vfa
(
sa
.
elements
(),
0.1
f
);
auto
fa
=
p
.
add_literal
(
migraphx
::
literal
(
sa
,
vfa
));
auto
ma
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fa
,
pa
);
// add the shift
std
::
vector
<
float
>
vsa
(
sa
.
elements
(),
1.0
f
);
auto
sfta
=
p
.
add_literal
(
migraphx
::
literal
(
sa
,
vsa
));
auto
msa
=
p
.
add_instruction
(
migraphx
::
op
::
add
{},
sfta
,
ma
);
auto
ra
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
msa
);
auto
ca
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
ra
);
auto
qa
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
ca
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pb
);
std
::
vector
<
float
>
vfb
(
sb
.
elements
(),
0.1
f
);
auto
fb
=
p
.
add_literal
(
migraphx
::
literal
(
sb
,
vfb
));
auto
mb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fb
,
pb
);
auto
rb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mb
);
auto
cb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rb
);
auto
qb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cb
);
// quantize parameter b 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
{
2000
,
51
},
qa
,
qb
,
qc
);
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
qdot
);
return
p
;
};
auto
p
=
create_program
();
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{
{
0.1
f
,
1.0
f
},
{
0.1
f
,
0.0
f
},
{
0.1
f
,
100.0
f
}};
// default scale 64.0f is used for all args
migraphx
::
quantize_int8
(
p
,
{
"dot"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
test
::
run
(
argc
,
argv
);
}
test/quantization.cpp
View file @
343c4cac
...
...
@@ -254,4 +254,331 @@ TEST_CASE(op_capture)
}
}
TEST_CASE
(
dot_float
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
float_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
float_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
float_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
p
.
add_instruction
(
migraphx
::
op
::
dot
{
2.0
f
,
1.5
f
},
pa
,
pb
,
pc
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
float_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
float_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
float_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
// quantize parameter a to int8 type, multiply the scale
std
::
vector
<
float
>
vfa
(
sa
.
elements
(),
0.1
f
);
auto
fa
=
p
.
add_literal
(
migraphx
::
literal
(
sa
,
vfa
));
auto
ma
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fa
,
pa
);
auto
ra
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
ma
);
auto
ca
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
ra
);
auto
qa
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
ca
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pb
);
std
::
vector
<
float
>
vfb
(
sb
.
elements
(),
0.1
f
);
auto
fb
=
p
.
add_literal
(
migraphx
::
literal
(
sb
,
vfb
));
auto
mb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fb
,
pb
);
auto
rb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mb
);
auto
cb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rb
);
auto
qb
=
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
fdot
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
qdot
);
std
::
vector
<
float
>
v_alpha
(
fdot
->
get_shape
().
elements
(),
200.0
f
);
auto
new_alpha
=
p
.
add_literal
(
migraphx
::
literal
(
fdot
->
get_shape
(),
v_alpha
));
auto
alpha_ab
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
new_alpha
,
fdot
);
std
::
vector
<
float
>
v_beta
(
pc
->
get_shape
().
elements
(),
1.5
f
);
auto
beta
=
p
.
add_literal
(
migraphx
::
literal
(
pc
->
get_shape
(),
v_beta
));
auto
beta_c
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
beta
,
pc
);
p
.
add_instruction
(
migraphx
::
op
::
add
{},
alpha_ab
,
beta_c
);
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
},
{
0.1
f
,
100.0
f
}};
migraphx
::
quantize_int8
(
p
,
{
"dot"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
TEST_CASE
(
dot_double_2args
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
double_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
double_type
,
{
16
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
p
.
add_instruction
(
migraphx
::
op
::
dot
{
2.0
f
,
1.5
f
},
pa
,
pb
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
double_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
double_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
double_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
// quantize parameter a to int8 type, multiply the scale
std
::
vector
<
float
>
vfa
(
sa
.
elements
(),
0.1
f
);
auto
fpa
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pa
);
auto
fa
=
p
.
add_literal
(
migraphx
::
literal
({
migraphx
::
shape
::
float_type
,
sa
.
lens
()},
vfa
));
auto
ma
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fa
,
fpa
);
auto
ra
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
ma
);
auto
ca
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
ra
);
auto
qa
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
ca
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pb
);
auto
fpb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pb
);
std
::
vector
<
float
>
vfb
(
sb
.
elements
(),
0.1
f
);
auto
fb
=
p
.
add_literal
(
migraphx
::
literal
({
migraphx
::
shape
::
float_type
,
sb
.
lens
()},
vfb
));
auto
mb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fb
,
fpb
);
auto
rb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mb
);
auto
cb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rb
);
auto
qb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cb
);
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
);
std
::
vector
<
float
>
v_alpha
(
fdot
->
get_shape
().
elements
(),
200.0
f
);
auto
new_alpha
=
p
.
add_literal
(
migraphx
::
literal
(
fdot
->
get_shape
(),
v_alpha
));
auto
alpha_ab
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
new_alpha
,
fdot
);
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
double_type
},
alpha_ab
);
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
,
{
"dot"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
TEST_CASE
(
dot_large_alpha_beta_float
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
float_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
float_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
float_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
p
.
add_instruction
(
migraphx
::
op
::
dot
{
20.0
f
,
50.5
f
},
pa
,
pb
,
pc
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
float_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
float_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
float_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
// quantize parameter a to int8 type, multiply the scale
std
::
vector
<
float
>
vfa
(
sa
.
elements
(),
0.1
f
);
auto
fa
=
p
.
add_literal
(
migraphx
::
literal
(
sa
,
vfa
));
auto
ma
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fa
,
pa
);
// add the shift
std
::
vector
<
float
>
vsa
(
sa
.
elements
(),
1.0
f
);
auto
sfta
=
p
.
add_literal
(
migraphx
::
literal
(
sa
,
vsa
));
auto
msa
=
p
.
add_instruction
(
migraphx
::
op
::
add
{},
sfta
,
ma
);
auto
ra
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
msa
);
auto
ca
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
ra
);
auto
qa
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
ca
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pb
);
std
::
vector
<
float
>
vfb
(
sb
.
elements
(),
0.1
f
);
auto
fb
=
p
.
add_literal
(
migraphx
::
literal
(
sb
,
vfb
));
auto
mb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fb
,
pb
);
auto
rb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mb
);
auto
cb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rb
);
auto
qb
=
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
{
2000
,
51
},
qa
,
qb
,
qc
);
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
qdot
);
return
p
;
};
auto
p
=
create_program
();
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{
{
0.1
f
,
1.0
f
},
{
0.1
f
,
0.0
f
},
{
0.1
f
,
100.0
f
}};
migraphx
::
quantize_int8
(
p
,
{
"dot"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
TEST_CASE
(
dot_large_alpha_beta_int32
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
int32_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
int32_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
int32_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
p
.
add_instruction
(
migraphx
::
op
::
dot
{
20.0
f
,
50.0
f
},
pa
,
pb
,
pc
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
int32_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
int32_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
int32_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
// quantize parameter a to int8 type, multiply the scale
std
::
vector
<
float
>
vfa
(
sa
.
elements
(),
0.1
f
);
auto
fa
=
p
.
add_literal
(
migraphx
::
literal
({
migraphx
::
shape
::
float_type
,
sa
.
lens
()},
vfa
));
auto
conv_a
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pa
);
auto
ma
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fa
,
conv_a
);
// add the shift
std
::
vector
<
float
>
vsa
(
sa
.
elements
(),
1.0
f
);
auto
sfta
=
p
.
add_literal
(
migraphx
::
literal
({
migraphx
::
shape
::
float_type
,
sa
.
lens
()},
vsa
));
auto
msa
=
p
.
add_instruction
(
migraphx
::
op
::
add
{},
sfta
,
ma
);
auto
ra
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
msa
);
auto
ca
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
ra
);
auto
qa
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
ca
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pb
);
std
::
vector
<
float
>
vfb
(
sb
.
elements
(),
0.1
f
);
auto
fb
=
p
.
add_literal
(
migraphx
::
literal
({
migraphx
::
shape
::
float_type
,
sb
.
lens
()},
vfb
));
auto
conv_b
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pb
);
auto
mb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fb
,
conv_b
);
auto
rb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mb
);
auto
cb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rb
);
auto
qb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cb
);
p
.
add_instruction
(
migraphx
::
op
::
quant_dot
{
2000
,
50
},
qa
,
qb
,
pc
);
return
p
;
};
auto
p
=
create_program
();
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{{
0.1
f
,
1.0
f
},
{
0.1
f
,
0.0
f
},
{
0.1
f
,
100.0
f
}};
migraphx
::
quantize_int8
(
p
,
{
"dot"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
TEST_CASE
(
dot_int32
)
{
auto
create_program
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
int32_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
int32_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
int32_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
p
.
add_instruction
(
migraphx
::
op
::
dot
{
2.0
f
,
5.5
f
},
pa
,
pb
,
pc
);
return
p
;
};
auto
create_int8_quantized_prog
=
[]
{
migraphx
::
program
p
;
migraphx
::
shape
sa
{
migraphx
::
shape
::
int32_type
,
{
2
,
16
}};
migraphx
::
shape
sb
{
migraphx
::
shape
::
int32_type
,
{
16
,
8
}};
migraphx
::
shape
sc
{
migraphx
::
shape
::
int32_type
,
{
2
,
8
}};
auto
pa
=
p
.
add_parameter
(
"a"
,
sa
);
auto
pb
=
p
.
add_parameter
(
"b"
,
sb
);
auto
pc
=
p
.
add_parameter
(
"c"
,
sc
);
// quantize parameter a to int8 type, multiply the scale
std
::
vector
<
float
>
vfa
(
sa
.
elements
(),
0.1
f
);
auto
fa
=
p
.
add_literal
(
migraphx
::
literal
({
migraphx
::
shape
::
float_type
,
sa
.
lens
()},
vfa
));
auto
conv_a
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pa
);
auto
ma
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
fa
,
conv_a
);
// add the shift
std
::
vector
<
float
>
vsa
(
sa
.
elements
(),
1.0
f
);
auto
sfta
=
p
.
add_literal
(
migraphx
::
literal
({
migraphx
::
shape
::
float_type
,
sa
.
lens
()},
vsa
));
auto
msa
=
p
.
add_instruction
(
migraphx
::
op
::
add
{},
sfta
,
ma
);
auto
ra
=
p
.
add_instruction
(
migraphx
::
op
::
round
{},
msa
);
auto
ca
=
p
.
add_instruction
(
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
ra
);
auto
qa
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
ca
);
// quantize parameter b to int8 type
auto
insert_loc
=
std
::
next
(
pb
);
std
::
vector
<
float
>
vfb
(
sb
.
elements
(),
0.1
f
);
auto
fb
=
p
.
add_literal
(
migraphx
::
literal
({
migraphx
::
shape
::
float_type
,
sb
.
lens
()},
vfb
));
auto
conv_b
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pb
);
auto
mb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
mul
{},
fb
,
conv_b
);
auto
rb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
round
{},
mb
);
auto
cb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
clip
{
127.0
f
,
-
128.0
f
},
rb
);
auto
qb
=
p
.
insert_instruction
(
insert_loc
,
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int8_type
},
cb
);
auto
qdot
=
p
.
add_instruction
(
migraphx
::
op
::
quant_dot
{
1
,
0
},
qa
,
qb
);
auto
fr
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
qdot
);
std
::
vector
<
float
>
v_alpha
(
fr
->
get_shape
().
elements
(),
20.0
f
);
auto
new_alpha
=
p
.
add_literal
(
migraphx
::
literal
(
fr
->
get_shape
(),
v_alpha
));
auto
alpha_ab
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
new_alpha
,
fr
);
auto
fc
=
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
float_type
},
pc
);
std
::
vector
<
float
>
v_beta
(
fc
->
get_shape
().
elements
(),
5.5
f
);
auto
beta
=
p
.
add_literal
(
migraphx
::
literal
(
fc
->
get_shape
(),
v_beta
));
auto
beta_c
=
p
.
add_instruction
(
migraphx
::
op
::
mul
{},
beta
,
fc
);
auto
f_res
=
p
.
add_instruction
(
migraphx
::
op
::
add
{},
alpha_ab
,
beta_c
);
p
.
add_instruction
(
migraphx
::
op
::
convert
{
migraphx
::
shape
::
int32_type
},
f_res
);
return
p
;
};
auto
p
=
create_program
();
const
std
::
vector
<
std
::
pair
<
float
,
float
>>&
quant_params
{{
0.1
f
,
1.0
f
},
{
0.1
f
,
0.0
f
},
{
0.1
f
,
100.0
f
}};
migraphx
::
quantize_int8
(
p
,
{
"dot"
},
quant_params
);
auto
qp
=
create_int8_quantized_prog
();
EXPECT
(
p
==
qp
);
}
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