o
    h                     @   s:   d Z ddlZddlmZ ddlmZ G dd dejZdS )a  
A module to use a Constituency Parser to make an embedding for a tree

The embedding can be produced just from the words and the top of the
tree, or it can be done with a form of attention over the nodes

Can be done over an existing parse tree or unparsed text
    N)Trainerc                       sZ   e Zd Z fddZdd Zdd Zdd Zdd
dZedddZ	edddZ
  ZS )TreeEmbeddingc                    s   t t|   |d |d |d |d d| _|| _| jj| jj | _| jd r6|  j| jj| jj 7  _n|  j| jj| jj d 7  _| jd rut	| jj| jj| _
t	| j| jj| _t	| jj| jj| _| jj| jj | _d S | j| _d S )N	all_wordsbackprop	node_attn	top_layer)r   r   r   r      )superr   __init__configconstituency_parserhidden_sizetransition_hidden_sizenum_tree_lstm_layersnnLinearquerykeyvalueoutput_size)selfr   args	__class__ d/var/www/html/env_mimamsha/lib/python3.10/site-packages/stanza/models/constituency/tree_embedding.pyr
      s"   

zTreeEmbedding.__init__c                    s   j d r j|}nt   j|}W d    n1 s!w   Y  dd |D }dd |D }tdd |D }tdd |D }t fdd|D } j d rft fd	d|D }ntjd
d |D dd} j d rdd t|||D }ntj||||fddd} j d s|S  fdd|D }dd |D }	 fdd|	D }
 fdd|	D }dd t|
|D }dd |D }dd t||D }|S )Nr   c                 S      g | ]}|j qS r   )constituents.0xr   r   r   
<listcomp><       z-TreeEmbedding.embed_trees.<locals>.<listcomp>c                 S   r   r   )stater   r   r   r   r!   =   r"   c                 S   s   g | ]}|j d  jqS )r   )
word_queuehxr   r#   r   r   r   r!   ?   s    c                 S   s   g | ]	}|j |j jqS r   )r$   word_positionr%   r&   r   r   r   r!   @       c                       g | ]
} j j|jqS r   )r   transition_stackoutputtransitionsr&   r   r   r   r!   A       r   c                    r)   r   )r   constituent_stackr+   r   r&   r-   r   r   r!   H   r.   c                 S   s   g | ]}|d  j qS )tree_hxr   r   r   r   r   r!   J       r   dimr   c                    s2   g | ]\} t j fd d|jD ddqS )c                    s   g | ]}t |j gqS r   )torchcatr%   )r   wordchxthxr   r   r!   N   s    8TreeEmbedding.embed_trees.<locals>.<listcomp>.<listcomp>r   r5   )r7   stackr$   r&   r   r:   r   r!   N   s    $   r   c                    s   g | ]}  |qS r   )r   r   r-   r   r   r!   U   r4   c                 S   s$   g | ]}t jd d |D ddqS )c                 S   r   r   r1   )r   conr   r   r   r!   W   r"   r=   r   r5   )r7   r>   r3   r   r   r   r!   W      $ c                    $   g | ]}  ||jd  dqS r   )r   reshapeshaper   nhxr-   r   r   r!   X   rA   c                    rB   rC   )r   rE   rF   rG   r-   r   r   r!   Y   rA   c              	   S   s$   g | ]\}}t ||d dqS r   r?   r7   matmul	transpose)r   qkr   r   r   r!   [   rA   c                 S   s   g | ]	}t j|d dqS )r   r5   )r7   softmaxr   r   r   r   r!   \   r(   c                 S   s$   g | ]\}}t |d d|qS rI   rJ   )r   weightr   r   r   r   r!   ]   rA   )	r   r   analyze_treesr7   no_gradr>   r8   zip	unsqueeze)r   inputsstatesconstituent_listsword_begin_hxword_end_hxtransition_hxconstituent_hxr   node_hxqueriesvaluesattnprevious_layerr   r-   r   embed_trees5   s8   





zTreeEmbedding.embed_treesc                 C   s
   t | |S N)ra   )r   rU   r   r   r   forward`   s   
zTreeEmbedding.forwardc                 C   sV   dd | j  D }|  D ]\}}|jr(|ds(|d|t| f  q|S )Nc                 S   s   g | ]}d | qS constituency_parser.r   r   r   r   r   r!   d   s    z+TreeEmbedding.get_norms.<locals>.<listcomp>re   z%s %.6g)	r   	get_normsnamed_parametersrequires_grad
startswithappendr7   normitem)r   linesnameparamr   r   r   rf   c   s   zTreeEmbedding.get_normsTc                 C   sH   |   }dd | D }|D ]}||= q| j|}||| jd}|S )Nc                 S   s   g | ]	}| d r|qS rd   )ri   )r   rN   r   r   r   r!   o   r(   z,TreeEmbedding.get_params.<locals>.<listcomp>)modelconstituencyr   )
state_dictkeysr   
get_paramsr   )r   skip_modulesmodel_stateskippedrN   parserparamsr   r   r   rt   k   s   zTreeEmbedding.get_paramsNc                 C   s   t | d | |}t|j| S )Nrp   )r   loadr   rp   )r   foundation_cacher   r   r   r   from_parser_file|   s   zTreeEmbedding.from_parser_filec                 C   s8   t | d d ||}t|| d }|j| d dd |S )Nrq   r   rp   F)strict)r   model_from_paramsr   load_state_dict)ry   r   r{   r   rp   r   r   r   r~      s   zTreeEmbedding.model_from_params)Trb   )__name__
__module____qualname__r
   ra   rc   rf   rt   staticmethodr|   r~   __classcell__r   r   r   r   r      s    $+
r   )__doc__r7   torch.nnr   "stanza.models.constituency.trainerr   Moduler   r   r   r   r   <module>   s
    
