Skip to content

Commit 1b8047b

Browse files
hshen14luotao1
authored andcommitted
Add INT8 calibration support in Paddle package (#15569)
* Add INT8 calibration support in Paddle package; test=develop
1 parent 312500d commit 1b8047b

File tree

6 files changed

+25
-9
lines changed

6 files changed

+25
-9
lines changed

paddle/fluid/API.spec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ paddle.fluid.contrib.QuantizeTranspiler.__init__ ArgSpec(args=['self', 'weight_b
361361
paddle.fluid.contrib.QuantizeTranspiler.convert_to_int8 ArgSpec(args=['self', 'program', 'place', 'scope'], varargs=None, keywords=None, defaults=(None,))
362362
paddle.fluid.contrib.QuantizeTranspiler.freeze_program ArgSpec(args=['self', 'program', 'place', 'fuse_bn', 'scope'], varargs=None, keywords=None, defaults=(False, None))
363363
paddle.fluid.contrib.QuantizeTranspiler.training_transpile ArgSpec(args=['self', 'program', 'startup_program'], varargs=None, keywords=None, defaults=(None, None))
364+
paddle.fluid.contrib.Calibrator.__init__ ArgSpec(args=['self'], varargs='args', keywords='kwargs', defaults=None)
365+
paddle.fluid.contrib.Calibrator.sample_data ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None)
366+
paddle.fluid.contrib.Calibrator.save_int8_model ArgSpec(args=['self'], varargs=None, keywords=None, defaults=None)
364367
paddle.fluid.contrib.reader.ctr_reader.ctr_reader ArgSpec(args=['feed_dict', 'file_type', 'file_format', 'dense_slot_index', 'sparse_slot_index', 'capacity', 'thread_num', 'batch_size', 'file_list', 'slots', 'name'], varargs=None, keywords=None, defaults=(None,))
365368
paddle.fluid.contrib.build_compressor ArgSpec(args=['place', 'data_reader', 'data_feeder', 'scope', 'metrics', 'epoch', 'config'], varargs=None, keywords=None, defaults=(None, None, None, None, None, None, None))
366369
paddle.fluid.contrib.CompressPass.__init__ ArgSpec(args=['self', 'place', 'data_reader', 'data_feeder', 'scope', 'metrics', 'epoch', 'program_exe'], varargs=None, keywords=None, defaults=(None, None, None, None, None, None, None))

python/paddle/fluid/contrib/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
from .op_frequence import *
2323
from . import quantize
2424
from .quantize import *
25+
from . import int8_inference
26+
from .int8_inference import *
2527
from . import reader
2628
from .reader import *
2729
from . import slim
@@ -34,6 +36,7 @@
3436
__all__ += memory_usage_calc.__all__
3537
__all__ += op_frequence.__all__
3638
__all__ += quantize.__all__
39+
__all__ += int8_inference.__all__
3740
__all__ += reader.__all__
3841
__all__ += slim.__all__
3942
__all__ += utils.__all__

python/paddle/fluid/contrib/int8_inference/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
from __future__ import print_function
16+
17+
from . import utility
18+
from .utility import *
19+
20+
__all__ = utility.__all__

python/paddle/fluid/contrib/int8_inference/utility.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
import paddle.fluid.core as core
14+
15+
from paddle.fluid import core
1516
import numpy as np
1617
import math
1718
import os
18-
import paddle.fluid as fluid
19+
from paddle.fluid.executor import global_scope
20+
from paddle.fluid import io
21+
22+
__all__ = ['Calibrator']
1923

2024

2125
class Calibrator(object):
@@ -76,8 +80,7 @@ def sample_data(self):
7680
'''
7781
for i in self.sampling_program.list_vars():
7882
if i.name in self.sampling_vars:
79-
np_data = np.array(fluid.global_scope().find_var(i.name)
80-
.get_tensor())
83+
np_data = np.array(global_scope().find_var(i.name).get_tensor())
8184
if i.name not in self._sampling_data:
8285
self._sampling_data[i.name] = []
8386
self._sampling_data[i.name].append(np_data)
@@ -86,9 +89,9 @@ def __save_offline_model(self):
8689
'''
8790
Save the quantized model to the disk.
8891
'''
89-
fluid.io.save_inference_model(self.output, self.feed_var_names,
90-
self.fetch_list, self.exe,
91-
self.sampling_program)
92+
io.save_inference_model(self.output, self.feed_var_names,
93+
self.fetch_list, self.exe,
94+
self.sampling_program)
9295

9396
def __display_debug(self):
9497
if self.debug:

python/paddle/fluid/contrib/tests/test_calibration.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
from paddle.dataset.common import download
2525
from PIL import Image, ImageEnhance
2626
import math
27-
sys.path.append('..')
28-
import int8_inference.utility as int8_utility
27+
import paddle.fluid.contrib.int8_inference.utility as int8_utility
2928

3029
random.seed(0)
3130
np.random.seed(0)

python/setup.py.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ packages=['paddle',
109109
'paddle.fluid.contrib',
110110
'paddle.fluid.contrib.decoder',
111111
'paddle.fluid.contrib.quantize',
112+
'paddle.fluid.contrib.int8_inference',
112113
'paddle.fluid.contrib.reader',
113114
'paddle.fluid.contrib.slim',
114115
'paddle.fluid.contrib.slim.core',

0 commit comments

Comments
 (0)