From ee8d1f5ed8abd95232134f0acf8fef6836f84932 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Wed, 25 Aug 2021 09:34:32 +0800 Subject: [PATCH] Fix histogram truncation. (#7181) * Fix truncation. * Lint. * lint. --- python-package/setup.py | 2 +- python-package/xgboost/__init__.py | 2 +- python-package/xgboost/compat.py | 4 ++-- python-package/xgboost/core.py | 3 ++- python-package/xgboost/sklearn.py | 6 +++--- src/tree/gpu_hist/histogram.cuh | 7 ++++--- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/python-package/setup.py b/python-package/setup.py index 8882e1c03..3947b53b3 100644 --- a/python-package/setup.py +++ b/python-package/setup.py @@ -302,7 +302,7 @@ if __name__ == '__main__': with open(os.path.join(CURRENT_DIR, 'README.rst'), encoding='utf-8') as fd: description = fd.read() - with open(os.path.join(CURRENT_DIR, 'xgboost/VERSION')) as fd: + with open(os.path.join(CURRENT_DIR, 'xgboost/VERSION'), encoding="ascii") as fd: version = fd.read().strip() setup(name='xgboost', diff --git a/python-package/xgboost/__init__.py b/python-package/xgboost/__init__.py index 663b5a5a2..7c1078c13 100644 --- a/python-package/xgboost/__init__.py +++ b/python-package/xgboost/__init__.py @@ -22,7 +22,7 @@ except ImportError: pass VERSION_FILE = os.path.join(os.path.dirname(__file__), 'VERSION') -with open(VERSION_FILE) as f: +with open(VERSION_FILE, encoding="ascii") as f: __version__ = f.read().strip() __all__ = ['DMatrix', 'DeviceQuantileDMatrix', 'Booster', 'DataIter', diff --git a/python-package/xgboost/compat.py b/python-package/xgboost/compat.py index cea85c8c6..77480f79c 100644 --- a/python-package/xgboost/compat.py +++ b/python-package/xgboost/compat.py @@ -70,7 +70,7 @@ try: '''Label encoder with JSON serialization methods.''' def to_json(self): '''Returns a JSON compatible dictionary''' - meta = dict() + meta = {} for k, v in self.__dict__.items(): if isinstance(v, np.ndarray): meta[k] = v.tolist() @@ -81,7 +81,7 @@ try: def from_json(self, doc): # pylint: disable=attribute-defined-outside-init '''Load the encoder back from a JSON compatible dict.''' - meta = dict() + meta = {} for k, v in doc.items(): if k == 'classes_': self.classes_ = np.array(v) diff --git a/python-package/xgboost/core.py b/python-package/xgboost/core.py index e25a15e43..0bbfbca62 100644 --- a/python-package/xgboost/core.py +++ b/python-package/xgboost/core.py @@ -2197,7 +2197,8 @@ class Booster(object): """ if isinstance(fout, (STRING_TYPES, os.PathLike)): fout = os.fspath(os.path.expanduser(fout)) - fout = open(fout, 'w') # pylint: disable=consider-using-with + # pylint: disable=consider-using-with + fout = open(fout, 'w', encoding="utf-8") need_close = True else: need_close = False diff --git a/python-package/xgboost/sklearn.py b/python-package/xgboost/sklearn.py index 757778f65..999caae45 100644 --- a/python-package/xgboost/sklearn.py +++ b/python-package/xgboost/sklearn.py @@ -538,7 +538,7 @@ class XGBModel(XGBModelBase): 'importance_type', 'kwargs', 'missing', 'n_estimators', 'use_label_encoder', "enable_categorical" } - filtered = dict() + filtered = {} for k, v in params.items(): if k not in wrapper_specific and not callable(v): filtered[k] = v @@ -557,7 +557,7 @@ class XGBModel(XGBModelBase): return self._estimator_type # pylint: disable=no-member def save_model(self, fname: Union[str, os.PathLike]) -> None: - meta = dict() + meta = {} for k, v in self.__dict__.items(): if k == '_le': meta['_le'] = self._le.to_json() @@ -596,7 +596,7 @@ class XGBModel(XGBModelBase): ) return meta = json.loads(meta_str) - states = dict() + states = {} for k, v in meta.items(): if k == '_le': self._le = XGBoostLabelEncoder() diff --git a/src/tree/gpu_hist/histogram.cuh b/src/tree/gpu_hist/histogram.cuh index 84c79568f..02e63bcad 100644 --- a/src/tree/gpu_hist/histogram.cuh +++ b/src/tree/gpu_hist/histogram.cuh @@ -1,5 +1,5 @@ /*! - * Copyright 2020 by XGBoost Contributors + * Copyright 2020-2021 by XGBoost Contributors */ #ifndef HISTOGRAM_CUH_ #define HISTOGRAM_CUH_ @@ -15,8 +15,9 @@ namespace tree { template GradientSumT CreateRoundingFactor(common::Span gpair); -template -XGBOOST_DEV_INLINE T TruncateWithRoundingFactor(T const rounding_factor, float const x) { +template +XGBOOST_DEV_INLINE T TruncateWithRoundingFactor(T const rounding_factor, U const x) { + static_assert(sizeof(T) >= sizeof(U), "Rounding must have higher or equal precision."); return (rounding_factor + static_cast(x)) - rounding_factor; }