From 993cbbcd5cdbda21ac9a7a09861786e8ed483d0a Mon Sep 17 00:00:00 2001 From: Dang Qingqing Date: Thu, 13 Sep 2018 17:56:22 +0800 Subject: [PATCH] Move quantization transpiler to fluid/contrib/quantize path. --- python/CMakeLists.txt | 1 + python/paddle/fluid/__init__.py | 2 +- python/paddle/fluid/contrib/__init__.py | 2 ++ .../paddle/fluid/contrib/quantize/__init__.py | 19 +++++++++++++++++ .../quantize}/quantize_transpiler.py | 10 +++++---- .../tests}/CMakeLists.txt | 0 .../tests}/test_quantize_transpiler.py | 21 ++++++++++++------- python/paddle/fluid/tests/CMakeLists.txt | 1 - python/paddle/fluid/transpiler/__init__.py | 11 ++++++---- 9 files changed, 50 insertions(+), 17 deletions(-) create mode 100644 python/paddle/fluid/contrib/quantize/__init__.py rename python/paddle/fluid/{transpiler => contrib/quantize}/quantize_transpiler.py (98%) rename python/paddle/fluid/{tests/transpiler => contrib/tests}/CMakeLists.txt (100%) rename python/paddle/fluid/{tests/transpiler => contrib/tests}/test_quantize_transpiler.py (94%) diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 9cdcb87df5d..1c5ded943b3 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -87,6 +87,7 @@ if (WITH_TESTING) endif() endif() add_subdirectory(paddle/fluid/tests) + add_subdirectory(paddle/fluid/contrib/tests) endif() install(DIRECTORY ${PADDLE_PYTHON_PACKAGE_DIR} DESTINATION opt/paddle/share/wheels diff --git a/python/paddle/fluid/__init__.py b/python/paddle/fluid/__init__.py index c2cf7dd84ea..9aac3c7fc16 100644 --- a/python/paddle/fluid/__init__.py +++ b/python/paddle/fluid/__init__.py @@ -47,7 +47,7 @@ from .param_attr import ParamAttr, WeightNormParamAttr from .data_feeder import DataFeeder from .core import LoDTensor, LoDTensorArray, CPUPlace, CUDAPlace, CUDAPinnedPlace, Scope from .transpiler import DistributeTranspiler, InferenceTranspiler, \ - memory_optimize, release_memory, DistributeTranspilerConfig, QuantizeTranspiler + memory_optimize, release_memory, DistributeTranspilerConfig from .lod_tensor import create_lod_tensor, create_random_int_lodtensor from . import clip from . import profiler diff --git a/python/paddle/fluid/contrib/__init__.py b/python/paddle/fluid/contrib/__init__.py index 5607f11932b..2d78af05ecc 100644 --- a/python/paddle/fluid/contrib/__init__.py +++ b/python/paddle/fluid/contrib/__init__.py @@ -18,5 +18,7 @@ from . import decoder from .decoder import * from . import memory_usage_calc from .memory_usage_calc import * +from . import quantize __all__ = decoder.__all__ + memory_usage_calc.__all__ +__all__ += quantize.__all__ diff --git a/python/paddle/fluid/contrib/quantize/__init__.py b/python/paddle/fluid/contrib/quantize/__init__.py new file mode 100644 index 00000000000..4e1f3d8dc40 --- /dev/null +++ b/python/paddle/fluid/contrib/quantize/__init__.py @@ -0,0 +1,19 @@ +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import print_function + +from . import quantize_transpiler + +__all__ = quantize_transpiler.__all__ diff --git a/python/paddle/fluid/transpiler/quantize_transpiler.py b/python/paddle/fluid/contrib/quantize/quantize_transpiler.py similarity index 98% rename from python/paddle/fluid/transpiler/quantize_transpiler.py rename to python/paddle/fluid/contrib/quantize/quantize_transpiler.py index ec8193760bd..b04c3508274 100644 --- a/python/paddle/fluid/transpiler/quantize_transpiler.py +++ b/python/paddle/fluid/contrib/quantize/quantize_transpiler.py @@ -18,14 +18,16 @@ import numpy as np from paddle.fluid.framework import default_main_program, default_startup_program, program_guard from paddle.fluid.layer_helper import LayerHelper from paddle.fluid import unique_name +from paddle.fluid import core from paddle.fluid.initializer import Constant from paddle.fluid.param_attr import ParamAttr from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.layers.nn import autoincreased_step_counter -from .. import core -from ..framework import Variable -from ..executor import global_scope -from inference_transpiler import InferenceTranspiler +from paddle.fluid.framework import Variable +from paddle.fluid.executor import global_scope +from paddle.fluid.transpiler.inference_transpiler import InferenceTranspiler + +__all__ = ['QuantizeTranspiler'] _QUANTIZABLE_OP_TYPES = ['conv2d', 'depthwise_conv2d', 'mul'] diff --git a/python/paddle/fluid/tests/transpiler/CMakeLists.txt b/python/paddle/fluid/contrib/tests/CMakeLists.txt similarity index 100% rename from python/paddle/fluid/tests/transpiler/CMakeLists.txt rename to python/paddle/fluid/contrib/tests/CMakeLists.txt diff --git a/python/paddle/fluid/tests/transpiler/test_quantize_transpiler.py b/python/paddle/fluid/contrib/tests/test_quantize_transpiler.py similarity index 94% rename from python/paddle/fluid/tests/transpiler/test_quantize_transpiler.py rename to python/paddle/fluid/contrib/tests/test_quantize_transpiler.py index 5245b5ea099..d4e161ad8ae 100644 --- a/python/paddle/fluid/tests/transpiler/test_quantize_transpiler.py +++ b/python/paddle/fluid/contrib/tests/test_quantize_transpiler.py @@ -13,17 +13,20 @@ # limitations under the license. import numpy as np +import six + import unittest import paddle import paddle.fluid as fluid -from paddle.fluid.transpiler.quantize_transpiler import _original_var_name +from paddle.fluid.contrib.quantize.quantize_transpiler import _original_var_name +from paddle.fluid.contrib.quantize.quantize_transpiler import QuantizeTranspiler def linear_fc(num): data = fluid.layers.data(name='image', shape=[1, 32, 32], dtype='float32') label = fluid.layers.data(name='label', shape=[1], dtype='int64') hidden = data - for _ in xrange(num): + for _ in six.moves.xrange(num): hidden = fluid.layers.fc(hidden, size=128, act='relu') loss = fluid.layers.cross_entropy(input=hidden, label=label) loss = fluid.layers.mean(loss) @@ -51,7 +54,7 @@ def residual_block(num): data = fluid.layers.data(name='image', shape=[1, 32, 32], dtype='float32') label = fluid.layers.data(name='label', shape=[1], dtype='int64') hidden = data - for _ in xrange(num): + for _ in six.moves.xrange(num): conv = conv_bn_layer(hidden, 16, 3, 1, 1, act=None, bias_attr=True) short = conv_bn_layer(hidden, 16, 1, 1, 0, act=None) hidden = fluid.layers.elementwise_add(x=conv, y=short, act='relu') @@ -142,7 +145,7 @@ class TestQuantizeTranspiler(unittest.TestCase): loss = linear_fc(3) opt = fluid.optimizer.Adam(learning_rate=0.001) opt.minimize(loss) - t = fluid.QuantizeTranspiler(activation_quantize_type=quant_type) + t = QuantizeTranspiler(activation_quantize_type=quant_type) t.training_transpile(main) self.check_program(main) @@ -161,7 +164,7 @@ class TestQuantizeTranspiler(unittest.TestCase): loss = residual_block(2) opt = fluid.optimizer.Adam(learning_rate=0.001) opt.minimize(loss) - t = fluid.QuantizeTranspiler(activation_quantize_type=quant_type) + t = QuantizeTranspiler(activation_quantize_type=quant_type) t.training_transpile(main) self.check_program(main) @@ -176,7 +179,7 @@ class TestQuantizeTranspiler(unittest.TestCase): def freeze_program(self, use_cuda): main = fluid.Program() startup = fluid.Program() - quant_transpiler = fluid.QuantizeTranspiler() + quant_transpiler = QuantizeTranspiler() with fluid.program_guard(main, startup): img = fluid.layers.data( name='image', shape=[1, 28, 28], dtype='float32') @@ -247,7 +250,11 @@ class TestQuantizeTranspiler(unittest.TestCase): self.assertEqual(np.sum(w_8bit), np.sum(w_freeze)) def test_freeze_program_cuda(self): - self.freeze_program(True) + if fluid.core.is_compiled_with_cuda(): + self.freeze_program(True) + + def test_freeze_program_cpu(self): + self.freeze_program(False) if __name__ == '__main__': diff --git a/python/paddle/fluid/tests/CMakeLists.txt b/python/paddle/fluid/tests/CMakeLists.txt index 32447dc11e2..d24417bbacb 100644 --- a/python/paddle/fluid/tests/CMakeLists.txt +++ b/python/paddle/fluid/tests/CMakeLists.txt @@ -8,4 +8,3 @@ endforeach() add_subdirectory(unittests) add_subdirectory(book) add_subdirectory(book_memory_optimization) -add_subdirectory(transpiler) diff --git a/python/paddle/fluid/transpiler/__init__.py b/python/paddle/fluid/transpiler/__init__.py index 16199890d66..79557a2cc04 100644 --- a/python/paddle/fluid/transpiler/__init__.py +++ b/python/paddle/fluid/transpiler/__init__.py @@ -16,12 +16,15 @@ from __future__ import print_function from .distribute_transpiler import DistributeTranspiler, DistributeTranspilerConfig from .inference_transpiler import InferenceTranspiler -from .quantize_transpiler import QuantizeTranspiler from .memory_optimization_transpiler import memory_optimize, release_memory from .ps_dispatcher import HashName, RoundRobin __all__ = [ - "DistributeTranspiler", "InferenceTranspiler", "memory_optimize", - "release_memory", "HashName", "RoundRobin", "DistributeTranspilerConfig", - "QuantizeTranspiler" + "DistributeTranspiler", + "InferenceTranspiler", + "memory_optimize", + "release_memory", + "HashName", + "RoundRobin", + "DistributeTranspilerConfig", ] -- GitLab