Spaces:
Running on Zero
Running on Zero
Patch TorchAO Float8Tensor conversion ops for ZeroGPU
Browse files- .zerogpu_tinker_service.py.swp +0 -0
- zerogpu_tinker_service.py +56 -29
.zerogpu_tinker_service.py.swp
DELETED
|
Binary file (16.4 kB)
|
|
|
zerogpu_tinker_service.py
CHANGED
|
@@ -25,6 +25,10 @@ from dataclasses import dataclass
|
|
| 25 |
from pathlib import Path
|
| 26 |
from typing import Any
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
from fastapi import APIRouter, Depends, FastAPI, Header, HTTPException
|
| 29 |
from pydantic import BaseModel
|
| 30 |
|
|
@@ -383,8 +387,8 @@ def _disable_deepgemm_for_zerogpu() -> None:
|
|
| 383 |
_log_event("zerogpu_deepgemm_disabled")
|
| 384 |
|
| 385 |
|
| 386 |
-
def
|
| 387 |
-
"""Patch TorchAO Float8Tensor
|
| 388 |
if os.environ.get("HATCHERY_ZEROGPU_PATCH_TORCHAO_EMPTY_LIKE", "1").lower() in {
|
| 389 |
"0",
|
| 390 |
"false",
|
|
@@ -395,35 +399,26 @@ def _patch_torchao_float8_empty_like() -> None:
|
|
| 395 |
try:
|
| 396 |
import inspect
|
| 397 |
|
| 398 |
-
import torch
|
| 399 |
from torch.utils._python_dispatch import return_and_correct_aliasing
|
| 400 |
from torchao.quantization import Float8Tensor
|
| 401 |
except Exception as exc: # noqa: BLE001
|
| 402 |
_log_event(
|
| 403 |
-
"
|
| 404 |
error_type=type(exc).__name__,
|
| 405 |
error=str(exc),
|
| 406 |
)
|
| 407 |
return
|
| 408 |
|
| 409 |
-
if getattr(Float8Tensor, "
|
| 410 |
return
|
| 411 |
|
| 412 |
aten = torch.ops.aten
|
| 413 |
implements = Float8Tensor.implements
|
|
|
|
| 414 |
|
| 415 |
-
|
| 416 |
-
def _(func, types, args, kwargs):
|
| 417 |
-
original_kwargs = dict(kwargs or {})
|
| 418 |
-
self = args[0]
|
| 419 |
-
out_dtype = original_kwargs.pop("dtype", None) or self.dtype
|
| 420 |
-
qdata = aten.empty_like.default(self.qdata, **original_kwargs)
|
| 421 |
-
scale_kwargs = {k: v for k, v in original_kwargs.items() if k != "dtype"}
|
| 422 |
-
scale = aten.empty_like.default(self.scale, **scale_kwargs)
|
| 423 |
-
|
| 424 |
-
init_params = inspect.signature(self.__class__).parameters
|
| 425 |
if "hp_value_lb" in init_params:
|
| 426 |
-
|
| 427 |
qdata,
|
| 428 |
scale,
|
| 429 |
self.block_size,
|
|
@@ -434,20 +429,52 @@ def _patch_torchao_float8_empty_like() -> None:
|
|
| 434 |
self.kernel_preference,
|
| 435 |
out_dtype,
|
| 436 |
)
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
return return_and_correct_aliasing(func, args, kwargs, new)
|
| 448 |
|
| 449 |
-
Float8Tensor.
|
| 450 |
-
_log_event("
|
| 451 |
|
| 452 |
|
| 453 |
def _ensure_supported_model(base_model: str, rank: int) -> None:
|
|
@@ -473,7 +500,7 @@ def _load_startup_model() -> Any:
|
|
| 473 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 474 |
|
| 475 |
_disable_deepgemm_for_zerogpu()
|
| 476 |
-
|
| 477 |
base_model = _supported_model_id()
|
| 478 |
started = time.monotonic()
|
| 479 |
_log_event("zerogpu_startup_model_load_start", base_model=base_model, rank=_supported_rank())
|
|
|
|
| 25 |
from pathlib import Path
|
| 26 |
from typing import Any
|
| 27 |
|
| 28 |
+
import torch
|
| 29 |
+
|
| 30 |
+
torch.__future__.set_swap_module_params_on_conversion(True)
|
| 31 |
+
|
| 32 |
from fastapi import APIRouter, Depends, FastAPI, Header, HTTPException
|
| 33 |
from pydantic import BaseModel
|
| 34 |
|
|
|
|
| 387 |
_log_event("zerogpu_deepgemm_disabled")
|
| 388 |
|
| 389 |
|
| 390 |
+
def _patch_torchao_float8_tensor_ops() -> None:
|
| 391 |
+
"""Patch TorchAO Float8Tensor ops used by ZeroGPU tensor packing."""
|
| 392 |
if os.environ.get("HATCHERY_ZEROGPU_PATCH_TORCHAO_EMPTY_LIKE", "1").lower() in {
|
| 393 |
"0",
|
| 394 |
"false",
|
|
|
|
| 399 |
try:
|
| 400 |
import inspect
|
| 401 |
|
|
|
|
| 402 |
from torch.utils._python_dispatch import return_and_correct_aliasing
|
| 403 |
from torchao.quantization import Float8Tensor
|
| 404 |
except Exception as exc: # noqa: BLE001
|
| 405 |
_log_event(
|
| 406 |
+
"zerogpu_torchao_float8_ops_patch_skipped",
|
| 407 |
error_type=type(exc).__name__,
|
| 408 |
error=str(exc),
|
| 409 |
)
|
| 410 |
return
|
| 411 |
|
| 412 |
+
if getattr(Float8Tensor, "_hatchery_float8_ops_patch", False):
|
| 413 |
return
|
| 414 |
|
| 415 |
aten = torch.ops.aten
|
| 416 |
implements = Float8Tensor.implements
|
| 417 |
+
init_params = inspect.signature(Float8Tensor).parameters
|
| 418 |
|
| 419 |
+
def _new_float8_tensor(self, qdata, scale, out_dtype):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
if "hp_value_lb" in init_params:
|
| 421 |
+
return self.__class__(
|
| 422 |
qdata,
|
| 423 |
scale,
|
| 424 |
self.block_size,
|
|
|
|
| 429 |
self.kernel_preference,
|
| 430 |
out_dtype,
|
| 431 |
)
|
| 432 |
+
return self.__class__(
|
| 433 |
+
qdata,
|
| 434 |
+
scale,
|
| 435 |
+
self.block_size,
|
| 436 |
+
self.mm_config,
|
| 437 |
+
self.act_quant_kwargs,
|
| 438 |
+
self.kernel_preference,
|
| 439 |
+
out_dtype,
|
| 440 |
+
)
|
| 441 |
+
|
| 442 |
+
@implements(aten.empty_like.default)
|
| 443 |
+
def _(func, types, args, kwargs):
|
| 444 |
+
original_kwargs = dict(kwargs or {})
|
| 445 |
+
self = args[0]
|
| 446 |
+
out_dtype = original_kwargs.pop("dtype", None) or self.dtype
|
| 447 |
+
qdata = aten.empty_like.default(self.qdata, **original_kwargs)
|
| 448 |
+
scale_kwargs = {k: v for k, v in original_kwargs.items() if k != "dtype"}
|
| 449 |
+
scale = aten.empty_like.default(self.scale, **scale_kwargs)
|
| 450 |
+
new = _new_float8_tensor(self, qdata, scale, out_dtype)
|
| 451 |
+
return return_and_correct_aliasing(func, args, kwargs, new)
|
| 452 |
+
|
| 453 |
+
@implements(aten._to_copy.default)
|
| 454 |
+
def _(func, types, args, kwargs):
|
| 455 |
+
kwargs = dict(kwargs or {})
|
| 456 |
+
self = args[0]
|
| 457 |
+
out_dtype = kwargs.get("dtype", None) or self.dtype
|
| 458 |
+
inner_kwargs = {k: v for k, v in kwargs.items() if k != "dtype"}
|
| 459 |
+
qdata = aten._to_copy.default(self.qdata, **inner_kwargs)
|
| 460 |
+
scale = aten._to_copy.default(self.scale, **inner_kwargs)
|
| 461 |
+
new = _new_float8_tensor(self, qdata, scale, out_dtype)
|
| 462 |
+
return return_and_correct_aliasing(func, args, kwargs, new)
|
| 463 |
+
|
| 464 |
+
@implements([aten.detach.default, aten.clone.default])
|
| 465 |
+
def _(func, types, args, kwargs):
|
| 466 |
+
kwargs = dict(kwargs or {})
|
| 467 |
+
self = args[0]
|
| 468 |
+
new = _new_float8_tensor(
|
| 469 |
+
self,
|
| 470 |
+
func(self.qdata, **kwargs),
|
| 471 |
+
func(self.scale, **kwargs),
|
| 472 |
+
self.dtype,
|
| 473 |
+
)
|
| 474 |
return return_and_correct_aliasing(func, args, kwargs, new)
|
| 475 |
|
| 476 |
+
Float8Tensor._hatchery_float8_ops_patch = True
|
| 477 |
+
_log_event("zerogpu_torchao_float8_ops_patched")
|
| 478 |
|
| 479 |
|
| 480 |
def _ensure_supported_model(base_model: str, rank: int) -> None:
|
|
|
|
| 500 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 501 |
|
| 502 |
_disable_deepgemm_for_zerogpu()
|
| 503 |
+
_patch_torchao_float8_tensor_ops()
|
| 504 |
base_model = _supported_model_id()
|
| 505 |
started = time.monotonic()
|
| 506 |
_log_event("zerogpu_startup_model_load_start", base_model=base_model, rank=_supported_rank())
|