PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
[LLMC] Wrap block for DDP (#1407)
Yi Liu9920441738cc
diff --git a/auto_round/compressors/base.py b/auto_round/compressors/base.pyindex 5c454e099..974a055cd 100644--- a/auto_round/compressors/base.py+++ b/auto_round/compressors/base.py@@ -109,6 +109,7 @@ from auto_round.utils.device import (set_auto_device_map_for_block_with_tuning,set_non_auto_device_map,)+from auto_round.utils.distributed import setup_ddp_if_needed_from auto_round.wrapper import WrapperLinear, WrapperMultiblock, unwrapper_block, unwrapper_layer, wrapper_blockSERIALIZATION_KEYS = (@@ -2957,7 +2958,7 @@ class BaseCompressor(object):if self.gradient_accumulate_steps != 1 and not self.attention_mask:whole_indices = torch.arange(global_batch_size)num_elm = self._get_current_num_elm(input_ids, whole_indices)-+ setup_ddp_if_needed_(self, block, self.device_list)index_sampler = IndexSampler(nsamples, global_batch_size)batch_size = self.batch_sizefor i in range(self.iters):diff --git a/auto_round/utils/distributed.py b/auto_round/utils/distributed.pynew file mode 100644index 000000000..47f12ba29--- /dev/null+++ b/auto_round/utils/distributed.py@@ -0,0 +1,46 @@+# Copyright (c) 2026 Intel Corporation+#+# 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 functools import lru_cache++import torch++from auto_round.logger import logger+++@lru_cache(maxsize=None)+def is_distributed():+ import torch.distributed as dist++ return dist.is_initialized() and dist.get_world_size() > 1+++def setup_ddp_if_needed_(ar, block: torch.nn.Module, device_list: list[int]):+ """Wrap ``block`` with DDP when distributed execution is enabled.++ This experimental hook coordinates AutoRound with the+ LLM-Compressor distributed workflow for DDP quantization.++ Args:+ ar: AutoRound instance invoking the helper.+ block: Model block that may need DDP wrapping.+ device_list: Device identifiers to pass through to DDP.+ """+ if not is_distributed():+ return+ from torch.nn.parallel import DistributedDataParallel as DDP++ logger.warning_once("AutoRound DDP is an experimental feature, please use with caution.")+ logger.trace("[Rank: %d] Using device list: %s", torch.distributed.get_rank(), device_list)+ block = DDP(block, device_ids=device_list, find_unused_parameters=True)