
    &`ip-                     >   d Z ddlmZmZmZmZ ddlZddlm	Z	 ddl
mZ ddlmZ ddlmZ  e            \  ZZedd	ed
efd            Zedeeef         deeeeef         f         deeeeef         f         d
eeeeef         f         eeef         ffd            Zedeeef         deeef         deeeeef         f         d
eeeeef         f         eeef         ffd            Zedeeef         deeeeef         f         deeeeef         f         d
eeef         fd            Ze G d dej                              Ze G d dej                              Ze G d dej                              Ze G d dej                              ZdS )z< Code adapted from https://github.com/ikostrikov/pytorch-a3c    )AnyListTupleUnionN)get_activation_fn)DeveloperAPI)try_import_torch)
TensorType      ?stdreturnc                       fd}|S )Nc           	          | j                             dd           | xj         t                              | j                             d                              dd                    z  z  c_         d S )Nr         T)keepdim)datanormal_torchsqrtpowsum)tensorr   s    o/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/rllib/models/torch/misc.pyinitializerz&normc_initializer.<locals>.initializer   sa    Aq!!!sUZZ(:(:(>(>q$(>(O(OPPPP     )r   r   s   ` r   normc_initializerr      s)    Q Q Q Q Q r   in_sizefilter_sizestride_sizec                    | \  }}t          |t                    r||}}n|\  }}t          |t          t          f          rt          |          t          |          }}n*t          |d                   t          |d                   }}t          t          j        t          |          t          |          z                      }	t          t          j        t          |          t          |          z                      }
t          |	dz
  |z  |z   |z
            }t          |
dz
  |z  |z   |z
            }|dz  }||z
  }|dz  }||z
  }||||f}|	|
f}||fS )a	  Note: Padding is added to match TF conv2d `same` padding.

    See www.tensorflow.org/versions/r0.12/api_docs/python/nn/convolution

    Args:
        in_size: Rows (Height), Column (Width) for input
        stride_size (Union[int,Tuple[int, int]]): Rows (Height), column (Width)
            for stride. If int, height == width.
        filter_size: Rows (Height), column (Width) for filter

    Returns:
        padding: For input into torch.nn.ZeroPad2d.
        output: Output shape after padding and convolution.
    r   r   r   
isinstanceintfloatnpceil)r   r    r!   	in_heightin_widthfilter_heightfilter_widthstride_heightstride_width
out_height	out_widthpad_along_heightpad_along_widthpad_top
pad_bottompad_left	pad_rightpaddingoutputs                      r   same_paddingr9      si   ( "Ix+s## 2&1;|&1#|+U|,, O&)+&6&6K8H8H|&)+a.&9&93{1~;N;N|RWU9--m0D0DDEEFFJBGE(OOeL.A.AABBCCIJNm;mKiWXX9q=L8<G(RSSO!#G!G+J!#H(*IGZ8G)$FF?r   strided_sizekernelstridec                 N   t          |t                    r||fn|\  }}t          |t                    r||fn|\  }}|dz
  |z   dz
  |dz
  |z   dz
  }}|dz  }	|	|dz  dk    rdndz   }
|dz  }||dz  dk    rdndz   }| d         |z   |z
  dz   | d         |z   |z
  dz   f}|
|	||f|fS )a  Computes padding and output size such that TF Conv2DTranspose `same` is matched.

    Note that when padding="same", TensorFlow's Conv2DTranspose makes sure that
    0-padding is added to the already strided image in such a way that the output image
    has the same size as the input image times the stride (and no matter the
    kernel size).

    For example: Input image is (4, 4, 24) (not yet strided), padding is "same",
    stride=2, kernel=5.

    First, the input image is strided (with stride=2):

    Input image (4x4):
    A B C D
    E F G H
    I J K L
    M N O P

    Stride with stride=2 -> (7x7)
    A 0 B 0 C 0 D
    0 0 0 0 0 0 0
    E 0 F 0 G 0 H
    0 0 0 0 0 0 0
    I 0 J 0 K 0 L
    0 0 0 0 0 0 0
    M 0 N 0 O 0 P

    Then this strided image (strided_size=7x7) is padded (exact padding values will be
    output by this function):

    padding -> (left=3, right=2, top=3, bottom=2)

    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 A 0 B 0 C 0 D 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 E 0 F 0 G 0 H 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 I 0 J 0 K 0 L 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 M 0 N 0 O 0 P 0 0
    0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0

    Then deconvolution with kernel=5 yields an output image of 8x8 (x num output
    filters).

    Args:
        strided_size: The size (width x height) of the already strided image.
        kernel: Either width x height (tuple of ints) or - if a square kernel is used -
            a single int for both width and height.
        stride: Either stride width x stride height (tuple of ints) or - if square
            striding is used - a single int for both width- and height striding.

    Returns:
        Tuple consisting of 1) `padding`: A 4-tuple to pad the input after(!) striding.
        The values are for left, right, top, and bottom padding, individually.
        This 4-tuple can be used in a torch.nn.ZeroPad2d layer, and 2) the output shape
        after striding, padding, and the conv transpose layer.
    r   r   r   )r$   r%   )r:   r;   r<   k_wk_hs_ws_hpad_total_wpad_total_hr6   r5   r4   r3   output_shapes                 r   #same_padding_transpose_after_striderE   C   s   J $.fc#:#:FHC#-fc#:#:FHC
  #Qw}q0#'C-!2CKq IqA!5!5AA1=H!JqA!5!5AA1=G 	Q+%+a/Q+%+a/L i*5|CCr   c                    | \  }}t          |t                    r||}}n|\  }}t          |t          t          f          rt          |          t          |          }}n*t          |d                   t          |d                   }}t          t          j        ||z
  dz   t          |          z                      }	t          t          j        ||z
  dz   t          |          z                      }
|	|
fS )au  Emulates TF Conv2DLayer "valid" padding (no padding) and computes output dims.

    This method, analogous to its "same" counterpart, but it only computes the output
    image size, since valid padding means (0, 0, 0, 0).

    See www.tensorflow.org/versions/r0.12/api_docs/python/nn/convolution

    Args:
        in_size: Rows (Height), Column (Width) for input
        stride_size (Union[int,Tuple[int, int]]): Rows (Height), column (Width)
            for stride. If int, height == width.
        filter_size: Rows (Height), column (Width) for filter

    Returns:
        The output shape after padding and convolution.
    r   r   r#   )r   r    r!   r)   r*   r+   r,   r-   r.   r/   r0   s              r   valid_paddingrG      s    , "Ix+s## 2&1;|&1#|+U|,, O&)+&6&6K8H8H|&)+a.&9&93{1~;N;N|RWi-7!;u]?S?SSTTUUJBGX4q8E,<O<OOPPQQI	""r   c                        e Zd ZdZ	 	 	 ddededeeeeef         f         deeeeef         f         deeeeef         f         d	ed
edef fdZ	de
de
fdZ xZS )
SlimConv2dzSimple mock of tf.slim Conv2ddefaultr   in_channelsout_channelsr;   r<   r7   r   activation_fn	bias_initc	                    t          t          |                                            g }	|r-|	                    t                              |                     t                              ||||          }
|r'|dk    rt          j        j        } ||
j	                   t          j        
                    |
j        |           |	                    |
           t          |t                    r#|dk    rt          j        }nt          |d          }||	                     |                       t	          j        |	 | _        dS )a  Creates a standard Conv2d layer, similar to torch.nn.Conv2d

        Args:
            in_channels: Number of input channels
            out_channels: Number of output channels
            kernel: If int, the kernel is
                a tuple(x,x). Elsewise, the tuple can be specified
            stride: Controls the stride
                for the cross-correlation. If int, the stride is a
                tuple(x,x). Elsewise, the tuple can be specified
            padding: Controls the amount
                of implicit zero-paddings during the conv operation
            initializer: Initializer function for kernel weights
            activation_fn: Activation function at the end of layer
            bias_init: Initalize bias weights to bias_init const
        rJ   r   N)superrI   __init__appendnn	ZeroPad2dConv2dinitxavier_uniform_weight	constant_biasr$   strReLUr   
Sequential_model)selfrK   rL   r;   r<   r7   r   rM   rN   layersconv	__class__s              r   rQ   zSlimConv2d.__init__   s$   8 	j$((*** 	1MM",,w//000yylFFCC 	%i'' g5K$$$
$)Y///dmS)) 	J	)) " 1- I I$MM--//***mV,r   xr   c                 ,    |                      |          S Nr^   r_   rc   s     r   forwardzSlimConv2d.forward       {{1~~r   )rJ   rJ   r   )__name__
__module____qualname____doc__r%   r   r   r   r&   rQ   r
   rh   __classcell__rb   s   @r   rI   rI      s        '' %&2- 2-2- 2- c5c?*+	2-
 c5c?*+2- sE#s(O+,2- 2- 2- 2- 2- 2- 2- 2- 2-h 
        r   rI   c                   V     e Zd ZdZ	 	 	 	 ddedededed	ed
ef fdZde	de	fdZ
 xZS )SlimFCz+Simple PyTorch version of `linear` functionNT        r   out_sizer   rM   use_biasrN   c                    t          t          |                                            g }t                              |||          }|t          j        j        } ||j                   |du r%t          j                            |j	        |           |
                    |           t          |t                    rt          |d          }||
                     |                       t          j        | | _        dS )a  Creates a standard FC layer, similar to torch.nn.Linear

        Args:
            in_size: Input size for FC Layer
            out_size: Output size for FC Layer
            initializer: Initializer function for FC layer weights
            activation_fn: Activation function at the end of layer
            use_bias: Whether to add bias weights or not
            bias_init: Initalize bias weights to bias_init const
        )rZ   NTr   )rP   rq   rQ   rS   LinearrV   rW   rX   rY   rZ   rR   r$   r[   r   r]   r^   )
r_   r   rs   r   rM   rt   rN   r`   linearrb   s
            r   rQ   zSlimFC.__init__  s    & 	fd$$&&&7H8<<'1KFM"""tGfk9555fmS)) 	F-mWEEM$MM--//***mV,r   rc   r   c                 ,    |                      |          S re   rf   rg   s     r   rh   zSlimFC.forward)  ri   r   )NNTrr   )rj   rk   rl   rm   r%   r   boolr&   rQ   r
   rh   rn   ro   s   @r   rq   rq      s        55  !#- #-#- #- 	#-
 #- #- #- #- #- #- #- #-J 
        r   rq   c                   8     e Zd ZdZdef fdZdedefdZ xZS )AppendBiasLayerz-Simple bias appending layer for free_log_std.num_bias_varsc                     t                                                       t          j                            t                              dg|z                      | _        |                     d| j                   d S )Nrr   log_std)rP   rQ   r   rS   	Parameter	as_tensorr~   register_parameter)r_   r|   rb   s     r   rQ   zAppendBiasLayer.__init__1  sa    x))%//3%-:O*P*PQQ	4<88888r   rc   r   c                     t                               || j                            d                              t          |          dg          gd          }|S )Nr   r   )axis)r   catr~   	unsqueezerepeatlen)r_   rc   outs      r   rh   zAppendBiasLayer.forward6  sJ    iiDL22155<<c!ffa[IIJQRiSS
r   )	rj   rk   rl   rm   r%   rQ   r
   rh   rn   ro   s   @r   r{   r{   -  sl        779c 9 9 9 9 9 9
 
        r   r{   c                   .     e Zd ZdZdef fdZd Z xZS )Reshapez,Standard module that reshapes/views a tensorshapec                 V    t                                                       || _        d S re   )rP   rQ   r   )r_   r   rb   s     r   rQ   zReshape.__init__?  s$    


r   c                       |j         | j         S re   )viewr   rg   s     r   rh   zReshape.forwardC  s    qvtz""r   )rj   rk   rl   rm   r   rQ   rh   rn   ro   s   @r   r   r   ;  sY        66d      # # # # # # #r   r   )r   )rm   typingr   r   r   r   numpyr'   ray.rllib.models.utilsr   ray.rllib.utils.annotationsr   ray.rllib.utils.frameworkr	   ray.rllib.utils.typingr
   r   rS   r&   r   r%   r9   rE   rG   ModulerI   rq   r{   r   r   r   r   <module>r      sS   B B * * * * * * * * * * * *     4 4 4 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 - - - - - -	r  5 3     (38_(sE#s(O+,( sE#s(O+,( CsCx !5c?3	( ( ( (V WDS/WD#s(OWD #uS#X&'WD CsCx !5c?3	WD WD WD WDt "#38_"#sE#s(O+,"# sE#s(O+,"# 38_	"# "# "# "#J 8 8 8 8 8 8 8 8v ) ) ) ) )RY ) ) )X 
 
 
 
 
bi 
 
 
 # # # # #bi # # # # #r   