def call(self, input_ids: TFModelInputType | None=None, bbox: np.ndarray | tf.Tensor | None=None, attention_mask: np.ndarray | tf.Tensor | None=None, token_type_ids: np.ndarray | tf.Tensor | None=None, position_ids: np.ndarray | tf.Tensor | None=None, head_mask: np.ndarray | tf.Tensor | None=None, inputs_embeds: np.ndarray | tf.Tensor | None=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, labels: np.ndarray | tf.Tensor | None=None, training: Optional[bool]=False) -> Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]:
    outputs = self.layoutlm(input_ids=input_ids, bbox=bbox, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, training=training)
    pooled_output = outputs[1]
    pooled_output = self.dropout(inputs=pooled_output, training=training)
    logits = self.classifier(inputs=pooled_output)
    loss = None if labels is None else self.hf_compute_loss(labels=labels, logits=logits)
    if not return_dict:
        output = (logits,) + outputs[2:]
        return (loss,) + output if loss is not None else output
    return TFSequenceClassifierOutput(loss=loss, logits=logits, hidden_states=outputs.hidden_states, attentions=outputs.attentions)

labels (`tf.Tensor` or `np.ndarray` of shape `(batch_size,)`, *optional*):
    Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
    config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
    `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

Returns:

Examples:

```python
>>> from transformers import AutoTokenizer, TFLayoutLMForSequenceClassification
>>> import tensorflow as tf

>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/layoutlm-base-uncased")
>>> model = TFLayoutLMForSequenceClassification.from_pretrained("microsoft/layoutlm-base-uncased")

>>> words = ["Hello", "world"]
>>> normalized_word_boxes = [637, 773, 693, 782], [698, 773, 733, 782]

>>> token_boxes = []
>>> for word, box in zip(words, normalized_word_boxes):
...     word_tokens = tokenizer.tokenize(word)
...     token_boxes.extend([box] * len(word_tokens))
>>> # add bounding boxes of cls + sep tokens
>>> token_boxes = [[0, 0, 0, 0]] + token_boxes + [[1000, 1000, 1000, 1000]]

>>> encoding = tokenizer(" ".join(words), return_tensors="tf")
>>> input_ids = encoding["input_ids"]
>>> attention_mask = encoding["attention_mask"]
>>> token_type_ids = encoding["token_type_ids"]
>>> bbox = tf.convert_to_tensor([token_boxes])
>>> sequence_label = tf.convert_to_tensor([1])

>>> outputs = model(
...     input_ids=input_ids,
...     bbox=bbox,
...     attention_mask=attention_mask,
...     token_type_ids=token_type_ids,
...     labels=sequence_label,
... )

>>> loss = outputs.loss
>>> logits = outputs.logits
```


def field_content_length(msg: message.Message, field: Union[descriptor.FieldDescriptor, str]) -> int:
    if isinstance(field, str):
        field = _field_descriptor_for_name(msg, field)
    if field_is_repeated(field):
        return len(getattr(msg, field.name))
    return 1 if msg.HasField(field.name) else 0

Returns the size of the field.

Args:
  msg: The Message whose fields to examine.
  field: The FieldDescriptor or name of the field to examine.

Returns:
  The number of elements at the provided field. If field describes a singular
  protobuf field, this will return 1. If the field is not set, returns 0.


def _get_layer_broadcasters_from_rps(zero_broadcaster, source_rps, target_rps):
    if not isinstance(zero_broadcaster, _LayerBroadcaster):
        raise TypeError('Not a _LayerBroadcaster: ' + str(zero_broadcaster))
    assert len(source_rps) == len(target_rps)
    if not source_rps:
        return [zero_broadcaster]
    next_broadcaster = zero_broadcaster.next_layer(source_rps[0], target_rps[0])
    tail_broadcasters = _get_layer_broadcasters_from_rps(next_broadcaster, source_rps[1:], target_rps[1:])
    return [zero_broadcaster] + tail_broadcasters

Get LayerBroadcasters from RowPartitions.

         *--zero_broadcaster->*
         |                    |
       source_rps[0]     target_rps[0]
         |                    |
         V                    V
         *---result[1]------->*
         |                    |
       source_rps[1]     target_rps[1]
         |                    |
         V                    V
         *---result[2]------->*
                .
                .
                .
         *---result[k-1]----->*
         |                    |
       source_rps[k]     target_rps[k]
         |                    |
         V                    V
         *---result[k]------->*

Note: result[0] = zero_broadcaster

Args:
  zero_broadcaster: a broadcaster between the source and target row
    partitions' rows, and equal to result[0].
  source_rps: source row partitions.
  target_rps: target row partitions (same length as source_rps).

Returns:
  result: a list of LayerBroadcasters.


def run(self, fn, args=(), kwargs=None, options=None):
    validate_run_function(fn)
    fn, args, kwargs = _maybe_partial_apply_variables(fn, args, kwargs)
    fn = autograph.tf_convert(fn, autograph_ctx.control_status_ctx())
    options = options or distribute_lib.RunOptions()
    return self.extended.tpu_run(fn, args, kwargs, options)

Run `fn` on each replica, with the given arguments.

Executes ops specified by `fn` on each replica. If `args` or `kwargs` have
"per-replica" values, such as those produced by a "distributed `Dataset`",
when `fn` is executed on a particular replica, it will be executed with the
component of those "per-replica" values that correspond to that replica.

`fn` may call `tf.distribute.get_replica_context()` to access members such
as `all_reduce`.

All arguments in `args` or `kwargs` should either be nest of tensors or
per-replica objects containing tensors or composite tensors.

Users can pass strategy specific options to `options` argument. An example
to enable bucketizing dynamic shapes in `TPUStrategy.run`
is:

>>> resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu='')
>>> tf.config.experimental_connect_to_cluster(resolver)
>>> tf.tpu.experimental.initialize_tpu_system(resolver)
>>> strategy = tf.distribute.experimental.TPUStrategy(resolver)

>>> options = tf.distribute.RunOptions(
...     experimental_bucketizing_dynamic_shape=True)

>>> dataset = tf.data.Dataset.range(
...    strategy.num_replicas_in_sync, output_type=dtypes.float32).batch(
...        strategy.num_replicas_in_sync, drop_remainder=True)
>>> input_iterator = iter(strategy.experimental_distribute_dataset(dataset))

>>> @tf.function()
... def step_fn(inputs):
...  output = tf.reduce_sum(inputs)
...  return output

>>> strategy.run(step_fn, args=(next(input_iterator),), options=options)

Args:
  fn: The function to run. The output must be a `tf.nest` of `Tensor`s.
  args: (Optional) Positional arguments to `fn`.
  kwargs: (Optional) Keyword arguments to `fn`.
  options: (Optional) An instance of `tf.distribute.RunOptions` specifying
    the options to run `fn`.

Returns:
  Merged return value of `fn` across replicas. The structure of the return
  value is the same as the return value from `fn`. Each element in the
  structure can either be "per-replica" `Tensor` objects or `Tensor`s
  (for example, if running on a single replica).


def report_to_rows(report):
    if type(report) is GeneratorType:
        leftovers = ''
        for chunk in report:
            data, extra = chunk.rsplit('\n', 1)
            for row in csv_to_rows(leftovers + data):
                yield row
            leftovers = extra
    else:
        for row in csv_to_rows(report):
            yield row

Helper to convert DCM files into iterator of rows, memory efficient.

Usage example:

```
filename, report = report_file(...)
rows = report_to_rows(report)
```

Args:
  * report: (iterator or file) Either an iterator or file that will be
    converted to rows.

Returns:
  * Iterator of lists representing each row.


def process_update(x):
    if callable(x):
        update = lambda: process_update(x())
        return update()
    elif isinstance(x, ops.Operation):
        update = x
    elif hasattr(x, 'op'):
        update = x.op
    else:
        update = tensor_conversion.convert_to_tensor_v2_with_dispatch(x)
    reachable = tf_utils.get_reachable_from_inputs(relevant_inputs, [update])
    update._unconditional_update = update not in reachable
    return update

Standardize update ops.

Args:
  x: Tensor, op, or callable.

Returns:
  An update op.


def get(self, path):
    if not path:
        parsed_path = '/vars'
    else:
        parsed_path = path
    weight_map = self.sharding_config['weight_map']
    filenames = weight_map.get(parsed_path) or weight_map.get('/' + parsed_path + '/vars')
    if filenames is not None:
        if not isinstance(filenames, list):
            filenames = [filenames]
        self.current_shard_filenames = filenames
        filename = filenames[0]
    else:
        self.current_shard_filenames = []
        filename = None
    if filename is not None and filename != self.current_shard_path.name:
        self.close()
        self.h5_file = self._get_h5_file(self.path.with_name(filename))
    return super().get(path)

Get the H5 entry group.

This method is only available in read mode. If the path is not found in
the current shard, it will switch to the correct shard.

Args:
    path: `str`. The variable path.


def _make_source_table(self, source_list, is_tf_py_library):
    path_head = 'Source file path'
    num_nodes_head = '#(nodes)'
    num_tensors_head = '#(tensors)'
    num_dumps_head = '#(tensor dumps)'
    if is_tf_py_library:
        color = cli_shared.COLOR_GRAY
        lines = [RL('TensorFlow Python library file(s):', color)]
    else:
        color = cli_shared.COLOR_WHITE
        lines = [RL('File(s) outside TensorFlow Python library:', color)]
    if not source_list:
        lines.append(RL('[No files.]'))
        lines.append(RL())
        return debugger_cli_common.rich_text_lines_from_rich_line_list(lines)
    path_column_width = max(max((len(item[0]) for item in source_list)), len(path_head)) + 1
    num_nodes_column_width = max(max((len(str(item[2])) for item in source_list)), len(num_nodes_head)) + 1
    num_tensors_column_width = max(max((len(str(item[3])) for item in source_list)), len(num_tensors_head)) + 1
    head = RL(path_head + ' ' * (path_column_width - len(path_head)), color)
    head += RL(num_nodes_head + ' ' * (num_nodes_column_width - len(num_nodes_head)), color)
    head += RL(num_tensors_head + ' ' * (num_tensors_column_width - len(num_tensors_head)), color)
    head += RL(num_dumps_head, color)
    lines.append(head)
    for file_path, _, num_nodes, num_tensors, num_dumps, first_line_num in source_list:
        path_attributes = [color]
        if source_utils.is_extension_uncompiled_python_source(file_path):
            path_attributes.append(debugger_cli_common.MenuItem(None, 'ps %s -b %d' % (file_path, first_line_num)))
        line = RL(file_path, path_attributes)
        line += ' ' * (path_column_width - len(line))
        line += RL(str(num_nodes) + ' ' * (num_nodes_column_width - len(str(num_nodes))), color)
        line += RL(str(num_tensors) + ' ' * (num_tensors_column_width - len(str(num_tensors))), color)
        line += RL(str(num_dumps), color)
        lines.append(line)
    lines.append(RL())
    return debugger_cli_common.rich_text_lines_from_rich_line_list(lines)

Make a table summarizing the source files that create nodes and tensors.

Args:
  source_list: List of source files and related information as a list of
    tuples (file_path, is_tf_library, num_nodes, num_tensors, num_dumps,
    first_line).
  is_tf_py_library: (`bool`) whether this table is for files that belong
    to the TensorFlow Python library.

Returns:
  The table as a `debugger_cli_common.RichTextLines` object.


def _channel_flatten_input(x, data_format):
    graph = ops.get_default_graph()
    cache_key = (graph, x.ref(), data_format)
    if cache_key not in _channel_flatten_input_cache:
        x_shape = array_ops.shape(x)
        neg_ones = constant_op.constant([-1], dtype=x_shape.dtype)
        if data_format == b'NCHW':
            order = [1, 0, 2, 3, 4]
            shape = array_ops.concat([x_shape[1:2], neg_ones, x_shape[3:]], axis=0)
            reverse_order = order
        else:
            order = [1, 2, 3, 0, 4]
            shape = array_ops.concat([x_shape[1:4], neg_ones], axis=0)
            reverse_order = [3, 0, 1, 2, 4]
        x = array_ops.transpose(x, order)
        reverse_shape = array_ops.shape(x)
        x = array_ops.reshape(x, shape)
        outputs = (x, reverse_order, reverse_shape)
        _channel_flatten_input_cache[cache_key] = outputs
    else:
        outputs = _channel_flatten_input_cache[cache_key]
    return outputs

Merge the stack dimension with the channel dimension.

If S is pfor's stacking dimension, then,
  - for SNCHW, we transpose to NSCHW. If N dimension has size 1, the transpose
    should be cheap.
  - for SNHWC, we transpose to NHWSC.
We then merge the S and C dimension.

Args:
  x: tensor_lib.Tensor to transform.
  data_format: "NCHW" or "NHWC".

Returns:
  A 3-element tuple with the transformed value, along with the shape for
  reshape and order for transpose required to transform back.


class DPRContextEncoderOutput(ModelOutput):
    pooler_output: torch.FloatTensor
    hidden_states: Optional[Tuple[torch.FloatTensor, ...]] = None
    attentions: Optional[Tuple[torch.FloatTensor, ...]] = None

Class for outputs of [`DPRQuestionEncoder`].

Args:
    pooler_output (`torch.FloatTensor` of shape `(batch_size, embeddings_size)`):
        The DPR encoder outputs the *pooler_output* that corresponds to the context representation. Last layer
        hidden-state of the first token of the sequence (classification token) further processed by a Linear layer.
        This output is to be used to embed contexts for nearest neighbors queries with questions embeddings.
    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(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.


def _prepare_skip_target_masks(self):
    return [l is None for l in self.loss_functions]

Boolean mask for whether the target in the output list should be skipped.

If the loss function corresponding to a model output is None, then this
output will be skipped during total loss calculation and feed targets
preparation.

Returns:
  A boolean list for whether the corresponding target in the output list
  should be skipped during loss calculation.


def _dimension_sizes(x):
    dynamic_shape = array_ops.shape(x)
    rank = x.get_shape().rank
    rank_is_known = rank is not None
    if rank_is_known and rank == 0:
        return (1,)
    if rank_is_known and rank > 0:
        static_shape = x.get_shape().as_list()
        sizes = [int(size) if size is not None else dynamic_shape[i] for i, size in enumerate(static_shape)]
        return sizes
    has_rank_zero = math_ops.equal(array_ops.rank(x), 0)
    return cond.cond(has_rank_zero, lambda: array_ops.constant([1]), lambda: dynamic_shape)

Gets the dimension sizes of a tensor `x`.

If a size can be determined statically it is returned as an integer,
otherwise as a tensor.

If `x` is a scalar it is treated as rank 1 size 1.

Args:
  x: A `Tensor`.

Returns:
  Dimension sizes.


def __init__(self, dataset_id, processing_mode, address, element_spec, protocol, data_transfer_protocol, job_name=None, consumer_index=None, num_consumers=None, max_outstanding_requests=None, task_refresh_interval_hint_ms=None, cross_trainer_cache=None, target_workers='AUTO'):
    if consumer_index is None != num_consumers is None:
        raise ValueError('Must either set both `consumer_index` and `num_consumers`, or neither. ', f'consumer_index={consumer_index}, num_consumers={num_consumers}')
    if num_consumers is not None and job_name is None:
        raise ValueError(f'`job_name` must be set when setting `num_consumers`. num_consumers was set to {num_consumers}.')
    processing_mode_def = data_service_pb2.ProcessingModeDef(sharding_policy=_get_validated_sharding_policy(processing_mode)._to_proto())
    if job_name is None:
        job_name = ''
    if max_outstanding_requests is None:
        max_outstanding_requests = dataset_ops.AUTOTUNE
    if task_refresh_interval_hint_ms is None:
        task_refresh_interval_hint_ms = dataset_ops.AUTOTUNE
    self._dataset_id = _to_tensor(dataset_id)
    self._processing_mode = ops.convert_to_tensor(processing_mode_def.SerializeToString(), dtype=dtypes.string, name='processing_mode')
    self._address = ops.convert_to_tensor(address, dtype=dtypes.string, name='address')
    self._protocol = ops.convert_to_tensor(protocol, dtype=dtypes.string, name='protocol')
    self._job_name = ops.convert_to_tensor(job_name, dtype=dtypes.string, name='job_name')
    self._consumer_index = ops.convert_to_tensor(-1 if consumer_index is None else consumer_index, dtype=dtypes.int64, name='consumer_index')
    self._num_consumers = ops.convert_to_tensor(-1 if num_consumers is None else num_consumers, dtype=dtypes.int64, name='num_consumers')
    self._max_outstanding_requests = ops.convert_to_tensor(max_outstanding_requests, dtype=dtypes.int64, name='max_outstanding_requests')
    self._element_spec = element_spec
    uncompress_func = structured_function.StructuredFunctionWrapper(lambda x: compression_ops.uncompress(x, output_spec=element_spec), transformation_name='DataServiceDataset.uncompress()', input_structure=tensor.TensorSpec(shape=(), dtype=dtypes.variant))
    cross_trainer_cache_options = cross_trainer_cache._to_proto().SerializeToString() if cross_trainer_cache else None
    compat_kwargs = {}
    if data_transfer_protocol is not None:
        compat_kwargs['data_transfer_protocol'] = data_transfer_protocol
    uncompress = True
    variant_tensor = gen_experimental_dataset_ops.data_service_dataset_v4(dataset_id=self._dataset_id, processing_mode=self._processing_mode, address=self._address, protocol=self._protocol, job_name=self._job_name, consumer_index=self._consumer_index, num_consumers=self._num_consumers, max_outstanding_requests=self._max_outstanding_requests, task_refresh_interval_hint_ms=task_refresh_interval_hint_ms, iteration_counter=gen_experimental_dataset_ops.dummy_iteration_counter(), target_workers=target_workers, uncompress=uncompress, uncompress_fn=uncompress_func.function, cross_trainer_cache_options=cross_trainer_cache_options, **compat_kwargs, **self._flat_structure)
    super(_DataServiceDatasetV2, self).__init__(variant_tensor)

Constructs a _DataServiceDatasetV2.

Args:
  dataset_id: The dataset id for the dataset to read from.
  processing_mode: A `tf.data.experimental.service.ShardingPolicy`
    specifying how to shard the dataset among tf.data workers. See
    `tf.data.experimental.service.ShardingPolicy` for details. For backwards
    compatibility, `processing_mode` may also be set to the strings
    `"parallel_epochs"` or `"distributed_epoch"`, which are respectively
    equivalent to `ShardingPolicy.OFF` and `ShardingPolicy.DYNAMIC`.
  address: The tf.data service address, e.g. "localhost:5000".
  element_spec: The dataset element spec for the dataset to read from.
  protocol: The protocol to use for communicating with the tf.data service,
    e.g. "grpc".
  data_transfer_protocol: (Optional.) The protocol to use for transferring
    data with the tf.data service. If not provided, a protocol is determined
    at runtime.
  job_name: (Optional.) The name of the job. If provided, it must be a
    non-empty string or Tensor. This argument makes it possible for multiple
    datasets to share the same job. The default behavior is that the dataset
    creates anonymous, exclusively owned jobs.
  consumer_index: (Optional.) The index of the consumer in the range from
    `0` to `num_consumers`. Must be specified alongside `num_consumers`.
    When specified, consumers will read from the job in a strict round-robin
    order, instead of the default first-come-first-served order.
  num_consumers: (Optional.) The number of consumers which will consume from
    the job. Must be specified alongside `consumer_index`. When specified,
    consumers will read from the job in a strict round-robin order, instead
    of the default first-come-first-served order. When `num_consumers` is
    specified, the dataset must have infinite cardinality to prevent a
    producer from running out of data early and causing consumers to go out
    of sync.
  max_outstanding_requests: (Optional.) A limit on how many elements may be
    requested at the same time. You can use this option to control the
    amount of memory used, since `distribute` won't use more than
    `element_size` * `max_outstanding_requests` of memory.
  task_refresh_interval_hint_ms: (Optional.) A hint for how often to query
    the dispatcher for task changes.
  cross_trainer_cache: (Optional.) If a `CrossTrainerCache` object is
    provided, dataset iteration will be shared across concurrently running
    trainers. See
    https://www.tensorflow.org/api_docs/python/tf/data/experimental/service#sharing_tfdata_service_with_concurrent_trainers
      for details.
  target_workers: (Optional.) Which workers to read from. If `"AUTO"`,
    tf.data runtime decides which workers to read from. If `"ANY"`, reads
    from any tf.data service workers. If `"LOCAL"`, only reads from local
    in-process tf.data service workers. `"AUTO"` works well for most cases,
    while users can specify other targets. For example, `"LOCAL"` helps
    avoid RPCs and data copy if every TF worker colocates with a tf.data
    service worker. Consumers of a shared job must use the same
    `target_workers`. Defaults to `"AUTO"`.


def assert_rank_in(x, ranks, data=None, summarize=None, message=None, name=None):
    with ops.name_scope(name, 'assert_rank_in', (x,) + tuple(ranks) + tuple(data or [])):
        if not isinstance(x, sparse_tensor.SparseTensor):
            x = ops.convert_to_tensor(x, name='x')
        ranks = tuple([ops.convert_to_tensor(rank, name='rank') for rank in ranks])
        message = _message_prefix(message)
        if context.executing_eagerly() or isinstance(x, sparse_tensor.SparseTensor):
            name = ''
        else:
            name = x.name
        if data is None:
            data = [message, 'Tensor %s must have rank in' % name] + list(ranks) + ['Received shape: ', array_ops.shape(x)]
        try:
            assert_op = _assert_ranks_condition(x, ranks, _static_rank_in, _dynamic_rank_in, data, summarize)
        except ValueError as e:
            if e.args[0] == 'Static rank condition failed':
                raise ValueError('%sTensor %s must have rank in %s.  Received rank %d, shape %s' % (message, name, tuple((r.item() for r in e.args[2])), e.args[1], x.get_shape()))
            else:
                raise
    return assert_op

Assert `x` has rank in `ranks`.

Example of adding a dependency to an operation:

```python
with tf.control_dependencies([tf.compat.v1.assert_rank_in(x, (2, 4))]):
  output = tf.reduce_sum(x)
```

Args:
  x:  Numeric `Tensor`.
  ranks:  Iterable of scalar `Tensor` objects.
  data:  The tensors to print out if the condition is False.  Defaults to
    error message and first few entries of `x`.
  summarize: Print this many entries of each tensor.
  message: A string to prefix to the default message.
  name: A name for this operation (optional).
    Defaults to "assert_rank_in".

Returns:
  Op raising `InvalidArgumentError` unless rank of `x` is in `ranks`.
  If static checks determine `x` has matching rank, a `no_op` is returned.

Raises:
  ValueError:  If static checks determine `x` has mismatched rank.


def view_as_real(x):
    if any_symbolic_tensors((x,)):
        return ViewAsReal().symbolic_call(x)
    x = backend.convert_to_tensor(x)
    real_part = backend.numpy.real(x)
    imag_part = backend.numpy.imag(x)
    return backend.numpy.stack((real_part, imag_part), axis=-1)

Converts a complex tensor to a real tensor with shape `(..., 2)`,
where the last dimension represents the real and imaginary components.

Args:
    x: A complex tensor.

Returns:
    A real tensor where the last dimension contains the
    real and imaginary parts.

Example:
```
>>> import numpy as np
>>> from keras import ops

>>> complex_tensor = np.array([1 + 2j, 3 + 4j])
>>> real = ops.view_as_real(complex_tensor)
>>> real
array([[1., 2.],
       [3., 4.]])
```


def expected_exercise_fn(design, continuation_value, exercise_value):
    batch_design = tf.broadcast_to(tf.expand_dims(design, -1), design.shape + [continuation_value.shape[-1]])
    mask = tf.cast(exercise_value > 0, design.dtype)
    masked = tf.transpose(batch_design * mask, perm=(2, 1, 0))
    lhs = tf.matmul(masked, masked, transpose_a=True)
    lhs_pinv = tf.linalg.pinv(lhs)
    rhs = tf.matmul(masked, tf.expand_dims(tf.transpose(continuation_value), -1), transpose_a=True)
    beta = tf.linalg.matmul(lhs_pinv, rhs)
    continuation = tf.matmul(tf.transpose(batch_design, perm=(2, 1, 0)), beta)
    return tf.maximum(tf.transpose(tf.squeeze(continuation, -1)), 0.0)

Returns the expected continuation value for each path.

Args:
  design: A real `Tensor` of shape `[basis_size, num_samples]`.
  continuation_value: A `Tensor` of shape `[num_samples, payoff_dim]` and of
    the same dtype as `design`. The optimal value of the option conditional on
    not exercising now or earlier, taking future information into account.
  exercise_value: A `Tensor` of the same shape and dtype as
    `continuation_value`. Value of the option if exercised immideately at
    the current time

Returns:
  A `Tensor` of the same shape and dtype as `continuation_value` whose
  `(n, v)`-th entry represents the expected continuation value of sample path
  `n` under the `v`-th payoff scheme.


def _is_framework_filename(filename):
    for pattern in _EXTERNAL_FILENAME_PATTERNS:
        if pattern.search(filename):
            return False
    for pattern in _FRAMEWORK_FILENAME_PATTERNS:
        if pattern.search(filename):
            return True
    for prefix in _FRAMEWORK_PATH_PREFIXES:
        if filename.startswith(prefix):
            return True
    return False

Returns whether a filename should be considered a part of the framework.

A file is part of the framework if it does not match a pattern in
_EXTERNAL_FILENAME_PATTERNS and it either matches a pattern in
_FRAMEWORK_FILENAME_PATTERNS or starts with a _FRAMEWORK_PATH_PREFIXES prefix.

Args:
  filename: A filename string.

Returns:
  Whether the filename should be considered to be internal to the
  TensorFlow framework for the purposes of reporting errors.


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

Request the job status. To request the status of a job, we recommend using `projects.locations.jobs.getMetrics` with a [regional endpoint] (https://cloud.google.com/dataflow/docs/concepts/regional-endpoints). Using `projects.jobs.getMetrics` is not recommended, as you can only request the status of jobs that are running in `us-central1`.

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


def get_unconditional_inputs(self, num_samples=1):
    last_hidden_state = torch.zeros((num_samples, 1, self.config.text_encoder.hidden_size), device=self.device, dtype=self.dtype)
    attention_mask = torch.zeros((num_samples, 1), device=self.device, dtype=torch.long)
    return MusicgenUnconditionalInput(encoder_outputs=(last_hidden_state,), attention_mask=attention_mask, guidance_scale=1.0)

Helper function to get null inputs for unconditional generation, enabling the model to be used without the
feature extractor or tokenizer.

Args:
    num_samples (int, *optional*):
        Number of audio samples to unconditionally generate.
    max_new_tokens (int, *optional*):
        Number of tokens to generate for each sample. More tokens means longer audio samples, at the expense of
        longer inference (since more audio tokens need to be generated per sample).

Example:
```python
>>> from transformers import MusicgenForConditionalGeneration

>>> model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")

>>> # get the unconditional (or 'null') inputs for the model
>>> unconditional_inputs = model.get_unconditional_inputs(num_samples=1)
>>> audio_samples = model.generate(**unconditional_inputs, max_new_tokens=256)
```


class ParameterListState(object):

    def __init__(self, opening_bracket, newline, opening_column):
        self.opening_bracket = opening_bracket
        self.has_split_before_first_param = newline
        self.opening_column = opening_column
        self.parameters = opening_bracket.parameters
        self.split_before_closing_bracket = False

    @property
    def closing_bracket(self):
        return self.opening_bracket.matching_bracket

    @property
    def has_typed_return(self):
        return self.closing_bracket.next_token.value == '->'

    @property
    @lru_cache()
    def has_default_values(self):
        return any((param.has_default_value for param in self.parameters))

    @property
    @lru_cache()
    def ends_in_comma(self):
        if not self.parameters:
            return False
        return self.parameters[-1].last_token.next_token.value == ','

    @property
    @lru_cache()
    def last_token(self):
        token = self.opening_bracket.matching_bracket
        while not token.is_comment and token.next_token:
            token = token.next_token
        return token

    @lru_cache()
    def LastParamFitsOnLine(self, indent):
        """Return true if the last parameter fits on a single line."""
        if not self.has_typed_return:
            return False
        if not self.parameters:
            return True
        total_length = self.last_token.total_length
        last_param = self.parameters[-1].first_token
        total_length -= last_param.total_length - len(last_param.value)
        return total_length + indent <= style.Get('COLUMN_LIMIT')

    @lru_cache()
    def SplitBeforeClosingBracket(self, indent):
        """Return true if there's a split before the closing bracket."""
        if style.Get('DEDENT_CLOSING_BRACKETS'):
            return True
        if self.ends_in_comma:
            return True
        if not self.parameters:
            return False
        total_length = self.last_token.total_length
        last_param = self.parameters[-1].first_token
        total_length -= last_param.total_length - len(last_param.value)
        return total_length + indent > style.Get('COLUMN_LIMIT')

    def Clone(self):
        clone = ParameterListState(self.opening_bracket, self.has_split_before_first_param, self.opening_column)
        clone.split_before_closing_bracket = self.split_before_closing_bracket
        clone.parameters = [param.Clone() for param in self.parameters]
        return clone

    def __repr__(self):
        return '[opening_bracket::%s, has_split_before_first_param::%s, opening_column::%d]' % (self.opening_bracket, self.has_split_before_first_param, self.opening_column)

    def __eq__(self, other):
        return hash(self) == hash(other)

    def __ne__(self, other):
        return not self == other

    def __hash__(self, *args, **kwargs):
        return hash((self.opening_bracket, self.has_split_before_first_param, self.opening_column, (hash(param) for param in self.parameters)))

Maintains the state of function parameter list formatting decisions.

Attributes:
  opening_bracket: The opening bracket of the parameter list.
  closing_bracket: The closing bracket of the parameter list.
  has_typed_return: True if the function definition has a typed return.
  ends_in_comma: True if the parameter list ends in a comma.
  last_token: Returns the last token of the function declaration.
  has_default_values: True if the parameters have default values.
  has_split_before_first_param: Whether there is a newline before the first
    parameter.
  opening_column: The position of the opening parameter before a newline.
  parameters: A list of parameter objects (Parameter).
  split_before_closing_bracket: Split before the closing bracket. Sometimes
    needed if the indentation would collide.


class CalibrationResult:
    alpha: types.RealTensor
    beta: types.RealTensor
    volvol: types.RealTensor
    rho: types.RealTensor

Collection of calibrated SABR parameters.

For a review of the SABR model and the conventions used, please see the
docstring for `SABRModel`, or for `calibration` below.

Attributes:
  alpha: Rank-1 `Tensor` specifying the initial volatility levels.
  beta: Rank-1 `Tensor` specifying the exponents.
  volvol: Rank-1 `Tensor` specifying the vol-vol parameters.
  rho: Rank-1 `Tensor` specifying the correlations between the forward and
    the stochastic volatility.


def extract_tensors_from_dataset(dataset):
    iterator = get_iterator(dataset)
    inputs, targets, sample_weight = unpack_iterator_input(iterator)
    return (inputs, targets, sample_weight)

Extract a tuple of tensors `inputs, targets, sample_weight` from a dataset.

Args:
  dataset: Dataset instance.

Returns:
  Tuple of tensors `x, y, weights`. `y` and `weights` entry may be None.


def get_tensor(self, tensor_index, subgraph_index=0):
    return self._interpreter.GetTensor(tensor_index, subgraph_index)

Gets the value of the output tensor (get a copy).

If you wish to avoid the copy, use `tensor()`. This function cannot be used
to read intermediate results.

Args:
  tensor_index: Tensor index of tensor to get. This value can be gotten from
    the 'index' field in get_output_details.
  subgraph_index: Index of the subgraph to fetch the tensor. Default value
    is 0, which means to fetch from the primary subgraph.

Returns:
  a numpy array.


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

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

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


def forward(self, input_ids: Optional[torch.LongTensor]=None, attention_mask: Optional[torch.LongTensor]=None, token_type_ids: Optional[torch.LongTensor]=None, position_ids: Optional[torch.LongTensor]=None, head_mask: Optional[torch.LongTensor]=None, inputs_embeds: Optional[torch.FloatTensor]=None, visual_embeds: Optional[torch.FloatTensor]=None, visual_attention_mask: Optional[torch.LongTensor]=None, visual_token_type_ids: Optional[torch.LongTensor]=None, image_text_alignment: Optional[torch.LongTensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, labels: Optional[torch.LongTensor]=None) -> Union[Tuple[torch.Tensor], SequenceClassifierOutput]:
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    index_to_gather = attention_mask.sum(1) - 2
    outputs = self.visual_bert(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, visual_embeds=visual_embeds, visual_attention_mask=visual_attention_mask, visual_token_type_ids=visual_token_type_ids, image_text_alignment=image_text_alignment, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    sequence_output = outputs[0]
    index_to_gather = index_to_gather.unsqueeze(-1).unsqueeze(-1).expand(index_to_gather.size(0), 1, sequence_output.size(-1))
    pooled_output = torch.gather(sequence_output, 1, index_to_gather)
    pooled_output = self.dropout(pooled_output)
    logits = self.cls(pooled_output)
    reshaped_logits = logits.view(-1, self.num_labels)
    loss = None
    if labels is not None:
        loss_fct = nn.KLDivLoss(reduction='batchmean')
        log_softmax = nn.LogSoftmax(dim=-1)
        reshaped_logits = log_softmax(reshaped_logits)
        loss = loss_fct(reshaped_logits, labels.contiguous())
    if not return_dict:
        output = (reshaped_logits,) + outputs[2:]
        return (loss,) + output if loss is not None else output
    return SequenceClassifierOutput(loss=loss, logits=reshaped_logits, hidden_states=outputs.hidden_states, attentions=outputs.attentions)

visual_embeds (`torch.FloatTensor` of shape `(batch_size, visual_seq_length, visual_embedding_dim)`, *optional*):
    The embedded representation of the visual inputs, generally derived using using an object detector.
visual_attention_mask (`torch.FloatTensor` of shape `(batch_size, visual_seq_length)`, *optional*):
    Mask to avoid performing attention on visual embeddings. Mask values selected in `[0, 1]`:

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

    [What are attention masks?](../glossary#attention-mask)
visual_token_type_ids (`torch.LongTensor` of shape `(batch_size, visual_seq_length)`, *optional*):
    Segment token indices to indicate different portions of the visual embeds.

    [What are token type IDs?](../glossary#token-type-ids) The authors of VisualBERT set the
    *visual_token_type_ids* to *1* for all tokens.
image_text_alignment (`torch.LongTensor` of shape `(batch_size, visual_seq_length, alignment_number)`, *optional*):
    Image-Text alignment uses to decide the position IDs of the visual embeddings.
labels (`torch.LongTensor` of shape `(batch_size, total_sequence_length)`, *optional*):
    Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
    config.num_labels - 1]`. A KLDivLoss is computed between the labels and the returned logits.

Example:

```python
# Assumption: *get_visual_embeddings(image)* gets the visual embeddings of the image in the batch.
from transformers import AutoTokenizer, VisualBertForQuestionAnswering
import torch

tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
model = VisualBertForQuestionAnswering.from_pretrained("uclanlp/visualbert-vqa")

text = "Who is eating the apple?"
inputs = tokenizer(text, return_tensors="pt")
visual_embeds = get_visual_embeddings(image).unsqueeze(0)
visual_token_type_ids = torch.ones(visual_embeds.shape[:-1], dtype=torch.long)
visual_attention_mask = torch.ones(visual_embeds.shape[:-1], dtype=torch.float)

inputs.update(
    {
        "visual_embeds": visual_embeds,
        "visual_token_type_ids": visual_token_type_ids,
        "visual_attention_mask": visual_attention_mask,
    }
)

labels = torch.tensor([[0.0, 1.0]]).unsqueeze(0)  # Batch size 1, Num labels 2

outputs = model(**inputs, labels=labels)
loss = outputs.loss
scores = outputs.logits
```


def where(self, criteria: 'Builder') -> 'Builder':
    param_nodes = self._function_args_to_nodes(self.node, [criteria], unnested=True)
    return self._to_builder(_evaluation.WhereFunction(self.node.context, self.node, param_nodes))

The FHIRPath where() function.

Filters the collection of FHIR elements to meet criteria defined by a
builder expression.

Here is an example use:

>>> pat = <patient fhirpath builder>
>>> home_addresses = pat.address.where(pat.address.use = 'home')

Args:
  criteria: An expression builder containing the filtering logic.

Returns:
  An expression that contains the items that match the given criteria.


def register_dispatchable_type(cls):
    _api_dispatcher.register_dispatchable_type(cls)
    return cls

Class decorator that registers a type for use with type-based dispatch.

Should *not* be used with subclasses of `CompositeTensor` or `ExtensionType`
(which are automatically registered).

Note: this function is intended to support internal legacy use cases (such
as RaggedTensorValue), and will probably not be exposed as a public API.

Args:
  cls: The class to register.

Returns:
  `cls`.


def _recreate(self, proto, node_id, nodes):
    registered_class = registration.get_registered_class(proto.registered_name)
    if registered_class is None:
        registered_class = _BUILT_IN_REGISTRATIONS.get(proto.WhichOneof('kind'))
    dependencies = {}
    for key, dep_node_id in self._get_node_dependencies(proto).items():
        dependencies[key] = nodes[dep_node_id]
    if registered_class:
        obj = registered_class._deserialize_from_proto(proto=proto.serialized_user_proto, object_proto=proto, dependencies=dependencies, export_dir=self._export_dir, asset_file_def=self._asset_file_def, operation_attributes=self._operation_attributes)
        if isinstance(obj, base.Trackable):
            setter = type(obj)._add_trackable_child
        else:
            setter = setattr
        return (obj, setter)
    else:
        return self._recreate_default(proto, node_id, dependencies)

Creates a Python object from a SavedObject protocol buffer.

Args:
  proto: a SavedObject proto
  node_id: int, the index of this object in the SavedObjectGraph node list.
  nodes: dict mapping int node_ids -> created objects.

Returns:
  The recreated object, and the set-attribute function for reconnecting
  the trackable children.


def he_normal(seed=None):
    return VarianceScaling(scale=2.0, mode='fan_in', distribution='truncated_normal', seed=seed)

He normal initializer.

Initializers allow you to pre-specify an initialization strategy, encoded in
the Initializer object, without knowing the shape and dtype of the variable
being initialized.

It draws samples from a truncated normal distribution centered on 0 with
`stddev = sqrt(2 / fan_in)` where `fan_in` is the number of input units in the
weight tensor.

Examples:

>>> def make_variables(k, initializer):
...   return (tf.Variable(initializer(shape=[k, k], dtype=tf.float32)),
...           tf.Variable(initializer(shape=[k, k, k], dtype=tf.float32)))
>>> v1, v2 = make_variables(3, tf.initializers.he_normal())
>>> v1
<tf.Variable ... shape=(3, 3) ...
>>> v2
<tf.Variable ... shape=(3, 3, 3) ...
>>> make_variables(4, tf.initializers.RandomNormal())
(<tf.Variable ... shape=(4, 4) dtype=float32...
 <tf.Variable ... shape=(4, 4, 4) dtype=float32...

Args:
  seed: A Python integer. Used to seed the random generator.

Returns:
  A callable Initializer with `shape` and `dtype` arguments which generates a
  tensor.

References:
    [He et al., 2015](https://www.cv-foundation.org/openaccess/content_iccv_2015/html/He_Delving_Deep_into_ICCV_2015_paper.html) # pylint: disable=line-too-long
    ([pdf](https://www.cv-foundation.org/openaccess/content_iccv_2015/papers/He_Delving_Deep_into_ICCV_2015_paper.pdf))


class OpcodeWithArg(Opcode):
    __slots__ = ('arg', 'argval')

    def __init__(self, index, line, endline, col, endcol, arg, argval):
        super().__init__(index, line, endline, col, endcol)
        self.arg = arg
        self.argval = argval

    def __str__(self):
        out = f'{self.basic_str()} {self.argval}'
        if self.annotation:
            return f'{out}  # type: {self.annotation}'
        else:
            return out

An opcode with one argument.

Attributes:
  arg: The integer opcode argument read in from the bytecode
  argval: A decoded version of arg, performing the same steps the cpython
    interpreter does to convert arg into a python value.


def type_spec_from_value(value) -> TypeSpec:
    spec = _type_spec_from_value(value)
    if spec is not None:
        return spec
    try:
        tensor = tensor_conversion_registry.convert(value)
        spec = _type_spec_from_value(tensor)
        if spec is not None:
            return spec
    except (ValueError, TypeError) as e:
        logging.vlog(3, 'Failed to convert %r to tensor: %s' % (type(value).__name__, e))
    raise TypeError(f'Could not build a TypeSpec for {value} of unsupported type {type(value)}.')

Returns a `tf.TypeSpec` that represents the given `value`.

Examples:

  >>> tf.type_spec_from_value(tf.constant([1, 2, 3]))
  TensorSpec(shape=(3,), dtype=tf.int32, name=None)
  >>> tf.type_spec_from_value(np.array([4.0, 5.0], np.float64))
  TensorSpec(shape=(2,), dtype=tf.float64, name=None)
  >>> tf.type_spec_from_value(tf.ragged.constant([[1, 2], [3, 4, 5]]))
  RaggedTensorSpec(TensorShape([2, None]), tf.int32, 1, tf.int64)

  >>> example_input = tf.ragged.constant([[1, 2], [3]])
  >>> @tf.function(input_signature=[tf.type_spec_from_value(example_input)])
  ... def f(x):
  ...   return tf.reduce_sum(x, axis=1)

Args:
  value: A value that can be accepted or returned by TensorFlow APIs. Accepted
    types for `value` include `tf.Tensor`, any value that can be converted to
    `tf.Tensor` using `tf.convert_to_tensor`, and any subclass of
    `CompositeTensor` (such as `tf.RaggedTensor`).

Returns:
  A `TypeSpec` that is compatible with `value`.

Raises:
  TypeError: If a TypeSpec cannot be built for `value`, because its type
    is not supported.


def get_unpartitioned_shape(self, shape):
    shape = tensor_shape.as_shape(shape)
    dims = shape.as_list()
    if self._shard_dimension is None or self._number_of_partitions is None or (not dims):
        return None
    if dims[self._shard_dimension] is None:
        raise ValueError(f'Shape {shape.as_list()} must have a fixed size for dimension {self._shard_dimension} that is known. ')
    if self._number_of_partitions > 1:
        dims[self._shard_dimension] *= self._number_of_partitions
    return tensor_shape.as_shape(dims)

Returns the shape of an unpartitioned Tensor.

When given the shape of a 'sharded-size' Tensor, returns the shape
of the full shape of its unpartitioned Tensor.

Args:
  shape: The shape of the sharded Tensor.

Returns:
  The shape of the unpartitioned version of the Tensor.

Raises:
  ValueError: if shape has unknown sharded dimension


def forward(self, hidden_states: torch.Tensor) -> Tuple:
    router_probs, router_logits = self._compute_router_probabilities(hidden_states)
    expert_index = torch.argmax(router_probs, dim=-1)
    expert_index = torch.nn.functional.one_hot(expert_index, num_classes=self.num_experts)
    token_priority = torch.cumsum(expert_index, dim=-2)
    expert_capacity_mask = token_priority <= self.expert_capacity
    expert_index = expert_index * expert_capacity_mask
    router_probs = torch.max(router_probs, dim=-1).values.unsqueeze(-1)
    return (expert_index, router_probs, router_logits)

Generic forward function for every Router class. Each Router expects to have the same input hidden states
(`hidden_states`) corresponding to the hidden states for each token, the `expert_capacity` corresponding to the
number of tokens the Router will send to each expert, some Routers can send up to few tokens to each expert.

Each Router works as the following: it expects the hidden states for each token, gets the `router_probs` and
`router_logits` from the `router_weights`. This will assign for each token, the raw probability to be assigned
to an expert. Then each Router class will have to define its own `_compute_routing_instructions`.

Args:
    hidden_states (`torch.Tensor`) :
        [num_groups, tokens_per_group, hidden_dim] inputs to send to experts.
Returns:
    Tuple[`torch.Tensor`, `torch.Tensor`, `torch.Tensor`] Tuple containing the expert index, the router probs
    and the router logits. The router probabilities and logits are required to compute the loss.


def forward(self, inputs, expert_size):
    input_list = inputs.split(expert_size, dim=0)
    output_list = []
    for i in range(self.num_experts):
        output_list.append(F.linear(input_list[i], self.weight[i]))
    results = torch.cat(output_list, dim=0)
    return results

Forward pass of the GraniteMoeParallelExperts module.

Args:
    inputs (Tensor):
        Input tensor.
    expert_size:
        Expert size information.

Returns:
    Tensor: Output tensor.


def convert_graph_def(graph_def, pass_pipeline='tf-standard-pipeline', show_debug_info=False):
    return pywrap_mlir.import_graphdef(graph_def, pass_pipeline, show_debug_info)

Import a GraphDef and convert it to a textual MLIR module.

This API is only intended for inspecting the internals of TensorFlow and the
string returned is at the moment intended for debugging purposes.

Args:
  graph_def: An object of type graph_pb2.GraphDef or a textual proto
    representation of a valid GraphDef.
  pass_pipeline: A textual description of an MLIR Pass Pipeline to run on the
    module, see MLIR documentation for the [textual pass pipeline
    syntax](https://mlir.llvm.org/docs/PassManagement/#textual-pass-pipeline-specification).
  show_debug_info: Whether to include locations in the emitted textual form.

Returns:
  A textual representation of the MLIR module corresponding to the graphdef.

Raises:
  InvalidArgumentError: if graph_def is invalid or cannot be converted to
    MLIR.


def _SparseTensorDenseMatMulGrad(op: ops.Operation, grad):
    a_indices, a_values, a_shape = op.inputs[:3]
    b = op.inputs[3]
    adj_a = op.get_attr('adjoint_a')
    adj_b = op.get_attr('adjoint_b')
    a_type = a_values.dtype.base_dtype
    b_type = b.dtype.base_dtype
    if a_type != b_type:
        raise TypeError(f'SparseTensorDenseMatMul op received operands with different types: `{a_type}` and `{b_type}`.')
    b_grad = gen_sparse_ops.sparse_tensor_dense_mat_mul(a_indices, a_values, a_shape, grad, adjoint_a=not adj_a)
    if adj_b:
        b_grad = array_ops.matrix_transpose(b_grad, conjugate=True)
    rows = a_indices[:, 0]
    cols = a_indices[:, 1]
    parts_a = array_ops.gather(grad, rows if not adj_a else cols)
    parts_b = array_ops.gather(b if not adj_b else array_ops.transpose(b), cols if not adj_a else rows)
    if not adj_a and (not adj_b):
        a_values_grad = math_ops.matmul(array_ops.expand_dims(parts_a, -2), array_ops.expand_dims(parts_b, -2), adjoint_b=True)
    elif adj_a and (not adj_b):
        a_values_grad = math_ops.matmul(array_ops.expand_dims(parts_a, -1), array_ops.expand_dims(parts_b, -1), adjoint_a=True)
    elif not adj_a and adj_b:
        a_values_grad = math_ops.matmul(array_ops.expand_dims(parts_a, -2), array_ops.expand_dims(parts_b, -1))
    elif adj_a and adj_b:
        a_values_grad = math_ops.matmul(array_ops.expand_dims(parts_a, -1), array_ops.expand_dims(parts_b, -2), adjoint_a=True, adjoint_b=True)
    return (None, array_ops.squeeze(a_values_grad, axis=[-2, -1]), None, b_grad)

Gradients for the dense tensor in the SparseTensorDenseMatMul op.

Args:
  op: the SparseTensorDenseMatMul op
  grad: the incoming gradient

Returns:
  Gradient for each of the 4 input tensors:
    (sparse_indices, sparse_values, sparse_shape, dense_tensor)
  The gradients for indices and shape are None.

Raises:
  TypeError: When the two operands don't have the same type.


def _is_valid_cloud_label_value(label_value):
    return _VALID_CLOUD_LABEL_PATTERN.match(label_value)

Returns true if label_value is a valid cloud label string.

This function can return false in cases where the label value is valid.
However, it will not return true in a case where the lavel value is invalid.
This is because a stricter set of allowed characters is used in this
validator, because foreign language characters are not accepted.
Thus, this should not be used as a generic validator for all cloud labels.

See Also:
  https://cloud.google.com/compute/docs/labeling-resources

Args:
  label_value: The label value to validate.

Returns:
  True if the label value is a valid


def next_sample(uid):
    return next(_SHARED_SEQUENCES[uid])

Gets the next value from the generator `uid`.

To allow multiple generators to be used at the same time, we use `uid` to
get a specific one. A single generator would cause the validation to
overwrite the training generator.

Args:
    uid: int, generator identifier

Returns:
    The next value of generator `uid`.


def _wrap_and_check_metrics(self, metrics):
    if not isinstance(metrics, dict):
        metrics = {self.METRICS_NAME: metrics}
    outputs = {}
    for key, value in metrics.items():
        if isinstance(value, tuple):
            metric_val, metric_op = value
        else:
            metric_val = value.result()
            assert len(value.updates) == 1
            metric_op = value.updates[0]
        key = self._check_output_key(key, self.METRICS_NAME)
        key = self._prefix_key(key, self.METRICS_NAME)
        val_name = key + self._SEPARATOR_CHAR + self.METRIC_VALUE_SUFFIX
        op_name = key + self._SEPARATOR_CHAR + self.METRIC_UPDATE_SUFFIX
        if not isinstance(metric_val, tensor.Tensor):
            raise ValueError('{} output value must be a Tensor; got {}.'.format(key, metric_val))
        if not (tensor_util.is_tensor(metric_op) or isinstance(metric_op, ops.Operation)):
            raise ValueError('{} update_op must be a Tensor or Operation; got {}.'.format(key, metric_op))
        metric_op_tensor = metric_op
        if not isinstance(metric_op, tensor.Tensor):
            with ops.control_dependencies([metric_op]):
                metric_op_tensor = constant_op.constant([], name='metric_op_wrapper')
        outputs[val_name] = metric_val
        outputs[op_name] = metric_op_tensor
    return outputs

Handle the saving of metrics.

Metrics is either a tuple of (value, update_op), or a dict of such tuples.
Here, we separate out the tuples and create a dict with names to tensors.

Args:
  metrics: Dict of metric results keyed by name.
    The values of the dict can be one of the following:
    (1) instance of `Metric` class.
    (2) (metric_value, update_op) tuples, or a single tuple.
    metric_value must be a Tensor, and update_op must be a Tensor or Op.

Returns:
  dict of output_names to tensors

Raises:
  ValueError: if the dict key is not a string, or the metric values or ops
    are not tensors.


def _composition_must_be_self_adjoint(operators):
    if len(operators) == 1 and operators[0].is_self_adjoint:
        return True
    if linear_operator_util.is_aat_form(operators):
        return True
    return False

Runs some checks to see if composition operators must be SA.

Args:
  operators: List of LinearOperators.

Returns:
  True if the composition must be SA. False if it is not SA OR if we did not
    determine whether the composition is SA.


def call(self, x):
    return ops.rms_normalization(x, scale=self.scale, axis=self.axis, epsilon=self.epsilon)

Applies RMS normalization to the input tensor.

Args:
    x: Input tensor of shape (batch_size, input_dim).

Returns:
    The RMS-normalized tensor of the same shape (batch_size, input_dim),
    scaled by the learned `scale` parameter.


def put(self, item):
    with self._not_full:
        if self._closed:
            raise QueueClosedError()
        if self._maxsize > 0:
            while len(self._queue) == self._maxsize:
                self._not_full.wait()
                if self._closed:
                    raise QueueClosedError()
        self._queue.append(item)
        self._not_empty.notify()

Put an item into the queue.

If the queue is closed, fails immediately.

If the queue is full, blocks until space is available or until the queue
is closed by a call to close(), at which point this call fails.

Args:
  item: an item to add to the queue

Raises:
  QueueClosedError: if insertion failed because the queue is closed


def CreateShapeFromDtypeAndTuple(dtype, shape_tuple):
    element_type = types_.MAP_DTYPE_TO_RECORD[str(dtype)].primitive_type
    return Shape(element_type, shape_tuple)

Create a shape from a Numpy dtype and a sequence of nonnegative integers.

Args:
  dtype: a numpy dtype, e.g. np.dtype('int32').
  shape_tuple: a sequence of nonnegative integers.

Returns:
  A Shape object.


def start_range(self, line, membership):
    last = self._transitions[-1] if self._transitions else -1
    if line < last:
        raise ValueError('Line number less than previous start_range() call.')
    previous = len(self._transitions) % 2 == 1
    if membership == previous:
        return
    elif line == last:
        self._transitions.pop()
    else:
        self._transitions.append(line)

Start a range of lines that are either included/excluded from the set.

Args:
  line: A line number.
  membership: If True, lines >= line are included in the set (starting a
    range), otherwise they are excluded (ending a range).

Raises:
  ValueError: if line is less than that of a previous call to start_range().


def unsqueeze(self, dim: int) -> Rigid:
    if dim >= len(self.shape):
        raise ValueError('Invalid dimension')
    rots = self._rots.unsqueeze(dim)
    trans = self._trans.unsqueeze(dim if dim >= 0 else dim - 1)
    return Rigid(rots, trans)

Analogous to torch.unsqueeze. The dimension is relative to the shared dimensions of the rotation/translation.

Args:
    dim: A positive or negative dimension index.
Returns:
    The unsqueezed transformation.


class PerceiverMaskedLMOutput(ModelOutput):
    loss: Optional[torch.FloatTensor] = None
    logits: Optional[torch.FloatTensor] = None
    hidden_states: Optional[Tuple[torch.FloatTensor]] = None
    attentions: Optional[Tuple[torch.FloatTensor]] = None
    cross_attentions: Optional[Tuple[torch.FloatTensor]] = None

Base class for Perceiver's masked language model outputs.

Args:
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Masked language modeling (MLM) loss.
    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).
    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(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, num_latents,
        num_latents)`. Attentions weights after the attention softmax, used to compute the weighted average in the
        self-attention heads.
    cross_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 of the decoder's cross-attention layer, after the attention softmax,
        used to compute the weighted average in the cross-attention heads.


class AlignConfig(PretrainedConfig):
    model_type = 'align'
    sub_configs = {'text_config': AlignTextConfig, 'vision_config': AlignVisionConfig}

    def __init__(self, text_config=None, vision_config=None, projection_dim=640, temperature_init_value=1.0, initializer_range=0.02, **kwargs):
        super().__init__(**kwargs)
        if text_config is None:
            text_config = {}
            logger.info('text_config is None. Initializing the AlignTextConfig with default values.')
        if vision_config is None:
            vision_config = {}
            logger.info('vision_config is None. Initializing the AlignVisionConfig with default values.')
        self.text_config = AlignTextConfig(**text_config)
        self.vision_config = AlignVisionConfig(**vision_config)
        self.projection_dim = projection_dim
        self.temperature_init_value = temperature_init_value
        self.initializer_range = initializer_range

    @classmethod
    def from_text_vision_configs(cls, text_config: AlignTextConfig, vision_config: AlignVisionConfig, **kwargs):
        """
        Instantiate a [`AlignConfig`] (or a derived class) from align text model configuration and align vision model
        configuration.

        Returns:
            [`AlignConfig`]: An instance of a configuration object
        """
        return cls(text_config=text_config.to_dict(), vision_config=vision_config.to_dict(), **kwargs)

[`AlignConfig`] is the configuration class to store the configuration of a [`AlignModel`]. It is used to
instantiate a ALIGN model according to the specified arguments, defining the text model and vision model configs.
Instantiating a configuration with the defaults will yield a similar configuration to that of the ALIGN
[kakaobrain/align-base](https://huggingface.co/kakaobrain/align-base) architecture.

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

Args:
    text_config (`dict`, *optional*):
        Dictionary of configuration options used to initialize [`AlignTextConfig`].
    vision_config (`dict`, *optional*):
        Dictionary of configuration options used to initialize [`AlignVisionConfig`].
    projection_dim (`int`, *optional*, defaults to 640):
        Dimensionality of text and vision projection layers.
    temperature_init_value (`float`, *optional*, defaults to 1.0):
        The initial value of the *temperature* parameter. Default is used as per the original ALIGN implementation.
    initializer_range (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    kwargs (*optional*):
        Dictionary of keyword arguments.

Example:

```python
>>> from transformers import AlignConfig, AlignModel

>>> # Initializing a AlignConfig with kakaobrain/align-base style configuration
>>> configuration = AlignConfig()

>>> # Initializing a AlignModel (with random weights) from the kakaobrain/align-base style configuration
>>> model = AlignModel(configuration)

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

>>> # We can also initialize a AlignConfig from a AlignTextConfig and a AlignVisionConfig
>>> from transformers import AlignTextConfig, AlignVisionConfig

>>> # Initializing ALIGN Text and Vision configurations
>>> config_text = AlignTextConfig()
>>> config_vision = AlignVisionConfig()

>>> config = AlignConfig.from_text_vision_configs(config_text, config_vision)
```


def forward(self, pixel_values: torch.FloatTensor, pixel_mask: Optional[torch.LongTensor]=None, decoder_attention_mask: Optional[torch.FloatTensor]=None, encoder_outputs: Optional[torch.FloatTensor]=None, inputs_embeds: Optional[torch.FloatTensor]=None, decoder_inputs_embeds: Optional[torch.FloatTensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple[torch.FloatTensor], DetrModelOutput]:
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    batch_size, num_channels, height, width = pixel_values.shape
    device = pixel_values.device
    if pixel_mask is None:
        pixel_mask = torch.ones((batch_size, height, width), device=device)
    features, object_queries_list = self.backbone(pixel_values, pixel_mask)
    feature_map, mask = features[-1]
    if mask is None:
        raise ValueError('Backbone does not return downsampled pixel mask')
    projected_feature_map = self.input_projection(feature_map)
    flattened_features = projected_feature_map.flatten(2).permute(0, 2, 1)
    object_queries = object_queries_list[-1].flatten(2).permute(0, 2, 1)
    flattened_mask = mask.flatten(1)
    if encoder_outputs is None:
        encoder_outputs = self.encoder(inputs_embeds=flattened_features, attention_mask=flattened_mask, object_queries=object_queries, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    elif return_dict and (not isinstance(encoder_outputs, BaseModelOutput)):
        encoder_outputs = BaseModelOutput(last_hidden_state=encoder_outputs[0], hidden_states=encoder_outputs[1] if len(encoder_outputs) > 1 else None, attentions=encoder_outputs[2] if len(encoder_outputs) > 2 else None)
    query_position_embeddings = self.query_position_embeddings.weight.unsqueeze(0).repeat(batch_size, 1, 1)
    queries = torch.zeros_like(query_position_embeddings)
    decoder_outputs = self.decoder(inputs_embeds=queries, attention_mask=None, object_queries=object_queries, query_position_embeddings=query_position_embeddings, encoder_hidden_states=encoder_outputs[0], encoder_attention_mask=flattened_mask, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    if not return_dict:
        return decoder_outputs + encoder_outputs
    return DetrModelOutput(last_hidden_state=decoder_outputs.last_hidden_state, decoder_hidden_states=decoder_outputs.hidden_states, decoder_attentions=decoder_outputs.attentions, cross_attentions=decoder_outputs.cross_attentions, encoder_last_hidden_state=encoder_outputs.last_hidden_state, encoder_hidden_states=encoder_outputs.hidden_states, encoder_attentions=encoder_outputs.attentions, intermediate_hidden_states=decoder_outputs.intermediate_hidden_states)

decoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, num_queries)`, *optional*):
    Not used by default. Can be used to mask object queries.
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
    Optionally, instead of passing the flattened feature map (output of the backbone + projection layer), you
    can choose to directly pass a flattened representation of an image.
decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, num_queries, hidden_size)`, *optional*):
    Optionally, instead of initializing the queries with a tensor of zeros, you can choose to directly pass an
    embedded representation.

Examples:

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

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

>>> image_processor = AutoImageProcessor.from_pretrained("facebook/detr-resnet-50")
>>> model = DetrModel.from_pretrained("facebook/detr-resnet-50")

>>> # prepare image for the model
>>> inputs = image_processor(images=image, return_tensors="pt")

>>> # forward pass
>>> outputs = model(**inputs)

>>> # the last hidden states are the final query embeddings of the Transformer decoder
>>> # these are of shape (batch_size, num_queries, hidden_size)
>>> last_hidden_states = outputs.last_hidden_state
>>> list(last_hidden_states.shape)
[1, 100, 256]
```


def DumpPyTree(tree, target_stream=sys.stdout):
    dumper = PyTreeDumper(target_stream)
    dumper.Visit(tree)

Convenience function for dumping a given pytree.

This function presents a very minimal interface. For more configurability (for
example, controlling how specific node types are displayed), use PyTreeDumper
directly.

Arguments:
  tree: the tree to dump.
  target_stream: the stream to dump the tree to. A file-like object. By
    default will dump into stdout.


def apply(self, transform, pvalueish=None, label=None):
    if isinstance(transform, ptransform._NamedPTransform):
        return self.apply(transform.transform, pvalueish, label or transform.label)
    if not isinstance(transform, ptransform.PTransform):
        raise TypeError('Expected a PTransform object, got %s' % transform)
    if label:
        old_label, transform.label = (transform.label, label)
        try:
            return self.apply(transform, pvalueish)
        finally:
            transform.label = old_label
    if self._current_transform() is self._root_transform():
        alter_label_if_ipython(transform, pvalueish)
    full_label = '/'.join([self._current_transform().full_label, transform.label]).lstrip('/')
    if full_label in self.applied_labels:
        auto_unique_labels = self._options.view_as(StandardOptions).auto_unique_labels
        if auto_unique_labels:
            logging.warning('Using --auto_unique_labels could cause data loss when updating a pipeline or reloading the job state. This is not recommended for streaming jobs.')
            unique_label = self._generate_unique_label(transform)
            return self.apply(transform, pvalueish, unique_label)
        else:
            raise RuntimeError('A transform with label "%s" already exists in the pipeline. To apply a transform with a specified label, write pvalue | "label" >> transform or use the option "auto_unique_labels" to automatically generate unique transform labels. Note "auto_unique_labels" could cause data loss when updating a pipeline or reloading the job state. This is not recommended for streaming jobs.' % full_label)
    self.applied_labels.add(full_label)
    if pvalueish is None:
        full_label = self._current_transform().full_label
        raise TypeCheckError(f'Transform "{full_label}" was applied to the output of an object of type None.')
    pvalueish, inputs = transform._extract_input_pvalues(pvalueish)
    try:
        if not isinstance(inputs, dict):
            inputs = {str(ix): input for ix, input in enumerate(inputs)}
    except TypeError:
        raise NotImplementedError('Unable to extract PValue inputs from %s; either %s does not accept inputs of this format, or it does not properly override _extract_input_pvalues' % (pvalueish, transform))
    for t, leaf_input in inputs.items():
        if not isinstance(leaf_input, pvalue.PValue) or not isinstance(t, str):
            raise NotImplementedError('%s does not properly override _extract_input_pvalues, returned %s from %s' % (transform, inputs, pvalueish))
    current = AppliedPTransform(self._current_transform(), transform, full_label, inputs, None, annotations=self._current_annotations())
    self._current_transform().add_part(current)
    try:
        self.transforms_stack.append(current)
        type_options = self._options.view_as(TypeOptions)
        if type_options.pipeline_type_check:
            transform.type_check_inputs(pvalueish)
        if isinstance(pvalueish, pvalue.PBegin) and isinstance(transform, ParDo):
            full_label = self._current_transform().full_label
            raise TypeCheckError(f"Transform '{full_label}' expects a PCollection as input. Got a PBegin/Pipeline instead.")
        self._assert_not_applying_PDone(pvalueish, transform)
        pvalueish_result = self.runner.apply(transform, pvalueish, self._options)
        if type_options is not None and type_options.pipeline_type_check:
            transform.type_check_outputs(pvalueish_result)
        for tag, result in ptransform.get_named_nested_pvalues(pvalueish_result):
            assert isinstance(result, (pvalue.PValue, pvalue.DoOutputsTuple))
            if result.producer is None:
                result.producer = current
            self._infer_result_type(transform, tuple(inputs.values()), result)
            assert isinstance(result.producer.inputs, tuple)
            if isinstance(result, pvalue.DoOutputsTuple):
                current.add_output(result, result._main_tag)
                continue
            base = tag
            counter = 0
            while tag in current.outputs:
                counter += 1
                tag = '%s_%d' % (base, counter)
            current.add_output(result, tag)
        if type_options is not None and type_options.type_check_strictness == 'ALL_REQUIRED' and (transform.get_type_hints().output_types is None):
            ptransform_name = '%s(%s)' % (transform.__class__.__name__, full_label)
            raise TypeCheckError('Pipeline type checking is enabled, however no output type-hint was found for the PTransform %s' % ptransform_name)
    finally:
        self.transforms_stack.pop()
    return pvalueish_result

Applies a custom transform using the pvalueish specified.

Args:
  transform (~apache_beam.transforms.ptransform.PTransform): the
    :class:`~apache_beam.transforms.ptransform.PTransform` to apply.
  pvalueish (~apache_beam.pvalue.PCollection): the input for the
    :class:`~apache_beam.transforms.ptransform.PTransform` (typically a
    :class:`~apache_beam.pvalue.PCollection`).
  label (str): label of the
    :class:`~apache_beam.transforms.ptransform.PTransform`.

Raises:
  TypeError: if the transform object extracted from the
    argument list is not a
    :class:`~apache_beam.transforms.ptransform.PTransform`.
  RuntimeError: if the transform object was already applied to
    this pipeline and needs to be cloned in order to apply again.


def _get_metrics_from_layers(layers):
    metrics = []
    layers = layer_utils.filter_empty_layer_containers(layers)
    for layer in layers:
        if isinstance(layer, Model):
            metrics.extend(layer._metrics)
            metrics.extend(_get_metrics_from_layers(layer.layers))
        else:
            metrics.extend(layer.metrics)
    return metrics

Returns list of metrics from the given layers.

This will not include the `compile` metrics of a model layer.

Args:
  layers: List of layers.

Returns:
  List of metrics.


def get_canonical_import(import_set):
    import_list = sorted(import_set, key=lambda imp_and_priority: (-imp_and_priority[1], imp_and_priority[0]))
    return import_list[0][0]

Obtain one single import from a set of possible sources of a symbol.

One symbol might come from multiple places as it is being imported and
reexported. To simplify API changes, we always use the same import for the
same module, and give preference based on higher priority and alphabetical
ordering.

Args:
  import_set: (set) Imports providing the same symbol. This is a set of tuples
    in the form (import, priority). We want to pick an import with highest
    priority.

Returns:
  A module name to import


def _convert_date(self, value):
    try:
        result = parser.parse(value)
        if not result.tzinfo:
            result = ZoneInfo(self._timezone).localize(result)
        if ':' in value:
            return result.strftime('%Y-%m-%dT%H:%M:%S.000%z')
        else:
            return result.strftime('%Y-%m-%d')
    except:
        return None

Converts dates into a Bulkdozer specific format to be written back to the Feed.

Args:
  value: String representation of the date.

Returns:
  Bulkdozer string representation of a date. Returns null if the value
  cannot be parsed into a date.


def assign(self: EventSetOrNode, **others: EventSetOrNode) -> EventSetOrNode:
    from temporian.core.operators.glue import assign
    return assign(self, **others)

Assign new features to an [EventSet][temporian.EventSet].

If the name provided already exists on the EventSet, the feature is
overriden.

Usage example:
    ```python
    >>> a = tp.event_set(
    ...     timestamps=[1, 2],
    ...     features={'A': [1, 2]},
    ... )
    >>> b = tp.event_set(
    ...     timestamps=[1, 2],
    ...     features={'B': [3, 4]},
    ...     same_sampling_as=a,
    ... )
    >>> ab = a.assign(new_name=b)
    >>> ab
    indexes: []
    features: [('A', int64), ('new_name', int64)]
    events:
        (2 events):
            timestamps: [1. 2.]
            'A': [1 2]
            'new_name': [3 4]
    ...
    >>> ab = a.assign(B=b, B2=b['B'] * 2)
    >>> ab
    indexes: []
    features: [('A', int64), ('B', int64), ('B2', int64)]
    events:
        (2 events):
            timestamps: [1. 2.]
            'A': [1 2]
            'B': [3 4]
            'B2': [6 8]
    ...

    ```

Args:
    **others: The argument name is going to be used as the new feature
        name. The EventSets need to have a single feature

Returns:
    EventSet with the added feature.


def metadata(self) -> Dict[str, Any]:
    return self._metadata

Metadata of this field.

Metadata is defined as a dict type, so we can add multiple annotations
to a field.

  userdata = field.metadata.get('userdata', None):

Returns:
  Metadata of this field as a dict.


def resize_for_vision_encoder(self, image: np.ndarray, vision_encoder_max_size: int, resample: PILImageResampling=PILImageResampling.LANCZOS, data_format: Optional[Union[str, ChannelDimension]]=None, input_data_format: Optional[Union[str, ChannelDimension]]=None):
    height, width = get_image_size(image, channel_dim=input_data_format)
    aspect_ratio = width / height
    if width >= height:
        width = math.ceil(width / vision_encoder_max_size) * vision_encoder_max_size
        height = int(width / aspect_ratio)
        height = math.ceil(height / vision_encoder_max_size) * vision_encoder_max_size
    elif height > width:
        height = math.ceil(height / vision_encoder_max_size) * vision_encoder_max_size
        width = int(height * aspect_ratio)
        width = math.ceil(width / vision_encoder_max_size) * vision_encoder_max_size
    new_size = {'height': height, 'width': width}
    return self.resize(image, size=new_size, resample=resample, input_data_format=input_data_format, data_format=data_format)

Resize images to be multiples of `vision_encoder_max_size` while preserving the aspect ratio.
Args:
    image (`np.ndarray`):
        Images to resize.
    vision_encoder_max_size (`int`):
        Maximum size of the output image. If the image is larger than this size, it will be split into
        patches of this size, and the original image will be concatenated with the patches, resized to max_size.
    resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.LANCZOS`):
        Resampling filter to use when resizing the image.
    data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format of the output image. If not provided, it will be the same as the input image.
    input_data_format (`ChannelDimension` or `str`, *optional*):
        The channel dimension format of the input image. If not provided, it will be inferred


def stack_residual_blocks_v2(x, filters, blocks, stride1=2, name=None):
    x = residual_block_v2(x, filters, conv_shortcut=True, name=name + '_block1')
    for i in range(2, blocks):
        x = residual_block_v2(x, filters, name=name + '_block' + str(i))
    x = residual_block_v2(x, filters, stride=stride1, name=name + '_block' + str(blocks))
    return x

A set of stacked residual blocks.

Args:
    x: Input tensor.
    filters: Number of filters in the bottleneck layer in a block.
    blocks: Number of blocks in the stacked blocks.
    stride1: Stride of the first layer in the first block. Defaults to `2`.
    name: Stack label.

Returns:
    Output tensor for the stacked blocks.


def _parse_logline_timestamp(t):
    date, time = t.split(' ')
    month, day = date.split('-')
    h, m, s = time.split(':')
    s, ms = s.split('.')
    return (month, day, h, m, s, ms)

Parses a logline timestamp into a tuple.

Args:
  t: Timestamp in logline format.

Returns:
  An iterable of date and time elements in the order of month, day, hour,
  minute, second, microsecond.


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

Args:
    hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
    attention_mask (`torch.FloatTensor`): attention mask of size
        `(batch, 1, target_len, source_len)` where padding elements are indicated by very large negative
        values.
    object_queries (`torch.FloatTensor`, *optional*):
        Object queries (also called content embeddings), to be added to the hidden 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.


def _prefix_output_keys(self, output_dict, output_name):
    new_outputs = {}
    for key, val in output_dict.items():
        key = self._prefix_key(key, output_name)
        new_outputs[key] = val
    return new_outputs

Prepend output_name to the output_dict keys if it doesn't exist.

This produces predictable prefixes for the pre-determined outputs
of SupervisedOutput.

Args:
  output_dict: dict of string to Tensor, assumed valid.
  output_name: prefix string to prepend to existing keys.

Returns:
  dict with updated keys and existing values.


def If(cond, inputs, then_branch, else_branch, name=None):
    if isinstance(then_branch, function._DefinedFunction):
        tlist = [_.type for _ in then_branch.definition.signature.output_arg]
        return gen_functional_ops._if(cond, inputs, tlist, then_branch, else_branch, name=name)
    then_out = then_branch.structured_outputs
    else_out = else_branch.structured_outputs
    nest.assert_same_structure(then_out, else_out, expand_composites=True)
    tlist = nest.flatten(then_branch.output_dtypes)
    ret = gen_functional_ops._if(cond, inputs, tlist, then_branch, else_branch, name=name)
    return nest.pack_sequence_as(then_out, ret, expand_composites=True)

output = Cond(inputs) ?

then_branch(inputs) : else_branch(inputs).

Args:
  cond: A `Tensor`. A scalar. If the scalar is not a boolean, the scalar is
    converted to a boolean according to the following rule: if the scalar is a
      numerical value, non-zero means True and zero means False; if the scalar
      is a string, non-empty means True and empty means False.
  inputs: A list of input tensors.
  then_branch: A function takes 'inputs' and returns a list of tensors, whose
    types are the same as what else_branch returns.
  else_branch: A function takes 'inputs' and returns a list of tensors. whose
    types are the same as what then_branch returns.
  name: A name for the operation (optional).

Returns:
  A list of tensors returned by either then_branch(inputs)
  or else_branch(inputs).


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

Create a mask from the two sequences passed to be used in a sequence-pair classification task. A Funnel
Transformer sequence pair mask has the following format:

```
2 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
| first sequence    | second sequence |
```

If `token_ids_1` is `None`, this method only returns the first portion of the mask (0s).

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

Returns:
    `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).