From 6d490cc4b6c9882d5bdadbef050eb5df7a8e468b Mon Sep 17 00:00:00 2001 From: Guanghua Yu <742925032@qq.com> Date: Fri, 19 Aug 2022 10:32:49 +0800 Subject: [PATCH] lazy import smac (#1361) --- paddleslim/quant/__init__.py | 4 ++-- paddleslim/quant/post_quant_hpo.py | 20 +++++++++++++------- requirements.txt | 1 - 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/paddleslim/quant/__init__.py b/paddleslim/quant/__init__.py index 8b472e72..69e54296 100644 --- a/paddleslim/quant/__init__.py +++ b/paddleslim/quant/__init__.py @@ -35,7 +35,7 @@ try: except Exception as e: _logger.warning(e) _logger.warning( - f"If you want to use training-aware and post-training quantization, " - "please use Paddle >= {min_paddle_version} or develop version") + "If you want to use training-aware and post-training quantization, " + "please use Paddle >= {} or develop version".format(min_paddle_version)) from .quant_embedding import quant_embedding diff --git a/paddleslim/quant/post_quant_hpo.py b/paddleslim/quant/post_quant_hpo.py index e6bc1d9f..dc41e48d 100755 --- a/paddleslim/quant/post_quant_hpo.py +++ b/paddleslim/quant/post_quant_hpo.py @@ -29,13 +29,7 @@ import shutil import glob from scipy.stats import wasserstein_distance -# smac -from ConfigSpace.hyperparameters import CategoricalHyperparameter, \ - UniformFloatHyperparameter, UniformIntegerHyperparameter -from smac.configspace import ConfigurationSpace -from smac.facade.smac_hpo_facade import SMAC4HPO -from smac.scenario.scenario import Scenario - +import pkg_resources as pkg from paddleslim.common import get_logger from paddleslim.quant import quant_post @@ -417,6 +411,18 @@ def quant_post_hpo( None """ + try: + pkg.require('smac') + except: + from pip._internal import main + main(['install', 'smac']) + # smac + from ConfigSpace.hyperparameters import CategoricalHyperparameter, \ + UniformFloatHyperparameter, UniformIntegerHyperparameter + from smac.configspace import ConfigurationSpace + from smac.facade.smac_hpo_facade import SMAC4HPO + from smac.scenario.scenario import Scenario + global g_quant_config g_quant_config = QuantConfig( executor, place, model_dir, quantize_model_path, algo, hist_percent, diff --git a/requirements.txt b/requirements.txt index 05435ec1..d558b221 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,3 @@ matplotlib pillow pyyaml scikit-learn -smac -- GitLab