o
    h[                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	T d dl
mZ d dlmZ ejjejjgZejdd	d
d Zdd Zejdd	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dcd!d"Zd#d$ Zd%d& Z d'd( Z!d)d* Z"d+d, Z#d-d. Z$d/d0 Z%d1d2 Z&d3d4 Z'd5d6 Z(d7d8 Z)d9d: Z*d;d< Z+d=d> Z,d?d@ Z-dAdB Z.dCdD Z/dEdF Z0dGdH Z1dIdJ Z2dKdL Z3dMdN Z4dOdP Z5dQdR Z6dSdT Z7dUdV Z8dWdX Z9dYdZ Z:d[d\ Z;d]d^ Z<d_d` Z=dadb Z>dS )d    N)pretrain)set_random_seed)parse_transitions)*)test_parse_transitions)build_trainermodule)scopec                   C   s
   t  dS )Nz/in/tiny_emb.pt)TEST_WORKING_DIR r   r   d/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/tests/constituency/test_lstm_model.pypretrain_file   s   
r   c                 G   s&   g dt | }t| g|R  }|jS )N)z--no_multistage--pattn_num_layers4--pattn_d_model256--hidden_size128z--use_lattn)listr   model)r   argstrainerr   r   r   build_model   s   r   c                 C   s   t | ddS )N--transition_schemeTOP_DOWN_UNARY)r   r   r   r   r   unary_model   s   r   c                 C      t |  d S N)r   test_initial_stater   r   r   r   r         r   c                 C   s   t | }t| d S r   )r   r   
test_shiftr   r   r   r   r   r"       s   r"   c                 C   r   r   )r   
test_unaryr    r   r   r   r$   &   r!   r$   c                 C   r   r   )r   test_unary_requires_rootr    r   r   r   r%   )   r!   r%   c                 C   r   r   )r   	test_openr    r   r   r   r&   ,   r!   r&   c                 C      t | dd}t| d S )Nr   TOP_DOWN_COMPOUND)r   r   test_compound_openr#   r   r   r   r)   /      r)   c                 C   r'   )Nr   IN_ORDER)r   r   test_in_order_openr#   r   r   r   r,   3   r*   r,   c                 C   r   r   )r   
test_closer    r   r   r   r-   7   r!   r-      c                    s6  t | |}| | t fddt|D }| ||}| | tdfddt|D }|d | s=J | ||}|d jdksLJ | | | ||}| | | ||}| | |d jdksmJ t	   fddt|D } |d | sJ | ||}|d jdksJ | | dS )	z
    Run a couple small transitions and a forward pass on the given model

    Results are not checked in any way.  This function allows for
    testing that building models with various options results in a
    functional model.
    c                       g | ]} qS r   r   .0_)shiftr   r   
<listcomp>F       z&run_forward_checks.<locals>.<listcomp>NPc                    r/   r   r   r0   )open_transitionr   r   r4   K   r5   r   r.   c                    r/   r   r   r0   )close_transitionr   r   r4   Y   r5   N)
r   build_initial_stater   Shiftrange
bulk_applyOpenConstituentis_legal	num_opensCloseConstituent)r   
num_statesstatesshiftsopen_transitionsclose_transitionsr   )r8   r7   r3   r   run_forward_checks:   s.   
rF   c                 C   s   t |  dS )z
    Checks that the forward pass doesn't crash when run after various operations

    Doesn't check the forward pass for making reasonable answers
    N)rF   r    r   r   r   test_unary_forward`   s   rG   c                 C   s$   t | }t|dd t|dd d S )Nr.   rA      r   rF   r#   r   r   r   test_lstm_forwardh   s   rK   c                 C   s@   t | dd}t| t | dd}t| t | dd}t| d S )N--num_lstm_layers123rJ   r#   r   r   r   test_lstm_layersm   s   rP   c                 C   sL   t | dddd}t| t | dddd}t| t | dddd}t| dS )z8
    Test a couple different sizes of output layers
    z--num_output_layersrM   rL   rN   rO   NrJ   r#   r   r   r   test_multiple_output_forwardu   s   rQ   c                 C   s,   t | dd}t| t | dd}t| dS )zX
    Test that the model continues to work if the tag embedding is turned on or off
    z--tag_embedding_dim200NrJ   r#   r   r   r   test_no_tag_embedding_forward   s   rT   c                 C   s(   t | d}t| t | d}t| dS )z7
    Tests combined dummy and open node embeddings
    z--combined_dummy_embeddingz--no_combined_dummy_embeddingNrJ   r#   r   r   r   test_forward_combined_dummy   s   

rU   c                 C   @   t | dd}t| t | dd}t| t | dd}t| dS )zc
    Tests that different initialization methods of the nonlinearities result in valid tensors
    z--nonlinearityrelutanhsiluNrJ   r#   r   r   r   test_nonlinearity_init      rZ   c                 C   s   t jtddd}t jtddd}t j|sJ dt j|s&J dt| d|d|d	d
}t| t| d|d|d	d}t| dS )z
    Tests loading and running a charlm

    Note that this doesn't test the results of the charlm itself,
    just that the model is shaped correctly
    enforward_charlmz1billion.ptbackward_charlmzFNeed to download en test models (or update path to the forward charlm)zGNeed to download en test models (or update path to the backward charlm)z--charlm_forward_filez--charlm_backward_file--sentence_boundary_vectorsnonewordsN)ospathjoinTEST_MODELS_DIRexistsr   rF   )r   forward_charlm_pathbackward_charlm_pathr   r   r   r   test_forward_charlm   s   ri   c                 C      d}t | d|}t| dS )z]
    Test on a tiny Bert, which hopefully does not take up too much disk space or memory
    zhf-internal-testing/tiny-bert--bert_modelNrJ   r   
bert_modelr   r   r   r   test_forward_bert      rn   c                 C   rj   )z^
    Test on a tiny xlnet, which hopefully does not take up too much disk space or memory
    z%hf-internal-testing/tiny-random-xlnetrk   NrJ   rl   r   r   r   test_forward_xlnet   ro   rp   c                 C   rV   )z,
    Test start & stop boundary vectors
    r_   
everythingra   r`   NrJ   r#   r   r   r    test_forward_sentence_boundaries   r[   rr   c                 C   s   t | dd}t|dd t | dd}t|dd t | dd}t|dd t | dd}t|dd t | dd}t|dd t | dd	}t|dd t | dd
}t|dd t | dd}t|dd t | dd}t|dd t | dd}t|dd dS )z;
    Test different constituency composition functions
    --constituency_compositionbilstmrI   rH   maxkey
untied_key
untied_max
bilstm_max	tree_lstmtree_lstm_cxbigramattnNrJ   r#   r   r   r   %test_forward_constituency_composition   s(   r~   c                 C   st   t | dddd}t|dd t | dddd}t|dd t | dddd}t|dd t | dddd}t|dd d	S )
zH
    Test KEY and UNTIED_KEY either with or without reduce_position
    rs   rw   z--reduce_positionrS   rI   rH   32rv   NrJ   r#   r   r   r   test_forward_key_position   s   r   c                 C   sr   t | dddd}|jdksJ |j|j dksJ t|dd t | ddddd	d
}|jdks0J |jdks7J dS )z
    Test that when attn is used with hidden sizes not evenly divisible by reduce_heads, the model reconfigures the hidden_size
    rs   r}   r   129   r   rI   rH   z--reduce_heads10   
   N)r   hidden_sizereduce_headsrF   r#   r   r   r   test_forward_attn_hidden_size	  s   r   c                 C   s4   t | dddd}t| t | dddd}t| dS )z:
    Test with & without partitioned attention layers
    --pattn_num_heads8r   rS   NrJ   r#   r   r   r   "test_forward_partitioned_attention  s   r   c                 C   sN   t | dddd}t| t | dddd}t| t | ddddd}t| dS )6
    Test with & without labeled attention layers
    --lattn_d_proj64--lattn_d_l16rS   z--lattn_combined_inputNrJ   r#   r   r   r   test_forward_labeled_attention   s   r   c                 C   s8   t | ddddd}t| t | ddddd}t| d S )Nr   r   r   r   --lattn_partitioned--no_lattn_partitionedrJ   r#   r   r   r   test_lattn_partitioned-  s   r   c                 C   s   t t t| ddddddddd	
}t| W d
   n1 s!w   Y  t| ddddddddd
}t| t| dddddd}t| t| dddddd}t| d
S )r   r   1024r   r   r   r   z--lattn_d_input_projr   r   Nr   768rS   )pytestraises
ValueErrorr   rF   r#   r   r   r   test_lattn_projection5  s   
r   c                 C   s<   t | dddddd}t| t | dddddd}t| dS )z4
    Test different timing / position encodings
    r   r   r   z--pattn_timingsinlearnedNrJ   r#   r   r   r   test_forward_timing_choicesI  s   r   c              
   C   `   t | dddddddd	}t| t | dddddddd	}t| t | dddddd	}t| d
S )zA
    Test different transition stack types: lstm & attention
    r   rS   r   z--transition_stackr}   z--transition_headsrM   r   lstmNrJ   r#   r   r   r   test_transition_stackS     r   c              
   C   r   )zB
    Test different constituent stack types: lstm & attention
    r   rS   r   z--constituent_stackr}   z--constituent_headsrM   r   r   NrJ   r#   r   r   r   test_constituent_stackf  r   r   c                 C   s   t | dddddddddd}t| t | dddddd	dddd}t| t | dddddddd	dd}t| t | dddddddddd
}t| t | dddddd	dddd
}t| t | dddddddd	dd
}t| dS )ze
    If the transition hidden size and embedding size are different, the model should still work
    r   rS   r   z--transition_embedding_dimr   z--transition_hidden_sizer_   rq   rR   r`   NrJ   r#   r   r   r   test_different_transition_sizesy  sH   r   c                 C   L   t | dddd}t| t | dddd}t| t | dddd}t| dS )z)
    Test the LSTM_TREE forward pass
    --num_tree_lstm_layersrM   rs   rz   rN   rO   NrJ   r#   r   r   r   test_lstm_tree_forward     r   c                 C   r   )z,
    Test the LSTM_TREE_CX forward pass
    r   rM   rs   r{   rN   rO   NrJ   r#   r   r   r   test_lstm_tree_cx_forward  r   r   c                 C   s   t | dd}t| |jd jjd t|jksJ t | dd}t| |jd jjjd t|jd ks7J t | dd}t| |jd jjjd t|jd ksTJ d	S )
z8
    Test with and without maxout layers for output
    z
--maxout_krS   r   rN   rI   rO      N)r   rF   output_layersweightshapelentransitionslinearr#   r   r   r   test_maxout  s    &*r   c           
      C   s*  t d t| g|R  }|  t d t| g|R  }|  t|jj|jjr,J t|jd j|jd jr<J || t|jj|jjsMJ t|jd j|jd js]J ttj	
|jjtj	
|jjsqJ t g}t|d}|||}t|d}|||}t|d j|d jD ]\}}	tj|j|	jddsJ qt|d j|d jD ]\}}	t|j|	jsJ t|j|	jsJ qt|d j|d jD ]9\}}	|jdu |	jdu ksJ |jdurtj|jj|	jjddsJ t|j|	jsJ t|j|	jsJ qdS )z
    Test that the "copy" method copies the parameters from one model to another

    Also check that the copied models produce the same results
    i  i  r   r.   gHz>)atolN)r   r   evaltorchallclosedelta_embeddingr   r   copy_with_new_structurelinalgnorm	word_lstmweight_ih_l0r   r:   r   r9   r<   zip
word_queuehxr   lstm_hxlstm_cxconstituentsvaluetree_hx)
r   args1args2otherr   r3   model_statesother_statesijr   r   r   check_structure_test  s:    
 (

r   c                 C   s   t | g dg d dS )K
    Test that copying the structure with no changes works as expected
    r   rS   r   rS   r   rR   --delta_embedding_dimr   Nr   r   r   r   r   !test_copy_with_new_structure_same     r   c                 C   s   t | g dg d dS )r   )
r   rS   r   rS   r   rR   r   r   rs   MAX)
r   rS   r   rS   r   rR   r   r   rs   
UNTIED_MAXNr   r   r   r   r   #test_copy_with_new_structure_untied   r   r   c                 C      t | g dg d d S )Nr   r   rM   r   rS   r   rR   r   r   r   rR   r   rN   r   r   r   r   r   "test_copy_with_new_structure_pattn     r   c                 C   r   )Nr   r   rM   r   r   r   rR   r   r   r   rR   r   rN   r   r   r   r   r   !test_copy_with_new_structure_both  r   r   c                 C   r   )Nr   r   r   r   r   r   r   "test_copy_with_new_structure_lattn  r   r   c                 C   s   t | }g d}||gd}t|dksJ dd |d  D }t||D ]\}}|jd j|d ks7J |j|d ks@J q'dS )z@
    Small test which doesn't check results, just execution
    ))IPRP)amVBZ)LuffaNNPr   r.   c                 S   s   g | ]}|qS r   r   )r1   xr   r   r   r4   $  r5   z+test_parse_tagged_words.<locals>.<listcomp>r   N)r   parse_tagged_wordsr   yield_preterminalsr   childrenlabel)r   r   sentenceresultptswordptr   r   r   test_parse_tagged_words  s   r   )r.   )?rb   r   r   stanza.models.commonr   stanza.models.common.utilsr   stanza.models.constituencyr   stanza.testsstanza.tests.constituencyr   &stanza.tests.constituency.test_trainerr   markpipelinetravis
pytestmarkfixturer   r   r   r   r"   r$   r%   r&   r)   r,   r-   rF   rG   rK   rP   rQ   rT   rU   rZ   ri   rn   rp   rr   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sp    




&



"

).