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
965ac6fc
Commit
965ac6fc
authored
Aug 07, 2019
by
Shucai Xiao
Browse files
clang format
parent
47f6954f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
21 deletions
+29
-21
src/quantization.cpp
src/quantization.cpp
+29
-21
No files found.
src/quantization.cpp
View file @
965ac6fc
...
@@ -44,43 +44,46 @@ instruction_ref insert_quant_ins(program& prog,
...
@@ -44,43 +44,46 @@ instruction_ref insert_quant_ins(program& prog,
ins
->
get_shape
().
type
()
==
shape
::
int32_type
);
ins
->
get_shape
().
type
()
==
shape
::
int32_type
);
instruction_ref
quant_ins
{};
instruction_ref
quant_ins
{};
auto
insert_loc
=
std
::
next
(
ins
);
auto
insert_loc
=
std
::
next
(
ins
);
if
(
type
==
shape
::
int8_type
)
if
(
type
==
shape
::
int8_type
)
{
{
auto
scaled_ins
=
ins
;
auto
scaled_ins
=
ins
;
if
(
scale
!=
1.0
f
)
if
(
scale
!=
1.0
f
)
{
{
auto
float_ins
=
scaled_ins
;
auto
float_ins
=
scaled_ins
;
if
(
scaled_ins
->
get_shape
().
type
()
!=
shape
::
float_type
)
if
(
scaled_ins
->
get_shape
().
type
()
!=
shape
::
float_type
)
{
{
float_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
shape
::
float_type
},
scaled_ins
);
float_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
shape
::
float_type
},
scaled_ins
);
}
}
std
::
vector
<
float
>
vec_scale
(
scaled_ins
->
get_shape
().
elements
(),
scale
);
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
(
scaled_ins
->
get_shape
(),
vec_scale
));
scaled_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
mul
{},
l_scale
,
float_ins
);
scaled_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
mul
{},
l_scale
,
float_ins
);
}
}
auto
shifted_ins
=
scaled_ins
;
auto
shifted_ins
=
scaled_ins
;
if
(
shift
!=
0.0
f
)
if
(
shift
!=
0.0
f
)
{
{
auto
float_ins
=
shifted_ins
;
auto
float_ins
=
shifted_ins
;
if
(
shifted_ins
->
get_shape
().
type
()
!=
shape
::
float_type
)
if
(
shifted_ins
->
get_shape
().
type
()
!=
shape
::
float_type
)
{
{
float_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
shape
::
float_type
},
shifted_ins
);
float_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
shape
::
float_type
},
shifted_ins
);
}
}
std
::
vector
<
float
>
vec_shift
(
shifted_ins
->
get_shape
().
elements
(),
shift
);
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
(
shifted_ins
->
get_shape
(),
vec_shift
));
shifted_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
add
{},
l_shift
,
float_ins
);
shifted_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
add
{},
l_shift
,
float_ins
);
}
}
auto
clipped_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
clip
{
127.0
f
,
-
128.0
f
},
shifted_ins
);
auto
clipped_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
clip
{
127.0
f
,
-
128.0
f
},
shifted_ins
);
auto
rounded_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
round
{},
clipped_ins
);
auto
rounded_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
round
{},
clipped_ins
);
quant_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
type
},
rounded_ins
);
quant_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
type
},
rounded_ins
);
}
}
else
else
{
{
quant_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
type
},
ins
);
quant_ins
=
prog
.
insert_instruction
(
insert_loc
,
op
::
convert
{
type
},
ins
);
}
}
map_ins
[
ins
]
=
quant_ins
;
map_ins
[
ins
]
=
quant_ins
;
return
quant_ins
;
return
quant_ins
;
...
@@ -239,12 +242,14 @@ void quantize_int8(program& prog,
...
@@ -239,12 +242,14 @@ void quantize_int8(program& prog,
}
}
else
else
{
{
quant_input
=
insert_quant_ins
(
prog
,
input
,
quant_type
,
map_quant_ins
,
param
.
first
,
param
.
second
);
quant_input
=
insert_quant_ins
(
prog
,
input
,
quant_type
,
map_quant_ins
,
param
.
first
,
param
.
second
);
}
}
}
}
else
else
{
{
quant_input
=
insert_quant_ins
(
prog
,
input
,
quant_type
,
map_quant_ins
,
param
.
first
,
param
.
second
);
quant_input
=
insert_quant_ins
(
prog
,
input
,
quant_type
,
map_quant_ins
,
param
.
first
,
param
.
second
);
}
}
converted_inputs
.
push_back
(
quant_input
);
converted_inputs
.
push_back
(
quant_input
);
}
}
...
@@ -341,25 +346,28 @@ void quantize_int8(program& prog,
...
@@ -341,25 +346,28 @@ void quantize_int8(program& prog,
converted_inputs
);
converted_inputs
);
float
threshold
=
50.0
f
;
float
threshold
=
50.0
f
;
std
::
vector
<
float
>
vec_factor
(
quant_conv
->
get_shape
().
elements
(),
adjust_factor
);
std
::
vector
<
float
>
vec_factor
(
quant_conv
->
get_shape
().
elements
(),
adjust_factor
);
if
(
quant_conv
->
get_shape
().
type
()
==
orig_type
and
adjust_factor
>=
threshold
)
if
(
quant_conv
->
get_shape
().
type
()
==
orig_type
and
adjust_factor
>=
threshold
)
{
{
auto
l_factor
=
prog
.
add_literal
(
literal
(
quant_conv
->
get_shape
(),
vec_factor
.
begin
(),
vec_factor
.
end
()));
auto
l_factor
=
prog
.
add_literal
(
literal
(
quant_conv
->
get_shape
(),
vec_factor
.
begin
(),
vec_factor
.
end
()));
prog
.
replace_instruction
(
ins
,
op
::
mul
{},
quant_conv
,
l_factor
);
prog
.
replace_instruction
(
ins
,
op
::
mul
{},
quant_conv
,
l_factor
);
}
}
// convert quant_conv output to float type, multiply the factor and
// convert quant_conv output to float type, multiply the factor and
// conver back to original type
// conver back to original type
else
else
{
{
auto
float_conv
=
prog
.
insert_instruction
(
ins
,
op
::
convert
{
shape
::
float_type
},
quant_conv
);
auto
float_conv
=
prog
.
insert_instruction
(
ins
,
op
::
convert
{
shape
::
float_type
},
quant_conv
);
auto
l_factor
=
prog
.
add_literal
(
literal
(
float_conv
->
get_shape
(),
vec_factor
));
auto
l_factor
=
prog
.
add_literal
(
literal
(
float_conv
->
get_shape
(),
vec_factor
));
if
(
orig_type
==
shape
::
float_type
)
if
(
orig_type
==
shape
::
float_type
)
{
{
prog
.
replace_instruction
(
ins
,
op
::
mul
{},
l_factor
,
float_conv
);
prog
.
replace_instruction
(
ins
,
op
::
mul
{},
l_factor
,
float_conv
);
}
}
else
else
{
{
auto
adjusted_conv
=
prog
.
insert_instruction
(
ins
,
op
::
mul
{},
l_factor
,
float_conv
);
auto
adjusted_conv
=
prog
.
replace_instruction
(
ins
,
op
::
convert
{
orig_type
},
adjusted_conv
);
prog
.
insert_instruction
(
ins
,
op
::
mul
{},
l_factor
,
float_conv
);
prog
.
replace_instruction
(
ins
,
op
::
convert
{
orig_type
},
adjusted_conv
);
}
}
}
}
}
}
...
...
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