Skip to content
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

fix(library): only compile CUDA extension on Linux #365

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bench/generation/metrics/latency.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def elapsed_time(self, other):

memory = get_device_memory(device)
if memory is not None:
print(f"Device memory: {memory / (2 ** 30):.4f} GB")
print(f"Device memory: {memory / (2**30):.4f} GB")

latencies = []
input_ids = torch.randint(1, model.config.vocab_size - 1, size=(batch_size, prompt_length)).to(device)
Expand All @@ -89,7 +89,7 @@ def elapsed_time(self, other):

if device.type == "cuda":
peak_memory = torch.cuda.max_memory_allocated()
print(f"Peak memory during benchmark: {peak_memory / (2 ** 30):.4f} GB")
print(f"Peak memory during benchmark: {peak_memory / (2**30):.4f} GB")

mean_latency = np.mean(latencies) / generation_config.min_new_tokens
print(f"Average latency per token: {mean_latency} ms")
Expand Down
2 changes: 1 addition & 1 deletion bench/generation/setup/quanto.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def setup(
calibrate(model, tokenizer, batch_size, batches=4)
print("Freezing")
freeze(model)
print(f"Finished: {time.time()-start:.2f}")
print(f"Finished: {time.time() - start:.2f}")
return model, tokenizer


Expand Down
4 changes: 3 additions & 1 deletion optimum/quanto/library/extensions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import platform

import torch

from .cpp import *
from .extension import *


if torch.cuda.is_available():
if torch.cuda.is_available() and platform.system() == "Linux":
if torch.version.cuda:
from .cuda import *
elif torch.version.hip:
Expand Down
2 changes: 2 additions & 0 deletions optimum/quanto/tensor/weights/qbits.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from packaging import version
from torch.autograd import Function

from ...library import is_extension_available
from ..function import QuantizedLinearFunction
from ..grouped import grouped_shape
from ..packed import PackedTensor
Expand Down Expand Up @@ -102,6 +103,7 @@ def create(qtype, axis, group_size, size, stride, data, scale, shift, requires_g
and len(size) == 2
and (data.device.type == "cuda" and torch.version.cuda)
and torch.cuda.get_device_capability(data.device)[0] >= 8
and is_extension_available("quanto_cuda")
):
if type(data) is PackedTensor:
data = data.unpack()
Expand Down
2 changes: 2 additions & 0 deletions optimum/quanto/tensor/weights/qbytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import torch
from torch.autograd import Function

from ...library import is_extension_available
from ..function import QuantizedLinearFunction
from ..qbytes import QBytesTensor
from ..qtensor import qfallback
Expand Down Expand Up @@ -126,6 +127,7 @@ def create(
and (data.device.type == "cuda" and torch.version.cuda)
and axis == 0
and torch.cuda.get_device_capability(data.device)[0] >= 8
and is_extension_available("quanto_cuda")
):
out_features, in_features = size
if (
Expand Down
6 changes: 3 additions & 3 deletions test/tensor/weights/weight_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ def check_weight_qtensor_linear(qweight, batch_size, tokens, use_bias, rel_max_e
rel_max_err = max_err / mean_val
# These values were evaluated empirically without any optimized kernels.
rtol = {"cpu": 1e-2, "cuda": 2e-2, "mps": 1e-2, "xpu": 2e-2}[device.type]
assert (
rel_max_err < rtol
), f"Maximum error {max_err:.2f} is too high for input of mean value {mean_val:.2f} ({rel_max_err*100:.2f} %)"
assert rel_max_err < rtol, (
f"Maximum error {max_err:.2f} is too high for input of mean value {mean_val:.2f} ({rel_max_err * 100:.2f} %)"
)
Loading