§
     `ƒi—  ã                   óô   — d dl mZ d dlmZmZ 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 ddlmZ  ej        e¦  «        Ze G d„ de	¦  «        ¦   «         Ze G d„ de¦  «        ¦   «         ZdgZdS )é    )Ú	dataclass)ÚOptionalÚUnionNé   )ÚCache)Ú$ImageClassifierOutputWithNoAttention)ÚPreTrainedModel)Úauto_docstringÚloggingé   )ÚAutoModelForImageTextToTexté   )ÚShieldGemma2Configc                   ó8   — e Zd ZU dZdZeej                 ed<   dS )Ú0ShieldGemma2ImageClassifierOutputWithNoAttentionz^ShieldGemma2 classifies imags as violative or not relative to a specific policy
    Args:
    NÚprobabilities)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚtorchÚTensorÚ__annotations__© ó    úŠ/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/shieldgemma2/modeling_shieldgemma2.pyr   r   #   s5   € € € € € € ðð ð -1€M8˜EœLÔ)Ð0Ð0Ñ0Ð0Ð0r   r   c                    óà  ‡ — e Zd ZU eed<   dddddœZdefˆ fd„Zd„ Zd	„ Zd
„ Z	d„ Z
d„ Zd„ Zd„ Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!deej                 deej                 deej                 deej                 dee         deej                 deej                 deej                 deej                 dee         dee         dee         dee         deeej        f         defd „¦   «         Zˆ xZS )"Ú"ShieldGemma2ForImageClassificationÚconfigzmodel.model.language_modelzmodel.model.vision_towerz!model.model.multi_modal_projectorzmodel.lm_head)zmodel.language_model.modelzmodel.vision_towerzmodel.multi_modal_projectorzmodel.language_model.lm_headc                 óØ   •— t          ¦   «                              |¬¦  «         t          |dd¦  «        | _        t          |dd¦  «        | _        t          j        |¬¦  «        | _        d S )N)r   Úyes_token_indexi *  Úno_token_indexi»  )ÚsuperÚ__init__Úgetattrr!   r"   r   Úfrom_configÚmodel)Úselfr   Ú	__class__s     €r   r$   z+ShieldGemma2ForImageClassification.__init__6   sa   ø€ Ý‰Œ×Ò ÐÑ'Ô'Ð'Ý& vÐ/@À&ÑIÔIˆÔÝ% fÐ.>ÀÑEÔEˆÔÝ0Ô<ÀFÐKÑKÔKˆŒ
ˆ
ˆ
r   c                 ó>   — | j         j                             ¦   «         S ©N)r'   Úlanguage_modelÚget_input_embeddings©r(   s    r   r-   z7ShieldGemma2ForImageClassification.get_input_embeddings<   s   € ØŒzÔ(×=Ò=Ñ?Ô?Ð?r   c                 óD   — | j         j                             |¦  «         d S r+   )r'   r,   Úset_input_embeddings)r(   Úvalues     r   r0   z7ShieldGemma2ForImageClassification.set_input_embeddings?   s!   € ØŒ
Ô!×6Ò6°uÑ=Ô=Ð=Ð=Ð=r   c                 ó>   — | j         j                             ¦   «         S r+   )r'   r,   Úget_output_embeddingsr.   s    r   r3   z8ShieldGemma2ForImageClassification.get_output_embeddingsB   s   € ØŒzÔ(×>Ò>Ñ@Ô@Ð@r   c                 óD   — | j         j                             |¦  «         d S r+   )r'   r,   Úset_output_embeddings)r(   Únew_embeddingss     r   r5   z8ShieldGemma2ForImageClassification.set_output_embeddingsE   s!   € ØŒ
Ô!×7Ò7¸ÑGÔGÐGÐGÐGr   c                 óD   — | j         j                             |¦  «         d S r+   )r'   r,   Úset_decoder)r(   Údecoders     r   r8   z.ShieldGemma2ForImageClassification.set_decoderH   s!   € ØŒ
Ô!×-Ò-¨gÑ6Ô6Ð6Ð6Ð6r   c                 ó>   — | j         j                             ¦   «         S r+   )r'   r,   Úget_decoderr.   s    r   r;   z.ShieldGemma2ForImageClassification.get_decoderK   ó   € ØŒzÔ(×4Ò4Ñ6Ô6Ð6r   c                 ó>   — | j         j                             ¦   «         S r+   )r'   r,   Útie_weightsr.   s    r   r>   z.ShieldGemma2ForImageClassification.tie_weightsN   r<   r   Nr   Ú	input_idsÚpixel_valuesÚattention_maskÚposition_idsÚpast_key_valuesÚtoken_type_idsÚcache_positionÚinputs_embedsÚlabelsÚ	use_cacheÚoutput_attentionsÚoutput_hidden_statesÚreturn_dictÚlogits_to_keepÚreturnc                 óÈ   —  | j         d|||||||||	|
||||dœ|¤Ž}|j        }|dd…d| j        | j        gf         }t	          j        |d¬¦  «        }t          ||¬¦  «        S )aY  
        Returns:
            A `ShieldGemma2ImageClassifierOutputWithNoAttention` instance containing the logits and probabilities
            associated with the model predicting the `Yes` or `No` token as the response to that prompt, captured in the
            following properties.

                *   `logits` (`torch.Tensor` of shape `(batch_size, 2)`):
                    The first position along dim=1 is the logits for the `Yes` token and the second position along dim=1 is
                    the logits for the `No` token.
                *   `probabilities` (`torch.Tensor` of shape `(batch_size, 2)`):
                    The first position along dim=1 is the probability of predicting the `Yes` token and the second position
                    along dim=1 is the probability of predicting the `No` token.

            ShieldGemma prompts are constructed such that predicting the `Yes` token means the content *does violate* the
            policy as described. If you are only interested in the violative condition, use
            `violated = outputs.probabilities[:, 1]` to extract that slice from the output tensors.

            When used with the `ShieldGemma2Processor`, the `batch_size` will be equal to `len(images) * len(policies)`,
            and the order within the batch will be img1_policy1, ... img1_policyN, ... imgM_policyN.
        )r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   Néÿÿÿÿ)Údim)Úlogitsr   r   )r'   rQ   r!   r"   r   Úsoftmaxr   )r(   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   Ú	lm_kwargsÚoutputsrQ   Úselected_logitsr   s                       r   Úforwardz*ShieldGemma2ForImageClassification.forwardQ   s°   € ðN $”*ð 
ØØ%Ø)Ø%Ø+Ø)Ø)Ø'ØØØ/Ø!5Ø#Ø)ð
ð 
ð ð
ð 
ˆð" ”ˆØ     B¨Ô)=¸tÔ?RÐ(SÐ!SÔTˆÝœ o¸2Ð>Ñ>Ô>ˆÝ?Ø"Ø'ð
ñ 
ô 
ð 	
r   )NNNNNNNNNNNNNr   )r   r   r   r   r   Ú_checkpoint_conversion_mappingr$   r-   r0   r3   r5   r8   r;   r>   r
   r   r   Ú
LongTensorÚFloatTensorr   r   Úboolr   Úintr   rV   Ú__classcell__)r)   s   @r   r   r   ,   s)  ø€ € € € € € àÐÐÑà&BØ8Ø'JØ(7ð	&ð &Ð"ðLÐ1ð Lð Lð Lð Lð Lð Lð@ð @ð @ð>ð >ð >ðAð Að AðHð Hð Hð7ð 7ð 7ð7ð 7ð 7ð7ð 7ð 7ð ð 15Ø48Ø15Ø37Ø+/Ø59Ø59Ø59Ø-1Ø$(Ø,0Ø/3Ø&*Ø34ð=
ð =
à˜EÔ,Ô-ð=
ð ˜uÔ0Ô1ð=
ð ! ¤Ô.ð	=
ð
 ˜uÔ/Ô0ð=
ð " %œð=
ð ! Ô!1Ô2ð=
ð ! Ô!1Ô2ð=
ð   Ô 1Ô2ð=
ð ˜Ô)Ô*ð=
ð ˜D”>ð=
ð $ Dœ>ð=
ð ' tœnð=
ð ˜d”^ð=
ð ˜c 5¤<Ð/Ô0ð=
ð" 
:ð#=
ð =
ð =
ñ „^ð=
ð =
ð =
ð =
ð =
r   r   )Údataclassesr   Útypingr   r   r   Úcache_utilsr   Úmodeling_outputsr   Úmodeling_utilsr	   Úutilsr
   r   Úautor   Úconfiguration_shieldgemma2r   Ú
get_loggerr   Úloggerr   r   Ú__all__r   r   r   ú<module>rh      sX  ðð  "Ð !Ð !Ð !Ð !Ð !Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "à €€€à  Ð  Ð  Ð  Ð  Ð  Ø DÐ DÐ DÐ DÐ DÐ DØ -Ð -Ð -Ð -Ð -Ð -ðð ð ð ð ð ð ð ð /Ð .Ð .Ð .Ð .Ð .Ø :Ð :Ð :Ð :Ð :Ð :ð 
ˆÔ	˜HÑ	%Ô	%€ð ð1ð 1ð 1ð 1ð 1Ð7[ñ 1ô 1ñ „ð1ð ðb
ð b
ð b
ð b
ð b
¨ñ b
ô b
ñ „ðb
ðL )ð€€€r   