
    Z j                         S SK Jr  S SKrSSKJr  SSKJr  SSKJr  SSK	J
r
Jr  SS	KJr  S
SKJr  \R                   " \5      r\ " S S\5      5       r\
 " S S\5      5       rS/rg)    )	dataclassN   )Cache)$ImageClassifierOutputWithNoAttention)PreTrainedModel)auto_docstringlogging   )AutoModelForImageTextToText   )ShieldGemma2Configc                   B    \ rS rSr% SrSr\R                  S-  \S'   Sr	g)0ShieldGemma2ImageClassifierOutputWithNoAttention!   zVShieldGemma2 classifies imags as violative or not relative to a specific policy
Args:
Nprobabilities )
__name__
__module____qualname____firstlineno____doc__r   torchTensor__annotations____static_attributes__r       ڇ/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/shieldgemma2/modeling_shieldgemma2.pyr   r   !   s     *.M5<<$&-r   r   c                     ^  \ rS rSr% \\S'   SrSrS\4U 4S jjrS r	S r
S rS	 r\             SS\R                  S
-  S\R                   S
-  S\R"                  S
-  S\R                  S
-  S\S
-  S\R                  S
-  S\R                   S
-  S\R                  S
-  S\S
-  S\S
-  S\S
-  S\S
-  S\\R"                  -  S\4S jj5       rSrU =r$ )"ShieldGemma2ForImageClassification*   config)imagetextmodelc                    > [         TU ]  US9  [        USS5      U l        [        USS5      U l        [
        R                  " US9U l        U R                  5         g )N)r!   yes_token_indexi *  no_token_indexi  )	super__init__getattrr&   r'   r   from_configr$   	post_init)selfr!   	__class__s     r   r)   +ShieldGemma2ForImageClassification.__init__0   sS    '&v/@&I%f.>E0<<FK
r   c                 R    U R                   R                  5       R                  5       $ N)r$   get_decoderget_input_embeddingsr-   s    r   r3   7ShieldGemma2ForImageClassification.get_input_embeddings7   s    zz%%'<<>>r   c                 V    U R                   R                  5       R                  U5        g r1   )r$   r2   set_input_embeddings)r-   values     r   r7   7ShieldGemma2ForImageClassification.set_input_embeddings:   s    

 55e<r   c                 R    U R                   R                  5       R                  5       $ r1   )r$   r2   get_output_embeddingsr4   s    r   r;   8ShieldGemma2ForImageClassification.get_output_embeddings=   s    zz%%'==??r   c                 V    U R                   R                  5       R                  U5        g r1   )r$   r2   set_output_embeddings)r-   new_embeddingss     r   r>   8ShieldGemma2ForImageClassification.set_output_embeddings@   s    

 66~Fr   N	input_idspixel_valuesattention_maskposition_idspast_key_valuestoken_type_idsinputs_embedslabels	use_cacheoutput_attentionsoutput_hidden_statesreturn_dictlogits_to_keepreturnc                     U R                   " SUUUUUUUUU	U
UUUS.UD6nUR                  nUSS2SU R                  U R                  /4   n[        R
                  " USS9n[        UUS9$ )a  
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.
)rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   N)dim)logitsr   r   )r$   rR   r&   r'   r   softmaxr   )r-   rA   rB   rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   	lm_kwargsoutputsrR   selected_logitsr   s                      r   forward*ShieldGemma2ForImageClassification.forwardC   s    L ** 
%)%+)'/!5#)
 
   B)=)=t?R?R(S!STo2>?"'
 	
r   )r$   r'   r&   )NNNNNNNNNNNNr   )r   r   r   r   r   r   input_modalitiesbase_model_prefixr)   r3   r7   r;   r>   r   r   
LongTensorFloatTensorr   r   boolintr   rW   r   __classcell__)r.   s   @r   r   r   *   sg   (1 ?=@G  .215.204(,2626*.!%)-,0#'-.;
##d*;
 ''$.;
 t+	;

 &&-;
 ;
 ((4/;
 ((4/;
   4';
 $;;
  $;;
 #Tk;
 D[;
 ell*;
  
:!;
 ;
r   r   )dataclassesr   r   cache_utilsr   modeling_outputsr   modeling_utilsr   utilsr   r	   autor   configuration_shieldgemma2r   
get_loggerr   loggerr   r   __all__r   r   r   <module>rj      s|    "    D - / : 
		H	% .7[ . . T
 T
 T
p )r   