
    Piw!                         d dl Z d dlmZ d dlmZ d dlmZ d dlZd dlmZ d dl	m
Z
mZ d dlmc mZ d dlmZ d dlmZ d d	lmZmZmZmZmZ d d
lmZ d Zd Zd Zd Z G d de
          Z G d de
          Z dS )    N)log2)partial)List)nn)Module
ModuleList)autocast)FSQ)	rearrangerepeatreducepackunpack)get_atc                 
    | d uS N )vals    x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vector_quantize_pytorch/residual_fsq.pyexistsr      s    d?    c                     | d         S )Nr   r   )ls    r   firstr      s    Q4Kr   c                 (    t          |           r| n|S r   )r   )r   ds     r   defaultr      s    ++$331$r   c                 ,    t          | |z            |z  S r   )ceil)nummults     r   round_up_multipler"      s    d
d""r   c                   l     e Zd ZdZdddddee         f fdZed             Zd	 Z	d
 Z
	 	 ddZ xZS )ResidualFSQz> Follows Algorithm 1. in https://arxiv.org/pdf/2107.03312.pdf Fr      )quantize_dropoutquantize_dropout_cutoff_indexquantize_dropout_multiple_oflevelsc                l   t                                                       t          |          }||k    }	|	rt          j        ||          nt          j                    | _        |	rt          j        ||          nt          j                    | _        |	| _        || _	        || _
        t          j        g           | _        t          j        |          }
g }t          |          D ]G}|                    |
dz
  | z             t#          d||d|}| j                            |           Ht%          d | j        D                       sJ | j        d         j        | _        |                     dt          j        |          d           |o|dk    | _        |dk    sJ || _        || _        d S )	Nr%   )r)   dimc                     g | ]
}|j          S r   )has_projections).0fsqs     r   
<listcomp>z(ResidualFSQ.__init__.<locals>.<listcomp>M   s    CCC++CCCr   r   scalesF)
persistentr   )super__init__lenr   LinearIdentity
project_inproject_outr-   num_quantizersr)   r   layerstorchTensorrangeappendr
   allcodebook_sizeregister_bufferstackr&   r'   r(   )selfr+   r)   r:   r&   r'   r(   kwargscodebook_dimrequires_projectionlevels_tensorr1   indr/   	__class__s                 r   r4   zResidualFSQ.__init__&   s    	6{{*c1:M`")C666SUS^S`S`;Na29\3777TVT_TaTa2,mB''V,,(( 		$ 		$CMM=1,#5666 "   C Ks####CCt{CCCDDDDD![^9Xu{6':':OOO 0 G^a5G,1111-J*,H)))r   c                 T    d | j         D             }t          j        |d          }|S )Nc                     g | ]	}|j         
S r   )implicit_codebook)r.   layers     r   r0   z)ResidualFSQ.codebooks.<locals>.<listcomp>\   s    FFFU,FFFr   r   r+   )r;   r<   rC   )rD   	codebookss     r   rP   zResidualFSQ.codebooksZ   s1    FF$+FFF	K	333	r   c                    |j         d         |j         d         }}t          |gd          \  }}|| j        k     r6| j        dk    s
J d            t	          j        |d| j        |z
  fd          }|dk    }|                    |d          }t          d| j        |          }|                    t          |d          d          }t          | j
        d	          }||z  }t          ||d
          \  }|S )Nr   zb * q        zmquantize dropout must be greater than 0 if you wish to reconstruct from a signal with less fine quantizations)valuezq [c] d, b n q -> q b n dzb n q -> q b n 1zq d -> q 1 1 dzq b * d)shaper   r:   r&   Fpadmasked_fillr   rP   r   r1   r   )rD   indicesbatchquantize_dimpsmask	all_codesr1   s           r   get_codes_from_indicesz"ResidualFSQ.get_codes_from_indices`   s   %mA.b0A| G9g..
 $---(2---  0_---eGa)<|)K%LVXYYYG "}%%dA..6PP	 )))D:L*M*MrRR	 4;(899&	 Ir955
	r   c                 x    |                      |          }t          |dd          }|                     |          S )Nzq ... -> ...sum)r_   r   r9   )rD   rY   codescodes_summeds       r   get_output_from_indicesz#ResidualFSQ.get_output_from_indices   s:    ++G44e^U;;---r   Nc                    | j         | j        |j        }}}|                     |          }d}t	          | j                                      |          }g }	| j        o| j        }
|
rt          |          rt          j        |          nt          }|                    | j        |          }|dk    rt          |dz   |          dz
  }t          j        |j        d d         d|t          j                  }t'          dd          5  t)          t+          | j        | j                            D ]m\  }\  }}|
r||k    r|	                    |           & |||z            \  }}||z  }||                                z
  }||z   }|	                    |           n	 d d d            n# 1 swxY w Y   |                     |          }t          j        |	d	
          }	||	f}|s|S |                     |	          }g ||R S )NrS   r%      g      )devicedtypecudaF)enabledrR   rO   )r:   r(   rg   r8   r   r;   boundtrainingr&   r   randomRandom	randranger'   r"   r<   fullrU   longr	   	enumeratezipr1   r?   detachr9   rC   r_   )rD   xreturn_all_codes rand_quantize_dropout_fixed_seed	num_quantquant_dropout_multiple_ofrg   quantized_outresidualall_indicesshould_quantize_dropoutrandrand_quantize_dropout_indexnull_indicesquantizer_indexrN   scale	quantizedrY   retr^   s                        r   forwardzResidualFSQ.forward   s    8<7JDLmopowf,	OOA%%++A.."&-"ID4I
 # 	]FLMmFnFnz6=!ABBBtzD*...9[]f*g*g'(A--.?@[^_@_az.{.{~.+ :agbqbk3QVQ[\\\L f... 	, 	,3<Sdk=Z=Z3[3[ , ,/%* A\/\/\&&|444%*U8e+;%<%<"	7%-	#i&6&6&8&88 -	 9""7++++,	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	," ((77 k+R888k* 	J //<<	 ! i   s   BF**F.1F.)FN)__name__
__module____qualname____doc__r   intr4   propertyrP   r_   rd   r   __classcell__rJ   s   @r   r$   r$   #   s        HH !()'(2I 2I 2I S		2I 2I 2I 2I 2I 2Ih   X
# # #J. . . !+/	B! B! B! B! B! B! B! B!r   r$   c                   h     e Zd Zddd fd
Zed             Zed             Zd Zd Z	 d
d	Z	 xZ
S )GroupedResidualFSQr%   F)groupsaccept_image_fmapc                b   t                                                       || _        || _        ||z  dk    sJ ||z  }|| _        t          j        g           | _        t          |          D ](}| j        	                    t          dd|i|           )| j        d         j        | _        d S )Nr   r+   r   )r3   r4   r+   r   r   r   r   rvqsr>   r?   r$   rA   )rD   r+   r   r   rE   dim_per_group_rJ   s          r   r4   zGroupedResidualFSQ.__init__   s     	f""""v!2M"%%	v 	 	AI[  #     
 "Yq\7r   c                 b    t          j        t          d | j        D                                 S )Nc              3   $   K   | ]}|j         V  d S r   )rP   )r.   rvqs     r   	<genexpr>z/GroupedResidualFSQ.codebooks.<locals>.<genexpr>   s$       D D3 D D D D D Dr   )r<   rC   tupler   rD   s    r   rP   zGroupedResidualFSQ.codebooks   s+    {5 D D$) D D DDDEEEr   c                     | j         rdndS )Nr%   rR   )r   r   s    r   	split_dimzGroupedResidualFSQ.split_dim   s    *2qq2r   c                     t          d t          | j        |          D                       }t          j        |          S )Nc              3   F   K   | ]\  }}|                     |          V  d S r   )r_   r.   r   chunk_indicess      r   r   z<GroupedResidualFSQ.get_codes_from_indices.<locals>.<genexpr>   s5      rrDVCc00??rrrrrrr   )r   rs   r   r<   rC   )rD   rY   rb   s      r   r_   z)GroupedResidualFSQ.get_codes_from_indices   s>    rrZ]^b^gipZqZqrrrrr{5!!!r   c                     t          d t          | j        |          D                       }t          j        || j                  S )Nc              3   F   K   | ]\  }}|                     |          V  d S r   )rd   r   s      r   r   z=GroupedResidualFSQ.get_output_from_indices.<locals>.<genexpr>   s5      uuGYsM33MBBuuuuuur   rO   )r   rs   r   r<   catr   )rD   rY   outputss      r   rd   z*GroupedResidualFSQ.get_output_from_indices   sE    uu]`aeajls]t]tuuuuuy7777r   c           	        
 |j         | j        }}||         | j        k    sJ |                    | j        |          }t          |t          j        dt          d                              
t          
fdt          | j        |          D                       }t          t          |           }|^}}}t          j        ||          }t          j        |          }||g|R }	|	S )NrO   r   g    cA)rv   rw   c              3   0   K   | ]\  }} ||fi V  d S r   r   )r.   r   chunkforward_kwargss      r   r   z-GroupedResidualFSQ.forward.<locals>.<genexpr>  s9      UUZS%CC0000UUUUUUr   )rU   r   r+   r   r   dictrm   randintr   r   rs   r   r<   r   rC   )rD   ru   rv   rU   r   outr   r|   maybe_all_codesr   r   s             @r   r   zGroupedResidualFSQ.forward   s    
 7DNyY48++++ GGDKyG11//5~aS/J/J
 
 
 UUUU3tyRSCTCTUUUUUCI 470	;Iiy999	k+..+888
r   )F)r   r   r   r4   r   rP   r   r_   rd   r   r   r   s   @r   r   r      s        
 !8 8 8 8 8 8 84 F F XF 3 3 X3" " "8 8 8 !       r   r   )!rm   mathr   	functoolsr   typingr   r<   r   torch.nnr   r   torch.nn.functional
functionalrV   	torch.ampr	   2vector_quantize_pytorch.finite_scalar_quantizationr
   einopsr   r   r   r   r   einxr   r   r   r   r"   r$   r   r   r   r   <module>r      s                             ' ' ' ' ' ' ' '                B B B B B B : : : : : : : : : : : : : :          % % %# # #
i! i! i! i! i!& i! i! i!ZI I I I I I I I I Ir   