11import functools
2- from dataclasses import dataclass
2+ from dataclasses import dataclass , replace
33import math
44from typing import Dict , Tuple
55import 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 )
380380class 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-
868873class LinearNF4 (torch .autograd .Function ):
869874 @staticmethod
870875 def forward (ctx , input : torch .Tensor , weight : NF4Tensor ):
0 commit comments