class ImagesKwargs(TypedDict, total=False):
    do_resize: Optional[bool]
    size: Optional[dict[str, int]]
    size_divisor: Optional[int]
    crop_size: Optional[Dict[str, int]]
    resample: Optional[Union['PILImageResampling', int]]
    do_rescale: Optional[bool]
    rescale_factor: Optional[float]
    do_normalize: Optional[bool]
    image_mean: Optional[Union[float, list[float]]]
    image_std: Optional[Union[float, list[float]]]
    do_pad: Optional[bool]
    pad_size: Optional[dict[str, int]]
    do_center_crop: Optional[bool]
    data_format: Optional[ChannelDimension]
    input_data_format: Optional[Union[str, ChannelDimension]]
    device: Optional[str]

Keyword arguments for image processing. For extended documentation, check the appropriate ImageProcessor
class methods and docstrings.

Attributes:
    do_resize (`bool`, *optional*):
        Whether to resize the image.
    size (`Dict[str, int]`, *optional*):
        Resize the shorter side of the input to `size["shortest_edge"]`.
    size_divisor (`int`, *optional*):
        The size by which to make sure both the height and width can be divided.
    crop_size (`Dict[str, int]`, *optional*):
        Desired output size when applying center-cropping.
    resample (`PILImageResampling`, *optional*):
        Resampling filter to use if resizing the image.
    do_rescale (`bool`, *optional*):
        Whether to rescale the image by the specified scale `rescale_factor`.
    rescale_factor (`int` or `float`, *optional*):
        Scale factor to use if rescaling the image.
    do_normalize (`bool`, *optional*):
        Whether to normalize the image.
    image_mean (`float` or `List[float]`, *optional*):
        Mean to use if normalizing the image.
    image_std (`float` or `List[float]`, *optional*):
        Standard deviation to use if normalizing the image.
    do_pad (`bool`, *optional*):
        Whether to pad the image to the `(max_height, max_width)` of the images in the batch.
    pad_size (`Dict[str, int]`, *optional*):
        The size `{"height": int, "width" int}` to pad the images to.
    do_center_crop (`bool`, *optional*):
        Whether to center crop the image.
    data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format for the output image.
    input_data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format for the input image.
    device (`str`, *optional*):
        The device to use for processing (e.g. "cpu", "cuda"), only relevant for fast image processing.


def _create_and_save_tf1_conv_model(self, saved_model_path: str, signature_key: str, tags: Collection[str], input_key: str, output_key: str, *, input_shape: Sequence[int]=(1, 3, 4, 3), filter_shape: Sequence[int]=(2, 3, 3, 2), use_variable: bool=False) -> core.Tensor:
    with ops.Graph().as_default(), session.Session() as sess:
        in_placeholder, output_tensor = self._create_simple_tf1_conv_model(input_shape=input_shape, filter_shape=filter_shape, use_variable_for_filter=use_variable)
        if use_variable:
            sess.run(variables.global_variables_initializer())
        self._save_tf1_model(sess, saved_model_path, signature_key, tags, inputs={input_key: in_placeholder}, outputs={output_key: output_tensor})
    return in_placeholder

Creates and saves a simple convolution model.

This is intended to be used for TF1 (graph mode) tests.

Args:
  saved_model_path: Directory to save the model.
  signature_key: The key to the SignatureDef that inputs & outputs
    correspond to.
  tags: Set of tags associated with the model.
  input_key: The key to the input tensor.
  output_key: The key to the output tensor.
  input_shape: Shape of the input tensor.
  filter_shape: Shape of the filter.
  use_variable: Setting this to `True` makes the filter for the conv
    operation a `tf.Variable`.

Returns:
  in_placeholder: The placeholder tensor used as an input to the model.


def forward(self, input_points: Optional[Tuple[torch.Tensor, torch.Tensor]], input_labels: Optional[torch.Tensor], input_boxes: Optional[torch.Tensor], input_masks: Optional[torch.Tensor]) -> Tuple[torch.Tensor, torch.Tensor]:
    sparse_embeddings = None
    batch_size = 1
    target_device = self.shared_embedding.positional_embedding.device
    if input_points is not None:
        batch_size, point_batch_size = input_points.shape[:2]
        if input_labels is None:
            raise ValueError('If points are provided, labels must also be provided.')
        point_embeddings = self._embed_points(input_points, input_labels, pad=input_boxes is None)
        sparse_embeddings = point_embeddings
    if input_boxes is not None:
        batch_size = input_boxes.shape[0]
        box_embeddings = self._embed_boxes(input_boxes)
        if sparse_embeddings is None:
            sparse_embeddings = box_embeddings
        else:
            sparse_embeddings = torch.cat([sparse_embeddings, box_embeddings], dim=2)
    if input_masks is not None:
        dense_embeddings = self.mask_embed(input_masks)
    else:
        dense_embeddings = self.no_mask_embed.weight.reshape(1, -1, 1, 1).expand(batch_size, -1, self.image_embedding_size[0], self.image_embedding_size[1])
    if sparse_embeddings is None:
        sparse_embeddings = torch.zeros((batch_size, 1, 1, self.hidden_size), device=target_device)
    return (sparse_embeddings, dense_embeddings)

Embeds different types of prompts, returning both sparse and dense embeddings.

Args:
    points (`torch.Tensor`, *optional*):
        point coordinates and labels to embed.
    boxes (`torch.Tensor`, *optional*):
        boxes to embed
    masks (`torch.Tensor`, *optional*):
        masks to embed


def get_output_batch_type(self, input_element_type) -> typing.Optional[typing.Union[TypeConstraint, type]]:
    output_batch_type = None
    if self._process_defined and self._process_yields_batches:
        output_batch_type = self._get_element_type_from_return_annotation(self.process, input_element_type)
    if self._process_batch_defined and (not self._process_batch_yields_elements):
        process_batch_type = self._get_element_type_from_return_annotation(self.process_batch, self._get_input_batch_type_normalized(input_element_type))
        if output_batch_type is not None and (not process_batch_type == output_batch_type):
            raise TypeError(f'DoFn {self!r} yields batches from both process and process_batch, but they produce different types:\n process: {output_batch_type}\n process_batch: {process_batch_type!r}')
        output_batch_type = process_batch_type
    return output_batch_type

Determine the batch type produced by this DoFn's ``process_batch``
implementation and/or its ``process`` implementation with
``@yields_batch``.

The default implementation of this method observes the return type
annotations on ``process_batch`` and/or ``process``.  A Batched DoFn may
override this method if a dynamic approach is required.

Args:
  input_element_type: The **element type** of the input PCollection this
    DoFn is being applied to.

Returns:
  ``None`` if this DoFn will never yield batches, else a Beam typehint or
  a native Python typehint.


def identity(x, name=None):
    return array_ops.identity(x, name=name)

Returns a tensor with the same content as the input tensor.

Args:
    x: The input tensor.
    name: String, name for the variable to create.

Returns:
    A tensor of the same shape, type and content.


def parse(self, sentence: str) -> typing.List[str]:
    if sentence == '':
        return []
    chunks = [sentence[0]]
    base_score = -sum((sum(g.values()) for g in self.model.values())) * 0.5
    for i in range(1, len(sentence)):
        score = base_score
        if i > 2:
            score += self.model.get('UW1', {}).get(sentence[i - 3], 0)
        if i > 1:
            score += self.model.get('UW2', {}).get(sentence[i - 2], 0)
        score += self.model.get('UW3', {}).get(sentence[i - 1], 0)
        score += self.model.get('UW4', {}).get(sentence[i], 0)
        if i + 1 < len(sentence):
            score += self.model.get('UW5', {}).get(sentence[i + 1], 0)
        if i + 2 < len(sentence):
            score += self.model.get('UW6', {}).get(sentence[i + 2], 0)
        if i > 1:
            score += self.model.get('BW1', {}).get(sentence[i - 2:i], 0)
        score += self.model.get('BW2', {}).get(sentence[i - 1:i + 1], 0)
        if i + 1 < len(sentence):
            score += self.model.get('BW3', {}).get(sentence[i:i + 2], 0)
        if i > 2:
            score += self.model.get('TW1', {}).get(sentence[i - 3:i], 0)
        if i > 1:
            score += self.model.get('TW2', {}).get(sentence[i - 2:i + 1], 0)
        if i + 1 < len(sentence):
            score += self.model.get('TW3', {}).get(sentence[i - 1:i + 2], 0)
        if i + 2 < len(sentence):
            score += self.model.get('TW4', {}).get(sentence[i:i + 3], 0)
        if score > 0:
            chunks.append(sentence[i])
        else:
            chunks[-1] += sentence[i]
    return chunks

Parses the input sentence and returns a list of semantic chunks.

Args:
  sentence (str): An input sentence.

Returns:
  A list of semantic chunks (List[str]).


def get_numpy_to_framework_fn(arr) -> Callable:
    if isinstance(arr, np.ndarray):
        return np.array
    if is_tf_available() and is_tf_tensor(arr):
        import tensorflow as tf
        return tf.convert_to_tensor
    if is_torch_available() and is_torch_tensor(arr):
        import torch
        return torch.tensor
    if is_flax_available() and is_jax_tensor(arr):
        import jax.numpy as jnp
        return jnp.array
    raise ValueError(f'Cannot convert arrays of type {type(arr)}')

Returns a function that converts a numpy array to the framework of the input array.

Args:
    arr (`np.ndarray`): The array to convert.


def get_header_from_ops_and_kernels(ops_and_kernels, include_all_ops_and_kernels):
    ops_and_kernels = sorted(ops_and_kernels)
    ops = set((op for op, _ in ops_and_kernels))
    result_list = []

    def append(s):
        result_list.append(s)
    _, script_name = os.path.split(sys.argv[0])
    append('// This file was autogenerated by %s' % script_name)
    append('#ifndef OPS_TO_REGISTER')
    append('#define OPS_TO_REGISTER')
    if include_all_ops_and_kernels:
        append('#define SHOULD_REGISTER_OP(op) true')
        append('#define SHOULD_REGISTER_OP_KERNEL(clz) true')
        append('#define SHOULD_REGISTER_OP_GRADIENT true')
    else:
        line = "\n    namespace {\n      constexpr const char* skip(const char* x) {\n        return (*x) ? (*x == ' ' ? skip(x + 1) : x) : x;\n      }\n\n      constexpr bool isequal(const char* x, const char* y) {\n        return (*skip(x) && *skip(y))\n                   ? (*skip(x) == *skip(y) && isequal(skip(x) + 1, skip(y) + 1))\n                   : (!*skip(x) && !*skip(y));\n      }\n\n      template<int N>\n      struct find_in {\n        static constexpr bool f(const char* x, const char* const y[N]) {\n          return isequal(x, y[0]) || find_in<N - 1>::f(x, y + 1);\n        }\n      };\n\n      template<>\n      struct find_in<0> {\n        static constexpr bool f(const char* x, const char* const y[]) {\n          return false;\n        }\n      };\n    }  // end namespace\n    "
        line += 'constexpr const char* kNecessaryOpKernelClasses[] = {\n'
        for _, kernel_class in ops_and_kernels:
            if kernel_class is None:
                continue
            line += '"%s",\n' % kernel_class
        line += '};'
        append(line)
        append('#define SHOULD_REGISTER_OP_KERNEL(clz) (find_in<sizeof(kNecessaryOpKernelClasses) / sizeof(*kNecessaryOpKernelClasses)>::f(clz, kNecessaryOpKernelClasses))')
        append('')
        append('constexpr inline bool ShouldRegisterOp(const char op[]) {')
        append('  return false')
        for op in sorted(ops):
            append('     || isequal(op, "%s")' % op)
        append('  ;')
        append('}')
        append('#define SHOULD_REGISTER_OP(op) ShouldRegisterOp(op)')
        append('')
        append('#define SHOULD_REGISTER_OP_GRADIENT ' + ('true' if 'SymbolicGradient' in ops else 'false'))
    append('#endif')
    return '\n'.join(result_list)

Returns a header for use with tensorflow SELECTIVE_REGISTRATION.

Args:
  ops_and_kernels: a set of (op_name, kernel_class_name) pairs to include.
  include_all_ops_and_kernels: if True, ops_and_kernels is ignored and all op
    kernels are included.

Returns:
  the string of the header that should be written as ops_to_register.h.


def segment_diff(x, segment_ids, order=1, exclusive=False, dtype=None, name=None):
    with tf.compat.v1.name_scope(name, default_name='segment_diff', values=[x]):
        x = tf.convert_to_tensor(x, dtype=dtype)
        raw_diffs = diff_ops.diff(x, order=order, exclusive=exclusive)
        if segment_ids is None:
            return raw_diffs
        has_segment_changed = tf.concat([[False], tf.not_equal(segment_ids[1:] - segment_ids[:-1], 0)], axis=0)
        segment_start_index = tf.cast(tf.where(has_segment_changed), dtype=tf.int32)
        segment_end_index = tf.concat([tf.reshape(segment_start_index, [-1])[1:], [tf.size(segment_ids)]], axis=0)
        segment_end_index = tf.reshape(segment_end_index, [-1, 1])
        fix_indices = segment_start_index + tf.range(order, dtype=segment_start_index.dtype)
        in_bounds = tf.where(fix_indices < segment_end_index)
        fix_indices = tf.reshape(tf.gather_nd(fix_indices, in_bounds), [-1, 1])
        needs_fix = tf.scatter_nd(fix_indices, tf.reshape(tf.ones_like(fix_indices, dtype=tf.int32), [-1]), shape=tf.shape(x))
        needs_fix = tf.cast(needs_fix, dtype=tf.bool)
        if not exclusive:
            return tf.where(needs_fix, x, raw_diffs)
        return tf.boolean_mask(raw_diffs, tf.logical_not(needs_fix[order:]))

Computes difference of successive elements in a segment.

For a complete description of segment_* ops see documentation of
`tf.segment_max`. This op extends the `diff` functionality to segmented
inputs.

The behaviour of this op is the same as that of the op `diff` within each
segment. The result is effectively a concatenation of the results of `diff`
applied to each segment.

#### Example

```python
  x = tf.constant([2, 5, 1, 7, 9] + [32, 10, 12, 3] + [4, 8, 5])
  segments = tf.constant([0, 0, 0, 0, 0] + [1, 1, 1, 1] + [2, 2, 2])
  # First order diff. Expected result: [3, -4, 6, 2, -22, 2, -9, 4, -3]
  dx1 = segment_diff(
      x, segment_ids=segments, order=1, exclusive=True)
  # Non-exclusive, second order diff.
  # Expected result: [2, 5, -1, 2, 8, 32, 10, -20, -7, 4, 8, 1]
  dx2 = segment_diff(
      x, segment_ids=segments, order=2, exclusive=False)
```

Args:
  x: A rank 1 `Tensor` of any dtype for which arithmetic operations are
    permitted.
  segment_ids: A `Tensor`. Must be one of the following types: int32, int64. A
    1-D tensor whose size is equal to the size of `x`. Values should be sorted
    and can be repeated.
  order: Positive Python int. The order of the difference to compute. `order =
    1` corresponds to the difference between successive elements.
    Default value: 1
  exclusive: Python bool. See description above.
    Default value: False
  dtype: Optional `tf.Dtype`. If supplied, the dtype for `x` to use when
    converting to `Tensor`.
    Default value: None which maps to the default dtype inferred by TF.
  name: Python `str` name prefixed to Ops created by this class.
    Default value: None which is mapped to the default name 'segment_diff'.

Returns:
  diffs: A `Tensor` of the same dtype as `x`. Assuming that each segment is
    of length greater than or equal to order, if `exclusive` is True,
    then the size is `n-order*k` where `n` is the size of x,
    `k` is the number of different segment ids supplied if `segment_ids` is
    not None or 1 if `segment_ids` is None. If any of the segments is of
    length less than the order, then the size is:
    `n-sum(min(order, length(segment_j)), j)` where the sum is over segments.
    If `exclusive` is False, then the size is `n`.


class Context(object):

    def __init__(self):
        self.g = None
        self.left = None
        self.right = None
        self.labels = None
        self.num_matched = 0
        self.s = None
        self.t = None
        self.matches = None
        self.slack = None
        self.slackx = None
        self.prev = None

    def MaxBipartiteMatching(self, graph):
        """Find a maximum matching for a bipartite graph.

    This is O(n^3) implementation of the Hungarian method for complete bipartite
    matching problems.

    Args:
      graph: A networkx graph object, assumed to be bipartite.
    Returns:
      A dictionary keyed on node names in left to node names in right.
    """
        self.g = nx.Graph(graph)
        self.left = set((n for n, d in self.g.nodes(data=True) if not d['bipartite']))
        self.right = set(self.g) - self.left
        self.num_matched = 0
        self.s = set()
        self.t = set()
        self.matches = {}
        self.slack = {}
        self.slackx = {}
        self.prev = {}
        self.labels = {}
        for x in self.left:
            self.labels[x] = max([val['weight'] for val in self.g[x].values()])
        for y in self.right:
            self.labels[y] = 0
        while self.num_matched != len(self.left):
            self._Augment()
        ret = {}
        for k in self.left:
            ret[k] = self.matches[k]
        return ret

    def _Augment(self):
        """Find an augmenting path starting from an unmatched node in |left|.

    Start with a root node in |left| and attempt to find an augmenting path
    starting from |root|. In order for a path to be augmenting, each edge in the
    path must have: weight(x, y) == labels[x] + labels[y]. The set of all edges
    which have this property is known as the "equality subgraph" for the current
    vertex labeling.

    In addition, an augmenting path must start with an unmatched edge and end
    with an unmatched edge; augmenting the path flips the matched-ness of each
    edge, so that the total number of matched edges increases by 1.

    If an augmenting path does not exist, we update the labels of all nodes in
    S and T to force new edges into the equality subgraph. Eventually, an
    augmenting path will be generated this way.
    """
        self.s = set()
        self.t = set()
        self.prev = {}
        queue = collections.deque()
        root = list(self.left - set(self.matches.keys()))[0]
        queue.append(root)
        self.s.add(root)
        for y in self.right:
            self.slack[y] = self._CalcSlack(root, y)
            self.slackx[y] = root
        while True:
            path_exists, x, y = self._FindAugmentingPath(queue)
            if path_exists:
                break
            self._UpdateLabels()
            queue.clear()
            path_exists, x, y = self._FindAugmentingEdge(queue)
            if path_exists:
                break
        self._InvertPath(x, y)

    def _FindAugmentingPath(self, queue):
        """Find an augmenting path for the current labeling.

    Perform a BFS to find an augmenting path for the current labeling.

    Args:
      queue: Queue for performing BFS traversal.
    Returns:
      found: True if path was found.
      x: Left vertex of final path edge.
      y: Right vertex of final path edge.
    """
        while queue:
            x = queue.popleft()
            for y in self.right - self.t:
                if not self._InEqualitySubgraph(x, y):
                    continue
                if y not in self.matches:
                    return (True, x, y)
                self.t.add(y)
                queue.append(self.matches[y])
                self._AddToTree(self.matches[y], x)
        return (False, None, None)

    def _FindAugmentingEdge(self, queue):
        """Find a final edge for an augmenting path after updating labels.

    At least one new edge should have been added to the equality subgraph, so
    we check if any new edges will create an augmenting path.

    Args:
      queue: Queue for performing BFS traversal.
    Returns:
      found: True if path was found.
      x: Left vertex of final path edge.
      y: Right vertex of final path edge.
    """
        for y in (v for v in self.right - self.t if self.slack[v] == 0):
            if y not in self.matches:
                return (True, self.slackx[y], y)
            self.t.add(y)
            if self.matches[y] not in self.s:
                queue.append(self.matches[y])
                self._AddToTree(self.matches[y], self.slackx[y])
        return (False, None, None)

    def _InvertPath(self, x, y):
        """Invert the augmenting path whose final edge is (x, y)."""
        self.num_matched += 1
        while True:
            if x in self.matches:
                ty = self.matches[x]
            self.matches[y] = x
            self.matches[x] = y
            if x not in self.prev:
                break
            y = ty
            x = self.prev[x]

    def _UpdateLabels(self):
        """Update labels to expand the equality subgraph.

    We will find the smallest slack value of a vertex in Right - T. The labels
    for vertices in S will decrease by slack, while the vertices in T increase
    by slack. This guarantees at least one vertex in Right will have a slack
    value of 0, thereby adding it to the equality subgraph.
    """
        delta = float('inf')
        for y in self.right - self.t:
            delta = min(delta, self.slack[y])
        for x in self.s:
            self.labels[x] -= delta
        for y in self.t:
            self.labels[y] += delta
        for y in self.right - self.t:
            self.slack[y] -= delta

    def _AddToTree(self, x, prevx):
        """Adds |x| to the current augmenting tree.

    x is a node which has already been matched to a node y in Right (which is
    itself connected to prevx via a non-matching edge in the equality subgraph).
    We indicate prevx comes before x in the tree so we can trace the path later.

    Args:
      x: Node which has already been matched to a node y in right
      prevx: Previous node in Left along the path.
    """
        self.s.add(x)
        self.prev[x] = prevx
        for y in self.right:
            slack = self._CalcSlack(x, y)
            if slack < self.slack[y]:
                self.slack[y] = slack
                self.slackx[y] = x

    def _InEqualitySubgraph(self, x, y):
        """Return True if x and y are in the equality subgraph."""
        return self.g[x][y]['weight'] == self.labels[x] + self.labels[y]

    def _CalcSlack(self, x, y):
        """Calculate the slack for an edge (x, y)."""
        return self.labels[x] + self.labels[y] - self.g[x][y]['weight']

Context object for running the Hungarian algorithm.

Contains algorithm variables.

Attributes:
  g: The complete bipartite graph we will find a maximum matching for.
  left: The set of left nodes.
  right: The set of right nodes.
  labels: Dictionary of vertex names to numeric algorithm label.
  num_matched: Current number of matched edges
  s: Set of left nodes in the augmenting tree
  t: Set of right nodes in the augmenting tree
  matches: Dictionary where matches[n] = m (and matches[m] = n) if n and m are
      currently matched.
  slack: Dictionary keyed on y, where slack[y] = min(for x in S: labels[x] +
      labels[y] - weight(x, y))
  slackx: Dictionary keyed on y, where slackx[y] is the node in right which
      gives y its current slack value.
  prev: Dictionary keyed on a node x in S, where prev[x] is the previous node
      in S along x's path in the augmenting tree.


def Lease(self, request, global_params=None):
    config = self.GetMethodConfig('Lease')
    return self._RunMethod(config, request, global_params=global_params)

Leases a dataflow WorkItem to run.

Args:
  request: (DataflowProjectsLocationsJobsWorkItemsLeaseRequest) input message
  global_params: (StandardQueryParameters, default: None) global arguments
Returns:
  (LeaseWorkItemResponse) The response message.


class ViTMSNConfig(PretrainedConfig):
    model_type = 'vit_msn'

    def __init__(self, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, hidden_act='gelu', hidden_dropout_prob=0.0, attention_probs_dropout_prob=0.0, initializer_range=0.02, layer_norm_eps=1e-06, image_size=224, patch_size=16, num_channels=3, qkv_bias=True, **kwargs):
        super().__init__(**kwargs)
        self.hidden_size = hidden_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.intermediate_size = intermediate_size
        self.hidden_act = hidden_act
        self.hidden_dropout_prob = hidden_dropout_prob
        self.attention_probs_dropout_prob = attention_probs_dropout_prob
        self.initializer_range = initializer_range
        self.layer_norm_eps = layer_norm_eps
        self.image_size = image_size
        self.patch_size = patch_size
        self.num_channels = num_channels
        self.qkv_bias = qkv_bias

This is the configuration class to store the configuration of a [`ViTMSNModel`]. It is used to instantiate an ViT
MSN model according to the specified arguments, defining the model architecture. Instantiating a configuration with
the defaults will yield a similar configuration to that of the ViT
[facebook/vit_msn_base](https://huggingface.co/facebook/vit_msn_base) architecture.

Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
documentation from [`PretrainedConfig`] for more information.

Args:
    hidden_size (`int`, *optional*, defaults to 768):
        Dimensionality of the encoder layers and the pooler layer.
    num_hidden_layers (`int`, *optional*, defaults to 12):
        Number of hidden layers in the Transformer encoder.
    num_attention_heads (`int`, *optional*, defaults to 12):
        Number of attention heads for each attention layer in the Transformer encoder.
    intermediate_size (`int`, *optional*, defaults to 3072):
        Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
    hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
        The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
        `"relu"`, `"selu"` and `"gelu_new"` are supported.
    hidden_dropout_prob (`float`, *optional*, defaults to 0.0):
        The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
    attention_probs_dropout_prob (`float`, *optional*, defaults to 0.0):
        The dropout ratio for the attention probabilities.
    initializer_range (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    layer_norm_eps (`float`, *optional*, defaults to 1e-06):
        The epsilon used by the layer normalization layers.
    image_size (`int`, *optional*, defaults to 224):
        The size (resolution) of each image.
    patch_size (`int`, *optional*, defaults to 16):
        The size (resolution) of each patch.
    num_channels (`int`, *optional*, defaults to 3):
        The number of input channels.
    qkv_bias (`bool`, *optional*, defaults to `True`):
        Whether to add a bias to the queries, keys and values.

Example:

```python
>>> from transformers import ViTMSNModel, ViTMSNConfig

>>> # Initializing a ViT MSN vit-msn-base style configuration
>>> configuration = ViTConfig()

>>> # Initializing a model from the vit-msn-base style configuration
>>> model = ViTMSNModel(configuration)

>>> # Accessing the model configuration
>>> configuration = model.config
```


def _mark_func_graph_as_unsaveable(graph, learning_phase):
    if graph.building_function and is_placeholder(learning_phase):
        graph.mark_as_unsaveable('The keras learning phase placeholder was used inside a function. Exporting placeholders is not supported when saving out a SavedModel. Please call `tf.keras.backend.set_learning_phase(0)` in the function to set the learning phase to a constant value.')

Mark func graph as unsaveable due to use of symbolic keras learning phase.

Functions that capture the symbolic learning phase cannot be exported to
SavedModel. Mark the funcgraph as unsaveable, so that an error will be raised
if it is exported.

Args:
  graph: Graph or FuncGraph object.
  learning_phase: Learning phase placeholder or int defined in the graph.


def extract_entities_with_patch_indices(text):
    pattern = '(?:(<phrase>([^<]+)</phrase>))?<object>((?:<patch_index_\\d+><patch_index_\\d+></delimiter_of_multi_objects/>)*<patch_index_\\d+><patch_index_\\d+>)</object>'
    matches = re.finditer(pattern, text)
    entities_with_patch_indices = []
    for match in matches:
        span = match.span(2)
        phrase_tag, phrase, match_content = match.groups()
        if not phrase_tag:
            phrase = None
            span = (match.span(0)[0], match.span(0)[0])
        patch_index_pairs = match_content.split('</delimiter_of_multi_objects/>')
        entity_bboxes = []
        for pair in patch_index_pairs:
            x = re.search('<patch_index_(\\d+)>', pair)
            y = re.search('<patch_index_(\\d+)>', pair[1:])
            if x and y:
                if phrase:
                    entity_bboxes.append((int(x.group(1)), int(y.group(1))))
                else:
                    entity_bboxes.append((int(x.group(1)), int(y.group(1))))
        if phrase:
            entities_with_patch_indices.append((phrase, span, entity_bboxes))
        else:
            for bbox in entity_bboxes:
                entity = f'<patch_index_{bbox[0]}><patch_index_{bbox[1]}>'
                entities_with_patch_indices.append((entity, span, [bbox]))
    return entities_with_patch_indices

Extract entities contained in `text`. The bounding bboxes is given in the form of patch indices.

This functioin is only intended to be used within `clean_text_and_extract_entities_with_bboxes` where further
processing happens, including converting to normalized coordinates and whitespace character cleaning up.

Examples:

```python
>>> text = "<grounding> An image of<phrase> a snowman</phrase><object><patch_index_0044><patch_index_0863></object> warming himself by<phrase> a fire</phrase><object><patch_index_0005><patch_index_0911></object>."
>>> entities = extract_entities_with_patch_indices(text)
>>> entities
[(' a snowman', (31, 41), [(44, 863)]), (' a fire', (130, 137), [(5, 911)])]
```


def __init__(self, inputs, num_clusters, initial_clusters, distance_metric, random_seed, kmeans_plus_plus_num_retries, kmc2_chain_length, cluster_centers, cluster_centers_updated, cluster_centers_initialized):
    self._inputs = inputs
    self._num_clusters = num_clusters
    self._initial_clusters = initial_clusters
    self._distance_metric = distance_metric
    self._seed = random_seed
    self._kmeans_plus_plus_num_retries = kmeans_plus_plus_num_retries
    self._kmc2_chain_length = kmc2_chain_length
    self._cluster_centers = cluster_centers
    self._cluster_centers_updated = cluster_centers_updated
    self._cluster_centers_initialized = cluster_centers_initialized
    self._num_selected = array_ops.shape(self._cluster_centers)[0]
    self._num_remaining = self._num_clusters - self._num_selected
    self._num_data = math_ops.add_n([array_ops.shape(i)[0] for i in self._inputs])

Creates an op factory.

Args:
  inputs: See KMeans constructor.
  num_clusters: An integer Tensor providing the number of clusters.
  initial_clusters: See KMeans constructor.
  distance_metric: See KMeans constructor.
  random_seed: See KMeans constructor.
  kmeans_plus_plus_num_retries: See KMeans constructor.
  kmc2_chain_length: See KMeans constructor.
  cluster_centers: The TF variable holding the initial centers. It may
    already contain some centers when the op is executed.
  cluster_centers_updated: A second TF variable to hold a copy of the
    initial centers, used for full-batch mode. In mini-batch mode,
    cluster_centers_updated is the same variable as cluster_centers.
  cluster_centers_initialized: A boolean TF variable that will be set to
    true when all the initial centers have been chosen.


def build(self, names_to_saveables, reshape=False, sharded=False, max_to_keep=5, keep_checkpoint_every_n_hours=10000.0, name=None, restore_sequentially=False, filename='model'):
    return self._build_internal(names_to_saveables=names_to_saveables, reshape=reshape, sharded=sharded, max_to_keep=max_to_keep, keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours, name=name, restore_sequentially=restore_sequentially, filename=filename)

Builds save/restore graph nodes or runs save/restore in eager mode.

Args:
  names_to_saveables: A dictionary mapping name to a Variable or
    SaveableObject. Each name will be associated with the corresponding
    variable in the checkpoint.
  reshape: If True, allow restoring parameters from a checkpoint that where
    the parameters have a different shape.  This is only needed when you try
    to restore from a Dist-Belief checkpoint, and only some times.
  sharded: If True, shard the checkpoints, one per device that has Variable
    nodes.
  max_to_keep: Maximum number of checkpoints to keep.  As new checkpoints
    are created, old ones are deleted.  If None or 0, no checkpoints are
    deleted from the filesystem but only the last one is kept in the
    `checkpoint` file.  Presently the number is only roughly enforced.  For
    example in case of restarts more than max_to_keep checkpoints may be
    kept.
  keep_checkpoint_every_n_hours: How often checkpoints should be kept.
    Defaults to 10,000 hours.
  name: String.  Optional name to use as a prefix when adding operations.
  restore_sequentially: A Bool, which if true, causes restore of different
    variables to happen sequentially within each device.
  filename: If known at graph construction time, filename used for variable
    loading/saving. If None, then the default name "model" will be used.

Returns:
  A SaverDef proto.

Raises:
  TypeError: If 'names_to_saveables' is not a dictionary mapping string
    keys to variable Tensors.
  ValueError: If any of the keys or values in 'names_to_saveables' is not
    unique.


def maybe_promote_tensors(*tensors, force_same_dtype=False):
    if ops.is_auto_dtype_conversion_enabled():
        return tensors
    if not tensors:
        return tensors
    if not ops.is_numpy_style_type_promotion():
        if not force_same_dtype:
            return tensors
        promoted_tensors = []
        promoted_tensors.append(tensors[0])
        dtype = tensors[0].dtype.base_dtype
        for tensor in tensors[1:]:
            promoted_tensors.append(ops.convert_to_tensor(tensor, dtype, name='x'))
        return promoted_tensors
    result_type = np_dtypes._result_type(*[_maybe_get_dtype(x) for x in nest.flatten(tensors)])

    def _promote_or_cast(x):
        if isinstance(x, tensor_lib.Tensor):
            x = gen_math_ops.cast(x, result_type)
        else:
            x = ops.convert_to_tensor(x, result_type)
        return x
    return [_promote_or_cast(x) for x in tensors]

Promotes tensors if numpy style promotion is enabled.

This function promotes `tensors` according to numpy promotion rules
if numpy style promotion is enabled.  Otherwise, if
`force_same_dtype` is `True`, it force-casts `tensors[1:]` to
`tensor[0]`'s dtype. Note that this force-cast can be problematic.
For example, when some `tensors[1:]` elements can be silently
downcasted.

Args:
  *tensors: the list of tensors to promote.
  force_same_dtype: bool (optional, default to `False`). When numpy
    style promotion is disabled and `force_same_dtype` is `True`,
    this function will force-casts `tensors[1:]` to `tensor[0]`'s
    dtype (which could be problematic).

Returns:
  The promoted list of tensors.


class _LazyLoadAllMappings(OrderedDict[str, str]):

    def __init__(self, mapping):
        self._mapping = mapping
        self._initialized = False
        self._data = {}

    def _initialize(self):
        if self._initialized:
            return
        for model_type, map_name in self._mapping.items():
            module_name = model_type_to_module_name(model_type)
            module = importlib.import_module(f'.{module_name}', 'transformers.models')
            mapping = getattr(module, map_name)
            self._data.update(mapping)
        self._initialized = True

    def __getitem__(self, key):
        self._initialize()
        return self._data[key]

    def keys(self) -> KeysView[str]:
        self._initialize()
        return self._data.keys()

    def values(self) -> ValuesView[str]:
        self._initialize()
        return self._data.values()

    def items(self) -> KeysView[str]:
        self._initialize()
        return self._data.keys()

    def __iter__(self) -> Iterator[str]:
        self._initialize()
        return iter(self._data)

    def __contains__(self, item: object) -> bool:
        self._initialize()
        return item in self._data

A mapping that will load all pairs of key values at the first access (either by indexing, requestions keys, values,
etc.)

Args:
    mapping: The mapping to load.


def show_path(from_op, tensors, sources):
    if isinstance(from_op, tensor_lib.Tensor):
        from_op = from_op.op
    if not isinstance(tensors, list):
        tensors = [tensors]
    final_ops = [_as_operation(tensor) for tensor in tensors]
    visited_ops = set((x.op for x in sources))
    ops_to_visit = list(final_ops)
    some_op_output = {}
    while ops_to_visit:
        op = ops_to_visit.pop()
        if op in visited_ops:
            continue
        visited_ops.add(op)
        if op == from_op:
            path_op = op
            path = [path_op]
            while path_op not in final_ops:
                path_op = some_op_output[path_op]
                path.append(path_op)
            return ' <- '.join(('%s (%s)' % (x.name, x.type) for x in reversed(path)))
        else:
            for inp in graph_inputs(op):
                if inp not in visited_ops and inp not in sources:
                    some_op_output[inp] = op
                    ops_to_visit.append(inp)
    return '??'

Find one path from `from_op` to any of `tensors`, ignoring `sources`.

Args:
  from_op: A `tf.Operation`.
  tensors: A `tf.Operation`, a `tf.Tensor`, or a list thereof.
  sources: A list of `tf.Tensor`.

Returns:
  A python string containing the path, or "??" if none is found.


def create_module_to_test_map(reverse_map: Optional[Dict[str, List[str]]]=None, filter_models: bool=False) -> Dict[str, List[str]]:
    if reverse_map is None:
        reverse_map = create_reverse_dependency_map()

    def is_test(fname):
        if fname.startswith('tests'):
            return True
        if fname.startswith('examples') and fname.split(os.path.sep)[-1].startswith('test'):
            return True
        return False
    test_map = {module: [f for f in deps if is_test(f)] for module, deps in reverse_map.items()}
    if not filter_models:
        return test_map
    num_model_tests = len(list(PATH_TO_TESTS.glob('models/*')))

    def has_many_models(tests):
        model_tests = {Path(t).parts[2] for t in tests if t.startswith('tests/models/')}
        return len(model_tests) > num_model_tests // 2

    def filter_tests(tests, module=''):
        filtered_tests = []
        for t in tests:
            if not t.startswith('tests/models/') or Path(t).parts[2] in IMPORTANT_MODELS or '/'.join(Path(t).parts[1:3]) in module:
                filtered_tests += [t]
        return filtered_tests
    return {module: filter_tests(tests, module=module) if has_many_models(tests) else tests for module, tests in test_map.items()}

Extract the tests from the reverse_dependency_map and potentially filters the model tests.

Args:
    reverse_map (`Dict[str, List[str]]`, *optional*):
        The reverse dependency map as created by `create_reverse_dependency_map`. Will default to the result of
        that function if not provided.
    filter_models (`bool`, *optional*, defaults to `False`):
        Whether or not to filter model tests to only include core models if a file impacts a lot of models.

Returns:
    `Dict[str, List[str]]`: A dictionary that maps each file to the tests to execute if that file was modified.


def with_dependencies(dependencies, output_tensor, name=None):
    if context.executing_eagerly():
        return output_tensor
    with ops.name_scope(name, 'control_dependency', list(dependencies) + [output_tensor]) as name:
        with ops.colocate_with(output_tensor):
            with ops.control_dependencies(dependencies):
                output_tensor = ops.convert_to_tensor_or_composite(output_tensor)
                if isinstance(output_tensor, indexed_slices.IndexedSlices):
                    return indexed_slices.IndexedSlices(_Identity(output_tensor.values, name=name), output_tensor.indices, output_tensor.dense_shape)
                else:
                    return _Identity(output_tensor, name=name)

Produces the content of `output_tensor` only after `dependencies`.

In some cases, a user may want the output of an operation to be
consumed externally only after some other dependencies have run
first. This function ensures returns `output_tensor`, but only after all
operations in `dependencies` have run. Note that this means that there is
no guarantee that `output_tensor` will be evaluated after any `dependencies`
have run.

See also `tf.tuple` and `tf.group`.

Args:
  dependencies: Iterable of operations to run before this op finishes.
  output_tensor: A `Tensor` or `IndexedSlices` that will be returned.
  name: (Optional) A name for this operation.

Returns:
  Same as `output_tensor`.

Raises:
  TypeError: if `output_tensor` is not a `Tensor` or `IndexedSlices`.


def set_error_message(self, error_message):
    self._empty = False
    self.error_message = error_message

Sets an error message on an instrumentation block.

This method is used exclusively to indicate that a test method failed
to complete, which is usually cause by a crash of some sort such that
the test method is marked as error instead of ignored.

Args:
  error_message: string, an error message to be added to the
    TestResultRecord to explain that something wrong happened.


def add_meta_graph_and_variables(self, sess, tags, signature_def_map=None, assets_list=None, clear_devices=False, init_op=None, train_op=None, strip_default_attrs=False, saver=None):
    if self._has_saved_variables:
        raise AssertionError('Graph state including variables and assets has already been saved. Please invoke `add_meta_graph()` instead.')
    signature_def_map = signature_def_map or {}
    self._validate_signature_def_map(signature_def_map)
    _add_op_to_signature_def_map(signature_def_map, init_op, constants.INIT_OP_SIGNATURE_KEY)
    _add_op_to_signature_def_map(signature_def_map, train_op, constants.TRAIN_OP_SIGNATURE_KEY)
    path_helpers.get_or_create_variables_dir(self._export_dir)
    variables_path = path_helpers.get_variables_path(self._export_dir)
    saver = self._maybe_create_saver(saver)
    saver.save(sess, variables_path, write_meta_graph=False, write_state=False)
    meta_graph_def = saver.export_meta_graph(clear_devices=clear_devices, strip_default_attrs=strip_default_attrs)
    self._save_and_write_assets(meta_graph_def, assets_list)
    self._tag_and_add_meta_graph(meta_graph_def, tags, signature_def_map)
    self._has_saved_variables = True

Adds the current meta graph to the SavedModel and saves variables.

Creates a Saver to save the variables from the provided session. Exports the
corresponding meta graph def. This function assumes that the variables to be
saved have been initialized. For a given `SavedModelBuilder`, this API must
be called exactly once and for the first meta graph to save. For subsequent
meta graph defs to be added, the `add_meta_graph()` API must be used.

Args:
  sess: The TensorFlow session from which to save the meta graph and
    variables.
  tags: The set of tags with which to save the meta graph.
  signature_def_map: The map of signature def map to add to the meta graph
    def.
  assets_list: Assets to be saved with SavedModel.
  clear_devices: Set to true if the device info on the default graph should
    be cleared.
  init_op: Op or group of ops to execute when the graph is loaded. Note
      that when the init_op is specified it is run after the restore op at
    load-time.
  train_op: Op or group of ops that trains the model when run. This will
    not be run automatically when the graph is loaded, instead saved in
    a SignatureDef accessible through the exported MetaGraph.
  strip_default_attrs: Boolean. If `True`, default-valued attributes will be
    removed from the NodeDefs. For a detailed guide, see
    [Stripping Default-Valued Attributes](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md#stripping-default-valued-attributes).
  saver: An instance of tf.compat.v1.train.Saver that will be used to export the
    metagraph and save variables. If None, a sharded Saver that restores
    all variables will be used.


def get_tensors(graph):
    if not isinstance(graph, ops.Graph):
        raise TypeError('Expected a graph, got: {}'.format(type(graph)))
    ts = []
    for op in graph.get_operations():
        ts += op.outputs
    return ts

get all the tensors which are input or output of an op in the graph.

Args:
  graph: a `tf.Graph`.
Returns:
  A list of `tf.Tensor`.
Raises:
  TypeError: if graph is not a `tf.Graph`.


def __call__(self, images: Union[ImageInput, List[ImageInput], List[List[ImageInput]]]=None, text: Union[TextInput, 'PreTokenizedInput', List[TextInput], List['PreTokenizedInput']]=None, audio=None, videos=None, image_seq_len: Optional[int]=None, **kwargs: Unpack[Idefics3ProcessorKwargs]) -> BatchEncoding:
    if text is None and images is None:
        raise ValueError('You must provide either `text` or `images`.')
    output_kwargs = self._merge_kwargs(Idefics3ProcessorKwargs, tokenizer_init_kwargs=self.tokenizer.init_kwargs, **kwargs)
    image_seq_len = image_seq_len if image_seq_len is not None else self.image_seq_len
    return_mm_token_type_ids = output_kwargs['text_kwargs'].pop('return_mm_token_type_ids', False)
    return_tensors = output_kwargs['text_kwargs'].pop('return_tensors', None)
    n_images_in_text = []
    n_images_in_images = []
    inputs = {}
    if text is not None:
        if isinstance(text, str):
            text = [text]
        elif not isinstance(text, list) and (not isinstance(text[0], str)):
            raise ValueError('Invalid input text. Please provide a string, or a list of strings')
        n_images_in_text = [sample.count(self.image_token) for sample in text]
    if images is not None:
        if is_image_or_image_url(images):
            images = [[images]]
        elif isinstance(images, (list, tuple)) and is_image_or_image_url(images[0]):
            if text is not None:
                if sum(n_images_in_text) != len(images):
                    raise ValueError(f'The total number of {self.image_token} tokens in the prompts should be the same as the number of images passed. Found {sum(n_images_in_text)} {self.image_token} tokens and {len(images)} images.')
                cumsum_images_in_text = [0] + list(accumulate(n_images_in_text))
                images = [images[cumsum_images_in_text[i]:cumsum_images_in_text[i + 1]] for i in range(len(n_images_in_text))]
            else:
                images = [images]
        elif not isinstance(images, (list, tuple)) and (not isinstance(images[0], (list, tuple))) and (not is_image_or_image_url(images[0][0])):
            raise ValueError('Invalid input images. Please provide a single image or a list of images or a list of list of images.')
        n_images_in_images = [len(sample) for sample in images]
        images = [[load_image(im) if is_url(im) else im for im in sample] for sample in images]
        image_inputs = self.image_processor(images, **output_kwargs['images_kwargs'])
        inputs.update(image_inputs)
        if text is not None:
            if n_images_in_images != n_images_in_text:
                raise ValueError(f'The number of images in the text {n_images_in_text} and images {n_images_in_images} should be the same.')
            image_rows = inputs.pop('rows', [[0] * len(text)])
            image_cols = inputs.pop('cols', [[0] * len(text)])
            fake_image_token = self.fake_image_token
            image_token = self.image_token
            global_img_token = self.global_image_tag
            prompt_strings = []
            batch_image_seq_lengths = []
            for sample, sample_rows, sample_cols in zip(text, image_rows, image_cols):
                image_prompt_strings = []
                image_seq_lengths = []
                for n_rows, n_cols in zip(sample_rows, sample_cols):
                    image_prompt_string = get_image_prompt_string(n_rows, n_cols, image_seq_len, image_token=image_token, fake_token_around_image=fake_image_token, global_img_token=global_img_token)
                    row_length = (self.image_seq_len + 2) * n_cols + 1
                    image_seq_lengths.append(self.image_seq_len + 3 + row_length * n_rows)
                    image_prompt_strings.append(image_prompt_string)
                batch_image_seq_lengths.append(image_seq_lengths)
                split_sample = sample.split(image_token)
                if len(split_sample) == 0:
                    raise ValueError('The image token should be present in the text.')
                sample = split_sample[0]
                for i, image_prompt_string in enumerate(image_prompt_strings):
                    sample += image_prompt_string + split_sample[i + 1]
                prompt_strings.append(sample)
            text_inputs = self.tokenizer(prompt_strings, **output_kwargs['text_kwargs'])
            self._check_special_mm_tokens(prompt_strings, text_inputs, modalities=['image'])
            inputs.update(text_inputs)
    elif text is not None:
        if any(n_images_in_text):
            raise ValueError(f'Found {sum(n_images_in_text)} {self.image_token} tokens in the text but no images were passed.')
        text_inputs = self.tokenizer(text=text, **output_kwargs['text_kwargs'])
        inputs.update(text_inputs)
    if return_mm_token_type_ids:
        array_ids = np.array(inputs['input_ids'])
        mm_token_type_ids = np.zeros_like(array_ids)
        for i, seq_lengths in enumerate(batch_image_seq_lengths):
            image_start_positions = np.where(array_ids[i] == self.fake_image_token_id)[0]
            j = 0
            for seq_len in seq_lengths:
                if j >= len(image_start_positions):
                    break
                start = image_start_positions[j]
                end = start + seq_len
                mm_token_type_ids[i, start:end] = 1
                j = np.searchsorted(image_start_positions, end)
        inputs['mm_token_type_ids'] = mm_token_type_ids.tolist()
    return BatchFeature(data=inputs, tensor_type=return_tensors)

Processes the input prompts and returns a BatchEncoding.

Example:

```python
>>> import requests
>>> from transformers import Idefics3Processor
>>> from transformers.image_utils import load_image

>>> processor = Idefics3Processor.from_pretrained("HuggingFaceM4/Idefics3-8B-Llama3")
>>> processor.image_processor.do_image_splitting = False  # Force as False to simplify the example

>>> url1 = "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
>>> url2 = "https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg"

>>> image1, image2 = load_image(url1), load_image(url2)
>>> images = [[image1], [image2]]

>>> text = [
...     "<image>In this image, we see",
...     "bla bla bla<image>",
... ]
>>> outputs = processor(images=images, text=text, return_tensors="pt", padding=True)
>>> input_ids = outputs.input_ids
>>> input_tokens = processor.tokenizer.batch_decode(input_ids)
>>> print(input_tokens)
['<|begin_of_text|><fake_token_around_image><global-img>((<image>)*169)<fake_token_around_image> In this image, we see', '<|reserved_special_token_0|><|reserved_special_token_0|><|reserved_special_token_0|><|begin_of_text|>bla bla bla<fake_token_around_image><global-img>((<image>)*169)<fake_token_around_image>']
```

Args:
    images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`, *optional*):
        The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
        tensor. If is of type `List[ImageInput]`, it's assumed that this is for a single prompt i.e. of batch size 1.
    text (`Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]`, *optional*):
        The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
        (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
        `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
        Wherever an image token, `<image>` is encountered it is expanded to
        `<fake_token_around_image>` + `<row_x_col_y>` + `<image>` * `image_seq_len` * <fake_token_around_image>`.
    image_seq_len (`int`, *optional*):
        The length of the image sequence. If not provided, the default value of self.image_seq_len is used.
        image_seq_len should be equal to int(((image_size // patch_size) ** 2) / (scale_factor**2))
    return_tensors (`Union[str, TensorType]`, *optional*):
        If set, will return tensors of a particular framework. See [`PreTrainedTokenizerFast.__call__`] for more
        information.


def __init__(self, default: typing.Optional[bool]=MISSING_VALUE, is_noneable: bool=False, frozen: bool=False):
    super().__init__(bool, default, is_noneable=is_noneable, frozen=frozen)

Constructor.

Args:
  default: Default value for the value spec.
  is_noneable: If True, None is acceptable.
  frozen: If True, values other than the default value is not accceptable.


class SiglipEncoder(nn.Module):

    def __init__(self, config: SiglipConfig):
        super().__init__()
        self.config = config
        self.layers = nn.ModuleList([SiglipEncoderLayer(config) for _ in range(config.num_hidden_layers)])
        self.gradient_checkpointing = False

    @can_return_tuple
    def forward(self, inputs_embeds, attention_mask: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None) -> BaseModelOutput:
        """
        Args:
            inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
                Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation.
                This is useful if you want more control over how to convert `input_ids` indices into associated vectors
                than the model's internal embedding lookup matrix.
            attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
                Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

                - 1 for tokens that are **not masked**,
                - 0 for tokens that are **masked**.

                [What are attention masks?](../glossary#attention-mask)
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
                for more detail.
            return_dict (`bool`, *optional*):
                Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        """
        output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
        output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        encoder_states = () if output_hidden_states else None
        all_attentions = () if output_attentions else None
        hidden_states = inputs_embeds
        for encoder_layer in self.layers:
            if output_hidden_states:
                encoder_states = encoder_states + (hidden_states,)
            layer_outputs = encoder_layer(hidden_states, attention_mask, output_attentions=output_attentions)
            hidden_states = layer_outputs[0]
            if output_attentions:
                all_attentions = all_attentions + (layer_outputs[1],)
        if output_hidden_states:
            encoder_states = encoder_states + (hidden_states,)
        return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)

Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
[`SiglipEncoderLayer`].

Args:
    config: SiglipConfig


def recipe_sheets_clear(config, auth_read, sheets_sheet, sheets_tab, sheets_range):
    sheets(config, {'auth': auth_read, 'sheet': sheets_sheet, 'tab': sheets_tab, 'range': sheets_range, 'clear': True})

Clear data from a sheet.

Args:
  auth_read (authentication) - Credentials used for reading data.
  sheets_sheet (string) - NA
  sheets_tab (string) - NA
  sheets_range (string) - NA


def clone_graph_nodes(inputs, outputs):
    nodes_to_clone = find_nodes_by_inputs_and_outputs(inputs, outputs)
    cloned_inputs = []
    cloned_outputs = []
    kt_id_mapping = {}
    op_id_mapping = {}
    for kt_input in tree.flatten(inputs):
        if is_input_keras_tensor(kt_input):
            cloned_inputs.append(kt_input)
            kt_id_mapping[id(kt_input)] = kt_input
        else:
            cloned_input = Input(batch_shape=kt_input.shape, dtype=kt_input.dtype, sparse=kt_input.sparse, name=kt_input.name + 'CLONE')
            cloned_inputs.append(cloned_input)
            kt_id_mapping[id(kt_input)] = cloned_input
            op_id_mapping[id(kt_input._keras_history[0])] = cloned_input._keras_history[0]
    cloned_inputs = tree.pack_sequence_as(inputs, cloned_inputs)
    for kt_output in tree.flatten(outputs):
        cpy = clone_single_keras_tensor(kt_output)
        cpy._keras_history = kt_output._keras_history
        cloned_outputs.append(cpy)
        kt_id_mapping[id(kt_output)] = cpy
    cloned_outputs = tree.pack_sequence_as(outputs, cloned_outputs)
    for node in nodes_to_clone:
        if id(node.operation) in op_id_mapping:
            operation = op_id_mapping[id(node.operation)]
        else:
            operation = node.operation
        output_copy = clone_keras_tensors(node.output_tensors, kt_id_mapping)
        if not isinstance(operation, InputLayer):
            call_args_copy = clone_keras_tensors(node.arguments.args, kt_id_mapping)
            call_kwargs_copy = clone_keras_tensors(node.arguments.kwargs, kt_id_mapping)
        else:
            call_args_copy = ()
            call_kwargs_copy = {}
        Node(operation, call_args=call_args_copy, call_kwargs=call_kwargs_copy, outputs=output_copy)
    return (cloned_inputs, cloned_outputs)

Clone the `Node` between the inputs and output tensors.

This function is used to create a new functional model from any intermediate
Keras tensors. The clone of the nodes mimic the behavior of reconstructing
the functional graph network by re-executing all the `__call__()` methods.
The cloned nodes will be appended to the layers.

Note that a new `keras.Input` will be created for any items in the
`inputs`

Args:
inputs: A nested structure of `KerasTensor` instances.
outputs: A nested structure of `KerasTensor` instances.

Returns:
    A pair of inputs and outputs, with cloned `KerasTensor` instances.
    They can be used to create a new functional model.


def call(self, pixel_values: tf.Tensor, input_ids: tf.Tensor | None=None, attention_mask: tf.Tensor | None=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, labels: tf.Tensor | None=None, return_dict: Optional[bool]=None, training: Optional[bool]=None) -> Union[Tuple, TFBlipForConditionalGenerationModelOutput]:
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    vision_outputs = self.vision_model(pixel_values=pixel_values, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, training=training)
    image_embeds = vision_outputs[0]
    outputs = self.text_decoder(input_ids=input_ids, attention_mask=attention_mask, encoder_hidden_states=image_embeds, labels=labels, return_dict=False, training=training)
    if not return_dict:
        outputs = (outputs[0], outputs[1], image_embeds, vision_outputs[0]) + vision_outputs[2:]
        return tuple((output for output in outputs if output is not None))
    if labels is not None:
        loss = outputs[0]
        logits = outputs[1]
    else:
        loss = None
        logits = outputs[0]
    if loss is not None and loss.shape.rank == 0:
        loss = tf.reshape(loss, (1,))
    return TFBlipForConditionalGenerationModelOutput(loss=loss, logits=logits, image_embeds=image_embeds, last_hidden_state=vision_outputs.last_hidden_state, hidden_states=vision_outputs.hidden_states, attentions=vision_outputs.attentions)

Returns:

Examples:

```python
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, TFBlipForConditionalGeneration

>>> processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
>>> model = TFBlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)
>>> text = "A picture of"

>>> inputs = processor(images=image, text=text, return_tensors="tf")

>>> outputs = model(**inputs)
```


def swap_price(pay_leg_cashflows, receive_leg_cashflows, pay_leg_discount_factors, receive_leg_discount_factors, dtype=None, name=None):
    name = name or 'swap_price'
    with tf.name_scope(name):
        pay_leg_cashflows = tf.convert_to_tensor(pay_leg_cashflows, dtype=dtype, name='pay_leg_cashflows')
        dtype = dtype or pay_leg_cashflows.dtype
        receive_leg_cashflows = tf.convert_to_tensor(receive_leg_cashflows, dtype=dtype, name='receive_leg_cashflows')
        pay_leg_discount_factors = tf.convert_to_tensor(pay_leg_discount_factors, dtype=dtype, name='pay_leg_discount_factors')
        receive_leg_discount_factors = tf.convert_to_tensor(receive_leg_discount_factors, dtype=dtype, name='receive_leg_discount_factors')
        receive_leg_pv = cashflows.present_value(receive_leg_cashflows, receive_leg_discount_factors)
        pay_leg_pv = cashflows.present_value(pay_leg_cashflows, pay_leg_discount_factors)
        return receive_leg_pv - pay_leg_pv

Computes prices of a batch of generic swaps.

#### Example
```python
pay_leg_cashflows = [[100, 100, 100], [200, 250, 300]]
receive_leg_cashflows = [[200, 250, 300, 300], [100, 100, 100, 100]]
pay_leg_discount_factors = [[0.95, 0.9, 0.8],
                            [0.9, 0.85, 0.8]]
receive_leg_discount_factors = [[0.95, 0.9, 0.8, 0.75],
                                [0.9, 0.85, 0.8, 0.75]]
swap_price(pay_leg_cashflows=pay_leg_cashflows,
           receive_leg_cashflows=receive_leg_cashflows,
           pay_leg_discount_factors=pay_leg_discount_factors,
           receive_leg_discount_factors=receive_leg_discount_factors,
           dtype=tf.float64)
# Expected: [615.0, -302.5]
```

Args:
  pay_leg_cashflows: A real `Tensor` of shape
    `batch_shape + [num_pay_cashflows]`, where `num_pay_cashflows` is the
    number of cashflows for each batch element. Cashflows of the pay leg of
    the swaps.
  receive_leg_cashflows: A `Tensor` of the same `dtype` as `pay_leg_cashflows`
    and of shape `batch_shape + [num_receive_cashflows]` where
    `num_pay_cashflows` is the number of cashflows for each batch element.
    Cashflows of the receive leg of the swaps.
  pay_leg_discount_factors: A `Tensor` of the same `dtype` as
    `pay_leg_cashflows` and of compatible shape. Discount factors for each
    cashflow of the pay leg.
  receive_leg_discount_factors: A `Tensor` of the same `dtype` as
    `receive_leg_cashflows` and of compatible shape. Discount factors for each
    cashflow of the receive leg.
  dtype: `tf.Dtype`. If supplied the dtype for the input and output `Tensor`s.
    Default value: None which maps to the default dtype inferred from
    `pay_leg_cashflows`.
  name: Python str. The name to give to the ops created by this function.
    Default value: None which maps to 'floating_coupons'.

Returns:
  A `Tensor` of the same `dtype` as `coupon_rates` and of shape `batch_shape`.
  Present values of swaps from receiver perspective.


def _prepare_4d_causal_attention_mask_with_cache_position(attention_mask: torch.Tensor, sequence_length: int, target_length: int, dtype: torch.dtype, cache_position: torch.Tensor, batch_size: int, **kwargs):
    if attention_mask is not None and attention_mask.dim() == 4:
        causal_mask = attention_mask
    else:
        min_dtype = torch.finfo(dtype).min
        causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=cache_position.device)
        if sequence_length != 1:
            causal_mask = torch.triu(causal_mask, diagonal=1)
        causal_mask *= torch.arange(target_length, device=cache_position.device) > cache_position.reshape(-1, 1)
        causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1)
        if attention_mask is not None:
            causal_mask = causal_mask.clone()
            mask_length = attention_mask.shape[-1]
            padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :].to(causal_mask.device)
            padding_mask = padding_mask == 0
            causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(padding_mask, min_dtype)
    return causal_mask

Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
`(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

Args:
    attention_mask (`torch.Tensor`):
        A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
        `(batch_size, 1, query_length, key_value_length)`.
    sequence_length (`int`):
        The sequence length being processed.
    target_length (`int`):
        The target length: when generating with static cache, the mask should be as long as the static cache,
        to account for the 0 padding, the part of the cache that is not filled yet.
    dtype (`torch.dtype`):
        The dtype to use for the 4D attention mask.
    cache_position (`torch.Tensor`):
        Indices depicting the position of the input sequence tokens in the sequence.
    batch_size (`torch.Tensor`):
        Batch size.


def forward(self, input_ids: Optional[torch.Tensor]=None, past_key_values: Optional[Tuple[Tuple[torch.Tensor]]]=None, attention_mask: Optional[torch.Tensor]=None, token_type_ids: Optional[torch.Tensor]=None, position_ids: Optional[torch.Tensor]=None, head_mask: Optional[torch.Tensor]=None, inputs_embeds: Optional[torch.Tensor]=None, encoder_hidden_states: Optional[torch.Tensor]=None, encoder_attention_mask: Optional[torch.Tensor]=None, labels: Optional[torch.Tensor]=None, use_cache: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, **kwargs: Any) -> Union[Tuple, CausalLMOutputWithCrossAttentions]:
    if 'pixel_values' in kwargs:
        warnings.warn('The `pixel_values` argument is deprecated and will be removed in v4.47, use `input_ids` instead.', FutureWarning)
        if input_ids is not None:
            raise ValueError('You cannot pass both `pixel_values` and `input_ids`. Please make sure to only pass `input_ids`.')
        input_ids = kwargs.pop('pixel_values')
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    transformer_outputs = self.transformer(input_ids, past_key_values=past_key_values, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=encoder_attention_mask, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    hidden_states = transformer_outputs[0]
    lm_logits = self.lm_head(hidden_states)
    loss = None
    if labels is not None:
        shift_logits = lm_logits[..., :-1, :].contiguous()
        shift_labels = labels[..., 1:].contiguous()
        loss_fct = CrossEntropyLoss()
        loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
    if not return_dict:
        output = (lm_logits,) + transformer_outputs[1:]
        return (loss,) + output if loss is not None else output
    return CausalLMOutputWithCrossAttentions(loss=loss, logits=lm_logits, past_key_values=transformer_outputs.past_key_values, hidden_states=transformer_outputs.hidden_states, attentions=transformer_outputs.attentions, cross_attentions=transformer_outputs.cross_attentions)

input_ids (`torch.LongTensor` of shape `(batch_size, input_ids_length)`):
    `input_ids_length` = `sequence_length` if `past_key_values` is `None` else
    `past_key_values[0][0].shape[-2]` (`sequence_length` of input past key value states). Indices of input
    sequence tokens in the vocabulary.

    If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as
    `input_ids`.

    Indices can be obtained using [`AutoImageProcessor`]. See [`ImageGPTImageProcessor.__call__`] for details.
labels (`torch.LongTensor` of shape `(batch_size, input_ids_length)`, *optional*):
    Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
    `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
    are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`

Examples:

```python
>>> from transformers import AutoImageProcessor, ImageGPTForCausalImageModeling
>>> import torch
>>> import matplotlib.pyplot as plt
>>> import numpy as np

>>> image_processor = AutoImageProcessor.from_pretrained("openai/imagegpt-small")
>>> model = ImageGPTForCausalImageModeling.from_pretrained("openai/imagegpt-small")
>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
>>> model.to(device)  # doctest: +IGNORE_RESULT

>>> # unconditional generation of 8 images
>>> batch_size = 4
>>> context = torch.full((batch_size, 1), model.config.vocab_size - 1)  # initialize with SOS token
>>> context = context.to(device)
>>> output = model.generate(
...     input_ids=context, max_length=model.config.n_positions + 1, temperature=1.0, do_sample=True, top_k=40
... )

>>> clusters = image_processor.clusters
>>> height = image_processor.size["height"]
>>> width = image_processor.size["width"]

>>> samples = output[:, 1:].detach().cpu().numpy()
>>> samples_img = [
...     np.reshape(np.rint(127.5 * (clusters[s] + 1.0)), [height, width, 3]).astype(np.uint8) for s in samples
... ]  # convert color cluster tokens back to pixels
>>> f, axes = plt.subplots(1, batch_size, dpi=300)

>>> for img, ax in zip(samples_img, axes):  # doctest: +IGNORE_RESULT
...     ax.axis("off")
...     ax.imshow(img)
```


def call(self, pixel_values: TFModelInputType | None=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, training: bool=False) -> Union[TFBaseModelOutputWithPooling, Tuple[tf.Tensor]]:
    outputs = self.groupvit(pixel_values=pixel_values, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, training=training)
    return outputs

Returns:

Examples:

```python
>>> from PIL import Image
>>> import requests
>>> from transformers import AutoProcessor, TFGroupViTVisionModel

>>> processor = AutoProcessor.from_pretrained("nvidia/groupvit-gcc-yfcc")
>>> model = TFGroupViTVisionModel.from_pretrained("nvidia/groupvit-gcc-yfcc")

>>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw)

>>> inputs = processor(images=image, return_tensors="tf")

>>> outputs = model(**inputs)
>>> last_hidden_state = outputs.last_hidden_state
>>> pooled_output = outputs.pooler_output  # pooled CLS states
```


def cosine_similarity(y_true, y_pred, axis=-1):
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.convert_to_tensor(y_true, dtype=y_pred.dtype)
    y_true, y_pred = squeeze_or_expand_to_same_rank(y_true, y_pred)
    y_pred = normalize(y_pred, axis=axis)
    y_true = normalize(y_true, axis=axis)
    return ops.sum(y_true * y_pred, axis=axis)

Computes the cosine similarity between labels and predictions.

Formula:

```python
loss = sum(l2_norm(y_true) * l2_norm(y_pred))
```

Args:
    y_true: Tensor of true targets.
    y_pred: Tensor of predicted targets.
    axis: Axis along which to determine similarity. Defaults to `-1`.

Returns:
    Cosine similarity tensor.

Example:

>>> y_true = [[0., 1.], [1., 1.], [1., 1.]]
>>> y_pred = [[1., 0.], [1., 1.], [-1., -1.]]
>>> loss = keras.losses.cosine_similarity(y_true, y_pred, axis=-1)
[0., 0.99999994, -0.99999994]


def try_split(self, position):
    raise NotImplementedError

Atomically splits the current range.

Determines a position to split the current range, split_position, based on
the given position. In most cases split_position and position will be the
same.

Splits the current range '[self.start_position, self.stop_position)'
into a "primary" part '[self.start_position, split_position)' and a
"residual" part '[split_position, self.stop_position)', assuming the
current last-consumed position is within
'[self.start_position, split_position)' (i.e., split_position has not been
consumed yet).

If successful, updates the current range to be the primary and returns a
tuple (split_position, split_fraction). split_fraction should be the
fraction of size of range '[self.start_position, split_position)' compared
to the original (before split) range
'[self.start_position, self.stop_position)'.

If the split_position has already been consumed, returns ``None``.

** Thread safety **

Methods of the class ``RangeTracker`` including this method may get invoked
by different threads, hence must be made thread-safe, e.g. by using a single
lock object.

Args:
  position: suggested position where the current range should try to
            be split at.
Returns:
  a tuple containing the split position and split fraction if split is
  successful. Returns ``None`` otherwise.


def _is_png(contents, name=None):
    with ops.name_scope(name, 'is_png'):
        substr = string_ops.substr(contents, 0, 3)
        return math_ops.equal(substr, b'\x89PN', name=name)

Convenience function to check if the 'contents' encodes a PNG image.

Args:
  contents: 0-D `string`. The encoded image bytes.
  name: A name for the operation (optional)

Returns:
   A scalar boolean tensor indicating if 'contents' may be a PNG image.
   is_png is susceptible to false positives.


def upsample_like(pixel_values: Tensor, like: Tensor, mode: str='bilinear') -> Tensor:
    _, _, height, width = like.shape
    upsampled = nn.functional.interpolate(pixel_values, size=(height, width), mode=mode, align_corners=False)
    return upsampled

An utility function that upsamples `pixel_values` to match the dimension of `like`.

Args:
    pixel_values (`torch.Tensor`):
        The tensor we wish to upsample.
    like (`torch.Tensor`):
        The tensor we wish to use as size target.
    mode (str, *optional*, defaults to `"bilinear"`):
        The interpolation mode.

Returns:
    `torch.Tensor`: The upsampled tensor


def to_proto(self, export_scope=None):
    if export_scope is None or self._variable.name.startswith(export_scope):
        var_def = variable_pb2.VariableDef()
        var_def.variable_name = ops.strip_name_scope(self._variable.name, export_scope)
        if self._initial_value is not None:
            var_def.initial_value_name = ops.strip_name_scope(self._initial_value.name, export_scope)
        var_def.trainable = self.trainable
        var_def.synchronization = self.synchronization.value
        var_def.aggregation = self.aggregation.value
        var_def.initializer_name = ops.strip_name_scope(self.initializer.name, export_scope)
        var_def.snapshot_name = ops.strip_name_scope(self._snapshot.name, export_scope)
        if self._save_slice_info:
            var_def.save_slice_info_def.MergeFrom(self._save_slice_info.to_proto(export_scope=export_scope))
        return var_def
    else:
        return None

Converts a `Variable` to a `VariableDef` protocol buffer.

Args:
  export_scope: Optional `string`. Name scope to remove.

Returns:
  A `VariableDef` protocol buffer, or `None` if the `Variable` is not
  in the specified name scope.


def get_special_tokens_mask(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None, already_has_special_tokens: bool=False) -> List[int]:
    if already_has_special_tokens:
        return super().get_special_tokens_mask(token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True)
    if token_ids_1 is not None:
        return [1] + [0] * len(token_ids_0) + [1] + [0] * len(token_ids_1) + [1]
    return [1] + [0] * len(token_ids_0) + [1]

Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer `prepare_for_model` method.

Args:
    token_ids_0 (`List[int]`):
        List of IDs.
    token_ids_1 (`List[int]`, *optional*):
        Optional second list of IDs for sequence pairs.
    already_has_special_tokens (`bool`, *optional*, defaults to `False`):
        Whether or not the token list is already formatted with special tokens for the model.

Returns:
    `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.


def get_readrows_iterator(bq_read_client: BigQueryReadClient, table_metadata: TableMetadata, columns: Iterable[str] | None=None, data_format: DataFormat=DataFormat.AVRO) -> Iterable[Mapping]:
    requested_session = ReadSession(table=table_metadata.table_path, data_format=data_format.value, read_options={'selected_fields': columns})
    session = bq_read_client.create_read_session(parent=f'projects/{table_metadata.project_id}', read_session=requested_session, max_stream_count=1)
    stream_name = session.streams[0].name
    reader = bq_read_client.read_rows(stream_name)
    rows = reader.rows(session)
    return cast(Iterable[Mapping], rows)

Get an Iterator of row Mappings with the requested columns of the table,
using an authenticated BigQuery Storage API client.

Note: Does NOT support nested columns.

Args:
    * bq_read_client: BigQuery Storage API Read client
    * table_metadata: TableMetadata object
    * columns (optional): List of columns to select
    * data_format: Format to fetch data in, one of:
        * DataFormat.AVRO
        * DataFormat.ARROW

Defaults:
    * columns: None, i.e. select all columns
    * data_format: AVRO, since it auto-parses to Dict

Returns:
    * Iterator of row Mappings


def run_functions_eagerly(run_eagerly):
    global RUN_FUNCTIONS_EAGERLY
    RUN_FUNCTIONS_EAGERLY = bool(run_eagerly)

Enables / disables eager execution of `tf.function`s.

Calling `tf.config.run_functions_eagerly(True)` will make all
invocations of `tf.function` run eagerly instead of running as a traced graph
function. This can be useful for debugging. As the code now runs line-by-line,
you can add arbitrary `print` messages or pdb breakpoints to monitor the
inputs/outputs of each Tensorflow operation. However, you should avoid using
this for actual production because it significantly slows down execution.

>>> def my_func(a):
...  print(f'a: {a}')
...  return a + a
>>> a_fn = tf.function(my_func)

>>> # A side effect the first time the function is traced
>>> # In tracing time, `a` is printed with shape and dtype only
>>> a_fn(tf.constant(1))
a: Tensor("a:0", shape=(), dtype=int32)
<tf.Tensor: shape=(), dtype=int32, numpy=2>

>>> # `print` is a python side effect, it won't execute as the traced function
>>> # is called
>>> a_fn(tf.constant(2))
<tf.Tensor: shape=(), dtype=int32, numpy=4>

>>> # Now, switch to eager running
>>> tf.config.run_functions_eagerly(True)
>>> # The code now runs eagerly and the actual value of `a` is printed
>>> a_fn(tf.constant(2))
a: 2
<tf.Tensor: shape=(), dtype=int32, numpy=4>

>>> # Turn this back off
>>> tf.config.run_functions_eagerly(False)

Note: This flag has no effect on functions passed into tf.data transformations
as arguments. tf.data functions are never executed eagerly and are always
executed as a compiled Tensorflow Graph.

Args:
  run_eagerly: Boolean. Whether to run functions eagerly.


class BlenderbotSmallTokenizerFast(PreTrainedTokenizerFast):
    vocab_files_names = VOCAB_FILES_NAMES
    slow_tokenizer_class = BlenderbotSmallTokenizer

    def __init__(self, vocab_file=None, merges_file=None, unk_token='<|endoftext|>', bos_token='<|endoftext|>', eos_token='<|endoftext|>', add_prefix_space=False, trim_offsets=True, **kwargs):
        super().__init__(ByteLevelBPETokenizer(vocab=vocab_file, merges=merges_file, add_prefix_space=add_prefix_space, trim_offsets=trim_offsets), bos_token=bos_token, eos_token=eos_token, unk_token=unk_token, **kwargs)
        self.add_prefix_space = add_prefix_space

    def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
        output = [self.bos_token_id] + token_ids_0 + [self.eos_token_id]
        if token_ids_1 is None:
            return output
        return output + [self.eos_token_id] + token_ids_1 + [self.eos_token_id]

    def create_token_type_ids_from_sequences(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None) -> List[int]:
        """
        Create a mask from the two sequences passed to be used in a sequence-pair classification task. BlenderbotSmall
        does not make use of token type ids, therefore a list of zeros is returned.

        Args:
            token_ids_0 (`List[int]`):
                List of IDs.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of zeros.
        """
        sep = [self.sep_token_id]
        cls = [self.cls_token_id]
        if token_ids_1 is None:
            return len(cls + token_ids_0 + sep) * [0]
        return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0]

Construct a "fast" BlenderbotSmall tokenizer (backed by HuggingFace's *tokenizers* library).

Args:
    vocab_file (`str`):
        Path to the vocabulary file.


class TFTopKLogitsWarper(TFLogitsWarper):

    def __init__(self, top_k: int, filter_value: float=-float('Inf'), min_tokens_to_keep: int=1):
        if not isinstance(top_k, int) or top_k <= 0:
            raise ValueError(f'`top_k` has to be a strictly positive integer, but is {top_k}')
        self.top_k = max(top_k, min_tokens_to_keep)
        self.filter_value = filter_value

    def __call__(self, input_ids: tf.Tensor, scores: tf.Tensor, cur_len: int) -> tf.Tensor:
        top_k = min(self.top_k, scores.shape[-1])
        indices_to_remove = scores < tf.math.top_k(scores, k=top_k)[0][..., -1:]
        next_scores = tf.where(indices_to_remove, self.filter_value, scores)
        return next_scores

[`TFLogitsWarper`] that performs top-k, i.e. restricting to the k highest probability elements.

Args:
    top_k (`int`):
        The number of highest probability vocabulary tokens to keep for top-k-filtering.
    filter_value (`float`, *optional*, defaults to -inf):
        All filtered values will be set to this float value.
    min_tokens_to_keep (`int`, *optional*, defaults to 1):
        Minimum number of tokens that cannot be filtered.


def forward(self, input_ids: Optional[torch.LongTensor]=None, pixel_values: Optional[torch.FloatTensor]=None, attention_mask: Optional[torch.Tensor]=None, position_ids: Optional[torch.LongTensor]=None, return_loss: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, interpolate_pos_encoding: bool=False, return_dict: Optional[bool]=None) -> Union[Tuple, XCLIPOutput]:
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    batch_size, num_frames, num_channels, height, width = pixel_values.shape
    pixel_values = pixel_values.reshape(-1, num_channels, height, width)
    vision_outputs = self.vision_model(pixel_values=pixel_values, output_attentions=output_attentions, output_hidden_states=output_hidden_states, interpolate_pos_encoding=interpolate_pos_encoding, return_dict=return_dict)
    video_embeds = vision_outputs[1]
    video_embeds = self.visual_projection(video_embeds)
    cls_features = video_embeds.view(batch_size, num_frames, -1)
    mit_outputs = self.mit(cls_features, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    video_embeds = mit_outputs[1]
    img_features = vision_outputs[0][:, 1:, :]
    img_features = self.prompts_visual_layernorm(img_features)
    img_features = img_features @ self.prompts_visual_projection
    img_features = img_features.view(batch_size, num_frames, -1, video_embeds.shape[-1])
    img_features = img_features.mean(dim=1, keepdim=False)
    text_outputs = self.text_model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    text_embeds = text_outputs[1]
    text_embeds = self.text_projection(text_embeds)
    text_embeds = text_embeds.unsqueeze(0).expand(batch_size, -1, -1)
    text_embeds = text_embeds + self.prompts_generator(text_embeds, img_features)
    video_embeds = video_embeds / video_embeds.norm(p=2, dim=-1, keepdim=True)
    text_embeds = text_embeds / text_embeds.norm(p=2, dim=-1, keepdim=True)
    logit_scale = self.logit_scale.exp()
    logits_per_video = torch.einsum('bd,bkd->bk', video_embeds, logit_scale * text_embeds)
    logits_per_text = logits_per_video.T
    loss = None
    if return_loss:
        loss = x_clip_loss(logits_per_text)
    if not return_dict:
        output = (logits_per_video, logits_per_text, text_embeds, video_embeds, text_outputs, vision_outputs)
        return (loss,) + output if loss is not None else output
    return XCLIPOutput(loss=loss, logits_per_video=logits_per_video, logits_per_text=logits_per_text, text_embeds=text_embeds, video_embeds=video_embeds, text_model_output=text_outputs, vision_model_output=vision_outputs, mit_output=mit_outputs)

return_loss (`bool`, *optional*):
    Whether or not to return the contrastive loss.

Examples:

```python
>>> import av
>>> import torch
>>> import numpy as np

>>> from transformers import AutoProcessor, AutoModel
>>> from huggingface_hub import hf_hub_download

>>> np.random.seed(0)

>>> def read_video_pyav(container, indices):
...     '''
...     Decode the video with PyAV decoder.
...     Args:
...         container (`av.container.input.InputContainer`): PyAV container.
...         indices (`List[int]`): List of frame indices to decode.
...     Returns:
...         result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3).
...     '''
...     frames = []
...     container.seek(0)
...     start_index = indices[0]
...     end_index = indices[-1]
...     for i, frame in enumerate(container.decode(video=0)):
...         if i > end_index:
...             break
...         if i >= start_index and i in indices:
...             frames.append(frame)
...     return np.stack([x.to_ndarray(format="rgb24") for x in frames])

>>> def sample_frame_indices(clip_len, frame_sample_rate, seg_len):
...     '''
...     Sample a given number of frame indices from the video.
...     Args:
...         clip_len (`int`): Total number of frames to sample.
...         frame_sample_rate (`int`): Sample every n-th frame.
...         seg_len (`int`): Maximum allowed index of sample's last frame.
...     Returns:
...         indices (`List[int]`): List of sampled frame indices
...     '''
...     converted_len = int(clip_len * frame_sample_rate)
...     end_idx = np.random.randint(converted_len, seg_len)
...     start_idx = end_idx - converted_len
...     indices = np.linspace(start_idx, end_idx, num=clip_len)
...     indices = np.clip(indices, start_idx, end_idx - 1).astype(np.int64)
...     return indices

>>> # video clip consists of 300 frames (10 seconds at 30 FPS)
>>> file_path = hf_hub_download(
...     repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset"
... )
>>> container = av.open(file_path)

>>> # sample 8 frames
>>> indices = sample_frame_indices(clip_len=8, frame_sample_rate=1, seg_len=container.streams.video[0].frames)
>>> video = read_video_pyav(container, indices)

>>> processor = AutoProcessor.from_pretrained("microsoft/xclip-base-patch32")
>>> model = AutoModel.from_pretrained("microsoft/xclip-base-patch32")

>>> inputs = processor(
...     text=["playing sports", "eating spaghetti", "go shopping"],
...     videos=list(video),
...     return_tensors="pt",
...     padding=True,
... )

>>> # forward pass
>>> with torch.no_grad():
...     outputs = model(**inputs)

>>> logits_per_video = outputs.logits_per_video  # this is the video-text similarity score
>>> probs = logits_per_video.softmax(dim=1)  # we can take the softmax to get the label probabilities
>>> print(probs)
tensor([[1.9496e-04, 9.9960e-01, 2.0825e-04]])
```