|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +# MIT License |
| 17 | + |
| 18 | +# Copyright (c) 2023 DeepSeek |
| 19 | + |
| 20 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 21 | +# of this software and associated documentation files (the "Software"), to deal |
| 22 | +# in the Software without restriction, including without limitation the rights |
| 23 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 24 | +# copies of the Software, and to permit persons to whom the Software is |
| 25 | +# furnished to do so, subject to the following conditions: |
| 26 | + |
| 27 | +# The above copyright notice and this permission notice shall be included in all |
| 28 | +# copies or substantial portions of the Software. |
| 29 | + |
| 30 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 35 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 36 | +# SOFTWARE. |
| 37 | + |
| 38 | +# SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 39 | +# SPDX-License-Identifier: Apache-2.0 |
| 40 | +# |
| 41 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 42 | +# you may not use this file except in compliance with the License. |
| 43 | +# You may obtain a copy of the License at |
| 44 | +# |
| 45 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 46 | +# |
| 47 | +# Unless required by applicable law or agreed to in writing, software |
| 48 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 49 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 50 | +# See the License for the specific language governing permissions and |
| 51 | +# limitations under the License. |
| 52 | + |
| 53 | +import torch |
| 54 | +import triton |
| 55 | +import triton.language as tl |
| 56 | + |
| 57 | +"""Reference: https://github.com/deepseek-ai/DeepSeek-V3/blob/main/inference/kernel.py""" |
| 58 | + |
| 59 | + |
| 60 | +@triton.jit |
| 61 | +def weight_dequant_kernel(x_ptr, s_ptr, y_ptr, M, N, BLOCK_SIZE: tl.constexpr): |
| 62 | + """ |
| 63 | + Dequantizes weights using the provided scaling factors and stores the result. |
| 64 | +
|
| 65 | + Args: |
| 66 | + x_ptr (tl.pointer): Pointer to the quantized weights. |
| 67 | + s_ptr (tl.pointer): Pointer to the scaling factors. |
| 68 | + y_ptr (tl.pointer): Pointer to the output buffer for dequantized weights. |
| 69 | + M (int): Number of rows in the weight matrix. |
| 70 | + N (int): Number of columns in the weight matrix. |
| 71 | + BLOCK_SIZE (tl.constexpr): Size of the block for tiling. |
| 72 | +
|
| 73 | + Returns: |
| 74 | + None |
| 75 | + """ |
| 76 | + pid_m = tl.program_id(axis=0) |
| 77 | + pid_n = tl.program_id(axis=1) |
| 78 | + n = tl.cdiv(N, BLOCK_SIZE) |
| 79 | + offs_m = pid_m * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) |
| 80 | + offs_n = pid_n * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE) |
| 81 | + offs = offs_m[:, None] * N + offs_n[None, :] |
| 82 | + mask = (offs_m[:, None] < M) & (offs_n[None, :] < N) |
| 83 | + x = tl.load(x_ptr + offs, mask=mask).to(tl.float32) |
| 84 | + s = tl.load(s_ptr + pid_m * n + pid_n) |
| 85 | + y = x * s |
| 86 | + tl.store(y_ptr + offs, y, mask=mask) |
| 87 | + |
| 88 | + |
| 89 | +def weight_dequant(x: torch.Tensor, s: torch.Tensor, block_size: int = 128) -> torch.Tensor: |
| 90 | + """ |
| 91 | + Dequantizes the given weight tensor using the provided scale tensor. |
| 92 | +
|
| 93 | + Args: |
| 94 | + x (torch.Tensor): The quantized weight tensor of shape (M, N). |
| 95 | + s (torch.Tensor): The scale tensor of shape (M//block_size, N//block_size). |
| 96 | + block_size (int, optional): The block size to use for dequantization. Defaults to 128. |
| 97 | +
|
| 98 | + Returns: |
| 99 | + torch.Tensor: The dequantized weight tensor of the same shape as `x`. |
| 100 | +
|
| 101 | + Raises: |
| 102 | + AssertionError: If `x` or `s` are not contiguous or if their dimensions are not 2. |
| 103 | + """ |
| 104 | + assert x.is_contiguous() and s.is_contiguous(), "Input tensors must be contiguous" |
| 105 | + assert x.dim() == 2 and s.dim() == 2, "Input tensors must have 2 dimensions" |
| 106 | + M, N = x.size() |
| 107 | + y = torch.empty_like(x, dtype=torch.get_default_dtype()) |
| 108 | + grid = lambda meta: (triton.cdiv(M, meta["BLOCK_SIZE"]), triton.cdiv(N, meta["BLOCK_SIZE"])) |
| 109 | + weight_dequant_kernel[grid](x, s, y, M, N, BLOCK_SIZE=block_size) |
| 110 | + return y |
0 commit comments