def zeros(shape, dtype=dtypes.float32, name=None, layout=None):
    dtype = dtypes.as_dtype(dtype).base_dtype
    with ops.name_scope(name, 'zeros', [shape]) as name:
        if dtype == dtypes.bool:
            zero = False
        elif dtype == dtypes.string:
            zero = ''
        elif dtype.is_quantized:
            zero = np.zeros([]).astype(dtype.as_numpy_dtype)
        else:
            zero = 0
        if not isinstance(shape, tensor_lib.Tensor):
            try:
                if not context.executing_eagerly():
                    output = _constant_if_small(zero, shape, dtype, name, layout=layout)
                    if output is not None:
                        return output
                shape = constant_op._tensor_shape_tensor_conversion_function(tensor_shape.TensorShape(shape))
            except (TypeError, ValueError, errors.UnimplementedError):
                shape = ops.convert_to_tensor(shape, dtype=dtypes.int32)
        if not shape._shape_tuple():
            shape = reshape(shape, [-1])
        output = fill(shape, constant(zero, dtype=dtype), name=name, layout=layout)
    assert output.dtype.base_dtype == dtype
    return output

Creates a tensor with all elements set to zero.

See also `tf.zeros_like`, `tf.ones`, `tf.fill`, `tf.eye`.

This operation returns a tensor of type `dtype` with shape `shape` and
all elements set to zero.

>>> tf.zeros([3, 4], tf.int32)
<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int32)>

Args:
  shape: A `list` of integers, a `tuple` of integers, or a 1-D `Tensor` of
    type `int32`.
  dtype: The DType of an element in the resulting `Tensor`.
  name: Optional string. A name for the operation.
  layout: Optional, `tf.experimental.dtensor.Layout`. If provided, the result
    is a [DTensor](https://www.tensorflow.org/guide/dtensor_overview) with the
    provided layout.

Returns:
  A `Tensor` with all elements set to zero.


def where(condition, x=None, y=None, name=None):
    if x is None and y is None:
        with ops.name_scope(name, 'Where', [condition]) as name:
            condition = ops.convert_to_tensor(condition, preferred_dtype=dtypes.bool, name='condition')
            return gen_array_ops.where(condition=condition, name=name)
    elif x is not None and y is not None:
        return gen_math_ops.select(condition=condition, x=x, y=y, name=name)
    else:
        raise ValueError('x and y must both be non-None or both be None.')

Return the elements, either from `x` or `y`, depending on the `condition`.

If both `x` and `y` are None, then this operation returns the coordinates of
true elements of `condition`.  The coordinates are returned in a 2-D tensor
where the first dimension (rows) represents the number of true elements, and
the second dimension (columns) represents the coordinates of the true
elements. Keep in mind, the shape of the output tensor can vary depending on
how many true values there are in input. Indices are output in row-major
order.

If both non-None, `x` and `y` must have the same shape.
The `condition` tensor must be a scalar if `x` and `y` are scalar.
If `x` and `y` are tensors of higher rank, then `condition` must be either a
vector with size matching the first dimension of `x`, or must have the same
shape as `x`.

The `condition` tensor acts as a mask that chooses, based on the value at each
element, whether the corresponding element / row in the output should be taken
from `x` (if true) or `y` (if false).

If `condition` is a vector and `x` and `y` are higher rank matrices, then it
chooses which row (outer dimension) to copy from `x` and `y`. If `condition`
has the same shape as `x` and `y`, then it chooses which element to copy from
`x` and `y`.

Args:
  condition: A `Tensor` of type `bool`
  x: A Tensor which may have the same shape as `condition`. If `condition` is
    rank 1, `x` may have higher rank, but its first dimension must match the
    size of `condition`.
  y: A `tensor` with the same shape and type as `x`.
  name: A name of the operation (optional)

Returns:
  A `Tensor` with the same type and shape as `x`, `y` if they are non-None.
  Otherwise, a `Tensor` with shape `(num_true, rank(condition))`.

Raises:
  ValueError: When exactly one of `x` or `y` is non-None.

@compatibility(TF2)

This API is compatible with eager execution and `tf.function`. However, this
is still a legacy API endpoint originally designed for TF1. To migrate to
fully-native TF2, please replace its usage with `tf.where` instead, which is
directly backwards compatible with `tf.compat.v1.where`.

However,`tf.compat.v1.where` is more restrictive than `tf.where`, requiring
`x` and `y` to have the same shape, and returning a `Tensor` with the same
type and shape as `x`, `y` (if they are both non-None).

`tf.where` will accept `x`, `y` that are not the same shape as long as they
are broadcastable with one another and with `condition`, and will return a
`Tensor` with shape broadcast from `condition`, `x`, and `y`.

For example, the following works with `tf.where` but not `tf.compat.v1.where`:

>>> tf.where([True, False, False, True], [1,2,3,4], [100])
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([  1, 100, 100,   4],
dtype=int32)>

>>> tf.where(True, [1,2,3,4], 100)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([1, 2, 3, 4],
dtype=int32)>

@end_compatibility


def call(self, input_ids=None, position_ids=None, inputs_embeds=None, training=False):
    assert not (input_ids is None and inputs_embeds is None)
    if input_ids is not None:
        check_embeddings_within_bounds(input_ids, self.config.vocab_size)
        inputs_embeds = tf.gather(params=self.weight, indices=input_ids)
    input_shape = shape_list(inputs_embeds)[:-1]
    if position_ids is None:
        if input_ids is not None:
            position_ids = self.create_position_ids_from_input_ids(input_ids=input_ids)
        else:
            position_ids = tf.expand_dims(tf.range(start=self.padding_idx + 1, limit=input_shape[-1] + self.padding_idx + 1), axis=0)
    position_embeds = tf.gather(params=self.position_embeddings, indices=position_ids)
    final_embeddings = inputs_embeds + position_embeds
    final_embeddings = self.LayerNorm(inputs=final_embeddings)
    final_embeddings = self.dropout(inputs=final_embeddings, training=training)
    return final_embeddings

Applies embedding based on inputs tensor.

Returns:
    final_embeddings (`tf.Tensor`): output embedding tensor.


def FromHttpToTimestamp(self, http_ts_string):
    t = time.strptime(http_ts_string, '%a, %d %b %Y %H:%M:%S GMT')
    return int(calendar.timegm(t))

Converts HTTP timestamp string to internal nss_cache timestamp.

Args:
  HTTP format timestamp string
Returns:
  number of seconds since epoch


def write_to_text(pcoll, path: str):
    try:
        field_names = [name for name, _ in schemas.named_fields_from_element_type(pcoll.element_type)]
    except Exception as exn:
        raise ValueError('WriteToText requires an input schema with exactly one field.') from exn
    if len(field_names) != 1:
        raise ValueError('WriteToText requires an input schema with exactly one field, got %s' % field_names)
    sole_field_name, = field_names
    return pcoll | beam.Map(lambda x: str(getattr(x, sole_field_name))) | beam.io.WriteToText(path)

Writes a PCollection to a (set of) text files(s).

The input must be a PCollection whose schema has exactly one field.

Args:
    path (str): The file path to write to. The files written will
      begin with this prefix, followed by a shard identifier.


def post_process_semantic_segmentation(self, outputs, target_sizes: Optional[List[Tuple[int, int]]]=None) -> 'torch.Tensor':
    class_queries_logits = outputs.class_queries_logits
    masks_queries_logits = outputs.masks_queries_logits
    masks_classes = class_queries_logits.softmax(dim=-1)[..., :-1]
    masks_probs = masks_queries_logits.sigmoid()
    segmentation = torch.einsum('bqc, bqhw -> bchw', masks_classes, masks_probs)
    batch_size = class_queries_logits.shape[0]
    if target_sizes is not None:
        if batch_size != len(target_sizes):
            raise ValueError('Make sure that you pass in as many target sizes as the batch dimension of the logits')
        semantic_segmentation = []
        for idx in range(batch_size):
            resized_logits = torch.nn.functional.interpolate(segmentation[idx].unsqueeze(dim=0), size=target_sizes[idx], mode='bilinear', align_corners=False)
            semantic_map = resized_logits[0].argmax(dim=0)
            semantic_segmentation.append(semantic_map)
    else:
        semantic_segmentation = segmentation.argmax(dim=1)
        semantic_segmentation = [semantic_segmentation[i] for i in range(semantic_segmentation.shape[0])]
    return semantic_segmentation

Converts the output of [`MaskFormerForInstanceSegmentation`] into semantic segmentation maps. Only supports
PyTorch.

Args:
    outputs ([`MaskFormerForInstanceSegmentation`]):
        Raw outputs of the model.
    target_sizes (`List[Tuple[int, int]]`, *optional*):
        List of length (batch_size), where each list item (`Tuple[int, int]]`) corresponds to the requested
        final size (height, width) of each prediction. If left to None, predictions will not be resized.
Returns:
    `List[torch.Tensor]`:
        A list of length `batch_size`, where each item is a semantic segmentation map of shape (height, width)
        corresponding to the target_sizes entry (if `target_sizes` is specified). Each entry of each
        `torch.Tensor` correspond to a semantic class id.


def tokenize_sentence(input_dict):
    text, uid = (input_dict['text'], input_dict['id'])
    tokens = Tokenizer([text], padding=True, truncation=True, return_tensors='pt')
    tokens = {key: torch.squeeze(val) for key, val in tokens.items()}
    return ((text, uid), tokens)

Takes a dictionary with a text and an id, tokenizes the text, and
returns a tuple of the text and id and the tokenized text

Args:
  input_dict: a dictionary with the text and id of the sentence

Returns:
  A tuple of the text and id, and a dictionary of the tokens.


class Qwen2AudioCausalLMOutputWithPast(ModelOutput):
    loss: Optional[torch.FloatTensor] = None
    logits: Optional[torch.FloatTensor] = None
    past_key_values: Optional[Cache] = None
    hidden_states: Optional[Tuple[torch.FloatTensor]] = None
    attentions: Optional[Tuple[torch.FloatTensor]] = None
    attention_mask: Optional[torch.FloatTensor] = None

Base class for Qwen2Audio causal language model (or autoregressive) outputs.

Args:
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Language modeling loss (for next-token prediction).
    logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
    past_key_values (`Cache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
        Pre-computed hidden-states that can be used to speed up auto-regressive (sequential) decoding. There are
        two sets of pre-computed hidden-states: key and values states in the self-attention blocks.
        The `past_key_values` are returned when `use_cache=True` is passed or when `config.use_cache=True`.
        It is a [`~cache_utils.Cache`] instance.

        If `past_key_values` are used, the user can optionally input only the last `input_ids` (those
        that don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of
        all `input_ids` of shape `(batch_size, sequence_length)`.
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
        one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

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

        Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
        heads.
    attention_mask (`torch.FloatTensor`, *optional*):
        Attentions mask, used to update attention mask and position_ids.


class Undefined(object):
    __slots__ = ('symbol_name',)

    def __init__(self, symbol_name):
        self.symbol_name = symbol_name

    def read(self):
        raise UnboundLocalError("'{}' is used before assignment".format(self.symbol_name))

    def __repr__(self):
        return self.symbol_name

    def __getattribute__(self, name):
        try:
            return object.__getattribute__(self, name)
        except AttributeError:
            return self

    def __getitem__(self, i):
        return self

Represents an undefined symbol in Python.

This is used to reify undefined symbols, which is required to use the
functional form of loops.
Example:

  while n > 0:
    n = n - 1
    s = n
  return s  # Runtime error if n == 0

This is valid Python code and will not result in an error as long as n
is positive. The use of this class is to stay as close to Python semantics
as possible for staged code of this nature.

Converted version of the above showing the possible usage of this class:

  s = Undefined('s')
  init_state = (s,)
  s = while_loop(cond, body, init_state)
  return s  # s is an instance of Undefined if the loop never runs

Attributes:
  symbol_name: Text, identifier for the undefined symbol


def _test_dir(temp_dir, test_name):
    test_dir = os.path.join(temp_dir, test_name)
    if os.path.isdir(test_dir):
        for f in glob.glob('%s/*' % test_dir):
            os.remove(f)
    else:
        os.makedirs(test_dir)
    return test_dir

Create an empty dir to use for tests.

Args:
  temp_dir: Tmp directory path.
  test_name: Name of the test.

Returns:
  Absolute path to the test directory.


class GroundingDinoDecoderOutput(ModelOutput):
    last_hidden_state: Optional[torch.FloatTensor] = None
    intermediate_hidden_states: Optional[torch.FloatTensor] = None
    intermediate_reference_points: Optional[torch.FloatTensor] = None
    hidden_states: Optional[Tuple[torch.FloatTensor]] = None
    attentions: Optional[Tuple[Tuple[torch.FloatTensor]]] = None

Base class for outputs of the GroundingDinoDecoder. This class adds two attributes to
BaseModelOutputWithCrossAttentions, namely:
- a stacked tensor of intermediate decoder hidden states (i.e. the output of each decoder layer)
- a stacked tensor of intermediate reference points.

Args:
    last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
        Sequence of hidden-states at the output of the last layer of the model.
    intermediate_hidden_states (`torch.FloatTensor` of shape `(batch_size, config.decoder_layers, num_queries, hidden_size)`):
        Stacked intermediate hidden states (output of each layer of the decoder).
    intermediate_reference_points (`torch.FloatTensor` of shape `(batch_size, config.decoder_layers, sequence_length, hidden_size)`):
        Stacked intermediate reference points (reference points of each layer of the decoder).
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings + 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(tuple(torch.FloatTensor))`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of tuples of `torch.FloatTensor` (one for attention 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, cross-attention and multi-scale deformable attention heads.


def get_registered_kernels_for_op(name):
    buf = c_api.TF_GetRegisteredKernelsForOp(name)
    data = c_api.TF_GetBuffer(buf)
    kernel_list = kernel_def_pb2.KernelList()
    kernel_list.ParseFromString(compat.as_bytes(data))
    return kernel_list

Returns a KernelList proto of registered kernels for a given op.

Args:
  name: A string representing the name of the op whose kernels to retrieve.


def forward(self, hidden_states: torch.Tensor, attention_mask: Optional[torch.Tensor]=None, encoder_hidden_states: Optional[torch.Tensor]=None, encoder_attention_mask: Optional[torch.Tensor]=None, layer_head_mask: Optional[torch.Tensor]=None, cross_attn_layer_head_mask: Optional[torch.Tensor]=None, past_key_value: Optional[Cache]=None, output_attentions: Optional[bool]=False, use_cache: Optional[bool]=True, cache_position: Optional[torch.Tensor]=None) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
    residual = hidden_states
    hidden_states, self_attn_weights, past_key_value = self.self_attn(hidden_states=hidden_states, past_key_value=past_key_value, attention_mask=attention_mask, layer_head_mask=layer_head_mask, output_attentions=output_attentions, cache_position=cache_position)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    cross_attn_weights = None
    if encoder_hidden_states is not None:
        residual = hidden_states
        hidden_states, cross_attn_weights, past_key_value = self.encoder_attn(hidden_states=hidden_states, key_value_states=encoder_hidden_states, attention_mask=encoder_attention_mask, layer_head_mask=cross_attn_layer_head_mask, past_key_value=past_key_value, output_attentions=output_attentions, cache_position=cache_position)
        hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
        hidden_states = residual + hidden_states
        hidden_states = self.encoder_attn_layer_norm(hidden_states)
    residual = hidden_states
    hidden_states = self.activation_fn(self.fc1(hidden_states))
    hidden_states = nn.functional.dropout(hidden_states, p=self.activation_dropout, training=self.training)
    hidden_states = self.fc2(hidden_states)
    hidden_states = nn.functional.dropout(hidden_states, p=self.dropout, training=self.training)
    hidden_states = residual + hidden_states
    hidden_states = self.final_layer_norm(hidden_states)
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (self_attn_weights, cross_attn_weights)
    if use_cache:
        outputs += (past_key_value,)
    return outputs

Args:
    hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
    attention_mask (`torch.FloatTensor`): attention mask of size
        `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
    encoder_hidden_states (`torch.FloatTensor`):
        cross attention input to the layer of shape `(batch, seq_len, embed_dim)`
    encoder_attention_mask (`torch.FloatTensor`): encoder attention mask of size
        `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
    layer_head_mask (`torch.FloatTensor`): mask for attention heads in a given layer of size
        `(encoder_attention_heads,)`.
    cross_attn_layer_head_mask (`torch.FloatTensor`): mask for cross-attention heads in a given layer of
        size `(decoder_attention_heads,)`.
    past_key_value (`Tuple(torch.FloatTensor)`): cached past key and value projection states
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers. See `attentions` under
        returned tensors for more detail.
    cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
        Indices depicting the position of the input sequence tokens in the sequence. It is used to update the
        cache in the correct position and to infer the complete sequence length.


class EventFromExternal(Event):

    def __init__(self, name, external):
        Event.__init__(self, name)
        self.external_name = external
        module, event = external.rsplit('.', 1)
        self.external_event = importlib.import_module(module).__getattribute__(event)()
        assert isinstance(self.external_event, stl.lib.Event)

    def __eq__(self, other):
        return Event.__eq__(self, other) and self.external_name == other.external_name

    def __str__(self):
        return '%s b(%s)' % (Event.__str__(self), self.external_name)

    def Resolve(self, env, resolved_params):
        logging.log(1, 'Resolving ' + self.name)
        return stl.base.FuncWithContext(self.external_name, self.external_event)

An external event (i.e. defined and provided in a python file).

event mEventExample(int param1) = external "foo.bar.EventFunction";

There should be a python module foo.bar with a top-level function
EventFunction. The actual event signature should be the same as
the one specified in the stl file, except it should expect to
take a Context as it's first argument, in addition to the others.

In the example above, EventFunction should look like:
  def EventFunction(context, param1):
    ...

Attributes:
  name: The STL event name (e.g. mEventExample)
  external_name: name of the external funciton, including modules
      (e.g. "foo.bar.EventFunction")
  external_event: The actual, callable event function (e.g. EventFunction)


def get_new_doctest_files(repo, base_commit, branching_commit) -> List[str]:
    for diff_obj in branching_commit.diff(base_commit):
        if diff_obj.a_path != 'utils/not_doctested.txt':
            continue
        folder = Path(repo.working_dir)
        with checkout_commit(repo, branching_commit):
            with open(folder / 'utils/not_doctested.txt', 'r', encoding='utf-8') as f:
                old_content = f.read()
        with open(folder / 'utils/not_doctested.txt', 'r', encoding='utf-8') as f:
            new_content = f.read()
        removed_content = {x.split(' ')[0] for x in old_content.split('\n')} - {x.split(' ')[0] for x in new_content.split('\n')}
        return sorted(removed_content)
    return []

Get the list of files that were removed from "utils/not_doctested.txt", between `base_commit` and
`branching_commit`.

Returns:
    `List[str]`: List of files that were removed from "utils/not_doctested.txt".


def update(self, media_blob: genai_types.Blob):
    if self.generation_start_sec is not None and self.ttft_sec is None:
        self.time_audio_start = time.perf_counter()
        self.ttft_sec = self.time_audio_start - self.generation_start_sec
    self.audio_duration += audio_duration_sec(media_blob.data, RECEIVE_SAMPLE_RATE)

Updates the generation request with the new media data.

Args:
  media_blob: The new media data.


def node_name(self):
    return self._node_name

Name of the node from which the tensor value was dumped.

Returns:
  (`str`) name of the node watched by the debug op.


def check_graphs(*args):
    graph = None
    for i, sgv in enumerate(args):
        if graph is None and sgv.graph is not None:
            graph = sgv.graph
        elif sgv.graph is not None and sgv.graph is not graph:
            raise ValueError(f'args[{i}] does not belong to the same graph as other arguments.')

Check that all the element in args belong to the same graph.

Args:
  *args: a list of object with a obj.graph property.
Raises:
  ValueError: if all the elements do not belong to the same graph.


def Create(conf):
    global _source_implementations
    if not _source_implementations:
        raise RuntimeError('no source implementations exist')
    source_name = conf['name']
    if source_name not in list(_source_implementations.keys()):
        raise RuntimeError('source not implemented: %r' % (source_name,))
    return _source_implementations[source_name](conf)

Source creation factory method.

Args:
 conf: a dictionary of configuration key/value pairs, including one
         required attribute 'name'.

Returns:
  A Source instance.

Raises:
  RuntimeError: no sources are registered with RegisterImplementation


def range_dimension_tensor(self, name='range_dimension_tensor'):
    with self._name_scope(name):
        return self._range_dimension_tensor()

Dimension (in the sense of vector spaces) of the range of this operator.

Determined at runtime.

If this operator acts like the batch matrix `A` with
`A.shape = [B1,...,Bb, M, N]`, then this returns `M`.

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

Returns:
  `int32` `Tensor`


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

Update an association between a GCP project and a GitHub Enterprise server.

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


def forward(self, hidden_states: torch.Tensor, position_embeddings: Tuple[torch.Tensor, torch.Tensor], attention_mask: Optional[torch.Tensor]=None, position_ids: Optional[torch.LongTensor]=None, past_key_value: Optional[Tuple[torch.Tensor]]=None, output_attentions: Optional[bool]=False, output_router_logits: Optional[bool]=False, use_cache: Optional[bool]=False, cache_position: Optional[torch.LongTensor]=None, **kwargs: Unpack[FlashAttentionKwargs]) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
    hidden_states = self.input_layernorm(hidden_states)
    residual = hidden_states
    hidden_states, self_attn_weights = self.self_attn(hidden_states=hidden_states, position_embeddings=position_embeddings, attention_mask=attention_mask, position_ids=position_ids, past_key_value=past_key_value, output_attentions=output_attentions, use_cache=use_cache, cache_position=cache_position, **kwargs)
    hidden_states = residual * self.attn_alpha_factor + hidden_states * self.attn_beta_factor
    hidden_states = self.post_attention_layernorm(hidden_states)
    residual = hidden_states
    hidden_states, router_logits = self.block_sparse_moe(hidden_states)
    hidden_states = residual * self.mlp_alpha_factor + hidden_states * self.mlp_beta_factor
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (self_attn_weights,)
    if output_router_logits:
        outputs += (router_logits,)
    return outputs

Args:
    hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
    position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`):
        Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`,
        with `head_dim` being the embedding dimension of each attention head.
    attention_mask (`torch.Tensor`, *optional*): attention mask of size
        `(batch, sequence_length)` where padding elements are indicated by 0.
    past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
    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_router_logits (`bool`, *optional*):
        Whether or not to return the logits of all the routers. They are useful for computing the router loss, and
        should not be returned during inference.
    use_cache (`bool`, *optional*):
        If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
        (see `past_key_values`).
    cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
        Indices depicting the position of the input sequence tokens in the sequence.
    kwargs (`dict`, *optional*):
        Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code
        into the model


class CSVLogger(Callback):

    def __init__(self, filename, separator=',', append=False):
        super().__init__()
        self.sep = separator
        self.filename = file_utils.path_to_string(filename)
        self.append = append
        self.writer = None
        self.keys = None
        self.append_header = True
        self.csv_file = None

    def on_train_begin(self, logs=None):
        if self.append:
            if file_utils.exists(self.filename):
                with file_utils.File(self.filename, 'r') as f:
                    self.append_header = not bool(len(f.readline()))
            mode = 'a'
        else:
            mode = 'w'
        if self.csv_file and (not self.csv_file.closed):
            self.csv_file.close()
        self.csv_file = file_utils.File(self.filename, mode)
        self.writer = None
        self.keys = None

    def on_epoch_end(self, epoch, logs=None):
        logs = logs or {}

        def handle_value(k):
            is_zero_dim_ndarray = isinstance(k, np.ndarray) and k.ndim == 0
            if isinstance(k, str):
                return k
            elif isinstance(k, collections.abc.Iterable) and (not is_zero_dim_ndarray):
                return f'"[{', '.join(map(str, k))}]"'
            else:
                return k
        if self.keys is None:
            self.keys = sorted(logs.keys())
            val_keys_found = False
            for key in self.keys:
                if key.startswith('val_'):
                    val_keys_found = True
                    break
            if not val_keys_found and self.keys:
                self.keys.extend(['val_' + k for k in self.keys])
        if not self.writer:

            class CustomDialect(csv.excel):
                delimiter = self.sep
            fieldnames = ['epoch'] + (self.keys or [])
            self.writer = csv.DictWriter(self.csv_file, fieldnames=fieldnames, dialect=CustomDialect)
            if self.append_header:
                self.writer.writeheader()
        row_dict = collections.OrderedDict({'epoch': epoch})
        row_dict.update(((key, handle_value(logs.get(key, 'NA'))) for key in self.keys))
        self.writer.writerow(row_dict)
        self.csv_file.flush()

    def on_train_end(self, logs=None):
        if self.csv_file and (not self.csv_file.closed):
            self.csv_file.close()
        self.writer = None

Callback that streams epoch results to a CSV file.

Supports all values that can be represented as a string,
including 1D iterables such as `np.ndarray`.

Args:
    filename: Filename of the CSV file, e.g. `'run/log.csv'`.
    separator: String used to separate elements in the CSV file.
    append: Boolean. True: append if file exists (useful for continuing
        training). False: overwrite existing file.

Example:

```python
csv_logger = CSVLogger('training.log')
model.fit(X_train, Y_train, callbacks=[csv_logger])
```


class PerceiverOneHotPreprocessor(AbstractPreprocessor):

    def __init__(self, config: PerceiverConfig) -> None:
        super().__init__()
        self.config: PerceiverConfig = config

    @property
    def num_channels(self) -> int:
        return self.config.num_labels

    def forward(self, inputs: torch.Tensor, pos: Optional[torch.Tensor]=None, network_input_is_1d: bool=True):
        inputs = inputs[:, None, :]
        return (inputs, None, inputs)

One-hot preprocessor for Perceiver Encoder. Can be used to add a dummy index dimension to the input.

Args:
    config ([`PerceiverConfig`]):
        Model configuration.


def _get_feed(self):
    if self.feed_name in self._feed_name_tab_map:
        for tab_name in self._feed_name_tab_map[self.feed_name]:
            for sheet in self.spreadsheet['sheets']:
                if sheet['properties']['title'] == tab_name:
                    self.tab_name = tab_name
                    return sheets_read(self.config, self.auth, self.trix_id, tab_name, self.trix_range)
    return [[]]

Fetches the feed based on initialization parameters.

Returns:
  List of lists that represents the rows and columns of the feed. If the
  feed isn't found returns a list with an empty list.


def _matrix_conv(self, m1, m2):
    n = m1[0, 0, 0].shape.as_list()[0]
    if n != m2[0, 0, 0].shape.as_list()[0]:
        raise ValueError(f'The entries in matrices m1 and m2 must have the same dimensions. Received m1[0, 0, 0].shape={m1[0, 0, 0].shape} and m2[0, 0, 0].shape={m2[0, 0, 0].shape}.')
    k = int(np.cbrt(len(m1)))
    l = int(np.cbrt(len(m2)))
    result = {}
    size = k + l - 1
    for i in range(size):
        for j in range(size):
            for r in range(size):
                result[i, j, r] = array_ops.zeros([n, n], self.dtype)
                for index1 in range(min(k, i + 1)):
                    for index2 in range(min(k, j + 1)):
                        for index3 in range(min(k, r + 1)):
                            if i - index1 < l and j - index2 < l and (r - index3 < l):
                                result[i, j, r] += math_ops.matmul(m1[index1, index2, index3], m2[i - index1, j - index2, r - index3])
    return result

Matrix convolution.

Args:
  m1: is a k x k x k  dictionary, each element is a n x n matrix.
  m2: is a l x l x l dictionary, each element is a n x n matrix.

Returns:
  (k + l - 1) x (k + l - 1) x (k + l - 1) dictionary each
  element is a n x n matrix.
Raises:
  ValueError: if the entries of m1 and m2 are of different dimensions.


def pad_to_square(self, image: np.ndarray, background_color: Union[int, Tuple[int, int, int]]=0, data_format: Optional[Union[str, ChannelDimension]]=None, input_data_format: Optional[Union[str, ChannelDimension]]=None) -> np.array:
    height, width = get_image_size(image, input_data_format)
    num_channels = image.shape[0] if input_data_format == ChannelDimension.FIRST else image.shape[-1]
    if height == width:
        image = to_channel_dimension_format(image, data_format, input_data_format) if data_format is not None else image
        return image
    max_dim = max(height, width)
    if isinstance(background_color, int):
        background_color = [background_color]
    elif len(background_color) != num_channels:
        raise ValueError(f'background_color must have no more than {num_channels} elements to match the number of channels')
    if input_data_format == ChannelDimension.FIRST:
        result = np.zeros((num_channels, max_dim, max_dim), dtype=image.dtype)
        for i, color in enumerate(background_color):
            result[i, :, :] = color
        if width > height:
            start = (max_dim - height) // 2
            result[:, start:start + height, :] = image
        else:
            start = (max_dim - width) // 2
            result[:, :, start:start + width] = image
    else:
        result = np.zeros((max_dim, max_dim, num_channels), dtype=image.dtype)
        for i, color in enumerate(background_color):
            result[:, :, i] = color
        if width > height:
            start = (max_dim - height) // 2
            result[start:start + height, :, :] = image
        else:
            start = (max_dim - width) // 2
            result[:, start:start + width, :] = image
    image = to_channel_dimension_format(result, data_format, input_data_format) if data_format is not None else result
    return image

Pads an image to a square based on the longest edge.

Args:
    image (`np.ndarray`):
        The image to pad.
    background_color (`int` or `Tuple[int, int, int]`, *optional*, defaults to 0):
        The color to use for the padding. Can be an integer for single channel or a
        tuple of integers representing for multi-channel images. If passed as integer
        in mutli-channel mode, it will default to `0` in subsequent channels.
    data_format (`str` or `ChannelDimension`, *optional*):
        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.
        If unset, will use same as the input image.
    input_data_format (`str` or `ChannelDimension`, *optional*):
        The channel dimension format for 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.
        If unset, will use the inferred format of the input image.

Returns:
    `np.ndarray`: The padded image.


def _assert_same_base_type(items, expected_type=None):
    original_expected_type = expected_type
    mismatch = False
    for item in items:
        if item is not None:
            item_type = item.dtype.base_dtype
            if not expected_type:
                expected_type = item_type
            elif expected_type != item_type:
                mismatch = True
                break
    if mismatch:
        expected_type = original_expected_type
        original_item_str = None
        for item in items:
            if item is not None:
                item_type = item.dtype.base_dtype
                if not expected_type:
                    expected_type = item_type
                    original_item_str = item.name if hasattr(item, 'name') else str(item)
                elif expected_type != item_type:
                    raise ValueError('%s, type=%s, must be of the same type (%s)%s.' % (item.name if hasattr(item, 'name') else str(item), item_type, expected_type, ' as %s' % original_item_str if original_item_str else ''))
        return expected_type
    else:
        return expected_type

Asserts all items are of the same base type.

Args:
  items: List of graph items (e.g., `Variable`, `Tensor`, `SparseTensor`,
      `Operation`, or `IndexedSlices`). Can include `None` elements, which
      will be ignored.
  expected_type: Expected type. If not specified, assert all items are
      of the same base type.

Returns:
  Validated type, or none if neither expected_type nor items provided.

Raises:
  ValueError: If any types do not match.


class Zeros(Initializer):

    def __call__(self, shape, dtype=None, **kwargs):
        """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, `tf.keras.backend.floatx()` is used,
       which default to `float32` unless you configured it otherwise
       (via `tf.keras.backend.set_floatx(float_dtype)`).
      **kwargs: Additional keyword arguments.
    """
        _validate_kwargs(self.__class__.__name__, kwargs)
        dtype = _get_dtype(dtype)
        if not dtype.is_numpy_compatible or dtype == dtypes.string:
            raise ValueError('Expected numeric or boolean dtype, got %s.' % dtype)
        if _PARTITION_SHAPE in kwargs:
            shape = kwargs[_PARTITION_SHAPE]
        return array_ops.zeros(shape, dtype)

Initializer that generates tensors initialized to 0.

Also available via the shortcut function `tf.keras.initializers.zeros`.

Examples:

>>> # Standalone usage:
>>> initializer = tf.keras.initializers.Zeros()
>>> values = initializer(shape=(2, 2))

>>> # Usage in a Keras layer:
>>> initializer = tf.keras.initializers.Zeros()
>>> layer = tf.keras.layers.Dense(3, kernel_initializer=initializer)


def saved_model_format_scope(value, **kwargs):
    previous_format = _thread_local_data.saved_model_format
    previous_kwargs = _thread_local_data.save_kwargs
    try:
        _thread_local_data.saved_model_format = value
        _thread_local_data.save_kwargs = kwargs
        yield
    finally:
        _thread_local_data.saved_model_format = previous_format
        _thread_local_data.save_kwargs = previous_kwargs

Provides a scope within which the savde model format to test is `value`.

The saved model format gets restored to its original value upon exiting the
scope.

Args:
   value: saved model format value
   **kwargs: optional kwargs to pass to the save function.

Yields:
  The provided value.


def add_request(self, input_ids: List[int], request_id: Optional[str]=None, max_new_tokens: Optional[int]=None) -> str:
    if request_id is None:
        with self._request_lock:
            request_id = f'req_{self._request_counter}'
            self._request_counter += 1
    max_new_tokens = self.generation_config.max_new_tokens if max_new_tokens is None else max_new_tokens
    state = RequestState(request_id=request_id, prompt_ids=list(input_ids), full_prompt_ids=list(input_ids), max_new_tokens=max_new_tokens, eos_token_id=self.generation_config.eos_token_id)
    self.input_queue.put(state, block=True, timeout=10)
    logger.debug(f'Added request {request_id} to queue.')
    return request_id

Add a new generation request to the queue.

Args:
    input_ids: Input token IDs to use as prompt
    request_id: Optional custom request ID (auto-generated if None)
    **kwargs: Additional generation parameters

Returns:
    str: The request ID


def _BroadcastMul(vec, mat):
    vec = array_ops.expand_dims(vec, -1)
    return vec * mat

Multiply after broadcasting vec to match dimensions of mat.

Args:
  vec: A 1-D tensor of dimension [D0]
  mat: A 2-D tensor of dimension [D0, D1]

Returns:
  A tensor of dimension [D0, D1], the result of vec * mat


class SpatialDropout1D(Dropout):

    def __init__(self, rate, **kwargs):
        super(SpatialDropout1D, self).__init__(rate, **kwargs)
        self.input_spec = InputSpec(ndim=3)

    def _get_noise_shape(self, inputs):
        input_shape = array_ops.shape(inputs)
        noise_shape = (input_shape[0], 1, input_shape[2])
        return noise_shape

Spatial 1D version of Dropout.

This version performs the same function as Dropout, however, it drops
entire 1D feature maps instead of individual elements. If adjacent frames
within feature maps are strongly correlated (as is normally the case in
early convolution layers) then regular dropout will not regularize the
activations and will otherwise just result in an effective learning rate
decrease. In this case, SpatialDropout1D will help promote independence
between feature maps and should be used instead.

Args:
  rate: Float between 0 and 1. Fraction of the input units to drop.

Call arguments:
  inputs: A 3D tensor.
  training: Python boolean indicating whether the layer should behave in
    training mode (adding dropout) or in inference mode (doing nothing).

Input shape:
  3D tensor with shape:
  `(samples, timesteps, channels)`

Output shape:
  Same as input.

References:
  - [Efficient Object Localization Using Convolutional
    Networks](https://arxiv.org/abs/1411.4280)


class MaskFormerPixelLevelModuleOutput(ModelOutput):
    encoder_last_hidden_state: Optional[torch.FloatTensor] = None
    decoder_last_hidden_state: Optional[torch.FloatTensor] = None
    encoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None
    decoder_hidden_states: Optional[Tuple[torch.FloatTensor]] = None

MaskFormer's pixel level module output. It returns both the last and (optionally) the hidden states from the
`encoder` and `decoder`. By default, the `encoder` is a MaskFormerSwin Transformer and the `decoder` is a Feature
Pyramid Network (FPN).

The `encoder_last_hidden_state` are referred on the paper as **images features**, while `decoder_last_hidden_state`
as **pixel embeddings**

Args:
    encoder_last_hidden_state (`torch.FloatTensor` of shape`(batch_size, num_channels, height, width)`):
        Last hidden states (final feature map) of the last stage of the encoder.
    encoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
        shape `(batch_size, num_channels, height, width)`. Hidden-states (also called feature maps) of the model at
        the output of each stage.
    decoder_last_hidden_state (`torch.FloatTensor` of shape`(batch_size, num_channels, height, width)`):
        Last hidden states (final feature map) of the last stage of the decoder.
    decoder_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
        shape `(batch_size, num_channels, height, width)`. Hidden-states (also called feature maps) of the model at
        the output of each stage.


def trace(self, name='trace'):
    with self._name_scope(name):
        return self._trace()

Trace of the linear operator, equal to sum of `self.diag_part()`.

If the operator is square, this is also the sum of the eigenvalues.

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

Returns:
  Shape `[B1,...,Bb]` `Tensor` of same `dtype` as `self`.


class InstructBlipForConditionalGenerationModelOutput(ModelOutput):
    loss: Optional[Tuple[torch.FloatTensor]] = None
    logits: Optional[Tuple[torch.FloatTensor]] = None
    vision_outputs: Optional[torch.FloatTensor] = None
    qformer_outputs: Optional[Tuple[torch.FloatTensor]] = None
    language_model_outputs: Optional[Tuple[torch.FloatTensor]] = None

    def to_tuple(self) -> Tuple[Any]:
        return tuple((self[k] if k not in ['vision_outputs', 'qformer_outputs', 'language_model_outputs'] else getattr(self, k).to_tuple() for k in self.keys()))

Class defining the outputs of [`InstructBlipForConditionalGeneration`].

Args:
    loss (`torch.FloatTensor`, *optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        Language modeling loss from the language model.
    logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the language modeling head of the language model.
    vision_outputs (`BaseModelOutputWithPooling`):
        Outputs of the vision encoder.
    qformer_outputs (`BaseModelOutputWithPoolingAndCrossAttentions`):
        Outputs of the Q-Former (Querying Transformer).
    language_model_outputs (`CausalLMOutputWithPast` or `Seq2SeqLMOutput`):
        Outputs of the language model.


def __init__(self, topic):
    super().__init__()
    self.topic = topic

Initializes ``_WriteStringsToPubSub``.

Attributes:
  topic: Cloud Pub/Sub topic in the form "/topics/<project>/<topic>".


def forward(self, vision_features, class_features, task_features, task_mask, output_attentions=None, output_hidden_states=None, return_dict=None):
    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
    vision_features, vision_shapes, vision_shapes_list, level_start_index = self._get_encoder_input(vision_features)
    denoise_embeddings, denoise_bboxes, key_padding_mask = (None, None, None)
    batch_size = task_mask.shape[0]
    task_features = self.task_encoder(task_features)
    if self.task_project is not None:
        task_features = self.task_project(task_features)
    src_key_mask = (task_mask == 0).detach()
    attn_mask_len = self.num_queries
    fusion_size = attn_mask_len + task_features.shape[0]
    key_padding_mask = torch.zeros([batch_size, fusion_size], dtype=torch.bool).to(task_features.device)
    key_padding_mask[:, attn_mask_len:] = src_key_mask
    attention_mask = _prepare_4d_attention_mask(~key_padding_mask, dtype=vision_features.dtype)
    decoder_embeddings, reference_points, encoder_bboxes, encoder_class_similarity, init_reference_points = self._get_decoder_input(vision_features, tuple(vision_shapes_list), class_features, denoise_embeddings, denoise_bboxes)
    all_hidden_states = () if output_hidden_states else None
    all_attns = () if output_attentions else None
    all_self_attns = () if output_attentions else None
    all_cross_attns = () if output_attentions else None
    predicted_class_features = decoder_embeddings
    if output_hidden_states:
        all_hidden_states = all_hidden_states + (predicted_class_features,)
    decoder_bboxes = []
    decoder_classes = []
    last_refined_bbox = None
    reference_points = reference_points.sigmoid()
    for i, layer in enumerate(self.layers):
        if self.gradient_checkpointing and self.training:
            predicted_class_features, task_features, self_attention, cross_attention = self._gradient_checkpointing_func(layer.__call__, predicted_class_features, task_features, reference_points, vision_features, vision_shapes, vision_shapes_list, level_start_index=level_start_index, attention_mask=attention_mask, query_position=self.query_position_head(reference_points), output_attentions=output_attentions, output_hidden_states=output_hidden_states)
        else:
            predicted_class_features, task_features, self_attention, cross_attention = layer(predicted_class_features, task_features, reference_points, vision_features, vision_shapes, vision_shapes_list, level_start_index=level_start_index, attention_mask=attention_mask, query_position=self.query_position_head(reference_points), output_attentions=output_attentions, output_hidden_states=output_hidden_states)
        if output_attentions:
            all_self_attns = all_self_attns + (self_attention,)
            all_cross_attns = all_cross_attns + (cross_attention,)
        if output_hidden_states:
            all_hidden_states = all_hidden_states + (predicted_class_features,)
        refined_bbox = torch.sigmoid(self.decoder_bbox_head[i](predicted_class_features) + _inverse_sigmoid(reference_points))
        original_class_projected = self.decoder_class_head[i](class_features).permute(1, 2, 0)
        if self.training:
            decoder_classes.append(get_class_similarity(class_distance_type=self.class_distance_type, cls_feature=predicted_class_features, class_proj=original_class_projected))
            if i == 0:
                decoder_bboxes.append(refined_bbox)
            else:
                decoder_bboxes.append(torch.sigmoid(self.decoder_bbox_head[i](predicted_class_features) + _inverse_sigmoid(last_refined_bbox)))
        elif i == self.decoder_num_layers - 1:
            decoder_classes.append(get_class_similarity(self.class_distance_type, predicted_class_features, original_class_projected))
            decoder_bboxes.append(refined_bbox)
            break
        last_refined_bbox = refined_bbox
        reference_points = refined_bbox.detach() if self.training else refined_bbox
    if output_attentions:
        all_attns += (all_self_attns, all_cross_attns)
    last_hidden_state = predicted_class_features
    decoder_bboxes = torch.stack(decoder_bboxes)
    decoder_classes = torch.stack(decoder_classes)
    if not return_dict:
        return (last_hidden_state, all_hidden_states, all_attns, decoder_bboxes, decoder_classes, encoder_bboxes, encoder_class_similarity, init_reference_points, reference_points)
    return OmDetTurboDecoderOutput(last_hidden_state=last_hidden_state, hidden_states=all_hidden_states, attentions=all_attns, decoder_coords=decoder_bboxes, decoder_classes=decoder_classes, encoder_coord_logits=encoder_bboxes, encoder_class_logits=encoder_class_similarity, init_reference_points=init_reference_points, intermediate_reference_points=reference_points)

Args:
    vision_features (`torch.FloatTensor`): The sequence of vision features. shape depends on the vision
        backbone.
    class_features (`torch.FloatTensor`): The sequence of class features of shape
        `(class_sequence_length, batch_size, class_embed_dim)`.
    task_features (`torch.FloatTensor`): The sequence of task features of shape
        `(task_sequence_length, batch_size, decoder_hidden_dim)`.
    task_mask (`torch.LongTensor`): The mask for the task features of shape `(batch_size, task_sequence_length)`.
    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 [`~file_utils.ModelOutput`] instead of a plain
        tuple.


def path_to_string(path):
    if isinstance(path, os.PathLike):
        return os.fspath(path)
    return path

Convert `PathLike` objects to their string representation.

If given a non-string typed path object, converts it to its string
representation.

If the object passed to `path` is not among the above, then it is
returned unchanged. This allows e.g. passthrough of file objects
through this function.

Args:
    path: `PathLike` object that represents a path

Returns:
    A string representation of the path argument, if Python support exists.


def forward(self, hidden_states: torch.FloatTensor, start_states: Optional[torch.FloatTensor]=None, start_positions: Optional[torch.LongTensor]=None, p_mask: Optional[torch.FloatTensor]=None) -> torch.FloatTensor:
    assert start_states is not None or start_positions is not None, 'One of start_states, start_positions should be not None'
    if start_positions is not None:
        slen, hsz = hidden_states.shape[-2:]
        start_positions = start_positions[:, None, None].expand(-1, -1, hsz)
        start_states = hidden_states.gather(-2, start_positions)
        start_states = start_states.expand(-1, slen, -1)
    x = self.dense_0(torch.cat([hidden_states, start_states], dim=-1))
    x = self.activation(x)
    x = self.LayerNorm(x)
    x = self.dense_1(x).squeeze(-1)
    if p_mask is not None:
        if get_parameter_dtype(self) == torch.float16:
            x = x * (1 - p_mask) - 65500 * p_mask
        else:
            x = x * (1 - p_mask) - 1e+30 * p_mask
    return x

Args:
    hidden_states (`torch.FloatTensor` of shape `(batch_size, seq_len, hidden_size)`):
        The final hidden states of the model.
    start_states (`torch.FloatTensor` of shape `(batch_size, seq_len, hidden_size)`, *optional*):
        The hidden states of the first tokens for the labeled span.
    start_positions (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
        The position of the first token for the labeled span.
    p_mask (`torch.FloatTensor` of shape `(batch_size, seq_len)`, *optional*):
        Mask for tokens at invalid position, such as query and special symbols (PAD, SEP, CLS). 1.0 means token
        should be masked.

<Tip>

One of `start_states` or `start_positions` should be not `None`. If both are set, `start_positions` overrides
`start_states`.

</Tip>

Returns:
    `torch.FloatTensor`: The end logits for SQuAD.


def HfArg(*, aliases: Optional[Union[str, list[str]]]=None, help: Optional[str]=None, default: Any=dataclasses.MISSING, default_factory: Callable[[], Any]=dataclasses.MISSING, metadata: Optional[dict]=None, **kwargs) -> dataclasses.Field:
    if metadata is None:
        metadata = {}
    if aliases is not None:
        metadata['aliases'] = aliases
    if help is not None:
        metadata['help'] = help
    return dataclasses.field(metadata=metadata, default=default, default_factory=default_factory, **kwargs)

Argument helper enabling a concise syntax to create dataclass fields for parsing with `HfArgumentParser`.

Example comparing the use of `HfArg` and `dataclasses.field`:
```
@dataclass
class Args:
    regular_arg: str = dataclasses.field(default="Huggingface", metadata={"aliases": ["--example", "-e"], "help": "This syntax could be better!"})
    hf_arg: str = HfArg(default="Huggingface", aliases=["--example", "-e"], help="What a nice syntax!")
```

Args:
    aliases (Union[str, List[str]], optional):
        Single string or list of strings of aliases to pass on to argparse, e.g. `aliases=["--example", "-e"]`.
        Defaults to None.
    help (str, optional): Help string to pass on to argparse that can be displayed with --help. Defaults to None.
    default (Any, optional):
        Default value for the argument. If not default or default_factory is specified, the argument is required.
        Defaults to dataclasses.MISSING.
    default_factory (Callable[[], Any], optional):
        The default_factory is a 0-argument function called to initialize a field's value. It is useful to provide
        default values for mutable types, e.g. lists: `default_factory=list`. Mutually exclusive with `default=`.
        Defaults to dataclasses.MISSING.
    metadata (dict, optional): Further metadata to pass on to `dataclasses.field`. Defaults to None.

Returns:
    Field: A `dataclasses.Field` with the desired properties.


def get_lagged_subsequences(self, sequence: torch.Tensor, subsequences_length: int, shift: int=0) -> torch.Tensor:
    sequence_length = sequence.shape[1]
    indices = [lag - shift for lag in self.config.lags_sequence]
    if max(indices) + subsequences_length > sequence_length:
        raise ValueError(f'lags cannot go further than history length, found lag {max(indices)} while history length is only {sequence_length}')
    lagged_values = []
    for lag_index in indices:
        begin_index = -lag_index - subsequences_length
        end_index = -lag_index if lag_index > 0 else None
        lagged_values.append(sequence[:, begin_index:end_index, ...])
    return torch.stack(lagged_values, dim=-1)

Returns lagged subsequences of a given sequence. Returns a tensor of shape (N, S, C, I),
    where S = subsequences_length and I = len(indices), containing lagged subsequences. Specifically, lagged[i,
    j, :, k] = sequence[i, -indices[k]-S+j, :].

Args:
    sequence: Tensor
        The sequence from which lagged subsequences should be extracted. Shape: (N, T, C).
    subsequences_length : int
        Length of the subsequences to be extracted.
    shift: int
        Shift the lags by this amount back.


class ForcedBOSTokenLogitsProcessor(LogitsProcessor):

    def __init__(self, bos_token_id: int):
        self.bos_token_id = bos_token_id

    @add_start_docstrings(LOGITS_PROCESSOR_INPUTS_DOCSTRING)
    def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
        cur_len = input_ids.shape[-1]
        scores_processed = scores
        if cur_len == 1:
            scores_processed = torch.full_like(scores, -math.inf)
            scores_processed[:, self.bos_token_id] = 0
        return scores_processed

[`LogitsProcessor`] that enforces the specified token as the first generated token. Used with encoder-decoder
models.

Args:
    bos_token_id (`int`):
        The id of the token to force as the first generated token.

Examples:

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

>>> model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
>>> tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")

>>> inputs = tokenizer("Translate from English to German: I love cats.", return_tensors="pt")

>>> # By default, it continues generating according to the model's logits
>>> outputs = model.generate(**inputs, max_new_tokens=10)
>>> print(tokenizer.batch_decode(outputs)[0])
<pad> Ich liebe Kitty.</s>

>>> # We can use `forced_bos_token_id` to force the start of generation with an encoder-decoder model
>>> # (including forcing it to end straight away with an EOS token)
>>> outputs = model.generate(**inputs, max_new_tokens=10, forced_bos_token_id=tokenizer.eos_token_id)
>>> print(tokenizer.batch_decode(outputs)[0])
<pad></s>
```


class ApproximateQuantilesCombineFn(CombineFn):
    _offset_jitter = 0
    _MAX_NUM_ELEMENTS = 1000000000.0
    _qs = None

    def __init__(self, num_quantiles, buffer_size, num_buffers, key=None, reverse=False, weighted=False, input_batched=False):
        self._num_quantiles = num_quantiles
        self._spec = _QuantileSpec(buffer_size, num_buffers, weighted, key, reverse)
        self._input_batched = input_batched
        if self._input_batched:
            setattr(self, 'add_input', self._add_inputs)

    def __reduce__(self):
        return (self.__class__, (self._num_quantiles, self._spec.buffer_size, self._spec.num_buffers, self._spec.key, self._spec.reverse, self._spec.weighted, self._input_batched))

    @classmethod
    def create(cls, num_quantiles, epsilon=None, max_num_elements=None, key=None, reverse=False, weighted=False, input_batched=False):
        """
    Creates an approximate quantiles combiner with the given key and desired
    number of quantiles.

    Args:
      num_quantiles: Number of quantiles to produce. It is the size of the
        final output list, including the mininum and maximum value items.
      epsilon: (optional) The default error bound is `epsilon`, which holds as
        long as the number of elements is less than `_MAX_NUM_ELEMENTS`.
        Specifically, if one considers the input as a sorted list x_1, ...,
        x_N, then the distance between each exact quantile x_c and its
        approximation x_c' is bounded by `|c - c'| < epsilon * N`. Note that
        these errors are worst-case scenarios. In practice the accuracy tends
        to be much better.
      max_num_elements: (optional) The cost (in time and space) to compute
        quantiles to a given accuracy is a function of the total number of
        elements in the data set.
      key: (optional) Key is a mapping of elements to a comparable key, similar
        to the key argument of Python's sorting methods.
      reverse: (optional) whether to order things smallest to largest, rather
        than largest to smallest.
      weighted: (optional) if set to True, the combiner produces weighted
        quantiles. The input elements are then expected to be tuples of values
        with the corresponding weight.
      input_batched: (optional) if set to True, inputs are expected to be
        batches of elements.
    """
        max_num_elements = max_num_elements or cls._MAX_NUM_ELEMENTS
        if not epsilon:
            epsilon = min(0.01, 1.0 / num_quantiles) if weighted else 1.0 / num_quantiles
        b = 2
        while (b - 2) * (1 << b - 2) < epsilon * max_num_elements:
            b = b + 1
        b = b - 1
        k = max(2, int(math.ceil(max_num_elements / float(1 << b - 1))))
        return cls(num_quantiles=num_quantiles, buffer_size=k, num_buffers=b, key=key, reverse=reverse, weighted=weighted, input_batched=input_batched)

    def _offset(self, new_weight):
        """
    If the weight is even, we must round up or down. Alternate between these
    two options to avoid a bias.
    """
        if new_weight % 2 == 1:
            return (new_weight + 1) / 2
        else:
            self._offset_jitter = 2 - self._offset_jitter
            return (new_weight + self._offset_jitter) / 2

    def create_accumulator(self):
        self._qs = _QuantileState(unbuffered_elements=[], unbuffered_weights=[], buffers=[], spec=self._spec)
        return self._qs

    def add_input(self, quantile_state, element):
        """
    Add a new element to the collection being summarized by quantile state.
    """
        quantile_state.add_unbuffered([element], self._offset)
        return quantile_state

    def _add_inputs(self, quantile_state, elements):
        """
    Add a batch of elements to the collection being summarized by quantile
    state.
    """
        if len(elements) == 0:
            return quantile_state
        quantile_state.add_unbuffered(elements, self._offset)
        return quantile_state

    def merge_accumulators(self, accumulators):
        """Merges all the accumulators (quantile state) as one."""
        qs = self.create_accumulator()
        for accumulator in accumulators:
            if accumulator.is_empty():
                continue
            if self._spec.weighted:
                qs.add_unbuffered([accumulator.unbuffered_elements, accumulator.unbuffered_weights], self._offset)
            else:
                qs.add_unbuffered(accumulator.unbuffered_elements, self._offset)
            qs.buffers.extend(accumulator.buffers)
        heapq.heapify(qs.buffers)
        qs.collapse_if_needed(self._offset)
        return qs

    def extract_output(self, accumulator):
        """
    Outputs num_quantiles elements consisting of the minimum, maximum and
    num_quantiles - 2 evenly spaced intermediate elements. Returns the empty
    list if no elements have been added.
    """
        if accumulator.is_empty():
            return []
        accumulator.finalize()
        all_elems = accumulator.buffers
        total_weight = 0
        if self._spec.weighted:
            for buffer_elem in all_elems:
                total_weight += sum(buffer_elem.weights)
        else:
            for buffer_elem in all_elems:
                total_weight += len(buffer_elem.elements) * buffer_elem.weights[0]
        step = total_weight / (self._num_quantiles - 1)
        offset = (total_weight - 1) / (self._num_quantiles - 1)
        quantiles, _, min_val, max_val = _interpolate(all_elems, self._num_quantiles - 2, step, offset, self._spec)
        return [min_val] + quantiles + [max_val]

This combiner gives an idea of the distribution of a collection of values
using approximate N-tiles. The output of this combiner is the list of size of
the number of quantiles (num_quantiles), containing the input values of the
minimum value item of the list, the intermediate values (n-tiles) and the
maximum value item of the list, in the sort order provided via key (similar
to the key argument of Python's sorting methods).

If there are fewer values to combine than the number of quantile
(num_quantiles), then the resulting list will contain all the values being
combined, in sorted order.

If no `key` is provided, then the results are sorted in the natural order.

To evaluate the quantiles, we use the "New Algorithm" described here:

[MRL98] Manku, Rajagopalan & Lindsay, "Approximate Medians and other
Quantiles in One Pass and with Limited Memory", Proc. 1998 ACM SIGMOD,
Vol 27, No 2, p 426-435, June 1998.
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.6.6513&rep=rep1
&type=pdf

Note that the weighted quantiles are evaluated using a generalized version of
the algorithm referenced in the paper.

The default error bound is (1 / num_quantiles) for uniformly distributed data
and min(1e-2, 1 / num_quantiles) for weighted case, though in practice the
accuracy tends to be much better.

Args:
  num_quantiles: Number of quantiles to produce. It is the size of the final
    output list, including the mininum and maximum value items.
  buffer_size: The size of the buffers, corresponding to k in the referenced
    paper.
  num_buffers: The number of buffers, corresponding to b in the referenced
    paper.
  key: (optional) Key is a mapping of elements to a comparable key, similar
    to the key argument of Python's sorting methods.
  reverse: (optional) whether to order things smallest to largest, rather
    than largest to smallest.
  weighted: (optional) if set to True, the combiner produces weighted
    quantiles. The input elements are then expected to be tuples of input
    values with the corresponding weight.
  input_batched: (optional) if set to True, inputs are expected to be batches
    of elements.


def insert_tokenizer_in_auto_module(old_model_patterns: ModelPatterns, new_model_patterns: ModelPatterns):
    if old_model_patterns.tokenizer_class is None or new_model_patterns.tokenizer_class is None:
        return
    with open(TRANSFORMERS_PATH / 'models' / 'auto' / 'tokenization_auto.py', 'r', encoding='utf-8') as f:
        content = f.read()
    pattern_tokenizer = re.compile('^\\s*TOKENIZER_MAPPING_NAMES\\s*=\\s*OrderedDict\\b')
    lines = content.split('\n')
    idx = 0
    while not pattern_tokenizer.search(lines[idx]):
        idx += 1
    idx += 1
    while not lines[idx].startswith('TOKENIZER_MAPPING = _LazyAutoMapping'):
        if lines[idx].endswith(','):
            block = lines[idx]
        else:
            block = []
            while not lines[idx].startswith('        ),'):
                block.append(lines[idx])
                idx += 1
            block.append(lines[idx])
            block = '\n'.join(block)
        idx += 1
        if f'"{old_model_patterns.model_type}"' in block and old_model_patterns.tokenizer_class in block:
            break
    new_block = block.replace(old_model_patterns.model_type, new_model_patterns.model_type)
    new_block = new_block.replace(old_model_patterns.tokenizer_class, new_model_patterns.tokenizer_class)
    new_lines = lines[:idx] + [new_block] + lines[idx:]
    with open(TRANSFORMERS_PATH / 'models' / 'auto' / 'tokenization_auto.py', 'w', encoding='utf-8') as f:
        f.write('\n'.join(new_lines))

Add a tokenizer to the relevant mappings in the auto module.

Args:
    old_model_patterns (`ModelPatterns`): The patterns for the old model.
    new_model_patterns (`ModelPatterns`): The patterns for the new model.


def _process_new(self, feed_item):
    return {'assetIdentifier': {'name': feed_item.get(FieldMap.CREATIVE_ASSET_FILE_NAME, None), 'type': feed_item.get(FieldMap.CREATIVE_TYPE, None)}}

Creates a new creative asset DCM object from a feed item representing a creative asset from the Bulkdozer feed.

This function simply creates the object to be inserted later by the BaseDAO
object.

Args:
  feed_item: Feed item representing the creative asset from the Bulkdozer
    feed.

Returns:
  A creative asset object ready to be inserted in DCM through the API.


def softmax_cross_entropy_with_logits_v2_helper(labels, logits, axis=None, name=None, dim=None):
    axis = deprecated_argument_lookup('axis', axis, 'dim', dim)
    del dim
    if axis is None:
        axis = -1
    with ops.name_scope(name, 'softmax_cross_entropy_with_logits', [logits, labels]) as name:
        logits = ops.convert_to_tensor(logits, name='logits')
        labels = ops.convert_to_tensor(labels, name='labels')
        convert_to_float32 = logits.dtype == dtypes.float16 or logits.dtype == dtypes.bfloat16
        precise_logits = math_ops.cast(logits, dtypes.float32) if convert_to_float32 else logits
        labels = math_ops.cast(labels, precise_logits.dtype)
        input_rank = array_ops.rank(precise_logits)
        shape = logits.get_shape()
        if axis != -1:

            def _move_dim_to_end(tensor, dim_index, rank):
                return array_ops.transpose(tensor, array_ops.concat([math_ops.range(dim_index), math_ops.range(dim_index + 1, rank), [dim_index]], 0))
            precise_logits = _move_dim_to_end(precise_logits, axis, input_rank)
            labels = _move_dim_to_end(labels, axis, input_rank)
        input_shape = array_ops.shape(precise_logits)
        precise_logits = _flatten_outer_dims(precise_logits)
        labels = _flatten_outer_dims(labels)
        if config.is_op_determinism_enabled():
            log_probs = log_softmax_v2(precise_logits)
            cost = -math_ops.reduce_sum(labels * log_probs, axis=1)
        else:
            cost, unused_backprop = gen_nn_ops.softmax_cross_entropy_with_logits(precise_logits, labels, name=name)
        output_shape = array_ops.slice(input_shape, [0], [math_ops.subtract(input_rank, 1)])
        cost = array_ops.reshape(cost, output_shape)
        if not context.executing_eagerly() and shape is not None and (shape.dims is not None):
            shape = shape.as_list()
            del shape[axis]
            cost.set_shape(shape)
        if convert_to_float32:
            return math_ops.cast(cost, logits.dtype)
        else:
            return cost

Computes softmax cross entropy between `logits` and `labels`.

Measures the probability error in discrete classification tasks in which the
classes are mutually exclusive (each entry is in exactly one class).  For
example, each CIFAR-10 image is labeled with one and only one label: an image
can be a dog or a truck, but not both.

**NOTE:**  While the classes are mutually exclusive, their probabilities
need not be.  All that is required is that each row of `labels` is
a valid probability distribution.  If they are not, the computation of the
gradient will be incorrect.

If using exclusive `labels` (wherein one and only
one class is true at a time), see `sparse_softmax_cross_entropy_with_logits`.

**WARNING:** This op expects unscaled logits, since it performs a `softmax`
on `logits` internally for efficiency.  Do not call this op with the
output of `softmax`, as it will produce incorrect results.

A common use case is to have logits and labels of shape
`[batch_size, num_classes]`, but higher dimensions are supported, with
the `axis` argument specifying the class dimension.

`logits` and `labels` must have the same dtype (either `float16`, `float32`,
or `float64`).

Backpropagation will happen into both `logits` and `labels`.  To disallow
backpropagation into `labels`, pass label tensors through `tf.stop_gradient`
before feeding it to this function.

**Note that to avoid confusion, it is required to pass only named arguments to
this function.**

Args:
  labels: Each vector along the class dimension should hold a valid
    probability distribution e.g. for the case in which labels are of shape
    `[batch_size, num_classes]`, each row of `labels[i]` must be a valid
    probability distribution.
  logits: Unscaled log probabilities.
  axis: The class dimension. Defaulted to -1 which is the last dimension.
  name: A name for the operation (optional).
  dim: Deprecated alias for axis.

Returns:
  A `Tensor` that contains the softmax cross entropy loss. Its type is the
  same as `logits` and its shape is the same as `labels` except that it does
  not have the last dimension of `labels`.


def input_shape(self):
    if not self._inbound_nodes:
        raise AttributeError('The layer has never been called and thus has no defined input shape.')
    all_input_shapes = set([str(node.input_shapes) for node in self._inbound_nodes])
    if len(all_input_shapes) == 1:
        return self._inbound_nodes[0].input_shapes
    else:
        raise AttributeError('The layer "' + str(self.name) + ' has multiple inbound nodes, with different input shapes. Hence the notion of "input shape" is ill-defined for the layer. Use `get_input_shape_at(node_index)` instead.')

Retrieves the input shape(s) of a layer.

Only applicable if the layer has exactly one input,
i.e. if it is connected to one incoming layer, or if all inputs
have the same shape.

Returns:
    Input shape, as an integer shape tuple
    (or list of shape tuples, one tuple per input tensor).

Raises:
    AttributeError: if the layer has no defined input_shape.
    RuntimeError: if called in Eager mode.


def build_worker_instruction(*args):
    tuple_class = collections.namedtuple(*args)
    tuple_class.__str__ = worker_object_to_string
    tuple_class.__repr__ = worker_object_to_string
    return tuple_class

Create an object representing a ParallelInstruction protobuf.

This will be a collections.namedtuple with a custom __str__ method.

Alas, this wrapper is not known to pylint, which thinks it creates
constants.  You may have to put a disable=invalid-name pylint
annotation on any use of this, depending on your names.

Args:
  *args: first argument is the name of the type to create.  Should
    start with "Worker".  Second arguments is alist of the
    attributes of this object.
Returns:
  A new class, a subclass of tuple, that represents the protobuf.


def matmul(x1, x2):
    if any_symbolic_tensors((x1, x2)):
        return Matmul().symbolic_call(x1, x2)
    return backend.numpy.matmul(x1, x2)

Matrix product of two tensors.

- If both tensors are 1-dimensional, the dot product (scalar) is returned.
- If either tensor is N-D, N > 2, it is treated as a stack of matrices
  residing in the last two indexes and broadcast accordingly.
- If the first tensor is 1-D, it is promoted to a matrix by prepending
  a 1 to its dimensions. After matrix multiplication the prepended
  1 is removed.
- If the second tensor is 1-D, it is promoted to a matrix by appending a 1
  to its dimensions. After matrix multiplication the appended 1 is removed.

Args:
    x1: First tensor.
    x2: Second tensor.

Returns:
    Output tensor, matrix product of the inputs.


def compare(self, reference_model):
    self.console.print('Running comparison')
    ref_spec = {}
    get_weight_spec_of_saveable(reference_model, ref_spec)

    def _compare(target, ref_spec, inner_path, target_name, ref_name, error_count, match_count, checked_paths):
        base_inner_path = inner_path
        for ref_key, ref_val in ref_spec.items():
            inner_path = base_inner_path + '/' + ref_key
            if inner_path in checked_paths:
                continue
            if ref_key not in target:
                error_count += 1
                checked_paths.add(inner_path)
                if isinstance(ref_val, dict):
                    self.console.print(f'[color(160)]...Object [bold]{inner_path}[/] present in {ref_name}, missing from {target_name}[/]')
                    self.console.print(f'    In {ref_name}, {inner_path} contains the following keys: {list(ref_val.keys())}')
                else:
                    self.console.print(f'[color(160)]...Weight [bold]{inner_path}[/] present in {ref_name}, missing from {target_name}[/]')
            elif isinstance(ref_val, dict):
                _error_count, _match_count = _compare(target[ref_key], ref_spec[ref_key], inner_path, target_name, ref_name, error_count=error_count, match_count=match_count, checked_paths=checked_paths)
                error_count += _error_count
                match_count += _match_count
            elif target[ref_key].shape != ref_val.shape:
                error_count += 1
                checked_paths.add(inner_path)
                self.console.print(f'[color(160)]...Weight shape mismatch for [bold]{inner_path}[/][/]\n    In {ref_name}: shape={ref_val.shape}\n    In {target_name}: shape={target[ref_key].shape}')
            else:
                match_count += 1
        return (error_count, match_count)
    checked_paths = set()
    error_count, match_count = _compare(self.weights_dict, ref_spec, inner_path='', target_name='saved file', ref_name='reference model', error_count=0, match_count=0, checked_paths=checked_paths)
    _error_count, _ = _compare(ref_spec, self.weights_dict, inner_path='', target_name='reference model', ref_name='saved file', error_count=0, match_count=0, checked_paths=checked_paths)
    error_count += _error_count
    self.console.print('─────────────────────')
    if error_count == 0:
        status = 'success'
        self.console.print('[color(28)][bold]Comparison successful:[/] saved file is compatible with the reference model[/]')
        if match_count == 1:
            plural = ''
        else:
            plural = 's'
        self.console.print(f'    Found {match_count} matching weight{plural}')
    else:
        status = 'error'
        if error_count == 1:
            plural = ''
        else:
            plural = 's'
        self.console.print(f'[color(160)][bold]Found {error_count} error{plural}:[/] saved file is not compatible with the reference model[/]')
    return {'status': status, 'error_count': error_count, 'match_count': match_count}

Compares the opened file to a reference model.

This method will list all mismatches between the
currently opened file and the provided reference model.

Args:
    reference_model: Model instance to compare to.

Returns:
    Dict with the following keys:
    `'status'`, `'error_count'`, `'match_count'`.
    Status can be `'success'` or `'error'`.
    `'error_count'` is the number of mismatches found.
    `'match_count'` is the number of matching weights found.


def get_text_features(self, input_ids: tf.Tensor | None=None, attention_mask: tf.Tensor | None=None, position_ids: tf.Tensor | None=None, return_dict: Optional[bool]=None) -> tf.Tensor:
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    text_outputs = self.blip.text_model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, return_dict=return_dict)
    pooled_output = text_outputs[1]
    text_features = self.blip.text_projection(pooled_output)
    return text_features

Returns:
    text_features (`tf.Tensor` of shape `(batch_size, output_dim`): The text embeddings obtained by applying
    the projection layer to the pooled output of [`TFBlipTextModel`].

Examples:

```python
>>> from transformers import AutoProcessor, TFBlipModel

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

>>> inputs = processor(text=["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="tf")
>>> text_features = model.get_text_features(**inputs)
```


def trace(name, *trace_args):

    def decorator(f):

        def wrapper(*args, **kwargs):
            t = tracer(name)
            if t.getEffectiveLevel() < logging.DEBUG:
                return f(*args, **kwargs)
            argspec = inspect.getfullargspec(f)
            t.debug('%s: {', f.__name__)
            for arg in trace_args:
                if isinstance(arg, int):
                    argname = argspec.args[arg]
                    val = args[arg]
                else:
                    argname = arg
                    val = kwargs[arg]
                t.debug('%s: %s = %s', f.__name__, argname, show(val))
            ret = f(*args, **kwargs)
            t.debug('%s: -> %s', f.__name__, show(ret))
            t.debug('%s: }', f.__name__)
            return ret
        return wrapper
    return decorator

Record args and return value for a function call.

The trace is of the form
  function name: {
  function name: arg = value
  function name: arg = value
  ...
  function name: -> return
  function name: }

This will let us write tools to pretty print the traces with indentation etc.

Args:
  name: module name, usually `__name__`
  *trace_args: function arguments to log

Returns:
  a decorator


class InformerConfig(PretrainedConfig):
    model_type = 'informer'
    attribute_map = {'hidden_size': 'd_model', 'num_attention_heads': 'encoder_attention_heads', 'num_hidden_layers': 'encoder_layers'}

    def __init__(self, prediction_length: Optional[int]=None, context_length: Optional[int]=None, distribution_output: str='student_t', loss: str='nll', input_size: int=1, lags_sequence: Optional[List[int]]=None, scaling: Optional[Union[str, bool]]='mean', num_dynamic_real_features: int=0, num_static_real_features: int=0, num_static_categorical_features: int=0, num_time_features: int=0, cardinality: Optional[List[int]]=None, embedding_dimension: Optional[List[int]]=None, d_model: int=64, encoder_ffn_dim: int=32, decoder_ffn_dim: int=32, encoder_attention_heads: int=2, decoder_attention_heads: int=2, encoder_layers: int=2, decoder_layers: int=2, is_encoder_decoder: bool=True, activation_function: str='gelu', dropout: float=0.05, encoder_layerdrop: float=0.1, decoder_layerdrop: float=0.1, attention_dropout: float=0.1, activation_dropout: float=0.1, num_parallel_samples: int=100, init_std: float=0.02, use_cache=True, attention_type: str='prob', sampling_factor: int=5, distil: bool=True, **kwargs):
        self.prediction_length = prediction_length
        self.context_length = context_length or prediction_length
        self.distribution_output = distribution_output
        self.loss = loss
        self.input_size = input_size
        self.num_time_features = num_time_features
        self.lags_sequence = lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
        self.scaling = scaling
        self.num_dynamic_real_features = num_dynamic_real_features
        self.num_static_real_features = num_static_real_features
        self.num_static_categorical_features = num_static_categorical_features
        if cardinality and num_static_categorical_features > 0:
            if len(cardinality) != num_static_categorical_features:
                raise ValueError('The cardinality should be a list of the same length as `num_static_categorical_features`')
            self.cardinality = cardinality
        else:
            self.cardinality = [0]
        if embedding_dimension and num_static_categorical_features > 0:
            if len(embedding_dimension) != num_static_categorical_features:
                raise ValueError('The embedding dimension should be a list of the same length as `num_static_categorical_features`')
            self.embedding_dimension = embedding_dimension
        else:
            self.embedding_dimension = [min(50, (cat + 1) // 2) for cat in self.cardinality]
        self.num_parallel_samples = num_parallel_samples
        self.feature_size = input_size * len(self.lags_sequence) + self._number_of_features
        self.d_model = d_model
        self.encoder_attention_heads = encoder_attention_heads
        self.decoder_attention_heads = decoder_attention_heads
        self.encoder_ffn_dim = encoder_ffn_dim
        self.decoder_ffn_dim = decoder_ffn_dim
        self.encoder_layers = encoder_layers
        self.decoder_layers = decoder_layers
        self.dropout = dropout
        self.attention_dropout = attention_dropout
        self.activation_dropout = activation_dropout
        self.encoder_layerdrop = encoder_layerdrop
        self.decoder_layerdrop = decoder_layerdrop
        self.activation_function = activation_function
        self.init_std = init_std
        self.use_cache = use_cache
        self.attention_type = attention_type
        self.sampling_factor = sampling_factor
        self.distil = distil
        super().__init__(is_encoder_decoder=is_encoder_decoder, **kwargs)

    @property
    def _number_of_features(self) -> int:
        return sum(self.embedding_dimension) + self.num_dynamic_real_features + self.num_time_features + self.num_static_real_features + self.input_size * 2

This is the configuration class to store the configuration of an [`InformerModel`]. It is used to instantiate an
Informer 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 Informer
[huggingface/informer-tourism-monthly](https://huggingface.co/huggingface/informer-tourism-monthly) architecture.

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

Args:
    prediction_length (`int`):
        The prediction length for the decoder. In other words, the prediction horizon of the model. This value is
        typically dictated by the dataset and we recommend to set it appropriately.
    context_length (`int`, *optional*, defaults to `prediction_length`):
        The context length for the encoder. If `None`, the context length will be the same as the
        `prediction_length`.
    distribution_output (`string`, *optional*, defaults to `"student_t"`):
        The distribution emission head for the model. Could be either "student_t", "normal" or "negative_binomial".
    loss (`string`, *optional*, defaults to `"nll"`):
        The loss function for the model corresponding to the `distribution_output` head. For parametric
        distributions it is the negative log likelihood (nll) - which currently is the only supported one.
    input_size (`int`, *optional*, defaults to 1):
        The size of the target variable which by default is 1 for univariate targets. Would be > 1 in case of
        multivariate targets.
    scaling (`string` or `bool`, *optional* defaults to `"mean"`):
        Whether to scale the input targets via "mean" scaler, "std" scaler or no scaler if `None`. If `True`, the
        scaler is set to "mean".
    lags_sequence (`list[int]`, *optional*, defaults to `[1, 2, 3, 4, 5, 6, 7]`):
        The lags of the input time series as covariates often dictated by the frequency of the data. Default is
        `[1, 2, 3, 4, 5, 6, 7]` but we recommend to change it based on the dataset appropriately.
    num_time_features (`int`, *optional*, defaults to 0):
        The number of time features in the input time series.
    num_dynamic_real_features (`int`, *optional*, defaults to 0):
        The number of dynamic real valued features.
    num_static_categorical_features (`int`, *optional*, defaults to 0):
        The number of static categorical features.
    num_static_real_features (`int`, *optional*, defaults to 0):
        The number of static real valued features.
    cardinality (`list[int]`, *optional*):
        The cardinality (number of different values) for each of the static categorical features. Should be a list
        of integers, having the same length as `num_static_categorical_features`. Cannot be `None` if
        `num_static_categorical_features` is > 0.
    embedding_dimension (`list[int]`, *optional*):
        The dimension of the embedding for each of the static categorical features. Should be a list of integers,
        having the same length as `num_static_categorical_features`. Cannot be `None` if
        `num_static_categorical_features` is > 0.
    d_model (`int`, *optional*, defaults to 64):
        Dimensionality of the transformer layers.
    encoder_layers (`int`, *optional*, defaults to 2):
        Number of encoder layers.
    decoder_layers (`int`, *optional*, defaults to 2):
        Number of decoder layers.
    encoder_attention_heads (`int`, *optional*, defaults to 2):
        Number of attention heads for each attention layer in the Transformer encoder.
    decoder_attention_heads (`int`, *optional*, defaults to 2):
        Number of attention heads for each attention layer in the Transformer decoder.
    encoder_ffn_dim (`int`, *optional*, defaults to 32):
        Dimension of the "intermediate" (often named feed-forward) layer in encoder.
    decoder_ffn_dim (`int`, *optional*, defaults to 32):
        Dimension 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 decoder. If string, `"gelu"` and
        `"relu"` are supported.
    dropout (`float`, *optional*, defaults to 0.1):
        The dropout probability for all fully connected layers in the encoder, and decoder.
    encoder_layerdrop (`float`, *optional*, defaults to 0.1):
        The dropout probability for the attention and fully connected layers for each encoder layer.
    decoder_layerdrop (`float`, *optional*, defaults to 0.1):
        The dropout probability for the attention and fully connected layers for each decoder layer.
    attention_dropout (`float`, *optional*, defaults to 0.1):
        The dropout probability for the attention probabilities.
    activation_dropout (`float`, *optional*, defaults to 0.1):
        The dropout probability used between the two layers of the feed-forward networks.
    num_parallel_samples (`int`, *optional*, defaults to 100):
        The number of samples to generate in parallel for each time step of inference.
    init_std (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated normal weight initialization distribution.
    use_cache (`bool`, *optional*, defaults to `True`):
        Whether to use the past key/values attentions (if applicable to the model) to speed up decoding.
    attention_type (`str`, *optional*, defaults to "prob"):
        Attention used in encoder. This can be set to "prob" (Informer's ProbAttention) or "full" (vanilla
        transformer's canonical self-attention).
    sampling_factor (`int`, *optional*, defaults to 5):
        ProbSparse sampling factor (only makes affect when `attention_type`="prob"). It is used to control the
        reduced query matrix (Q_reduce) input length.
    distil (`bool`, *optional*, defaults to `True`):
        Whether to use distilling in encoder.

Example:

```python
>>> from transformers import InformerConfig, InformerModel

>>> # Initializing an Informer configuration with 12 time steps for prediction
>>> configuration = InformerConfig(prediction_length=12)

>>> # Randomly initializing a model (with random weights) from the configuration
>>> model = InformerModel(configuration)

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


class DepthProPreActResidualLayer(nn.Module):

    def __init__(self, config):
        super().__init__()
        self.use_batch_norm = config.use_batch_norm_in_fusion_residual
        use_bias_in_fusion_residual = config.use_bias_in_fusion_residual if config.use_bias_in_fusion_residual is not None else not self.use_batch_norm
        self.activation1 = nn.ReLU()
        self.convolution1 = nn.Conv2d(config.fusion_hidden_size, config.fusion_hidden_size, kernel_size=3, stride=1, padding=1, bias=use_bias_in_fusion_residual)
        self.activation2 = nn.ReLU()
        self.convolution2 = nn.Conv2d(config.fusion_hidden_size, config.fusion_hidden_size, kernel_size=3, stride=1, padding=1, bias=use_bias_in_fusion_residual)
        if self.use_batch_norm:
            self.batch_norm1 = nn.BatchNorm2d(config.fusion_hidden_size)
            self.batch_norm2 = nn.BatchNorm2d(config.fusion_hidden_size)

    def forward(self, hidden_state: torch.Tensor) -> torch.Tensor:
        residual = hidden_state
        hidden_state = self.activation1(hidden_state)
        hidden_state = self.convolution1(hidden_state)
        if self.use_batch_norm:
            hidden_state = self.batch_norm1(hidden_state)
        hidden_state = self.activation2(hidden_state)
        hidden_state = self.convolution2(hidden_state)
        if self.use_batch_norm:
            hidden_state = self.batch_norm2(hidden_state)
        return hidden_state + residual

ResidualConvUnit, pre-activate residual unit.

Args:
    config (`[DepthProConfig]`):
        Model configuration class defining the model architecture.


def batch(self, spec, batch_size):
    raise NotImplementedError(f'{type(self).__name__}.batch')

Returns the TypeSpec representing a batch of values described by `spec`.

Args:
  spec: The `TypeSpec` for an individual value.
  batch_size: An `int` indicating the number of values that are batched
    together, or `None` if the batch size is not known.

Returns:
  A `TypeSpec` for a batch of values.


def _repr_to_list(value: torch.Tensor):
    torch.set_printoptions(sci_mode=True, linewidth=120)
    with StringIO() as buf, redirect_stdout(buf):
        print(value)
        raw = buf.getvalue()
    return _sanitize_repr_for_diff(raw).splitlines()

Converts a tensor into a sanitized multi-line string representation.

Args:
    value (`torch.Tensor`): The tensor to represent.

Returns:
    `List[str]`: List of string lines representing the tensor.