Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat (gptq): optimizing CPU to GPU memory transfer #1009

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/brevitas/graph/gptq.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ def __init__(
# Initialize Hessian matrix and counter. We need it in float32 to compute the inverse
self.H = torch.zeros((self.groups, self.columns, self.columns),
device='cpu',
dtype=torch.float32)
dtype=torch.float32,
pin_memory=torch.cuda.is_available())
self.B = torch.zeros((self.groups, self.columns, self.columns),
device='cpu',
dtype=torch.float32,
pin_memory=torch.cuda.is_available())
self.nsamples = 0

assert torch_version >= version.parse('1.10'), "GPTQ requires torch 1.10 or higher"
Expand Down Expand Up @@ -184,7 +189,9 @@ def update_batch(self, module, input, current_layer):
self.H *= self.nsamples / (self.nsamples + batch_size)
self.nsamples += batch_size
inp_processed = math.sqrt(2 / self.nsamples) * inp_processed.to(torch.float32)
self.H += (inp_processed.bmm(inp_processed.transpose(2, 1))).to(self.H.device)
# optimizing CPU to GPU transfer using in-place copy to pinned memory
self.B.copy_(inp_processed.bmm(inp_processed.transpose(2, 1)))
self.H += self.B
# If we are executing GPTQ with group of parallel layers, we keep track of how many forward
# we executed. Once we executed as many as the number of parallel_layers, we raise
# StopFwdException
Expand Down Expand Up @@ -255,7 +262,7 @@ def single_layer_update(self, percdamp=.01):
f'Increasing the number of samples might fix this issue')
return
finally:
del self.H
del self.H, self.B

for i1 in range(0, self.columns, self.blocksize):
i2 = min(i1 + self.blocksize, self.columns)
Expand Down
Loading