-
Notifications
You must be signed in to change notification settings - Fork 68
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
Integrate C++ kernels for 4-bit & 2-bit MatMul #113
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7e3cadd
feat(ungroup): add cpp and python implementation
SunMarc d19cbb6
feat(ungroup): add ungroup bench
SunMarc ea9aee2
feat(PackedTensor): add axis and update unpack function
SunMarc a26bad3
feat(PackedTensor): implement torch.ops.aten.t
SunMarc be5a2fd
feat(QBitsTensor): implement torch.ops.aten.t
SunMarc c967cff
feat(udqmm): add c++ and python implementation
younesbelkada 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 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,19 @@ | ||
#include "udqmm.h" | ||
#include "unpack.h" | ||
#include "ungroup.h" | ||
|
||
#include <iostream> | ||
#include <torch/extension.h> | ||
|
||
using namespace std; | ||
|
||
torch::Tensor udqmm(torch::Tensor &input, torch::Tensor &weights, torch::Tensor &scale, torch::Tensor &zeropoint, int axis, int bits, torch::IntArrayRef orig_shape, torch::IntArrayRef unpacked_shape, int packed_axis) { | ||
TORCH_CHECK(zeropoint.scalar_type() == torch::kInt8, "zeropoint must have scalar type: torch.int8"); | ||
torch::Tensor unpacked_weights = unpack(weights, bits, unpacked_shape, packed_axis); | ||
|
||
torch::Tensor dq_output = (unpacked_weights.to(torch::kInt8) - zeropoint).to(scale.dtype()) * scale; | ||
|
||
torch::Tensor ungrouped_output = ungroup(dq_output, axis, orig_shape); | ||
|
||
return torch::mm(input, ungrouped_output); | ||
} |
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,3 @@ | ||
#include <torch/extension.h> | ||
|
||
torch::Tensor udqmm(torch::Tensor &input, torch::Tensor &weights, torch::Tensor &scale, torch::Tensor &zeropoint, int axis, int bits, torch::IntArrayRef orig_shape, torch::IntArrayRef unpacked_shape, int packed_axis); |
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,22 @@ | ||
#include "ungroup.h" | ||
#include <torch/extension.h> | ||
|
||
torch::Tensor ungroup(torch::Tensor &grouped, int axis, torch::IntArrayRef orig_shape){ | ||
if (grouped.sizes() == orig_shape){ | ||
return grouped; | ||
} | ||
if (axis == 0) { | ||
return torch::reshape(grouped, orig_shape); | ||
} | ||
int64_t group_size = (axis == -1) ? grouped.size(0) : grouped.size(-1); | ||
int64_t axis_dim = (axis == -1) ? orig_shape.back() : orig_shape[axis]; | ||
// Calculate the number of groups per axis | ||
int64_t groups_per_axis = grouped.numel() / axis_dim / group_size; | ||
|
||
torch::Tensor ungrouped = grouped.reshape({group_size, axis_dim, groups_per_axis}); | ||
ungrouped = ungrouped.transpose(1, 2); | ||
ungrouped = ungrouped.transpose(0, 1); | ||
|
||
// Reshape to the original shape | ||
return ungrouped.reshape(orig_shape); | ||
} |
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,3 @@ | ||
#include <torch/extension.h> | ||
|
||
torch::Tensor ungroup(torch::Tensor &grouped, int axis, torch::IntArrayRef orig_shape); |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#include <torch/extension.h> | ||
|
||
torch::Tensor unpack(torch::Tensor &t, int bits); | ||
torch::Tensor unpack(torch::Tensor &t, int bits, torch::IntArrayRef orig_shape, int axis); |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#include <torch/extension.h> | ||
|
||
torch::Tensor unpack(const torch::Tensor &input, int bits); | ||
torch::Tensor unpack(const torch::Tensor &input, int bits, torch::IntArrayRef orig_shape, int axis); |
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
from .mm import * | ||
from .quantize import * | ||
from .udqmm import * | ||
from .ungroup import * | ||
from .unpack import * |
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,20 @@ | ||
import torch | ||
|
||
|
||
@torch.library.impl("quanto_py::udqmm", "default") | ||
def udqmm( | ||
input: torch.Tensor, | ||
weights: torch.Tensor, | ||
scale: torch.Tensor, | ||
zeropoint: torch.Tensor, | ||
axis: int, | ||
bits: int, | ||
orig_shape: torch.Size, | ||
unpacked_shape: torch.Size, | ||
packed_axis: int, | ||
) -> torch.Tensor: | ||
unpacked_weights = torch.ops.quanto.unpack(weights, bits, unpacked_shape, packed_axis) | ||
shifted_weights = unpacked_weights.to(torch.int8) - zeropoint | ||
scaled_weights = shifted_weights.to(scale.dtype) * scale | ||
ungrouped_weights = torch.ops.quanto.ungroup(scaled_weights, axis, orig_shape) | ||
return torch.ops.aten.mm(input, ungrouped_weights) |
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,18 @@ | ||
import torch | ||
|
||
|
||
@torch.library.impl("quanto_py::ungroup", "default") | ||
def ungroup(grouped: torch.Tensor, axis: int, orig_shape: torch.Size) -> torch.Tensor: | ||
if grouped.shape == orig_shape: | ||
return grouped | ||
if axis == 0: | ||
# No transposition required, just reshape | ||
return grouped.reshape(orig_shape) | ||
group_size = grouped.shape[0] if axis == -1 else grouped.shape[-1] | ||
axis_dim = orig_shape[axis] | ||
groups_per_axis = grouped.numel() // axis_dim // group_size | ||
ungrouped = grouped.reshape(group_size, axis_dim, groups_per_axis) | ||
# A dual tranposition is required to reorder to (groups_per_axis, group_size, axis_dim) | ||
ungrouped = ungrouped.transpose(1, 2) | ||
ungrouped = ungrouped.transpose(0, 1) | ||
return ungrouped.reshape(orig_shape) |
Oops, something went wrong.
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.
This is where we will need the other pull-request to support also MPS devices.