U
    <c	                     @   sh   U d dl mZ d dlZd dlZd dlmZmZmZ i Zeeej	j
f ed< dd ZG dd dejZdS )	    )_TensorBaseN)CallableDictcastHANDLED_FUNCTIONSc                    s   t   fdd}|S )Nc                    s   | t  < | S N)r   )Zautograd_functorch_function Z/tmp/pip-unpacked-wheel-gikjz4vx/torch/nn/utils/_expanded_weights/expanded_weights_impl.py	decorator
   s    z.implements_per_sample_grads.<locals>.decorator)	functoolswraps)r	   r   r
   r   r   implements_per_sample_grads	   s    r   c                   @   sF   e Zd Zdd ZeZdd ZedddZe	d	d
 Z
e	dd ZdS )ExpandedWeightc                 C   s   || _ || _|| _d S r   )
batch_sizeorig_weightloss_reduction)selfr   r   r   r
   r
   r   __init__   s    zExpandedWeight.__init__c                 C   sH   t |tjs tdt|j |js.tdtjtt	| |d}|S )Nz/Can only make Expanded Weights of Tensors, got zCCan only build ExpandedWeights objects of tensors that require_gradT)

isinstancetorchTensorRuntimeErrortype__name__Zrequires_gradZ_make_subclassr   r   )clsr   r   r   retr
   r
   r   __new__$   s    zExpandedWeight.__new__r
   Nc                 C   sV   |d kri }|| j krB| j | jt| |f|t|   S td|j d S )Nz8Expanded Weights encountered but cannot handle function )handled_functionsapplytuplekeysvaluesr   r   )r   func_argskwargsr
   r
   r   __torch_function__,   s
    
,z!ExpandedWeight.__torch_function__c                 C   s   | j jS r   )r   dtyper   r
   r
   r   r)   6   s    zExpandedWeight.dtypec                 C   s   | j jS r   )r   shaper*   r
   r
   r   r+   :   s    zExpandedWeight.shape)r
   N)r   
__module____qualname__r   r   r   r   classmethodr(   propertyr)   r+   r
   r
   r
   r   r      s   	
r   )Ztorch._Cr   r   r   typingr   r   r   r   ZautogradZFunction__annotations__r   r   r   r
   r
   r
   r   <module>   s    