Improve streaming, log_level, memory report, weight loading, and benchmark script (#7632)
Co-authored-by: Kan Wu <wukanustc@gmail.com>
This commit is contained in:
@@ -534,6 +534,12 @@ class DummyModelLoader(BaseModelLoader):
|
||||
model_config: ModelConfig,
|
||||
device_config: DeviceConfig,
|
||||
) -> nn.Module:
|
||||
|
||||
if get_bool_env_var("SGL_CPU_QUANTIZATION"):
|
||||
return load_model_with_cpu_quantization(
|
||||
self, model_config=model_config, device_config=device_config
|
||||
)
|
||||
|
||||
with set_default_torch_dtype(model_config.dtype):
|
||||
with torch.device(device_config.device):
|
||||
model = _initialize_model(
|
||||
@@ -1464,6 +1470,38 @@ class RemoteModelLoader(BaseModelLoader):
|
||||
return model.eval()
|
||||
|
||||
|
||||
def load_model_with_cpu_quantization(
|
||||
self,
|
||||
*,
|
||||
model_config: ModelConfig,
|
||||
device_config: DeviceConfig,
|
||||
) -> nn.Module:
|
||||
target_device = torch.device(device_config.device)
|
||||
with set_default_torch_dtype(model_config.dtype):
|
||||
model = _initialize_model(
|
||||
model_config,
|
||||
self.load_config,
|
||||
)
|
||||
|
||||
if not isinstance(self, DummyModelLoader):
|
||||
model.load_weights(self._get_all_weights(model_config, model))
|
||||
|
||||
for _, module in model.named_modules():
|
||||
quant_method = getattr(module, "quant_method", None)
|
||||
if quant_method is not None:
|
||||
# When quant methods need to process weights after loading
|
||||
# (for repacking, quantizing, etc), they expect parameters
|
||||
# to be on the global target device. This scope is for the
|
||||
# case where cpu offloading is used, where we will move the
|
||||
# parameters onto device for processing and back off after.
|
||||
with device_loading_context(module, target_device):
|
||||
quant_method.process_weights_after_loading(module)
|
||||
|
||||
model.to(target_device)
|
||||
|
||||
return model.eval()
|
||||
|
||||
|
||||
def get_model_loader(load_config: LoadConfig) -> BaseModelLoader:
|
||||
"""Get a model loader based on the load format."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user