Skip to content

Commit

Permalink
Call cuda empty_cache to prevent OOM when quantizing model (#2671)
Browse files Browse the repository at this point in the history
* Call cuda empty_cache to prevent OOM when quantizing model

* empty cache during export and after forward
  • Loading branch information
AllentDan authored Oct 31, 2024
1 parent 587c184 commit dde5d23
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lmdeploy/lite/quantization/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,11 @@ def export(self, out_dir):

inp_stats = self.collect_inputs_stats()
torch.save(inp_stats, out_dir / 'inputs_stats.pth')
torch.cuda.empty_cache()

out_stats = self.collect_outputs_stats()
torch.save(out_stats, out_dir / 'outputs_stats.pth')
torch.cuda.empty_cache()

def calibrate(self, data):
"""Forward pass through the model in inference mode with given data."""
Expand All @@ -267,6 +269,7 @@ def calibrate(self, data):
model = self.model.model
with torch.inference_mode():
_ = model(data.to(self.device))
torch.cuda.empty_cache()

def __enter__(self):
"""Prepares the Calibration object for a 'with' statement by
Expand Down Expand Up @@ -440,6 +443,7 @@ def export(self, out_dir):
inputs_stats['absmean'][name] = obs.absmean_val
inputs_stats['ratios'][name] = obs.ratio
torch.save(inputs_stats, out_dir / 'inputs_stats.pth')
torch.cuda.empty_cache()

def _wrap_decoder_layers_for_search(self):
"""Method to wrap the decoder layers' forward functions for observing
Expand Down

0 comments on commit dde5d23

Please sign in to comment.