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

Fix (graph/bias_correction): Fix when layer parameters are offloaded to accelerate #962

Merged
merged 4 commits into from
Jul 8, 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
7 changes: 7 additions & 0 deletions src/brevitas/graph/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,18 @@ def apply_correction(self, model):
for name, module in model.named_modules():
if name in self.correction_map.keys():
correction = self.correction_map[name] / self.iterations[name]
# When accelerate is enabled, bring tensors onto the device to avoid allocating a meta parameter.
if hasattr(module, 'allocate_params'):
module.allocate_params(module)
if module.bias is not None:
module.bias.data += correction
elif self.skip_if_no_bias is False:
# If accelerate is enabled, bias will be on the same execution device as the weights, but won't be managed properly by accelerate
module.register_parameter(
'bias', nn.Parameter(correction).to(module.weight.device))
# Offload params again
if hasattr(module, 'offload_params'):
module.offload_params(module)

def compute_correct_bias(self, module, inp, name):
inp = self.unpack_input(inp)
Expand Down
Loading