diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index 9cdcb87df5dd1669066c204c86c269973df506f1..1c5ded943b3814688af1f177503d3bdc35073c3f 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 c2cf7dd84ea149d345ac7a148de842d7e3965c24..9aac3c7fc16ae1ded2700662764895385b043130 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 5607f11932bbe6aff548be316dc39b4636e079f4..2d78af05ecc05ac06a6c5caf55c0697947ebea57 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 0000000000000000000000000000000000000000..4e1f3d8dc40ba796f8b07b82ae6bb1eee723da33 --- /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 ec8193760bdb95eebb6891d1ff1791e139091511..b04c35082748f88bf33cb4d5fa2f275eb8aefb5e 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 5245b5ea09918d830fcdeb541c65810b139a790e..d4e161ad8ae936496bea5015b7bd03f2d55d5249 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 32447dc11e2f37c18b267a7d13b49718854bc56c..d24417bbacb503d9ea70e68e7e0edb59e7dddbde 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 16199890d66731f4c77cfa82601cb0580310d62e..79557a2cc04ff5cb4c0f974c98c05d512cc2d53f 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", ]