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

Patch specific fields in the specified model.

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


class GroupViTVisionConfig(PretrainedConfig):
    model_type = 'groupvit_vision_model'
    base_config_key = 'vision_config'

    def __init__(self, hidden_size=384, intermediate_size=1536, depths=[6, 3, 3], num_hidden_layers=12, num_group_tokens=[64, 8, 0], num_output_groups=[64, 8, 8], num_attention_heads=6, image_size=224, patch_size=16, num_channels=3, hidden_act='gelu', layer_norm_eps=1e-05, dropout=0.0, attention_dropout=0.0, initializer_range=0.02, initializer_factor=1.0, assign_eps=1.0, assign_mlp_ratio=[0.5, 4], **kwargs):
        super().__init__(**kwargs)
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.depths = depths
        if num_hidden_layers != sum(depths):
            logger.warning(f'Manually setting num_hidden_layers to {num_hidden_layers}, but we expect num_hidden_layers = sum(depth) = {sum(depths)}')
        self.num_hidden_layers = num_hidden_layers
        self.num_group_tokens = num_group_tokens
        self.num_output_groups = num_output_groups
        self.num_attention_heads = num_attention_heads
        self.image_size = image_size
        self.patch_size = patch_size
        self.num_channels = num_channels
        self.hidden_act = hidden_act
        self.layer_norm_eps = layer_norm_eps
        self.dropout = dropout
        self.attention_dropout = attention_dropout
        self.initializer_range = initializer_range
        self.initializer_factor = initializer_factor
        self.assign_eps = assign_eps
        self.assign_mlp_ratio = assign_mlp_ratio

This is the configuration class to store the configuration of a [`GroupViTVisionModel`]. It is used to instantiate
an GroupViT 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 GroupViT
[nvidia/groupvit-gcc-yfcc](https://huggingface.co/nvidia/groupvit-gcc-yfcc) architecture.

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

Args:
    hidden_size (`int`, *optional*, defaults to 384):
        Dimensionality of the encoder layers and the pooler layer.
    intermediate_size (`int`, *optional*, defaults to 1536):
        Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
    depths (`List[int]`, *optional*, defaults to [6, 3, 3]):
        The number of layers in each encoder block.
    num_group_tokens (`List[int]`, *optional*, defaults to [64, 8, 0]):
        The number of group tokens for each stage.
    num_output_groups (`List[int]`, *optional*, defaults to [64, 8, 8]):
        The number of output groups for each stage, 0 means no group.
    num_attention_heads (`int`, *optional*, defaults to 6):
        Number of attention heads for each attention layer in the Transformer encoder.
    image_size (`int`, *optional*, defaults to 224):
        The size (resolution) of each image.
    patch_size (`int`, *optional*, defaults to 16):
        The size (resolution) of each patch.
    hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
        The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
        `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
    layer_norm_eps (`float`, *optional*, defaults to 1e-5):
        The epsilon used by the layer normalization layers.
    dropout (`float`, *optional*, defaults to 0.0):
        The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
    attention_dropout (`float`, *optional*, defaults to 0.0):
        The dropout ratio for the attention probabilities.
    initializer_range (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    initializer_factor (`float`, *optional*, defaults to 1.0):
        A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
        testing).

Example:

```python
>>> from transformers import GroupViTVisionConfig, GroupViTVisionModel

>>> # Initializing a GroupViTVisionModel with nvidia/groupvit-gcc-yfcc style configuration
>>> configuration = GroupViTVisionConfig()

>>> model = GroupViTVisionModel(configuration)

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


def copy_paths(self, src_dest_pairs):
    if not src_dest_pairs:
        return []
    results = []
    for src_path, dest_path in src_dest_pairs:
        if src_path.endswith('/') and dest_path.endswith('/'):
            try:
                results += self.copy_tree(src_path, dest_path)
            except messages.S3ClientError as err:
                results.append((src_path, dest_path, err))
        elif not src_path.endswith('/') and (not dest_path.endswith('/')):
            src_bucket, src_key = parse_s3_path(src_path)
            dest_bucket, dest_key = parse_s3_path(dest_path)
            request = messages.CopyRequest(src_bucket, src_key, dest_bucket, dest_key)
            try:
                self.client.copy(request)
                results.append((src_path, dest_path, None))
            except messages.S3ClientError as err:
                results.append((src_path, dest_path, err))
        else:
            e = messages.S3ClientError("Can't copy mismatched paths (one directory, one non-directory):" + ' %s, %s' % (src_path, dest_path), 400)
            results.append((src_path, dest_path, e))
    return results

Copies the given S3 objects from src to dest. This can handle directory
or file paths.

Args:
  src_dest_pairs: list of (src, dest) tuples of s3://<bucket>/<name> file
                  paths to copy from src to dest
Returns: List of tuples of (src, dest, exception) in the same order as the
        src_dest_pairs argument, where exception is None if the operation
        succeeded or the relevant exception if the operation failed.


def forward(self, input_ids: Optional[torch.LongTensor]=None, attention_mask: Optional[torch.Tensor]=None, encoder_hidden_states: Optional[torch.FloatTensor]=None, encoder_attention_mask: Optional[torch.LongTensor]=None, head_mask: Optional[torch.Tensor]=None, cross_attn_head_mask: Optional[torch.Tensor]=None, past_key_values: Optional[Tuple[Tuple[torch.FloatTensor]]]=None, inputs_embeds: Optional[torch.FloatTensor]=None, labels: Optional[torch.LongTensor]=None, use_cache: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple, CausalLMOutputWithCrossAttentions]:
    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
    outputs = self.model.decoder(input_ids=input_ids, attention_mask=attention_mask, encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=encoder_attention_mask, head_mask=head_mask, cross_attn_head_mask=cross_attn_head_mask, past_key_values=past_key_values, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    logits = self.output_projection(outputs[0])
    loss = None
    if labels is not None:
        loss_fct = CrossEntropyLoss()
        loss = loss_fct(logits.view(-1, self.config.vocab_size), labels.view(-1))
    if not return_dict:
        output = (logits,) + outputs[1:]
        return (loss,) + output if loss is not None else output
    return CausalLMOutputWithCrossAttentions(loss=loss, logits=logits, past_key_values=outputs.past_key_values, hidden_states=outputs.hidden_states, attentions=outputs.attentions, cross_attentions=outputs.cross_attentions)

cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*):
    Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`:

    - 1 indicates the head is **not masked**,
    - 0 indicates the head is **masked**.
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
    config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
    (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

Example:

```python
>>> from transformers import (
...     TrOCRConfig,
...     TrOCRProcessor,
...     TrOCRForCausalLM,
...     ViTConfig,
...     ViTModel,
...     VisionEncoderDecoderModel,
... )
>>> import requests
>>> from PIL import Image

>>> # TrOCR is a decoder model and should be used within a VisionEncoderDecoderModel
>>> # init vision2text model with random weights
>>> encoder = ViTModel(ViTConfig())
>>> decoder = TrOCRForCausalLM(TrOCRConfig())
>>> model = VisionEncoderDecoderModel(encoder=encoder, decoder=decoder)

>>> # If you want to start from the pretrained model, load the checkpoint with `VisionEncoderDecoderModel`
>>> processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
>>> model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")

>>> # load image from the IAM dataset
>>> url = "https://fki.tic.heia-fr.ch/static/img/a01-122-02.jpg"
>>> image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
>>> pixel_values = processor(image, return_tensors="pt").pixel_values
>>> text = "industry, ' Mr. Brown commented icily. ' Let us have a"

>>> # training
>>> model.config.decoder_start_token_id = processor.tokenizer.eos_token_id
>>> model.config.pad_token_id = processor.tokenizer.pad_token_id
>>> model.config.vocab_size = model.config.decoder.vocab_size

>>> labels = processor.tokenizer(text, return_tensors="pt").input_ids
>>> outputs = model(pixel_values, labels=labels)
>>> loss = outputs.loss
>>> round(loss.item(), 2)
5.30

>>> # inference
>>> generated_ids = model.generate(pixel_values)
>>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
>>> generated_text
'industry, " Mr. Brown commented icily. " Let us have a'
```


class NewModelConfig(PretrainedConfig):
    model_type = 'new_model'
    keys_to_ignore_at_inference = ['past_key_values']
    base_model_tp_plan = {'layers.*.self_attn.q_proj': 'colwise', 'layers.*.self_attn.k_proj': 'colwise', 'layers.*.self_attn.v_proj': 'colwise', 'layers.*.self_attn.o_proj': 'rowwise', 'layers.*.mlp.gate_proj': 'colwise', 'layers.*.mlp.up_proj': 'colwise', 'layers.*.mlp.down_proj': 'rowwise'}
    base_model_pp_plan = {'embed_tokens': (['input_ids'], ['inputs_embeds']), 'layers': (['hidden_states', 'attention_mask'], ['hidden_states']), 'norm': (['hidden_states'], ['hidden_states'])}

    def __init__(self, vocab_size=256030, hidden_size=64, intermediate_size=90, num_hidden_layers=28, num_attention_heads=16, num_key_value_heads=16, head_dim=256, hidden_act='gelu_pytorch_tanh', hidden_activation=None, max_position_embeddings=1500, initializer_range=0.02, rms_norm_eps=1e-06, use_cache=True, pad_token_id=0, eos_token_id=1, bos_token_id=2, tie_word_embeddings=True, rope_theta=10000.0, attention_bias=False, attention_dropout=0.0, **kwargs):
        super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, tie_word_embeddings=tie_word_embeddings, **kwargs)
        self.vocab_size = vocab_size
        self.max_position_embeddings = max_position_embeddings
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.head_dim = head_dim
        self.num_key_value_heads = num_key_value_heads
        self.hidden_act = hidden_act
        self.hidden_activation = hidden_activation
        self.initializer_range = initializer_range
        self.rms_norm_eps = rms_norm_eps
        self.use_cache = use_cache
        self.rope_theta = rope_theta
        self.attention_bias = attention_bias
        self.attention_dropout = attention_dropout

    @property
    def num_heads(self):
        return self.num_attention_heads

This is the configuration class to store the configuration of a [`NewModelModel`]. It is used to instantiate an NewModel
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 NewModel-7B.
e.g. [google/new_model-7b](https://huggingface.co/google/new_model-7b)
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
documentation from [`PretrainedConfig`] for more information.
Args:
    vocab_size (`int`, *optional*, defaults to 256000):
        Vocabulary size of the NewModel model. Defines the number of different tokens that can be represented by the
        `inputs_ids` passed when calling [`NewModelModel`]
    hidden_size (`int`, *optional*, defaults to 3072):
        Dimension of the hidden representations.
    intermediate_size (`int`, *optional*, defaults to 24576):
        Dimension of the MLP representations.
    num_hidden_layers (`int`, *optional*, defaults to 28):
        Number of hidden layers in the Transformer decoder.
    num_attention_heads (`int`, *optional*, defaults to 16):
        Number of attention heads for each attention layer in the Transformer decoder.
    num_key_value_heads (`int`, *optional*, defaults to 16):
        This is the number of key_value heads that should be used to implement Grouped Query Attention. If
        `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
        `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
        converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
        by meanpooling all the original heads within that group. For more details, check out [this
        paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
        `num_attention_heads`.
    head_dim (`int`, *optional*, defaults to 256):
        The attention head dimension.
    hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
        The legacy activation function. It is overwritten by the `hidden_activation`.
    hidden_activation (`str` or `function`, *optional*):
        The non-linear activation function (function or string) in the decoder. Will default to `"gelu_pytorch_tanh"`
        if not specified. `"gelu_pytorch_tanh"` uses an approximation of the `"gelu"` activation function.
    max_position_embeddings (`int`, *optional*, defaults to 8192):
        The maximum sequence length that this model might ever be used with.
    initializer_range (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    rms_norm_eps (`float`, *optional*, defaults to 1e-06):
        The epsilon used by the rms normalization layers.
    use_cache (`bool`, *optional*, defaults to `True`):
        Whether or not the model should return the last key/values attentions (not used by all models). Only
        relevant if `config.is_decoder=True`.
    pad_token_id (`int`, *optional*, defaults to 0):
        Padding token id.
    eos_token_id (`int`, *optional*, defaults to 1):
        End of stream token id.
    bos_token_id (`int`, *optional*, defaults to 2):
        Beginning of stream token id.
    tie_word_embeddings (`bool`, *optional*, defaults to `True`):
        Whether to tie weight embeddings
    rope_theta (`float`, *optional*, defaults to 10000.0):
        The base period of the RoPE embeddings.
    attention_bias (`bool`, defaults to `False`, *optional*, defaults to `False`):
        Whether to use a bias in the query, key, value and output projection layers during self-attention.
    attention_dropout (`float`, *optional*, defaults to 0.0):
        The dropout ratio for the attention probabilities.
```python
>>> from transformers import NewModelModel, NewModelConfig
>>> # Initializing a NewModel new_model-7b style configuration
>>> configuration = NewModelConfig()
>>> # Initializing a model from the new_model-7b style configuration
>>> model = NewModelModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
```


def maybe_propagate_compile_time_consts_in_xla(op):
    if control_flow_util.GraphOrParentsInXlaContext(op.graph):
        op._set_attr('_xla_propagate_compile_time_consts', attr_value_pb2.AttrValue(b=True))

Tells XLA whether to propagate compile-time consts in the loop body.

This is needed to make compile time constants available to ops, for example
`max_num_elements` in `EmptyTensorList`, inside the loop body. Ideally this
would always be turned on, but that doesn't work with legacy functionalized
while_loops.

Args:
  op: A `While` Operation.


def remove(self, keys, name=None):
    return self.erase(keys, name)

Removes `keys` and its associated values from the table.

If a key is not present in the table, it is silently ignored.

Args:
  keys: Keys to remove. Can be a tensor of any shape. Must match the table's
    key type.
  name: A name for the operation (optional).

Returns:
  The created Operation.

Raises:
  TypeError: when `keys` do not match the table data types.


def temp_pubsub_emulator(project_id='apache-beam-testing'):
    with PubSubContainer(project=project_id) as pubsub_container:
        publisher = pubsub_v1.PublisherClient()
        random_front_charactor = random.choice(string.ascii_lowercase)
        topic_id = f'{random_front_charactor}{uuid.uuid4().hex[:8]}'
        topic_name_to_create = f'projects/{pubsub_container.project}/topics/{topic_id}'
        created_topic_object = publisher.create_topic(name=topic_name_to_create)
        yield created_topic_object.name

Context manager to provide a temporary Pub/Sub emulator for testing.

This function uses 'testcontainers' to spin up a Google Cloud SDK
container running the Pub/Sub emulator. It yields the emulator host
string (e.g., "localhost:xxxxx") that can be used to configure Pub/Sub
clients.

The Docker container is automatically managed and torn down when the
context manager exits.

Args:
    project_id (str): The GCP project ID to be used by the emulator.
                      This doesn't need to be a real project for the emulator.

Yields:
    str: The host and port for the Pub/Sub emulator (e.g., "localhost:xxxx").
          This will be the address to point your Pub/Sub client to.

Raises:
    Exception: If the container fails to start or the emulator isn't ready.


class Unknown(_base.BaseValue):
    _current_id = 0
    IGNORED_ATTRIBUTES: Sequence[str] = ['__get__', '__set__', '__getattribute__']

    def __init__(self, ctx: 'context.Context') -> None:
        name = escape.unknown(Unknown._current_id)
        super().__init__(name, ctx)
        self.members = datatypes.MonitorDict()
        self.owner = None
        Unknown._current_id += 1
        self.class_name = self.name
        self._calls: list[tuple[tuple[cfg.Variable, ...], dict[str, cfg.Variable], cfg.Variable]] = []
        log.info('Creating %s', self.class_name)

    def compute_mro(self):
        return self.default_mro()

    def get_fullhash(self, seen: set[int] | None=None) -> int:
        return hash((type(self),) + tuple(sorted(self.members)))

    @classmethod
    def _to_pytd(cls, node, v):
        if isinstance(v, cfg.Variable):
            return pytd_utils.JoinTypes((cls._to_pytd(node, t) for t in v.data))
        elif isinstance(v, Unknown):
            return pytd.NamedType(v.class_name)
        else:
            return v.to_pytd_type(node)

    @classmethod
    def _make_params(cls, node: cfg.CFGNode, args: Sequence[cfg.Variable], kwargs: dict[str, cfg.Variable]) -> tuple[pytd.Parameter, ...]:
        """Convert a list of types/variables to pytd parameters."""

        def _make_param(name: str, p):
            return pytd.Parameter(name, cls._to_pytd(node, p), kind=pytd.ParameterKind.REGULAR, optional=False, mutated_type=None)
        pos_params = tuple((_make_param(f'_{i + 1}', p) for i, p in enumerate(args)))
        key_params = tuple((_make_param(name, p) for name, p in kwargs.items()))
        return pos_params + key_params

    def get_special_attribute(self, node: cfg.CFGNode, name: str, valself: cfg.Variable) -> cfg.Variable | None:
        del node, valself
        if name in self.IGNORED_ATTRIBUTES:
            return None
        if name in self.members:
            return self.members[name]
        new = self.ctx.convert.create_new_unknown(self.ctx.root_node, action='getattr_' + self.name + ':' + name)
        self.ctx.attribute_handler.set_attribute(self.ctx.root_node, self, name, new)
        return new

    def call(self, node: cfg.CFGNode, func: cfg.Binding | None, args: 'function.Args', alias_map: datatypes.UnionFind | None=None) -> tuple[cfg.CFGNode, cfg.Variable]:
        ret = self.ctx.convert.create_new_unknown(node, source=self.owner, action='call:' + self.name)
        self._calls.append((args.posargs, args.namedargs, ret))
        return (node, ret)

    def argcount(self, _) -> int:
        return 0

    def to_variable(self, node: cfg.CFGNode) -> cfg.Variable:
        v = self.ctx.program.NewVariable()
        val = v.AddBinding(self, source_set=[], where=node)
        self.owner = val
        self.ctx.vm.trace_unknown(self.class_name, val)
        return v

    def to_structural_def(self, node: cfg.CFGNode, class_name: str) -> pytd.Class:
        """Convert this Unknown to a pytd.Class."""
        self_param = (pytd.Parameter('self', pytd.AnythingType(), pytd.ParameterKind.REGULAR, False, None),)
        starargs = None
        starstarargs = None

        def _make_sig(args: tuple[cfg.Variable, ...], kwargs: dict[str, cfg.Variable], ret: cfg.Variable) -> pytd.Signature:
            return pytd.Signature(self_param + self._make_params(node, args, kwargs), starargs, starstarargs, return_type=Unknown._to_pytd(node, ret), exceptions=(), template=())
        calls = tuple(pytd_utils.OrderedSet((_make_sig(args, kwargs, ret) for args, kwargs, ret in self._calls)))
        if calls:
            methods = (pytd.Function('__call__', calls, pytd.MethodKind.METHOD),)
        else:
            methods = ()
        return pytd.Class(name=class_name, keywords=(), bases=(pytd.NamedType('builtins.object'),), methods=methods, constants=tuple((pytd.Constant(name, Unknown._to_pytd(node, c)) for name, c in self.members.items())), classes=(), decorators=(), slots=None, template=())

    def instantiate(self, node: cfg.CFGNode, container: '_instance_base.SimpleValue | abstract_utils.DummyContainer | None'=None) -> cfg.Variable:
        return self.to_variable(node)

Representation of unknown values.

These are e.g. the return values of certain functions (e.g. eval()). They
"adapt": E.g. they'll respond to get_attribute requests by creating that
attribute.

Attributes:
  members: Attributes that were written or read so far. Mapping of str to
    cfg.Variable.
  owner: cfg.Binding that contains this instance as data.


def preprocess(train_data_path: str, feature_thres: int, val_data_path: typing.Optional[str]=None) -> typing.Tuple[Dataset, typing.List[str], typing.Optional[Dataset]]:
    features = extract_features(train_data_path, feature_thres)
    feature_index = dict(((feature, i) for i, feature in enumerate(features)))
    train_dataset = load_dataset(train_data_path, feature_index)
    val_dataset = load_dataset(val_data_path, feature_index) if val_data_path else None
    return (train_dataset, features, val_dataset)

Loads entries and translates them into JAX arrays. The boolean matrix of
the input data is represented by row indices and column indices of True values
instead of the matrix itself for memory efficiency, assuming the matrix is
highly sparse. Row and column indices are not guaranteed to be sorted.

Args:
  train_data_path (str): A file path to the training data file.
  feature_thres (str): A threshold to filter out features whose number of
    occurances does not exceed the value.
  val_data_path (str, optional): A file path to the validation data file.

Returns:
  A tuple of following items:
  - train_dataset (Dataset): The training dataset.
  - features (List[str]): The list of features.
  - val_dataset (Optional[Dataset]): The validation dataset.
      This becomes None if val_data_path is None.


def _maybe_save_assets(write_fn, assets_to_add=None):
    asset_filename_map = {}
    if assets_to_add is None:
        tf_logging.info('No assets to save.')
        return asset_filename_map
    for asset_tensor in assets_to_add:
        asset_source_filepath = _asset_path_from_tensor(asset_tensor)
        if not asset_source_filepath:
            raise ValueError(f'Asset filepath tensor {asset_tensor} in is invalid.')
        asset_filename = get_asset_filename_to_add(asset_source_filepath, asset_filename_map)
        write_fn(asset_filename, asset_tensor)
        asset_filename_map[asset_filename] = asset_source_filepath
    tf_logging.info('Assets added to graph.')
    return asset_filename_map

Saves assets to the meta graph.

Args:
  write_fn: A function callback that writes assets into meta graph.
  assets_to_add: The list where the asset paths are setup.

Returns:
  A dict of asset basenames for saving to the original full path to the asset.

Raises:
  ValueError: Indicating an invalid filepath tensor.


def AsCode(self, indent_per_depth=2):
    indent = ' ' * indent_per_depth * self.depth
    tokens_str = ' '.join((tok.value for tok in self._tokens))
    return indent + tokens_str

Return a "code" representation of this line.

The code representation shows how the line would be printed out as code.

TODO(eliben): for now this is rudimentary for debugging - once we add
formatting capabilities, this method will have other uses (not all tokens
have spaces around them, for example).

Arguments:
  indent_per_depth: how much spaces to indent per depth level.

Returns:
  A string representing the line as code.


def is_closed(self, name=None):
    if name is None:
        name = '%s_Is_Closed' % self._name
    if self._queue_ref.dtype == _dtypes.resource:
        return gen_data_flow_ops.queue_is_closed_v2(self._queue_ref, name=name)
    else:
        return gen_data_flow_ops.queue_is_closed_(self._queue_ref, name=name)

Returns true if queue is closed.

This operation returns true if the queue is closed and false if the queue
is open.

>>> q = tf.queue.FIFOQueue(capacity=3, dtypes=tf.int32)
>>> q.is_closed()
<tf.Tensor: shape=(), dtype=bool, numpy=False>

Args:
  name: A name for the operation (optional).

Returns:
  True if the queue is closed and false if the queue is open.


def __call__(self, inputs: Union[str, List[str]], **kwargs: Any) -> Union[List[Dict[str, Any]], List[List[Dict[str, Any]]]]:
    outputs = super().__call__(inputs, **kwargs)
    if isinstance(inputs, list) and len(inputs) == 1:
        return outputs[0]
    return outputs

Fill the masked token in the text(s) given as inputs.

Args:
    inputs (`str` or `List[str]`):
        One or several texts (or one list of prompts) with masked tokens.
    targets (`str` or `List[str]`, *optional*):
        When passed, the model will limit the scores to the passed targets instead of looking up in the whole
        vocab. If the provided targets are not in the model vocab, they will be tokenized and the first
        resulting token will be used (with a warning, and that might be slower).
    top_k (`int`, *optional*):
        When passed, overrides the number of predictions to return.

Return:
    A list or a list of list of `dict`: Each result comes as list of dictionaries with the following keys:

    - **sequence** (`str`) -- The corresponding input with the mask token prediction.
    - **score** (`float`) -- The corresponding probability.
    - **token** (`int`) -- The predicted token id (to replace the masked one).
    - **token_str** (`str`) -- The predicted token (to replace the masked one).


def _update_graph(self, vertex_dict=None, edge_dict=None):

    def set_attrs(ref, attrs):
        for attr_name, attr_val in attrs.items():
            ref.set(attr_name, attr_val)
    with self._lock:
        if vertex_dict:
            for vertex, vertex_attrs in vertex_dict.items():
                set_attrs(self._vertex_refs[vertex], vertex_attrs)
        if edge_dict:
            for edge, edge_attrs in edge_dict.items():
                if isinstance(edge, tuple):
                    set_attrs(self._edge_refs[edge], edge_attrs)
                else:
                    for vertex_pair in self._edge_to_vertex_pairs[edge]:
                        set_attrs(self._edge_refs[vertex_pair], edge_attrs)

Updates the pydot.Dot object with the given attribute update

Args:
  vertex_dict: (Dict[str, Dict[str, str]]) maps vertex names to attributes
  edge_dict: This should be
      Either (Dict[str, Dict[str, str]]) which maps edge names to attributes
      Or (Dict[(str, str), Dict[str, str]]) which maps vertex pairs to edge
      attributes


def get_input_params(distribution_strategy, num_samples, steps, batch_size, mode=None):
    use_per_replica_batch = not dist_utils.global_batch_size_supported(distribution_strategy)
    if context.executing_eagerly():
        allow_partial_batch = mode != ModeKeys.TRAIN or not backend.is_tpu_strategy(distribution_strategy)
    else:
        allow_partial_batch = mode == ModeKeys.TRAIN or ((mode == ModeKeys.PREDICT or mode == ModeKeys.TEST) and backend.is_tpu_strategy(distribution_strategy))
    if steps is None:
        if batch_size is None:
            global_batch_size = min(num_samples, 32)
        else:
            global_batch_size = batch_size
            if use_per_replica_batch:
                global_batch_size *= distribution_strategy.num_replicas_in_sync
        if allow_partial_batch:
            steps = np.ceil(num_samples / global_batch_size).astype(int)
        else:
            if num_samples % global_batch_size:
                raise ValueError('The number of samples %s is not divisible by batch size %s.' % (num_samples, global_batch_size))
            steps = num_samples // global_batch_size
    elif batch_size is None:
        if num_samples % steps:
            raise ValueError('The number of samples %s is not divisible by steps %s. Please change the number of steps to a value that can consume all the samples' % (num_samples, steps))
        global_batch_size = num_samples // steps
    else:
        global_batch_size = batch_size
        if use_per_replica_batch:
            global_batch_size *= distribution_strategy.num_replicas_in_sync
        min_num_samples = global_batch_size * steps
        if allow_partial_batch:
            min_num_samples = global_batch_size * (steps - 1) + 1 if steps > 1 else 0
        if num_samples < min_num_samples:
            raise ValueError('Number of samples %s is less than samples required for specified batch_size %s and steps %s' % (num_samples, global_batch_size, steps))
    if use_per_replica_batch:
        if global_batch_size % distribution_strategy.num_replicas_in_sync:
            raise ValueError('The batch size (%s) could not be sharded evenly across the sync replicas (%s) in the distribution strategy.' % (global_batch_size, distribution_strategy.num_replicas_in_sync))
        batch_size = global_batch_size // distribution_strategy.num_replicas_in_sync
    else:
        batch_size = global_batch_size
    return (steps, batch_size)

Calculate the number of batches and steps/steps_per_epoch.

Args:
  distribution_strategy: The DistributionStrategy used to compile the model.
  num_samples: The number of samples from which we determine the batch size
    and steps.
  steps:  The specified number of steps.
  batch_size: The specified batch_size.
  mode: ModeKey representing whether input will be used for training,
    evaluation, or prediction. This is used to relax the constraints on
    consuming all the training samples to keep compatibility till we support
    partial batches. If none, then partial batches are not allowed.

Returns:
  steps: The steps or steps_per_epoch argument depending on if a user is
      calling `fit`, `evaluate` or `predict`. If the is_training flag is set
      we don't require the number of samples to be used completely.
  batch_size: The batch size to be used in model iterations.

Raises:
  ValueError: If the number of batches or steps evaluates to 0.


class Precision(Metric):

    def __init__(self, thresholds=None, top_k=None, class_id=None, name=None, dtype=None):
        super().__init__(name=name, dtype=dtype)
        self._direction = 'up'
        self.init_thresholds = thresholds
        self.top_k = top_k
        self.class_id = class_id
        default_threshold = 0.5 if top_k is None else metrics_utils.NEG_INF
        self.thresholds = metrics_utils.parse_init_thresholds(thresholds, default_threshold=default_threshold)
        self._thresholds_distributed_evenly = metrics_utils.is_evenly_distributed_thresholds(self.thresholds)
        self.true_positives = self.add_variable(shape=(len(self.thresholds),), initializer=initializers.Zeros(), name='true_positives')
        self.false_positives = self.add_variable(shape=(len(self.thresholds),), initializer=initializers.Zeros(), name='false_positives')

    def update_state(self, y_true, y_pred, sample_weight=None):
        """Accumulates true positive and false positive statistics.

        Args:
            y_true: The ground truth values, with the same dimensions as
                `y_pred`. Will be cast to `bool`.
            y_pred: The predicted values. Each element must be in the range
                `[0, 1]`.
            sample_weight: Optional weighting of each example. Defaults to `1`.
                Can be a tensor whose rank is either 0, or the same rank as
                `y_true`, and must be broadcastable to `y_true`.
        """
        metrics_utils.update_confusion_matrix_variables({metrics_utils.ConfusionMatrix.TRUE_POSITIVES: self.true_positives, metrics_utils.ConfusionMatrix.FALSE_POSITIVES: self.false_positives}, y_true, y_pred, thresholds=self.thresholds, thresholds_distributed_evenly=self._thresholds_distributed_evenly, top_k=self.top_k, class_id=self.class_id, sample_weight=sample_weight)

    def result(self):
        result = ops.divide_no_nan(self.true_positives, ops.add(self.true_positives, self.false_positives))
        return result[0] if len(self.thresholds) == 1 else result

    def reset_state(self):
        num_thresholds = len(to_list(self.thresholds))
        self.true_positives.assign(ops.zeros((num_thresholds,)))
        self.false_positives.assign(ops.zeros((num_thresholds,)))

    def get_config(self):
        config = {'thresholds': self.init_thresholds, 'top_k': self.top_k, 'class_id': self.class_id}
        base_config = super().get_config()
        return {**base_config, **config}

Computes the precision of the predictions with respect to the labels.

The metric creates two local variables, `true_positives` and
`false_positives` that are used to compute the precision. This value is
ultimately returned as `precision`, an idempotent operation that simply
divides `true_positives` by the sum of `true_positives` and
`false_positives`.

If `sample_weight` is `None`, weights default to 1.
Use `sample_weight` of 0 to mask values.

If `top_k` is set, we'll calculate precision as how often on average a class
among the top-k classes with the highest predicted values of a batch entry
is correct and can be found in the label for that entry.

If `class_id` is specified, we calculate precision by considering only the
entries in the batch for which `class_id` is above the threshold and/or in
the top-k highest predictions, and computing the fraction of them for which
`class_id` is indeed a correct label.

Args:
    thresholds: (Optional) A float value, or a Python list/tuple of float
        threshold values in `[0, 1]`. A threshold is compared with
        prediction values to determine the truth value of predictions (i.e.,
        above the threshold is `True`, below is `False`). If used with a
        loss function that sets `from_logits=True` (i.e. no sigmoid applied
        to predictions), `thresholds` should be set to 0. One metric value
        is generated for each threshold value. If neither `thresholds` nor
        `top_k` are set, the default is to calculate precision with
        `thresholds=0.5`.
    top_k: (Optional) Unset by default. An int value specifying the top-k
        predictions to consider when calculating precision.
    class_id: (Optional) Integer class ID for which we want binary metrics.
        This must be in the half-open interval `[0, num_classes)`, where
        `num_classes` is the last dimension of predictions.
    name: (Optional) string name of the metric instance.
    dtype: (Optional) data type of the metric result.

Example:

>>> m = keras.metrics.Precision()
>>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1])
>>> m.result()
0.6666667

>>> m.reset_state()
>>> m.update_state([0, 1, 1, 1], [1, 0, 1, 1], sample_weight=[0, 0, 1, 0])
>>> m.result()
1.0

>>> # With top_k=2, it will calculate precision over y_true[:2]
>>> # and y_pred[:2]
>>> m = keras.metrics.Precision(top_k=2)
>>> m.update_state([0, 0, 1, 1], [1, 1, 1, 1])
>>> m.result()
0.0

>>> # With top_k=4, it will calculate precision over y_true[:4]
>>> # and y_pred[:4]
>>> m = keras.metrics.Precision(top_k=4)
>>> m.update_state([0, 0, 1, 1], [1, 1, 1, 1])
>>> m.result()
0.5

Usage with `compile()` API:

```python
model.compile(optimizer='sgd',
              loss='binary_crossentropy',
              metrics=[keras.metrics.Precision()])
```

Usage with a loss with `from_logits=True`:

```python
model.compile(optimizer='adam',
              loss=keras.losses.BinaryCrossentropy(from_logits=True),
              metrics=[keras.metrics.Precision(thresholds=0)])
```


def scatter_nd_update(self, indices, updates, name=None):
    raise NotImplementedError

Applies sparse assignment to individual values or slices in a Variable.

The Variable has rank `P` and `indices` is a `Tensor` of rank `Q`.

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

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

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

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

For example, say we want to add 4 scattered elements to a rank-1 tensor to
8 elements. In Python, that update would look like this:

```python
    v = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
    indices = tf.constant([[4], [3], [1] ,[7]])
    updates = tf.constant([9, 10, 11, 12])
    v.scatter_nd_update(indices, updates)
    print(v)
```

The resulting update to v would look like this:

    [1, 11, 3, 10, 9, 6, 7, 12]

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

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

Returns:
  The updated variable.


def discount_rate(self, date: Optional[types.DateTensor]=None, time: Optional[types.FloatTensor]=None, context=None) -> tf.Tensor:
    pass

Returns the discount rates to a specified set of dates.

Args:
  date: A `DateTensor` specifying the dates at which to evaluate the
    discount rates. The function expects either `date` or `time` to be
    specified.
  time: A real `Tensor` specifying the times at which to evaluate the
    discount rates. The function expects either `date` or `time` to be
    specified.
  context: The context object, e.g., curve_type.

Returns:
  A `Tensor` of the same shape as `dates` with the corresponding discount
  rates.


def permute_dimensions(x, pattern):
    return array_ops.transpose(x, perm=pattern)

Permutes axes in a tensor.

Args:
    x: Tensor or variable.
    pattern: A tuple of
        dimension indices, e.g. `(0, 2, 1)`.

Returns:
    A tensor.

Example:

  >>> a = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]])
  >>> a
  <tf.Tensor: shape=(4, 3), dtype=int32, numpy=
  array([[ 1,  2,  3],
         [ 4,  5,  6],
         [ 7,  8,  9],
         [10, 11, 12]], dtype=int32)>
  >>> tf.keras.backend.permute_dimensions(a, pattern=(1, 0))
  <tf.Tensor: shape=(3, 4), dtype=int32, numpy=
  array([[ 1,  4,  7, 10],
         [ 2,  5,  8, 11],
         [ 3,  6,  9, 12]], dtype=int32)>


def check_annotation_type_mismatch(self, node, name, typ, value, stack, allow_none, details=None):
    if not typ or not value:
        return
    if value.data == [self.convert.ellipsis] or (allow_none and value.data == [self.convert.none]):
        return
    contained_type = abstract_utils.match_type_container(typ, ('typing.ClassVar', 'dataclasses.InitVar'))
    if contained_type:
        typ = contained_type
    bad = self.matcher(node).compute_one_match(value, typ).bad_matches
    for match in bad:
        self.errorlog.annotation_type_mismatch(stack, match.expected.typ, match.actual_binding, name, match.error_details, details)

Checks for a mismatch between a variable's annotation and value.

Args:
  node: node
  name: variable name
  typ: variable annotation
  value: variable value
  stack: a frame stack for error reporting
  allow_none: whether a value of None is allowed for any type
  details: any additional details to add to the error message


def __init__(self, capacity, types, shapes=None, names=None, shared_name=None, name='priority_queue'):
    types = _as_type_list(types)
    shapes = _as_shape_list(shapes, types)
    queue_ref = gen_data_flow_ops.priority_queue_v2(component_types=types, shapes=shapes, capacity=capacity, shared_name=_shared_name(shared_name), name=name)
    priority_dtypes = [_dtypes.int64] + types
    priority_shapes = [()] + shapes if shapes else shapes
    super(PriorityQueue, self).__init__(priority_dtypes, priority_shapes, names, queue_ref)

Creates a queue that dequeues elements in a first-in first-out order.

A `PriorityQueue` has bounded capacity; supports multiple concurrent
producers and consumers; and provides exactly-once delivery.

A `PriorityQueue` holds a list of up to `capacity` elements. Each
element is a fixed-length tuple of tensors whose dtypes are
described by `types`, and whose shapes are optionally described
by the `shapes` argument.

If the `shapes` argument is specified, each component of a queue
element must have the respective fixed shape. If it is
unspecified, different queue elements may have different shapes,
but the use of `dequeue_many` is disallowed.

Enqueues and Dequeues to the `PriorityQueue` must include an additional
tuple entry at the beginning: the `priority`.  The priority must be
an int64 scalar (for `enqueue`) or an int64 vector (for `enqueue_many`).

Args:
  capacity: An integer. The upper bound on the number of elements
    that may be stored in this queue.
  types:  A list of `DType` objects. The length of `types` must equal
    the number of tensors in each queue element, except the first priority
    element.  The first tensor in each element is the priority,
    which must be type int64.
  shapes: (Optional.) A list of fully-defined `TensorShape` objects,
    with the same length as `types`, or `None`.
  names: (Optional.) A list of strings naming the components in the queue
    with the same length as `dtypes`, or `None`.  If specified, the dequeue
    methods return a dictionary with the names as keys.
  shared_name: (Optional.) If non-empty, this queue will be shared under
    the given name across multiple sessions.
  name: Optional name for the queue operation.


def call(self, sequence_output: tf.Tensor) -> tf.Tensor:
    logits = (tf.einsum('bsj,j->bs', sequence_output, self.output_weights) + self.output_bias) / self.temperature
    return logits

Computes logits per token

Args:
    sequence_output (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`):
        Also known as last_hidden_state. Sequence of hidden-states at the output of the last layer of the
        model.

Returns:
    logits (`tf.Tensor` of shape `(batch_size, sequence_length)`): Logits per token.


def __init__(self, index: Optional[int]=None):
    super().__init__()
    self._index = index

Constructor.

Args:
  index: index of the tuple field that this key spec applies to.
    If None, this tuple value spec applies to all elements of a
    variable-length tuple.


class Select(StandardSqlExpression):
    select_part: StandardSqlExpression
    from_part: Optional[str]
    where_part: Optional[str] = None
    limit_part: Optional[int] = None
    sql_dialect: Optional[SqlDialect] = SqlDialect.BIGQUERY

    @property
    def sql_data_type(self) -> StandardSqlDataType:
        return self.select_part.sql_data_type

    @property
    def sql_alias(self) -> str:
        return self.select_part.sql_alias

    def union(self, rhs: Select, distinct: bool) -> UnionExpression:
        """Builds a UNION with this and the given Select."""
        return UnionExpression(self, rhs, distinct)

    def to_subquery(self) -> StandardSqlExpression:
        """Renders the expression as a subquery."""
        return SubQuery(self)

    def __str__(self) -> str:
        """Builds the SQL expression from its given components."""
        query_parts = ['SELECT ']
        select_part = wrap_time_types(str(self.select_part), self.sql_data_type, self.sql_dialect)
        query_parts.append(select_part)
        if select_part != str(self.select_part) or not self.select_part.matches_alias(self.sql_alias):
            query_parts.extend((' AS ', str(self.sql_alias)))
        if self.from_part:
            query_parts.extend(f'\nFROM {self.from_part}')
        if self.where_part:
            query_parts.extend(('\n', 'WHERE ', str(self.where_part)))
        if self.limit_part:
            query_parts.extend(('\n', 'LIMIT ', str(self.limit_part)))
        return ''.join(query_parts)

    def as_operand(self) -> str:
        """Returns select_part.as_operand() if this expression has no other parts.

    Otherwise it just returns the expression's __str__ representation in a
    subquery.

    Also excludes the inital `SELECT`.
    """
        if self.from_part or self.where_part:
            return str(self.to_subquery())
        return wrap_time_types(self.select_part.as_operand(), self.sql_data_type, self.sql_dialect)

Representation of a Standard SQL SELECT expression.

Attributes:
  select_part: The expression being SELECT'd
  from_part: The body of the FROM clause. Optional to support subquery
    expressions taking their FROM from a parent query.
  where_part: The body of the WHERE clause.
  limit_part: The body of the LIMIT clause.
  sql_dialect: The SQL dialect to use. Defaults to BigQuery


def from_operator(cls, operator):
    validation_fields = ('is_non_singular', 'is_self_adjoint', 'is_positive_definite', 'is_square')
    kwargs = _extract_attrs(operator, keys=set(operator._composite_tensor_fields + validation_fields))
    non_tensor_params = {}
    param_specs = {}
    for k, v in list(kwargs.items()):
        type_spec_or_v = _extract_type_spec_recursively(v)
        is_tensor = [isinstance(x, type_spec.TypeSpec) for x in nest.flatten(type_spec_or_v)]
        if all(is_tensor):
            param_specs[k] = type_spec_or_v
        elif not any(is_tensor):
            non_tensor_params[k] = v
        else:
            raise NotImplementedError(f'Field {k} contains a mix of `Tensor` and  non-`Tensor` values.')
    return cls(param_specs=param_specs, non_tensor_params=non_tensor_params, prefer_static_fields=operator._composite_tensor_prefer_static_fields)

Builds a `_LinearOperatorSpec` from a `LinearOperator` instance.

Args:
  operator: An instance of `LinearOperator`.

Returns:
  linear_operator_spec: An instance of `_LinearOperatorSpec` to be used as
    the `TypeSpec` of `operator`.


def update_state(self, y_true, y_pred, sample_weight=None):
    y_true = math_ops.cast(y_true, self._dtype)
    y_pred = math_ops.cast(y_pred, self._dtype)
    if y_pred.shape.ndims > 1:
        y_pred = array_ops.reshape(y_pred, [-1])
    if y_true.shape.ndims > 1:
        y_true = array_ops.reshape(y_true, [-1])
    if sample_weight is not None:
        sample_weight = math_ops.cast(sample_weight, self._dtype)
        if sample_weight.shape.ndims > 1:
            sample_weight = array_ops.reshape(sample_weight, [-1])
    current_cm = confusion_matrix.confusion_matrix(y_true, y_pred, self.num_classes, weights=sample_weight, dtype=self._dtype)
    return self.total_cm.assign_add(current_cm)

Accumulates the confusion matrix statistics.

Args:
  y_true: The ground truth values.
  y_pred: The predicted values.
  sample_weight: Optional weighting of each example. Defaults to 1. Can be a
    `Tensor` whose rank is either 0, or the same rank as `y_true`, and must
    be broadcastable to `y_true`.

Returns:
  Update op.


def collective_manager_ids_from_op(op):
    if op.type == 'CollectiveReduce':
        try:
            return [op.get_attr('_collective_manager_id')]
        except ValueError:
            pass
    elif op.type == 'StatefulPartitionedCall':
        try:
            return op.get_attr(utils.COLLECTIVE_MANAGER_IDS)
        except ValueError:
            pass
    return []

Returns CollectiveManager ID from the op if one exists, else None.

CollectiveManager adds collective and no_op operations tagged with an ID,
unique to the manager object. This function extracts that ID, or None, if the
node was not generated by a CollectiveManager.

Args:
  op: `Operation` to get the collective manager ID from.

Returns:
  List of CollectiveManager IDs used by the op.


def logdet(x):
    if any_symbolic_tensors((x,)):
        return Logdet().symbolic_call(x)
    return backend.math.logdet(x)

Computes log of the determinant of a hermitian positive definite matrix.

Args:
    x: Input matrix. It must 2D and square.

Returns:
    The natural log of the determinant of matrix.


class MeanSquaredLogarithmicError(LossFunctionWrapper):

    def __init__(self, reduction='sum_over_batch_size', name='mean_squared_logarithmic_error', dtype=None):
        super().__init__(mean_squared_logarithmic_error, name=name, reduction=reduction, dtype=dtype)

    def get_config(self):
        return Loss.get_config(self)

Computes the mean squared logarithmic error between `y_true` & `y_pred`.

Formula:

```python
loss = mean(square(log(y_true + 1) - log(y_pred + 1)))
```

Args:
    reduction: Type of reduction to apply to the loss. In almost all cases
        this should be `"sum_over_batch_size"`. Supported options are
        `"sum"`, `"sum_over_batch_size"`, `"mean"`,
        `"mean_with_sample_weight"` or `None`. `"sum"` sums the loss,
        `"sum_over_batch_size"` and `"mean"` sum the loss and divide by the
        sample size, and `"mean_with_sample_weight"` sums the loss and
        divides by the sum of the sample weights. `"none"` and `None`
        perform no aggregation. Defaults to `"sum_over_batch_size"`.
    name: Optional name for the loss instance.
    dtype: The dtype of the loss's computations. Defaults to `None`, which
        means using `keras.backend.floatx()`. `keras.backend.floatx()` is a
        `"float32"` unless set to different value
        (via `keras.backend.set_floatx()`). If a `keras.DTypePolicy` is
        provided, then the `compute_dtype` will be utilized.


def get_image_size(image: np.ndarray, channel_dim: ChannelDimension=None) -> tuple[int, int]:
    if channel_dim is None:
        channel_dim = infer_channel_dimension_format(image)
    if channel_dim == ChannelDimension.FIRST:
        return (image.shape[-2], image.shape[-1])
    elif channel_dim == ChannelDimension.LAST:
        return (image.shape[-3], image.shape[-2])
    else:
        raise ValueError(f'Unsupported data format: {channel_dim}')

Returns the (height, width) dimensions of the image.

Args:
    image (`np.ndarray`):
        The image to get the dimensions of.
    channel_dim (`ChannelDimension`, *optional*):
        Which dimension the channel dimension is in. If `None`, will infer the channel dimension from the image.

Returns:
    A tuple of the image's height and width.


def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
    cos = cos.unsqueeze(unsqueeze_dim)
    sin = sin.unsqueeze(unsqueeze_dim)
    q_embed = q * cos + rotate_half(q) * sin
    k_embed = k * cos + rotate_half(k) * sin
    return (q_embed, k_embed)

Applies Rotary Position Embedding to the query and key tensors.

Args:
    q (`torch.Tensor`): The query tensor.
    k (`torch.Tensor`): The key tensor.
    cos (`torch.Tensor`): The cosine part of the rotary embedding.
    sin (`torch.Tensor`): The sine part of the rotary embedding.
    position_ids (`torch.Tensor`, *optional*):
        Deprecated and unused.
    unsqueeze_dim (`int`, *optional*, defaults to 1):
        The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
        sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
        that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
        k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
        cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
        the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
Returns:
    `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.


class FlaxSeq2SeqSequenceClassifierOutput(ModelOutput):
    logits: Optional[jnp.ndarray] = None
    past_key_values: Optional[Tuple[Tuple[jnp.ndarray]]] = None
    decoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None
    decoder_attentions: Optional[Tuple[jnp.ndarray]] = None
    cross_attentions: Optional[Tuple[jnp.ndarray]] = None
    encoder_last_hidden_state: Optional[jnp.ndarray] = None
    encoder_hidden_states: Optional[Tuple[jnp.ndarray]] = None
    encoder_attentions: Optional[Tuple[jnp.ndarray]] = None

Base class for outputs of sequence-to-sequence sentence classification models.

Args:
    logits (`jnp.ndarray` of shape `(batch_size, config.num_labels)`):
        Classification (or regression if config.num_labels==1) scores (before SoftMax).
    past_key_values (`tuple(tuple(jnp.ndarray))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
        Tuple of `tuple(jnp.ndarray)` of length `config.n_layers`, with each tuple having 2 tensors of shape
        `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape
        `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.

        Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
        blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.
    decoder_hidden_states (`tuple(jnp.ndarray)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `jnp.ndarray` (one for the output of the embeddings + one for the output of each layer) of shape
        `(batch_size, sequence_length, hidden_size)`.

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

        Attentions weights of the decoder, after the attention softmax, used to compute the weighted average in the
        self-attention heads.
    cross_attentions (`tuple(jnp.ndarray)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `jnp.ndarray` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
        sequence_length)`.

        Attentions weights of the decoder's cross-attention layer, after the attention softmax, used to compute the
        weighted average in the cross-attention heads.
    encoder_last_hidden_state (`jnp.ndarray` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
        Sequence of hidden-states at the output of the last layer of the encoder of the model.
    encoder_hidden_states (`tuple(jnp.ndarray)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `jnp.ndarray` (one for the output of the embeddings + one for the output of each layer) of shape
        `(batch_size, sequence_length, hidden_size)`.

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

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


def memberOf(self, value_set: Union[str, message.Message]) -> 'Builder':
    param_nodes = self._function_args_to_nodes(self.node, [value_set])
    return self._to_builder(_evaluation.MemberOfFunction(self.node.context, self.node, param_nodes))

The FHIRPath memberOf() function.

This is used to determine whether a codeable concept is a member of a given
value set. The value set may be a literal, FHIR ValueSet proto or
(more commonly) a URI that is resolved by the underlying evaluation engine.

For example, the following expression can be used to determine if an
observation code is part of a specific valueset:

>>> obs = <obs expression builder>
>>> obs.code.MemberOf('url:example:my:valueset')

Args:
  value_set: may be either a string containing a value set URL or an
    expanded value set in the form of a proto. See examples in the README
    documentation for more details.

Returns:
  An expression to that evaluates to true if the parent is a member of
  the given value set.


def pad(x, pad_width, mode='constant', constant_values=None):
    return Pad(pad_width, mode=mode)(x, constant_values=constant_values)

Pad a tensor.

Args:
    x: Tensor to pad.
    pad_width: Number of values padded to the edges of each axis.
        `((before_1, after_1), ...(before_N, after_N))` unique pad
        widths for each axis.
        `((before, after),)` yields same before and after pad for
        each axis.
        `(pad,)` or `int` is a shortcut for `before = after = pad`
        width for all axes.
    mode: One of `"constant"`, `"edge"`, `"linear_ramp"`,
        `"maximum"`, `"mean"`, `"median"`, `"minimum"`,
        `"reflect"`, `"symmetric"`, `"wrap"`, `"empty"`,
        `"circular"`. Defaults to `"constant"`.
    constant_values: value to pad with if `mode == "constant"`.
        Defaults to `0`. A `ValueError` is raised if not None and
        `mode != "constant"`.

Note:
    Torch backend only supports modes `"constant"`, `"reflect"`,
    `"symmetric"` and `"circular"`.
    Only Torch backend supports `"circular"` mode.

Note:
    Tensorflow backend only supports modes `"constant"`, `"reflect"`
    and `"symmetric"`.

Returns:
    Padded tensor.


def __call__(self, table: 'pd.DataFrame', queries: Optional[Union[TextInput, PreTokenizedInput, EncodedInput, List[TextInput], List[PreTokenizedInput], List[EncodedInput]]]=None, answer_coordinates: Optional[Union[List[Tuple], List[List[Tuple]]]]=None, answer_text: Optional[Union[List[TextInput], List[List[TextInput]]]]=None, add_special_tokens: bool=True, padding: Union[bool, str, PaddingStrategy]=False, truncation: Union[bool, str, TapasTruncationStrategy]=False, max_length: Optional[int]=None, pad_to_multiple_of: Optional[int]=None, padding_side: Optional[str]=None, return_tensors: Optional[Union[str, TensorType]]=None, return_token_type_ids: Optional[bool]=None, return_attention_mask: Optional[bool]=None, return_overflowing_tokens: bool=False, return_special_tokens_mask: bool=False, return_offsets_mapping: bool=False, return_length: bool=False, verbose: bool=True, **kwargs) -> BatchEncoding:
    assert isinstance(table, pd.DataFrame), 'Table must be of type pd.DataFrame'
    valid_query = False
    if queries is None or isinstance(queries, str):
        valid_query = True
    elif isinstance(queries, (list, tuple)):
        if len(queries) == 0 or isinstance(queries[0], str):
            valid_query = True
    if not valid_query:
        raise ValueError('queries input must of type `str` (single example), `List[str]` (batch or single pretokenized example). ')
    is_batched = isinstance(queries, (list, tuple))
    if is_batched:
        return self.batch_encode_plus(table=table, queries=queries, answer_coordinates=answer_coordinates, answer_text=answer_text, add_special_tokens=add_special_tokens, padding=padding, truncation=truncation, max_length=max_length, pad_to_multiple_of=pad_to_multiple_of, padding_side=padding_side, return_tensors=return_tensors, return_token_type_ids=return_token_type_ids, return_attention_mask=return_attention_mask, return_overflowing_tokens=return_overflowing_tokens, return_special_tokens_mask=return_special_tokens_mask, return_offsets_mapping=return_offsets_mapping, return_length=return_length, verbose=verbose, **kwargs)
    else:
        return self.encode_plus(table=table, query=queries, answer_coordinates=answer_coordinates, answer_text=answer_text, add_special_tokens=add_special_tokens, padding=padding, truncation=truncation, max_length=max_length, pad_to_multiple_of=pad_to_multiple_of, padding_side=padding_side, return_tensors=return_tensors, return_token_type_ids=return_token_type_ids, return_attention_mask=return_attention_mask, return_overflowing_tokens=return_overflowing_tokens, return_special_tokens_mask=return_special_tokens_mask, return_offsets_mapping=return_offsets_mapping, return_length=return_length, verbose=verbose, **kwargs)

Main method to tokenize and prepare for the model one or several sequence(s) related to a table.

Args:
    table (`pd.DataFrame`):
        Table containing tabular data. Note that all cell values must be text. Use *.astype(str)* on a Pandas
        dataframe to convert it to string.
    queries (`str` or `List[str]`):
        Question or batch of questions related to a table to be encoded. Note that in case of a batch, all
        questions must refer to the **same** table.
    answer_coordinates (`List[Tuple]` or `List[List[Tuple]]`, *optional*):
        Answer coordinates of each table-question pair in the batch. In case only a single table-question pair
        is provided, then the answer_coordinates must be a single list of one or more tuples. Each tuple must
        be a (row_index, column_index) pair. The first data row (not the column header row) has index 0. The
        first column has index 0. In case a batch of table-question pairs is provided, then the
        answer_coordinates must be a list of lists of tuples (each list corresponding to a single
        table-question pair).
    answer_text (`List[str]` or `List[List[str]]`, *optional*):
        Answer text of each table-question pair in the batch. In case only a single table-question pair is
        provided, then the answer_text must be a single list of one or more strings. Each string must be the
        answer text of a corresponding answer coordinate. In case a batch of table-question pairs is provided,
        then the answer_coordinates must be a list of lists of strings (each list corresponding to a single
        table-question pair).


def to_sql(self, view: views.View, limit: Optional[int]=None) -> str:
    encoder = _spark_interpreter.SparkSqlInterpreter(value_set_codes_table='VALUESET_VIEW')
    dataset = f'{self._fhir_dataset}'
    sql_generator = runner_utils.RunnerSqlGenerator(view, encoder, dataset, self._snake_case_resource_tables)
    sql_statement = sql_generator.build_sql_statement()
    valuesets_clause = sql_generator.build_valueset_expression(self._value_set_codes_table)
    if limit is not None and limit < 1:
        raise ValueError('Query limits must be positive integers.')
    limit_clause = '' if limit is None else f' LIMIT {limit}'
    return f'{valuesets_clause}{sql_statement}{limit_clause}'

Returns the SQL used to run the given view in Spark.

Args:
  view: the view used to generate the SQL.
  limit: optional limit to attach to the generated SQL.

Returns:
  The SQL used to run the given view.


def fixed_field_for_type_code(type_code: str) -> str:
    url_type_code = _TYPE_CODE_URI_RE.search(type_code)
    if url_type_code is not None:
        type_code = url_type_code[1]
    fixed_field = stringcase.snakecase(type_code[:1].lower() + type_code[1:])
    return 'string_value' if fixed_field == 'string' else fixed_field

Retrieves the `ElementDefinition.fixed.choice` oneof field for `type_code`.

Args:
  type_code: The FHIR type code to look up. Could be a value like 'boolean' or
    URL like 'http://hl7.org/fhirpath/System.Boolean'

Returns:
  The attribute corresponding to this type code on the
  ElementDefinition.FixedX.choice oneof.


class MoshiDepthConfig(PretrainedConfig):
    model_type = 'moshi_depth'
    keys_to_ignore_at_inference = ['past_key_values']

    def __init__(self, vocab_size=32000, hidden_size=1024, input_size=4096, num_hidden_layers=6, num_attention_heads=16, num_key_value_heads=None, audio_vocab_size=2048, max_position_embeddings=9, hidden_act='silu', head_dim=None, initializer_range=0.02, use_cache=True, sliding_window=8, attention_dropout=0.0, ffn_dim=5632, rms_norm_eps=1e-08, num_codebooks=8, tie_word_embeddings=False, **kwargs):
        self.vocab_size = vocab_size
        self.hidden_size = hidden_size
        self.input_size = input_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.num_key_value_heads = num_key_value_heads if num_key_value_heads is not None else num_attention_heads
        self.max_position_embeddings = max_position_embeddings
        self.hidden_act = hidden_act
        self.head_dim = head_dim or hidden_size // num_attention_heads
        self.initializer_range = initializer_range
        self.use_cache = use_cache
        self.sliding_window = sliding_window
        self.attention_dropout = attention_dropout
        if ffn_dim % 2 == 1:
            raise ValueError(f'`ffn_dim={ffn_dim}` must be even.')
        self.ffn_dim = ffn_dim
        self.rms_norm_eps = rms_norm_eps
        self.num_codebooks = num_codebooks
        self.audio_vocab_size = audio_vocab_size
        super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)

This is the configuration class to store the configuration of a [`MoshiDepthDecoder`]. It is used to instantiate a
Moshi depth decoder model according to the specified arguments, defining the Moshi depth decoder config.

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

Args:
    vocab_size (`int`, *optional*, defaults to 32000):
        Vocabulary size of the MoshiDepthDecoder model. Defines the number of different tokens that can be
        represented by the `inputs_ids` passed when calling [`MoshiDepthDecoder`].
    hidden_size (`int`, *optional*, defaults to 1024):
        Dimensionality of the layers and the pooler layer of the depth decoder.
    input_size (`int`, *optional*, defaults to 4096):
        Dimensionality of the input hidden states. Used to connect the main decoder to the depth decoder.
    num_hidden_layers (`int`, *optional*, defaults to 6):
        Number of depth decoder layers.
    num_attention_heads (`int`, *optional*, defaults to 16):
        Number of attention heads for each attention layer in the depth decoder block.
    num_key_value_heads (`int`, *optional*):
        This is the number of key_value heads that should be used to implement Grouped Query Attention. If
        `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
        `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
        converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
        by meanpooling all the original heads within that group. For more details, check out [this
        paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to `num_attention_heads`.
    audio_vocab_size (`int`, *optional*, defaults to 2048):
        Vocabulary size of the audio part of model. Defines the number of different tokens that can be
        represented by the `audio_codes` passed when calling the Moshi models.
    max_position_embeddings (`int`, *optional*, defaults to 9):
        The maximum sequence length that this model might ever be used with. Typically, set this to something large
        just in case (e.g., 512 or 1024 or 2048).
    hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
        The non-linear activation function (function or string) in the depth decoder.
    head_dim (`int`, *optional*, defaults to `hidden_size // num_attention_heads`):
        The attention head dimension.
    initializer_range (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    use_cache (`bool`, *optional*, defaults to `True`):
        Whether or not the model should return the last key/values attentions (not used by all models). Only
        relevant if `config.is_decoder=True`.
    sliding_window (`int`, *optional*, defaults to 8):
        Sliding window attention window size. If not specified, will default to `8`.
    attention_dropout (`float`, *optional*, defaults to 0.0):
        The dropout ratio for the attention probabilities.
    ffn_dim (`int`, *optional*, defaults to 5632):
        Dimensionality of the "intermediate" (often named feed-forward) layer in the depth decoder block. Must be even.
    rms_norm_eps (`float`, *optional*, defaults to 1e-08):
        The epsilon used by the rms normalization layers.
    num_codebooks (`int`, *optional*, defaults to 8):
        The number of audio codebooks for each audio channels.
    tie_word_embeddings (`bool`, *optional*, defaults to `False`):
        Whether to tie weight embeddings
    kwargs (*optional*):
        Dictionary of keyword arguments. Notably:
            - **audio_encoder_config** ([`PretrainedConfig`], *optional*) -- An instance of a configuration object that
              defines the audio encoder config.

Example:

```python
>>> from transformers import (
...     MoshiDepthConfig,
...     MoshiDepthDecoder,
... )

>>> configuration = MoshiDepthConfig()

>>> # Initializing a MoshiDepthDecoder (with random weights) from the kmhf/hf-moshiko style configuration
>>> model = MoshiDepthDecoder(configuration)

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


def amax(x, axis=None, keepdims=False):
    if any_symbolic_tensors((x,)):
        return Amax(axis=axis, keepdims=keepdims).symbolic_call(x)
    return backend.numpy.amax(x, axis=axis, keepdims=keepdims)

Returns the maximum of an array or maximum value along an axis.

Args:
    x: Input tensor.
    axis: Axis along which to compute the maximum.
        By default (`axis=None`), find the maximum value in all the
        dimensions of the input array.
    keepdims: If `True`, axes which are reduced are left in the result as
        dimensions that are broadcast to the size of the original
        input tensor. Defaults to `False`.

Returns:
    An array with the maximum value. If `axis=None`, the result is a scalar
    value representing the maximum element in the entire array. If `axis` is
    given, the result is an array with the maximum values along
    the specified axis.

Examples:
>>> x = keras.ops.convert_to_tensor([[1, 3, 5], [2, 3, 6]])
>>> keras.ops.amax(x)
array(6, dtype=int32)

>>> x = keras.ops.convert_to_tensor([[1, 6, 8], [1, 5, 2]])
>>> keras.ops.amax(x, axis=0)
array([1, 6, 8], dtype=int32)

>>> x = keras.ops.convert_to_tensor([[1, 6, 8], [1, 5, 2]])
>>> keras.ops.amax(x, axis=1, keepdims=True)
array([[8], [5]], dtype=int32)


def detach(self) -> Rotation:
    if self._rot_mats is not None:
        return Rotation(rot_mats=self._rot_mats.detach(), quats=None)
    elif self._quats is not None:
        return Rotation(rot_mats=None, quats=self._quats.detach(), normalize_quats=False)
    else:
        raise ValueError('Both rotations are None')

Returns a copy of the Rotation whose underlying Tensor has been detached from its torch graph.

Returns:
    A copy of the Rotation whose underlying Tensor has been detached from its torch graph


def uniform_row_length(self):
    return self._row_partition.uniform_row_length()

The length of each row in this ragged tensor, or None if rows are ragged.

>>> rt1 = tf.ragged.constant([[1, 2, 3], [4], [5, 6], [7, 8, 9, 10]])
>>> print(rt1.uniform_row_length)  # rows are ragged.
None

>>> rt2 = tf.RaggedTensor.from_uniform_row_length(
...     values=rt1, uniform_row_length=2)
>>> print(rt2)
<tf.RaggedTensor [[[1, 2, 3], [4]], [[5, 6], [7, 8, 9, 10]]]>
>>> print(rt2.uniform_row_length)  # rows are not ragged (all have size 2).
tf.Tensor(2, shape=(), dtype=int64)

A RaggedTensor's rows are only considered to be uniform (i.e. non-ragged)
if it can be determined statically (at graph construction time) that the
rows all have the same length.

Returns:
  A scalar integer `Tensor`, specifying the length of every row in this
  ragged tensor (for ragged tensors whose rows are uniform); or `None`
  (for ragged tensors whose rows are ragged).


def forward(self, input_ids: Optional[torch.LongTensor]=None, attention_mask: Optional[torch.Tensor]=None, encoder_hidden_states: Optional[torch.FloatTensor]=None, encoder_attention_mask: Optional[torch.FloatTensor]=None, head_mask: Optional[torch.Tensor]=None, cross_attn_head_mask: Optional[torch.Tensor]=None, past_key_values: Optional[List[torch.FloatTensor]]=None, inputs_embeds: Optional[torch.FloatTensor]=None, labels: Optional[torch.LongTensor]=None, use_cache: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None) -> Union[Tuple, CausalLMOutputWithCrossAttentions]:
    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
    outputs = self.model.decoder(input_ids=input_ids, attention_mask=attention_mask, encoder_hidden_states=encoder_hidden_states, encoder_attention_mask=encoder_attention_mask, head_mask=head_mask, cross_attn_head_mask=cross_attn_head_mask, past_key_values=past_key_values, inputs_embeds=inputs_embeds, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict)
    logits = self.lm_head(outputs[0])
    loss = None
    if labels is not None:
        loss_fct = CrossEntropyLoss()
        loss = loss_fct(logits.view(-1, self.config.vocab_size), labels.view(-1))
    if not return_dict:
        output = (logits,) + outputs[1:]
        return (loss,) + output if loss is not None else output
    return CausalLMOutputWithCrossAttentions(loss=loss, logits=logits, past_key_values=outputs.past_key_values, hidden_states=outputs.hidden_states, attentions=outputs.attentions, cross_attentions=outputs.cross_attentions)

cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*):
    Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`:

    - 1 indicates the head is **not masked**,
    - 0 indicates the head is **masked**.
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
    Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
    config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
    (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

Example:

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

>>> tokenizer = AutoTokenizer.from_pretrained("RUCAIBox/mvp")
>>> model = MvpForCausalLM.from_pretrained("RUCAIBox/mvp", add_cross_attention=False)

>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)

>>> logits = outputs.logits
>>> list(logits.shape)
[1, 8, 50267]
```


def forward(self, hidden_states, attention_mask, output_attentions=False, query_states=None, relative_pos=None, rel_embeddings=None):
    if query_states is None:
        query_states = hidden_states
    query_layer = self.transpose_for_scores(self.query_proj(query_states), self.num_attention_heads)
    key_layer = self.transpose_for_scores(self.key_proj(hidden_states), self.num_attention_heads)
    value_layer = self.transpose_for_scores(self.value_proj(hidden_states), self.num_attention_heads)
    rel_att = None
    scale_factor = 1
    if 'c2p' in self.pos_att_type:
        scale_factor += 1
    if 'p2c' in self.pos_att_type:
        scale_factor += 1
    scale = torch.sqrt(torch.tensor(query_layer.size(-1), dtype=torch.float) * scale_factor)
    attention_scores = torch.bmm(query_layer, key_layer.transpose(-1, -2) / scale.to(dtype=query_layer.dtype))
    if self.relative_attention:
        rel_embeddings = self.pos_dropout(rel_embeddings)
        rel_att = self.disentangled_attention_bias(query_layer, key_layer, relative_pos, rel_embeddings, scale_factor)
    if rel_att is not None:
        attention_scores = attention_scores + rel_att
    attention_scores = attention_scores
    attention_scores = attention_scores.view(-1, self.num_attention_heads, attention_scores.size(-2), attention_scores.size(-1))
    attention_probs = XSoftmax.apply(attention_scores, attention_mask, -1)
    attention_probs = self.dropout(attention_probs)
    context_layer = torch.bmm(attention_probs.view(-1, attention_probs.size(-2), attention_probs.size(-1)), value_layer)
    context_layer = context_layer.view(-1, self.num_attention_heads, context_layer.size(-2), context_layer.size(-1)).permute(0, 2, 1, 3).contiguous()
    new_context_layer_shape = context_layer.size()[:-2] + (-1,)
    context_layer = context_layer.view(new_context_layer_shape)
    if output_attentions:
        return (context_layer, attention_probs)
    else:
        return context_layer

Call the module

Args:
    hidden_states (`torch.FloatTensor`):
        Input states to the module usually the output from previous layer, it will be the Q,K and V in
        *Attention(Q,K,V)*

    attention_mask (`torch.BoolTensor`):
        An attention mask matrix of shape [*B*, *N*, *N*] where *B* is the batch size, *N* is the maximum
        sequence length in which element [i,j] = *1* means the *i* th token in the input can attend to the *j*
        th token.

    output_attentions (`bool`, *optional*):
        Whether return the attention matrix.

    query_states (`torch.FloatTensor`, *optional*):
        The *Q* state in *Attention(Q,K,V)*.

    relative_pos (`torch.LongTensor`):
        The relative position encoding between the tokens in the sequence. It's of shape [*B*, *N*, *N*] with
        values ranging in [*-max_relative_positions*, *max_relative_positions*].

    rel_embeddings (`torch.FloatTensor`):
        The embedding of relative distances. It's a tensor of shape [\(2 \times
        \text{max_relative_positions}\), *hidden_size*].


class ConvBertSequenceSummary(nn.Module):

    def __init__(self, config: ConvBertConfig):
        super().__init__()
        self.summary_type = getattr(config, 'summary_type', 'last')
        if self.summary_type == 'attn':
            raise NotImplementedError
        self.summary = nn.Identity()
        if hasattr(config, 'summary_use_proj') and config.summary_use_proj:
            if hasattr(config, 'summary_proj_to_labels') and config.summary_proj_to_labels and (config.num_labels > 0):
                num_classes = config.num_labels
            else:
                num_classes = config.hidden_size
            self.summary = nn.Linear(config.hidden_size, num_classes)
        activation_string = getattr(config, 'summary_activation', None)
        self.activation: Callable = get_activation(activation_string) if activation_string else nn.Identity()
        self.first_dropout = nn.Identity()
        if hasattr(config, 'summary_first_dropout') and config.summary_first_dropout > 0:
            self.first_dropout = nn.Dropout(config.summary_first_dropout)
        self.last_dropout = nn.Identity()
        if hasattr(config, 'summary_last_dropout') and config.summary_last_dropout > 0:
            self.last_dropout = nn.Dropout(config.summary_last_dropout)

    def forward(self, hidden_states: torch.FloatTensor, cls_index: Optional[torch.LongTensor]=None) -> torch.FloatTensor:
        """
        Compute a single vector summary of a sequence hidden states.

        Args:
            hidden_states (`torch.FloatTensor` of shape `[batch_size, seq_len, hidden_size]`):
                The hidden states of the last layer.
            cls_index (`torch.LongTensor` of shape `[batch_size]` or `[batch_size, ...]` where ... are optional leading dimensions of `hidden_states`, *optional*):
                Used if `summary_type == "cls_index"` and takes the last token of the sequence as classification token.

        Returns:
            `torch.FloatTensor`: The summary of the sequence hidden states.
        """
        if self.summary_type == 'last':
            output = hidden_states[:, -1]
        elif self.summary_type == 'first':
            output = hidden_states[:, 0]
        elif self.summary_type == 'mean':
            output = hidden_states.mean(dim=1)
        elif self.summary_type == 'cls_index':
            if cls_index is None:
                cls_index = torch.full_like(hidden_states[..., :1, :], hidden_states.shape[-2] - 1, dtype=torch.long)
            else:
                cls_index = cls_index.unsqueeze(-1).unsqueeze(-1)
                cls_index = cls_index.expand((-1,) * (cls_index.dim() - 1) + (hidden_states.size(-1),))
            output = hidden_states.gather(-2, cls_index).squeeze(-2)
        elif self.summary_type == 'attn':
            raise NotImplementedError
        output = self.first_dropout(output)
        output = self.summary(output)
        output = self.activation(output)
        output = self.last_dropout(output)
        return output

Compute a single vector summary of a sequence hidden states.

Args:
    config ([`ConvBertConfig`]):
        The config used by the model. Relevant arguments in the config class of the model are (refer to the actual
        config class of your model for the default values it uses):

        - **summary_type** (`str`) -- The method to use to make this summary. Accepted values are:

            - `"last"` -- Take the last token hidden state (like XLNet)
            - `"first"` -- Take the first token hidden state (like Bert)
            - `"mean"` -- Take the mean of all tokens hidden states
            - `"cls_index"` -- Supply a Tensor of classification token position (GPT/GPT-2)
            - `"attn"` -- Not implemented now, use multi-head attention

        - **summary_use_proj** (`bool`) -- Add a projection after the vector extraction.
        - **summary_proj_to_labels** (`bool`) -- If `True`, the projection outputs to `config.num_labels` classes
          (otherwise to `config.hidden_size`).
        - **summary_activation** (`Optional[str]`) -- Set to `"tanh"` to add a tanh activation to the output,
          another string or `None` will add no activation.
        - **summary_first_dropout** (`float`) -- Optional dropout probability before the projection and activation.
        - **summary_last_dropout** (`float`)-- Optional dropout probability after the projection and activation.


def remat(f):
    return tf.recompute_grad(f)

Implementation of rematerialization.

Args:
    f: The function or operation to rematerialize.
Returns:
    A function wrapping f that defines a custom gradient, which
    recomputes f on the backwards pass of a gradient call.


class SamHQPromptEncoderConfig(SamPromptEncoderConfig):
    pass

This is the configuration class to store the configuration of a [`SamHQPromptEncoderModel`].The [`SamHQPromptEncoderModel`]
module is used to encode the input 2D points and bounding boxes. Instantiating a configuration defaults will yield a
similar configuration to that of the SAM_HQ model. The configuration is used to store the configuration of the model.
[Uminosachi/sam-hq](https://huggingface.co/Uminosachi/sam-hq) architecture.

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

Args:
    hidden_size (`int`, *optional*, defaults to 256):
        Dimensionality of the hidden states.
    image_size (`int`, *optional*, defaults to 1024):
        The expected output resolution of the image.
    patch_size (`int`, *optional*, defaults to 16):
        The size (resolution) of each patch.
    mask_input_channels (`int`, *optional*, defaults to 16):
        The number of channels to be fed to the `MaskDecoder` module.
    num_point_embeddings (`int`, *optional*, defaults to 4):
        The number of point embeddings to be used.
    hidden_act (`str`, *optional*, defaults to `"gelu"`):
        The non-linear activation function in the encoder and pooler.


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

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

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

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


class OwlViTTextConfig(PretrainedConfig):
    model_type = 'owlvit_text_model'
    base_config_key = 'text_config'

    def __init__(self, vocab_size=49408, hidden_size=512, intermediate_size=2048, num_hidden_layers=12, num_attention_heads=8, max_position_embeddings=16, hidden_act='quick_gelu', layer_norm_eps=1e-05, attention_dropout=0.0, initializer_range=0.02, initializer_factor=1.0, pad_token_id=0, bos_token_id=49406, eos_token_id=49407, **kwargs):
        super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
        self.vocab_size = vocab_size
        self.hidden_size = hidden_size
        self.intermediate_size = intermediate_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.max_position_embeddings = max_position_embeddings
        self.hidden_act = hidden_act
        self.layer_norm_eps = layer_norm_eps
        self.attention_dropout = attention_dropout
        self.initializer_range = initializer_range
        self.initializer_factor = initializer_factor

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

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

Args:
    vocab_size (`int`, *optional*, defaults to 49408):
        Vocabulary size of the OWL-ViT text model. Defines the number of different tokens that can be represented
        by the `inputs_ids` passed when calling [`OwlViTTextModel`].
    hidden_size (`int`, *optional*, defaults to 512):
        Dimensionality of the encoder layers and the pooler layer.
    intermediate_size (`int`, *optional*, defaults to 2048):
        Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
    num_hidden_layers (`int`, *optional*, defaults to 12):
        Number of hidden layers in the Transformer encoder.
    num_attention_heads (`int`, *optional*, defaults to 8):
        Number of attention heads for each attention layer in the Transformer encoder.
    max_position_embeddings (`int`, *optional*, defaults to 16):
        The maximum sequence length that this model might ever be used with. Typically set this to something large
        just in case (e.g., 512 or 1024 or 2048).
    hidden_act (`str` or `function`, *optional*, defaults to `"quick_gelu"`):
        The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
        `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
    layer_norm_eps (`float`, *optional*, defaults to 1e-05):
        The epsilon used by the layer normalization layers.
    attention_dropout (`float`, *optional*, defaults to 0.0):
        The dropout ratio for the attention probabilities.
    initializer_range (`float`, *optional*, defaults to 0.02):
        The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
    initializer_factor (`float`, *optional*, defaults to 1.0):
        A factor for initializing all weight matrices (should be kept to 1, used internally for initialization
        testing).
    pad_token_id (`int`, *optional*, defaults to 0):
        The id of the padding token in the input sequences.
    bos_token_id (`int`, *optional*, defaults to 49406):
        The id of the beginning-of-sequence token in the input sequences.
    eos_token_id (`int`, *optional*, defaults to 49407):
        The id of the end-of-sequence token in the input sequences.

Example:

```python
>>> from transformers import OwlViTTextConfig, OwlViTTextModel

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

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

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


def forward(self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, position_bias: Optional[torch.Tensor]=None, output_attentions: Optional[bool]=False, past_key_values: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, use_cache: Optional[bool]=None):
    outputs = self.layernorm_before_attention(hidden_states)
    outputs = self.self_attention(outputs, outputs, attention_mask, position_bias, output_attentions, past_key_values, use_cache)
    outputs, attn_weights, current_key_value = outputs
    if self.dropout is not None:
        outputs = self.dropout(outputs)
    hidden_states = hidden_states + outputs
    return (hidden_states, attn_weights, current_key_value)

Args:
    hidden_states (`torch.Tensor` of shape `(batch, len_seq, dim_model)`):
        Input of transformer block(self-attention block). It can be the raw embedding of a batch of sequences.
    attention_mask (`torch.Tensor` of shape `(batch, len_seq, len_seq)`):
        Avoid invalid areas to participate in the calculation of self-attention.
    position_bias (`torch.Tensor` of shape `(batch, len_seq, len_seq)`):
        Provide positional information to self-attention block.
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers.
    past_key_values (`Tuple(torch.FloatTensor)`, *optional*):
        Cached past key and value projection states.
    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`).


def cashflows(self, market: pmd.ProcessedMarketData, past_fixing: Optional[types.FloatTensor]=None, name: Optional[str]=None) -> Tuple[types.DateTensor, types.FloatTensor]:
    name = name or self._name + '_cashflows'
    with tf.name_scope(name):
        _, forward_rates = self.forward_rates(market, past_fixing=past_fixing)
        coupon_rate = forward_rates + tf.expand_dims(self._spread, axis=-1)
        notional = tf.expand_dims(self._notional, axis=-1)
        cashflows = notional * (self._daycount_fractions * coupon_rate)
        return (self._coupon_end_dates, cashflows)

Returns cashflows for the floating leg.

Args:
  market: An instance of `ProcessedMarketData`.
  past_fixing: An optional `Tensor` of shape compatible with
    `batch_shape + [1]`. Represents the fixings for the cashflows as
    observed at `market.date`.
  name: Python str. The name to give to the ops created by this function.
    Default value: `None` which maps to 'cashflows'.

Returns:
  A tuple of two `Tensor`s of shape `batch_shape + [num_cashflows]` and
  containing the dates and the corresponding cashflows price for each
  stream based on the input market data.


def create_rag_adapter() -> EmbeddingTypeAdapter[Chunk, Chunk]:
    return EmbeddingTypeAdapter(input_fn=_extract_chunk_text, output_fn=_add_embedding_fn)

Creates adapter for converting between Chunk and Embedding types.

The adapter:
- Extracts text from Chunk.content.text for embedding
- Creates Embedding objects from model output
- Sets Embedding in Chunk.embedding

Returns:
    EmbeddingTypeAdapter configured for RAG pipeline types


class AveragePooling1D(BasePooling):

    def __init__(self, pool_size, strides=None, padding='valid', data_format=None, name=None, **kwargs):
        super().__init__(pool_size, strides, pool_dimensions=1, pool_mode='average', padding=padding, data_format=data_format, name=name, **kwargs)

Average pooling for temporal data.

Downsamples the input representation by taking the average value over the
window defined by `pool_size`. The window is shifted by `strides`.  The
resulting output when using "valid" padding option has a shape of:
`output_shape = (input_shape - pool_size + 1) / strides)`

The resulting output shape when using the "same" padding option is:
`output_shape = input_shape / strides`

Args:
    pool_size: int, size of the max pooling window.
    strides: int or None. Specifies how much the pooling window moves
        for each pooling step. If None, it will default to `pool_size`.
    padding: string, either `"valid"` or `"same"` (case-insensitive).
        `"valid"` means no padding. `"same"` results in padding evenly to
        the left/right or up/down of the input such that output has the same
        height/width dimension as the input.
    data_format: string, either `"channels_last"` or `"channels_first"`.
        The ordering of the dimensions in the inputs. `"channels_last"`
        corresponds to inputs with shape `(batch, steps, features)`
        while `"channels_first"` corresponds to inputs with shape
        `(batch, features, steps)`. It defaults to the `image_data_format`
        value found in your Keras config file at `~/.keras/keras.json`.
        If you never set it, then it will be `"channels_last"`.

Input shape:

- If `data_format="channels_last"`:
    3D tensor with shape `(batch_size, steps, features)`.
- If `data_format="channels_first"`:
    3D tensor with shape `(batch_size, features, steps)`.

Output shape:

- If `data_format="channels_last"`:
    3D tensor with shape `(batch_size, downsampled_steps, features)`.
- If `data_format="channels_first"`:
    3D tensor with shape `(batch_size, features, downsampled_steps)`.

Examples:

`strides=1` and `padding="valid"`:

>>> x = np.array([1., 2., 3., 4., 5.])
>>> x = np.reshape(x, [1, 5, 1])
>>> avg_pool_1d = keras.layers.AveragePooling1D(pool_size=2,
...    strides=1, padding="valid")
>>> avg_pool_1d(x)

`strides=2` and `padding="valid"`:

>>> x = np.array([1., 2., 3., 4., 5.])
>>> x = np.reshape(x, [1, 5, 1])
>>> avg_pool_1d = keras.layers.AveragePooling1D(pool_size=2,
...    strides=2, padding="valid")
>>> avg_pool_1d(x)

`strides=1` and `padding="same"`:

>>> x = np.array([1., 2., 3., 4., 5.])
>>> x = np.reshape(x, [1, 5, 1])
>>> avg_pool_1d = keras.layers.AveragePooling1D(pool_size=2,
...    strides=1, padding="same")
>>> avg_pool_1d(x)