def reverse(tensor: ragged_tensor.Ragged, axis, name=None):
    type_error_msg = '`axis` must be a list of int or a constant tensorwhen reversing axes in a ragged tensor'
    with ops.name_scope(name, 'Reverse', [tensor, axis]):
        if isinstance(axis, tensor_lib.Tensor):
            axis = tensor_util.constant_value(axis)
            if axis is None:
                raise TypeError(type_error_msg)
        elif not (isinstance(axis, (list, tuple)) and all((isinstance(dim, int) for dim in axis))):
            raise TypeError(type_error_msg)
        tensor = ragged_tensor.convert_to_tensor_or_ragged_tensor(tensor, name='tensor')
        axis = [array_ops.get_positive_axis(dim, tensor.shape.rank, 'axis[%d]' % i, 'rank(tensor)') for i, dim in enumerate(axis)]
        slices = [slice(None)] * (max(axis) + 1 if axis else 0)
        for dim in axis:
            slices[dim] = slice(None, None, -1)
        return tensor[tuple(slices)]

Reverses a RaggedTensor along the specified axes.

#### Example:

>>> data = tf.ragged.constant([
...   [[1, 2], [3, 4]], [[5, 6]], [[7, 8], [9, 10], [11, 12]]])
>>> tf.reverse(data, axis=[0, 2])
<tf.RaggedTensor [[[8, 7], [10, 9], [12, 11]], [[6, 5]], [[2, 1], [4, 3]]]>

Args:
  tensor: A 'RaggedTensor' to reverse.
  axis: A list or tuple of 'int' or a constant 1D 'tf.Tensor'. The indices of
    the axes to reverse.
  name: A name prefix for the returned tensor (optional).

Returns:
  A 'RaggedTensor'.


def convert_op_hints_to_stubs(session=None, graph_def=None, write_callback=lambda graph_def, comments: None):
    if session is not None and graph_def is not None:
        raise ValueError('Provide only one of session and graph_def.')
    if session is not None:
        return _convert_op_hints_to_stubs_helper(session.graph_def, write_callback)
    elif graph_def is not None:
        return _convert_op_hints_to_stubs_helper(graph_def, write_callback)
    else:
        raise ValueError('Must specify session or graph_def as input.')

Converts a graphdef with LiteOp hints into stub operations.

This is used to prepare for toco conversion of complex intrinsic usages.
Note: only one of session or graph_def should be used, not both.

Args:
  session: A TensorFlow session that contains the graph to convert.
  graph_def: A graph def that we should convert.
  write_callback: A function pointer that can be used to write intermediate
    steps of graph transformation (optional).

Returns:
  A new graphdef with all ops contained in OpHints being replaced by
  a single op call with the right parameters.
Raises:
  ValueError: If both session and graph_def are provided.


def __call__(self, images: Optional[ImageInput]=None, text: Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]]=None, *args, audio=None, videos=None, **kwargs: Unpack[Owlv2ProcessorKwargs]) -> BatchFeature:
    output_kwargs = self._merge_kwargs(Owlv2ProcessorKwargs, tokenizer_init_kwargs=self.tokenizer.init_kwargs, **kwargs, **self.prepare_and_validate_optional_call_args(*args))
    query_images = output_kwargs['images_kwargs'].pop('query_images', None)
    return_tensors = output_kwargs['common_kwargs']['return_tensors']
    if text is None and query_images is None and (images is None):
        raise ValueError('You have to specify at least one text or query image or image. All three cannot be none.')
    images, text = _validate_images_text_input_order(images, text)
    data = {}
    if text is not None:
        if isinstance(text, str) or (isinstance(text, List) and (not isinstance(text[0], List))):
            encodings = [self.tokenizer(text, **output_kwargs['text_kwargs'])]
        elif isinstance(text, List) and isinstance(text[0], List):
            encodings = []
            max_num_queries = max([len(text_single) for text_single in text])
            for text_single in text:
                if len(text_single) != max_num_queries:
                    text_single = text_single + [' '] * (max_num_queries - len(text_single))
                encoding = self.tokenizer(text_single, **output_kwargs['text_kwargs'])
                encodings.append(encoding)
        else:
            raise TypeError('Input text should be a string, a list of strings or a nested list of strings')
        if return_tensors == 'np':
            input_ids = np.concatenate([encoding['input_ids'] for encoding in encodings], axis=0)
            attention_mask = np.concatenate([encoding['attention_mask'] for encoding in encodings], axis=0)
        elif return_tensors == 'jax' and is_flax_available():
            import jax.numpy as jnp
            input_ids = jnp.concatenate([encoding['input_ids'] for encoding in encodings], axis=0)
            attention_mask = jnp.concatenate([encoding['attention_mask'] for encoding in encodings], axis=0)
        elif return_tensors == 'pt' and is_torch_available():
            import torch
            input_ids = torch.cat([encoding['input_ids'] for encoding in encodings], dim=0)
            attention_mask = torch.cat([encoding['attention_mask'] for encoding in encodings], dim=0)
        elif return_tensors == 'tf' and is_tf_available():
            import tensorflow as tf
            input_ids = tf.stack([encoding['input_ids'] for encoding in encodings], axis=0)
            attention_mask = tf.stack([encoding['attention_mask'] for encoding in encodings], axis=0)
        else:
            raise ValueError('Target return tensor type could not be returned')
        data['input_ids'] = input_ids
        data['attention_mask'] = attention_mask
    if query_images is not None:
        query_pixel_values = self.image_processor(query_images, **output_kwargs['images_kwargs']).pixel_values
        data = {'query_pixel_values': query_pixel_values}
    if images is not None:
        image_features = self.image_processor(images, **output_kwargs['images_kwargs'])
        data['pixel_values'] = image_features.pixel_values
    return BatchFeature(data=data, tensor_type=return_tensors)

Main method to prepare for the model one or several text(s) and image(s). This method forwards the `text` and
`kwargs` arguments to CLIPTokenizerFast's [`~CLIPTokenizerFast.__call__`] if `text` is not `None` to encode:
the text. To prepare the image(s), this method forwards the `images` and `kwrags` arguments to
CLIPImageProcessor's [`~CLIPImageProcessor.__call__`] if `images` is not `None`. Please refer to the docstring
of the above two methods for more information.

Args:
    images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`,
    `List[torch.Tensor]`):
        The image or batch of images to be prepared. Each image can be a PIL image, NumPy array or PyTorch
        tensor. Both channels-first and channels-last formats are supported.
    text (`str`, `List[str]`, `List[List[str]]`):
        The sequence or batch of sequences to be encoded. Each sequence can be a string or a list of strings
        (pretokenized string). If the sequences are provided as list of strings (pretokenized), you must set
        `is_split_into_words=True` (to lift the ambiguity with a batch of sequences).
    query_images (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`, `List[PIL.Image.Image]`, `List[np.ndarray]`, `List[torch.Tensor]`):
        The query image to be prepared, one query image is expected per target image to be queried. Each image
        can be a PIL image, NumPy array or PyTorch tensor. In case of a NumPy array/PyTorch tensor, each image
        should be of shape (C, H, W), where C is a number of channels, H and W are image height and width.
    return_tensors (`str` or [`~utils.TensorType`], *optional*):
        If set, will return tensors of a particular framework. Acceptable values are:
        - `'tf'`: Return TensorFlow `tf.constant` objects.
        - `'pt'`: Return PyTorch `torch.Tensor` objects.
        - `'np'`: Return NumPy `np.ndarray` objects.
        - `'jax'`: Return JAX `jnp.ndarray` objects.

Returns:
    [`BatchFeature`]: A [`BatchFeature`] with the following fields:
    - **input_ids** -- List of token ids to be fed to a model. Returned when `text` is not `None`.
    - **attention_mask** -- List of indices specifying which tokens should be attended to by the model (when
      `return_attention_mask=True` or if *"attention_mask"* is in `self.model_input_names` and if `text` is not
      `None`).
    - **pixel_values** -- Pixel values to be fed to a model. Returned when `images` is not `None`.
    - **query_pixel_values** -- Pixel values of the query images to be fed to a model. Returned when `query_images` is not `None`.


class Flatten(Layer):

    def __init__(self, data_format=None, **kwargs):
        super().__init__(**kwargs)
        self.data_format = backend.standardize_data_format(data_format)
        self.input_spec = InputSpec(min_ndim=1)
        self._channels_first = self.data_format == 'channels_first'

    def call(self, inputs):
        input_shape = inputs.shape
        rank = len(input_shape)
        if self._channels_first and rank > 1:
            inputs = ops.transpose(inputs, axes=(0, *range(2, rank), 1))
        output_shape = tuple((dim if dim is not None else -1 for dim in self.compute_output_shape(input_shape)))
        return ops.reshape(inputs, output_shape)

    def compute_output_shape(self, input_shape):
        non_batch_dims = input_shape[1:]
        if len(non_batch_dims) == 0:
            flattened_dim = 1
        elif any((d is None for d in non_batch_dims)):
            flattened_dim = None
        else:
            flattened_dim = math.prod(non_batch_dims)
        return (input_shape[0], flattened_dim)

    def compute_output_spec(self, inputs):
        output_shape = self.compute_output_shape(inputs.shape)
        return KerasTensor(shape=output_shape, dtype=inputs.dtype, sparse=inputs.sparse)

    def get_config(self):
        config = {'data_format': self.data_format}
        base_config = super().get_config()
        return {**base_config, **config}

Flattens the input. Does not affect the batch size.

Note: If inputs are shaped `(batch,)` without a feature axis, then
flattening adds an extra channel dimension and output shape is `(batch, 1)`.

Args:
    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, ..., channels)` while `"channels_first"` corresponds to
        inputs with shape `(batch, channels, ...)`.
        When unspecified, uses `image_data_format` value found in your Keras
        config file at `~/.keras/keras.json` (if exists). Defaults to
        `"channels_last"`.

Example:

>>> x = keras.Input(shape=(10, 64))
>>> y = keras.layers.Flatten()(x)
>>> y.shape
(None, 640)


def forward(self, pixel_values: torch.FloatTensor, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> BackboneOutput:
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    outputs = self.encoder(pixel_values=pixel_values, output_attentions=output_attentions, output_hidden_states=True, return_dict=return_dict)
    hidden_states = outputs.hidden_states
    feature_maps = ()
    for idx, stage in enumerate(self.stage_names):
        if stage in self.out_features:
            feature_maps += (hidden_states[idx],)
    if not return_dict:
        output = (feature_maps,)
        if output_hidden_states:
            output += (outputs.hidden_states,)
        return output
    return BackboneOutput(feature_maps=feature_maps, hidden_states=outputs.hidden_states if output_hidden_states else None, attentions=None)

Examples:

```python
>>> from transformers import AutoImageProcessor, AutoBackbone
>>> import torch
>>> from PIL import Image
>>> import requests

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

>>> processor = AutoImageProcessor.from_pretrained("OpenGVLab/pvt_v2_b0")
>>> model = AutoBackbone.from_pretrained(
...     "OpenGVLab/pvt_v2_b0", out_features=["stage1", "stage2", "stage3", "stage4"]
... )

>>> inputs = processor(image, return_tensors="pt")

>>> outputs = model(**inputs)
>>> feature_maps = outputs.feature_maps
>>> list(feature_maps[-1].shape)
[1, 256, 7, 7]
```


def call(self, pixel_values: TFModelInputType | None=None, noise: Optional[tf.Tensor]=None, head_mask: np.ndarray | tf.Tensor | None=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, training: bool=False, interpolate_pos_encoding: bool=False) -> Union[TFViTMAEForPreTrainingOutput, Tuple[tf.Tensor]]:
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    outputs = self.vit(pixel_values=pixel_values, noise=noise, head_mask=head_mask, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, training=training, interpolate_pos_encoding=interpolate_pos_encoding)
    latent = outputs.last_hidden_state
    ids_restore = outputs.ids_restore
    mask = outputs.mask
    decoder_outputs = self.decoder(latent, ids_restore, interpolate_pos_encoding=interpolate_pos_encoding)
    logits = decoder_outputs.logits
    loss = self.forward_loss(pixel_values, logits, mask, interpolate_pos_encoding=interpolate_pos_encoding)
    if not return_dict:
        output = (logits, mask, ids_restore) + outputs[2:]
        return (loss,) + output if loss is not None else output
    return TFViTMAEForPreTrainingOutput(loss=loss, logits=logits, mask=mask, ids_restore=ids_restore, hidden_states=outputs.hidden_states, attentions=outputs.attentions)

Returns:

Examples:

```python
>>> from transformers import AutoImageProcessor, TFViTMAEForPreTraining
>>> from PIL import Image
>>> import requests

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

>>> image_processor = AutoImageProcessor.from_pretrained("facebook/vit-mae-base")
>>> model = TFViTMAEForPreTraining.from_pretrained("facebook/vit-mae-base")

>>> inputs = image_processor(images=image, return_tensors="pt")
>>> outputs = model(**inputs)
>>> loss = outputs.loss
>>> mask = outputs.mask
>>> ids_restore = outputs.ids_restore
```


def read_value_no_copy(self):
    with ops.name_scope('Read'):
        value = self._read_variable_op(no_copy=True)
    return array_ops.identity(value)

Constructs an op which reads the value of this variable without copy.

The variable is read without making a copy even when it has been sparsely
accessed. Variables in copy-on-read mode will be converted to copy-on-write
mode.

Returns:
  The value of the variable.


def log_abs_determinant(self, name='log_abs_det'):
    if self.is_square is False:
        raise NotImplementedError('Determinant not implemented for an operator that is expected to not be square.')
    with self._name_scope(name):
        return self._log_abs_determinant()

Log absolute value of determinant for every batch member.

Args:
  name:  A name for this `Op`.

Returns:
  `Tensor` with shape `self.batch_shape` and same `dtype` as `self`.

Raises:
  NotImplementedError:  If `self.is_square` is `False`.


class OwlViTVisionConfig(PretrainedConfig):
    model_type = 'owlvit_vision_model'
    base_config_key = 'vision_config'

    def __init__(self, hidden_size=768, intermediate_size=3072, num_hidden_layers=12, num_attention_heads=12, num_channels=3, image_size=768, patch_size=32, hidden_act='quick_gelu', layer_norm_eps=1e-05, attention_dropout=0.0, initializer_range=0.02, initializer_factor=1.0, **kwargs):
        super().__init__(**kwargs)
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.num_channels = num_channels
        self.image_size = image_size
        self.patch_size = patch_size
        self.hidden_act = hidden_act
        self.layer_norm_eps = layer_norm_eps
        self.attention_dropout = attention_dropout
        self.initializer_range = initializer_range
        self.initializer_factor = initializer_factor

This is the configuration class to store the configuration of an [`OwlViTVisionModel`]. It is used to instantiate
an OWL-ViT image encoder according to the specified arguments, defining the model architecture. Instantiating a
configuration with the defaults will yield a similar configuration to that of the OWL-ViT
[google/owlvit-base-patch32](https://huggingface.co/google/owlvit-base-patch32) architecture.

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

Args:
    hidden_size (`int`, *optional*, defaults to 768):
        Dimensionality of the encoder layers and the pooler layer.
    intermediate_size (`int`, *optional*, defaults to 3072):
        Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
    num_hidden_layers (`int`, *optional*, defaults to 12):
        Number of hidden layers in the Transformer encoder.
    num_attention_heads (`int`, *optional*, defaults to 12):
        Number of attention heads for each attention layer in the Transformer encoder.
    num_channels (`int`, *optional*, defaults to 3):
        Number of channels in the input images.
    image_size (`int`, *optional*, defaults to 768):
        The size (resolution) of each image.
    patch_size (`int`, *optional*, defaults to 32):
        The size (resolution) of each patch.
    hidden_act (`str` or `function`, *optional*, defaults to `"quick_gelu"`):
        The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
        `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
    layer_norm_eps (`float`, *optional*, defaults to 1e-05):
        The epsilon used by the layer normalization layers.
    attention_dropout (`float`, *optional*, defaults to 0.0):
        The dropout ratio for the attention probabilities.
    initializer_range (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    initializer_factor (`float`, *optional*, defaults to 1.0):
        A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
        testing).

Example:

```python
>>> from transformers import OwlViTVisionConfig, OwlViTVisionModel

>>> # Initializing a OwlViTVisionModel with google/owlvit-base-patch32 style configuration
>>> configuration = OwlViTVisionConfig()

>>> # Initializing a OwlViTVisionModel model from the google/owlvit-base-patch32 style configuration
>>> model = OwlViTVisionModel(configuration)

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


def create_token_type_ids_from_sequences(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None) -> List[int]:
    sep = [self.sep_token_id]
    cls = [self.cls_token_id]
    if token_ids_1 is None:
        return len(cls + token_ids_0 + sep) * [0]
    return len(cls + token_ids_0 + sep + sep + token_ids_1 + sep) * [0]

Create a mask from the two sequences passed to be used in a sequence-pair classification task. MVP does not
make use of token type ids, therefore a list of zeros is returned.

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

Returns:
    `List[int]`: List of zeros.


def _internal_operation_seed(self):
    return self._rng.randint(0, _MAXINT32)

Returns a fake operation seed.

  In eager mode, user shouldn't set or depend on operation seed.
  Here, we generate a random seed based on global seed to make
  operation's randomness different and depend on the global seed.

Returns:
  A fake operation seed based on global seed.


def identity(n, dtype=None):
    return backend.numpy.identity(n, dtype=dtype)

Return the identity tensor.

The identity tensor is a square tensor with ones on the main diagonal and
zeros elsewhere.

Args:
    n: Number of rows (and columns) in the `n x n` output tensor.
    dtype: Data type of the output tensor.

Returns:
    The identity tensor.


def is_broadcast_compatible(shape_x, shape_y):
    if shape_x.ndims is None or shape_y.ndims is None:
        return False
    return _broadcast_shape_helper(shape_x, shape_y) is not None

Returns True if `shape_x` and `shape_y` are broadcast compatible.

Args:
  shape_x: A `TensorShape`
  shape_y: A `TensorShape`

Returns:
  True if a shape exists that both `shape_x` and `shape_y` can be broadcasted
  to.  False otherwise.


class DepthwiseConv1D(BaseDepthwiseConv):

    def __init__(self, kernel_size, strides=1, padding='valid', depth_multiplier=1, data_format=None, dilation_rate=1, activation=None, use_bias=True, depthwise_initializer='glorot_uniform', bias_initializer='zeros', depthwise_regularizer=None, bias_regularizer=None, activity_regularizer=None, depthwise_constraint=None, bias_constraint=None, **kwargs):
        super().__init__(rank=1, depth_multiplier=depth_multiplier, kernel_size=kernel_size, strides=strides, padding=padding, data_format=data_format, dilation_rate=dilation_rate, activation=activation, use_bias=use_bias, depthwise_initializer=depthwise_initializer, bias_initializer=bias_initializer, depthwise_regularizer=depthwise_regularizer, bias_regularizer=bias_regularizer, activity_regularizer=activity_regularizer, depthwise_constraint=depthwise_constraint, bias_constraint=bias_constraint, **kwargs)

1D depthwise convolution layer.

Depthwise convolution is a type of convolution in which each input channel
is convolved with a different kernel (called a depthwise kernel). You can
understand depthwise convolution as the first step in a depthwise separable
convolution.

It is implemented via the following steps:

- Split the input into individual channels.
- Convolve each channel with an individual depthwise kernel with
  `depth_multiplier` output channels.
- Concatenate the convolved outputs along the channels axis.

Unlike a regular 1D convolution, depthwise convolution does not mix
information across different input channels.

The `depth_multiplier` argument determines how many filters are applied to
one input channel. As such, it controls the amount of output channels that
are generated per input channel in the depthwise step.

Args:
    kernel_size: int or tuple/list of 1 integer, specifying the size of the
        depthwise convolution window.
    strides: int or tuple/list of 1 integer, specifying the stride length
        of the convolution. `strides > 1` is incompatible with
        `dilation_rate > 1`.
    padding: string, either `"valid"` or `"same"` (case-insensitive).
        `"valid"` means no padding. `"same"` results in padding evenly to
        the left/right or up/down of the input. When `padding="same"` and
        `strides=1`, the output has the same size as the input.
    depth_multiplier: The number of depthwise convolution output channels
        for each input channel. The total number of depthwise convolution
        output channels will be equal to `input_channel * depth_multiplier`.
    data_format: string, either `"channels_last"` or `"channels_first"`.
        The ordering of the dimensions in the inputs. `"channels_last"`
        corresponds to inputs with shape `(batch, steps, features)`
        while `"channels_first"` corresponds to inputs with shape
        `(batch, features, steps)`. It defaults to the `image_data_format`
        value found in your Keras config file at `~/.keras/keras.json`.
        If you never set it, then it will be `"channels_last"`.
    dilation_rate: int or tuple/list of 1 integers, specifying the dilation
        rate to use for dilated convolution.
    activation: Activation function. If `None`, no activation is applied.
    use_bias: bool, if `True`, bias will be added to the output.
    depthwise_initializer: Initializer for the convolution kernel.
        If `None`, the default initializer (`"glorot_uniform"`)
        will be used.
    bias_initializer: Initializer for the bias vector. If `None`, the
        default initializer (`"zeros"`) will be used.
    depthwise_regularizer: Optional regularizer for the convolution kernel.
    bias_regularizer: Optional regularizer for the bias vector.
    activity_regularizer: Optional regularizer function for the output.
    depthwise_constraint: Optional projection function to be applied to the
        kernel after being updated by an `Optimizer` (e.g. used to implement
        norm constraints or value constraints for layer weights). The
        function must take as input the unprojected variable and must return
        the projected variable (which must have the same shape). Constraints
        are not safe to use when doing asynchronous distributed training.
    bias_constraint: Optional projection function to be applied to the
        bias after being updated by an `Optimizer`.

Input shape:

- If `data_format="channels_last"`:
    A 3D tensor with shape: `(batch_shape, steps, channels)`
- If `data_format="channels_first"`:
    A 3D tensor with shape: `(batch_shape, channels, steps)`

Output shape:

- If `data_format="channels_last"`:
    A 3D tensor with shape:
    `(batch_shape, new_steps, channels * depth_multiplier)`
- If `data_format="channels_first"`:
    A 3D tensor with shape:
    `(batch_shape, channels * depth_multiplier, new_steps)`

Returns:
    A 3D tensor representing
    `activation(depthwise_conv1d(inputs, kernel) + bias)`.

Raises:
    ValueError: when both `strides > 1` and `dilation_rate > 1`.

Example:

>>> x = np.random.rand(4, 10, 12)
>>> y = keras.layers.DepthwiseConv1D(3, 3, 2, activation='relu')(x)
>>> print(y.shape)
(4, 4, 36)


def preprocess(self, images: ImageInput, annotations: Optional[Union[AnnotationType, List[AnnotationType]]]=None, return_segmentation_masks: Optional[bool]=None, masks_path: Optional[Union[str, pathlib.Path]]=None, do_resize: Optional[bool]=None, size: Optional[Dict[str, int]]=None, resample=None, do_rescale: Optional[bool]=None, rescale_factor: Optional[Union[int, float]]=None, do_normalize: Optional[bool]=None, do_convert_annotations: Optional[bool]=None, image_mean: Optional[Union[float, List[float]]]=None, image_std: Optional[Union[float, List[float]]]=None, do_pad: Optional[bool]=None, format: Optional[Union[str, AnnotationFormat]]=None, return_tensors: Optional[Union[TensorType, str]]=None, data_format: Union[str, ChannelDimension]=ChannelDimension.FIRST, input_data_format: Optional[Union[str, ChannelDimension]]=None, pad_size: Optional[Dict[str, int]]=None, **kwargs) -> BatchFeature:
    if 'pad_and_return_pixel_mask' in kwargs:
        logger.warning_once('The `pad_and_return_pixel_mask` argument is deprecated and will be removed in a future version, use `do_pad` instead.')
        do_pad = kwargs.pop('pad_and_return_pixel_mask')
    if 'max_size' in kwargs:
        logger.warning_once("The `max_size` argument is deprecated and will be removed in a future version, use `size['longest_edge']` instead.")
        size = kwargs.pop('max_size')
    do_resize = self.do_resize if do_resize is None else do_resize
    size = self.size if size is None else size
    size = get_size_dict(size=size, default_to_square=False)
    resample = self.resample if resample is None else resample
    do_rescale = self.do_rescale if do_rescale is None else do_rescale
    rescale_factor = self.rescale_factor if rescale_factor is None else rescale_factor
    do_normalize = self.do_normalize if do_normalize is None else do_normalize
    image_mean = self.image_mean if image_mean is None else image_mean
    image_std = self.image_std if image_std is None else image_std
    do_convert_annotations = self.do_convert_annotations if do_convert_annotations is None else do_convert_annotations
    do_pad = self.do_pad if do_pad is None else do_pad
    pad_size = self.pad_size if pad_size is None else pad_size
    format = self.format if format is None else format
    images = make_list_of_images(images)
    if not valid_images(images):
        raise ValueError('Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.')
    validate_kwargs(captured_kwargs=kwargs.keys(), valid_processor_keys=self._valid_processor_keys)
    validate_preprocess_arguments(do_rescale=do_rescale, rescale_factor=rescale_factor, do_normalize=do_normalize, image_mean=image_mean, image_std=image_std, do_resize=do_resize, size=size, resample=resample)
    if annotations is not None and isinstance(annotations, dict):
        annotations = [annotations]
    if annotations is not None and len(images) != len(annotations):
        raise ValueError(f'The number of images ({len(images)}) and annotations ({len(annotations)}) do not match.')
    format = AnnotationFormat(format)
    if annotations is not None:
        validate_annotations(format, SUPPORTED_ANNOTATION_FORMATS, annotations)
    if masks_path is not None and format == AnnotationFormat.COCO_PANOPTIC and (not isinstance(masks_path, (pathlib.Path, str))):
        raise ValueError(f'The path to the directory containing the mask PNG files should be provided as a `pathlib.Path` or string object, but is {type(masks_path)} instead.')
    images = [to_numpy_array(image) for image in images]
    if do_rescale and is_scaled_image(images[0]):
        logger.warning_once('It looks like you are trying to rescale already rescaled images. If the input images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again.')
    if input_data_format is None:
        input_data_format = infer_channel_dimension_format(images[0])
    if annotations is not None:
        prepared_images = []
        prepared_annotations = []
        for image, target in zip(images, annotations):
            target = self.prepare_annotation(image, target, format, return_segmentation_masks=return_segmentation_masks, masks_path=masks_path, input_data_format=input_data_format)
            prepared_images.append(image)
            prepared_annotations.append(target)
        images = prepared_images
        annotations = prepared_annotations
        del prepared_images, prepared_annotations
    if do_resize:
        if annotations is not None:
            resized_images, resized_annotations = ([], [])
            for image, target in zip(images, annotations):
                orig_size = get_image_size(image, input_data_format)
                resized_image = self.resize(image, size=size, resample=resample, input_data_format=input_data_format)
                resized_annotation = self.resize_annotation(target, orig_size, get_image_size(resized_image, input_data_format))
                resized_images.append(resized_image)
                resized_annotations.append(resized_annotation)
            images = resized_images
            annotations = resized_annotations
            del resized_images, resized_annotations
        else:
            images = [self.resize(image, size=size, resample=resample, input_data_format=input_data_format) for image in images]
    if do_rescale:
        images = [self.rescale(image, rescale_factor, input_data_format=input_data_format) for image in images]
    if do_normalize:
        images = [self.normalize(image, image_mean, image_std, input_data_format=input_data_format) for image in images]
    if do_convert_annotations and annotations is not None:
        annotations = [self.normalize_annotation(annotation, get_image_size(image, input_data_format)) for annotation, image in zip(annotations, images)]
    if do_pad:
        encoded_inputs = self.pad(images, annotations=annotations, return_pixel_mask=True, data_format=data_format, input_data_format=input_data_format, update_bboxes=do_convert_annotations, return_tensors=return_tensors, pad_size=pad_size)
    else:
        images = [to_channel_dimension_format(image, data_format, input_channel_dim=input_data_format) for image in images]
        encoded_inputs = BatchFeature(data={'pixel_values': images}, tensor_type=return_tensors)
        if annotations is not None:
            encoded_inputs['labels'] = [BatchFeature(annotation, tensor_type=return_tensors) for annotation in annotations]
    return encoded_inputs

Preprocess an image or a batch of images so that it can be used by the model.

Args:
    images (`ImageInput`):
        Image or batch of images to preprocess. Expects a single or batch of images with pixel values ranging
        from 0 to 255. If passing in images with pixel values between 0 and 1, set `do_rescale=False`.
    annotations (`AnnotationType` or `List[AnnotationType]`, *optional*):
        List of annotations associated with the image or batch of images. If annotation is for object
        detection, the annotations should be a dictionary with the following keys:
        - "image_id" (`int`): The image id.
        - "annotations" (`List[Dict]`): List of annotations for an image. Each annotation should be a
          dictionary. An image can have no annotations, in which case the list should be empty.
        If annotation is for segmentation, the annotations should be a dictionary with the following keys:
        - "image_id" (`int`): The image id.
        - "segments_info" (`List[Dict]`): List of segments for an image. Each segment should be a dictionary.
          An image can have no segments, in which case the list should be empty.
        - "file_name" (`str`): The file name of the image.
    return_segmentation_masks (`bool`, *optional*, defaults to self.return_segmentation_masks):
        Whether to return segmentation masks.
    masks_path (`str` or `pathlib.Path`, *optional*):
        Path to the directory containing the segmentation masks.
    do_resize (`bool`, *optional*, defaults to self.do_resize):
        Whether to resize the image.
    size (`Dict[str, int]`, *optional*, defaults to self.size):
        Size of the image's `(height, width)` dimensions after resizing. Available options are:
            - `{"height": int, "width": int}`: The image will be resized to the exact size `(height, width)`.
                Do NOT keep the aspect ratio.
            - `{"shortest_edge": int, "longest_edge": int}`: The image will be resized to a maximum size respecting
                the aspect ratio and keeping the shortest edge less or equal to `shortest_edge` and the longest edge
                less or equal to `longest_edge`.
            - `{"max_height": int, "max_width": int}`: The image will be resized to the maximum size respecting the
                aspect ratio and keeping the height less or equal to `max_height` and the width less or equal to
                `max_width`.
    resample (`PILImageResampling`, *optional*, defaults to self.resample):
        Resampling filter to use when resizing the image.
    do_rescale (`bool`, *optional*, defaults to self.do_rescale):
        Whether to rescale the image.
    rescale_factor (`float`, *optional*, defaults to self.rescale_factor):
        Rescale factor to use when rescaling the image.
    do_normalize (`bool`, *optional*, defaults to self.do_normalize):
        Whether to normalize the image.
    do_convert_annotations (`bool`, *optional*, defaults to self.do_convert_annotations):
        Whether to convert the annotations to the format expected by the model. Converts the bounding
        boxes from the format `(top_left_x, top_left_y, width, height)` to `(center_x, center_y, width, height)`
        and in relative coordinates.
    image_mean (`float` or `List[float]`, *optional*, defaults to self.image_mean):
        Mean to use when normalizing the image.
    image_std (`float` or `List[float]`, *optional*, defaults to self.image_std):
        Standard deviation to use when normalizing the image.
    do_pad (`bool`, *optional*, defaults to self.do_pad):
        Whether to pad the image. If `True`, padding will be applied to the bottom and right of
        the image with zeros. If `pad_size` is provided, the image will be padded to the specified
        dimensions. Otherwise, the image will be padded to the maximum height and width of the batch.
    format (`str` or `AnnotationFormat`, *optional*, defaults to self.format):
        Format of the annotations.
    return_tensors (`str` or `TensorType`, *optional*, defaults to self.return_tensors):
        Type of tensors to return. If `None`, will return the list of images.
    data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
        The channel dimension format for the output image. Can be one of:
        - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
        - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        - Unset: Use the channel dimension format of the input image.
    input_data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format for the input image. If unset, the channel dimension format is inferred
        from the input image. Can be one of:
        - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
        - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
    pad_size (`Dict[str, int]`, *optional*):
        The size `{"height": int, "width" int}` to pad the images to. Must be larger than any image size
        provided for preprocessing. If `pad_size` is not provided, images will be padded to the largest
        height and width in the batch.


def visit_inner_types(type_constraint, visitor, visitor_arg):
    if isinstance(type_constraint, TypeConstraint):
        return type_constraint.visit(visitor, visitor_arg)
    return visitor(type_constraint, visitor_arg)

Visitor pattern to visit all inner types of a type constraint.

Args:
  type_constraint: A type constraint or a type.
  visitor: A callable invoked for all nodes in the type tree comprising a
    composite type. The visitor will be called with the node visited and the
    visitor argument specified here.
  visitor_arg: Visitor callback second argument.

Note:
  Raise and capture a StopIteration to terminate the visit, e.g.

  ```
  def visitor(type_constraint, visitor_arg):
    if ...:
      raise StopIteration

  try:
    visit_inner_types(type_constraint, visitor, visitor_arg)
  except StopIteration:
    pass
  ```


def array_to_img(x, data_format=None, scale=True, dtype=None):
    data_format = backend.standardize_data_format(data_format)
    if dtype is None:
        dtype = backend.floatx()
    if pil_image is None:
        raise ImportError('Could not import PIL.Image. The use of `array_to_img` requires PIL.')
    x = np.asarray(x, dtype=dtype)
    if x.ndim != 3:
        raise ValueError(f'Expected image array to have rank 3 (single image). Got array with shape: {x.shape}')
    if data_format == 'channels_first':
        x = x.transpose(1, 2, 0)
    if scale:
        x = x - np.min(x)
        x_max = np.max(x)
        if x_max != 0:
            x /= x_max
        x *= 255
    if x.shape[2] == 4:
        return pil_image.fromarray(x.astype('uint8'), 'RGBA')
    elif x.shape[2] == 3:
        return pil_image.fromarray(x.astype('uint8'), 'RGB')
    elif x.shape[2] == 1:
        if np.max(x) > 255:
            return pil_image.fromarray(x[:, :, 0].astype('int32'), 'I')
        return pil_image.fromarray(x[:, :, 0].astype('uint8'), 'L')
    else:
        raise ValueError(f'Unsupported channel number: {x.shape[2]}')

Converts a 3D NumPy array to a PIL Image instance.

Example:

```python
from PIL import Image
img = np.random.random(size=(100, 100, 3))
pil_img = keras.utils.array_to_img(img)
```

Args:
    x: Input data, in any form that can be converted to a NumPy array.
    data_format: Image data format, can be either `"channels_first"` or
        `"channels_last"`. Defaults to `None`, in which case the global
        setting `keras.backend.image_data_format()` is used (unless you
        changed it, it defaults to `"channels_last"`).
    scale: Whether to rescale the image such that minimum and maximum values
        are 0 and 255 respectively. Defaults to `True`.
    dtype: Dtype to use. `None` means the global setting
        `keras.backend.floatx()` is used (unless you changed it, it
        defaults to `"float32"`). Defaults to `None`.

Returns:
    A PIL Image instance.


def resolve(phrases: typing.List[str], html: str, separator: str='\u200b') -> str:
    resolver = HTMLChunkResolver(phrases, separator)
    resolver.feed(html)
    result = '%s' % (PARENT_CSS_STYLE, resolver.output)
    return result

Wraps phrases in the HTML string with non-breaking markup.

Args:
  phrases (List[str]): The phrases included in the HTML string.
  html (str): The HTML string to resolve.
  separator (str, optional): The separator string.

Returns:
  The HTML string with phrases wrapped in non-breaking markup.


def is_ref(x):
    return isinstance(x, variables_module.Variable) or (isinstance(x, module.Module) and hasattr(x, 'dtype') and hasattr(x, 'shape'))

Evaluates if the object has reference semantics.

An object is deemed "reference" if it is a `tf.Variable` instance or is
derived from a `tf.Module` with `dtype` and `shape` properties.

Args:
  x: Any object.

Returns:
  is_ref: Python `bool` indicating input is has nonreference semantics, i.e.,
    is a `tf.Variable` or a `tf.Module` with `dtype` and `shape` properties.


def report_file(config, auth, account, report_id=None, name=None, timeout=60, chunksize=DCM_CHUNK_SIZE):
    account_id, advertiser_id = parse_account(config, auth, account)
    file_json = report_fetch(config, auth, account, report_id, name, timeout)
    if file_json == False:
        return (None, None)
    elif file_json == True:
        return ('report_running.csv', None)
    else:
        filename = '%s_%s.csv' % (file_json['fileName'], file_json['dateRange']['endDate'].replace('-', ''))
        if chunksize:
            return (filename, media_download(API_DCM(config, auth).files().get_media(reportId=file_json['reportId'], fileId=file_json['id']).execute(False), chunksize, 'utf-8'))
        else:
            return (filename, StringIO(API_DCM(config, auth).files().get_media(reportId=file_json['reportId'], fileId=file_json['id']).execute().decode('utf-8')))

Retrieves most recent DCM file by name or ID, if in progress, waits for it to complete.

Bulletproofing:
https://developers.google.com/doubleclick-advertisers/v3.2/files/get

Timeout is in minutes ( retries will happen at 1 minute interval, default
total time is 60 minutes )
If chunksize is set to 0 then the whole file is downloaded at once.

Args:
  * auth: (string) Either user or service.
  * account: (string) [account:advertiser@profile] token.
  * report_id: (int) ID of DCm report to fetch ( either or name ).
  * name: (string) Name of report to fetch ( either or report_id ).
  * timeout: (int) Minutes to wait for in progress report before giving up.
  * chunksize: (int) number of bytes to download at a time, for memory
    constrained systems.

Returns:
  * (filename, iterator) if file exists and is ready to download in chunks.
  * (filename, file) if file exists and chunking is off.
  * ('report_running.csv', None) if report is in progress.
  * (None, None) if file does not exist.


class PreprocessingLayer(Layer, metaclass=abc.ABCMeta):
    _must_restore_from_config = True

    def __init__(self, streaming=True, **kwargs):
        super(PreprocessingLayer, self).__init__(**kwargs)
        self._streaming = streaming
        self._is_compiled = False
        self._is_adapted = False
        self._reset_state_impl = self.reset_state
        self.reset_state = self._reset_state_wrapper
        self._adapt_function = None

    @property
    def streaming(self):
        """Whether `adapt` can be called twice without resetting the state."""
        return self._streaming

    @property
    def is_adapted(self):
        """Whether the layer has been fit to data already."""
        return self._is_adapted

    def update_state(self, data):
        """Accumulates statistics for the preprocessing layer.

    Arguments:
      data: A mini-batch of inputs to the layer.
    """
        raise NotImplementedError

    def reset_state(self):
        """Resets the statistics of the preprocessing layer."""
        raise NotImplementedError

    def merge_state(self, layers):
        """Merge the statistics of multiple preprocessing layers.

    This layer will contain the merged state.

    Arguments:
      layers: Layers whose statistics should be merge with the statistics of
        this layer.
    """
        raise NotImplementedError

    def finalize_state(self):
        """Finalize the statistics for the preprocessing layer.

    This method is called at the end of `adapt` or after restoring a serialized
    preprocessing layer's state. This method handles any one-time operations
    that should occur on the layer's state before `Layer.__call__`.
    """
        pass

    def make_adapt_function(self):
        """Creates a function to execute one step of `adapt`.

    This method can be overridden to support custom adapt logic.
    This method is called by `PreprocessingLayer.adapt`.

    Typically, this method directly controls `tf.function` settings,
    and delegates the actual state update logic to
    `PreprocessingLayer.update_state`.

    This function is cached the first time `PreprocessingLayer.adapt`
    is called. The cache is cleared whenever `PreprocessingLayer.compile`
    is called.

    Returns:
      Function. The function created by this method should accept a
      `tf.data.Iterator`, retrieve a batch, and update the state of the
      layer.
    """
        if self._adapt_function is not None:
            return self._adapt_function

        def adapt_step(iterator):
            data = next(iterator)
            self._adapt_maybe_build(data)
            self.update_state(data)
        if self._steps_per_execution.numpy().item() == 1:
            adapt_fn = adapt_step
        else:

            def adapt_fn(iterator):
                for _ in math_ops.range(self._steps_per_execution):
                    adapt_step(iterator)
        if not self._run_eagerly:
            adapt_fn = def_function.function(adapt_fn)
        self._adapt_function = adapt_fn
        return self._adapt_function

    def compile(self, run_eagerly=None, steps_per_execution=None):
        """Configures the layer for `adapt`.

    Arguments:
      run_eagerly: Bool. Defaults to `False`. If `True`, this `Model`'s logic
        will not be wrapped in a `tf.function`. Recommended to leave this as
        `None` unless your `Model` cannot be run inside a `tf.function`.
        steps_per_execution: Int. Defaults to 1. The number of batches to run
          during each `tf.function` call. Running multiple batches inside a
          single `tf.function` call can greatly improve performance on TPUs or
          small models with a large Python overhead.
    """
        if steps_per_execution is None:
            steps_per_execution = 1
        self._configure_steps_per_execution(steps_per_execution)
        if run_eagerly is None:
            run_eagerly = self.dynamic
        self._run_eagerly = run_eagerly
        self._is_compiled = True

    def adapt(self, data, batch_size=None, steps=None, reset_state=True):
        """Fits the state of the preprocessing layer to the data being passed.

    After calling `adapt` on a layer, a preprocessing layer's state will not
    update during training. In order to make preprocessing layers efficient in
    any distribution context, they are kept constant with respect to any
    compiled `tf.Graph`s that call the layer. This does not affect the layer use
    when adapting each layer only once, but if you adapt a layer multiple times
    you will need to take care to re-compile any compiled functions as follows:

     * If you are adding a preprocessing layer to a `keras.Model`, you need to
       call `model.compile` after each subsequent call to `adapt`.
     * If you are calling a preprocessing layer inside `tf.data.Dataset.map`,
       you should call `map` again on the input `tf.data.Dataset` after each
       `adapt`.
     * If you are using a `tf.function` directly which calls a preprocessing
       layer, you need to call `tf.function` again on your callable after
       each subsequent call to `adapt`.

    `tf.keras.Model` example with multiple adapts:

    >>> layer = tf.keras.layers.experimental.preprocessing.Normalization(
    ...     axis=None)
    >>> layer.adapt([0, 2])
    >>> model = tf.keras.Sequential(layer)
    >>> model.predict([0, 1, 2])
    array([-1.,  0.,  1.], dtype=float32)
    >>> layer.adapt([-1, 1])
    >>> model.compile() # This is needed to re-compile model.predict!
    >>> model.predict([0, 1, 2])
    array([0., 1., 2.], dtype=float32)

    `tf.data.Dataset` example with multiple adapts:

    >>> layer = tf.keras.layers.experimental.preprocessing.Normalization(
    ...     axis=None)
    >>> layer.adapt([0, 2])
    >>> input_ds = tf.data.Dataset.range(3)
    >>> normalized_ds = input_ds.map(layer)
    >>> list(normalized_ds.as_numpy_iterator())
    [array([-1.], dtype=float32),
     array([0.], dtype=float32),
     array([1.], dtype=float32)]
    >>> layer.adapt([-1, 1])
    >>> normalized_ds = input_ds.map(layer) # Re-map over the input dataset.
    >>> list(normalized_ds.as_numpy_iterator())
    [array([0.], dtype=float32),
     array([1.], dtype=float32),
     array([2.], dtype=float32)]

    Arguments:
        data: The data to train on. It can be passed either as a tf.data
          Dataset, or as a numpy array.
        batch_size: Integer or `None`.
            Number of samples per state update.
            If unspecified, `batch_size` will default to 32.
            Do not specify the `batch_size` if your data is in the
            form of datasets, generators, or `keras.utils.Sequence` instances
            (since they generate batches).
        steps: Integer or `None`.
            Total number of steps (batches of samples)
            When training with input tensors such as
            TensorFlow data tensors, the default `None` is equal to
            the number of samples in your dataset divided by
            the batch size, or 1 if that cannot be determined. If x is a
            `tf.data` dataset, and 'steps' is None, the epoch will run until
            the input dataset is exhausted. When passing an infinitely
            repeating dataset, you must specify the `steps` argument. This
            argument is not supported with array inputs.
        reset_state: Optional argument specifying whether to clear the state of
          the layer at the start of the call to `adapt`, or whether to start
          from the existing state. This argument may not be relevant to all
          preprocessing layers: a subclass of PreprocessingLayer may choose to
          throw if 'reset_state' is set to False.
    """
        _disallow_inside_tf_function('adapt')
        if not version_utils.should_use_v2():
            raise RuntimeError('`adapt` is only supported in tensorflow v2.')
        if not self.streaming and self._is_adapted and (not reset_state):
            raise ValueError('{} does not supporting calling `adapt` twice without resetting the state.'.format(self.__class__.__name__))
        if not self._is_compiled:
            self.compile()
        if self.built and reset_state:
            self.reset_state()
        data_handler = data_adapter.DataHandler(data, batch_size=batch_size, steps_per_epoch=steps, epochs=1, steps_per_execution=self._steps_per_execution, distribute=False)
        self._adapt_function = self.make_adapt_function()
        for _, iterator in data_handler.enumerate_epochs():
            with data_handler.catch_stop_iteration():
                for _ in data_handler.steps():
                    self._adapt_function(iterator)
                    if data_handler.should_sync:
                        context.async_wait()
        self.finalize_state()
        self._is_adapted = True

    def _reset_state_wrapper(self):
        """Calls `reset_state` and sets `adapted` to `False`."""
        self._reset_state_impl()
        self._is_adapted = False

    @trackable.no_automatic_dependency_tracking
    def _configure_steps_per_execution(self, steps_per_execution):
        self._steps_per_execution = variables.Variable(steps_per_execution, dtype='int64', aggregation=variables.VariableAggregationV2.ONLY_FIRST_REPLICA)

    def _adapt_maybe_build(self, data):
        if not self.built:
            try:
                data_shape = data.shape
                data_shape_nones = tuple([None] * len(data.shape))
            except AttributeError:
                data_shape = None
                data_shape_nones = None
            batch_input_shape = getattr(self, '_batch_input_shape', None)
            if batch_input_shape is None:
                self._batch_input_shape = data_shape_nones
            self.build(data_shape)
            self.built = True

Base class for Preprocessing Layers.

**Don't use this class directly: it's an abstract base class!** You may
be looking for one of the many built-in
[preprocessing layers](https://keras.io/guides/preprocessing_layers/)
instead.

Preprocessing layers are layers whose state gets computed before model
training starts. They do not get updated during training.
Most preprocessing layers implement an `adapt()` method for state computation.

The `PreprocessingLayer` class is the base class you would subclass to
implement your own preprocessing layers.

Attributes:
  streaming: Whether a layer can be adapted multiple times without resetting
    the state of the layer.


def __call__(self, shape, dtype=None):
    dtype = standardize_dtype(dtype)
    return ops.ones(shape, dtype=dtype)

Returns a tensor object initialized as specified by the initializer.

Args:
    shape: Shape of the tensor.
    dtype: Optional dtype of the tensor. Only numeric or boolean dtypes
        are supported. If not specified, `keras.backend.floatx()`
        is used, which default to `float32` unless you configured it
        otherwise (via `keras.backend.set_floatx(float_dtype)`).


def get_matching_files_v2(pattern):
    if isinstance(pattern, six.string_types):
        return [compat.as_str_any(matching_filename) for matching_filename in _pywrap_file_io.GetMatchingFiles(compat.as_bytes(pattern))]
    else:
        return [compat.as_str_any(matching_filename) for single_filename in pattern for matching_filename in _pywrap_file_io.GetMatchingFiles(compat.as_bytes(single_filename))]

Returns a list of files that match the given pattern(s).

The patterns are defined as strings. Supported patterns are defined
here. Note that the pattern can be a Python iteratable of string patterns.

The format definition of the pattern is:

**pattern**: `{ term }`

**term**:
  * `'*'`: matches any sequence of non-'/' characters
  * `'?'`: matches a single non-'/' character
  * `'[' [ '^' ] { match-list } ']'`: matches any single
    character (not) on the list
  * `c`: matches character `c`  where `c != '*', '?', '\\', '['`
  * `'\\' c`: matches character `c`

**character range**:
  * `c`: matches character `c` while `c != '\\', '-', ']'`
  * `'\\' c`: matches character `c`
  * `lo '-' hi`: matches character `c` for `lo <= c <= hi`

Examples:

>>> tf.io.gfile.glob("*.py")
... # For example, ['__init__.py']

>>> tf.io.gfile.glob("__init__.??")
... # As above

>>> files = {"*.py"}
>>> the_iterator = iter(files)
>>> tf.io.gfile.glob(the_iterator)
... # As above

See the C++ function `GetMatchingPaths` in
[`core/platform/file_system.h`]
(../../../core/platform/file_system.h)
for implementation details.

Args:
  pattern: string or iterable of strings. The glob pattern(s).

Returns:
  A list of strings containing filenames that match the given pattern(s).

Raises:
  errors.OpError: If there are filesystem / directory listing errors.
  errors.NotFoundError: If pattern to be matched is an invalid directory.


def PrepareForExport(module_name, ast, loader):
    src = pytd_utils.Print(ast)
    return SourceToExportableAst(module_name, src, loader)

Prepare an ast as if it was parsed and loaded.

External dependencies will not be resolved, as the ast generated by this
method is supposed to be exported.

Args:
  module_name: The module_name as a string for the returned ast.
  ast: pytd.TypeDeclUnit, is only used if src is None.
  loader: A load_pytd.Loader instance.

Returns:
  A pytd.TypeDeclUnit representing the supplied AST as it would look after
  being written to a file and parsed.


def search(nasbench, search_model, algo, repeat_id, max_train_hours=5000000.0):
    nasbench.reset_budget_counters()
    times, best_valids, best_tests = ([0.0], [0.0], [0.0])
    valid_models = 0
    time_spent = 0
    start_time = time.time()
    last_report_time = start_time
    for model, feedback in pg.sample(search_model, algo, name=str(repeat_id)):
        spec = model()
        if nasbench.is_valid(spec):
            results = nasbench.query(spec)
            valid_models += 1
            feedback(results['validation_accuracy'])
            if results['validation_accuracy'] > best_valids[-1]:
                best_valids.append(results['validation_accuracy'])
                best_tests.append(results['test_accuracy'])
            else:
                best_valids.append(best_valids[-1])
                best_tests.append(best_tests[-1])
            time_spent, _ = nasbench.get_budget_counters()
            times.append(time_spent)
            if time_spent > max_train_hours:
                feedback.end_loop()
                break
        else:
            feedback.skip()
        if feedback.id % 100 == 0:
            now = time.time()
            print(f'Tried {feedback.id} models, valid {valid_models}, time_spent {time_spent}, elapse since last report: {now - last_report_time} seconds.')
            last_report_time = now
    print(f'Total time elapse: {time.time() - start_time} seconds.')
    return (times, best_valids, best_tests)

Define the search procedure.

Args:
  nasbench: NASBench object.
  search_model: which is a `model` object annotated with `oneof`.
  algo: algorithm for search.
  repeat_id: identifier of current repeat.
  max_train_hours: max time budget to train the models, which is the sum
    of training time queried from NAS-Bench.

Returns:
  A tuple of (total time spent at step i for all steps,
              best validation accuracy at step i for all steps,
              best test accuracy at step i for all steps)


def error(msg: str, *args, **kwargs) -> None:
    _DEFAULT_LOGGER.error(msg, *args, **kwargs)

Logs error message.

Args:
  msg: Message with possible format string.
  *args: Values for variables in the format string.
  **kwargs: Keyword arguments for the logger.


def set_optimizer(self, name: Union[str, OptimizerNames]='adamw_torch', learning_rate: float=5e-05, weight_decay: float=0, beta1: float=0.9, beta2: float=0.999, epsilon: float=1e-08, args: Optional[str]=None):
    self.optim = OptimizerNames(name)
    self.learning_rate = learning_rate
    self.weight_decay = weight_decay
    self.adam_beta1 = beta1
    self.adam_beta2 = beta2
    self.adam_epsilon = epsilon
    self.optim_args = args
    return self

A method that regroups all arguments linked to the optimizer and its hyperparameters.

Args:
    name (`str` or [`training_args.OptimizerNames`], *optional*, defaults to `"adamw_torch"`):
        The optimizer to use: `"adamw_torch"`, `"adamw_torch_fused"`, `"adamw_apex_fused"`,
        `"adamw_anyprecision"` or `"adafactor"`.
    learning_rate (`float`, *optional*, defaults to 5e-5):
        The initial learning rate.
    weight_decay (`float`, *optional*, defaults to 0):
        The weight decay to apply (if not zero) to all layers except all bias and LayerNorm weights.
    beta1 (`float`, *optional*, defaults to 0.9):
        The beta1 hyperparameter for the adam optimizer or its variants.
    beta2 (`float`, *optional*, defaults to 0.999):
        The beta2 hyperparameter for the adam optimizer or its variants.
    epsilon (`float`, *optional*, defaults to 1e-8):
        The epsilon hyperparameter for the adam optimizer or its variants.
    args (`str`, *optional*):
        Optional arguments that are supplied to AnyPrecisionAdamW (only useful when
        `optim="adamw_anyprecision"`).

Example:

```py
>>> from transformers import TrainingArguments

>>> args = TrainingArguments("working_dir")
>>> args = args.set_optimizer(name="adamw_torch", beta1=0.8)
>>> args.optim
'adamw_torch'
```


def __init__(self, funcs, trackable_obj=None):
    super(TFLiteConverterV2, self).__init__(funcs, trackable_obj)

Constructor for TFLiteConverter.

Args:
  funcs: List of TensorFlow ConcreteFunctions. The list should not contain
    duplicate elements.
  trackable_obj: tf.AutoTrackable object associated with `funcs`. A
    reference to this object needs to be maintained so that Variables do not
    get garbage collected since functions have a weak reference to
    Variables. This is only required when the tf.AutoTrackable object is not
    maintained by the user (e.g. `from_saved_model`).


def map_values(op, *args, **kwargs):
    sparse_list = []
    inner_args = _replace_sparse_with_values(args, sparse_list)
    inner_kwargs = _replace_sparse_with_values(kwargs, sparse_list)
    if not sparse_list:
        raise ValueError('No SparseTensor in argument list of map_values')
    with ops.control_dependencies(_assert_sparse_compatible(sparse_list)):
        return sparse_tensor.SparseTensor(sparse_list[0].indices, op(*inner_args, **inner_kwargs), sparse_list[0].dense_shape)

Applies `op` to the `.values` tensor of one or more `SparseTensor`s.

Replaces any `SparseTensor` in `args` or `kwargs` with its `values`
tensor (which contains the non-default values for the SparseTensor),
and then calls `op`.  Returns a `SparseTensor` that is constructed
from the input `SparseTensor`s' `indices`, `dense_shape`, and the
value returned by the `op`.

If the input arguments contain multiple `SparseTensor`s, then they must have
equal `indices` and dense shapes.

Examples:

>>> s = tf.sparse.from_dense([[1, 2, 0],
...                           [0, 4, 0],
...                           [1, 0, 0]])
>>> tf.sparse.to_dense(tf.sparse.map_values(tf.ones_like, s)).numpy()
array([[1, 1, 0],
       [0, 1, 0],
       [1, 0, 0]], dtype=int32)

>>> tf.sparse.to_dense(tf.sparse.map_values(tf.multiply, s, s)).numpy()
array([[ 1,  4,  0],
       [ 0, 16,  0],
       [ 1,  0,  0]], dtype=int32)

>>> tf.sparse.to_dense(tf.sparse.map_values(tf.add, s, 5)).numpy()
array([[6, 7, 0],
       [0, 9, 0],
       [6, 0, 0]], dtype=int32)

Note: even though `tf.add(0, 5) != 0`, implicit zeros
will remain unchanged. However, if the sparse tensor contains any explicit
zeros, these will be affected by the mapping!

Args:
  op: The operation that should be applied to the SparseTensor `values`. `op`
    is typically an element-wise operation (such as math_ops.add), but any
    operation that preserves the shape can be used.
  *args: Arguments for `op`.
  **kwargs: Keyword arguments for `op`.

Returns:
  A `SparseTensor` whose `indices` and `dense_shape` matches the `indices`
  and `dense_shape` of all input `SparseTensor`s.
Raises:
  ValueError: If args contains no `SparseTensor`, or if the `indices`
    or `dense_shape`s of the input `SparseTensor`s are not equal.


def forward(self, inputs: torch.Tensor):
    output = inputs.transpose(1, 2)
    output = self.batchnorm(output)
    return output.transpose(1, 2)

Parameters:
    inputs (`torch.Tensor` of shape `(batch_size, sequence_length, d_model)`):
        input for Batch norm calculation
Returns:
    `torch.Tensor` of shape `(batch_size, sequence_length, d_model)`


class XGBoostModelHandlerPandas(XGBoostModelHandler[pandas.DataFrame, PredictionResult, Union[xgboost.Booster, xgboost.XGBModel]]):

    def run_inference(self, batch: Sequence[pandas.DataFrame], model: Union[xgboost.Booster, xgboost.XGBModel], inference_args: Optional[dict[str, Any]]=None) -> Iterable[PredictionResult]:
        """Runs inferences on a batch of pandas dataframes.

    Args:
      batch: A sequence of examples as pandas dataframes. Each
        row in a dataframe is a single example. The dimensions
        must match the dimensions of the data used to train
        the model.
      model: XGBoost booster or XBGModel (sklearn interface). Must
        implement predict(X). Where the parameter X is a pandas dataframe.
      inference_args: Any additional arguments for an inference.

    Returns:
      An Iterable of type PredictionResult.
    """
        return self._inference_fn(batch, model, inference_args)

    def get_num_bytes(self, batch: Sequence[pandas.DataFrame]) -> int:
        """
    Returns:
        The number of bytes of data for a batch of Numpy arrays.
    """
        return sum((df.memory_usage(deep=True).sum() for df in batch))

Implementation of the ModelHandler interface for XGBoost
using pandas dataframes as input.

Example Usage::

    pcoll | RunInference(
                XGBoostModelHandlerPandas(
                    model_class="XGBoost Model Class",
                    model_state="my_model_state.json")))

Args:
  model_class: class of the XGBoost model that defines the model
    structure.
  model_state: path to a json file that contains the model's
    configuration.
  inference_fn: the inference function to use during RunInference.
    default=default_xgboost_inference_fn


class ProphetNetTokenizer(PreTrainedTokenizer):
    vocab_files_names = VOCAB_FILES_NAMES
    model_input_names: List[str] = ['input_ids', 'attention_mask']

    def __init__(self, vocab_file: str, do_lower_case: Optional[bool]=True, do_basic_tokenize: Optional[bool]=True, never_split: Optional[Iterable]=None, unk_token: Optional[str]='[UNK]', sep_token: Optional[str]='[SEP]', x_sep_token: Optional[str]='[X_SEP]', pad_token: Optional[str]='[PAD]', mask_token: Optional[str]='[MASK]', tokenize_chinese_chars: Optional[bool]=True, strip_accents: Optional[bool]=None, clean_up_tokenization_spaces: bool=True, **kwargs):
        if not os.path.isfile(vocab_file):
            raise ValueError(f"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained model use `tokenizer = AutoTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`")
        self.vocab = load_vocab(vocab_file)
        self.ids_to_tokens = collections.OrderedDict([(ids, tok) for tok, ids in self.vocab.items()])
        self.do_basic_tokenize = do_basic_tokenize
        if do_basic_tokenize:
            self.basic_tokenizer = BasicTokenizer(do_lower_case=do_lower_case, never_split=never_split, tokenize_chinese_chars=tokenize_chinese_chars, strip_accents=strip_accents)
        self.wordpiece_tokenizer = WordpieceTokenizer(vocab=self.vocab, unk_token=str(unk_token))
        super().__init__(do_lower_case=do_lower_case, do_basic_tokenize=do_basic_tokenize, never_split=never_split, unk_token=unk_token, sep_token=sep_token, x_sep_token=x_sep_token, pad_token=pad_token, mask_token=mask_token, tokenize_chinese_chars=tokenize_chinese_chars, strip_accents=strip_accents, clean_up_tokenization_spaces=clean_up_tokenization_spaces, **kwargs)

    @property
    def vocab_size(self):
        return len(self.vocab)

    def get_vocab(self):
        return dict(self.vocab, **self.added_tokens_encoder)

    def _tokenize(self, text):
        split_tokens = []
        if self.do_basic_tokenize:
            for token in self.basic_tokenizer.tokenize(text, never_split=self.all_special_tokens):
                if token in self.basic_tokenizer.never_split:
                    split_tokens.append(token)
                else:
                    split_tokens += self.wordpiece_tokenizer.tokenize(token)
        else:
            split_tokens = self.wordpiece_tokenizer.tokenize(text)
        return split_tokens

    def _convert_token_to_id(self, token: str):
        """Converts a token (str) in an id using the vocab."""
        return self.vocab.get(token, self.vocab.get(self.unk_token))

    def _convert_id_to_token(self, index: int):
        """Converts an index (integer) in a token (str) using the vocab."""
        return self.ids_to_tokens.get(index, self.unk_token)

    def convert_tokens_to_string(self, tokens: str):
        """Converts a sequence of tokens (string) in a single string."""
        out_string = ' '.join(tokens).replace(' ##', '').strip()
        return out_string

    def get_special_tokens_mask(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None, already_has_special_tokens: Optional[bool]=False) -> List[int]:
        """
        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.
        """
        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 [0] * len(token_ids_0) + [1]
        return [0] * len(token_ids_0) + [1] + [0] * len(token_ids_1) + [1]

    def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str]=None) -> Tuple[str]:
        index = 0
        if os.path.isdir(save_directory):
            vocab_file = os.path.join(save_directory, (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file'])
        else:
            vocab_file = (filename_prefix + '-' if filename_prefix else '') + save_directory
        with open(vocab_file, 'w', encoding='utf-8') as writer:
            for token, token_index in sorted(self.vocab.items(), key=lambda kv: kv[1]):
                if index != token_index:
                    logger.warning(f'Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive. Please check that the vocabulary is not corrupted!')
                    index = token_index
                writer.write(token + '\n')
                index += 1
        return (vocab_file,)

    def build_inputs_with_special_tokens(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None) -> List[int]:
        """
        Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
        adding special tokens. A BERT sequence has the following format:

        - single sequence: `[CLS] X [SEP]`
        - pair of sequences: `[CLS] A [SEP] B [SEP]`

        Args:
            token_ids_0 (`List[int]`):
                List of IDs to which the special tokens will be added.
            token_ids_1 (`List[int]`, *optional*):
                Optional second list of IDs for sequence pairs.

        Returns:
            `List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
        """
        if token_ids_1 is None:
            return token_ids_0 + [self.sep_token_id]
        sep = [self.sep_token_id]
        return token_ids_0 + sep + token_ids_1 + sep

Construct a ProphetNetTokenizer. Based on WordPiece.

This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
this superclass for more information regarding those methods.

Args:
    vocab_file (`str`):
        File containing the vocabulary.
    do_lower_case (`bool`, *optional*, defaults to `True`):
        Whether or not to lowercase the input when tokenizing.
    do_basic_tokenize (`bool`, *optional*, defaults to `True`):
        Whether or not to do basic tokenization before WordPiece.
    never_split (`Iterable`, *optional*):
        Collection of tokens which will never be split during tokenization. Only has an effect when
        `do_basic_tokenize=True`
    unk_token (`str`, *optional*, defaults to `"[UNK]"`):
        The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
        token instead.
    sep_token (`str`, *optional*, defaults to `"[SEP]"`):
        The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for
        sequence classification or for a text and a question for question answering. It is also used as the last
        token of a sequence built with special tokens.
    x_sep_token (`str`, *optional*, defaults to `"[X_SEP]"`):
        Special second separator token, which can be generated by [`ProphetNetForConditionalGeneration`]. It is
        used to separate bullet-point like sentences in summarization, *e.g.*.
    pad_token (`str`, *optional*, defaults to `"[PAD]"`):
        The token used for padding, for example when batching sequences of different lengths.
    mask_token (`str`, *optional*, defaults to `"[MASK]"`):
        The token used for masking values. This is the token used when training this model with masked language
        modeling. This is the token which the model will try to predict.
    tokenize_chinese_chars (`bool`, *optional*, defaults to `True`):
        Whether or not to tokenize Chinese characters.

        This should likely be deactivated for Japanese (see this
        [issue](https://github.com/huggingface/transformers/issues/328)).
    strip_accents (`bool`, *optional*):
        Whether or not to strip all accents. If this option is not specified, then it will be determined by the
        value for `lowercase` (as in the original BERT).
    clean_up_tokenization_spaces (`bool`, *optional*, defaults to `True`):
        Whether or not to cleanup spaces after decoding, cleanup consists in removing potential artifacts like
        extra spaces.


class Ones(Initializer):

    def __call__(self, shape, dtype=None):
        """Returns a tensor object initialized as specified by the initializer.

        Args:
            shape: Shape of the tensor.
            dtype: Optional dtype of the tensor. Only numeric or boolean dtypes
                are supported. If not specified, `keras.backend.floatx()`
                is used, which default to `float32` unless you configured it
                otherwise (via `keras.backend.set_floatx(float_dtype)`).
        """
        dtype = standardize_dtype(dtype)
        return ops.ones(shape, dtype=dtype)

Initializer that generates tensors initialized to 1.

Also available via the shortcut function `ones`.

Examples:

>>> # Standalone usage:
>>> initializer = Ones()
>>> values = initializer(shape=(2, 2))

>>> # Usage in a Keras layer:
>>> initializer = Ones()
>>> layer = Dense(3, kernel_initializer=initializer)


def douglas_adi_step(theta=0.5):

    def _step_fn(time, next_time, coord_grid, value_grid, boundary_conditions, second_order_coeff_fn, first_order_coeff_fn, zeroth_order_coeff_fn, inner_second_order_coeff_fn, inner_first_order_coeff_fn, num_steps_performed, dtype=None, name=None):
        """Performs the step."""
        del num_steps_performed
        name = name or 'douglas_adi_step'
        return multidim_parabolic_equation_step(time, next_time, coord_grid, value_grid, boundary_conditions, douglas_adi_scheme(theta), second_order_coeff_fn, first_order_coeff_fn, zeroth_order_coeff_fn, inner_second_order_coeff_fn, inner_first_order_coeff_fn, dtype=dtype, name=name)
    return _step_fn

Creates a stepper function with Crank-Nicolson time marching scheme.

Douglas ADI scheme is the simplest time marching scheme for solving parabolic
PDEs with multiple spatial dimensions. The time step consists of several
substeps: the first one is fully explicit, and the following `N` steps are
implicit with respect to contributions of one of the `N` axes (hence "ADI" -
alternating direction implicit). See `douglas_adi_scheme` below for more
details.

Args:
  theta: positive Number. `theta = 0` corresponds to fully explicit scheme.
  The larger `theta` the stronger are the corrections by the implicit
  substeps. The recommended value is `theta = 0.5`, because the scheme is
  second order accurate in that case, unless mixed second derivative terms are
  present in the PDE.
Returns:
  Callable to be used in finite-difference PDE solvers (see fd_solvers.py).


def forward(self, input_ids: Optional[torch.Tensor]=None, attention_mask: Optional[torch.Tensor]=None, position_ids: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None) -> BaseModelOutputWithPooling:
    return self.text_model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, output_attentions=output_attentions, output_hidden_states=output_hidden_states)

Examples:

```python
>>> from transformers import AutoTokenizer, SiglipTextModel

>>> model = SiglipTextModel.from_pretrained("google/siglip-base-patch16-224")
>>> tokenizer = AutoTokenizer.from_pretrained("google/siglip-base-patch16-224")

>>> # important: make sure to set padding="max_length" as that's how the model was trained
>>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding="max_length", return_tensors="pt")

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


def scatter_nd_max(self, indices, updates, name=None):
    return self._lazy_read(gen_state_ops.resource_scatter_nd_max(self.handle, indices, ops.convert_to_tensor(updates, self.dtype), name=name))

Updates this variable with the max of `tf.IndexedSlices` and itself.

`ref` is a `Tensor` with rank `P` and `indices` is a `Tensor` of rank `Q`.

`indices` must be integer tensor, containing indices into `ref`.
It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

The innermost dimension of `indices` (with length `K`) corresponds to
indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
dimension of `ref`.

`updates` is `Tensor` of rank `Q-1+P-K` with shape:

```
[d_0, ..., d_{Q-2}, ref.shape[K], ..., ref.shape[P-1]].
```

See `tf.scatter_nd` for more details about how to make updates to
slices.

Args:
  indices: The indices to be used in the operation.
  updates: The values to be used in the operation.
  name: the name of the operation.

Returns:
  The updated variable.


def mlir_convert_file(graph_def_filename, input_tensors, output_tensors, quantization_params=None, additional_flags=''):
    bin_path = resource_loader.get_path_to_datafile('../../../../compiler/mlir/lite/tf_tfl_translate')
    with tempfile.NamedTemporaryFile() as output_file, tempfile.NamedTemporaryFile('w+') as stdout_file:
        input_shapes = []
        for input_tensor in input_tensors:
            shape = input_tensor[1]
            input_shapes.append(','.join([str(dim) for dim in shape]))
        input_shapes_str = ':'.join(input_shapes)
        input_types = ','.join([x[2] for x in input_tensors])
        quant_flags = ''
        if quantization_params is not None:
            min_vals = ','.join([str(val) for val in quantization_params[1]])
            max_vals = ','.join([str(val) for val in quantization_params[2]])
            quant_flags = '-tf-inference-type=' + quantization_params[0] + " -tf-input-min-values='" + min_vals + "' -tf-input-max-values='" + max_vals + "' " + '-emit-quant-adaptor-ops '
        cmd = '%s -tf-input-arrays=%s -tf-input-data-types=%s -tf-input-shapes=%s -tf-output-arrays=%s ' + quant_flags + additional_flags + '%s -o %s'
        cmd = cmd % (bin_path, ','.join([x[0] for x in input_tensors]), input_types, input_shapes_str, ','.join(output_tensors), graph_def_filename, output_file.name)
        exit_code = os.system(cmd)
        log = cmd + 'exited with code %d' % exit_code + '\n------------------\n' + stdout_file.read()
        return (None if exit_code != 0 else output_file.read(), log)

Convert a graphdef file into a tflite model with MLIR-based conversion.

NOTE: this currently shells out to the MLIR binary binary, but we would like
convert to Python API tooling in the future.

Args:
  graph_def_filename: A GraphDef file.
  input_tensors: List of input tensor tuples `(name, shape, type)`. name
    should be a string. shape should be a tuple of integers. type should be a
    string, for example 'DT_FLOAT'
  output_tensors: List of output tensors (names).
  quantization_params: parameters `(inference_type, min_values, max_values)`
    to quantize the model.
  additional_flags: A string of additional command line flags to be passed to
    MLIR converter.

Returns:
  output tflite model, log_txt from conversion
  or None, log_txt if it did not convert properly.


def forward(self, inputs_embeds, attention_mask: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple, BaseModelOutput]:
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    encoder_states = () if output_hidden_states else None
    all_attentions = () if output_attentions else None
    hidden_states = inputs_embeds
    for idx, encoder_layer in enumerate(self.layers):
        if output_hidden_states:
            encoder_states = encoder_states + (hidden_states,)
        layer_outputs = encoder_layer(hidden_states, attention_mask, output_attentions=output_attentions)
        hidden_states = layer_outputs[0]
        if output_attentions:
            all_attentions = all_attentions + (layer_outputs[1],)
    if output_hidden_states:
        encoder_states = encoder_states + (hidden_states,)
    if not return_dict:
        return tuple((v for v in [hidden_states, encoder_states, all_attentions] if v is not None))
    return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)

Args:
    inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
        Embedded representation of the inputs. Should be float, not int tokens.
    attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
        Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

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

        [What are attention masks?](../glossary#attention-mask)
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers. See `attentions` under
        returned tensors for more detail.
    output_hidden_states (`bool`, *optional*):
        Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
        for more detail.
    return_dict (`bool`, *optional*):
        Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.


class UpSampling2D(Layer):

    def __init__(self, size=(2, 2), data_format=None, interpolation='nearest', **kwargs):
        super().__init__(**kwargs)
        self.data_format = backend.standardize_data_format(data_format)
        self.size = argument_validation.standardize_tuple(size, 2, 'size')
        self.interpolation = interpolation.lower()
        self.input_spec = InputSpec(ndim=4)

    def compute_output_shape(self, input_shape):
        if self.data_format == 'channels_first':
            height = self.size[0] * input_shape[2] if input_shape[2] is not None else None
            width = self.size[1] * input_shape[3] if input_shape[3] is not None else None
            return (input_shape[0], input_shape[1], height, width)
        else:
            height = self.size[0] * input_shape[1] if input_shape[1] is not None else None
            width = self.size[1] * input_shape[2] if input_shape[2] is not None else None
            return (input_shape[0], height, width, input_shape[3])

    def call(self, inputs):
        return self._resize_images(inputs, self.size[0], self.size[1], self.data_format, interpolation=self.interpolation)

    def get_config(self):
        config = {'size': self.size, 'data_format': self.data_format, 'interpolation': self.interpolation}
        base_config = super().get_config()
        return {**base_config, **config}

    def _resize_images(self, x, height_factor, width_factor, data_format, interpolation='nearest'):
        """Resizes the images contained in a 4D tensor.

        Args:
            x: Tensor or variable to resize.
            height_factor: Positive integer.
            width_factor: Positive integer.
            data_format: One of `"channels_first"`, `"channels_last"`.
            interpolation: A string, one of `"bicubic"`, `"bilinear"`,
            `"lanczos3"`, `"lanczos5"`, or `"nearest"`.

        Returns:
            A tensor.
        """
        if data_format not in {'channels_last', 'channels_first'}:
            raise ValueError(f'Invalid `data_format` argument: {data_format}')
        if data_format == 'channels_first':
            x = ops.transpose(x, [0, 2, 3, 1])
        if interpolation == 'nearest':
            x = ops.repeat(x, height_factor, axis=1)
            x = ops.repeat(x, width_factor, axis=2)
        else:
            shape = ops.shape(x)
            new_shape = (shape[1] * height_factor, shape[2] * width_factor)
            x = ops.image.resize(x, new_shape, interpolation=interpolation)
        if data_format == 'channels_first':
            x = ops.transpose(x, [0, 3, 1, 2])
        return x

Upsampling layer for 2D inputs.

The implementation uses interpolative resizing, given the resize method
(specified by the `interpolation` argument). Use `interpolation=nearest`
to repeat the rows and columns of the data.

Example:

>>> input_shape = (2, 2, 1, 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 = keras.layers.UpSampling2D(size=(1, 2))(x)
>>> print(y)
[[[[ 0  1  2]
   [ 0  1  2]]
  [[ 3  4  5]
   [ 3  4  5]]]
 [[[ 6  7  8]
   [ 6  7  8]]
  [[ 9 10 11]
   [ 9 10 11]]]]

Args:
    size: Int, or tuple of 2 integers.
        The upsampling factors for rows and columns.
    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_size, height, width, channels)` while `"channels_first"`
        corresponds to inputs with shape
        `(batch_size, channels, height, width)`.
        When unspecified, uses
        `image_data_format` value found in your Keras config file at
        `~/.keras/keras.json` (if exists) else `"channels_last"`.
        Defaults to `"channels_last"`.
    interpolation: A string, one of `"bicubic"`, `"bilinear"`, `"lanczos3"`,
        `"lanczos5"`, `"nearest"`.

Input shape:
    4D tensor with shape:
    - If `data_format` is `"channels_last"`:
        `(batch_size, rows, cols, channels)`
    - If `data_format` is `"channels_first"`:
        `(batch_size, channels, rows, cols)`

Output shape:
    4D tensor with shape:
    - If `data_format` is `"channels_last"`:
        `(batch_size, upsampled_rows, upsampled_cols, channels)`
    - If `data_format` is `"channels_first"`:
        `(batch_size, channels, upsampled_rows, upsampled_cols)`


class MarianConfig(PretrainedConfig):
    model_type = 'marian'
    keys_to_ignore_at_inference = ['past_key_values']
    attribute_map = {'num_attention_heads': 'encoder_attention_heads', 'hidden_size': 'd_model'}

    def __init__(self, vocab_size=58101, decoder_vocab_size=None, max_position_embeddings=1024, encoder_layers=12, encoder_ffn_dim=4096, encoder_attention_heads=16, decoder_layers=12, decoder_ffn_dim=4096, decoder_attention_heads=16, encoder_layerdrop=0.0, decoder_layerdrop=0.0, use_cache=True, is_encoder_decoder=True, activation_function='gelu', d_model=1024, dropout=0.1, attention_dropout=0.0, activation_dropout=0.0, init_std=0.02, decoder_start_token_id=58100, scale_embedding=False, pad_token_id=58100, eos_token_id=0, forced_eos_token_id=0, share_encoder_decoder_embeddings=True, **kwargs):
        self.vocab_size = vocab_size
        self.decoder_vocab_size = decoder_vocab_size or vocab_size
        self.max_position_embeddings = max_position_embeddings
        self.d_model = d_model
        self.encoder_ffn_dim = encoder_ffn_dim
        self.encoder_layers = encoder_layers
        self.encoder_attention_heads = encoder_attention_heads
        self.decoder_ffn_dim = decoder_ffn_dim
        self.decoder_layers = decoder_layers
        self.decoder_attention_heads = decoder_attention_heads
        self.dropout = dropout
        self.attention_dropout = attention_dropout
        self.activation_dropout = activation_dropout
        self.activation_function = activation_function
        self.init_std = init_std
        self.encoder_layerdrop = encoder_layerdrop
        self.decoder_layerdrop = decoder_layerdrop
        self.use_cache = use_cache
        self.num_hidden_layers = encoder_layers
        self.scale_embedding = scale_embedding
        self.share_encoder_decoder_embeddings = share_encoder_decoder_embeddings
        super().__init__(pad_token_id=pad_token_id, eos_token_id=eos_token_id, is_encoder_decoder=is_encoder_decoder, decoder_start_token_id=decoder_start_token_id, forced_eos_token_id=forced_eos_token_id, **kwargs)

This is the configuration class to store the configuration of a [`MarianModel`]. It is used to instantiate an
Marian 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 Marian
[Helsinki-NLP/opus-mt-en-de](https://huggingface.co/Helsinki-NLP/opus-mt-en-de) architecture.

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

Args:
    vocab_size (`int`, *optional*, defaults to 58101):
        Vocabulary size of the Marian model. Defines the number of different tokens that can be represented by the
        `inputs_ids` passed when calling [`MarianModel`] or [`TFMarianModel`].
    d_model (`int`, *optional*, defaults to 1024):
        Dimensionality of the layers and the pooler layer.
    encoder_layers (`int`, *optional*, defaults to 12):
        Number of encoder layers.
    decoder_layers (`int`, *optional*, defaults to 12):
        Number of decoder layers.
    encoder_attention_heads (`int`, *optional*, defaults to 16):
        Number of attention heads for each attention layer in the Transformer encoder.
    decoder_attention_heads (`int`, *optional*, defaults to 16):
        Number of attention heads for each attention layer in the Transformer decoder.
    decoder_ffn_dim (`int`, *optional*, defaults to 4096):
        Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
    encoder_ffn_dim (`int`, *optional*, defaults to 4096):
        Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
    activation_function (`str` or `function`, *optional*, defaults to `"gelu"`):
        The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
        `"relu"`, `"silu"` and `"gelu_new"` are supported.
    dropout (`float`, *optional*, defaults to 0.1):
        The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
    attention_dropout (`float`, *optional*, defaults to 0.0):
        The dropout ratio for the attention probabilities.
    activation_dropout (`float`, *optional*, defaults to 0.0):
        The dropout ratio for activations inside the fully connected layer.
    max_position_embeddings (`int`, *optional*, defaults to 1024):
        The maximum sequence length that this model might ever be used with. Typically set this to something large
        just in case (e.g., 512 or 1024 or 2048).
    init_std (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    encoder_layerdrop (`float`, *optional*, defaults to 0.0):
        The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
        for more details.
    decoder_layerdrop (`float`, *optional*, defaults to 0.0):
        The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
        for more details.
    scale_embedding (`bool`, *optional*, defaults to `False`):
        Scale embeddings by diving by sqrt(d_model).
    use_cache (`bool`, *optional*, defaults to `True`):
        Whether or not the model should return the last key/values attentions (not used by all models)
    forced_eos_token_id (`int`, *optional*, defaults to 0):
        The id of the token to force as the last generated token when `max_length` is reached. Usually set to
        `eos_token_id`.

Examples:

```python
>>> from transformers import MarianModel, MarianConfig

>>> # Initializing a Marian Helsinki-NLP/opus-mt-en-de style configuration
>>> configuration = MarianConfig()

>>> # Initializing a model from the Helsinki-NLP/opus-mt-en-de style configuration
>>> model = MarianModel(configuration)

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


class FlaxBaseModelOutputWithPast(ModelOutput):
    last_hidden_state: Optional[jnp.ndarray] = None
    past_key_values: Optional[Dict[str, jnp.ndarray]] = None
    hidden_states: Optional[Tuple[jnp.ndarray]] = None
    attentions: Optional[Tuple[jnp.ndarray]] = None

Base class for model's outputs, with potential hidden states and attentions.

Args:
    last_hidden_state (`jnp.ndarray` of shape `(batch_size, sequence_length, hidden_size)`):
        Sequence of hidden-states at the output of the last layer of the model.
    past_key_values (`Dict[str, jnp.ndarray]`):
        Dictionary of pre-computed hidden-states (key and values in the attention blocks) that can be used for fast
        auto-regressive decoding. Pre-computed key and value hidden-states are of shape *[batch_size, max_length]*.
    hidden_states (`tuple(jnp.ndarray)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `jnp.ndarray` (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(jnp.ndarray)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `jnp.ndarray` (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 _resize_for_patching(self, image: np.array, target_resolution: tuple, resample, input_data_format: ChannelDimension) -> np.array:
    new_height, new_width = get_patch_output_size(image, target_resolution, input_data_format)
    resized_image = resize(image, (new_height, new_width), resample=resample, input_data_format=input_data_format)
    return resized_image

Resizes an image to a target resolution while maintaining aspect ratio.

Args:
    image (np.array):
        The input image.
    target_resolution (tuple):
        The target resolution (height, width) of the image.
    resample (`PILImageResampling`):
        Resampling filter to use if resizing the image.
    input_data_format (`ChannelDimension` or `str`):
        The channel dimension format of the input image.

Returns:
    np.array: The resized and padded image.


def _GetGenericBasesLookupMap(self, node):
    mapping = collections.defaultdict(list)
    seen_bases = set()
    bases = list(reversed(node.bases))
    while bases:
        base = bases.pop()
        if base in seen_bases:
            continue
        seen_bases.add(base)
        if isinstance(base, pytd.GenericType) and isinstance(base.base_type, pytd.ClassType):
            mapping[base.base_type].append(base)
            bases.extend(reversed(base.base_type.cls.bases))
        elif isinstance(base, pytd.ClassType):
            bases.extend(reversed(base.cls.bases))
    return mapping

Get a lookup map for the generic bases of a class.

Gets a map from a pytd.ClassType to the list of pytd.GenericType bases of
the node that have that class as their base. This method does depth-first
traversal of the bases, which ensures that the order of elements in each
list is consistent with the node's MRO.

Args:
  node: A pytd.Class node.

Returns:
  A pytd.ClassType -> List[pytd.GenericType] map.


def _LineContainsI18n(line):
    if style.Get('I18N_COMMENT'):
        for tok in line.tokens:
            if tok.is_comment and re.match(style.Get('I18N_COMMENT'), tok.value):
                return True
    if style.Get('I18N_FUNCTION_CALL'):
        length = len(line.tokens)
        for index in range(length - 1):
            if line.tokens[index + 1].value == '(' and line.tokens[index].value in style.Get('I18N_FUNCTION_CALL'):
                return True
    return False

Return true if there are i18n comments or function calls in the line.

I18n comments and pseudo-function calls are closely related. They cannot
be moved apart without breaking i18n.

Arguments:
  line: (logical_line.LogicalLine) The line currently being formatted.

Returns:
  True if the line contains i18n comments or function calls. False otherwise.


def _matches_section_title(title, section_title):
    title = title.lower()
    section_title = section_title.lower()
    return section_title in (title, title[:-1])

Returns whether title is a match for a specific section_title.

Example:
  _matches_section_title('Yields', 'yield') == True

Args:
  title: The title to check for matching.
  section_title: A specific known section title to check against.