-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat (graph_eq): activation equalization
- Loading branch information
Showing
10 changed files
with
620 additions
and
205 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import torch | ||
|
||
from brevitas.nn.quant_mha import QuantMultiheadAttention | ||
|
||
|
||
class EqualizedModule(torch.nn.Module): | ||
|
||
def __init__(self, scale_module, layer) -> None: | ||
super().__init__() | ||
self.scale = scale_module | ||
self.layer = layer | ||
|
||
def forward(self, x, *args, **kwargs): | ||
args = list(args) | ||
out = x | ||
if 'key' in kwargs: | ||
if kwargs['key'].data_ptr() != out.data_ptr(): | ||
raise ValueError( | ||
"Cross MHA is not supported for activation equalization." | ||
"Replace kwargs with positional args to avoid this exception.") | ||
out = self.scale(out) | ||
|
||
pos_inputs = [out] | ||
# QuantMultiheadAttention is not a subclass of MultiheadAttention | ||
# We need to preserve the correctness of the forward even after | ||
# quantization has been applied | ||
if isinstance(self.layer, (torch.nn.MultiheadAttention, QuantMultiheadAttention)): | ||
if 'key' not in kwargs.items(): | ||
pos_inputs.append(out) | ||
args.pop(0) | ||
else: | ||
kwargs['key'] = out | ||
if 'value' not in kwargs.items(): | ||
pos_inputs.append(out) | ||
args.pop(0) | ||
else: | ||
kwargs['value'] = out | ||
|
||
out = self.layer(*pos_inputs, *args, **kwargs) | ||
return out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.