
import tensorflow as tf

from ...tf_utils import shape_list




class 				_lowerCamelCase (      tf.keras.layers.Layer ):





     """simple docstring"""







     def __init__(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=1						,						__SCREAMING_SNAKE_CASE=False						,						**__SCREAMING_SNAKE_CASE  )  ->   List[Any]:




           """simple docstring"""







           super().__init__(**_A  )

           UpperCamelCase__    :   int					=				vocab_size
           UpperCamelCase__    :   Union[str, Any]					=				d_embed
           UpperCamelCase__    :   Any					=				d_proj

           UpperCamelCase__    :   List[Any]					=				cutoffs + [vocab_size]
           UpperCamelCase__    :   Any					=				[0] + self.cutoffs
           UpperCamelCase__    :   Optional[Any]					=				div_val

           UpperCamelCase__    :   Any					=				self.cutoffs[0]
           UpperCamelCase__    :   Tuple					=				len(self.cutoffs  ) - 1
           UpperCamelCase__    :   Union[str, Any]					=				self.shortlist_size + self.n_clusters
           UpperCamelCase__    :   List[Any]					=				keep_order

           UpperCamelCase__    :   int					=				[]
           UpperCamelCase__    :   str					=				[]






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE  )  ->   Union[str, Any]:




           """simple docstring"""







           if self.n_clusters > 0:
                 UpperCamelCase__    :   Tuple					=				self.add_weight(
                     shape=(self.n_clusters, self.d_embed)						,						initializer='''zeros'''						,						trainable=_A						,						name='''cluster_weight'''  )
                 UpperCamelCase__    :   int					=				self.add_weight(
                     shape=(self.n_clusters,)						,						initializer='''zeros'''						,						trainable=_A						,						name='''cluster_bias'''  )

           if self.div_val == 1:
                 for i in range(len(self.cutoffs  )  ):
                       if self.d_proj != self.d_embed:
                             UpperCamelCase__    :   Any					=				self.add_weight(
                                 shape=(self.d_embed, self.d_proj)						,						initializer='''zeros'''						,						trainable=_A						,						name=F'''out_projs_._{i}'''						,						)
                             self.out_projs.append(_A  )
                       else:
                             self.out_projs.append(_A  )
                       UpperCamelCase__    :   List[str]					=				self.add_weight(
                           shape=(self.vocab_size, self.d_embed)						,						initializer='''zeros'''						,						trainable=_A						,						name=F'''out_layers_._{i}_._weight'''						,						)
                       UpperCamelCase__    :   Dict					=				self.add_weight(
                           shape=(self.vocab_size,)						,						initializer='''zeros'''						,						trainable=_A						,						name=F'''out_layers_._{i}_._bias'''						,						)
                       self.out_layers.append((weight, bias)  )
           else:
                 for i in range(len(self.cutoffs  )  ):
                       UpperCamelCase__				,UpperCamelCase__    :   Any					=				self.cutoff_ends[i], self.cutoff_ends[i + 1]
                       UpperCamelCase__    :   Tuple					=				self.d_embed // (self.div_val**i)

                       UpperCamelCase__    :   int					=				self.add_weight(
                           shape=(d_emb_i, self.d_proj)						,						initializer='''zeros'''						,						trainable=_A						,						name=F'''out_projs_._{i}'''  )
                       self.out_projs.append(_A  )
                       UpperCamelCase__    :   Tuple					=				self.add_weight(
                           shape=(r_idx - l_idx, d_emb_i)						,						initializer='''zeros'''						,						trainable=_A						,						name=F'''out_layers_._{i}_._weight'''						,						)
                       UpperCamelCase__    :   str					=				self.add_weight(
                           shape=(r_idx - l_idx,)						,						initializer='''zeros'''						,						trainable=_A						,						name=F'''out_layers_._{i}_._bias'''						,						)
                       self.out_layers.append((weight, bias)  )
           super().build(_A  )






     @staticmethod
     def    __SCREAMING_SNAKE_CASE			(							__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=None  )  ->   Tuple:




           """simple docstring"""







           UpperCamelCase__    :   Union[str, Any]					=				x
           if proj is not None:
                 UpperCamelCase__    :   List[str]					=				tf.einsum('''ibd,ed->ibe'''						,						_A						,						_A  )
           return tf.einsum('''ibd,nd->ibn'''						,						_A						,						_A  ) + b






     @staticmethod
     def    __SCREAMING_SNAKE_CASE			(							__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )  ->   str:




           """simple docstring"""







           UpperCamelCase__    :   List[Any]					=				shape_list(_A  )
           UpperCamelCase__    :   Tuple					=				tf.range(lp_size[0]						,						dtype=target.dtype  )
           UpperCamelCase__    :   Optional[int]					=				tf.stack([r, target]						,						1  )
           return tf.gather_nd(_A						,						_A  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=True						,						__SCREAMING_SNAKE_CASE=False  )  ->   List[str]:




           """simple docstring"""







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

                       if self.div_val == 1:
                             UpperCamelCase__    :   List[Any]					=				self.out_layers[0][0][l_idx:r_idx]
                             UpperCamelCase__    :   int					=				self.out_layers[0][1][l_idx:r_idx]
                       else:
                             UpperCamelCase__    :   List[str]					=				self.out_layers[i][0]
                             UpperCamelCase__    :   Any					=				self.out_layers[i][1]

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

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

           if target is not None:
                 if return_mean:
                       UpperCamelCase__    :   int					=				tf.reduce_mean(_A  )
                 # Add the training-time loss value to the layer using `self.add_loss()`.
                 self.add_loss(_A  )

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

           return out




from io import BytesIO
from typing import List, Union

import requests

from ..utils import add_end_docstrings, is_decord_available, is_torch_available, logging, requires_backends
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_decord_available():
    import numpy as np
    from decord import VideoReader


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

lowerCamelCase      =logging.get_logger(__name__)




@add_end_docstrings(UpperCamelCase_ )
class 				_lowerCamelCase (      UpperCamelCase_ ):





     """simple docstring"""







     def __init__(							self						,						*__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )  ->   Dict:




           """simple docstring"""







           super().__init__(*__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )
           requires_backends(self						,						'''decord'''  )
           self.check_model_type(__SCREAMING_SNAKE_CASE  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE=None						,						__SCREAMING_SNAKE_CASE=None						,						__SCREAMING_SNAKE_CASE=None  )  ->   Optional[Any]:




           """simple docstring"""







           UpperCamelCase__    :   List[str]					=				{}
           if frame_sampling_rate is not None:
                 UpperCamelCase__    :   Tuple					=				frame_sampling_rate
           if num_frames is not None:
                 UpperCamelCase__    :   str					=				num_frames

           UpperCamelCase__    :   List[str]					=				{}
           if top_k is not None:
                 UpperCamelCase__    :   List[Any]					=				top_k
           return preprocess_params, {}, postprocess_params






     def __call__(							self						,						__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )  ->   Tuple:




           """simple docstring"""







           return super().__call__(__SCREAMING_SNAKE_CASE						,						**__SCREAMING_SNAKE_CASE  )






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=None						,						__SCREAMING_SNAKE_CASE=1  )  ->   Optional[Any]:




           """simple docstring"""







           if num_frames is None:
                 UpperCamelCase__    :   Optional[Any]					=				self.model.config.num_frames

           if video.startswith('''http://'''  ) or video.startswith('''https://'''  ):
                 UpperCamelCase__    :   str					=				BytesIO(requests.get(__SCREAMING_SNAKE_CASE  ).content  )

           UpperCamelCase__    :   Tuple					=				VideoReader(__SCREAMING_SNAKE_CASE  )
           videoreader.seek(0  )

           UpperCamelCase__    :   Optional[int]					=				0
           UpperCamelCase__    :   str					=				num_frames * frame_sampling_rate - 1
           UpperCamelCase__    :   List[Any]					=				np.linspace(__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE						,						num=__SCREAMING_SNAKE_CASE						,						dtype=np.intaa  )

           UpperCamelCase__    :   str					=				videoreader.get_batch(__SCREAMING_SNAKE_CASE  ).asnumpy()
           UpperCamelCase__    :   Tuple					=				list(__SCREAMING_SNAKE_CASE  )

           UpperCamelCase__    :   Dict					=				self.image_processor(__SCREAMING_SNAKE_CASE						,						return_tensors=self.framework  )
           return model_inputs






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE  )  ->   Union[str, Any]:




           """simple docstring"""







           UpperCamelCase__    :   Tuple					=				self.model(**__SCREAMING_SNAKE_CASE  )
           return model_outputs






     def    __SCREAMING_SNAKE_CASE			(							self						,						__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE=5  )  ->   str:




           """simple docstring"""







           if top_k > self.model.config.num_labels:
                 UpperCamelCase__    :   Dict					=				self.model.config.num_labels

           if self.framework == "pt":
                 UpperCamelCase__    :   Any					=				model_outputs.logits.softmax(-1  )[0]
                 UpperCamelCase__				,UpperCamelCase__    :   List[str]					=				probs.topk(__SCREAMING_SNAKE_CASE  )
           else:
                 raise ValueError(F'''Unsupported framework: {self.framework}'''  )

           UpperCamelCase__    :   Any					=				scores.tolist()
           UpperCamelCase__    :   Any					=				ids.tolist()
           return [{"score": score, "label": self.model.config.idalabel[_id]} for score, _id in zip(__SCREAMING_SNAKE_CASE						,						__SCREAMING_SNAKE_CASE  )]


