-
Notifications
You must be signed in to change notification settings - Fork 313
Fix slice and padding for TensorCoreTiledLayout #2015
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
174cce6
Fix slice and padding for TensorCoreTiledLayout for int4 weight only …
jerryzh168 6c7671a
skip if no cuda
jerryzh168 a07d137
update callsites for post_process
jerryzh168 6da8f15
add back missing post process
jerryzh168 bd34790
adding missing arg for floatx
jerryzh168 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
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
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 |
---|---|---|
|
@@ -192,31 +192,26 @@ def __torch_dispatch__(cls, func, types, args, kwargs): | |
|
||
if func is aten.slice.Tensor: | ||
self, dim, start, end, step = fill_defaults(args, 5, [0, None, None, 1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same for this |
||
if dim == 0: | ||
int_data, scale, zero_point = self.get_plain() | ||
int_data = aten.slice.Tensor(int_data, dim, start, end, step) | ||
# this is to handle padding | ||
int_data = self._layout.post_process(int_data) | ||
sliced = self.from_plain(int_data, scale, zero_point, self._layout) | ||
return return_and_correct_aliasing(func, args, kwargs, sliced) | ||
elif dim == 1: | ||
int_data, scale, zero_point = self.get_plain() | ||
if dim in [0, 1]: | ||
assert step == 1, "Only step == 1 is supported in slicing right now" | ||
int_data, scale, zero_point = self.get_plain() | ||
data_len = int_data.shape[dim] | ||
scale_len = scale.shape[dim] | ||
ratio = data_len / scale_len | ||
start_scale = int(start / ratio) | ||
end_scale = int(end / ratio) | ||
|
||
int_data = aten.slice.Tensor(int_data, dim, start, end, step) | ||
# this is to handle padding | ||
int_data = self._layout.post_process(int_data) | ||
scale = aten.slice.Tensor(scale, dim, start_scale, end_scale, step) | ||
zero_point = aten.slice.Tensor( | ||
zero_point, dim, start_scale, end_scale, step | ||
) | ||
# this is to handle padding | ||
int_data, scale, zero_point = self._layout.post_process( | ||
int_data, scale, zero_point, self.block_size | ||
) | ||
sliced = self.from_plain(int_data, scale, zero_point, self._layout) | ||
return sliced | ||
return return_and_correct_aliasing(func, args, kwargs, sliced) | ||
else: | ||
raise NotImplementedError( | ||
f"Int4CPUAQTTensorImpl dispatch: attempting to run {func}, with dim={dim}, that is not supported" | ||
|
@@ -228,6 +223,18 @@ def __torch_dispatch__(cls, func, types, args, kwargs): | |
|
||
__torch_function__ = torch._C._disabled_torch_function_impl | ||
|
||
@property | ||
def block_size(self): | ||
from torchao.quantization.utils import unpack_tinygemm_scales_and_zeros | ||
|
||
scale, zero = unpack_tinygemm_scales_and_zeros(self.scale_and_zero) | ||
cur_shape = self.shape | ||
assert len(cur_shape) == 4 | ||
inner_k_tiles = cur_shape[-1] * 2 | ||
original_shape = (cur_shape[0] * 8, cur_shape[1] * (inner_k_tiles * 16)) | ||
groupsize = int(original_shape[1] / scale.shape[-2]) | ||
return (1, groupsize) | ||
|
||
def get_plain(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
from torchao.quantization.quant_primitives import ( | ||
ZeroPointDomain, | ||
|
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 |
---|---|---|
|
@@ -153,15 +153,30 @@ def pre_process_static( | |
zero_point = torch.nn.functional.pad(zero_point, padding_changes) | ||
return input, scale, zero_point | ||
|
||
def post_process(self, input: torch.Tensor) -> torch.Tensor: | ||
def post_process( | ||
self, | ||
input: torch.Tensor, | ||
scale: torch.Tensor, | ||
zero_point: torch.Tensor, | ||
block_size: Tuple[int, ...], | ||
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
orig_out_features, orig_in_features = input.shape | ||
in_features = find_multiple(orig_in_features, 1024) | ||
out_features = find_multiple(orig_out_features, 8) | ||
input = torch.nn.functional.pad( | ||
input, | ||
(0, in_features - orig_in_features, 0, out_features - orig_out_features), | ||
) | ||
return input | ||
assert ( | ||
len(block_size) == 2 | ||
), f"TensorCoreTiledLayout only supports len(block_size) == 2, got: {block_size}" | ||
scale_pad_dim_0 = (out_features - orig_out_features) // block_size[0] | ||
scale_pad_dim_1 = (in_features - orig_in_features) // block_size[1] | ||
scale = torch.nn.functional.pad(scale, (0, scale_pad_dim_1, 0, scale_pad_dim_0)) | ||
zero_point = torch.nn.functional.pad( | ||
zero_point, (0, scale_pad_dim_1, 0, scale_pad_dim_0) | ||
) | ||
return input, scale, zero_point | ||
|
||
def extra_repr(self): | ||
return f"inner_k_tiles={self.inner_k_tiles}" | ||
|
@@ -335,31 +350,25 @@ def __torch_dispatch__(cls, func, types, args, kwargs): | |
|
||
if func is aten.slice.Tensor: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also same for this |
||
self, dim, start, end, step = fill_defaults(args, 5, [0, None, None, 1]) | ||
if dim == 0: | ||
int_data, scale, zero_point = self.get_plain() | ||
int_data = aten.slice.Tensor(int_data, dim, start, end, step) | ||
# this is to handle padding | ||
int_data = self._layout.post_process(int_data) | ||
sliced = self.from_plain(int_data, scale, zero_point, self._layout) | ||
return return_and_correct_aliasing(func, args, kwargs, sliced) | ||
elif dim == 1: | ||
if dim in [0, 1]: | ||
int_data, scale, zero_point = self.get_plain() | ||
assert step == 1, "Only step == 1 is supported in slicing right now" | ||
data_len = int_data.shape[dim] | ||
scale_len = scale.shape[dim] | ||
ratio = data_len / scale_len | ||
start_scale = int(start / ratio) | ||
end_scale = int(end / ratio) | ||
|
||
int_data = aten.slice.Tensor(int_data, dim, start, end, step) | ||
# this is to handle padding | ||
int_data = self._layout.post_process(int_data) | ||
scale = aten.slice.Tensor(scale, dim, start_scale, end_scale, step) | ||
zero_point = aten.slice.Tensor( | ||
zero_point, dim, start_scale, end_scale, step | ||
) | ||
# this is to handle padding | ||
int_data, scale, zero_point = self._layout.post_process( | ||
int_data, scale, zero_point, self.block_size | ||
) | ||
sliced = self.from_plain(int_data, scale, zero_point, self._layout) | ||
return sliced | ||
return return_and_correct_aliasing(func, args, kwargs, sliced) | ||
else: | ||
raise NotImplementedError( | ||
f"TensorCoreTiledAQTTensorImpl dispatch: attempting to run {func}, with dim={dim}, that is not supported" | ||
|
@@ -371,6 +380,18 @@ def __torch_dispatch__(cls, func, types, args, kwargs): | |
|
||
__torch_function__ = torch._C._disabled_torch_function_impl | ||
|
||
@property | ||
def block_size(self): | ||
from torchao.quantization.utils import unpack_tinygemm_scales_and_zeros | ||
|
||
scale, zero = unpack_tinygemm_scales_and_zeros(self.scale_and_zero) | ||
cur_shape = self.shape | ||
assert len(cur_shape) == 4 | ||
inner_k_tiles = cur_shape[-1] * 2 | ||
original_shape = (cur_shape[0] * 8, cur_shape[1] * (inner_k_tiles * 16)) | ||
groupsize = int(original_shape[1] / scale.shape[-2]) | ||
return (1, groupsize) | ||
|
||
def get_plain(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
from torchao.quantization.quant_primitives import ( | ||
ZeroPointDomain, | ||
|
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we update this class to use the @dispatch pattern
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, can I do this in a separate PR?