PyTorch Custom Operator Integration#1544
Merged
matthewdouglas merged 35 commits intomainfrom Mar 25, 2025
Merged
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
zou3519
reviewed
Mar 3, 2025
3 tasks
…/bitsandbytes into customop-refactoring
Titus-von-Koeller
approved these changes
Mar 25, 2025
Collaborator
There was a problem hiding this comment.
The custom_ops as well as clean up parts are looking really excellent. Thanks for this impactful work!
As we said, let's do separate PRs for
- torch.compile
- about fixing avoidable issues with graph breaks
- fix issues around torch.compile of quantize functions
- deprecate:
- TestSpMMFunctional tests and mark related functions with @deprecated
- SwitchBackLinear and communicate on issues / repo of open_clip
- fix AttributeError: module 'bitsandbytes.functional' has no attribute 'double_quant'
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Overview
This PR introduces the initial scaffolding to integrate PyTorch Custom Operators as the primary mechanism for dispatching to device-specific operator implementation.
As outlined in the related RFC #1545, the intent is that this will supersede the previous backend registration interface that was developed on the
multi-backend-refactorbranch. The baseline CUDA operators are established in this PR, and the implementation for additional backends is to be ported over to this new interface.Why Custom Ops?
torch.libraryallows us to take advantage of the existing device dispatch mechanisms in PyTorch.torch.compilesupport.Operator Definitions
We broadly categorize operator functionality into three feature groups, though there can be some overlap.
LLM.int8()
Inference requirements
int8_vectorwise_quant(A: Tensor, threshold: float = 0.0) -> (Tensor, Tensor, Tensor?)int8_scaled_mm(A: Tensor, B: Tensor, row_stats: Tensor, col_stats: Tensor, bias: Tensor?, dtype=torch.float16)int8_linear_matmul(A: Tensor, B: Tensor) -> TensorA @ B.Tint8_mm_dequant(A: Tensor, row_stats: Tensor, col_stats: Tensor, dtype=torch.float16, bias: Tensor?) -> Tensortorch.float16for the current CUDA implementation.Optional
int8_vectorwise_dequant(A: Tensor, stats: Tensor)int8_vectorwise_quant.int8_double_quant(A: Tensor, threshold: float = 0.0)int8_vectorwise_quant.NF4/FP4
Minimal requirements
dequantize_4bit(A: Tensor, absmax: Tensor, blocksize: int, quant_type: Literal["nf4" | "fp4"], shape: int[], dtype) -> Tensorbitsandbytes.functional.dequantize_4bit, this operator does not dequantize theabsmaxtensor. If utilized,dequantize_blockwisemust be performed first.quantize_4bit(A: Tensor, blocksize: int, quant_type: Literal["nf4" | "fp4"], quant_storage=torch.uint8) -> (Tensor, Tensor)bitsandbytes.functional.quantize_4bit, this operator does not quantize theabsmaxtensor. If utilized,quantize_blockwisemust be performed first.Double quantization (aka
compressed_statisticsornested)dequantize_blockwise(A: Tensor, absmax: Tensor, code: Tensor, blocksize: int, dtype) -> Tensorquantize_blockwisequantize_blockwise(A: Tensor, code: Tensor, blocksize: int) -> (Tensor, Tensor)code.blocksizewill typically be 256 for usage with NF4/FP4 and optimizers.Optional
gemv_4bitOptimizers
Optimizer functionality will be implemented to support the custom operators in a future update.