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

[NF4] Freeze dataclass | prep for better pt2 support #799

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
17 changes: 11 additions & 6 deletions torchao/dtypes/nf4tensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import functools
from dataclasses import dataclass
from dataclasses import dataclass, replace
import math
from typing import Dict, Tuple
import math
Expand Down Expand Up @@ -376,7 +376,7 @@ def nf4_pin_memory(aten_op, args, kwargs=None):
return NF4Tensor(*construct_nf4_args(nf4tensor, updated_attrs))


@dataclass
@dataclass(frozen=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious how is frozen=True related to PT2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @mlazos

class SubclassTensorArgs:
original_shape: torch.Size

Expand Down Expand Up @@ -837,8 +837,14 @@ def fsdp_post_all_gather(
(quantized_scalers, quantization_factor, quantized_data) = all_gather_outputs
(tensor_meta, block_size, n_blocks, scaler_block_size, scaler_mean, nf4, pg_size) = metadata
if len(tensor_meta.original_shape) != 2:
raise NotImplementedError(f"only support 2D shape but got dim={len(tensor_meta.original_shape)}")
tensor_meta.original_shape = torch.Size((tensor_meta.original_shape[0] * pg_size, tensor_meta.original_shape[1]))
raise NotImplementedError(
f"only support 2D shape but got dim={len(tensor_meta.original_shape)}"
)

new_shape = torch.Size(
(tensor_meta.original_shape[0] * pg_size, tensor_meta.original_shape[1])
)
new_tensor_meta = replace(tensor_meta, original_shape=new_shape)
if out is not None:
# TODO: add param dtype for mixed precision
assert isinstance(out, NF4Tensor), f"{type(out)}"
Expand All @@ -853,7 +859,7 @@ def fsdp_post_all_gather(
return

return nf4_constructor(
tensor_meta,
new_tensor_meta,
block_size,
n_blocks,
scaler_block_size,
Expand All @@ -864,7 +870,6 @@ def fsdp_post_all_gather(
nf4,
), (quantized_scalers, quantization_factor, quantized_data)


class LinearNF4(torch.autograd.Function):
@staticmethod
def forward(ctx, input: torch.Tensor, weight: NF4Tensor):
Expand Down
Loading