o
    
sh'                     @   s   d dl Z d dlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZmZ dd	lmZmZ d
dlmZ e rGd dlZd dlmZ dZdZdZdZdZ	dddZG dd dZdS )    N)Image   )create_causal_mask)_get_model_class)
AutoConfig)MODEL_FOR_PRETRAINING_MAPPINGMODEL_MAPPING)PROCESSOR_MAPPING_NAMESAutoProcessor)TOKENIZER_MAPPING_NAMESAutoTokenizer   )is_torch_availablez[92mz[93mz[0mu   ■u   ⬚<img>c              
      st    jdkrdddddf jdkr%ddddddf ttdd D }d}g }tD ]6\ }	|	krL|sL }d  f< |dkrp|	ksZ d krp d krd d7  d| | f< d}q:ddurfd	d
tD dfddtD }
|dur|dk}t|	dd | 
 |	ddtd|d |}tj|dt t t dt t t d}|d|  d|d  dt|
d  }dur|d7 }|| g }tD ]3\}||f dkr|dd
 tt|ttD  q|tt|tt qtttt| }|D ]}|durO|d d d| d d| nd q4tD ]\\ t|}v rpt | t n|}d fddtD }d}durd fddtD }|| dt d d| d|  qXd|S )z
    Generates an attention matrix from a given attention mask.

    Optionally applies a sliding window mask (e.g., for Gemma2/3) and
    marks regions where image tokens occur based on the specified `img_token`.
       r   N   c                 s   s    | ]	}t t|V  qd S N)lenrepr).0word r   e/var/www/html/alpaca_bot/venv/lib/python3.10/site-packages/transformers/utils/attention_visualizer.py	<genexpr>8   s    z6generate_attention_matrix_from_mask.<locals>.<genexpr>r   r   c                    s$   g | ]  fd dt D qS )c                    s0   g | ]}d  |   krk rn ndnd qS )r   r   r   r   j)isliding_windowr   r   
<listcomp>I   s   0 zBgenerate_attention_matrix_from_mask.<locals>.<listcomp>.<listcomp>)range)r   )nr   )r   r   r   I   s   $ z7generate_attention_matrix_from_mask.<locals>.<listcomp> c                 3   sX    | ]'} d |f rt  t t nd |krt t t n	 d |f r&tntV  qdS r   NYELLOWBLACK_SQUARERESETGREENWHITE_SQUAREr   )maskr   r   r   K   s    


   )
boundariesz: i == j (diagonal)   z: token_type_idszAttention MatrixzSliding Window Maskc                 S   s   g | ]
}t  | t qS r   )r$   r&   )r   kr   r   r   r   i   s    z	    |     c                 3   sl    | ]1}| v r |f rv rt  t t n |kr(t t t n	 |f r0tntV  qd S r   r#   r   )r   	img_tokenr)   r   wordsr   r   r   x   s     

c                 3   sx    | ]7}| v r"v r"d  f d |f kr"t  t t n |kr.t t t n	  | r6tntV  qdS r"   r#   r   )r   r/   sliding_window_masktoken_type_bucketsr   r0   r   r   r      s    ,

z: 
)intndimr   max	enumerater   jointorchwherecumsumboolarange	bucketizer'   r%   r&   r$   appendljustliststrrjustmapzipr   )r0   r)   r/   r   token_type_idsimage_seq_lengthmax_word_lengthfirst_img_idxoutputr-   	row_dummy
is_specialr,   legendf_stringvertical_headeridxrow	word_reprcolored_wordrow_displaysliding_window_rowr   )	r   r/   r)   r    r   r1   r2   r   r0   r   #generate_attention_matrix_from_mask(   s~   	

 
,"
$

.
rV   c                   @   s:   e Zd ZdefddZddefddZddefdd	Zd
S )AttentionMaskVisualizer
model_namec                 C   s   t |}d| _t| drt| dd | _zt|t}W n t	y-   t|t
}Y nw |d u r:td| d|| _G dd d|tj}|||| _| j|j || _|| _d S )Nr   r   zModel name z- is not supported for attention visualizationc                   @   s   e Zd Zdd ZdS )z7AttentionMaskVisualizer.__init__.<locals>._ModelWrapperc                 S   s$   t j|  t dd| _|| _d S )Nr   )nnModule__init__Lineardummy_moduleconfig)selfr^   rX   r   r   r   r[      s   
z@AttentionMaskVisualizer.__init__.<locals>._ModelWrapper.__init__N)__name__
__module____qualname__r[   r   r   r   r   _ModelWrapper   s    rc   )r   from_pretrainedimage_tokenhasattrget_text_configgetattrr   r   r   	Exceptionr   
ValueError
mapped_clsrY   rZ   modeltodtyperepo_idr^   )r_   rX   r^   rk   rc   r   r   r   r[      s"   

z AttentionMaskVisualizer.__init__r.   input_sentencec                 C   s   | j ||d d S )N)suffix)visualize_attention_mask)r_   rp   rq   r   r   r   __call__   s   z AttentionMaskVisualizer.__call__c                 C   sV  | j }i }d }| jjtv rkd}ttj|ddj}d}t	j
| j|d}t|dr-|j}n
|j|jgd }|r?|d|}||||d	d
}	|j|jgd | _|	d }
d|	v r`|	d |d< |j|	d d }n$| jjtv rt
| j}||}||d	dd }
n
td|jj dd|j_|  |
j\}}tj|||jjf| j jd}t|}t|j||
|d d}|d ur|  }
n|
 d d!|d||}
dt"d| jj d| j# d  }d}t$d|  t$dd| jj d| j d| j#j% &t"| d |  t$|  t'||
| jt(| jdd |d|d}t$| t$|  d S ) Nzchttps://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg?download=trueT)streamr+   )rG   re   r   r   pt)imagestextrq   return_tensorsattention_maskrF   	input_ids)rx   zModel type z) does not support attention visualizationeager)rn   )r^   input_embedsry   cache_positionpast_key_valuesr   z##zAttention visualization for z | r   r3   z"  Attention visualization for [1m:z[0m z    r   )r/   r   rF   rG   ))rl   r^   
model_typer	   r   openrequestsgetrawr
   rd   ro   rf   re   	tokenizerconvert_ids_to_tokensimage_token_idreplacer   r   tokenizerj   _attn_implementationtrainshaper9   zeroshidden_sizern   r=   r   r<   	unsqueezeexpandr   rk   printr`   centerrV   rh   )r_   rp   rq   rl   kwargsrG   img	processorre   inputsry   tokensr   
batch_size
seq_lengthr|   r}   causal_masktop_bottom_borderside_borderrN   r   r   r   rr      s   



 
z0AttentionMaskVisualizer.visualize_attention_maskN)r.   )r`   ra   rb   rB   r[   rs   rr   r   r   r   r   rW      s    rW   )r   NNN)r   PILr   masking_utilsr   models.auto.auto_factoryr   models.auto.configuration_autor   models.auto.modeling_autor   r   models.auto.processing_autor	   r
   models.auto.tokenization_autor   r   import_utilsr   r9   torch.nnrY   r'   r$   r&   r%   r(   rV   rW   r   r   r   r   <module>   s(   
l