Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
f46445f
Update
vkuzo Apr 20, 2026
3c92c1a
Update
vkuzo Apr 20, 2026
b513b61
Update
vkuzo Apr 21, 2026
a669b9e
Update
vkuzo Apr 21, 2026
53bd8d0
Update
vkuzo Apr 21, 2026
4c86363
Update
vkuzo Apr 21, 2026
3cc91ed
Update
vkuzo Apr 21, 2026
9b7dc74
Update
vkuzo Apr 21, 2026
d69b32a
Update
vkuzo Apr 21, 2026
294c9cc
Update
vkuzo Apr 21, 2026
65fae62
Update
vkuzo Apr 21, 2026
5ee2ad2
Update
vkuzo Apr 22, 2026
2adda75
Update
vkuzo Apr 22, 2026
6463808
Update
vkuzo Apr 22, 2026
d121bff
Update
vkuzo Apr 22, 2026
80421c8
Update
vkuzo Apr 22, 2026
d302888
Update
vkuzo Apr 22, 2026
9631b76
Update
vkuzo Apr 22, 2026
5fe6574
Update
vkuzo Apr 22, 2026
5292f2f
Update
vkuzo Apr 22, 2026
f679216
Update
vkuzo Apr 22, 2026
68dc794
Update
vkuzo Apr 23, 2026
3ffc619
Update
vkuzo Apr 23, 2026
2f0a3cf
Update
vkuzo Apr 23, 2026
fad1467
Update
vkuzo Apr 23, 2026
f668c26
Update
vkuzo Apr 23, 2026
522de32
Update
vkuzo Apr 23, 2026
f635432
Update
vkuzo Apr 23, 2026
31bcb11
Update
vkuzo Apr 23, 2026
75542fa
Update
vkuzo Apr 23, 2026
be9dc1b
Update
vkuzo Apr 23, 2026
f14cde0
Update
vkuzo Apr 23, 2026
83283cf
Update
vkuzo Apr 23, 2026
ed9e39f
Update
vkuzo Apr 23, 2026
2386670
Update
vkuzo Apr 23, 2026
c1da849
Update
vkuzo Apr 23, 2026
cdcd2b3
Update
vkuzo Apr 23, 2026
196d439
Update
vkuzo Apr 23, 2026
b0697ac
Update
vkuzo Apr 23, 2026
620250d
Update
vkuzo Apr 23, 2026
19bc5c8
Update
vkuzo Apr 23, 2026
61493d9
Update
vkuzo Apr 23, 2026
5a0db16
Update
vkuzo Apr 23, 2026
7794548
Update
vkuzo Apr 23, 2026
8f1f410
Update
vkuzo Apr 23, 2026
b664d8a
Update
vkuzo Apr 23, 2026
dc35c65
Update
vkuzo Apr 23, 2026
f23cf38
Update
vkuzo Apr 23, 2026
6650b1d
Update
vkuzo Apr 23, 2026
4d9b68f
Update
vkuzo Apr 23, 2026
8a21110
Update
vkuzo Apr 23, 2026
4a456ec
Update
vkuzo Apr 23, 2026
f8d1861
Update
vkuzo Apr 23, 2026
932677b
Update
vkuzo Apr 23, 2026
9efbf9f
Update
vkuzo Apr 24, 2026
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
18 changes: 13 additions & 5 deletions test/prototype/gptq/test_gptqv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ def test_observer_tensor_creation(self):
# Check hp_data is stored correctly
torch.testing.assert_close(observer.hp_data, weight)

# Check hessian is initialized as None
assert observer.hessian is None
# Check hessian is initialized as zeros
assert torch.equal(
observer.hessian, torch.zeros(64, 64, dtype=torch.float32, device="cuda")
)

# Check total_batches is initialized as 0
assert observer.total_batches == 0
Expand All @@ -120,7 +122,9 @@ def test_observer_tensor_attributes(self):

# Test hessian attribute
assert hasattr(observer, "hessian")
assert observer.hessian is None
assert torch.equal(
observer.hessian, torch.zeros(32, 32, dtype=torch.float32, device="cuda")
)

# Test total_batches attribute
assert hasattr(observer, "total_batches")
Expand Down Expand Up @@ -193,6 +197,7 @@ def test_multiple_observations(self):
# Check total_batches matches total samples
assert observer_weight.total_batches == total_samples

@pytest.mark.skip(reason="bmm math is incorrect, will fix in next PR")
@pytest.mark.skipif(not torch.cuda.is_available(), reason="Need CUDA available")
def test_bmm_operation_with_observer(self):
"""Test torch.bmm with GPTQObserverTensor updates Hessian correctly."""
Expand Down Expand Up @@ -250,8 +255,11 @@ def test_observer_config_transform(self, base_config):
# Check hp_data matches original weight
torch.testing.assert_close(linear.weight.hp_data, original_weight)

# Check hessian is None initially
assert linear.weight.hessian is None
# Check hessian is initialized as zeros
assert torch.equal(
linear.weight.hessian,
torch.zeros(64, 64, dtype=torch.float32, device="cuda"),
)
assert linear.weight.total_batches == 0

# Perform a forward pass
Expand Down
4 changes: 2 additions & 2 deletions torchao/prototype/gptq/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ def _gptq_config_transform(
)

# Validate that observations were recorded
if tensor.hessian is None:
if tensor.total_batches == 0:
raise ValueError(
f"No observations recorded for {parameter_name}. "
f"Hessian is None. Did you run forward passes during the observe step?"
f"total_batches is 0. Did you run forward passes during the observe step?"
)

# Use pre-computed Hessian directly
Expand Down
2 changes: 1 addition & 1 deletion torchao/prototype/gptq/gptq_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ def skip_lm_head_o_proj(module, fqn):
import inspect

source = inspect.getsource(TorchAoHfQuantizer.get_weight_conversions)
if "_weight_per_tensor_scale" not in source:
if "per_tensor_scale" not in source:
raise RuntimeError(
"Your version of `transformers` does not support NVFP4 serialization. "
"Please install a version that includes "
Expand Down
21 changes: 11 additions & 10 deletions torchao/prototype/gptq/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ def __init__(self, hp_data: torch.Tensor, total_batches: int, hessian=None):
self.hessian = hessian
self.total_batches = total_batches

# initialize hessian
assert self.hp_data.is_contiguous()
if self.hessian is None:
feature_dim = self.hp_data.shape[-1]
self.hessian = torch.zeros(
feature_dim,
feature_dim,
dtype=torch.float32,
device=self.hp_data.device,
)

def update(self, input: torch.Tensor):
"""Incrementally update Hessian matrix from input activations."""
# Move input to same device as hp_data and convert to float
Expand All @@ -39,16 +50,6 @@ def update(self, input: torch.Tensor):
n = 1 if len(shape) == 2 else shape[0]
x = x.reshape(-1, shape[-1])

# Lazily initialize Hessian on first call
if self.hessian is None:
feature_dim = x.shape[-1]
self.hessian = torch.zeros(
feature_dim,
feature_dim,
dtype=torch.float32,
device=self.hp_data.device,
)

# Apply running average formula
if self.total_batches > 0:
self.hessian *= self.total_batches / (self.total_batches + n)
Expand Down
Loading