diff --git a/.azure-pipelines/scripts/codeScan/pydocstyle/scan_path.txt b/.azure-pipelines/scripts/codeScan/pydocstyle/scan_path.txt index 8eaab8f0b89..317a74f7d2f 100644 --- a/.azure-pipelines/scripts/codeScan/pydocstyle/scan_path.txt +++ b/.azure-pipelines/scripts/codeScan/pydocstyle/scan_path.txt @@ -15,13 +15,14 @@ /neural-compressor/neural_compressor/strategy /neural-compressor/neural_compressor/training.py /neural-compressor/neural_compressor/utils -/neural_compressor/torch/algorithms/mx_quant -/neural-compressor/neural_compressor/torch/algorithms/static_quant +/neural-compressor/neural_compressor/common +/neural-compressor/neural_compressor/tensorflow +/neural-compressor/neural_compressor/torch/algorithms/layer_wise +/neural-compressor/neural_compressor/torch/algorithms/mixed_precision +/neural-compressor/neural_compressor/torch/algorithms/mx_quant +/neural-compressor/neural_compressor/torch/algorithms/pt2e_quant /neural-compressor/neural_compressor/torch/algorithms/smooth_quant -/neural_compressor/torch/algorithms/pt2e_quant -/neural_compressor/torch/export -/neural_compressor/common -/neural_compressor/torch/algorithms/weight_only -/neural_compressor/torch/algorithms/layer_wise -/neural_compressor/torch/algorithms/mixed_precision -/neural_compressor/tensorflow \ No newline at end of file +/neural-compressor/neural_compressor/torch/algorithms/static_quant +/neural-compressor/neural_compressor/torch/algorithms/weight_only +/neural-compressor/neural_compressor/torch/export +/neural-compressor/neural_compressor/torch/utils diff --git a/neural_compressor/common/benchmark.py b/neural_compressor/common/benchmark.py index f732eab5fd8..5c3ba3ee765 100644 --- a/neural_compressor/common/benchmark.py +++ b/neural_compressor/common/benchmark.py @@ -11,6 +11,7 @@ # 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. +"""Benchmark API for Intel Neural Compressor.""" import argparse import os @@ -242,7 +243,9 @@ def get_numa_node(core_list, reversed_numa_info): def set_cores_for_instance(args, numa_info): - """All use cases are listed below: + """Set cores for each instance based on the input args. + + All use cases are listed below: Params: a=num_instance; b=num_cores_per_instance; c=cores; - no a, b, c: a=1, c=numa:0 - no a, b: a=1, c=c @@ -357,6 +360,7 @@ def generate_prefix(args, core_list): Args: args (argparse): arguments for setting different configurations core_list: ["node_index", "cpu_index", num_cpu] + Returns: command_prefix (str): command_prefix with specific core list for Linux or Windows. """ diff --git a/neural_compressor/common/utils/utility.py b/neural_compressor/common/utils/utility.py index 56326246d85..9285864a14c 100644 --- a/neural_compressor/common/utils/utility.py +++ b/neural_compressor/common/utils/utility.py @@ -328,6 +328,8 @@ def wrapper(*args, **kwargs): class ProcessorType(enum.Enum): + """The processor type.""" + Client = "Client" Server = "Server" diff --git a/neural_compressor/torch/algorithms/__init__.py b/neural_compressor/torch/algorithms/__init__.py index 84422b39647..7222c892595 100644 --- a/neural_compressor/torch/algorithms/__init__.py +++ b/neural_compressor/torch/algorithms/__init__.py @@ -11,5 +11,6 @@ # 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. +"""Intel Neural Compressor PyTorch algorithms.""" from .base_algorithm import Quantizer diff --git a/neural_compressor/torch/algorithms/base_algorithm.py b/neural_compressor/torch/algorithms/base_algorithm.py index 48e1b390db4..b40d8074bf6 100644 --- a/neural_compressor/torch/algorithms/base_algorithm.py +++ b/neural_compressor/torch/algorithms/base_algorithm.py @@ -11,6 +11,7 @@ # 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. +"""Intel Neural Compressor PyTorch Quantizer API.""" import copy from abc import ABC, abstractmethod diff --git a/neural_compressor/torch/quantization/__init__.py b/neural_compressor/torch/quantization/__init__.py index f6a015eb89f..86271b44021 100644 --- a/neural_compressor/torch/quantization/__init__.py +++ b/neural_compressor/torch/quantization/__init__.py @@ -11,7 +11,7 @@ # 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. -"""Intel Neural Compressor Pytorch quantization API.""" +"""Intel Neural Compressor PyTorch quantization API.""" from neural_compressor.torch.quantization.quantize import quantize, prepare, convert from neural_compressor.torch.quantization.config import ( diff --git a/neural_compressor/torch/quantization/algorithm_entry.py b/neural_compressor/torch/quantization/algorithm_entry.py index 014e48129e7..3be3643e27f 100644 --- a/neural_compressor/torch/quantization/algorithm_entry.py +++ b/neural_compressor/torch/quantization/algorithm_entry.py @@ -11,6 +11,7 @@ # 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. +"""Intel Neural Compressor PyTorch supported algorithm entries.""" from copy import deepcopy from types import MethodType diff --git a/neural_compressor/torch/quantization/config.py b/neural_compressor/torch/quantization/config.py index 29f944b93e3..19fd1e3e70c 100644 --- a/neural_compressor/torch/quantization/config.py +++ b/neural_compressor/torch/quantization/config.py @@ -266,7 +266,7 @@ def get_model_info(model: torch.nn.Module) -> List[Tuple[str, Callable]]: @classmethod def get_config_set_for_tuning(cls) -> Union[None, "RTNConfig", List["RTNConfig"]]: - """Get the configuration set for tuning. + """Get the default configuration set for tuning. Returns: Union[None, "RTNConfig", List["RTNConfig"]]: The configuration set for tuning. @@ -296,7 +296,7 @@ def get_default_rtn_config(processor_type: Optional[Union[str, torch_utils.Proce Defaults to None. Returns: - RTNConfig: _description_ + RTNConfig: RTNConfig """ process_type = torch_utils.get_processor_type_from_user_config(processor_type) return RTNConfig.get_predefined_configs()[process_type] @@ -483,7 +483,7 @@ def get_model_info(model: torch.nn.Module) -> List[Tuple[str, Callable]]: @classmethod def get_config_set_for_tuning(cls) -> Union[None, "GPTQConfig", List["GPTQConfig"]]: - """Get the configuration set for tuning. + """Get the default configuration set for tuning. Returns: Union[None, "GPTQConfig", List["GPTQConfig"]]: The configuration set for tuning. @@ -493,6 +493,11 @@ def get_config_set_for_tuning(cls) -> Union[None, "GPTQConfig", List["GPTQConfig @classmethod def get_predefined_configs(cls) -> Dict[torch_utils.ProcessorType, "GPTQConfig"]: + """Get the predefined configuration set. + + Returns: + Dict[torch_utils.ProcessorType, "GPTQConfig"]: The configuration of GPTQ. + """ pre_defined_configs: Dict[torch_utils.ProcessorType, GPTQConfig] = {} pre_defined_configs[torch_utils.ProcessorType.Client] = cls( use_layer_wise=True @@ -501,7 +506,16 @@ def get_predefined_configs(cls) -> Dict[torch_utils.ProcessorType, "GPTQConfig"] return pre_defined_configs -def get_default_gptq_config(processor_type: Optional[Union[str, torch_utils.ProcessorType]] = None) -> RTNConfig: +def get_default_gptq_config(processor_type: Optional[Union[str, torch_utils.ProcessorType]] = None) -> GPTQConfig: + """Get the default configuration of GPTQ. + + Args: + processor_type (Optional[Union[str, torch_utils.ProcessorType]], optional): The user-specified processor type. + Defaults to None. + + Returns: + GPTQConfig: GPTQConfig + """ process_type = torch_utils.get_processor_type_from_user_config(processor_type) return GPTQConfig.get_predefined_configs()[process_type] @@ -669,7 +683,7 @@ def get_model_info(model: torch.nn.Module) -> List[Tuple[str, Callable]]: @classmethod def get_config_set_for_tuning(cls) -> Union[None, "AWQConfig", List["AWQConfig"]]: - """Get the configuration set for tuning. + """Get the default configuration set for tuning. Returns: Union[None, "AWQConfig", List["AWQConfig"]]: The configuration set for tuning. @@ -837,7 +851,7 @@ def get_model_info(model: torch.nn.Module) -> List[Tuple[str, Callable]]: @classmethod def get_config_set_for_tuning(cls) -> Union[None, "TEQConfig", List["TEQConfig"]]: - """Get the configuration set for tuning. + """Get the default configuration set for tuning. Returns: Union[None, "TEQConfig", List["TEQConfig"]]: The configuration set for tuning. @@ -1020,7 +1034,7 @@ def get_model_info(model: torch.nn.Module) -> List[Tuple[str, Callable]]: @classmethod def get_config_set_for_tuning(cls) -> Union[None, "AutoRoundConfig", List["AutoRoundConfig"]]: - """Get the configuration set for tuning. + """Get the default configuration set for tuning. Returns: Union[None, "AutoRoundConfig", List["AutoRoundConfig"]]: The configuration set for tuning. @@ -1030,6 +1044,11 @@ def get_config_set_for_tuning(cls) -> Union[None, "AutoRoundConfig", List["AutoR @classmethod def get_predefined_configs(cls) -> Dict[torch_utils.ProcessorType, "AutoRoundConfig"]: + """Get the predefined configuration set. + + Returns: + Dict[torch_utils.ProcessorType, "AutoRoundConfig"]: The configuration of AutoRound. + """ pre_defined_configs: Dict[torch_utils.ProcessorType, AutoRoundConfig] = {} pre_defined_configs[torch_utils.ProcessorType.Client] = cls(use_layer_wise=True) pre_defined_configs[torch_utils.ProcessorType.Server] = cls() @@ -1037,6 +1056,15 @@ def get_predefined_configs(cls) -> Dict[torch_utils.ProcessorType, "AutoRoundCon def get_default_AutoRound_config(processor_type: Optional[Union[str, torch_utils.ProcessorType]] = None) -> RTNConfig: + """Get the default configuration of AutoRound. + + Args: + processor_type (Optional[Union[str, torch_utils.ProcessorType]], optional): The user-specified processor type. + Defaults to None. + + Returns: + AutoRoundConfig: AutoRoundConfig + """ process_type = torch_utils.get_processor_type_from_user_config(processor_type) return AutoRoundConfig.get_predefined_configs()[process_type] @@ -1076,6 +1104,8 @@ def __init__( blocksize (int): Granularity to share the scale, default is 32. round_method (str): Round method, default is "nearest". weight_only (bool): Whether implement weight_only, default is False. + white_list (Optional[List[OP_NAME_OR_MODULE_TYPE]]): White list of operator names or module types. + Default is DEFAULT_WHITE_LIST. """ super().__init__(white_list=white_list) self.w_dtype = w_dtype @@ -1088,6 +1118,7 @@ def __init__( @classmethod def register_supported_configs(cls) -> List[OperatorConfig]: + """Register supported configurations.""" supported_configs = [] linear_mx_config = MXQuantConfig( w_dtype=[ @@ -1127,6 +1158,14 @@ def register_supported_configs(cls) -> List[OperatorConfig]: @staticmethod def get_model_info(model: torch.nn.Module) -> List[Tuple[str, Callable]]: + """Get information about the model. + + Args: + model (torch.nn.Module): The model. + + Returns: + List[Tuple[str, Callable]]: List of tuples containing the name and type of each module in the model. + """ white_list = ( torch.nn.Linear, torch.nn.functional.linear, @@ -1142,6 +1181,7 @@ def get_model_info(model: torch.nn.Module) -> List[Tuple[str, Callable]]: @classmethod def get_config_set_for_tuning(cls) -> Union[None, "MXQuantConfig", List["MXQuantConfig"]]: + """Get the default configuration set for tuning.""" return MXQuantConfig(weight_only=[False, True]) @@ -1198,6 +1238,7 @@ def __init__( @classmethod def register_supported_configs(cls) -> List[OperatorConfig]: + """Register supported configurations.""" supported_configs = [] linear_static_config = cls() operators = [torch.nn.Linear] @@ -1206,16 +1247,35 @@ def register_supported_configs(cls) -> List[OperatorConfig]: @staticmethod def get_model_info(model: torch.nn.Module, example_inputs=None): + """Get information about the model. + + Args: + model (torch.nn.Module): The model. + + Returns: + List[Tuple[str, Callable]]: List of tuples containing the name and type of each module in the model. + """ return None def to_config_mapping( self, config_list: List[BaseConfig] = None, model_info: List[Tuple[str, str]] = None ) -> OrderedDictType[Union[str, str], OrderedDictType[str, BaseConfig]]: + """Convert the configuration to a mapping. + + Args: + config_list (List[BaseConfig]): List of base configurations. Default is None. + model_info (List[Tuple[str, str]]): List of tuples containing the name and type of each module in the model. + Default is None. + + Returns: + OrderedDictType[Union[str, str], OrderedDictType[str, BaseConfig]]: The configuration mapping. + """ config_mapping = OrderedDict({self.name: self}) return config_mapping @classmethod def get_config_set_for_tuning(cls) -> Union[None, "DynamicQuantConfig", List["DynamicQuantConfig"]]: + """Get the default configuration set for tuning.""" return cls(act_sym=[True, False], act_algo=["kl", "minmax"]) @@ -1273,6 +1333,8 @@ def __init__( act_granularity (str): Level of quantization granularity for activations, default is "per_channel". act_algo (str): Quatization algorithm used to compute parameters for activations, default is "minmax". excluded_precisions (list): Precisions to be excluded, Default value is empty list. + white_list (Optional[List[OP_NAME_OR_MODULE_TYPE]]): White list of operator names or module types. + Default is DEFAULT_WHITE_LIST. """ super().__init__(white_list=white_list) self.w_dtype = w_dtype @@ -1289,6 +1351,7 @@ def __init__( @classmethod def register_supported_configs(cls) -> List[OperatorConfig]: + """Register supported configurations.""" supported_configs = [] linear_static_config = StaticQuantConfig() operators = [torch.nn.Linear] @@ -1297,12 +1360,28 @@ def register_supported_configs(cls) -> List[OperatorConfig]: @staticmethod def get_model_info_for_ipex(model: torch.nn.Module, example_inputs) -> List[Tuple[str, Callable]]: + """Get information about the model. + + Args: + model (torch.nn.Module): The model. + + Returns: + List[Tuple[str, Callable]]: List of tuples containing the name and type of each module in the model. + """ from neural_compressor.torch.algorithms.static_quant import get_quantizable_ops_recursively _, _, _, _, model_info = get_quantizable_ops_recursively(model, example_inputs=example_inputs) return model_info def get_model_info_for_ipex_xpu(self, model: torch.nn.Module) -> List[Tuple[str, Callable]]: # pragma: no cover + """Get information about the model. + + Args: + model (torch.nn.Module): The model. + + Returns: + List[Tuple[str, Callable]]: List of tuples containing the name and type of each module in the model. + """ if self.model_info: return self.model_info else: @@ -1317,6 +1396,14 @@ def get_model_info_for_ipex_xpu(self, model: torch.nn.Module) -> List[Tuple[str, return filter_result def get_model_info(self, model: torch.nn.Module, example_inputs=None) -> List[Tuple[str, Callable]]: + """Get information about the model. + + Args: + model (torch.nn.Module): The model. + + Returns: + List[Tuple[str, Callable]]: List of tuples containing the name and type of each module in the model. + """ from neural_compressor.torch.utils.auto_accelerator import auto_detect_accelerator if is_ipex_imported(): @@ -1328,6 +1415,16 @@ def get_model_info(self, model: torch.nn.Module, example_inputs=None) -> List[Tu def to_config_mapping( self, config_list: List[BaseConfig] = None, model_info: List[Tuple[str, str]] = None ) -> OrderedDictType[Union[str, str], OrderedDictType[str, BaseConfig]]: + """Convert the configuration to a mapping. + + Args: + config_list (List[BaseConfig]): List of base configurations. Default is None. + model_info (List[Tuple[str, str]]): List of tuples containing the name and type of each module in the model. + Default is None. + + Returns: + OrderedDictType[Union[str, str], OrderedDictType[str, BaseConfig]]: The configuration mapping. + """ if is_ipex_imported(): return super().to_config_mapping(config_list, model_info) config_mapping = OrderedDict({self.name: self}) @@ -1335,6 +1432,7 @@ def to_config_mapping( @classmethod def get_config_set_for_tuning(cls) -> Union[None, "StaticQuantConfig", List["StaticQuantConfig"]]: + """Get the default configuration set for tuning.""" return StaticQuantConfig(act_sym=[True, False], act_algo=["kl", "minmax"]) @@ -1417,6 +1515,9 @@ def __init__( alpha_step (float): Step_size of auto-tuning alpha search space, default is 0.1. shared_criterion (str): Criterion for input LayerNorm op of a transformer block, default is "max". do_blockwise (bool): Whether to enable block-wise auto-tuning, default is False. + auto_alpha_args (bool): Arguments for auto alpha searching, default is None. + white_list (Optional[List[OP_NAME_OR_MODULE_TYPE]]): White list of operator names or module types. + Default is DEFAULT_WHITE_LIST. """ super().__init__(white_list=white_list) self.w_dtype = w_dtype @@ -1450,6 +1551,7 @@ def __init__( @classmethod def register_supported_configs(cls) -> List[OperatorConfig]: + """Register supported configurations.""" supported_configs = [] # TODO(Yi) linear_sq_config = SmoothQuantConfig() @@ -1458,6 +1560,14 @@ def register_supported_configs(cls) -> List[OperatorConfig]: cls.supported_configs = supported_configs def get_model_info(self, model: torch.nn.Module, example_inputs) -> List[Tuple[str, Callable]]: + """Get information about the model. + + Args: + model (torch.nn.Module): The model. + + Returns: + List[Tuple[str, Callable]]: List of tuples containing the name and type of each module in the model. + """ from neural_compressor.torch.algorithms.smooth_quant import get_quantizable_ops_recursively model_info, cfgs, op_infos_from_cfgs, output_tensor_id_op_name = get_quantizable_ops_recursively( @@ -1472,6 +1582,7 @@ def get_model_info(self, model: torch.nn.Module, example_inputs) -> List[Tuple[s @classmethod def get_config_set_for_tuning(cls) -> Union[None, "SmoothQuantConfig", List["SmoothQuantConfig"]]: + """Get the default configuration set for tuning.""" return SmoothQuantConfig(alpha=[0.1, 0.5], folding=[True, False], scale_sharing=[True, False]) @@ -1746,7 +1857,14 @@ def register_supported_configs(cls) -> List[OperatorConfig]: @staticmethod def get_model_info(model: torch.nn.Module) -> List[Tuple[str, Callable]]: - """Get concrete node names for supported operators.""" + """Get information about the model. + + Args: + model (torch.nn.Module): The model. + + Returns: + List[Tuple[str, Callable]]: List of tuples containing the name and type of each module in the model. + """ white_list = tuple(MixedPrecisionConfig.supported_half_precision_ops) filter_result = [] for op_name, module in model.named_modules(): @@ -1787,6 +1905,7 @@ def get_default_mixed_precision_config_set() -> MixedPrecisionConfig: def get_all_registered_configs() -> Dict[str, BaseConfig]: + """Get all registered configs.""" registered_configs = config_registry.get_all_configs() return registered_configs.get(FRAMEWORK_NAME, {}) diff --git a/neural_compressor/torch/quantization/load_entry.py b/neural_compressor/torch/quantization/load_entry.py index 7cc7f8075d0..1afd87a3e4a 100644 --- a/neural_compressor/torch/quantization/load_entry.py +++ b/neural_compressor/torch/quantization/load_entry.py @@ -11,6 +11,7 @@ # 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. +"""Intel Neural Compressor PyTorch load entry for all algorithms.""" import json import os @@ -54,7 +55,6 @@ def load(model_name_or_path, original_model=None, format="default", device="cpu" from neural_compressor.torch.quantization import load load(model_name_or_path=model_name_or_path) - Args: model_name_or_path (str): torch checkpoint directory or hugginface model_name_or_path. If 'format' is set to 'huggingface', it means the huggingface model_name_or_path. @@ -70,6 +70,7 @@ def load(model_name_or_path, original_model=None, format="default", device="cpu" kwargs (remaining dictionary of keyword arguments, optional): remaining dictionary of keyword arguments for loading huggingface models. Will be passed to the huggingface model's `__init__` method, such as 'trust_remote_code', 'revision'. + Returns: The quantized model """ diff --git a/neural_compressor/torch/quantization/quantize.py b/neural_compressor/torch/quantization/quantize.py index 85e73d47078..0fb1251891b 100644 --- a/neural_compressor/torch/quantization/quantize.py +++ b/neural_compressor/torch/quantization/quantize.py @@ -28,6 +28,15 @@ def need_apply(configs_mapping: Dict[Tuple[str, callable], BaseConfig], algo_name): + """Check whether to apply this algorithm according to configs_mapping. + + Args: + configs_mapping (Dict[Tuple[str, callable], BaseConfig]): configs mapping + algo_name (str): algo name + + Returns: + Bool: True or False. + """ return any(config.name == algo_name for config in configs_mapping.values()) diff --git a/neural_compressor/torch/utils/__init__.py b/neural_compressor/torch/utils/__init__.py index dab02a017c6..6d69121251b 100644 --- a/neural_compressor/torch/utils/__init__.py +++ b/neural_compressor/torch/utils/__init__.py @@ -11,6 +11,7 @@ # 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. +"""Intel Neural Compressor PyTorch related utility modules.""" from .environ import * from .constants import * diff --git a/neural_compressor/torch/utils/auto_accelerator.py b/neural_compressor/torch/utils/auto_accelerator.py index be7dd7f842f..b72cffff5ad 100644 --- a/neural_compressor/torch/utils/auto_accelerator.py +++ b/neural_compressor/torch/utils/auto_accelerator.py @@ -19,6 +19,7 @@ # NOTICE: The design adapted from: # https://github.com/microsoft/DeepSpeed/blob/master/accelerator/abstract_accelerator.py. +"""Auto Accelerator Module.""" # To keep it simply, only add the APIs we need. @@ -40,6 +41,8 @@ class AcceleratorRegistry: + """Accelerator Registry.""" + registered_accelerators = {} @classmethod @@ -94,171 +97,253 @@ class CUDA_Accelerator: name: the accelerator name. priority: the priority of the accelerator. A larger number indicates a higher priority, """ - return accelerator_registry.register_accelerator_impl(name=name, priority=priority) class Auto_Accelerator(ABC): # pragma: no cover + """Auto Accelerator Base class.""" + @classmethod @abstractmethod def is_available(cls) -> bool: + """Check if the accelerator is available.""" pass @abstractmethod def name(self) -> str: + """Get the accelerator name.""" pass @abstractmethod def device_name(self, device_indx) -> str: + """Get the device name.""" pass @abstractmethod def set_device(self, device_index): + """Set the device.""" pass @abstractmethod def current_device(self): + """Get the current device.""" pass @abstractmethod def current_device_name(self): + """Get the current device name.""" pass @abstractmethod def device(self, device_index=None): + """Get the device.""" pass @abstractmethod def empty_cache(self): + """Empty the cache.""" pass @abstractmethod def synchronize(self): + """Synchronize the accelerator.""" pass def mark_step(self): + """Trigger graph to run.""" pass @register_accelerator(name="cpu", priority=PRIORITY_CPU) class CPU_Accelerator(Auto_Accelerator): + """CPU Accelerator.""" + def __init__(self) -> None: + """Initialize CPU Accelerator.""" self._name = "cpu" def name(self) -> str: + """Get the accelerator name.""" return self._name @classmethod def is_available(cls) -> bool: + """Always return True.""" return True def device_name(self, device_indx) -> str: + """Get the device name.""" return "cpu" def set_device(self, device_index): + """Do nothing.""" pass def current_device(self): + """Get the current device.""" return "cpu" def current_device_name(self): + """Get the current device name.""" return "cpu" def device(self, device_index=None): + """Do nothing.""" pass def empty_cache(self): + """Do nothing.""" pass def synchronize(self): + """Do nothing.""" pass @register_accelerator(name="cuda", priority=PRIORITY_CUDA) class CUDA_Accelerator(Auto_Accelerator): # pragma: no cover + """CUDA Accelerator.""" + def __init__(self) -> None: + """Initialize CUDA Accelerator.""" self._name = "cuda" def name(self) -> str: + """Get the accelerator name.""" return self._name @classmethod def is_available(cls) -> bool: + """Check if the 'cuda' device is available.""" return torch.cuda.is_available() def device_name(self, device_indx) -> str: + """Returns the name of the 'cuda' device with the given index.""" if device_indx is None: return "cuda" return f"cuda:{device_indx}" def synchronize(self): + """Synchronizes the 'cuda' device.""" return torch.cuda.synchronize() def set_device(self, device_index): + """Sets the current 'cuda' device to the one with the given index.""" return torch.cuda.set_device(device_index) def current_device(self): + """Returns the index of the current 'cuda' device.""" return torch.cuda.current_device() def current_device_name(self): + """Returns the name of the current 'cuda' device.""" return "cuda:{}".format(torch.cuda.current_device()) def device(self, device_index=None): + """Returns a torch.device object for the 'cuda' device with the given index.""" return torch.cuda.device(device_index) def empty_cache(self): + """Empties the cuda cache.""" return torch.cuda.empty_cache() @register_accelerator(name="xpu", priority=PRIORITY_XPU) class XPU_Accelerator(Auto_Accelerator): # pragma: no cover + """XPU Accelerator.""" + def __init__(self) -> None: + """Initialize XPU Accelerator.""" self._name = "xpu" def name(self) -> str: + """Get the accelerator name.""" return self._name @classmethod def is_available(cls) -> bool: + """Checks if the 'xpu' device is available. + + Returns: + bool: True if the 'xpu' device is available, False otherwise. + """ if hasattr(torch, "xpu") and torch.xpu.is_available(): return True else: return False def device_name(self, device_indx) -> str: + """Returns the name of the 'xpu' device with the given index. + + Args: + device_indx (int): The index of the 'xpu' device. + + Returns: + str: The name of the 'xpu' device. + """ if device_indx is None: return "xpu" return f"xpu:{device_indx}" def synchronize(self): + """Synchronizes the 'xpu' device.""" return torch.xpu.synchronize() def set_device(self, device_index): + """Sets the current 'xpu' device to the one with the given index. + + Args: + device_index (int): The index of the 'xpu' device. + """ return torch.xpu.set_device(device_index) def current_device(self): + """Returns the index of the current 'xpu' device. + + Returns: + int: The index of the current 'xpu' device. + """ return torch.xpu.current_device() def current_device_name(self): + """Returns the name of the current 'xpu' device. + + Returns: + str: The name of the current 'xpu' device. + """ return "xpu:{}".format(torch.xpu.current_device()) def device(self, device_index=None): + """Returns a torch.device object for the 'xpu' device with the given index. + + Args: + device_index (int, optional): The index of the 'xpu' device. Defaults to None. + + Returns: + torch.device: The torch.device object for the 'xpu' device. + """ return torch.xpu.device(device_index) def empty_cache(self): + """Empties the xpu cache.""" return torch.xpu.empty_cache() @register_accelerator(name="hpu", priority=PRIORITY_HPU) class HPU_Accelerator(Auto_Accelerator): # pragma: no cover + """HPU Accelerator.""" + def __init__(self) -> None: + """Initialize HPU Accelerator.""" self._name = "hpu" def name(self) -> str: + """Get the accelerator name.""" return self._name @classmethod def is_available(cls) -> bool: + """Checks if the 'hpu' device is available.""" from .environ import is_hpex_available if is_hpex_available(): @@ -267,43 +352,54 @@ def is_available(cls) -> bool: return False def device_name(self, device_indx) -> str: + """Returns the name of the 'hpu' device with the given index.""" if device_indx is None: return "hpu" return f"hpu:{device_indx}" def synchronize(self): + """Synchronizes the 'hpu' device.""" return torch.hpu.synchronize() def set_device(self, device_index): + """Sets the current 'hpu' device to the one with the given index.""" try: torch.hpu.set_device(device_index) except Exception as e: logger.warning(e) def current_device(self): + """Returns the index of the current 'hpu' device.""" return torch.hpu.current_device() def current_device_name(self): + """Returns the name of the current 'hpu' device.""" return "hpu:{}".format(torch.hpu.current_device()) def device(self, device_index=None): + """Returns a torch.device object for the 'hpu' device with the given index.""" return torch.hpu.device(device_index) def empty_cache(self): + """Empties the hpu cache.""" try: torch.hpu.empty_cache() except Exception as e: logger.warning(e) def mark_step(self): + """Trigger graph to run.""" return htcore.mark_step() def auto_detect_accelerator(device_name="auto") -> Auto_Accelerator: - # Force use the cpu on node has both cpu and gpu: `FORCE_DEVICE=cpu` python main.py ... - # The `FORCE_DEVICE` is case insensitive. - # The environment variable `FORCE_DEVICE` has higher priority than the `device_name`. - # TODO: refine the docs and logic later + """Automatically detects and selects the appropriate accelerator. + + Force use the cpu on node has both cpu and gpu: `FORCE_DEVICE=cpu` python main.py ... + The `FORCE_DEVICE` is case insensitive. + The environment variable `FORCE_DEVICE` has higher priority than the `device_name`. + TODO: refine the docs and logic later + """ # 1. Get the device setting from environment variable `FORCE_DEVICE`. FORCE_DEVICE = os.environ.get("FORCE_DEVICE", None) if FORCE_DEVICE: diff --git a/neural_compressor/torch/utils/constants.py b/neural_compressor/torch/utils/constants.py index dee05af4088..f75dff0bb51 100644 --- a/neural_compressor/torch/utils/constants.py +++ b/neural_compressor/torch/utils/constants.py @@ -12,10 +12,9 @@ # 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. - +"""Intel Neural Compressor PyTorch constants.""" # double quant params - DOUBLE_QUANT_CONFIGS = { "BNB_NF4": { "dtype": "nf4", @@ -60,6 +59,8 @@ class LoadFormat(Enum): + """Load API formats for different usages.""" + DEFAULT = "default" HUGGINGFACE = "huggingface" diff --git a/neural_compressor/torch/utils/environ.py b/neural_compressor/torch/utils/environ.py index e6703a0b8fd..aa6b9affba0 100644 --- a/neural_compressor/torch/utils/environ.py +++ b/neural_compressor/torch/utils/environ.py @@ -12,6 +12,7 @@ # 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. +"""Intel Neural Compressor PyTorch environment check.""" import importlib import sys diff --git a/neural_compressor/torch/utils/utility.py b/neural_compressor/torch/utils/utility.py index b210288c1e7..58b84262aa2 100644 --- a/neural_compressor/torch/utils/utility.py +++ b/neural_compressor/torch/utils/utility.py @@ -11,6 +11,7 @@ # 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. +"""Intel Neural Compressor PyTorch utilities.""" import enum @@ -185,13 +186,11 @@ def postprocess_model(model, mode, quantizer): def dump_model_op_stats(mode, tune_cfg): - """This is a function to dump quantizable ops of model to user. + """Dump quantizable ops stats of model to user. Args: - model (object): input model + mode (object): quantization mode. tune_cfg (dict): quantization config - Returns: - None """ if mode == Mode.PREPARE: return