-
Notifications
You must be signed in to change notification settings - Fork 3
model: [WIP] explicit quantized layer #268
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
Draft
rebel-jongho
wants to merge
9
commits into
main
Choose a base branch
from
model/quantize_layer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d5a1638
initial run
rebel-jongho c907101
a8
rebel-jongho f9af79b
merge main
rebel-jongho afe4986
fix dtype
rebel-jongho 084aa93
remove n_layer if none
rebel-jongho 1784ef6
ruff
rebel-jongho e0c75b6
Merge remote-tracking branch 'origin/main' into model/quantize_layer
rebel-jongho 582785e
add dynamic
rebel-jongho 0fc9f44
add clamp for numerical stability
rebel-jongho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
| from .rbln_quantization import RBLNQuantizationConfig |
This file contains hidden or 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,73 @@ | ||
| from typing import Optional | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
| import torch.nn.functional as F | ||
|
|
||
|
|
||
| class QLinear(nn.Module): | ||
| def __init__( | ||
| self, | ||
| weight: Optional[torch.Tensor] = None, | ||
| bias: Optional[torch.Tensor] = None, | ||
| weight_scale: Optional[torch.Tensor] = None, | ||
| input_scale: Optional[torch.Tensor] = None, | ||
| # FIXME(jongho): Make it only holds k_scale or v_scale | ||
| k_scale: Optional[torch.Tensor] = None, | ||
| v_scale: Optional[torch.Tensor] = None, | ||
| dynamic: bool = False, | ||
| ): | ||
| super().__init__() | ||
|
|
||
| self.weight = weight | ||
| self.bias = bias | ||
| self.weight_scale = weight_scale | ||
| self.input_scale = input_scale | ||
| self.k_scale = k_scale | ||
| self.v_scale = v_scale | ||
| self.dynamic = dynamic | ||
|
|
||
| if weight_scale is None: | ||
| raise ValueError("weight_scale is required") | ||
|
|
||
| def dtype(self) -> torch.dtype: | ||
| return self.weight.dtype | ||
|
|
||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| class QIntLinear(QLinear): | ||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| iinfo = torch.iinfo(self.dtype()) | ||
| finfo = torch.finfo(x.dtype) | ||
| if self.dynamic: | ||
| if self.input_scale: | ||
| raise NotImplementedError("Dynamic quantization with input_scale is not supported.") | ||
| x_max = x.abs().max(dim=-1, keepdim=True).values | ||
| x_scale = x_max / iinfo.max | ||
| x_scale = torch.clamp(x_scale, min=finfo.eps) | ||
|
|
||
| x = (x / x_scale).clamp(min=iinfo.min, max=iinfo.max) | ||
| else: | ||
| if self.input_scale: | ||
| x = (x / self.input_scale).clamp(min=iinfo.min, max=iinfo.max) | ||
|
|
||
| weight = self.weight * self.weight_scale | ||
| qact = F.linear(x, weight, self.bias) | ||
|
|
||
| if self.dynamic: # Dequantize | ||
| qact = qact * x_scale | ||
|
|
||
| return qact | ||
|
|
||
|
|
||
| class QFloatLinear(QLinear): | ||
| def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| if self.input_scale: | ||
| finfo = torch.finfo(self.dtype()) | ||
| x = (x / self.input_scale).clamp(min=finfo.min, max=finfo.max) | ||
|
|
||
| weight = self.weight.to(self.weight_scale.dtype) * self.weight_scale | ||
rebel-jongho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return F.linear(x, weight, self.bias) | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.