-
Notifications
You must be signed in to change notification settings - Fork 672
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
PyTorch Custom Operator Integration #1544
base: main
Are you sure you want to change the base?
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. |
torch.library.define( | ||
"bitsandbytes::int8_linear_dequant", | ||
"(Tensor A, Tensor B, Tensor row_stats, Tensor col_stats, Tensor? bias=None, ScalarType dtype=float16) -> Tensor", | ||
) |
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.
Hey! I'm the main maintainer of custom operators in PyTorch. I'm curious -- why not use the torch.library.custom_op API instead of torch.library.define?
It would look something like:
@torch.library.custom_op("bitsandbytes::int8_linear_dequant", mutates_args=())
def int8_linear_dequant(A: Tensor, B: Tensor, row_stats: Tensor, col_stats: Tensor, bias: Optional[Tensor], dtype: torch.dtype) -> Tensor:
raise NotImplementedError("")
@int8_linear_dequant.register_fake
def _(
A: torch.Tensor,
B: torch.Tensor,
row_stats: torch.Tensor,
col_stats: torch.Tensor,
bias: Optional[torch.Tensor] = None,
dtype=torch.float16,
) -> torch.Tensor:
shapeC = (*A.shape[:-1], B.shape[0])
return torch.empty(shapeC, device=A.device, dtype=dtype)
We generally encourage people to use torch.library.custom_op because the custom ops produced from it are guarded from various footguns when compared to torch.library.Library.define
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.
Hey! Thanks for the feedback :)
While the custom_op
API does look to be convenient, there are two main reasons it was avoided:
- I'm not sure if we're ready to bump our minimum PyTorch requirement to 2.4.0+. With that said, we're not strictly opposed to that, however.
- I've heard from some others that there was significant overhead introduced with the use of
custom_op
:
- E.g. the decision was made in quanto to not use it: huggingface/optimum-quanto@f36a6ac
- It is mentioned here in vLLM: [torch.compile] directly register custom op vllm-project/vllm#9896 and [Kernel] Register punica ops directly vllm-project/vllm#10522
- PyTorch issue: Custom operators registered via decorator slower than ops registered via
torch.Library.{define, impl}
pytorch/pytorch#139500
I am curious, is it still reasonable to make use of infer_schema
, and is that API available in torch < 2.4?
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.
Thanks for the feedback! It's not clear to me if we have fully fixed the performance issues, but I will check.
torch.library.infer_schema is only available in 2.5+. So if your goal is to support older pytorch versions you are doing the right thing
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-refactor
branch. 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.library
allows us to take advantage of the existing device dispatch mechanisms in PyTorch.torch.compile
support.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_linear_dequant(A: Tensor, B: Tensor, row_stats: Tensor, col_stats: Tensor, bias: Tensor?, dtype=torch.float16)
Name may changeint8_linear_matmul(A: Tensor, B: Tensor) -> Tensor
A @ B.T
int8_mm_dequant(A: Tensor, row_stats: Tensor, col_stats: Tensor, dtype=torch.float16, bias: Tensor?) -> Tensor
torch.float16
for 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) -> Tensor
bitsandbytes.functional.dequantize_4bit
, this operator does not dequantize theabsmax
tensor. If utilized,dequantize_blockwise
must 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 theabsmax
tensor. If utilized,quantize_blockwise
must be performed first.Double quantization (aka
compressed_statistics
ornested
)dequantize_blockwise(A: Tensor, absmax: Tensor, code: Tensor, blocksize: int, dtype) -> Tensor
quantize_blockwise
quantize_blockwise(A: Tensor, code: Tensor, blocksize: int) -> (Tensor, Tensor)
code
.blocksize
will typically be 256 for usage with NF4/FP4 and optimizers.Optional
gemv_4bit
Optimizers
Optimizer functionality will be implemented to support the custom operators in a future update.