o
    }oi;                     @   sp   d dl Z d dlmZ d dlmZmZmZ G dd dZG dd dZedd	d
dd Z	eddd
dd Z
dS )    N)nn)StateDictTransformTransformCTXstate_transformc                   @   s|   e Zd ZdZejdd Zejdd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )TestStateDictTransformz9
    Tests for the StateDictTransform functionality.
    c                 C   s   i ddddddddd	d
dddddddddddddddd
ddddddddddi}ddddddddddd
}t t |t |d}|S )z
        Provides a mock transformation context with predefined source and target states.

        Returns
        -------
            TransformCTX: A context object with source and target states.
        z&model.layers.0.self_attn.q_proj.weight   z&model.layers.0.self_attn.k_proj.weight   z&model.layers.0.self_attn.v_proj.weight   z-model.layers.0.mlp.experts.0.gate_proj.weight   z+model.layers.0.mlp.experts.0.up_proj.weight   z-model.layers.0.mlp.experts.0.down_proj.weight   z-model.layers.0.mlp.experts.1.gate_proj.weight   z+model.layers.0.mlp.experts.1.up_proj.weight   z-model.layers.0.mlp.experts.1.down_proj.weight&model.layers.1.self_attn.q_proj.weight&model.layers.1.self_attn.k_proj.weight&model.layers.1.self_attn.v_proj.weightz-model.layers.1.mlp.experts.0.gate_proj.weightz+model.layers.1.mlp.experts.0.up_proj.weightz-model.layers.1.mlp.experts.0.down_proj.weight	   z-model.layers.1.mlp.experts.1.gate_proj.weightz+model.layers.1.mlp.experts.1.up_proj.weightz-model.layers.1.mlp.experts.1.down_proj.weight)
1decoder.layers.0.self_attention.linear_qkv.weightz2decoder.layers.0.self_attention.linear_proj.weight/decoder.layers.0.mlp.experts.linear_fc1.weight0/decoder.layers.0.mlp.experts.linear_fc1.weight1.decoder.layers.0.mlp.experts.linear_fc2.weight1decoder.layers.1.self_attention.linear_qkv.weightz2decoder.layers.1.self_attention.linear_proj.weight/decoder.layers.1.mlp.experts.linear_fc1.weight0/decoder.layers.1.mlp.experts.linear_fc1.weight1.decoder.layers.1.mlp.experts.linear_fc2.weightsourcesource_statetargettarget_stater   r   Moduleselfr   r    ctx r&   R/home/ubuntu/.local/lib/python3.10/site-packages/tests/lightning/_io/test_state.pymock_ctx   sh   		
zTestStateDictTransform.mock_ctxc                 C   sL   ddddddd}ddddddddddddd	}t t |t |d
}|S )z
        Provides a mock transformation context with a source state that matches the expected source_key
        and a target state prepared with initial values for the expected target_keys.
        r   r      -   "   8   )z&decoder.layers.0.mlp.linear_fc1.weightz&decoder.layers.1.mlp.linear_fc1.weightz/decoder.layers.2.mlp.experts.linear_fc1.weight0z/decoder.layers.2.mlp.experts.linear_fc1.weight1z/decoder.layers.3.mlp.experts.linear_fc1.weight0z/decoder.layers.3.mlp.experts.linear_fc1.weight1r   )#model.layers.0.mlp.gate_proj.weight!model.layers.0.mlp.up_proj.weight#model.layers.1.mlp.gate_proj.weight!model.layers.1.mlp.up_proj.weight-model.layers.2.mlp.experts.0.gate_proj.weight+model.layers.2.mlp.experts.0.up_proj.weight-model.layers.2.mlp.experts.1.gate_proj.weight+model.layers.2.mlp.experts.1.up_proj.weight-model.layers.3.mlp.experts.0.gate_proj.weight+model.layers.3.mlp.experts.0.up_proj.weight-model.layers.3.mlp.experts.1.gate_proj.weight+model.layers.3.mlp.experts.1.up_proj.weightr   r!   r#   r&   r&   r'   mock_multi_target_ctxH   s0   
z,TestStateDictTransform.mock_multi_target_ctxc                 C   B   t dddd d}|| |jd dksJ |jd d	ksJ d
S )zW
        Test transformation when a single source and target key is specified.
        -model.layers.*.mlp.experts.0.down_proj.weightz.decoder.layers.*.mlp.experts.linear_fc2.weightc                 S   s   |d S )Nd   r&   r%   xr&   r&   r'   <lambda>q       zXTestStateDictTransform.test_transform_with_single_source_single_target.<locals>.<lambda>
source_key
target_key	transformr   i   r   i  Nr   r    r$   r(   rD   r&   r&   r'   /test_transform_with_single_source_single_targetj      zFTestStateDictTransform.test_transform_with_single_source_single_targetc                 C   r:   )zN
        Test transformation when multiple source keys are specified.
        )&model.layers.*.self_attn.q_proj.weight&model.layers.*.self_attn.k_proj.weight&model.layers.*.self_attn.v_proj.weight1decoder.layers.*.self_attention.linear_qkv.weightc                 S      |d |d  | S Nr<   
   r&   r%   qkvr&   r&   r'   r?          zMTestStateDictTransform.test_transform_with_multiple_sources.<locals>.<lambda>rA   r   {   r      NrE   rF   r&   r&   r'   $test_transform_with_multiple_sourcesw   s   	z;TestStateDictTransform.test_transform_with_multiple_sourcesc                 C   sJ   t ddddddd d}|| |jd	 d
ksJ |jd dks#J dS )zP
        Test transformation with a dictionary mapping for source keys.
        rI   rJ   rK   )rQ   rR   rS   rL   c                 S   rM   rN   r&   rP   r&   r&   r'   r?      rT   zTTestStateDictTransform.test_transform_with_multiple_mapped_sources.<locals>.<lambda>rA   r   rU   r   rV   NrE   rF   r&   r&   r'   +test_transform_with_multiple_mapped_sources   s   	zBTestStateDictTransform.test_transform_with_multiple_mapped_sourcesc                 C   r:   )zU
        Test transformation with a wildcard pattern and variable arguments.
        z&model.layers.*.self_attn.*_proj.weightrL   c                 W      t |S Nsumr%   argsr&   r&   r'   r?      r@   zOTestStateDictTransform.test_transform_with_variable_arguments.<locals>.<lambda>rA   r   r   r   r   NrE   rF   r&   r&   r'   &test_transform_with_variable_arguments   rH   z=TestStateDictTransform.test_transform_with_variable_argumentsc                 C   J   t dddd d}tt || W d   dS 1 sw   Y  dS )L
        Test transformation when no source keys match the pattern.
        non.existent.patternrL   c                 W   rY   rZ   r[   r]   r&   r&   r'   r?      r@   zOTestStateDictTransform.test_transform_with_no_matching_source.<locals>.<lambda>rA   Nr   pytestraises
ValueErrorrF   r&   r&   r'   &test_transform_with_no_matching_source      
"z=TestStateDictTransform.test_transform_with_no_matching_sourcec                 C   sj   dd }t dd|d}|| |jd dksJ |jd d	ks!J |jd
 dks*J |jd dks3J dS )z
        Test transformation where the target_key is a tuple and the transform function
        returns multiple values that are then unrolled to these target keys.
        c                 S   s   |d |d fS )Nr   r&   r=   r&   r&   r'   split_transform      zTTestStateDictTransform.test_transform_with_multiple_targets.<locals>.split_transformz&decoder.layers.*.mlp.linear_fc1.weight)z#model.layers.*.mlp.gate_proj.weightz!model.layers.*.mlp.up_proj.weightrA   r-   r   r.   r   r/   r   r0   r	   NrE   r$   r9   ri   rD   r&   r&   r'   $test_transform_with_multiple_targets   s   z;TestStateDictTransform.test_transform_with_multiple_targetsc                 C   sf   t dddd d}|| |jd dksJ |jd d	ksJ |jd
 dks(J |jd dks1J dS )zn
        Test transformation when multiple source keys are specified, each with more than 1 wildcard.
        z-model.layers.*.mlp.experts.*.gate_proj.weightz+model.layers.*.mlp.experts.*.up_proj.weight/decoder.layers.*.mlp.experts.linear_fc1.weight*c                 S   s   |d | S NrO   r&   )r%   gateupr&   r&   r'   r?      s    z`TestStateDictTransform.test_transform_with_multiple_sources_multiple_wildcards.<locals>.<lambda>rA   r   r*   r   C   r   r,   r   N   NrE   rF   r&   r&   r'   7test_transform_with_multiple_sources_multiple_wildcards   s   zNTestStateDictTransform.test_transform_with_multiple_sources_multiple_wildcardsc                 C   s   dd }t dd|d}|| |jd dksJ |jd d	ks!J |jd
 dks*J |jd dks3J |jd d	ks<J |jd dksEJ |jd dksNJ |jd dksWJ dS )zn
        Test transformation when multiple target keys are specified, each with more than 1 wildcard.
        c                 S   s   |d |d fS ro   r&   r=   r&   r&   r'   ri      rj   zgTestStateDictTransform.test_transform_with_multiple_targets_multiple_wildcards.<locals>.split_transformrn   rm   rA   r1   r   r2   r	   r3   r
   r4   r   r5   r6   r7   r8   r   NrE   rk   r&   r&   r'   7test_transform_with_multiple_targets_multiple_wildcards   s   	zNTestStateDictTransform.test_transform_with_multiple_targets_multiple_wildcardsc                 C   r`   )ra   r;   rb   c                 W   rY   rZ   r[   r]   r&   r&   r'   r?     r@   zOTestStateDictTransform.test_transform_with_no_matching_target.<locals>.<lambda>rA   Nrc   rF   r&   r&   r'   &test_transform_with_no_matching_target   rh   z=TestStateDictTransform.test_transform_with_no_matching_targetc                 C   r`   )zg
        Test transformation with a transform function that does not match expected signature.
        rI   rL   c                 S   s   dS )Nr   r&   )r%   r&   r&   r'   r?     s    zWTestStateDictTransform.test_transform_with_invalid_transform_function.<locals>.<lambda>rA   Nrc   rF   r&   r&   r'   .test_transform_with_invalid_transform_function  rh   zETestStateDictTransform.test_transform_with_invalid_transform_functionN)__name__
__module____qualname____doc__rd   fixturer(   r9   rG   rW   rX   r_   rg   rl   rt   ru   rv   rw   r&   r&   r&   r'   r      s     
-
!r   c                   @   s.   e Zd ZdZejdd Zdd Zdd ZdS )	TestStateTransformDecoratorzA
    Tests for the @state_transform decorator functionality.
    c                 C   s4   dddd}dddd}t t |t |d}|S )zb
        Provides a mock transformation context with predefined source and target states.
        r   r   r	   )r   r   r   r   )/decoder.layers.1.self_attention.linear_q.weight/decoder.layers.1.self_attention.linear_k.weight/decoder.layers.1.self_attention.linear_v.weightr   r!   r#   r&   r&   r'   r(     s   z$TestStateTransformDecorator.mock_ctxc                 C   s   t | |jd dksJ dS )zZ
        Test the @state_transform decorator with a single source and target key.
        r~      N)single_transformr    r$   r(   r&   r&   r'   test_single_transform-  s   z1TestStateTransformDecorator.test_single_transformc                 C   sB   t | |jd dksJ |jd dksJ |jd dksJ dS )zh
        Test the @state_transform decorator with a single source key and multiple target keys.
        r~   r   r   r   r   r	   N)multiple_outputs_transformr    r   r&   r&   r'   test_multiple_outputs_transform6  s   z;TestStateTransformDecorator.test_multiple_outputs_transformN)	rx   ry   rz   r{   rd   r|   r(   r   r   r&   r&   r&   r'   r}     s    
	r}   rI   r~   )rB   rC   c                 C   s   |d S )zK
    A single transformation function that adds 10 to the input value.
    rO   r&   r=   r&   r&   r'   r   B  s   r   z&model.layers.1.self_attn.*_proj.weight)z/decoder.layers.1.self_attention.linear_*.weightc                 G   s   |S )zZ
    A transformation function that returns multiple values for multiple target keys.
    r&   r]   r&   r&   r'   r   L  s   r   )rd   torchr   nemo.lightning.io.stater   r   r   r   r}   r   r   r&   r&   r&   r'   <module>   s    /
