o
    ãÊih  ã                   @   s¢   d dl Z d dlmZ d dlmZ ddgZe jjZG dd„ deƒZej	Z	ej
Z
e
e jjjƒdd„ ƒZe	ejjƒd	d„ ƒZe	ejjƒd
d„ ƒZejZe j eg¡ dS )é    N)Úreturn_and_correct_aliasing)ÚTorchAOBaseTensorÚ-WeightTensorWithLinearActivationScaleMetadataÚ6to_weight_tensor_with_linear_activation_scale_metadatac                   @   sŠ   e Zd ZdZddgZg Zdejdejfdd„Zdejdejfdd„Z	dd	„ Z
ed
ejdejdejfdd„ƒZedejdejfdd„ƒZdS )r   aé  
    Tensor subclass that wraps a weight tensor and provides metadata for linear activation scaling.
    Right now we hardcode how we apply the scale:
       scaled_linear_act = input_act / scale
       out = F.linear(scaled_linear_act, weight, ...)

    We can generalize this to accept a function as well if needed.

    Args:
        original_weight_tensor (torch.Tensor): The weight tensor to be wrapped.
        scale (torch.Tensor): The scale tensor to be applied to activation.
    Úoriginal_weight_tensorÚscalec                 C   s@   i }|j }||d< d|d< |j|d< |j}tjj| |fi |¤ŽS )NÚdtypeFÚrequires_gradÚdevice)r   r
   ÚshapeÚtorchÚTensorÚ_make_wrapper_subclass)Úclsr   r   Úkwargsr   r   © r   ú`/home/ubuntu/.local/lib/python3.10/site-packages/torchao/quantization/linear_activation_scale.pyÚ__new__$   s   
z5WeightTensorWithLinearActivationScaleMetadata.__new__c                 C   s   || _ || _d S ©N)r   r   )Úselfr   r   r   r   r   Ú__init__1   s   
z6WeightTensorWithLinearActivationScaleMetadata.__init__c                 C   s   | j › S r   )Ú	__class__)r   r   r   r   Ú_quantization_type9   s   z@WeightTensorWithLinearActivationScaleMetadata._quantization_typeÚinput_tensorÚweight_tensorÚbiasc                 C   s&   |j }|j}| | }tjj |||¡S r   )r   r   r   ÚnnÚ
functionalÚlinear)r   r   r   r   r   Úscaled_input_actr   r   r   Ú_quantized_linear_op<   s   ÿzBWeightTensorWithLinearActivationScaleMetadata._quantized_linear_opÚinput_floatc                 C   s
   | ||ƒS r   r   )r   r!   r   r   r   r   Ú
from_floatH   s   
z8WeightTensorWithLinearActivationScaleMetadata.from_floatN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Útensor_data_namesÚtensor_attribute_namesr   r   r   r   r   Ústaticmethodr    Úclassmethodr"   r   r   r   r   r      s:    þ
ýþ
ýÿÿÿþýc                 C   sL   |d |d t |ƒdkr|d nd }}}t|tƒr"| |||¡S tdƒ‚)Nr   é   é   zLLinearActivationQuantizedTensor: No specialized dispatch found for linear op)ÚlenÚ
isinstancer   r    ÚNotImplementedError)ÚfuncÚtypesÚargsr   r   r   r   r   r   r   Ú_W   s   
ý
ÿr3   c                 C   s@   |d }|  | |jg|dd … ¢R i |¤Ž|j¡}t| |||ƒS )Nr   r+   )r   r   r   r   )r0   r1   r2   r   r   Únewr   r   r   r3   f   s
   "ÿc                 C   s   t | |||d  tj¡ƒS )Nr   )r   Ú_apply_fn_to_datar   Út)r0   r1   r2   r   r   r   r   r3   o   s   ÿ)r   Útorch.utils._python_dispatchr   Útorchao.utilsr   Ú__all__ÚopsÚatenr   Ú
implementsÚimplements_torch_functionr   r   r   r3   Úslicer   r6   Údefaultr"   r   ÚserializationÚadd_safe_globalsr   r   r   r   Ú<module>   s(   þ>ÿ




ÿ