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 (brevitas/bias_correction): Add zero-valued bias to linear layers when accelerate is enabled. #139

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions optimum/amd/brevitas/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,8 @@ def __post_init__(self):
self.activations_group_size = None
self.activations_param_method = None

def add_bias_to_linear(self):
return self.apply_bias_correction and self.device == "auto"

def requires_fx_graph(self):
return self.activations_equalization == "cross_layer" or self.apply_weight_equalization
21 changes: 19 additions & 2 deletions optimum/amd/brevitas/quantizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ def quantize(
if use_accelerate:
remove_hooks(model)
device = None
if quantization_config.add_bias_to_linear():
model = add_zero_bias_to_linear(model)
else:
device = next(model.parameters()).device

Expand Down Expand Up @@ -244,6 +246,7 @@ def quantize(
apply_bias_correction(
model,
calibration_dataset,
skip_if_no_bias=use_accelerate, # We can't add keys to the state dict if accelerate is being used
)
logger.info("Bias Correction applied.")

Expand Down Expand Up @@ -331,7 +334,21 @@ def apply_calibration(model: torch.nn.Module, dataset: List[Dict]) -> None:


@torch.no_grad()
def apply_bias_correction(model: torch.nn.Module, dataset: List[Dict]) -> None:
with bias_correction_mode(model):
def apply_bias_correction(model: torch.nn.Module, dataset: List[Dict], skip_if_no_bias: bool = False) -> None:
with bias_correction_mode(model, skip_if_no_bias=skip_if_no_bias):
for inps in tqdm(dataset):
model(**inps)


@torch.no_grad()
def add_zero_bias_to_linear(model: torch.nn.Module) -> torch.nn.Module:
for name, module in model.named_modules():
if type(module) == torch.nn.Linear:
if module.bias is None:
module.register_parameter(
"bias",
torch.nn.Parameter(
torch.zeros((module.weight.shape[0],), device=module.weight.device, dtype=module.weight.dtype)
),
)
return model
Loading