U
    Mh                     @   s<  d Z ddlZddlmZmZ ddlZddlmZ ddlmZm	Z	m
Z
 ddlmZmZmZ ddd	gZejejd
dZede
ddddddejejee ejejeedddZedejddd	Zdd Zede
ddddddddejdejejeee ee eej eee ee ejd
ddZdS ) a  This file exports ONNX ops for opset 17.

Note [ONNX Operators that are added/updated in opset 17]

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
https://github.com/onnx/onnx/blob/main/docs/Changelog.md#version-17-of-the-default-onnx-operator-set
New operators:
    BlackmanWindow
    DFT
    HammingWindow
    HannWindow
    LayerNormalization
    MelWeightMatrix
    STFT
    SequenceMap
    N)OptionalSequence)_C)_type_utilserrorssymbolic_helper)	_beartype	jit_utilsregistration
layer_normstftquantized_layer_norm   )Zopsetzaten::layer_normvisfnone)ginputnormalized_shapeweightbiasepscudnn_enablec                 C   s   t | }tj|tjj}| }	t|rJtj	||	d}
| j
d|
d}t|rptj||	d}| j
d|d}| j
d|||||dS )NdtypeConstantZvalue_tZLayerNormalization)Z	epsilon_faxis_i)lenr   JitScalarType
from_valueFLOATr   r   _is_nonetorchonesopzeros)r   r   r   r   r   r   r   ZaxisZscalar_typer   Zweight_valueZ
bias_value r(   M/var/www/html/venv/lib/python3.8/site-packages/torch/onnx/symbolic_opset17.pyr   #   s(    
 

zquantized::layer_norm)r   c           
      C   s8   t | |\}}}}t| |||||d}	t | |	||S )NF)r   Zdequantize_helperr   Zquantize_helper)
r   xr   r   r   r   Zop_scaleZop_zero_point_outputr(   r(   r)   r   G   s    c                 C   s    | | d }| | | }||fS )zuHelper function to compute the sizes of the edges (left and right)
    of a given window centered within an FFT size.   r(   )n_fftZwindow_sizeleftrightr(   r(   r)   _compute_edge_sizesY   s    r1   z
aten::stftibFT)
r   r   r.   
hop_length
win_lengthwindow
normalizedonesidedreturn_complexreturnc	              
   C   s  |rt jd|d|dk	r|n|d }	| jdtj|	tjdd}
| jdtj|tjdd}|}t|}|dkr| d	|| jdtjd
gtjdd}n|dkrt jd| d|dtj|d
d}|dk	rH|r|n|}||kst	d| df||k rHt
||\}}| jdt|d}| jdt|d}| jd|||d
d}t|r|r||krt jd| d| d|dt
||\}}tt|t|t|f}n
t|}|jd
 |kst	| jd|d}| jd|tj| d}| jd||
|||dks|rdnd
d}| jd|d
dddgd}|dkrl| d|| jdtjd
gtjdd}|rttj||  d}| d|| jd|d}|S )a  Associates `torch.stft` with the `STFT` ONNX operator.
    Note that torch.stft calls _VF.stft, without centering or padding options.
    Hence, this function does not contain these two arguments.
    See torch.stft source code for more info.

    Args:
        g: Graph to write the ONNX representation into
        input: Input tensor for the transformation
        n_fft: FFT size
        hop_length: Size of the hop. Defaults to `floot(n_fft // 4)`
        win_length: Size of the analysis window. Defaults to `n_fft`
        window: Analysis window. Defaults to a window of all ones
        normalized: Whether to return a normalized STFT
        onesided: Whether to return only half (+1) of the results, given the
            symmetry of the STFT
        return_complex: Whether to return the complex value (Note: Must be
            `False` or `None`)

    Returns:
        op: Operator for torch.stft associated with STFT (ONNX)
    z-STFT does not currently support complex types)msgvalueN   r   r   r      Z	Unsqueezer   r-   zcSTFT can only take inputs of 1 [signal] or 2 [batch, signal] dimensions. Current rank of signal is z, please reduce it.)dimzuAnalysis window size must equal `win_length` or `n_fft`. Please, set `win_length` or `n_fft` to match `window` size ()ZConcat)r   zWThe analysis window can't be longer than the size of the FFT. Please set `win_length` (z) to `n_fft` (z
) or less.ZCast)Zto_iZSTFT)Z
onesided_iZ	Transpose   )Zperm_iZSqueezeDiv)r   ZSymbolicValueErrorr&   r$   ZtensorZint64r   Z_get_tensor_rankZ_get_tensor_dim_sizeAssertionErrorr1   r'   r#   Zhstackr%   shaper   r    r!   Z	onnx_typesqrttyper   )r   r   r.   r4   r5   r6   r7   r8   r9   Zframe_step_valueZframe_step_constZframe_length_constsignalZsignal_rankZn_winZwin_length_defaultr/   r0   Zleft_winZ	right_winZtorch_windowresultZ	sqrt_nfftr(   r(   r)   r   a   s    $   







  

)NNNFTF)__doc__	functoolstypingr   r   r$   r   Z
torch.onnxr   r   r   Ztorch.onnx._internalr   r	   r
   __all__partialZonnx_symbolicZ_onnx_symbolic
parse_argsZGraphContextValueintfloatboolr   r   r1   Zbeartyper   r(   r(   r(   r)   <module>   sV   
"      