def matmul(self, x, adjoint=False, adjoint_arg=False, name='matmul'):
    if isinstance(x, linear_operator.LinearOperator):
        left_operator = self.adjoint() if adjoint else self
        right_operator = x.adjoint() if adjoint_arg else x
        if right_operator.range_dimension is not None and left_operator.domain_dimension is not None and (right_operator.range_dimension != left_operator.domain_dimension):
            raise ValueError('Operators are incompatible. Expected `x` to have dimension {} but got {}.'.format(left_operator.domain_dimension, right_operator.range_dimension))
        with self._name_scope(name):
            return self._linop_matmul(left_operator, right_operator)
    with self._name_scope(name):
        arg_dim = -1 if adjoint_arg else -2
        block_dimensions = self._block_range_dimensions() if adjoint else self._block_domain_dimensions()
        if linear_operator_util.arg_is_blockwise(block_dimensions, x, arg_dim):
            for i, block in enumerate(x):
                if not isinstance(block, linear_operator.LinearOperator):
                    block = tensor_conversion.convert_to_tensor_v2_with_dispatch(block)
                    self._check_input_dtype(block)
                    block_dimensions[i].assert_is_compatible_with(block.shape[arg_dim])
                    x[i] = block
        else:
            x = tensor_conversion.convert_to_tensor_v2_with_dispatch(x, name='x')
            self._check_input_dtype(x)
            op_dimension = self.range_dimension if adjoint else self.domain_dimension
            op_dimension.assert_is_compatible_with(x.shape[arg_dim])
        return self._matmul(x, adjoint=adjoint, adjoint_arg=adjoint_arg)

Transform [batch] matrix `x` with left multiplication:  `x --> Ax`.

```python
# Make an operator acting like batch matrix A.  Assume A.shape = [..., M, N]
operator = LinearOperator(...)
operator.shape = [..., M, N]

X = ... # shape [..., N, R], batch matrix, R > 0.

Y = operator.matmul(X)
Y.shape
==> [..., M, R]

Y[..., :, r] = sum_j A[..., :, j] X[j, r]
```

Args:
  x: `LinearOperator`, `Tensor` with compatible shape and same `dtype` as
    `self`, or a blockwise iterable of `LinearOperator`s or `Tensor`s. See
    class docstring for definition of shape compatibility.
  adjoint: Python `bool`.  If `True`, left multiply by the adjoint: `A^H x`.
  adjoint_arg:  Python `bool`.  If `True`, compute `A x^H` where `x^H` is
    the hermitian transpose (transposition and complex conjugation).
  name:  A name for this `Op`.

Returns:
  A `LinearOperator` or `Tensor` with shape `[..., M, R]` and same `dtype`
    as `self`, or if `x` is blockwise, a list of `Tensor`s with shapes that
    concatenate to `[..., M, R]`.


def get_size_with_aspect_ratio(image_size, size, max_size=None) -> Tuple[int, int]:
    height, width = image_size
    raw_size = None
    if max_size is not None:
        min_original_size = float(min((height, width)))
        max_original_size = float(max((height, width)))
        if max_original_size / min_original_size * size > max_size:
            raw_size = max_size * min_original_size / max_original_size
            size = int(round(raw_size))
    if height <= width and height == size or (width <= height and width == size):
        oh, ow = (height, width)
    elif width < height:
        ow = size
        if max_size is not None and raw_size is not None:
            oh = int(raw_size * height / width)
        else:
            oh = int(size * height / width)
    else:
        oh = size
        if max_size is not None and raw_size is not None:
            ow = int(raw_size * width / height)
        else:
            ow = int(size * width / height)
    return (oh, ow)

Computes the output image size given the input image size and the desired output size.

Args:
    image_size (`Tuple[int, int]`):
        The input image size.
    size (`int`):
        The desired output size.
    max_size (`int`, *optional*):
        The maximum allowed output size.


def _reshape(self, fused_qkv: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
    batch_size, seq_length, three_times_hidden_size = fused_qkv.shape
    fused_qkv = fused_qkv.view(batch_size, seq_length, self.num_heads, 3, self.head_dim)
    query_layer = fused_qkv[..., 0, :].transpose(1, 2)
    key_layer = fused_qkv[..., 1, :].transpose(1, 2)
    value_layer = fused_qkv[..., 2, :].transpose(1, 2)
    return (query_layer, key_layer, value_layer)

Split the last dimension into (num_heads, head_dim) and reshapes to (bs, heads, len, dim) shape
without making any copies, results share same memory storage as `fused_qkv`

Args:
    fused_qkv (`torch.tensor`): [batch_size, seq_length, num_heads * 3 * head_dim]

Returns:
    query: [batch_size, num_heads, seq_length, head_dim]
    key: [batch_size, num_heads, seq_length, head_dim]
    value: [batch_size, num_heads, seq_length, head_dim]


def wrap_in_placeholder(self, arg, shape_info):
    if shape_info == 'known':
        return arg
    if isinstance(arg, ragged_tensor.RaggedTensor):
        return arg.with_flat_values(self.wrap_in_placeholder(arg.flat_values, shape_info))
    if isinstance(arg, tensor_shape.TensorShape):
        if arg.ndims is None:
            return arg
        arg = constant_op.constant(arg.as_list())
    if shape_info == 'unknown_rank':
        return array_ops.placeholder_with_default(arg, None)
    if shape_info == 'unknown_dims':
        return array_ops.placeholder_with_default(arg, [None] * arg.shape.rank)
    raise AssertionError('Unexpected shape_info %r' % shape_info)

Wraps `arg` in a placeholder to limit static shape info.

Args:
  arg: The value to wrap.  A Tensor, RaggedTensor, or TensorShape.
  shape_info: One of ['known', 'unknown_dims', 'unknown_rank'].

Returns:
  * If shape_info is 'known': returns `arg`.
  * If shape_info is 'unknown_dims': returns a placeholder wrapping `arg`
    where the dimension sizes are unknown.  If `arg` is a TensorShape,
    then convert it to a vector first.  If `arg` is a RaggedTensor, then
    wrap the flat_values.
  * If shape_info is 'unknown_rank': returns a placeholder wrapping `arg`
    where the rank is unknown.  If `arg` is a TensorShape, then convert it
    to a vector first.  If `arg` is a RaggedTensor, then wrap the
    flat_values.


def __init__(self, project):
    self._project = project
    self._client = None
    self._rpc_successes = Metrics.counter(_Mutate.DatastoreMutateFn, 'datastoreRpcSuccesses')
    self._rpc_errors = Metrics.counter(_Mutate.DatastoreMutateFn, 'datastoreRpcErrors')
    self._throttled_secs = Metrics.counter(_Mutate.DatastoreMutateFn, 'cumulativeThrottlingSeconds')
    self._throttler = AdaptiveThrottler(window_ms=120000, bucket_ms=1000, overload_ratio=1.25)


def _should_merge(self, pytd_type, union):
    names = self._CONTAINER_NAMES[pytd_type]
    length = None
    for t in union.type_list:
        if isinstance(t, pytd_type):
            if length is None:
                length = len(t.parameters)
            elif length != len(t.parameters):
                return True
        elif isinstance(t, pytd.GenericType) and t.name in names:
            return True
    return False

Determine whether pytd_type values in the union should be merged.

If the union contains the homogeneous flavor of pytd_type (e.g.,
GenericType(base_type=tuple) when pytd_type is TupleType), or pytd_type
values of different lengths, we want to turn all of the pytd_type values
into homogeneous ones so that they can be merged into a single container.

Args:
  pytd_type: The pytd type, either TupleType or CallableType.
  union: a pytd.UnionType

Returns:
  True if the pytd_type values should be merged, False otherwise.


class TFTransfoXLModelOutput(ModelOutput):
    last_hidden_state: Optional[tf.Tensor] = None
    mems: List[tf.Tensor] = None
    hidden_states: Tuple[tf.Tensor] | None = None
    attentions: Tuple[tf.Tensor] | None = None

Base class for model's outputs that may also contain a past key/values (to speed up sequential decoding).

Args:
    last_hidden_state (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`):
        Sequence of hidden-states at the output of the last layer of the model.
    mems (`List[tf.Tensor]` of length `config.n_layers`):
        Contains pre-computed hidden-states (key and values in the attention blocks). Can be used (see `mems`
        input) to speed up sequential decoding. The token ids which have their past given to this model should not
        be passed as input ids as they have already been computed.
    hidden_states (`tuple(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `tf.Tensor` (one for the output of the embeddings + one for the output of each layer) of shape
        `(batch_size, sequence_length, hidden_size)`.

        Hidden-states of the model at the output of each layer plus the initial embedding outputs.
    attentions (`tuple(tf.Tensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `tf.Tensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
        sequence_length)`.

        Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
        heads.


def forward(self, past_values: torch.Tensor, past_time_features: torch.Tensor, past_observed_mask: torch.Tensor, static_categorical_features: Optional[torch.Tensor]=None, static_real_features: Optional[torch.Tensor]=None, future_values: Optional[torch.Tensor]=None, future_time_features: Optional[torch.Tensor]=None, future_observed_mask: Optional[torch.Tensor]=None, decoder_attention_mask: Optional[torch.LongTensor]=None, head_mask: Optional[torch.Tensor]=None, decoder_head_mask: Optional[torch.Tensor]=None, cross_attn_head_mask: Optional[torch.Tensor]=None, encoder_outputs: Optional[List[torch.FloatTensor]]=None, past_key_values: Optional[List[torch.FloatTensor]]=None, output_hidden_states: Optional[bool]=None, output_attentions: Optional[bool]=None, use_cache: Optional[bool]=None, return_dict: Optional[bool]=None, cache_position: Optional[torch.LongTensor]=None) -> Union[Seq2SeqTSModelOutput, Tuple]:
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    if future_values is not None:
        use_cache = False
    outputs = self.model(past_values=past_values, past_time_features=past_time_features, past_observed_mask=past_observed_mask, static_categorical_features=static_categorical_features, static_real_features=static_real_features, future_values=future_values, future_time_features=future_time_features, decoder_attention_mask=decoder_attention_mask, head_mask=head_mask, decoder_head_mask=decoder_head_mask, cross_attn_head_mask=cross_attn_head_mask, encoder_outputs=encoder_outputs, past_key_values=past_key_values, output_hidden_states=output_hidden_states, output_attentions=output_attentions, use_cache=use_cache, return_dict=return_dict, cache_position=cache_position)
    prediction_loss = None
    params = None
    if future_values is not None:
        params = self.output_params(outputs[0])
        distribution = self.output_distribution(params, loc=outputs[-3], scale=outputs[-2])
        loss = self.loss(distribution, future_values)
        if future_observed_mask is None:
            future_observed_mask = torch.ones_like(future_values)
        if len(self.target_shape) == 0:
            loss_weights = future_observed_mask
        else:
            loss_weights, _ = future_observed_mask.min(dim=-1, keepdim=False)
        prediction_loss = weighted_average(loss, weights=loss_weights)
    if not return_dict:
        outputs = (params,) + outputs[1:] if params is not None else outputs[1:]
        return (prediction_loss,) + outputs if prediction_loss is not None else outputs
    return Seq2SeqTSPredictionOutput(loss=prediction_loss, params=params, past_key_values=outputs.past_key_values, decoder_hidden_states=outputs.decoder_hidden_states, decoder_attentions=outputs.decoder_attentions, cross_attentions=outputs.cross_attentions, encoder_last_hidden_state=outputs.encoder_last_hidden_state, encoder_hidden_states=outputs.encoder_hidden_states, encoder_attentions=outputs.encoder_attentions, loc=outputs.loc, scale=outputs.scale, static_features=outputs.static_features)

past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, input_size)`):
    Past values of the time series, that serve as context in order to predict the future. The sequence size of
    this tensor must be larger than the `context_length` of the model, since the model will use the larger size
    to construct lag features, i.e. additional values from the past which are added in order to serve as "extra
    context".

    The `sequence_length` here is equal to `config.context_length` + `max(config.lags_sequence)`, which if no
    `lags_sequence` is configured, is equal to `config.context_length` + 7 (as by default, the largest
    look-back index in `config.lags_sequence` is 7). The property `_past_length` returns the actual length of
    the past.

    The `past_values` is what the Transformer encoder gets as input (with optional additional features, such as
    `static_categorical_features`, `static_real_features`, `past_time_features` and lags).

    Optionally, missing values need to be replaced with zeros and indicated via the `past_observed_mask`.

    For multivariate time series, the `input_size` > 1 dimension is required and corresponds to the number of
    variates in the time series per time step.
past_time_features (`torch.FloatTensor` of shape `(batch_size, sequence_length, num_features)`):
    Required time features, which the model internally will add to `past_values`. These could be things like
    "month of year", "day of the month", etc. encoded as vectors (for instance as Fourier features). These
    could also be so-called "age" features, which basically help the model know "at which point in life" a
    time-series is. Age features have small values for distant past time steps and increase monotonically the
    more we approach the current time step. Holiday features are also a good example of time features.

    These features serve as the "positional encodings" of the inputs. So contrary to a model like BERT, where
    the position encodings are learned from scratch internally as parameters of the model, the Time Series
    Transformer requires to provide additional time features. The Time Series Transformer only learns
    additional embeddings for `static_categorical_features`.

    Additional dynamic real covariates can be concatenated to this tensor, with the caveat that these features
    must but known at prediction time.

    The `num_features` here is equal to `config.`num_time_features` + `config.num_dynamic_real_features`.
past_observed_mask (`torch.BoolTensor` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, input_size)`, *optional*):
    Boolean mask to indicate which `past_values` were observed and which were missing. Mask values selected in
    `[0, 1]`:

    - 1 for values that are **observed**,
    - 0 for values that are **missing** (i.e. NaNs that were replaced by zeros).
static_categorical_features (`torch.LongTensor` of shape `(batch_size, number of static categorical features)`, *optional*):
    Optional static categorical features for which the model will learn an embedding, which it will add to the
    values of the time series.

    Static categorical features are features which have the same value for all time steps (static over time).

    A typical example of a static categorical feature is a time series ID.
static_real_features (`torch.FloatTensor` of shape `(batch_size, number of static real features)`, *optional*):
    Optional static real features which the model will add to the values of the time series.

    Static real features are features which have the same value for all time steps (static over time).

    A typical example of a static real feature is promotion information.
future_values (`torch.FloatTensor` of shape `(batch_size, prediction_length)` or `(batch_size, prediction_length, input_size)`, *optional*):
    Future values of the time series, that serve as labels for the model. The `future_values` is what the
    Transformer needs during training to learn to output, given the `past_values`.

    The sequence length here is equal to `prediction_length`.

    See the demo notebook and code snippets for details.

    Optionally, during training any missing values need to be replaced with zeros and indicated via the
    `future_observed_mask`.

    For multivariate time series, the `input_size` > 1 dimension is required and corresponds to the number of
    variates in the time series per time step.
future_time_features (`torch.FloatTensor` of shape `(batch_size, prediction_length, num_features)`):
    Required time features for the prediction window, which the model internally will add to `future_values`.
    These could be things like "month of year", "day of the month", etc. encoded as vectors (for instance as
    Fourier features). These could also be so-called "age" features, which basically help the model know "at
    which point in life" a time-series is. Age features have small values for distant past time steps and
    increase monotonically the more we approach the current time step. Holiday features are also a good example
    of time features.

    These features serve as the "positional encodings" of the inputs. So contrary to a model like BERT, where
    the position encodings are learned from scratch internally as parameters of the model, the Time Series
    Transformer requires to provide additional time features. The Time Series Transformer only learns
    additional embeddings for `static_categorical_features`.

    Additional dynamic real covariates can be concatenated to this tensor, with the caveat that these features
    must but known at prediction time.

    The `num_features` here is equal to `config.`num_time_features` + `config.num_dynamic_real_features`.
future_observed_mask (`torch.BoolTensor` of shape `(batch_size, sequence_length)` or `(batch_size, sequence_length, input_size)`, *optional*):
    Boolean mask to indicate which `future_values` were observed and which were missing. Mask values selected
    in `[0, 1]`:

    - 1 for values that are **observed**,
    - 0 for values that are **missing** (i.e. NaNs that were replaced by zeros).

    This mask is used to filter out missing values for the final loss calculation.
cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*):
    Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`:

    - 1 indicates the head is **not masked**,
    - 0 indicates the head is **masked**.
encoder_outputs (`tuple(tuple(torch.FloatTensor)`, *optional*):
    Tuple consists of `last_hidden_state`, `hidden_states` (*optional*) and `attentions` (*optional*)
    `last_hidden_state` of shape `(batch_size, sequence_length, hidden_size)` (*optional*) is a sequence of
    hidden-states at the output of the last layer of the encoder. Used in the cross-attention of the decoder.

Examples:

```python
>>> from huggingface_hub import hf_hub_download
>>> import torch
>>> from transformers import TimeSeriesTransformerForPrediction

>>> file = hf_hub_download(
...     repo_id="hf-internal-testing/tourism-monthly-batch", filename="train-batch.pt", repo_type="dataset"
... )
>>> batch = torch.load(file)

>>> model = TimeSeriesTransformerForPrediction.from_pretrained(
...     "huggingface/time-series-transformer-tourism-monthly"
... )

>>> # during training, one provides both past and future values
>>> # as well as possible additional features
>>> outputs = model(
...     past_values=batch["past_values"],
...     past_time_features=batch["past_time_features"],
...     past_observed_mask=batch["past_observed_mask"],
...     static_categorical_features=batch["static_categorical_features"],
...     static_real_features=batch["static_real_features"],
...     future_values=batch["future_values"],
...     future_time_features=batch["future_time_features"],
... )

>>> loss = outputs.loss
>>> loss.backward()

>>> # during inference, one only provides past values
>>> # as well as possible additional features
>>> # the model autoregressively generates future values
>>> outputs = model.generate(
...     past_values=batch["past_values"],
...     past_time_features=batch["past_time_features"],
...     past_observed_mask=batch["past_observed_mask"],
...     static_categorical_features=batch["static_categorical_features"],
...     static_real_features=batch["static_real_features"],
...     future_time_features=batch["future_time_features"],
... )

>>> mean_prediction = outputs.sequences.mean(dim=1)
```


def _get_executor_init(self, workers):

    def pool_fn(seqs):
        pool = get_pool_class(True)(workers, initializer=init_pool_generator, initargs=(seqs, self.random_seed, get_worker_id_queue()))
        _DATA_POOLS.add(pool)
        return pool
    return pool_fn

Gets the Pool initializer for multiprocessing.

Args:
  workers: Number of works.

Returns:
    A Function to initialize the pool


def clip_by_value(t, clip_value_min, clip_value_max, name=None):
    with ops.name_scope(name, 'clip_by_value', [t, clip_value_min, clip_value_max]) as name:
        values = ops.convert_to_tensor(t.values if isinstance(t, indexed_slices.IndexedSlices) else t, name='t')
        t_min = math_ops.minimum(values, clip_value_max)
        values.shape.assert_is_compatible_with(t_min.shape)
        t_max = math_ops.maximum(t_min, clip_value_min, name=name)
        values.shape.assert_is_compatible_with(t_max.shape)
        if isinstance(t, indexed_slices.IndexedSlices):
            t_max = indexed_slices.IndexedSlices(t_max, t.indices, t.dense_shape)
    return t_max

Clips tensor values to a specified min and max.

Given a tensor `t`, this operation returns a tensor of the same type and
shape as `t` with its values clipped to `clip_value_min` and `clip_value_max`.
Any values less than `clip_value_min` are set to `clip_value_min`. Any values
greater than `clip_value_max` are set to `clip_value_max`.

Note: `clip_value_min` needs to be smaller or equal to `clip_value_max` for
correct results.

For example:

Basic usage passes a scalar as the min and max value.

>>> t = tf.constant([[-10., -1., 0.], [0., 2., 10.]])
>>> t2 = tf.clip_by_value(t, clip_value_min=-1, clip_value_max=1)
>>> t2.numpy()
array([[-1., -1.,  0.],
       [ 0.,  1.,  1.]], dtype=float32)

The min and max can be the same size as `t`, or broadcastable to that size.

>>> t = tf.constant([[-1, 0., 10.], [-1, 0, 10]])
>>> clip_min = [[2],[1]]
>>> t3 = tf.clip_by_value(t, clip_value_min=clip_min, clip_value_max=100)
>>> t3.numpy()
array([[ 2.,  2., 10.],
       [ 1.,  1., 10.]], dtype=float32)

Broadcasting fails, intentionally, if you would expand the dimensions of `t`

>>> t = tf.constant([[-1, 0., 10.], [-1, 0, 10]])
>>> clip_min = [[[2, 1]]] # Has a third axis
>>> t4 = tf.clip_by_value(t, clip_value_min=clip_min, clip_value_max=100)
Traceback (most recent call last):
...
InvalidArgumentError: Incompatible shapes: [2,3] vs. [1,1,2]

It throws a `TypeError` if you try to clip an `int` to a `float` value
(`tf.cast` the input to `float` first).

>>> t = tf.constant([[1, 2], [3, 4]], dtype=tf.int32)
>>> t5 = tf.clip_by_value(t, clip_value_min=-3.1, clip_value_max=3.1)
Traceback (most recent call last):
...
TypeError: Cannot convert ...

Args:
  t: A `Tensor` or `IndexedSlices`.
  clip_value_min: The minimum value to clip to. A scalar `Tensor` or one that
    is broadcastable to the shape of `t`.
  clip_value_max: The maximum value to clip to. A scalar `Tensor` or one that
    is broadcastable to the shape of `t`.
  name: A name for the operation (optional).

Returns:
  A clipped `Tensor` or `IndexedSlices`.

Raises:
  `tf.errors.InvalidArgumentError`: If the clip tensors would trigger array
    broadcasting that would make the returned tensor larger than the input.
  TypeError: If dtype of the input is `int32` and dtype of
    the `clip_value_min` or `clip_value_max` is `float32`


class DepthProConfig(PretrainedConfig):
    model_type = 'depth_pro'
    sub_configs = {'image_model_config': AutoConfig, 'patch_model_config': AutoConfig, 'fov_model_config': AutoConfig}

    def __init__(self, fusion_hidden_size=256, patch_size=384, initializer_range=0.02, intermediate_hook_ids=[11, 5], intermediate_feature_dims=[256, 256], scaled_images_ratios=[0.25, 0.5, 1], scaled_images_overlap_ratios=[0.0, 0.5, 0.25], scaled_images_feature_dims=[1024, 1024, 512], merge_padding_value=3, use_batch_norm_in_fusion_residual=False, use_bias_in_fusion_residual=True, use_fov_model=False, num_fov_head_layers=2, image_model_config=None, patch_model_config=None, fov_model_config=None, **kwargs):
        super().__init__(**kwargs)
        if scaled_images_ratios != sorted(scaled_images_ratios):
            raise ValueError(f'Values in scaled_images_ratios={scaled_images_ratios} should be sorted from low to high')
        if not len(scaled_images_ratios) == len(scaled_images_overlap_ratios) == len(scaled_images_feature_dims):
            raise ValueError(f'len(scaled_images_ratios)={len(scaled_images_ratios)} and len(scaled_images_overlap_ratios)={len(scaled_images_overlap_ratios)} and len(scaled_images_feature_dims)={len(scaled_images_feature_dims)}, should match in config.')
        if not len(intermediate_hook_ids) == len(intermediate_feature_dims):
            raise ValueError(f'len(intermediate_hook_ids)={len(intermediate_hook_ids)} and len(intermediate_feature_dims)={len(intermediate_feature_dims)}, should match in config.')
        if fusion_hidden_size // 2 ** num_fov_head_layers == 0:
            raise ValueError(f'fusion_hidden_size={fusion_hidden_size} should be consistent with num_fov_head_layers={num_fov_head_layers} i.e fusion_hidden_size // 2**num_fov_head_layers > 0')
        self.fusion_hidden_size = fusion_hidden_size
        self.patch_size = patch_size
        self.initializer_range = initializer_range
        self.use_batch_norm_in_fusion_residual = use_batch_norm_in_fusion_residual
        self.use_bias_in_fusion_residual = use_bias_in_fusion_residual
        self.use_fov_model = use_fov_model
        self.num_fov_head_layers = num_fov_head_layers
        self.intermediate_hook_ids = intermediate_hook_ids
        self.intermediate_feature_dims = intermediate_feature_dims
        self.scaled_images_ratios = scaled_images_ratios
        self.scaled_images_overlap_ratios = scaled_images_overlap_ratios
        self.scaled_images_feature_dims = scaled_images_feature_dims
        self.merge_padding_value = merge_padding_value
        self.image_model_config = image_model_config
        self.patch_model_config = patch_model_config
        self.fov_model_config = fov_model_config
        for sub_config_key in self.sub_configs.keys():
            sub_config = getattr(self, sub_config_key)
            if sub_config is None:
                sub_config = CONFIG_MAPPING['dinov2'](image_size=patch_size)
                logger.info(f'`{sub_config_key}` is `None`. Initializing `{sub_config_key}` with the `Dinov2Config` with default values except `{sub_config_key}.image_size` is set to `config.patch_size`.')
            elif isinstance(sub_config, dict):
                sub_config = deepcopy(sub_config)
                if 'model_type' not in sub_config:
                    raise KeyError(f'The `model_type` key is missing in the `{sub_config_key}` dictionary. Please provide the model type.')
                elif sub_config['model_type'] not in CONFIG_MAPPING:
                    raise ValueError(f'The model type `{sub_config['model_type']}` in `{sub_config_key}` is not supported. Please provide a valid model type.')
                image_size = sub_config.get('image_size')
                if image_size != patch_size:
                    logger.info(f'The `image_size` in `{sub_config_key}` is set to `{image_size}`, but it does not match the required `patch_size` of `{patch_size}`. Updating `image_size` to `{patch_size}` for consistency. Ensure that `image_size` aligns with `patch_size` in the configuration.')
                    sub_config.update({'image_size': patch_size})
                sub_config = CONFIG_MAPPING[sub_config['model_type']](**sub_config)
            elif isinstance(sub_config, PretrainedConfig):
                sub_config = sub_config
                image_size = getattr(sub_config, 'image_size', None)
                if image_size != patch_size:
                    raise ValueError(f'`config.{sub_config_key}.image_size={image_size}` should match `config.patch_size={patch_size}`.')
            else:
                raise TypeError(f'Invalid type for `sub_config`. Expected `PretrainedConfig`, `dict`, or `None`, but got {type(sub_config)}.')
            setattr(self, sub_config_key, sub_config)

This is the configuration class to store the configuration of a [`DepthProModel`]. It is used to instantiate a
DepthPro 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 DepthPro
[apple/DepthPro](https://huggingface.co/apple/DepthPro) architecture.

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

Args:
    fusion_hidden_size (`int`, *optional*, defaults to 256):
        The number of channels before fusion.
    patch_size (`int`, *optional*, defaults to 384):
        The size (resolution) of each patch. This is also the image_size for backbone model.
    initializer_range (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    intermediate_hook_ids (`List[int]`, *optional*, defaults to `[11, 5]`):
        Indices of the intermediate hidden states from the patch encoder to use for fusion.
    intermediate_feature_dims (`List[int]`, *optional*, defaults to `[256, 256]`):
        Hidden state dimensions during upsampling for each intermediate hidden state in `intermediate_hook_ids`.
    scaled_images_ratios (`List[float]`, *optional*, defaults to `[0.25, 0.5, 1]`):
        Ratios of scaled images to be used by the patch encoder.
    scaled_images_overlap_ratios (`List[float]`, *optional*, defaults to `[0.0, 0.5, 0.25]`):
        Overlap ratios between patches for each scaled image in `scaled_images_ratios`.
    scaled_images_feature_dims (`List[int]`, *optional*, defaults to `[1024, 1024, 512]`):
        Hidden state dimensions during upsampling for each scaled image in `scaled_images_ratios`.
    merge_padding_value (`int`, *optional*, defaults to 3):
        When merging smaller patches back to the image size, overlapping sections of this size are removed.
    use_batch_norm_in_fusion_residual (`bool`, *optional*, defaults to `False`):
        Whether to use batch normalization in the pre-activate residual units of the fusion blocks.
    use_bias_in_fusion_residual (`bool`, *optional*, defaults to `True`):
        Whether to use bias in the pre-activate residual units of the fusion blocks.
    use_fov_model (`bool`, *optional*, defaults to `False`):
        Whether to use `DepthProFovModel` to generate the field of view.
    num_fov_head_layers (`int`, *optional*, defaults to 2):
        Number of convolution layers in the head of `DepthProFovModel`.
    image_model_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*):
        The configuration of the image encoder model, which is loaded using the [`AutoModel`] API.
        By default, Dinov2 model is used as backbone.
    patch_model_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*):
        The configuration of the patch encoder model, which is loaded using the [`AutoModel`] API.
        By default, Dinov2 model is used as backbone.
    fov_model_config (`Union[Dict[str, Any], PretrainedConfig]`, *optional*):
        The configuration of the fov encoder model, which is loaded using the [`AutoModel`] API.
        By default, Dinov2 model is used as backbone.

Example:

```python
>>> from transformers import DepthProConfig, DepthProModel

>>> # Initializing a DepthPro apple/DepthPro style configuration
>>> configuration = DepthProConfig()

>>> # Initializing a model (with random weights) from the apple/DepthPro style configuration
>>> model = DepthProModel(configuration)

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


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 None:
        return [1] + [0] * len(token_ids_0) + [1]
    return [1] + [0] * len(token_ids_0) + [1, 1] + [0] * len(token_ids_1) + [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.


class AveragePooling1D(keras_layers.AveragePooling1D, base.Layer):

    def __init__(self, pool_size, strides, padding='valid', data_format='channels_last', name=None, **kwargs):
        if strides is None:
            raise ValueError('Argument `strides` must not be None.')
        super(AveragePooling1D, self).__init__(pool_size=pool_size, strides=strides, padding=padding, data_format=data_format, name=name, **kwargs)

Average Pooling layer for 1D inputs.

Args:
  pool_size: An integer or tuple/list of a single integer,
    representing the size of the pooling window.
  strides: An integer or tuple/list of a single integer, specifying the
    strides of the pooling operation.
  padding: A string. The padding method, either 'valid' or 'same'.
    Case-insensitive.
  data_format: A string, one of `channels_last` (default) or `channels_first`.
    The ordering of the dimensions in the inputs.
    `channels_last` corresponds to inputs with shape
    `(batch, length, channels)` while `channels_first` corresponds to
    inputs with shape `(batch, channels, length)`.
  name: A string, the name of the layer.


def compute_gradient_error(x, x_shape, y, y_shape, x_init_value=None, delta=0.001, init_targets=None, extra_feed_dict=None):
    grad = compute_gradient(x, x_shape, y, y_shape, x_init_value, delta, init_targets, extra_feed_dict=extra_feed_dict)
    return _compute_error(grad)

Computes the gradient error.

Computes the maximum error for dy/dx between the computed Jacobian and the
numerically estimated Jacobian.

This function will modify the tensors passed in as it adds more operations
and hence changing the consumers of the operations of the input tensors.

This function adds operations to the current session. To compute the error
using a particular device, such as a GPU, use the standard methods for
setting a device (e.g. using with sess.graph.device() or setting a device
function in the session constructor).

Args:
  x: a tensor or list of tensors
  x_shape: the dimensions of x as a tuple or an array of ints. If x is a list,
  then this is the list of shapes.
  y: a tensor
  y_shape: the dimensions of y as a tuple or an array of ints.
  x_init_value: (optional) a numpy array of the same shape as "x"
    representing the initial value of x. If x is a list, this should be a list
    of numpy arrays.  If this is none, the function will pick a random tensor
    as the initial value.
  delta: (optional) the amount of perturbation.
  init_targets: list of targets to run to initialize model params.
  extra_feed_dict: dict that allows fixing specified tensor values
    during the Jacobian calculation.

Returns:
  The maximum error in between the two Jacobians.


def start(logdir, options=None):
    global _profiler
    with _profiler_lock:
        if _profiler is not None:
            raise errors.AlreadyExistsError(None, None, 'Another profiler is running.')
        _profiler = _pywrap_profiler.ProfilerSession()
        try:
            opts = dict(options._asdict()) if options is not None else {}
            _profiler.start(logdir, opts)
        except errors.AlreadyExistsError:
            logging.warning('Another profiler session is running which is probably created by profiler server. Please avoid using profiler server and profiler APIs at the same time.')
            raise errors.AlreadyExistsError(None, None, 'Another profiler is running.')
        except Exception:
            _profiler = None
            raise

Start profiling TensorFlow performance.

Args:
  logdir: Profiling results log directory.
  options: `ProfilerOptions` namedtuple to specify miscellaneous profiler
    options. See example usage below.

Raises:
  AlreadyExistsError: If a profiling session is already running.

Example usage:
```python
options = tf.profiler.experimental.ProfilerOptions(host_tracer_level = 3,
                                                   python_tracer_level = 1,
                                                   device_tracer_level = 1)
tf.profiler.experimental.start('logdir_path', options = options)
# Training code here
tf.profiler.experimental.stop()
```

To view the profiling results, launch TensorBoard and point it to `logdir`.
Open your browser and go to `localhost:6006/#profile` to view profiling
results.


class Blip2VisionModelOutput(ModelOutput):
    image_embeds: Optional[torch.FloatTensor] = None
    last_hidden_state: Optional[torch.FloatTensor] = None
    hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None
    attentions: Optional[Tuple[torch.FloatTensor, ...]] = None

Base class for vision model's outputs that also contains image embeddings of the pooling of the last hidden states.

Args:
    image_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim)` *optional* returned when model is initialized with `with_projection=True`):
        The image embeddings obtained by applying the projection layer to the pooler_output.
    last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
        Sequence of hidden-states at the output of the last layer of the model.
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
        one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

        Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
    attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
        sequence_length)`.

        Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
        heads.


class AssertEqual(beam.PTransform):

    def __init__(self, elements: Iterable[Any]):
        self._elements = elements

    def expand(self, pcoll):
        return assert_that(pcoll | beam.Map(lambda row: beam.Row(**row._asdict())), equal_to(dicts_to_rows(self._elements)))

Asserts that the input contains exactly the elements provided.

This is primarily used for testing; it will cause the entire pipeline to
fail if the input to this transform is not exactly the set of `elements`
given in the config parameter.

As with Create, YAML/JSON-style mappings are interpreted as Beam rows,
e.g.::

    type: AssertEqual
    input: SomeTransform
    config:
      elements:
         - {a: 0, b: "foo"}
         - {a: 1, b: "bar"}

would ensure that `SomeTransform` produced exactly two elements with values
`(a=0, b="foo")` and `(a=1, b="bar")` respectively.

Args:
    elements: The set of elements that should belong to the PCollection.
        YAML/JSON-style mappings will be interpreted as Beam rows.


def __init__(self, model_uri: str, model_type: ModelType=ModelType.SAVED_MODEL, create_model_fn: Optional[Callable]=None, *, load_model_args: Optional[dict[str, Any]]=None, custom_weights: str='', inference_fn: TensorInferenceFn=default_numpy_inference_fn, min_batch_size: Optional[int]=None, max_batch_size: Optional[int]=None, max_batch_duration_secs: Optional[int]=None, large_model: bool=False, model_copies: Optional[int]=None, **kwargs):
    self._model_uri = model_uri
    self._model_type = model_type
    self._inference_fn = inference_fn
    self._create_model_fn = create_model_fn
    self._env_vars = kwargs.get('env_vars', {})
    self._load_model_args = {} if not load_model_args else load_model_args
    self._custom_weights = custom_weights
    self._batching_kwargs = {}
    if min_batch_size is not None:
        self._batching_kwargs['min_batch_size'] = min_batch_size
    if max_batch_size is not None:
        self._batching_kwargs['max_batch_size'] = max_batch_size
    if max_batch_duration_secs is not None:
        self._batching_kwargs['max_batch_duration_secs'] = max_batch_duration_secs
    self._share_across_processes = large_model or model_copies is not None
    self._model_copies = model_copies or 1

Implementation of the ModelHandler interface for Tensorflow.

Example Usage::

  pcoll | RunInference(TFModelHandlerNumpy(model_uri="my_uri"))

See https://www.tensorflow.org/tutorials/keras/save_and_load for details.

Args:
    model_uri (str): path to the trained model.
    model_type: type of model to be loaded. Defaults to SAVED_MODEL.
    create_model_fn: a function that creates and returns a new
      tensorflow model to load the saved weights.
      It should be used with ModelType.SAVED_WEIGHTS.
    load_model_args: a dictionary of parameters to pass to the load_model
      function of TensorFlow to specify custom config.
    custom_weights (str): path to the custom weights to be applied
      once the model is loaded.
    inference_fn: inference function to use during RunInference.
      Defaults to default_numpy_inference_fn.
    large_model: set to true if your model is large enough to run into
      memory pressure if you load multiple copies. Given a model that
      consumes N memory and a machine with W cores and M memory, you should
      set this to True if N*W > M.
    model_copies: The exact number of models that you would like loaded
      onto your machine. This can be useful if you exactly know your CPU or
      GPU capacity and want to maximize resource utilization.
    kwargs: 'env_vars' can be used to set environment variables
      before loading the model.

**Supported Versions:** RunInference APIs in Apache Beam have been tested
with Tensorflow 2.9, 2.10, 2.11.


def svd(x, full_matrices=True, compute_uv=True):
    if any_symbolic_tensors((x,)):
        return SVD(full_matrices, compute_uv).symbolic_call(x)
    return _svd(x, full_matrices, compute_uv)

Computes the singular value decomposition of a matrix.

Args:
    x: Input tensor of shape `(..., M, N)`.

Returns:
    A tuple of three tensors: a tensor of shape `(..., M, M)` containing the
    left singular vectors, a tensor of shape `(..., M, N)` containing the
    singular values and a tensor of shape `(..., N, N)` containing the
    right singular vectors.


def _gather_saveables_for_checkpoint(self):
    return getattr(self, '_self_saveable_object_factories', {})

Returns a dictionary of values to checkpoint with this object.

NOTE: This method is deprecated, prefer implementing `_serialize_to_tensors`
and `_restore_from_tensors` instead. This method is only used in the
deprecated `tf.compat.v1.train.Saver`.

Keys in the returned dictionary are local to this object and in a separate
namespace from dependencies. Values may either be `SaveableObject` factories
or variables easily converted to `SaveableObject`s (as in
`tf.compat.v1.train.Saver`'s
`var_list` constructor argument).

`SaveableObjects` have a name set, which Trackable needs to generate
itself. So rather than returning `SaveableObjects` directly, this method
should return a dictionary of callables which take `name` arguments and
return `SaveableObjects` with that name.

If this object may also be passed to the global-name-based
`tf.compat.v1.train.Saver`,
the returned callables should have a default value for their name argument
(i.e. be callable with no arguments).

Returned values must be saved only by this object; if any value may be
shared, it should instead be a dependency. For example, variable objects
save their own values with the key `VARIABLE_VALUE_KEY`, but objects which
reference variables simply add a dependency.

**AsyncCheckpoint Support**
If your Trackable implements `_gather_saveables_for_checkpoint`,
`_copy_trackable_to_cpu` needs to be implemented as well to support
asynchronous checkpoint.

Returns:
  The dictionary mapping attribute names to `SaveableObject` factories
  described above. For example:
  {VARIABLE_VALUE_KEY:
   lambda name="global_name_for_this_object":
   SaveableObject(name=name, ...)}


class RealmScorer(RealmPreTrainedModel):

    def __init__(self, config, query_embedder=None):
        super().__init__(config)
        self.embedder = RealmEmbedder(self.config)
        self.query_embedder = query_embedder if query_embedder is not None else self.embedder
        self.post_init()

    @add_start_docstrings_to_model_forward(REALM_INPUTS_DOCSTRING.format('batch_size, sequence_length'))
    @replace_return_docstrings(output_type=RealmScorerOutput, config_class=_CONFIG_FOR_DOC)
    def forward(self, input_ids: Optional[torch.LongTensor]=None, attention_mask: Optional[torch.FloatTensor]=None, token_type_ids: Optional[torch.LongTensor]=None, position_ids: Optional[torch.LongTensor]=None, candidate_input_ids: Optional[torch.LongTensor]=None, candidate_attention_mask: Optional[torch.FloatTensor]=None, candidate_token_type_ids: Optional[torch.LongTensor]=None, candidate_inputs_embeds: Optional[torch.FloatTensor]=None, head_mask: Optional[torch.FloatTensor]=None, inputs_embeds: Optional[torch.FloatTensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple, RealmScorerOutput]:
        """
        candidate_input_ids (`torch.LongTensor` of shape `(batch_size, num_candidates, sequence_length)`):
            Indices of candidate input sequence tokens in the vocabulary.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        candidate_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_candidates, 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)
        candidate_token_type_ids (`torch.LongTensor` of shape `(batch_size, num_candidates, sequence_length)`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        candidate_inputs_embeds (`torch.FloatTensor` of shape `(batch_size * num_candidates, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `candidate_input_ids` you can choose to directly pass an embedded
            representation. This is useful if you want more control over how to convert *candidate_input_ids* indices
            into associated vectors than the model's internal embedding lookup matrix.

        Returns:

        Example:

        ```python
        >>> import torch
        >>> from transformers import AutoTokenizer, RealmScorer

        >>> tokenizer = AutoTokenizer.from_pretrained("google/realm-cc-news-pretrained-scorer")
        >>> model = RealmScorer.from_pretrained("google/realm-cc-news-pretrained-scorer", num_candidates=2)

        >>> # batch_size = 2, num_candidates = 2
        >>> input_texts = ["How are you?", "What is the item in the picture?"]
        >>> candidates_texts = [["Hello world!", "Nice to meet you!"], ["A cute cat.", "An adorable dog."]]

        >>> inputs = tokenizer(input_texts, return_tensors="pt")
        >>> candidates_inputs = tokenizer.batch_encode_candidates(candidates_texts, max_length=10, return_tensors="pt")

        >>> outputs = model(
        ...     **inputs,
        ...     candidate_input_ids=candidates_inputs.input_ids,
        ...     candidate_attention_mask=candidates_inputs.attention_mask,
        ...     candidate_token_type_ids=candidates_inputs.token_type_ids,
        ... )
        >>> relevance_score = outputs.relevance_score
        ```"""
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        if input_ids is None and inputs_embeds is None:
            raise ValueError('You have to specify either input_ids or input_embeds.')
        if candidate_input_ids is None and candidate_inputs_embeds is None:
            raise ValueError('You have to specify either candidate_input_ids or candidate_inputs_embeds.')
        query_outputs = self.query_embedder(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
        flattened_input_ids, flattened_attention_mask, flattened_token_type_ids = self._flatten_inputs(candidate_input_ids, candidate_attention_mask, candidate_token_type_ids)
        candidate_outputs = self.embedder(flattened_input_ids, attention_mask=flattened_attention_mask, token_type_ids=flattened_token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=candidate_inputs_embeds, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
        query_score = query_outputs[0]
        candidate_score = candidate_outputs[0]
        candidate_score = candidate_score.view(-1, self.config.num_candidates, self.config.retriever_proj_size)
        relevance_score = torch.einsum('bd,bnd->bn', query_score, candidate_score)
        if not return_dict:
            return (relevance_score, query_score, candidate_score)
        return RealmScorerOutput(relevance_score=relevance_score, query_score=query_score, candidate_score=candidate_score)

Args:
    query_embedder ([`RealmEmbedder`]):
        Embedder for input sequences. If not specified, it will use the same embedder as candidate sequences.


def forward(self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, position_embeddings: Optional[torch.Tensor]=None, reference_points=None, spatial_shapes=None, spatial_shapes_list=None, level_start_index=None, output_attentions: bool=False):
    residual = hidden_states
    hidden_states, attn_weights = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask, encoder_hidden_states=hidden_states, encoder_attention_mask=attention_mask, position_embeddings=position_embeddings, reference_points=reference_points, spatial_shapes=spatial_shapes, spatial_shapes_list=spatial_shapes_list, level_start_index=level_start_index, output_attentions=output_attentions)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    residual = hidden_states
    hidden_states = self.activation_fn(self.fc1(hidden_states))
    hidden_states = nn.functional.dropout(hidden_states, p=self.activation_dropout, training=self.training)
    hidden_states = self.fc2(hidden_states)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.final_layer_norm(hidden_states)
    if self.training:
        if torch.isinf(hidden_states).any() or torch.isnan(hidden_states).any():
            clamp_value = torch.finfo(hidden_states.dtype).max - 1000
            hidden_states = torch.clamp(hidden_states, min=-clamp_value, max=clamp_value)
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (attn_weights,)
    return outputs

Args:
    hidden_states (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
        Input to the layer.
    attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`):
        Attention mask.
    position_embeddings (`torch.FloatTensor`, *optional*):
        Position embeddings, to be added to `hidden_states`.
    reference_points (`torch.FloatTensor`, *optional*):
        Reference points.
    spatial_shapes (`torch.LongTensor`, *optional*):
        Spatial shapes of the backbone feature maps.
    level_start_index (`torch.LongTensor`, *optional*):
        Level start index.
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers. See `attentions` under
        returned tensors for more detail.


def _copy_assets(src_path: str, dst_path: str) -> None:
    for assets_dir_name in [_ASSETS_DIR, _ASSETS_EXTRA_DIR]:
        src_assets_path = file_io.join(src_path, assets_dir_name)
        if not file_io.file_exists_v2(src_assets_path):
            continue
        dst_assets_path = file_io.join(dst_path, assets_dir_name)
        file_io.create_dir_v2(dst_assets_path)
        for curr_dir, _, files in file_io.walk_v2(src_assets_path):
            for asset_file_name in files:
                src_asset_file = file_io.join(curr_dir, asset_file_name)
                curr_dst_dir = curr_dir.replace(src_assets_path, dst_assets_path)
                dst_asset_file = file_io.join(curr_dst_dir, asset_file_name)
                file_io.copy_v2(src_asset_file, dst_asset_file)
                logging.info('Copied asset file: %s -> %s', src_asset_file, dst_asset_file)

Copies the assets directory of the saved model.

Clones the contents of the assets/ directory from the source saved model
directory to the destination saved model directory. Nothing will be copied if
there are no assets directory in the source directory.

Args:
  src_path: Source saved model directory.
  dst_path: Destination saved model directory. This directory must exist.


def create_database_view(self, view: views.View, view_name: str) -> None:
    view_sql = f'CREATE OR REPLACE VIEW {self._view_dataset}.{view_name} AS\n{self.to_sql(view)}'
    self._engine.execute(view_sql).fetchall()

Creates a Spark view with the given name in the runner's view_dataset.

Args:
  view: the FHIR view that creates
  view_name: the view name passed to the CREATE OR REPLACE VIEW statement.


def from_encoder_decoder_pretrained(cls, encoder_pretrained_model_name_or_path: Optional[str]=None, decoder_pretrained_model_name_or_path: Optional[str]=None, *model_args, **kwargs) -> PreTrainedModel:
    kwargs_encoder = {argument[len('encoder_'):]: value for argument, value in kwargs.items() if argument.startswith('encoder_')}
    kwargs_decoder = {argument[len('decoder_'):]: value for argument, value in kwargs.items() if argument.startswith('decoder_')}
    for key in kwargs_encoder.keys():
        del kwargs['encoder_' + key]
    for key in kwargs_decoder.keys():
        del kwargs['decoder_' + key]
    encoder = kwargs_encoder.pop('model', None)
    if encoder is None:
        if encoder_pretrained_model_name_or_path is None:
            raise ValueError('If `encoder_model` is not defined as an argument, a `encoder_pretrained_model_name_or_path` has to be defined.')
        if 'config' not in kwargs_encoder:
            encoder_config, kwargs_encoder = AutoConfig.from_pretrained(encoder_pretrained_model_name_or_path, **kwargs_encoder, return_unused_kwargs=True)
            if encoder_config.is_decoder is True or encoder_config.add_cross_attention is True:
                logger.info(f'Initializing {encoder_pretrained_model_name_or_path} as a encoder model from a decoder model. Cross-attention and casual mask are disabled.')
                encoder_config.is_decoder = False
                encoder_config.add_cross_attention = False
            kwargs_encoder['config'] = encoder_config
        encoder = AutoModel.from_pretrained(encoder_pretrained_model_name_or_path, *model_args, **kwargs_encoder)
    decoder = kwargs_decoder.pop('model', None)
    if decoder is None:
        if decoder_pretrained_model_name_or_path is None:
            raise ValueError('If `decoder_model` is not defined as an argument, a `decoder_pretrained_model_name_or_path` has to be defined.')
        if 'config' not in kwargs_decoder:
            decoder_config, kwargs_decoder = AutoConfig.from_pretrained(decoder_pretrained_model_name_or_path, **kwargs_decoder, return_unused_kwargs=True)
            if decoder_config.is_decoder is False or decoder_config.add_cross_attention is False:
                logger.info(f"Initializing {decoder_pretrained_model_name_or_path} as a decoder model. Cross attention layers are added to {decoder_pretrained_model_name_or_path} and randomly initialized if {decoder_pretrained_model_name_or_path}'s architecture allows for cross attention layers.")
                decoder_config.is_decoder = True
                decoder_config.add_cross_attention = True
            kwargs_decoder['config'] = decoder_config
        if kwargs_decoder['config'].is_decoder is False or kwargs_decoder['config'].add_cross_attention is False:
            logger.warning(f'Decoder model {decoder_pretrained_model_name_or_path} is not initialized as a decoder. In order to initialize {decoder_pretrained_model_name_or_path} as a decoder, make sure that the attributes `is_decoder` and `add_cross_attention` of `decoder_config` passed to `.from_encoder_decoder_pretrained(...)` are set to `True` or do not pass a `decoder_config` to `.from_encoder_decoder_pretrained(...)`')
        decoder = AutoModelForCausalLM.from_pretrained(decoder_pretrained_model_name_or_path, **kwargs_decoder)
    config = VisionEncoderDecoderConfig.from_encoder_decoder_configs(encoder.config, decoder.config, **kwargs)
    config.tie_word_embeddings = False
    return cls(encoder=encoder, decoder=decoder, config=config)

Instantiate an encoder and a decoder from one or two base classes of the library from pretrained model
checkpoints.

The model is set in evaluation mode by default using `model.eval()` (Dropout modules are deactivated). To train
the model, you need to first set it back in training mode with `model.train()`.

Params:
    encoder_pretrained_model_name_or_path (`str`, *optional*):
        Information necessary to initiate the image encoder. Can be either:

            - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co. An
              example is `google/vit-base-patch16-224-in21k`.
            - A path to a *directory* containing model weights saved using
              [`~PreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.
            - A path or url to a *tensorflow index checkpoint file* (e.g, `./tf_model/model.ckpt.index`). In
              this case, `from_tf` should be set to `True` and a configuration object should be provided as
              `config` argument. This loading path is slower than converting the TensorFlow checkpoint in a
              PyTorch model using the provided conversion scripts and loading the PyTorch model afterwards.

    decoder_pretrained_model_name_or_path (`str`, *optional*, defaults to `None`):
        Information necessary to initiate the text decoder. Can be either:

            - A string, the *model id* of a pretrained model hosted inside a model repo on huggingface.co.
            - A path to a *directory* containing model weights saved using
              [`~PreTrainedModel.save_pretrained`], e.g., `./my_model_directory/`.
            - A path or url to a *tensorflow index checkpoint file* (e.g, `./tf_model/model.ckpt.index`). In
              this case, `from_tf` should be set to `True` and a configuration object should be provided as
              `config` argument. This loading path is slower than converting the TensorFlow checkpoint in a
              PyTorch model using the provided conversion scripts and loading the PyTorch model afterwards.

    model_args (remaining positional arguments, *optional*):
        All remaining positional arguments will be passed to the underlying model's `__init__` method.

    kwargs (remaining dictionary of keyword arguments, *optional*):
        Can be used to update the configuration object (after it being loaded) and initiate the model (e.g.,
        `output_attentions=True`).

        - To update the encoder configuration, use the prefix *encoder_* for each configuration parameter.
        - To update the decoder configuration, use the prefix *decoder_* for each configuration parameter.
        - To update the parent model configuration, do not use a prefix for each configuration parameter.

        Behaves differently depending on whether a `config` is provided or automatically loaded.

Example:

```python
>>> from transformers import VisionEncoderDecoderModel

>>> # initialize a vit-bert from a pretrained ViT and a pretrained BERT model. Note that the cross-attention layers will be randomly initialized
>>> model = VisionEncoderDecoderModel.from_encoder_decoder_pretrained(
...     "google/vit-base-patch16-224-in21k", "google-bert/bert-base-uncased"
... )
>>> # saving model after fine-tuning
>>> model.save_pretrained("./vit-bert")
>>> # load fine-tuned model
>>> model = VisionEncoderDecoderModel.from_pretrained("./vit-bert")
```


def forward_log_det_jacobian(self, x, event_ndims, name='forward_log_det_jacobian'):
    return self._call_forward_log_det_jacobian(x, event_ndims, name)

Returns both the forward_log_det_jacobian.

Args:
  x: `Tensor`. The input to the "forward" Jacobian determinant evaluation.
  event_ndims: Number of dimensions in the probabilistic events being
    transformed. Must be greater than or equal to
    `self.forward_min_event_ndims`. The result is summed over the final
    dimensions to produce a scalar Jacobian determinant for each event,
    i.e. it has shape `x.shape.ndims - event_ndims` dimensions.
  name: The name to give this op.

Returns:
  `Tensor`, if this bijector is injective.
    If not injective this is not implemented.

Raises:
  TypeError: if `self.dtype` is specified and `y.dtype` is not
    `self.dtype`.
  NotImplementedError: if neither `_forward_log_det_jacobian`
    nor {`_inverse`, `_inverse_log_det_jacobian`} are implemented, or
    this is a non-injective bijector.


def list_devices(device_type=None):
    device_type = device_type.upper() if device_type else None
    tf_devices = tf.config.list_logical_devices(device_type=device_type)
    cpu_devices = []
    other_devices = []
    for device in tf_devices:
        if device.device_type.lower() == 'cpu':
            cpu_devices.append(device)
        else:
            other_devices.append(device)
    if device_type is None:
        tf_devices = other_devices if len(other_devices) > 0 else cpu_devices
    return [f'{device.device_type.lower()}:{device.name.split(':')[-1]}' for device in tf_devices]

Return all the available devices based on the device type.

Note that this should return the global devices in a distributed setting.

Args:
    device_type: string of `"cpu"`, `"gpu"` or `"tpu"`. Default to `gpu` or
    `tpu` if available when device_type is not provided. Otherwise will
    return the `cpu` devices.

Return:
    List of devices that are available for distribute computation.


def _build_advisor_options(options):
    opts = tfprof_options_pb2.AdvisorOptionsProto()
    if options is None:
        return opts
    for checker, checker_opts in options.items():
        checker_ops_pb = tfprof_options_pb2.AdvisorOptionsProto.CheckerOption()
        for k, v in checker_opts.items():
            checker_ops_pb[k] = v
        opts.checkers[checker].MergeFrom(checker_ops_pb)
    return opts

Build tfprof.AdvisorOptionsProto.

Args:
  options: A dictionary of options. See ALL_ADVICE example.

Returns:
  tfprof.AdvisorOptionsProto.


def assert_sources_equal_reference_source(reference_source_info, sources_info):
    if not (isinstance(reference_source_info, tuple) and len(reference_source_info) == 3 and isinstance(reference_source_info[0], iobase.BoundedSource)):
        raise ValueError('reference_source_info must a three-tuple where firstitem of the tuple gives a iobase.BoundedSource. Received: %r' % reference_source_info)
    reference_records = read_from_source(*reference_source_info)
    source_records = []
    for source_info in sources_info:
        assert isinstance(source_info, tuple)
        assert len(source_info) == 3
        if not (isinstance(source_info, tuple) and len(source_info) == 3 and isinstance(source_info[0], iobase.BoundedSource)):
            raise ValueError('source_info must a three tuple where firstitem of the tuple gives a iobase.BoundedSource. Received: %r' % source_info)
        if type(reference_source_info[0].default_output_coder()) != type(source_info[0].default_output_coder()):
            raise ValueError('Reference source %r and the source %r must use the same coder. They are using %r and %r respectively instead.' % (reference_source_info[0], source_info[0], type(reference_source_info[0].default_output_coder()), type(source_info[0].default_output_coder())))
        source_records.extend(read_from_source(*source_info))
    if len(reference_records) != len(source_records):
        raise ValueError('Reference source must produce the same number of records as the list of sources. Number of records were %d and %d instead.' % (len(reference_records), len(source_records)))
    if equal_to(reference_records)(source_records):
        raise ValueError('Reference source and provided list of sources must produce the same set of records.')

Tests if a reference source is equal to a given set of sources.

Given a reference source (a :class:`~apache_beam.io.iobase.BoundedSource`
and a position range) and a list of sources, assert that the union of the
records read from the list of sources is equal to the records read from the
reference source.

Args:
  reference_source_info        (Tuple[~apache_beam.io.iobase.BoundedSource, int, int]):
    a three-tuple that gives the reference
    :class:`~apache_beam.io.iobase.BoundedSource`, position to start
    reading at, and position to stop reading at.
  sources_info        (Iterable[Tuple[~apache_beam.io.iobase.BoundedSource, int, int]]):
    a set of sources. Each source is a three-tuple that is of the same
    format described above.

Raises:
  ValueError: if the set of data produced by the reference source
    and the given set of sources are not equivalent.


def _get_dense_tensor(self, inputs, weight_collections=None, trainable=None):
    del weight_collections
    del trainable
    if isinstance(self.categorical_column, _SequenceCategoricalColumn):
        raise ValueError('In indicator_column: {}. categorical_column must not be of type _SequenceCategoricalColumn. Suggested fix A: If you wish to use input_layer, use a non-sequence categorical_column_with_*. Suggested fix B: If you wish to create sequence input, use sequence_input_layer instead of input_layer. Given (type {}): {}'.format(self.name, type(self.categorical_column), self.categorical_column))
    return inputs.get(self)

Returns dense `Tensor` representing feature.

Args:
  inputs: A `_LazyBuilder` object to access inputs.
  weight_collections: Unused `weight_collections` since no variables are
    created in this function.
  trainable: Unused `trainable` bool since no variables are created in this
    function.

Returns:
  Dense `Tensor` created within `_transform_feature`.

Raises:
  ValueError: If `categorical_column` is a `_SequenceCategoricalColumn`.


def from_pretrained(cls, pretrained_model_name_or_path, *inputs, **kwargs):
    use_auth_token = kwargs.pop('use_auth_token', None)
    if use_auth_token is not None:
        warnings.warn('The `use_auth_token` argument is deprecated and will be removed in v5 of Transformers. Please use `token` instead.', FutureWarning)
        if kwargs.get('token', None) is not None:
            raise ValueError('`token` and `use_auth_token` are both specified. Please set only the argument `token`.')
        kwargs['token'] = use_auth_token
    config = kwargs.pop('config', None)
    use_fast = kwargs.pop('use_fast', None)
    trust_remote_code = kwargs.pop('trust_remote_code', None)
    kwargs['_from_auto'] = True
    if 'image_processor_filename' in kwargs:
        image_processor_filename = kwargs.pop('image_processor_filename')
    elif is_timm_local_checkpoint(pretrained_model_name_or_path):
        image_processor_filename = CONFIG_NAME
    else:
        image_processor_filename = IMAGE_PROCESSOR_NAME
    try:
        config_dict, _ = ImageProcessingMixin.get_image_processor_dict(pretrained_model_name_or_path, image_processor_filename=image_processor_filename, **kwargs)
    except Exception as initial_exception:
        try:
            config_dict, _ = ImageProcessingMixin.get_image_processor_dict(pretrained_model_name_or_path, image_processor_filename=CONFIG_NAME, **kwargs)
        except Exception:
            raise initial_exception
        if not is_timm_config_dict(config_dict):
            raise initial_exception
    image_processor_type = config_dict.get('image_processor_type', None)
    image_processor_auto_map = None
    if 'AutoImageProcessor' in config_dict.get('auto_map', {}):
        image_processor_auto_map = config_dict['auto_map']['AutoImageProcessor']
    if image_processor_type is None and image_processor_auto_map is None:
        feature_extractor_class = config_dict.pop('feature_extractor_type', None)
        if feature_extractor_class is not None:
            image_processor_type = feature_extractor_class.replace('FeatureExtractor', 'ImageProcessor')
        if 'AutoFeatureExtractor' in config_dict.get('auto_map', {}):
            feature_extractor_auto_map = config_dict['auto_map']['AutoFeatureExtractor']
            image_processor_auto_map = feature_extractor_auto_map.replace('FeatureExtractor', 'ImageProcessor')
    if image_processor_type is None and image_processor_auto_map is None:
        if not isinstance(config, PretrainedConfig):
            config = AutoConfig.from_pretrained(pretrained_model_name_or_path, trust_remote_code=trust_remote_code, **kwargs)
        image_processor_type = getattr(config, 'image_processor_type', None)
        if hasattr(config, 'auto_map') and 'AutoImageProcessor' in config.auto_map:
            image_processor_auto_map = config.auto_map['AutoImageProcessor']
    image_processor_class = None
    if image_processor_type is not None:
        if use_fast is None:
            use_fast = image_processor_type.endswith('Fast')
            if not use_fast:
                logger.warning_once("Using a slow image processor as `use_fast` is unset and a slow processor was saved with this model. `use_fast=True` will be the default behavior in v4.52, even if the model was saved with a slow processor. This will result in minor differences in outputs. You'll still be able to use a slow processor with `use_fast=False`.")
        if use_fast and (not image_processor_type.endswith('Fast')):
            image_processor_type += 'Fast'
        if use_fast and (not is_torchvision_available()):
            image_processor_class = get_image_processor_class_from_name(image_processor_type[:-4])
            if image_processor_class is None:
                raise ValueError(f'`{image_processor_type}` requires `torchvision` to be installed. Please install `torchvision` and try again.')
            logger.warning_once('Using `use_fast=True` but `torchvision` is not available. Falling back to the slow image processor.')
            use_fast = False
        if use_fast:
            for _, image_processors in IMAGE_PROCESSOR_MAPPING_NAMES.items():
                if image_processor_type in image_processors:
                    break
            else:
                image_processor_type = image_processor_type[:-4]
                use_fast = False
                logger.warning_once('`use_fast` is set to `True` but the image processor class does not have a fast version.  Falling back to the slow version.')
            image_processor_class = get_image_processor_class_from_name(image_processor_type)
        else:
            image_processor_type_slow = image_processor_type[:-4] if image_processor_type.endswith('Fast') else image_processor_type
            image_processor_class = get_image_processor_class_from_name(image_processor_type_slow)
            if image_processor_class is None and image_processor_type.endswith('Fast'):
                raise ValueError(f'`{image_processor_type}` does not have a slow version. Please set `use_fast=True` when instantiating the processor.')
    has_remote_code = image_processor_auto_map is not None
    has_local_code = image_processor_class is not None or type(config) in IMAGE_PROCESSOR_MAPPING
    if has_remote_code:
        if image_processor_auto_map is not None and (not isinstance(image_processor_auto_map, tuple)):
            image_processor_auto_map = (image_processor_auto_map, None)
        if use_fast and image_processor_auto_map[1] is not None:
            class_ref = image_processor_auto_map[1]
        else:
            class_ref = image_processor_auto_map[0]
        if '--' in class_ref:
            upstream_repo = class_ref.split('--')[0]
        else:
            upstream_repo = None
        trust_remote_code = resolve_trust_remote_code(trust_remote_code, pretrained_model_name_or_path, has_local_code, has_remote_code, upstream_repo)
    if has_remote_code and trust_remote_code:
        if not use_fast and image_processor_auto_map[1] is not None:
            _warning_fast_image_processor_available(image_processor_auto_map[1])
        image_processor_class = get_class_from_dynamic_module(class_ref, pretrained_model_name_or_path, **kwargs)
        _ = kwargs.pop('code_revision', None)
        image_processor_class.register_for_auto_class()
        return image_processor_class.from_dict(config_dict, **kwargs)
    elif image_processor_class is not None:
        return image_processor_class.from_dict(config_dict, **kwargs)
    elif type(config) in IMAGE_PROCESSOR_MAPPING:
        image_processor_tuple = IMAGE_PROCESSOR_MAPPING[type(config)]
        image_processor_class_py, image_processor_class_fast = image_processor_tuple
        if not use_fast and image_processor_class_fast is not None:
            _warning_fast_image_processor_available(image_processor_class_fast)
        if image_processor_class_fast and (use_fast or image_processor_class_py is None):
            return image_processor_class_fast.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
        elif image_processor_class_py is not None:
            return image_processor_class_py.from_pretrained(pretrained_model_name_or_path, *inputs, **kwargs)
        else:
            raise ValueError('This image processor cannot be instantiated. Please make sure you have `Pillow` installed.')
    raise ValueError(f'Unrecognized image processor in {pretrained_model_name_or_path}. Should have a `image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following `model_type` keys in its {CONFIG_NAME}: {', '.join((c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys()))}')

Instantiate one of the image processor classes of the library from a pretrained model vocabulary.

The image processor class to instantiate is selected based on the `model_type` property of the config object
(either passed as an argument or loaded from `pretrained_model_name_or_path` if possible), or when it's
missing, by falling back to using pattern matching on `pretrained_model_name_or_path`:

List options

Params:
    pretrained_model_name_or_path (`str` or `os.PathLike`):
        This can be either:

        - a string, the *model id* of a pretrained image_processor hosted inside a model repo on
          huggingface.co.
        - a path to a *directory* containing a image processor file saved using the
          [`~image_processing_utils.ImageProcessingMixin.save_pretrained`] method, e.g.,
          `./my_model_directory/`.
        - a path or url to a saved image processor JSON *file*, e.g.,
          `./my_model_directory/preprocessor_config.json`.
    cache_dir (`str` or `os.PathLike`, *optional*):
        Path to a directory in which a downloaded pretrained model image processor should be cached if the
        standard cache should not be used.
    force_download (`bool`, *optional*, defaults to `False`):
        Whether or not to force to (re-)download the image processor files and override the cached versions if
        they exist.
    resume_download:
        Deprecated and ignored. All downloads are now resumed by default when possible.
        Will be removed in v5 of Transformers.
    proxies (`Dict[str, str]`, *optional*):
        A dictionary of proxy servers to use by protocol or endpoint, e.g., `{'http': 'foo.bar:3128',
        'http://hostname': 'foo.bar:4012'}.` The proxies are used on each request.
    token (`str` or *bool*, *optional*):
        The token to use as HTTP bearer authorization for remote files. If `True`, will use the token generated
        when running `huggingface-cli login` (stored in `~/.huggingface`).
    revision (`str`, *optional*, defaults to `"main"`):
        The specific model version to use. It can be a branch name, a tag name, or a commit id, since we use a
        git-based system for storing models and other artifacts on huggingface.co, so `revision` can be any
        identifier allowed by git.
    use_fast (`bool`, *optional*, defaults to `False`):
        Use a fast torchvision-base image processor if it is supported for a given model.
        If a fast image processor is not available for a given model, a normal numpy-based image processor
        is returned instead.
    return_unused_kwargs (`bool`, *optional*, defaults to `False`):
        If `False`, then this function returns just the final image processor object. If `True`, then this
        functions returns a `Tuple(image_processor, unused_kwargs)` where *unused_kwargs* is a dictionary
        consisting of the key/value pairs whose keys are not image processor attributes: i.e., the part of
        `kwargs` which has not been used to update `image_processor` and is otherwise ignored.
    trust_remote_code (`bool`, *optional*, defaults to `False`):
        Whether or not to allow for custom models defined on the Hub in their own modeling files. This option
        should only be set to `True` for repositories you trust and in which you have read the code, as it will
        execute code present on the Hub on your local machine.
    image_processor_filename (`str`, *optional*, defaults to `"config.json"`):
        The name of the file in the model directory to use for the image processor config.
    kwargs (`Dict[str, Any]`, *optional*):
        The values in kwargs of any keys which are image processor attributes will be used to override the
        loaded values. Behavior concerning key/value pairs whose keys are *not* image processor attributes is
        controlled by the `return_unused_kwargs` keyword parameter.

<Tip>

Passing `token=True` is required when you want to use a private model.

</Tip>

Examples:

```python
>>> from transformers import AutoImageProcessor

>>> # Download image processor from huggingface.co and cache.
>>> image_processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224-in21k")

>>> # If image processor files are in a directory (e.g. image processor was saved using *save_pretrained('./test/saved_model/')*)
>>> # image_processor = AutoImageProcessor.from_pretrained("./test/saved_model/")
```


def add_to_graph(self, g=None, overwrite=False):
    if not context.executing_eagerly() and (not g):
        g = ops.get_default_graph()
    if g is not None:
        g._add_function_recursive(self._delayed_rewrite_functions.forward())

Registers the function, adds it to the graph g or default graph.

Args:
  g: If specified, registers the function with this graph. Defaults to the
    current context (either the default graph or the eager context).
  overwrite: A bool. If True, its forward function will overwrite
    any existing function of the same signature name in the graph `g`.


def _abstractify_value(val: '_instances.ConcreteValue', ctx: 'context.Context', seen: 'set[_base.BaseValue] | None'=None) -> '_instances.ConcreteValue':
    if seen is None:
        seen = set()
    if not val.is_concrete or val in seen:
        return val
    seen = seen | {val}
    if not isinstance(val.pyval, (list, tuple)):
        return ctx.convert.get_maybe_abstract_instance(val)
    new_content = []
    for elem in val.pyval:
        new_elem_data = [_abstractify_value(v, ctx, seen) for v in elem.data]
        if any((v != new_v for v, new_v in zip(elem.data, new_elem_data))):
            new_elem = ctx.program.NewVariable()
            for b, new_data in zip(elem.bindings, new_elem_data):
                new_elem.PasteBindingWithNewData(b, new_data)
            new_content.append(new_elem)
        else:
            new_content.append(elem)
    if any((elem != new_elem for elem, new_elem in zip(val.pyval, new_content))):
        return type(val)(type(val.pyval)(new_content), ctx)
    else:
        return val

Converts a maybe-abstract value to a concrete one.

Args:
  val: A value.
  ctx: The context.
  seen: Optionally, a seen values set.

Unlike ctx.convert.get_maybe_abstract_instance, this method recursively
descends into lists and tuples.

Returns:
  A concrete value.


def format(self, compact: bool=False, verbose: bool=True, root_indent: int=0, **kwargs) -> str:

Formats this object into a string representation.

Args:
  compact: If True, this object will be formatted into a single line.
  verbose: If True, this object will be formatted with verbosity.
    Subclasses should define `verbosity` on their own.
  root_indent: The start indent level for this object if the output is a
    multi-line string.
  **kwargs: Subclass specific keyword arguments.

Returns:
  A string of formatted object.


def _count_op_with_name_and_attribute(self, nodes: Iterable[node_def_pb2.NodeDef], op_name: str, attr_name: str, attr_val: _AttrValType, get_op_name: bool=False) -> int:
    if get_op_name:
        return len([node.attr.get(attr_name) == attr_val for node in nodes if node.name == op_name])
    else:
        return len([node.attr.get(attr_name) == attr_val for node in nodes if node.op == op_name])

Determine the number of nodes whose operation name matches `op_name`.

If `attr_name` is given, additionally check if the `attr_val` matches with
the attribute value of the op.

Args:
  nodes: Iterable of NodeDefs.
  op_name: Name of the op to match.
  attr_name: Name of the attribute of the op to match.
  attr_val: Value of the attr_name to check.
  get_op_name: If set True, checks node.name rather than node.op.

Returns:
  The number of occurrences of nodes whose name match `op_name` and
  'attr_val' if 'attr_name' is given.


def pad_to_bounding_box(image, offset_height, offset_width, target_height, target_width):
    return pad_to_bounding_box_internal(image, offset_height, offset_width, target_height, target_width, check_dims=True)

Pad `image` with zeros to the specified `height` and `width`.

Adds `offset_height` rows of zeros on top, `offset_width` columns of
zeros on the left, and then pads the image on the bottom and right
with zeros until it has dimensions `target_height`, `target_width`.

This op does nothing if `offset_*` is zero and the image already has size
`target_height` by `target_width`.

Usage Example:

>>> x = [[[1., 2., 3.],
...       [4., 5., 6.]],
...       [[7., 8., 9.],
...       [10., 11., 12.]]]
>>> padded_image = tf.image.pad_to_bounding_box(x, 1, 1, 4, 4)
>>> padded_image
<tf.Tensor: shape=(4, 4, 3), dtype=float32, numpy=
array([[[ 0.,  0.,  0.],
[ 0.,  0.,  0.],
[ 0.,  0.,  0.],
[ 0.,  0.,  0.]],
[[ 0.,  0.,  0.],
[ 1.,  2.,  3.],
[ 4.,  5.,  6.],
[ 0.,  0.,  0.]],
[[ 0.,  0.,  0.],
[ 7.,  8.,  9.],
[10., 11., 12.],
[ 0.,  0.,  0.]],
[[ 0.,  0.,  0.],
[ 0.,  0.,  0.],
[ 0.,  0.,  0.],
[ 0.,  0.,  0.]]], dtype=float32)>

Args:
  image: 4-D Tensor of shape `[batch, height, width, channels]` or 3-D Tensor
    of shape `[height, width, channels]`.
  offset_height: Number of rows of zeros to add on top.
  offset_width: Number of columns of zeros to add on the left.
  target_height: Height of output image.
  target_width: Width of output image.

Returns:
  If `image` was 4-D, a 4-D float Tensor of shape
  `[batch, target_height, target_width, channels]`
  If `image` was 3-D, a 3-D float Tensor of shape
  `[target_height, target_width, channels]`

Raises:
  ValueError: If the shape of `image` is incompatible with the `offset_*` or
    `target_*` arguments, or either `offset_height` or `offset_width` is
    negative.


def sanitize_filename(filename):
    dirname = os.path.dirname(filename)
    basename = os.path.basename(filename)
    basename = _sanitize_windows_filename(basename)
    basename = _truncate_filename(basename, LINUX_MAX_FILENAME_LENGTH)
    basename = basename.replace(' ', '_')
    return os.path.join(dirname, basename)

Sanitizes a filename for various operating systems.

Args:
  filename: string, the filename to sanitize.

Returns:
  A string that is safe to use as a filename on various operating systems.


def aggregate_and_return_name_for_output(self, fused_op_name, output_index, out_graphdef):
    del fused_op_name, output_index, out_graphdef
    raise RuntimeError('Unimplemented abstract method.')

Add node(s) to graph representing output operands and returns type.

Args:
  fused_op_name: name of the fused op stub name.
  output_index: Output index that we are currently processing from stub.
  out_graphdef: The destination graphdef we are currently building up.

Returns:
  The datatype of this identity.

Raises:
  RuntimeError: if the method is not implemented.


def _prepare_images_structure(self, images: ImageInput) -> ImageInput:
    return make_flat_list_of_images(images)

Prepare the images structure for processing.

Args:
    images (`ImageInput`):
        The input images to process.

Returns:
    `ImageInput`: The images with a valid nesting.


def _generate_graph_update_dicts(self):
    transform_dict = {}
    pcoll_dict = {}
    for transform_id, transform_proto in self._top_level_transforms():
        transform_dict[transform_proto.unique_name] = {'required': transform_id in self._required_transforms}
        for pcoll_id in transform_proto.outputs.values():
            pcoll_dict[pcoll_id] = {'cached': pcoll_id in self._cached_pcollections, 'referenced': pcoll_id in self._referenced_pcollections}

    def vertex_properties_to_attributes(vertex):
        """Converts PCollection properties to DOT vertex attributes."""
        attrs = {}
        if 'leaf' in vertex:
            attrs['style'] = 'invis'
        elif vertex.get('required'):
            attrs['color'] = 'blue'
            attrs['fontcolor'] = 'blue'
        else:
            attrs['color'] = 'grey'
        return attrs

    def edge_properties_to_attributes(edge):
        """Converts PTransform properties to DOT edge attributes."""
        attrs = {}
        if edge.get('cached'):
            attrs['color'] = 'red'
        elif edge.get('referenced'):
            attrs['color'] = 'black'
        else:
            attrs['color'] = 'grey'
        return attrs
    vertex_dict = {}
    edge_dict = {}
    for transform_name, transform_properties in transform_dict.items():
        vertex_dict[transform_name] = vertex_properties_to_attributes(transform_properties)
    for pcoll_id, pcoll_properties in pcoll_dict.items():
        edge_dict[pcoll_id] = edge_properties_to_attributes(pcoll_properties)
    return (vertex_dict, edge_dict)

Generate updates specific to interactive pipeline.

Returns:
  vertex_dict: (Dict[str, Dict[str, str]]) maps vertex name to attributes
  edge_dict: (Dict[str, Dict[str, str]]) maps vertex name to attributes


def elu(x, alpha=1.0):
    if any_symbolic_tensors((x,)):
        return Elu(alpha).symbolic_call(x)
    return backend.nn.elu(x, alpha=alpha)

Exponential Linear Unit activation function.

It is defined as:

`f(x) =  alpha * (exp(x) - 1.) for x < 0`, `f(x) = x for x >= 0`.

Args:
    x: Input tensor.
    alpha: A scalar, slope of positive section. Defaults to `1.0`.

Returns:
    A tensor with the same shape as `x`.

Example:

>>> x = np.array([-1., 0., 1.])
>>> x_elu = keras.ops.elu(x)
>>> print(x_elu)
array([-0.63212055, 0., 1.], shape=(3,), dtype=float64)


class StateValueInTransition(stl.base.NamedObject):

    def __init__(self, state, value):
        stl.base.NamedObject.__init__(self, state)
        self.value = value
        self.param_values = []

    def __eq__(self, other):
        return stl.base.NamedObject.__eq__(self, other) and self.value == other.value and (self.param_values == other.param_values)

    def __str__(self):
        return 'STATE %s: p(%s) v(%s)' % (self.name, stl.base.GetCSV(self.param_values), str(self.value))

    def Resolve(self, env, resolved_params):
        logging.log(1, 'Resolving ' + self.name)
        states = env['_current_module'].states
        if self.name not in states:
            did_you_mean = stl.levenshtein.closest_candidate(self.name, states.keys())
            raise NameError('Cannot find a state to expand: %s. Did you mean %s?' % (self.name, did_you_mean))
        found = states[self.name]
        if len(self.param_values) != len(found.params):
            raise TypeError('Wrong number of parameters: %s. Found %d params, expected %d params.' % (found.name, len(found.params), len(self.param_values)))
        resolved_state = StateResolved(self.name, found)
        for v in self.param_values:
            resolved_state.resolved_params.append(v.Resolve(env, resolved_params))
        for v in found.values:
            if self.value == v:
                return StateValue(resolved_state, v)
        did_you_mean = stl.levenshtein.closest_candidate(self.value, found.values)
        raise NameError('Invalid value in state %s: %s. Did you mean %s?' % (self.name, self.value, did_you_mean))

State instance defined in a state transition spec.

A state instance is defined either in "pre_states", "post_states", or
"error_states" in a state transition spec. Resolve() turns this into
state.StateValue resolved.

Attributes:
  value: Current value of state of |name|.
  param_values: List of unresolved parameters of state of |name|.


def __init__(self, value_type, default: typing.Optional[numbers.Number]=MISSING_VALUE, min_value: typing.Optional[numbers.Number]=None, max_value: typing.Optional[numbers.Number]=None, is_noneable: bool=False, frozen: bool=False):
    if min_value is not None and max_value is not None and (min_value > max_value):
        raise ValueError(f'"max_value" must be equal or greater than "min_value". Encountered: min_value={min_value}, max_value={max_value}.')
    self._min_value = min_value
    self._max_value = max_value
    super().__init__(value_type, default, is_noneable=is_noneable, frozen=frozen)

Constructor.

Args:
  value_type: Type of number.
  default: Default value for this spec.
  min_value: (Optional) minimum value of acceptable values.
  max_value: (Optional) maximum value of acceptable values.
  is_noneable: If True, None is acceptable.
  frozen: If True, values other than the default value is not accceptable.


def main_op():
    init = variables.global_variables_initializer()
    init_local = variables.local_variables_initializer()
    init_tables = lookup_ops.tables_initializer()
    return control_flow_ops.group(init, init_local, init_tables)

Returns a main op to init variables and tables.

Returns the main op including the group of ops that initializes all
variables, initializes local variables and initialize all tables.

Returns:
  The set of ops to be run as part of the main op upon the load operation.


def on_test_batch_begin(self, batch, logs=None):

Called at the beginning of a batch in `evaluate` methods.

Also called at the beginning of a validation batch in the `fit`
methods, if validation data is provided.

Subclasses should override for any actions to run.

Note that if the `steps_per_execution` argument to `compile` in
`Model` is set to `N`, this method will only be called every
`N` batches.

Args:
    batch: Integer, index of batch within the current epoch.
    logs: Dict. Currently no data is passed to this argument for this
      method but that may change in the future.


def as_videos(content: ProcessorContentTypes, *, ignore_unsupported_types: bool=False) -> list[ProcessorPart]:
    return _as_format_helper(content, lambda mime: mime.startswith('video/'), ignore_unsupported_types)

Returns the video parts from the content.

Args:
  content: Input content.
  ignore_unsupported_types: By default if content contains non-video parts a
    ValueError would be raised. This argument allows ingoring such parts.

Returns:
  A list of video parts.


class ZeroPadding1D(Layer):

    def __init__(self, padding=1, **kwargs):
        super(ZeroPadding1D, self).__init__(**kwargs)
        self.padding = conv_utils.normalize_tuple(padding, 2, 'padding')
        self.input_spec = InputSpec(ndim=3)

    def compute_output_shape(self, input_shape):
        if input_shape[1] is not None:
            length = input_shape[1] + self.padding[0] + self.padding[1]
        else:
            length = None
        return tensor_shape.TensorShape([input_shape[0], length, input_shape[2]])

    def call(self, inputs):
        return backend.temporal_padding(inputs, padding=self.padding)

    def get_config(self):
        config = {'padding': self.padding}
        base_config = super(ZeroPadding1D, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

Zero-padding layer for 1D input (e.g. temporal sequence).

Examples:

>>> input_shape = (2, 2, 3)
>>> x = np.arange(np.prod(input_shape)).reshape(input_shape)
>>> print(x)
[[[ 0  1  2]
  [ 3  4  5]]
 [[ 6  7  8]
  [ 9 10 11]]]
>>> y = tf.keras.layers.ZeroPadding1D(padding=2)(x)
>>> print(y)
tf.Tensor(
  [[[ 0  0  0]
    [ 0  0  0]
    [ 0  1  2]
    [ 3  4  5]
    [ 0  0  0]
    [ 0  0  0]]
   [[ 0  0  0]
    [ 0  0  0]
    [ 6  7  8]
    [ 9 10 11]
    [ 0  0  0]
    [ 0  0  0]]], shape=(2, 6, 3), dtype=int64)

Args:
    padding: Int, or tuple of int (length 2), or dictionary.
        - If int:
        How many zeros to add at the beginning and end of
        the padding dimension (axis 1).
        - If tuple of int (length 2):
        How many zeros to add at the beginning and the end of
        the padding dimension (`(left_pad, right_pad)`).

Input shape:
    3D tensor with shape `(batch_size, axis_to_pad, features)`

Output shape:
    3D tensor with shape `(batch_size, padded_axis, features)`


class XSoftmax(torch.autograd.Function):

    @staticmethod
    def forward(ctx, input, mask, dim):
        ctx.dim = dim
        rmask = ~mask.to(torch.bool)
        output = input.masked_fill(rmask, torch.tensor(torch.finfo(input.dtype).min))
        output = torch.softmax(output, ctx.dim)
        output.masked_fill_(rmask, 0)
        ctx.save_for_backward(output)
        return output

    @staticmethod
    def backward(ctx, grad_output):
        output, = ctx.saved_tensors
        inputGrad = softmax_backward_data(ctx, grad_output, output, ctx.dim, output)
        return (inputGrad, None, None)

    @staticmethod
    def symbolic(g, self, mask, dim):
        import torch.onnx.symbolic_helper as sym_help
        from torch.onnx.symbolic_opset9 import masked_fill, softmax
        mask_cast_value = g.op('Cast', mask, to_i=sym_help.cast_pytorch_to_onnx['Long'])
        r_mask = g.op('Cast', g.op('Sub', g.op('Constant', value_t=torch.tensor(1, dtype=torch.int64)), mask_cast_value), to_i=sym_help.cast_pytorch_to_onnx['Bool'])
        output = masked_fill(g, self, r_mask, g.op('Constant', value_t=torch.tensor(torch.finfo(self.type().dtype()).min)))
        output = softmax(g, output, dim)
        return masked_fill(g, output, r_mask, g.op('Constant', value_t=torch.tensor(0, dtype=torch.bool)))

Masked Softmax which is optimized for saving memory

Args:
    input (`torch.tensor`): The input tensor that will apply softmax.
    mask (`torch.IntTensor`):
        The mask matrix where 0 indicate that element will be ignored in the softmax calculation.
    dim (int): The dimension that will apply softmax

Example:

```python
>>> import torch
>>> from transformers.models.deberta_v2.modeling_deberta_v2 import XSoftmax

>>> # Make a tensor
>>> x = torch.randn([4, 20, 100])

>>> # Create a mask
>>> mask = (x > 0).int()

>>> # Specify the dimension to apply softmax
>>> dim = -1

>>> y = XSoftmax.apply(x, mask, dim)
```


def _log_epoch_metrics(self, epoch, logs):
    if not logs:
        return
    train_logs = {k: v for k, v in logs.items() if not k.startswith('val_')}
    val_logs = {k: v for k, v in logs.items() if k.startswith('val_')}
    train_logs = self._collect_learning_rate(train_logs)
    if self.write_steps_per_second:
        train_logs['steps_per_second'] = self._compute_steps_per_second()
    if train_logs:
        with self._train_writer.as_default():
            for name, value in train_logs.items():
                self.summary.scalar('epoch_' + name, value, step=epoch)
    if val_logs:
        with self._val_writer.as_default():
            for name, value in val_logs.items():
                name = name[4:]
                self.summary.scalar('epoch_' + name, value, step=epoch)

Writes epoch metrics out as scalar summaries.

Args:
    epoch: Int. The global step to use for TensorBoard.
    logs: Dict. Keys are scalar summary names, values are scalars.