Skip to content

Commit 272e7f6

Browse files
drisspgjerryzh168
authored andcommitted
[NF4] Freeze dataclass | prep for better pt2 support (pytorch#799)
1 parent 18c92da commit 272e7f6

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

torchao/dtypes/nf4tensor.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import functools
2-
from dataclasses import dataclass
2+
from dataclasses import dataclass, replace
33
import math
44
from typing import Dict, Tuple
55
import math
@@ -376,7 +376,7 @@ def nf4_pin_memory(aten_op, args, kwargs=None):
376376
return NF4Tensor(*construct_nf4_args(nf4tensor, updated_attrs))
377377

378378

379-
@dataclass
379+
@dataclass(frozen=True)
380380
class SubclassTensorArgs:
381381
original_shape: torch.Size
382382

@@ -837,8 +837,14 @@ def fsdp_post_all_gather(
837837
(quantized_scalers, quantization_factor, quantized_data) = all_gather_outputs
838838
(tensor_meta, block_size, n_blocks, scaler_block_size, scaler_mean, nf4, pg_size) = metadata
839839
if len(tensor_meta.original_shape) != 2:
840-
raise NotImplementedError(f"only support 2D shape but got dim={len(tensor_meta.original_shape)}")
841-
tensor_meta.original_shape = torch.Size((tensor_meta.original_shape[0] * pg_size, tensor_meta.original_shape[1]))
840+
raise NotImplementedError(
841+
f"only support 2D shape but got dim={len(tensor_meta.original_shape)}"
842+
)
843+
844+
new_shape = torch.Size(
845+
(tensor_meta.original_shape[0] * pg_size, tensor_meta.original_shape[1])
846+
)
847+
new_tensor_meta = replace(tensor_meta, original_shape=new_shape)
842848
if out is not None:
843849
# TODO: add param dtype for mixed precision
844850
assert isinstance(out, NF4Tensor), f"{type(out)}"
@@ -853,7 +859,7 @@ def fsdp_post_all_gather(
853859
return
854860

855861
return nf4_constructor(
856-
tensor_meta,
862+
new_tensor_meta,
857863
block_size,
858864
n_blocks,
859865
scaler_block_size,
@@ -864,7 +870,6 @@ def fsdp_post_all_gather(
864870
nf4,
865871
), (quantized_scalers, quantization_factor, quantized_data)
866872

867-
868873
class LinearNF4(torch.autograd.Function):
869874
@staticmethod
870875
def forward(ctx, input: torch.Tensor, weight: NF4Tensor):

0 commit comments

Comments
 (0)