o
    ZhD%                     @   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m	Z	 ddl
mZmZ ddlmZmZ d	d
lmZ e rAd 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   )_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              
      sp    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 ]Z\ }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   V/var/www/auris/lib/python3.10/site-packages/transformers/utils/attention_visualizer.py	<genexpr>7   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>H   s   0 zBgenerate_attention_matrix_from_mask.<locals>.<listcomp>.<listcomp>)range)r   )nr   )r   r   r   H   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   J   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   h   s    z	    |     c                 3   sp    | ]3}| v r |f r  v rt  t t n |kr*t t t n	 |f r2tntV  qd S r   r"   r   )r   	img_tokenr(   wordsr   r   r   w   s    $

c                 3   s|    | ]9}| v r$  v r$d  f d |f kr$t  t t n |kr0t t t n	  | r8tntV  qdS r!   r"   r   )r   r.   sliding_window_masktoken_type_bucketsr/   r   r   r      s    


z: 
)intndimr   max	enumerater   jointorchwhereZcumsumboolarangeZ	bucketizer&   r$   r%   r#   appendljustliststrrjustmapzipr   )r/   r(   r.   r   token_type_idsimage_seq_lengthZmax_word_lengthZfirst_img_idxoutputr,   Z	row_dummyZ
is_specialr+   Zlegendf_stringZvertical_headeridxr   rowZ	word_reprZcolored_wordZrow_displayZsliding_window_rowr   )r   r.   r(   r   r   r0   r1   r/   r   #generate_attention_matrix_from_mask'   s~   	

 
,"
$


.
rI   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d|_zt|t}W n ty'   t|t	}Y nw |d u r4t
d| d|| _G dd d|tj}|||| _| j|j || _|| _d S )Nr   r   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__ZLinearZdummy_moduleconfig)selfrO   rK   r   r   r   rN      s   
z@AttentionMaskVisualizer.__init__.<locals>._ModelWrapper.__init__N)__name__
__module____qualname__rN   r   r   r   r   _ModelWrapper   s    rT   )r   from_pretrainedimage_tokenhasattrZget_text_configr   r   r   	Exceptionr   
ValueError
mapped_clsrL   rM   modeltoZtorch_dtyperepo_idrO   )rP   rK   rO   rZ   rT   r   r   r   rN      s"   

z AttentionMaskVisualizer.__init__r-   input_sentencec                 C   s   | j ||d d S )N)suffix)visualize_attention_mask)rP   r^   r_   r   r   r   __call__   s   z AttentionMaskVisualizer.__call__c              	   C   s  | j }i }| jjtv rid}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d|
|
| j jt|
j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 |d}t!| t!|  d S ) Nzchttps://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg?download=trueT)streamr*   )rD   rV   r   r   pt)Zimagestextr_   return_tensorsattention_maskrC   Z	input_ids)re   zModel type z) does not support attention visualizationeagerr   )rf   Zinput_tensorZcache_positionZpast_key_valuesz##zAttention visualization for z | r   r2   z"  Attention visualization for [1m:z[0m z    r   )r.   r   rC   rD   r   )&r[   rO   Z
model_typer   r   openrequestsgetrawr	   rU   r]   rW   rV   	tokenizerZconvert_ids_to_tokensZimage_token_idreplacer
   r   tokenizerY   Z_attn_implementationtrainZ_update_causal_maskr\   Zdtyper8   r;   shaper:   r   rZ   printrQ   centerrI   getattr)rP   r^   r_   r[   kwargsZimgrD   	processorrV   Zinputsrf   tokensrm   Ztop_bottom_borderZside_borderrF   r   r   r   r`      s|   

 

z0AttentionMaskVisualizer.visualize_attention_maskN)r-   )rQ   rR   rS   r?   rN   ra   r`   r   r   r   r   rJ      s    rJ   )r   NNN)rj   ZPILr   Zmodels.auto.auto_factoryr   Zmodels.auto.configuration_autor   Zmodels.auto.modeling_autor   r   Zmodels.auto.processing_autor   r	   Zmodels.auto.tokenization_autor
   r   Zimport_utilsr   r8   Ztorch.nnrL   r&   r#   r%   r$   r'   rI   rJ   r   r   r   r   <module>   s&   
n