
'''simple docstring'''
import tensorflow as tf

from ...tf_utils import shape_list







class  __A				( tf.keras.layers.Layer			):
 def __init__(   self			:    Any				,   lowerCamelCase			:    str				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    List[str]=1				,   lowerCamelCase			:    Optional[Any]=False				,   **lowerCamelCase			:    Dict       ):



   """simple docstring"""





   super().__init__(**lowerCamelCase       )

   __A		:     Tuple												=		vocab_size
   __A		:     Optional[Any]												=		d_embed
   __A		:     List[str]												=		d_proj

   __A		:     Union[str, Any]												=		cutoffs + [vocab_size]
   __A		:     int												=		[0] + self.cutoffs
   __A		:     int												=		div_val

   __A		:     Dict												=		self.cutoffs[0]
   __A		:     List[str]												=		len(self.cutoffs       ) - 1
   __A		:     Tuple												=		self.shortlist_size + self.n_clusters
   __A		:     str												=		keep_order

   __A		:     Union[str, Any]												=		[]
   __A		:     int												=		[]
 def 		lowercase_(   self			:    Optional[int]				,   lowerCamelCase			:    Union[str, Any]       ):



   """simple docstring"""





   if self.n_clusters > 0:
     __A		:     Union[str, Any]												=		self.add_weight(
         shape=(self.n_clusters, self.d_embed)				,   initializer="""zeros"""				,   trainable=lowerCamelCase				,   name="""cluster_weight"""       )
     __A		:     Dict												=		self.add_weight(
         shape=(self.n_clusters,)				,   initializer="""zeros"""				,   trainable=lowerCamelCase				,   name="""cluster_bias"""       )

   if self.div_val == 1:
     for i in range(len(self.cutoffs       )       ):
       if self.d_proj != self.d_embed:
         __A		:     Any												=		self.add_weight(
             shape=(self.d_embed, self.d_proj)				,   initializer="""zeros"""				,   trainable=lowerCamelCase				,   name=f"out_projs_._{i}"				,   )
         self.out_projs.append(lowerCamelCase       )
       else:
         self.out_projs.append(lowerCamelCase       )
       __A		:     int												=		self.add_weight(
           shape=(self.vocab_size, self.d_embed)				,   initializer="""zeros"""				,   trainable=lowerCamelCase				,   name=f"out_layers_._{i}_._weight"				,   )
       __A		:     Tuple												=		self.add_weight(
           shape=(self.vocab_size,)				,   initializer="""zeros"""				,   trainable=lowerCamelCase				,   name=f"out_layers_._{i}_._bias"				,   )
       self.out_layers.append((weight, bias)       )
   else:
     for i in range(len(self.cutoffs       )       ):
       __A		:     Any												=		self.cutoff_ends[i], self.cutoff_ends[i + 1]
       __A		:     int												=		self.d_embed // (self.div_val**i)

       __A		:     List[str]												=		self.add_weight(
           shape=(d_emb_i, self.d_proj)				,   initializer="""zeros"""				,   trainable=lowerCamelCase				,   name=f"out_projs_._{i}"       )
       self.out_projs.append(lowerCamelCase       )
       __A		:     Tuple												=		self.add_weight(
           shape=(r_idx - l_idx, d_emb_i)				,   initializer="""zeros"""				,   trainable=lowerCamelCase				,   name=f"out_layers_._{i}_._weight"				,   )
       __A		:     Any												=		self.add_weight(
           shape=(r_idx - l_idx,)				,   initializer="""zeros"""				,   trainable=lowerCamelCase				,   name=f"out_layers_._{i}_._bias"				,   )
       self.out_layers.append((weight, bias)       )
   super().build(lowerCamelCase       )
 @staticmethod
 def 		lowercase_(   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    List[Any]=None       ):



   """simple docstring"""





   __A		:     Any												=		x
   if proj is not None:
     __A		:     Tuple												=		tf.einsum("""ibd,ed->ibe"""				,   lowerCamelCase				,   lowerCamelCase       )
   return tf.einsum("""ibd,nd->ibn"""				,   lowerCamelCase				,   lowerCamelCase       ) + b
 @staticmethod
 def 		lowercase_(   lowerCamelCase			:    Tuple				,   lowerCamelCase			:    Union[str, Any]       ):



   """simple docstring"""





   __A		:     Tuple												=		shape_list(lowerCamelCase       )
   __A		:     Tuple												=		tf.range(lp_size[0]				,   dtype=target.dtype       )
   __A		:     Dict												=		tf.stack([r, target]				,   1       )
   return tf.gather_nd(lowerCamelCase				,   lowerCamelCase       )






 def 		lowercase_(   self			:    int				,   lowerCamelCase			:    Any				,   lowerCamelCase			:    int				,   lowerCamelCase			:    Optional[int]=True				,   lowerCamelCase			:    Optional[int]=False       ):



   """simple docstring"""





   __A		:     Any												=		0
   if self.n_clusters == 0:
     __A		:     Union[str, Any]												=		self._logit(lowerCamelCase				,   self.out_layers[0][0]				,   self.out_layers[0][1]				,   self.out_projs[0]       )
     if target is not None:
       __A		:     List[str]												=		tf.nn.sparse_softmax_cross_entropy_with_logits(labels=lowerCamelCase				,   logits=lowerCamelCase       )
     __A		:     str												=		tf.nn.log_softmax(lowerCamelCase				,   axis=-1       )
   else:
     __A		:     Tuple												=		shape_list(lowerCamelCase       )
     __A		:     Any												=		[]
     __A		:     Optional[Any]												=		tf.zeros(hidden_sizes[:2]       )
     for i in range(len(self.cutoffs       )       ):
       __A		:     Dict												=		self.cutoff_ends[i], self.cutoff_ends[i + 1]
       if target is not None:
         __A		:     Optional[Any]												=		(target >= l_idx) & (target < r_idx)
         __A		:     List[Any]												=		tf.where(lowerCamelCase       )
         __A		:     Optional[int]												=		tf.boolean_mask(lowerCamelCase				,   lowerCamelCase       ) - l_idx

       if self.div_val == 1:
         __A		:     List[str]												=		self.out_layers[0][0][l_idx:r_idx]
         __A		:     Union[str, Any]												=		self.out_layers[0][1][l_idx:r_idx]
       else:
         __A		:     str												=		self.out_layers[i][0]
         __A		:     Any												=		self.out_layers[i][1]

       if i == 0:
         __A		:     str												=		tf.concat([cur_W, self.cluster_weight]				,   0       )
         __A		:     int												=		tf.concat([cur_b, self.cluster_bias]				,   0       )

         __A		:     List[str]												=		self._logit(lowerCamelCase				,   lowerCamelCase				,   lowerCamelCase				,   self.out_projs[0]       )
         __A		:     Dict												=		tf.nn.log_softmax(lowerCamelCase       )
         out.append(head_logprob[..., : self.cutoffs[0]]       )
         if target is not None:
           __A		:     Tuple												=		tf.boolean_mask(lowerCamelCase				,   lowerCamelCase       )
           __A		:     Optional[int]												=		self._gather_logprob(lowerCamelCase				,   lowerCamelCase       )
       else:
         __A		:     Union[str, Any]												=		self._logit(lowerCamelCase				,   lowerCamelCase				,   lowerCamelCase				,   self.out_projs[i]       )
         __A		:     str												=		tf.nn.log_softmax(lowerCamelCase       )
         __A		:     Union[str, Any]												=		self.cutoffs[0] + i - 1  # No probability for the head cluster
         __A		:     Tuple												=		head_logprob[..., cluster_prob_idx, None] + tail_logprob
         out.append(lowerCamelCase       )
         if target is not None:
           __A		:     Any												=		tf.boolean_mask(lowerCamelCase				,   lowerCamelCase       )
           __A		:     Optional[int]												=		tf.boolean_mask(lowerCamelCase				,   lowerCamelCase       )
           __A		:     Union[str, Any]												=		self._gather_logprob(lowerCamelCase				,   lowerCamelCase       )
           cur_logprob += cur_head_logprob[:, self.cutoff_ends[1] + i - 1]
       if target is not None:
         loss += tf.scatter_nd(lowerCamelCase				,   -cur_logprob				,   shape_list(lowerCamelCase       )       )
     __A		:     Optional[int]												=		tf.concat(lowerCamelCase				,   axis=-1       )

   if target is not None:
     if return_mean:
       __A		:     List[Any]												=		tf.reduce_mean(lowerCamelCase       )
     # Add the training-time loss value to the layer using `self.add_loss()`.
     self.add_loss(lowerCamelCase       )

     # Log the loss as a metric (we could log arbitrary metrics,
     # including different metrics for training and inference.
     self.add_metric(lowerCamelCase				,   name=self.name				,   aggregation="""mean""" if return_mean else """"""       )

   return out





'''simple docstring'''
from typing import Union

from ..utils import add_end_docstrings, is_torch_available, is_vision_available, logging
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
     from PIL import Image

     from ..image_utils import load_image

if is_torch_available():
     from ..models.auto.modeling_auto import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

A__   :		Any 		=logging.get_logger(__name__)







@add_end_docstrings(_SCREAMING_SNAKE_CASE			)
class  __A				( _SCREAMING_SNAKE_CASE			):
 def __init__(   self			:    Union[str, Any]				,   *lowerCamelCase			:    int				,   **lowerCamelCase			:    Optional[int]       ):



   """simple docstring"""





   super().__init__(*lowerCamelCase				,   **lowerCamelCase       )
   self.check_model_type(lowerCamelCase       )
 def 		lowercase_(   self			:    Any				,   lowerCamelCase			:    Dict=None				,   lowerCamelCase			:    Union[str, Any]=None				,   lowerCamelCase			:    int=None				,   **lowerCamelCase			:    int       ):



   """simple docstring"""





   __A							,					__A		:     Tuple												=		{}, {}
   if padding is not None:
     __A		:     Any												=		padding
   if truncation is not None:
     __A		:     Optional[Any]												=		truncation
   if top_k is not None:
     __A		:     List[str]												=		top_k
   return preprocess_params, {}, postprocess_params
 def __call__(   self			:    Any				,   lowerCamelCase			:    Union["Image.Image", str]				,   lowerCamelCase			:    str = None				,   **lowerCamelCase			:    Tuple       ):



   """simple docstring"""





   if isinstance(lowerCamelCase				,   (Image.Image, str)       ) and isinstance(lowerCamelCase				,   lowerCamelCase       ):
     __A		:     Tuple												=		{"""image""": image, """question""": question}
   else:
     __A		:     List[Any]												=		image
   __A		:     Any												=		super().__call__(lowerCamelCase				,   **lowerCamelCase       )
   return results
 def 		lowercase_(   self			:    int				,   lowerCamelCase			:    Optional[int]				,   lowerCamelCase			:    Dict=False				,   lowerCamelCase			:    str=False       ):



   """simple docstring"""





   __A		:     List[str]												=		load_image(inputs["""image"""]       )
   __A		:     Optional[Any]												=		self.tokenizer(
       inputs["""question"""]				,   return_tensors=self.framework				,   padding=lowerCamelCase				,   truncation=lowerCamelCase       )
   __A		:     Union[str, Any]												=		self.image_processor(images=lowerCamelCase				,   return_tensors=self.framework       )
   model_inputs.update(lowerCamelCase       )
   return model_inputs
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    Tuple       ):



   """simple docstring"""





   __A		:     List[str]												=		self.model(**lowerCamelCase       )
   return model_outputs






 def 		lowercase_(   self			:    str				,   lowerCamelCase			:    Optional[Any]				,   lowerCamelCase			:    List[Any]=5       ):



   """simple docstring"""





   if top_k > self.model.config.num_labels:
     __A		:     str												=		self.model.config.num_labels

   if self.framework == "pt":
     __A		:     Optional[Any]												=		model_outputs.logits.sigmoid()[0]
     __A							,					__A		:     int												=		probs.topk(lowerCamelCase       )
   else:
     raise ValueError(f"Unsupported framework: {self.framework}"       )

   __A		:     Optional[Any]												=		scores.tolist()
   __A		:     Dict												=		ids.tolist()
   return [{"score": score, "answer": self.model.config.idalabel[_id]} for score, _id in zip(lowerCamelCase				,   lowerCamelCase       )]



