def check_compatibility(self):
    usr_keys = list(self.usr_config.keys())
    for k in self.usr_config.keys():
        if k not in usr_keys:
            err_msg = '[Error] Required config not found in user config.'
            err_msg += '(required = %s, ' % str(k)
            err_msg += 'user configs = %s)' % str(usr_keys)
            logging.error(err_msg)
            self.error_msg.append(err_msg)
            self.failures.append([k, err_msg])
            return False
    overall_status = True
    for config_name, spec in self.usr_config.items():
        temp_status = True
        in_required = config_name in list(self.required.keys())
        in_optional = config_name in list(self.optional.keys())
        in_unsupported = config_name in list(self.unsupported.keys())
        in_dependency = config_name in list(self.dependency.keys())
        if not (in_required or in_optional or in_unsupported or in_dependency):
            warn_msg = '[Error] User config not defined in config file.'
            warn_msg += '(user config = %s)' % str(config_name)
            logging.warning(warn_msg)
            self.warning_msg.append(warn_msg)
            self.failures.append([config_name, warn_msg])
            temp_status = False
        else:
            if in_unsupported:
                if self.in_range(spec, self.unsupported[config_name]):
                    err_msg = '[Error] User config is unsupported. It is '
                    err_msg += "defined under 'Unsupported' section in the config file."
                    err_msg += ' (config = %s, spec = %s)' % (config_name, str(spec))
                    logging.error(err_msg)
                    self.error_msg.append(err_msg)
                    self.failures.append([config_name, err_msg])
                    temp_status = False
            if in_required:
                if not self.in_range(spec, self.required[config_name]):
                    err_msg = '[Error] User config cannot be supported. It is not in '
                    err_msg += "the supported range as defined in the 'Required' "
                    err_msg += 'section. (config = %s, ' % config_name
                    err_msg += 'spec = %s)' % str(spec)
                    logging.error(err_msg)
                    self.error_msg.append(err_msg)
                    self.failures.append([config_name, err_msg])
                    temp_status = False
            if in_optional:
                if not self.in_range(spec, self.optional[config_name]):
                    err_msg = '[Error] User config cannot be supported. It is not in '
                    err_msg += "the supported range as defined in the 'Optional' "
                    err_msg += 'section. (config = %s, ' % config_name
                    err_msg += 'spec = %s)' % str(spec)
                    logging.error(err_msg)
                    self.error_msg.append(err_msg)
                    self.failures.append([config_name, err_msg])
                    temp_status = False
            if in_dependency:
                dep_list = self.dependency[config_name]
                if dep_list:
                    for rule in dep_list:
                        cfg = rule[0]
                        cfg_req = rule[1]
                        dep = rule[2]
                        dep_req = rule[3]
                        try:
                            cfg_name = self.usr_config[cfg]
                            dep_name = self.usr_config[dep]
                            cfg_status = self.in_range(cfg_name, cfg_req)
                            dep_status = self.in_range(dep_name, dep_req)
                            if cfg_status:
                                if not dep_status:
                                    err_msg = '[Error] User config has a dependency that cannot'
                                    err_msg += ' be supported. '
                                    err_msg += "'%s' has a dependency on " % str(config_name)
                                    err_msg += "'%s'." % str(dep)
                                    logging.error(err_msg)
                                    self.error_msg.append(err_msg)
                                    self.failures.append([config_name, err_msg])
                                    temp_status = False
                        except KeyError:
                            err_msg = '[Error] Dependency is missing from `Required`. '
                            err_msg += '(config = %s, dep = %s)' % (cfg, dep)
                            logging.error(err_msg)
                            self.error_msg.append(err_msg)
                            self.failures.append([config_name, err_msg])
                            temp_status = False
        if temp_status:
            self.successes.append([config_name, spec])
        else:
            overall_status = False
    return overall_status

Checks version and dependency compatibility for a given configuration.

    `check_compatibility` immediately returns with `False` (or failure status)
    if any child process or checks fail. For error and warning messages, either
    print `self.(error_msg|warning_msg)` or call `_print` function.

Returns:
        Boolean that is a status of the compatibility check result.


def _get_argspec_for_partial(obj):
    n_prune_args = len(obj.args)
    partial_keywords = obj.keywords or {}
    args, varargs, keywords, defaults = getargspec(obj.func)
    args = args[n_prune_args:]
    no_default = object()
    all_defaults = [no_default] * len(args)
    if defaults:
        all_defaults[-len(defaults):] = defaults
    for kw, default in iter(partial_keywords.items()):
        if kw in args:
            idx = args.index(kw)
            all_defaults[idx] = default
        elif not keywords:
            raise ValueError(f'{obj} does not have a **kwargs parameter, but contains an unknown partial keyword {kw}.')
    first_default = next((idx for idx, x in enumerate(all_defaults) if x is not no_default), None)
    if first_default is None:
        return ArgSpec(args, varargs, keywords, None)
    invalid_default_values = [args[i] for i, j in enumerate(all_defaults) if j is no_default and i > first_default]
    if invalid_default_values:
        raise ValueError(f'{obj} has some keyword-only arguments, which are not supported: {invalid_default_values}.')
    return ArgSpec(args, varargs, keywords, tuple(all_defaults[first_default:]))

Implements `getargspec` for `functools.partial` objects.

Args:
        obj: The `functools.partial` object

Returns:
        An `inspect.ArgSpec`

Raises:
        ValueError: When callable's signature can not be expressed with
        ArgSpec.


def __init__(self, backend: str='quanto', nbits: Optional[int]=4, axis_key: Optional[int]=0, axis_value: Optional[int]=0, q_group_size: Optional[int]=64, residual_length: Optional[int]=128, compute_dtype: Optional[torch.dtype]=torch.float16, device: Optional[str]='cpu'):
    self.backend = backend
    self.nbits = nbits
    self.axis_key = axis_key
    self.axis_value = axis_value
    self.q_group_size = q_group_size
    self.residual_length = residual_length
    self.compute_dtype = compute_dtype
    self.device = device

Configuration class for quantized cache settings.

Attributes:
        backend (`str`, *optional*, defaults to `"quanto"`):
            Backend to use when performing quantization, Can be one of [`quanto`, `HQQ`]
        nbits (`Optional[int]`, *optional*, defaults to 4):
            Number of bits, can be 2 or 4 for the `quanto` backend and one of [1, 2, 3, 4, 8] for the `HQQ` backend. Defaults to 2.
        axis_key (`int`, *optional*, defaults to 0):
            Axis over which to perform grouping for the key tensors. Can be [0, -1] for `quanto` backend and [0, 1] for `HQQ` backend.
        axis_value (`int`, *optional*, defaults to 0):
            Axis over which to perform grouping for the value tensors. Can be [0, -1] for `quanto` backend and [0, 1] for `HQQ` backend.
        q_group_size (`Optional[int]`, *optional*, defaults to 64):
            Size of the quantization group, should be a divisor of the model's hidden dimension.
            Defaults to 64.
        residual_length (`Optional[int]`, *optional*, defaults to 128):
            Length of the residual cache which will always be stored in original precision.
            Defaults to 128.
        compute_dtype (`torch.dtype`, *optional*, defaults to `torch.float16`):
            The default dtype used for computations in the model. Keys and Values will be cast to this dtype after dequantization.
        device (`str`, *optional*, defaults to `"cpu"`):
            Device on which to perform computations, should be same as the model's device.


def assert_split_at_fraction_binary(source, expected_items, num_items_to_read_before_split, left_fraction, left_result, right_fraction, right_result, stats, start_position=None, stop_position=None):
    assert right_fraction > left_fraction
    if right_fraction - left_fraction < 0.001:
        return
    middle_fraction = (left_fraction + right_fraction) / 2
    if left_result is None:
        left_result = _assert_split_at_fraction_behavior(source, expected_items, num_items_to_read_before_split, left_fraction, ExpectedSplitOutcome.MUST_BE_CONSISTENT_IF_SUCCEEDS)
    if right_result is None:
        right_result = _assert_split_at_fraction_behavior(source, expected_items, num_items_to_read_before_split, right_fraction, ExpectedSplitOutcome.MUST_BE_CONSISTENT_IF_SUCCEEDS)
    middle_result = _assert_split_at_fraction_behavior(source, expected_items, num_items_to_read_before_split, middle_fraction, ExpectedSplitOutcome.MUST_BE_CONSISTENT_IF_SUCCEEDS)
    if middle_result[1] != -1:
        stats.successful_fractions.append(middle_fraction)
    if middle_result[1] > 0:
        stats.non_trivial_fractions.append(middle_fraction)
    if left_result[0] != middle_result[0]:
        assert_split_at_fraction_binary(source, expected_items, num_items_to_read_before_split, left_fraction, left_result, middle_fraction, middle_result, stats)
    if right_fraction == 1.0 or middle_result[0] != right_result[0]:
        assert_split_at_fraction_binary(source, expected_items, num_items_to_read_before_split, middle_fraction, middle_result, right_fraction, right_result, stats)

Performs dynamic work rebalancing for fractions within a given range.

    Asserts that given a start position, a source can be split at every
    interesting fraction (halfway between two fractions that differ by at
    least one item) and the results are consistent if a split succeeds.

Args:
        source: source to perform dynamic splitting on.
        expected_items: total set of items expected when reading the source.
        num_items_to_read_before_split: number of items to read before splitting.
        left_fraction: left fraction for binary splitting.
        left_result: result received by splitting at left fraction.
        right_fraction: right fraction for binary splitting.
        right_result: result received by splitting at right fraction.
        stats: a ``SplitFractionStatistics`` for storing results.


def _validate_signature_def_map(self, signature_def_map):
    for signature_def_key in signature_def_map:
        signature_def = signature_def_map[signature_def_key]
        inputs = signature_def.inputs
        outputs = signature_def.outputs
        for inputs_key in inputs:
            self._validate_tensor_info(inputs[inputs_key])
        for outputs_key in outputs:
            self._validate_tensor_info(outputs[outputs_key])
    if constants.INIT_OP_SIGNATURE_KEY in signature_def_map:
        raise KeyError(f'SignatureDef map key "{constants.INIT_OP_SIGNATURE_KEY}" is reserved for initialization. Please use a different key.')
    if constants.TRAIN_OP_SIGNATURE_KEY in signature_def_map:
        raise KeyError(f'SignatureDef map key "{constants.TRAIN_OP_SIGNATURE_KEY}" is reserved for the train op. Please use a different key.')

Validates the `SignatureDef` entries in the signature def map.

    Validation of entries in the signature def map includes ensuring that the
    `name` and `dtype` fields of the TensorInfo protos of the `inputs` and
    `outputs` of each `SignatureDef` are populated. Also ensures that reserved
    SignatureDef keys for the initialization and train ops are not used.

Args:
        signature_def_map: The map of signature defs to be validated.

Raises:
        AssertionError: If a TensorInfo is not valid.
        KeyError: If a reserved signature key is used in the map.


def to_channel_dimension_format(image: np.ndarray, channel_dim: Union[ChannelDimension, str], input_channel_dim: Optional[Union[ChannelDimension, str]]=None) -> np.ndarray:
    if not isinstance(image, np.ndarray):
        raise ValueError(f'Input image must be of type np.ndarray, got {type(image)}')
    if input_channel_dim is None:
        input_channel_dim = infer_channel_dimension_format(image)
    target_channel_dim = ChannelDimension(channel_dim)
    if input_channel_dim == target_channel_dim:
        return image
    if target_channel_dim == ChannelDimension.FIRST:
        image = image.transpose((2, 0, 1))
    elif target_channel_dim == ChannelDimension.LAST:
        image = image.transpose((1, 2, 0))
    else:
        raise ValueError('Unsupported channel dimension format: {}'.format(channel_dim))
    return image

Converts `image` to the channel dimension format specified by `channel_dim`.

Args:
        image (`numpy.ndarray`):
            The image to have its channel dimension set.
        channel_dim (`ChannelDimension`):
            The channel dimension format to use.
        input_channel_dim (`ChannelDimension`, *optional*):
            The channel dimension format of the input image. If not provided, it will be inferred from the input image.

Returns:
        `np.ndarray`:
            The image with the channel dimension set to `channel_dim`.


def call_replica_local_fn(fn, *args, **kwargs):
    strategy = None
    if 'strategy' in kwargs:
        strategy = kwargs.pop('strategy')
    elif distribute_lib.has_strategy():
        strategy = distribute_lib.get_strategy()
    is_tpu = backend.is_tpu_strategy(strategy)
    if not is_tpu and strategy and distribute_lib.in_cross_replica_context():
        with strategy.scope():
            return strategy.extended.call_for_each_replica(fn, args, kwargs)
    return fn(*args, **kwargs)

Call a function that uses replica-local variables.

    This function correctly handles calling `fn` in a cross-replica
    context.

Args:
        fn: The function to call.
        *args: Positional arguments to the `fn`.
        **kwargs: Keyword argument to `fn`.

Returns:
        The result of calling `fn`.


def hsv_to_rgb(images, data_format=None):
    if any_symbolic_tensors((images,)):
        return HSVToRGB(data_format=data_format).symbolic_call(images)
    return backend.image.hsv_to_rgb(images, data_format=data_format)

Convert HSV images to RGB.

    `images` must be of float dtype, and the output is only well defined if the
    values in `images` are in `[0, 1]`.

Args:
        images: Input image or batch of images. Must be 3D or 4D.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

Returns:
        RGB image or batch of RGB images.

Example:
    >>> import numpy as np
    >>> from keras import ops
    >>> x = np.random.random((2, 4, 4, 3))
    >>> y = ops.image.hsv_to_rgb(x)
    >>> y.shape
    (2, 4, 4, 3)

    >>> x = np.random.random((4, 4, 3)) # Single HSV image
    >>> y = ops.image.hsv_to_rgb(x)
    >>> y.shape
    (4, 4, 3)

    >>> x = np.random.random((2, 3, 4, 4))
    >>> y = ops.image.hsv_to_rgb(x, data_format="channels_first")
    >>> y.shape
    (2, 3, 4, 4)


def get_v2_constants(module: Any) -> Sequence[str]:
    constants_v2 = []
    tensorflow_constants_attr = API_ATTRS[TENSORFLOW_API_NAME].constants
    if hasattr(module, tensorflow_constants_attr):
        constants_v2.extend(getattr(module, tensorflow_constants_attr))
    return constants_v2

Get a list of TF 2.0 constants in this module.

Args:
        module: TensorFlow module.

Returns:
        List of all API constants under the given module.


def byte_swap_string_content(buffer, from_endiness, to_endiness):
    num_of_strings = int.from_bytes(buffer.data[0:4], from_endiness)
    string_content = bytearray(buffer.data[4 * (num_of_strings + 2):])
    prefix_data = b''.join([int.from_bytes(buffer.data[i:i + 4], from_endiness).to_bytes(4, to_endiness) for i in range(0, (num_of_strings + 1) * 4 + 1, 4)])
    buffer.data = prefix_data + string_content

Helper function for byte-swapping the string buffer.

Args:
        buffer: TFLite string buffer of from_endiness format.
        from_endiness: The original endianness format of the string buffer.
        to_endiness: The destined endianness format of the string buffer.


def init_variable(v, init, name='init'):
    with ops.name_scope(None, v.op.name + '/', [v, init]):
        with ops.name_scope(name) as scope:
            with ops.colocate_with(v):
                if callable(init):
                    assert v.get_shape().is_fully_defined(), 'Variable shape unknown.'
                    value = init(v.get_shape().as_list(), v.dtype.base_dtype)
                    value = ops.convert_to_tensor(value, name='value')
                    return gen_state_ops.assign(v, value, name=scope)
                else:
                    init = ops.convert_to_tensor(init, name='init')
                    return gen_state_ops.assign(v, init, name=scope)

Initializes variable with "init".

    This op does the following:
    if init is a Tensor, v = init
    if callable(init): v = init(VariableShape(v), v.dtype)

Args:
        v: Variable to initialize
        init: Tensor to assign to v,
        Or an object convertible to Tensor e.g. nparray,
        Or an Initializer that generates a tensor given the shape and type of v.
        An "Initializer" is a callable that returns a tensor that "v" should be
        set to. It will be called as init(shape, dtype).
        name: Optional name for the op.

Returns:
        The operation that initializes v.


def handle_malformed_config(error: MalformedConfigError) -> ResponseReturnValue:
    return (DQMResponse(name='MalformedConfigError', description=str(error), code=400), 400)

DQM Malformed Config Response.

Args:
        * error: Config error

Returns:
        * DQMResponse for the error with a 400 status code


def reducer_override(self, obj):
    t = type(obj)
    try:
        is_anyclass = issubclass(t, type)
    except TypeError:
        is_anyclass = False
    if is_anyclass:
        return _class_reduce(obj)
    elif isinstance(obj, types.FunctionType):
        return self._function_reduce(obj)
    else:
        return NotImplemented

Type-agnostic reducing callback for function and classes.

    For performance reasons, subclasses of the C `pickle.Pickler` class
    cannot register custom reducers for functions and classes in the
    dispatch_table attribute. Reducers for such types must instead
    implemented via the special `reducer_override` method.

    Note that this method will be called for any object except a few
    builtin-types (int, lists, dicts etc.), which differs from reducers
    in the Pickler's dispatch_table, each of them being invoked for
    objects of a specific type only.

    This property comes in handy for classes: although most classes are
    instances of the ``type`` metaclass, some of them can be instances
    of other custom metaclasses (such as enum.EnumMeta for example). In
    particular, the metaclass will likely not be known in advance, and
    thus cannot be special-cased using an entry in the dispatch_table.
    reducer_override, among other things, allows us to register a
    reducer that will be called for any class, independently of its
    type.

Note:
    * reducer_override has the priority over dispatch_table-registered
    reducers.
    * reducer_override can be used to fix other limitations of
        cloudpickle for other types that suffered from type-specific
        reducers, such as Exceptions. See
        https://github.com/cloudpipe/cloudpickle/issues/248


def field(*, validate: Optional[Callable[[_In], _OutT]]=None, **kwargs: Any) -> dataclasses.Field[_OutT]:
    if validate is None:
        return dataclasses.field(**kwargs)
    else:
        field_ = _Field(validate=validate, field_kwargs=kwargs)
        return typing.cast(dataclasses.Field, field_)

Like `dataclasses.field`, but allow `validator`.

Args:
        validate: A callable `(x) -> x` called each time the variable is assigned.
        **kwargs: Kwargs forwarded to `dataclasses.field`

Returns:
        The field.


def __init__(self, decorator: Callable[[_GenericCallable], _GenericCallable]):
    ...

A type annotation for decorators that do not change signatures.

    This is a stand-in for using `Callable[[T], T]` to represent a decorator.

    Given a decorator function, which takes in a callable and returns a callable
    with the same signature, apply this class as a decorator to that function.
    This can also be used for decorator factories.

Example:
    Plain decorator (decorator matches Callable[[T], T]):

    >>> @pytype_extensions.Decorator
    ... def MyDecorator(func):
    ...   def wrapper(...):
    ...     ...
    ...   return wrapper

    Decorator factory (factory matches Callable[..., Callable[[T], T]]):

    >>> def MyDecoratorFactory(foo: int) -> pytype_extensions.Decorator:
    ...   @pytype_extensions.Decorator
    ...   def MyDecorator(func):
    ...     def Wrapper(*args, **kwargs):
    ...       return func(foo, *args, **kwargs)
    ...     return Wrapper
    ...   return MyDecorator

    Note for the above example: the return type annotation (first line) is the
    most important one; it indicates to callers that MyDecoratorFactory is
    returning a decorator. The "@pytype_extensions.Decorator" annotation (second
    line) indicates to pytype that MyDecorator is a Decorator; without it, you
    would need to add "pytype: disable=bad-return-type" on the final line.

    This class only exists at build time, for typechecking. At runtime, the
    'Decorator' member of this module is a simple identity function (see below).

    More information: pytype-decorators

    Shortlink: pytype_extensions.Decorator


def forward(self, hidden_states):
    hidden_states = hidden_states.transpose(1, 2)
    hidden_states = self.pointwise_conv1(hidden_states)
    hidden_states = nn.functional.glu(hidden_states, dim=1)
    hidden_states = self.depthwise_conv(hidden_states)
    hidden_states = self.norm(hidden_states)
    hidden_states = hidden_states * torch.sigmoid(hidden_states)
    hidden_states = self.pointwise_conv2(hidden_states)
    return hidden_states.transpose(1, 2)

Compute convolution module.

Args:
        hidden_states (`torch.Tensor` of shape `(batch, time, channels)`): Input tensor.

Returns:
        `torch.Tensor`: Output tensor of shape `(batch, time, channels)`.


def sym_has(self, path: Union[utils.KeyPath, str, int]) -> bool:
    return utils.KeyPath.from_value(path).exists(self)

Returns True if a path exists in the sub-tree.

Args:
        path: A KeyPath object or equivalence.

Returns:
        True if the path exists in current sub-tree, otherwise False.


def float_operation():
    return {'max_depth': 10000, 'min_bytes': 0, 'min_micros': 0, 'min_params': 0, 'min_float_ops': 1, 'min_occurrence': 0, 'order_by': 'float_ops', 'account_type_regexes': ['.*'], 'start_name_regexes': ['.*'], 'trim_name_regexes': [], 'show_name_regexes': ['.*'], 'hide_name_regexes': [], 'account_displayed_op_only': True, 'select': ['float_ops'], 'step': -1, 'output': 'stdout'}

Options used to profile float operations.

    Please see https://github.com/tensorflow/tensorflow/tree/master/tensorflow/core/profiler/g3doc/profile_model_architecture.md
    on the caveats of calculating float operations.

Returns:
        A dict of profiling options.


def export(preprocessor: Union['PreTrainedTokenizer', 'FeatureExtractionMixin', 'ProcessorMixin'], model: Union['PreTrainedModel', 'TFPreTrainedModel'], config: OnnxConfig, opset: int, output: Path, tokenizer: Optional['PreTrainedTokenizer']=None, device: str='cpu') -> Tuple[List[str], List[str]]:
    if not (is_torch_available() or is_tf_available()):
        raise ImportError('Cannot convert because neither PyTorch nor TensorFlow are not installed. Please install torch or tensorflow first.')
    if is_tf_available() and isinstance(model, TFPreTrainedModel) and (device == 'cuda'):
        raise RuntimeError('`tf2onnx` does not support export on CUDA device.')
    if isinstance(preprocessor, PreTrainedTokenizerBase) and tokenizer is not None:
        raise ValueError('You cannot provide both a tokenizer and a preprocessor to export the model.')
    if tokenizer is not None:
        warnings.warn('The `tokenizer` argument is deprecated and will be removed in version 5 of Transformers. Use `preprocessor` instead.', FutureWarning)
        logger.info('Overwriting the `preprocessor` argument with `tokenizer` to generate dummy inputs.')
        preprocessor = tokenizer
    if is_torch_available():
        from ..utils import get_torch_version
        if not config.is_torch_support_available:
            logger.warning(f'Unsupported PyTorch version for this model. Minimum required is {config.torch_onnx_minimum_version}, got: {get_torch_version()}')
    if is_torch_available() and issubclass(type(model), PreTrainedModel):
        return export_pytorch(preprocessor, model, config, opset, output, tokenizer=tokenizer, device=device)
    elif is_tf_available() and issubclass(type(model), TFPreTrainedModel):
        return export_tensorflow(preprocessor, model, config, opset, output, tokenizer=tokenizer)

Export a Pytorch or TensorFlow model to an ONNX Intermediate Representation (IR)

Args:
        preprocessor: ([`PreTrainedTokenizer`], [`FeatureExtractionMixin`] or [`ProcessorMixin`]):
            The preprocessor used for encoding the data.
        model ([`PreTrainedModel`] or [`TFPreTrainedModel`]):
            The model to export.
        config ([`~onnx.config.OnnxConfig`]):
            The ONNX configuration associated with the exported model.
        opset (`int`):
            The version of the ONNX operator set to use.
        output (`Path`):
            Directory to store the exported ONNX model.
        device (`str`, *optional*, defaults to `cpu`):
            The device on which the ONNX model will be exported. Either `cpu` or `cuda`. Only PyTorch is supported for
            export on CUDA devices.

Returns:
        `Tuple[List[str], List[str]]`: A tuple with an ordered list of the model's inputs, and the named inputs from
        the ONNX configuration.


def normalize(self, image: np.ndarray, mean: Union[float, Iterable[float]], std: Union[float, Iterable[float]], data_format: Optional[Union[str, ChannelDimension]]=None, input_data_format: Optional[Union[str, ChannelDimension]]=None, **kwargs) -> np.ndarray:
    return normalize(image, mean=mean, std=std, data_format=data_format, input_data_format=input_data_format, **kwargs)

Normalize an image. image = (image - image_mean) / image_std.

Args:
        image (`np.ndarray`):
            Image to normalize.
        mean (`float` or `Iterable[float]`):
            Image mean to use for normalization.
        std (`float` or `Iterable[float]`):
            Image standard deviation to use for normalization.
        data_format (`str` or `ChannelDimension`, *optional*):
            The channel dimension format for the output image. If unset, the channel dimension format of the input
            image is used. Can be one of:
            - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
            - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
        input_data_format (`ChannelDimension` or `str`, *optional*):
            The channel dimension format for the input image. If unset, the channel dimension format is inferred
            from the input image. Can be one of:
            - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
            - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.

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


def experimental_local_results(self, value):
    return super(CentralStorageStrategy, self).experimental_local_results(value)

Returns the list of all local per-replica values contained in `value`.

    In `CentralStorageStrategy` there is a single worker so the value returned
    will be all the values on that worker.

Args:
        value: A value returned by `run()`, `extended.call_for_each_replica()`,
        or a variable created in `scope`.

Returns:
        A tuple of values contained in `value`. If `value` represents a single
        value, this returns `(value,).`


def process(self, feed_item):
    if not feed_item.get(FieldMap.CAMPAIGN_CREATIVE_ASSOCIATION_ID, None):
        campaign = self.campaign_dao.get(feed_item, required=True)
        creative = self.creative_dao.get(feed_item, required=True)
        if campaign and creative:
            if campaign:
                feed_item[FieldMap.CAMPAIGN_ID] = campaign['id']
                feed_item[FieldMap.CAMPAIGN_NAME] = campaign['name']
            association = {'creativeId': str(creative['id'])}
            result = self._api().insert(profileId=self.profile_id, campaignId=str(campaign['id']), body=association).execute()
            feed_item[FieldMap.CAMPAIGN_CREATIVE_ASSOCIATION_ID] = '%s|%s' % (campaign['id'], creative['id'])
            return result
    return None

Processes a feed item by creating the creative association in DCM.

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

Returns:
        The newly created object from DCM.


def in_top_k_v2(targets, predictions, k, name=None):
    return in_top_k(predictions, targets, k, name)

Outputs whether the targets are in the top `K` predictions.

    This outputs a `batch_size` bool array, an entry `out[i]` is `true` if the
    prediction for the target class is finite (not inf, -inf, or nan) and among
    the top `k` predictions among all predictions for example `i`.
    `predictions` does not have to be normalized.

    Note that the behavior of `InTopK` differs from the `TopK` op in its handling
    of ties; if multiple classes have the same prediction value and straddle the
    top-`k` boundary, all of those classes are considered to be in the top `k`.

    >>> target = tf.constant([0, 1, 3])
    >>> pred = tf.constant([
    ...  [1.2, -0.3, 2.8, 5.2],
    ...  [0.1, 0.0, 0.0, 0.0],
    ...  [0.0, 0.5, 0.3, 0.3]],
    ...  dtype=tf.float32)
    >>> print(tf.math.in_top_k(target, pred, 2))
    tf.Tensor([False  True  True], shape=(3,), dtype=bool)

Args:
        targets: A `batch_size` vector of class ids. Must be `int32` or `int64`.
        predictions: A `batch_size` x `classes` tensor of type `float32`.
        k: An `int`. The parameter to specify search space.
        name: A name for the operation (optional).

Returns:
        A `Tensor` with the same shape of `targets` with type of `bool`. Each
        element specifies if the target falls into top-k predictions.


def call(self, inputs, **kwargs):
    return inputs

This is where the layer's logic lives.

Args:
        inputs: Input tensor, or list/tuple of input tensors.
        **kwargs: Additional keyword arguments.

Returns:
        A tensor or list/tuple of tensors.


def is_sparse(tensor):
    spec = getattr(tensor, '_type_spec', None)
    if spec is not None:
        return isinstance(spec, sparse_tensor.SparseTensorSpec)
    return isinstance(tensor, sparse_tensor.SparseTensor)

Returns whether a tensor is a sparse tensor.

Args:
        tensor: A tensor instance.

Returns:
        A boolean.

Example:
    >>> a = tf.keras.backend.placeholder((2, 2), sparse=False)
    >>> print(tf.keras.backend.is_sparse(a))
    False
    >>> b = tf.keras.backend.placeholder((2, 2), sparse=True)
    >>> print(tf.keras.backend.is_sparse(b))
    True


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

Args:
    hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
    attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
        `(batch, sequence_length)` where padding elements are indicated by 0.
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers. See `attentions` under
        returned tensors for more detail.
    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`).
    past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
    cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
        Indices depicting the position of the input sequence tokens in the sequence.
    position_embeddings (`Tuple[torch.FloatTensor, torch.FloatTensor]`, *optional*):
        Tuple containing the cosine and sine positional embeddings of shape `(batch_size, seq_len, head_dim)`,
        with `head_dim` being the embedding dimension of each attention head.
    kwargs (`dict`, *optional*):
        Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code
        into the model


def __init__(self, model_name: str, columns: list[str], title: Optional[str]=None, task_type: str=DEFAULT_TASK_TYPE, project: Optional[str]=None, location: Optional[str]=None, credentials: Optional[Credentials]=None, **kwargs):
    self.model_name = model_name
    self.project = project
    self.location = location
    self.credentials = credentials
    self.title = title
    self.task_type = task_type
    super().__init__(columns=columns, **kwargs)

Embedding Config for Vertex AI Text Embedding models following
    https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings # pylint: disable=line-too-long
    Text Embeddings are generated for a batch of text using the Vertex AI SDK.
    Embeddings are returned in a list for each text in the batch. Look at
    https://cloud.google.com/vertex-ai/docs/generative-ai/learn/model-versioning#stable-versions-available.md # pylint: disable=line-too-long
    for more information on model versions and lifecycle.

Args:
        model_name: The name of the Vertex AI Text Embedding model.
        columns: The columns containing the text to be embedded.
        task_type: The downstream task for the embeddings. Valid values are
        RETRIEVAL_QUERY, RETRIEVAL_DOCUMENT, SEMANTIC_SIMILARITY,
        CLASSIFICATION, CLUSTERING. For more information on the task type,
        look at https://cloud.google.com/vertex-ai/docs/generative-ai/embeddings/get-text-embeddings # pylint: disable=line-too-long
        title: Identifier of the text content.
        project: The default GCP project for API calls.
        location: The default location for API calls.
        credentials: Custom credentials for API calls.
        Defaults to environment credentials.


def ndtri(p, name='ndtri'):
    with ops.name_scope(name, values=[p]):
        p = ops.convert_to_tensor(p, name='p')
        if p.dtype.as_numpy_dtype not in [np.float32, np.float64]:
            raise TypeError('p.dtype=%s is not handled, see docstring for supported types.' % p.dtype)
        return _ndtri(p)

The inverse of the CDF of the Normal distribution function.

    Returns x such that the area under the pdf from minus infinity to x is equal
    to p.

    A piece-wise rational approximation is done for the function.
    This is a port of the implementation in netlib.

Args:
        p: `Tensor` of type `float32`, `float64`.
        name: Python string. A name for the operation (default="ndtri").

Returns:
        x: `Tensor` with `dtype=p.dtype`.

Raises:
        TypeError: if `p` is not floating-type.


def reduce_max(values, index, name='segmented_reduce_max'):
    return _segment_reduce(values, index, 'amax', name)

Computes the maximum over segments.

    This operation computes the maximum over segments, with support for:

        - Batching using the first dimensions [B1, B2, ..., Bn]. Each element in a batch can have different indices.
        - Vectorization using the last dimension [V1, V2, ...]. If they are present, the output will be an element-wise
        maximum of vectors rather than scalars.

    Only the middle dimensions [I1, ..., Ik] are reduced by the operation.

Args:
        values (`torch.Tensor` of shape [B1, B2, ..., Bn, I1, .., Ik, V1, V2, ..]):
            Tensor containing the values of which the max must be taken segment-wise.
        index (`IndexMap`, indices are of shape [B1, B2, ..., Bn, I1, .., Ik].):
            Index defining the segments.
        name (`str`, *optional*, defaults to 'segmented_reduce_sum'):
            Name for the operation. Currently not used

Returns:
        output_values (`torch.Tensor`of shape [B1, B2, ..., Bn, num_segments, V1, V2, ..]): Tensor containing the
        output values. output_index (`IndexMap`): IndexMap with shape [B1, B2, ..., Bn, num_segments].


def close(self):
    if not self.closed:
        self._uploader.finish()
    super().close()

Complete the upload and close this stream.

    This method has no effect if the stream is already closed.

Raises:
        Any error encountered by the uploader.


def __init__(self, learning_rate=0.001, initial_accumulator_value=0.1, epsilon=1e-07, weight_decay=None, clipnorm=None, clipvalue=None, global_clipnorm=None, use_ema=False, ema_momentum=0.99, ema_overwrite_frequency=None, loss_scale_factor=None, gradient_accumulation_steps=None, name='adagrad', **kwargs):
    super().__init__(learning_rate=learning_rate, weight_decay=weight_decay, clipnorm=clipnorm, clipvalue=clipvalue, global_clipnorm=global_clipnorm, use_ema=use_ema, ema_momentum=ema_momentum, ema_overwrite_frequency=ema_overwrite_frequency, loss_scale_factor=loss_scale_factor, gradient_accumulation_steps=gradient_accumulation_steps, name=name, **kwargs)
    self.initial_accumulator_value = initial_accumulator_value
    self.epsilon = epsilon

Optimizer that implements the Adagrad algorithm.

    Adagrad is an optimizer with parameter-specific learning rates,
    which are adapted relative to how frequently a parameter gets
    updated during training. The more updates a parameter receives,
    the smaller the updates.

Args:
        learning_rate: A float, a
            `keras.optimizers.schedules.LearningRateSchedule` instance, or
            a callable that takes no arguments and returns the actual value to
            use. The learning rate. Defaults to `0.001`. Note that `Adagrad`
            tends to benefit from higher initial learning rate values compared
            to other optimizers. To match the exact form in the original paper,
            use `1.0`.
        initial_accumulator_value: Floating point value. Starting value for the
            accumulators (per-parameter momentum values). Must be non-negative.
        epsilon: Small floating point value for maintaining numerical stability.
        {{base_optimizer_keyword_args}}

    Reference:

    - [Duchi et al., 2011](
        http://www.jmlr.org/papers/volume12/duchi11a/duchi11a.pdf).


def save(value: Any, path: str, *args, **kwargs) -> Any:
    save_handler = flags.get_save_handler() or default_save_handler
    return save_handler(value, path, *args, **kwargs)

Save a symbolic value using the global save handler.

    Example::

        @pg.members([
        ('x', pg.typing.Any())
        ])
        class A(pg.Object):
        pass

        a1 = A(1)
        file = 'my_file.json'
        a1.save(file)
        a2 = pg.load(file)
        assert pg.eq(a1, a2)

Args:
        value: value to save.
        path: A path string for saving `value`.
        *args: Positional arguments that will be passed through to the global
        save handler.
        **kwargs: Keyword arguments that will be passed through to the global
        save handler.

Returns:
        Return value from the global save handler.

Raises:
        RuntimeError: if global save handler is not set.


def assertAllGreater(self, a, comparison_target):
    a, comparison_target = self.evaluate_if_both_tensors(a, comparison_target)
    a = self._GetNdArray(a)
    self.assertGreater(np.min(a), comparison_target)

Assert element values are all greater than a target value.

Args:
        a: The numpy `ndarray`, or anything that can be converted into a numpy
        `ndarray` (including Tensor).
        comparison_target: The target value of comparison.


def get_seed(seed):
    seed, seed2 = random_seed.get_seed(seed)
    if seed is None:
        seed = constant_op.constant(0, dtype=dtypes.int64, name='seed')
    else:
        seed = ops.convert_to_tensor(seed, dtype=dtypes.int64, name='seed')
    if seed2 is None:
        seed2 = constant_op.constant(0, dtype=dtypes.int64, name='seed2')
    else:
        with ops.name_scope('seed2') as scope:
            seed2 = ops.convert_to_tensor(seed2, dtype=dtypes.int64)
            seed2 = array_ops.where_v2(math_ops.logical_and(math_ops.equal(seed, 0), math_ops.equal(seed2, 0)), constant_op.constant(2 ** 31 - 1, dtype=dtypes.int64), seed2, name=scope)
    return (seed, seed2)

Returns the local seeds an operation should use given an op-specific seed.

    See `random_seed.get_seed` for more details. This wrapper adds support for
    the case where `seed` may be a tensor.

Args:
        seed: An integer or a `tf.int64` scalar tensor.

Returns:
        A tuple of two `tf.int64` scalar tensors that should be used for the local
        seed of the calling dataset.


def _prepare_4d_causal_attention_mask_with_cache_position(attention_mask: torch.Tensor, sequence_length: int, target_length: int, dtype: torch.dtype, cache_position: torch.Tensor, batch_size: int, config: PhimoeConfig, past_key_values: Cache):
    if attention_mask is not None and attention_mask.dim() == 4:
        causal_mask = attention_mask
    else:
        min_dtype = torch.finfo(dtype).min
        causal_mask = torch.full((sequence_length, target_length), fill_value=min_dtype, dtype=dtype, device=cache_position.device)
        diagonal_attend_mask = torch.arange(target_length, device=cache_position.device) > cache_position.reshape(-1, 1)
        text_config = config.get_text_config()
        if getattr(text_config, 'use_sliding_window', True) and text_config.sliding_window is not None:
            if not isinstance(past_key_values, SlidingWindowCache) or sequence_length > target_length:
                sliding_attend_mask = torch.arange(target_length, device=cache_position.device) <= cache_position.reshape(-1, 1) - text_config.sliding_window
                diagonal_attend_mask.bitwise_or_(sliding_attend_mask)
        causal_mask *= diagonal_attend_mask
        causal_mask = causal_mask[None, None, :, :].expand(batch_size, 1, -1, -1)
        if attention_mask is not None:
            causal_mask = causal_mask.clone()
            if attention_mask.shape[-1] > target_length:
                attention_mask = attention_mask[:, :target_length]
            mask_length = attention_mask.shape[-1]
            padding_mask = causal_mask[:, :, :, :mask_length] + attention_mask[:, None, None, :].to(causal_mask.device)
            padding_mask = padding_mask == 0
            causal_mask[:, :, :, :mask_length] = causal_mask[:, :, :, :mask_length].masked_fill(padding_mask, min_dtype)
    return causal_mask

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

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


def ndtri(x, name=None):
    with ops.name_scope(name, 'ndtri', [x]):
        return gen_math_ops.ndtri(x)

Compute quantile of Standard Normal.

Args:
        x: `Tensor` with type `float` or `double`.
        name: A name for the operation (optional).

Returns:
        Inverse error function of `x`.


def on_predict_batch_end(self, batch, logs=None):
    if self._should_call_predict_batch_hooks:
        self._call_batch_hook(ModeKeys.PREDICT, 'end', batch, logs=logs)

Calls the `on_predict_batch_end` methods of its callbacks.

Args:
        batch: Integer, index of batch within the current epoch.
        logs: Dict. Aggregated metric results up until this batch.


def _get_token_budget(self, question_tokens, max_length=None):
    return (max_length if max_length is not None else self.model_max_length) - self._question_encoding_cost(question_tokens)

Computes the number of tokens left for the table after tokenizing a question, taking into account the max
    sequence length of the model.

Args:
        question_tokens (`List[String]`):
            List of question tokens. Returns: `int`: the number of tokens left for the table, given the model max
            length.


def to_dict(self) -> dict[str, Any]:
    output = copy.deepcopy(self.__dict__)
    output['image_processor_type'] = self.__class__.__name__
    return output

Serializes this instance to a Python dictionary.

Returns:
        `Dict[str, Any]`: Dictionary of all the attributes that make up this image processor instance.


def fit(self, X, y, **kwargs):
    X, y = _validate_data(self, X, y)
    y = self._process_target(y, reset=True)
    model = self._get_model(X, y)
    _check_model(model)
    fit_kwargs = self.fit_kwargs or {}
    fit_kwargs.update(kwargs)
    self.history_ = model.fit(X, y, **fit_kwargs)
    self.model_ = model
    return self

Fit the model.

Args:
        X: array-like, shape=(n_samples, n_features)
            The input samples.
        y: array-like, shape=(n_samples,) or (n_samples, n_outputs)
            The targets.
        **kwargs: keyword arguments passed to `model.fit`


def _build_cond(pred, true_graph, false_graph, true_inputs, false_inputs, building_gradient, add_identities: bool=True, prevent_lowering: bool=False, name=None):
    _make_indexed_slices_indices_types_match(_COND, [true_graph, false_graph])
    _check_same_outputs(_COND, [true_graph, false_graph])
    cond_inputs = _make_inputs_match([true_graph, false_graph], [true_inputs, false_inputs])
    if not building_gradient and util.output_all_intermediates():
        true_intermediates = _get_intermediates(true_graph)
        false_intermediates = _get_intermediates(false_graph)
        wrapped_true_intermediates = _wrap_intermediates(true_graph, true_intermediates)
        wrapped_false_intermediates = _wrap_intermediates(false_graph, false_intermediates)
        extra_true_outputs, extra_false_outputs = _make_intermediates_match([true_graph, false_graph], [wrapped_true_intermediates, wrapped_false_intermediates])
        true_graph.outputs.extend(extra_true_outputs)
        false_graph.outputs.extend(extra_false_outputs)
        _check_same_outputs(_COND, [true_graph, false_graph])
    with ops.control_dependencies(list(true_graph.function_captures.control) + list(false_graph.function_captures.control)):
        true_stateful_ops = [op for op in true_graph.get_operations() if _is_op_stateful(op)]
        false_stateful_ops = [op for op in false_graph.get_operations() if _is_op_stateful(op)]
        if true_stateful_ops or false_stateful_ops:
            op_fn = gen_functional_ops._if
        else:
            op_fn = gen_functional_ops.stateless_if

        def _make_op(inputs):
            if_op, tensors = util.get_op_and_outputs(op_fn(pred, inputs, [t.dtype for t in true_graph.outputs], util.create_new_tf_function(true_graph), util.create_new_tf_function(false_graph), output_shapes=_get_output_shapes(true_graph.outputs, false_graph.outputs), name=name))
            _copy_handle_data(tensors, true_graph.outputs, false_graph.outputs)
            if if_op is not None:
                true_graph.outer_graph = ops.get_default_graph()
                false_graph.outer_graph = ops.get_default_graph()
                if_op._true_graph = true_graph
                if_op._false_graph = false_graph
                util.maybe_set_lowering_attr(if_op, False if prevent_lowering else None)
                util.maybe_propagate_compile_time_consts_in_xla(if_op)
                _set_read_only_resource_inputs_attr(if_op, [true_graph, false_graph])
                if not prevent_lowering:
                    if_op.graph.prevent_fetching(if_op)
            return tensors
        tensors = util.run_as_function_for_tape_gradients(_make_op, cond_inputs)
    if add_identities:
        tensors = [array_ops.identity(t) for t in tensors]
    structured_output_specs = _get_compatible_structured_output_specs(true_graph, false_graph)
    return _pack_sequence_as(structured_output_specs, tensors)

Creates an If op from the specified predicate, branch functions and inputs.

    Note that this modifies true_graph and false_graph to make the inputs match,
    and to output all intermediates values so they're available for the gradient
    computation.

    true_graph and false_graph need not have the same input types, but they must
    have the same output types.

Args:
        pred: boolean Tensor
        true_graph: FuncGraph
        false_graph: FuncGraph
        true_inputs: a list of Tensors to be passed to true_graph as input.
        false_inputs: a list of Tensors to be passed to false_graph as input.
        building_gradient: Whether this is a gradient If op.
        add_identities: If `True`, adds an identity op for each output of the If op.
        This is useful for pruning, but can be disabled if the caller does not
        want to rely on pruning and wants to avoid the overhead of extra identity
        ops in the graph.
        prevent_lowering: If `True`, prevents the If op from being lowered to
        V1 `Switch` and `Merge` ops.
        name: the name for the If op.

Returns:
        A list of Tensors which are the outputs of the If op. Does not include added
        intermediate outputs.


def build_info(self):
    if self.is_bootloader:
        self.log.error('Device is in fastboot mode, could not get build info.')
        return
    if self._build_info is None or self._is_rebooting:
        info = {}
        build_info = self.adb.getprops(CACHED_SYSTEM_PROPS)
        for build_info_constant in BuildInfoConstants:
            info[build_info_constant.build_info_key] = build_info.get(build_info_constant.system_prop_key, '')
        self._build_info = info
        return info
    return self._build_info

Gets the build info of this Android device, including build id and type.

    This is not available if the device is in bootloader mode.

Returns:
        A dict with the build info of this Android device, or None if the
        device is in bootloader mode.


def stochastic_cast(t, dtype, seed, alg='auto_select', name=None):
    with ops.name_scope(name, 'stochastic_cast', [t, seed]) as name:
        t = ops.convert_to_tensor(t)
        key, counter, algorithm = random_ops_util.get_key_counter_alg(seed, alg)
        if dtype in allowed_to_types(is_integer=True):
            return gen_stochastic_cast_op.stochastic_cast_to_int(t, key=key, counter=counter, alg=algorithm, Tout=dtype)
        else:
            raise NotImplementedError(f'Stochastic cast to small float {dtype} has not yet been supported.')

Casts input to the desired precision with stochastic rounding.

    This means the value of the cast result will be rounded to two of the closest
    values with with a probability proportional to the distance between the number
    and the two closest to the input. For example, if a number falls between 2 and
    3, and is closer to 2 than to 3, it has a higher probability of being rounded
    to 2. On the other hand, if it's closer to 3 than to 2, it has a higher
    probability of being rounded to 3. This is intended to eliminate rounding bias
    introduced by determinisitc rounding methods. If cast to integers, the values
    will saturate if out of range, e.g. 254.8 in floating point will become 127 in
    int8. If inputs are NaN, the results will be zero. Given the same random seed,
    the results will be deterministic, but not otherwise.

Args:
        t: The input tensor. This is the same as the output shape.
        dtype: The output type, currently int32, int16 and int8 are supported.
        seed: A shape [2] Tensor, the seed to the random number generator. Must have
        dtype `int32` or `int64`. (When using XLA, only `int32` is allowed.)
        alg: The RNG algorithm used to generate the random numbers. See
        `tf.random.stateless_uniform` for a detailed explanation.
        name: A name for the operation (optional).

Returns:
        A tensor of the specified data type whose values are rounded to the
        specified precisions with stochastic rounding.


def save(model, filepath, overwrite, include_optimizer, signatures=None, options=None, save_traces=True):
    if not overwrite and os.path.exists(filepath):
        proceed = ask_to_proceed_with_overwrite(filepath)
        if not proceed:
            return
    if save_traces:
        if save_impl.should_skip_serialization(model):
            saving_utils.raise_model_input_error(model)
    if not include_optimizer:
        orig_optimizer = model.optimizer
        model.optimizer = None
        model._delete_tracking('optimizer')
    with K.deprecated_internal_learning_phase_scope(0):
        with utils.keras_option_scope(save_traces):
            saved_nodes, node_paths = save_lib.save_and_return_nodes(model, filepath, signatures, options)
        metadata = generate_keras_metadata(saved_nodes, node_paths)
    with gfile.GFile(os.path.join(filepath, constants.SAVED_METADATA_PATH), 'wb') as w:
        w.write(metadata.SerializeToString(deterministic=True))
    if not include_optimizer:
        model.optimizer = orig_optimizer

Saves a model as a SavedModel to the filepath.

Args:
        model: Keras model instance to be saved.
        filepath: String path to save the model.
        overwrite: whether to overwrite the existing filepath.
        include_optimizer: If True, save the model's optimizer state.
        signatures: Signatures to save with the SavedModel. Applicable to the 'tf'
        format only. Please see the `signatures` argument in `tf.saved_model.save`
        for details.
        options: (only applies to SavedModel format) `tf.saved_model.SaveOptions`
        object that specifies options for saving to SavedModel.
        save_traces: (only applies to SavedModel format) When enabled, the
        SavedModel will store the function traces for each layer. This
        can be disabled, so that only the configs of each layer are stored.
        Defaults to `True`. Disabling this will decrease serialization time
        and reduce file size, but it requires that all custom layers/models
        implement a `get_config()` method.

Raises:
        ValueError: if the model's inputs have not been defined.


def __init__(self, content=None, min=0, max=HUGE, name=None):
    assert 0 <= min <= max <= HUGE, (min, max)
    if content is not None:
        content = tuple(map(tuple, content))
        assert len(content), repr(content)
        for alt in content:
            assert len(alt), repr(alt)
    self.content = content
    self.min = min
    self.max = max
    self.name = name

Initializer.

Args:
        content: optional sequence of subsequences of patterns;
                if absent, matches one node;
                if present, each subsequence is an alternative [*]
        min: optional minimum number of times to match, default 0
        max: optional maximum number of times to match, default HUGE
        name: optional name assigned to this match

    [*] Thus, if content is [[a, b, c], [d, e], [f, g, h]] this is
        equivalent to (a b c | d e | f g h); if content is None,
        this is equivalent to '.' in regular expression terms.
        The min and max parameters work as follows:
            min=0, max=maxint: .*
            min=1, max=maxint: .+
            min=0, max=1: .?
            min=1, max=1: .
        If content is not None, replace the dot with the parenthesized
        list of alternatives, e.g. (a b c | d e | f g h)*


def calibrate_and_quantize(self, dataset_gen, input_type, output_type, allow_float, activations_type=dtypes.int8, bias_type=dtypes.int32, resize_input=True, disable_per_channel=False, disable_per_channel_quantization_for_dense_layers=False):
    self._feed_tensors(dataset_gen, resize_input)
    return self._calibrator.QuantizeModel(np.dtype(input_type.as_numpy_dtype()).num, np.dtype(output_type.as_numpy_dtype()).num, allow_float, np.dtype(activations_type.as_numpy_dtype()).num, np.dtype(bias_type.as_numpy_dtype()).num, disable_per_channel, disable_per_channel_quantization_for_dense_layers)

Calibrates the model with specified generator and then quantizes it.

    The input shapes of the calibrator are resized with the calibration data if
    `resize_input` is set.

Returns:
        A quantized model.

Args:
        dataset_gen: A generator that generates calibration samples.
        input_type: A tf.dtype representing the desired real-value input type.
        output_type: A tf.dtype representing the desired real-value output type.
        allow_float: A boolean. False if the resulting model cannot perform float
        computation, useful when targeting an integer-only backend. If False, an
        error will be thrown if an operation cannot be quantized, otherwise the
        model will fallback to float ops.
        activations_type: A tf.dtype representing the desired type for
        activations.
        bias_type: A tf.dtype representing the desired type for bias.
        resize_input: A boolean. True if the shape of the sample data is different
        from the input.
        disable_per_channel: A boolean. True if disabling per-channel
        quantization.
        disable_per_channel_quantization_for_dense_layers: A boolean. True if
        disabling per-channel quantization only in Dense layers.


def hinge(y_true, y_pred):
    y_pred = ops.convert_to_tensor(y_pred)
    y_true = ops.cast(y_true, dtype=y_pred.dtype)
    y_true = ops.convert_to_tensor(y_true)
    y_true = convert_binary_labels_to_hinge(y_true)
    return ops.mean(ops.maximum(1.0 - y_true * y_pred, 0.0), axis=-1)

Computes the hinge loss between `y_true` & `y_pred`.

    Formula:

    ```python
    loss = mean(maximum(1 - y_true * y_pred, 0), axis=-1)
    ```

Args:
        y_true: The ground truth values. `y_true` values are expected to be -1
            or 1. If binary (0 or 1) labels are provided they will be converted
            to -1 or 1 with shape = `[batch_size, d0, .. dN]`.
        y_pred: The predicted values with shape = `[batch_size, d0, .. dN]`.

Returns:
        Hinge loss values with shape = `[batch_size, d0, .. dN-1]`.

Example:
    >>> y_true = np.random.choice([-1, 1], size=(2, 3))
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = keras.losses.hinge(y_true, y_pred)


def metrics_names(self):
    return [m.name for m in self.metrics]

Returns the model's display labels for all outputs.

    Note: `metrics_names` are available only after a `keras.Model` has been
    trained/evaluated on actual data.

Example:
    >>> inputs = tf.keras.layers.Input(shape=(3,))
    >>> outputs = tf.keras.layers.Dense(2)(inputs)
    >>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
    >>> model.compile(optimizer="Adam", loss="mse", metrics=["mae"])
    >>> model.metrics_names
    []

    >>> x = np.random.random((2, 3))
    >>> y = np.random.randint(0, 2, (2, 2))
    >>> model.fit(x, y)
    >>> model.metrics_names
    ['loss', 'mae']

    >>> inputs = tf.keras.layers.Input(shape=(3,))
    >>> d = tf.keras.layers.Dense(2, name='out')
    >>> output_1 = d(inputs)
    >>> output_2 = d(inputs)
    >>> model = tf.keras.models.Model(
    ...    inputs=inputs, outputs=[output_1, output_2])
    >>> model.compile(optimizer="Adam", loss="mse", metrics=["mae", "acc"])
    >>> model.fit(x, (y, y))
    >>> model.metrics_names
    ['loss', 'out_loss', 'out_1_loss', 'out_mae', 'out_acc', 'out_1_mae',
    'out_1_acc']


def __init__(self, e, position=None):
    self.exception = e
    self.type = type(e).__name__
    self.stacktrace = None
    self.extras = None
    self.position = position
    self.is_test_signal = isinstance(e, signals.TestSignal)
    exc_traceback = e.__traceback__
    if exc_traceback:
        self.stacktrace = ''.join(traceback.format_exception(e.__class__, e, exc_traceback))
    if self.is_test_signal:
        self._set_details(e.details)
        self.extras = e.extras
    else:
        self._set_details(e)

A record representing exception objects in TestResultRecord.

Attributes:
        exception: Exception object, the original Exception.
        type: string, type name of the exception object.
        stacktrace: string, stacktrace of the Exception.
        extras: optional serializable, this corresponds to the
        `TestSignal.extras` field.
        position: string, an optional label specifying the position where the
        Exception ocurred.


def collect_constant_renames():
    renames = set()
    for module in sys.modules.copy().values():
        try:
            constants_v1_list = tf_export.get_v1_constants(module)
            constants_v2_list = tf_export.get_v2_constants(module)
        except:
            pass
        constants_v1 = {constant_name: api_names for api_names, constant_name in constants_v1_list}
        constants_v2 = {constant_name: api_names for api_names, constant_name in constants_v2_list}
        for constant_name, api_names_v1 in constants_v1.items():
            api_names_v2 = constants_v2[constant_name]
            for name in api_names_v1:
                if name not in api_names_v2:
                    renames.add((name, get_canonical_name(api_names_v2, name)))
    return renames

Looks for constants that need to be renamed in TF 2.0.

Returns:
        Set of tuples of the form (current name, new name).


def get_indent(code: str) -> str:
    lines = code.split('\n')
    idx = 0
    while idx < len(lines) and len(lines[idx]) == 0:
        idx += 1
    if idx < len(lines):
        return re.search('^(\\s*)\\S', lines[idx]).groups()[0]
    return ''

Find the indent in the first non empty line in a code sample.

Args:
        code (`str`): The code to inspect.

Returns:
        `str`: The indent looked at (as string).


def take_along_axis(x, indices, axis=None):
    if any_symbolic_tensors((x, indices)):
        return TakeAlongAxis(axis=axis).symbolic_call(x, indices)
    return backend.numpy.take_along_axis(x, indices, axis=axis)

Select values from `x` at the 1-D `indices` along the given axis.

Args:
        x: Source tensor.
        indices: The indices of the values to extract.
        axis: The axis over which to select values. By default, the flattened
            input tensor is used.

Returns:
        The corresponding tensor of values.


def call(self, input_ids: TFModelInputType | None=None, attention_mask: np.ndarray | tf.Tensor | None=None, decoder_input_ids: np.ndarray | tf.Tensor | None=None, decoder_attention_mask: np.ndarray | tf.Tensor | None=None, head_mask: np.ndarray | tf.Tensor | None=None, decoder_head_mask: np.ndarray | tf.Tensor | None=None, encoder_outputs: np.ndarray | tf.Tensor | None=None, past_key_values: Optional[Tuple[Tuple[Union[np.ndarray, tf.Tensor]]]]=None, inputs_embeds: np.ndarray | tf.Tensor | None=None, decoder_inputs_embeds: np.ndarray | tf.Tensor | None=None, use_cache: Optional[bool]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, training: Optional[bool]=False) -> Union[Tuple, TFSeq2SeqModelOutput]:
    if head_mask is not None and decoder_head_mask is None:
        warnings.warn(_HEAD_MASK_WARNING_MSG, FutureWarning)
        decoder_head_mask = head_mask
    if encoder_outputs is None:
        encoder_outputs = self.encoder(input_ids, attention_mask=attention_mask, encoder_hidden_states=None, encoder_attention_mask=None, inputs_embeds=inputs_embeds, head_mask=head_mask, past_key_values=None, use_cache=False, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, training=training)
    hidden_states = encoder_outputs[0]
    decoder_outputs = self.decoder(decoder_input_ids, attention_mask=decoder_attention_mask, encoder_hidden_states=hidden_states, encoder_attention_mask=attention_mask, inputs_embeds=decoder_inputs_embeds, head_mask=decoder_head_mask, encoder_head_mask=head_mask, past_key_values=past_key_values, use_cache=use_cache, output_attentions=output_attentions, output_hidden_states=output_hidden_states, return_dict=return_dict, training=training)
    past = decoder_outputs[1] if use_cache else None
    if not return_dict:
        if past_key_values is not None:
            decoder_outputs = decoder_outputs[:1] + (past,) + decoder_outputs[2:]
        return decoder_outputs + encoder_outputs
    return TFSeq2SeqModelOutput(last_hidden_state=decoder_outputs.last_hidden_state, past_key_values=past, decoder_hidden_states=decoder_outputs.hidden_states, decoder_attentions=decoder_outputs.attentions, cross_attentions=decoder_outputs.cross_attentions, encoder_last_hidden_state=encoder_outputs.last_hidden_state, encoder_hidden_states=encoder_outputs.hidden_states, encoder_attentions=encoder_outputs.attentions)

Returns:

Example:
    ```python
    >>> from transformers import AutoTokenizer, TFT5Model

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

    >>> input_ids = tokenizer(
    ...     "Studies have been shown that owning a dog is good for you", return_tensors="tf"
    ... ).input_ids  # Batch size 1
    >>> decoder_input_ids = tokenizer("Studies show that", return_tensors="tf").input_ids  # Batch size 1

    >>> # preprocess: Prepend decoder_input_ids with start token which is pad token for T5Model.
    >>> # This is not needed for torch's T5ForConditionalGeneration as it does this internally using labels arg.
    >>> decoder_input_ids = model._shift_right(decoder_input_ids)

    >>> # forward pass
    >>> outputs = model(input_ids, decoder_input_ids=decoder_input_ids)
    >>> last_hidden_states = outputs.last_hidden_state
    ```


def _inspect_summary_cache(self, cache, replica_id, step_num, output_stream, tensor_trace_order):

    def _inspect_tensor(tensor):
        """Returns the text to be printed for inspection output."""
        if self._parameters.trace_mode == tensor_tracer_flags.TRACE_MODE_NAN_INF:
            return cond.cond(math_ops.greater(tensor, 0.0), lambda: 'has NaNs/Infs!', lambda: 'has no NaNs or Infs.')
        else:
            return tensor
    if not tensor_trace_order.traced_tensors:
        logging.warn('Inspect mode has no tensors in the cache to check.')
        return control_flow_ops.no_op
    if self._parameters.trace_mode == tensor_tracer_flags.TRACE_MODE_NAN_INF:
        step_has_nan_or_inf = math_ops.greater(math_ops.reduce_sum(cache), 0.0)
    else:
        step_has_nan_or_inf = math_ops.reduce_any(gen_math_ops.logical_or(gen_math_ops.is_nan(cache), gen_math_ops.is_inf(cache)))
    step_error_message = cond.cond(step_has_nan_or_inf, lambda: 'NaNs or Infs in the step!', lambda: 'No numerical issues have been found for the step.')
    if self._parameters.collect_summary_per_core:
        stats = ['\n\n', 'core:', replica_id, ',', 'step:', step_num, '-->', step_error_message, 'Printing tensors for mode:%s...' % self._parameters.trace_mode]
    else:
        stats = ['\n\n', 'step:', step_num, '-->', step_error_message, 'Printing tensors for mode:%s...' % self._parameters.trace_mode]
    for tensor_name, cache_idx in sorted(tensor_trace_order.tensorname_to_cache_idx.items(), key=lambda item: item[1]):
        if self._parameters.collect_summary_per_core:
            stats.extend(['\n', 'core:', replica_id, ',', 'step:', step_num, ',', tensor_name, '-->', _inspect_tensor(cache[cache_idx, 0])])
        else:
            stats.extend(['\n', 'step:', step_num, ',', tensor_name, '-->', _inspect_tensor(cache[cache_idx, 0])])
    return logging_ops.print_v2(*stats, summarize=-1, output_stream=output_stream)

Generates a print operation to print trace inspection.

Args:
        cache: Tensor storing the trace results for the step.
        replica_id: Tensor storing the replica id of the running core.
        step_num: Step number.
        output_stream: Where to print the outputs, e.g., file path, or sys.stderr.
        tensor_trace_order: TensorTraceOrder object holding tensorname to id map.

Returns:
        The Op to flush the cache to file.


def _agg_with_no_function(gb, *args, **kwargs):
    if not kwargs:
        raise ValueError('No aggregation functions specified')
    result_columns, result_frames = ([], [])
    for col_name, (input_col, agg_fn) in kwargs.items():
        frame = _handle_agg_function(gb[input_col], agg_fn, f'agg_{col_name}', *args)
        result_frames.append(frame)
        result_columns.append(col_name)
    return DeferredDataFrame(expressions.ComputedExpression('agg', lambda *results: pd.concat(results, axis=1, keys=result_columns), [frame._expr for frame in result_frames], requires_partition_by=partitionings.Index(), preserves_partition_by=partitionings.Singleton()))

Applies aggregation functions to the grouped data based on the provided
    keyword arguments and combines the results into a single DataFrame.

Args:
        gb: The groupby instance (DeferredGroupBy).
        *args: Additional positional arguments passed to the aggregation funcs.
        **kwargs: A dictionary where each key is the column name to aggregate,
                the value is a tuple containing the input column name and
                the aggregation function to apply.

Returns:
        DeferredDataFrame: A DataFrame that contains the aggregated results of
                            all specified columns.

Raises:
        ValueError: If no aggregation functions are provided in the `kwargs`.
        NotImplementedError: If the aggregation function type is unsupported.


def _sample_proposals(self, matched_idxs: torch.Tensor, matched_labels: torch.Tensor, gt_classes: torch.Tensor):
    has_gt = gt_classes.numel() > 0
    if has_gt:
        gt_classes = gt_classes[matched_idxs]
        gt_classes[matched_labels == 0] = self.bg_label
        gt_classes[matched_labels == -1] = -1
    else:
        gt_classes = torch.zeros_like(matched_idxs) + self.bg_label
    sampled_fg_idxs, sampled_bg_idxs = subsample_labels(gt_classes, self.batch_size_per_image, self.positive_fraction, self.bg_label)
    sampled_idxs = torch.cat([sampled_fg_idxs, sampled_bg_idxs], dim=0)
    return (sampled_idxs, gt_classes[sampled_idxs])

Based on the matching between N proposals and M groundtruth, sample the proposals and set their classification
    labels.

Args:
        matched_idxs (Tensor): a vector of length N, each is the best-matched
            gt index in [0, M) for each proposal.
        matched_labels (Tensor): a vector of length N, the matcher's label
            (one of cfg.MODEL.ROI_HEADS.IOU_LABELS) for each proposal.
        gt_classes (Tensor): a vector of length M.

Returns:
        Tensor: a vector of indices of sampled proposals. Each is in [0, N). Tensor: a vector of the same length,
        the classification label for
            each sampled proposal. Each sample is labeled as either a category in [0, num_classes) or the
            background (num_classes).


def get_regularization_loss(scope=None, name='total_regularization_loss'):
    losses = get_regularization_losses(scope)
    if losses:
        return math_ops.add_n(losses, name=name)
    else:
        return constant_op.constant(0.0)

Gets the total regularization loss.

Args:
        scope: An optional scope name for filtering the losses to return.
        name: The name of the returned tensor.

Returns:
        A scalar regularization loss.


def tanh_shrink(x):
    if any_symbolic_tensors((x,)):
        return TanhShrink().symbolic_call(x)
    return backend.nn.tanh_shrink(x)

Applies the tanh shrink function element-wise.

    It is defined as:

    `f(x) = x - tanh(x)`.

Args:
        x: Input tensor.

Returns:
        Output tensor of the same shape as `x`, where each element is
        transformed according to the tanh shrink operation.

Example:
    >>> x = np.array([ -1., 0., 1.])
    >>> x_tanh_shrink = keras.ops.tanh_shrink(x)
    >>> print(x_tanh_shrink)
    array([-0.23840584  0.  0.23840584], shape=(3,), dtype=float64)


def add_import(self, from_package, import_list):
    if from_package:
        for item in import_list:
            t = self.module_info.process_from_import(from_package, item)
            self.type_map[t.new_name] = t.pytd_node
            if isinstance(item, tuple) or from_package != 'typing' or self.module_info.module_name == 'protocols':
                self.aliases[t.new_name] = t.pytd_alias()
                if t.new_name != 'typing':
                    self.module_path_map[t.new_name] = t.qualified_name
    else:
        for item in import_list:
            t = self.module_info.process_import(item)
            if t:
                self.aliases[t.new_name] = t.pytd_alias()

Add an import.

Args:
        from_package: A dotted package name if this is a "from" statement, or None
        if it is an "import" statement.
        import_list: A list of imported items, which are either strings or pairs
        of strings.  Pairs are used when items are renamed during import using
        "as".


def get_special_dtypes_update(self, model, torch_dtype: 'torch.dtype') -> Dict[str, 'torch.dtype']:
    return {name: torch_dtype for name, _ in model.named_parameters() if any((m in name for m in self.modules_to_not_convert))}

returns dtypes for modules that are not quantized - used for the computation of the device_map in case
    one passes a str as a device_map. The method will use the `modules_to_not_convert` that is modified
    in `_process_model_before_weight_loading`.

Args:
        model (`~transformers.PreTrainedModel`):
            The model to quantize
        torch_dtype (`torch.dtype`):
            The dtype passed in `from_pretrained` method.


def enable_traceback_filtering():
    if sys.version_info.major != 3 or sys.version_info.minor < 7:
        raise RuntimeError(f'Traceback filtering is only available with Python 3.7 or higher. This Python version: {sys.version}')
    global _ENABLE_TRACEBACK_FILTERING
    _ENABLE_TRACEBACK_FILTERING.value = True

Enable filtering out TensorFlow-internal frames in exception stack traces.

    Raw TensorFlow stack traces involve many internal frames, which can be
    challenging to read through, while not being actionable for end users.
    By default, TensorFlow filters internal frames in most exceptions that it
    raises, to keep stack traces short, readable, and focused on what's
    actionable for end users (their own code).

    If you have previously disabled traceback filtering via
    `tf.debugging.disable_traceback_filtering()`, you can re-enable it via
    `tf.debugging.enable_traceback_filtering()`.

Raises:
        RuntimeError: If Python version is not at least 3.7.


def ParseCodeToTree(code):
    if not code.endswith(os.linesep):
        code += os.linesep
    try:
        parser_driver = driver.Driver(_PYTHON_GRAMMAR, convert=pytree.convert)
        tree = parser_driver.parse_string(code, debug=False)
    except parse.ParseError:
        ast.parse(code)
        raise
    return _WrapEndMarker(tree)

Parse the given code to a lib2to3 pytree.

Args:
        code: a string with the code to parse.

Raises:
        SyntaxError if the code is invalid syntax.
        parse.ParseError if some other parsing failure.

Returns:
        The root node of the parsed tree.


def _scipy_raised_cosine(length, symmetric=True, a=0.5, b=0.5):
    if length == 1:
        return np.ones(1)
    odd = length % 2
    if not symmetric and (not odd):
        length += 1
    window = a - b * np.cos(2.0 * np.pi * np.arange(length) / (length - 1))
    if not symmetric and (not odd):
        window = window[:-1]
    return window

A simple implementation of a raised cosine window that matches SciPy.

    https://en.wikipedia.org/wiki/Window_function#Hann_window
    https://github.com/scipy/scipy/blob/v0.14.0/scipy/signal/windows.py#L615

Args:
        length: The window length.
        symmetric: Whether to create a symmetric window.
        a: The alpha parameter of the raised cosine window.
        b: The beta parameter of the raised cosine window.

Returns:
        A raised cosine window of length `length`.


def __init__(self, tie_breaker=DEFAULT_NORMAL_LABEL, **kwargs):
    self._tie_breaker = tie_breaker

    def inner(predictions: Iterable[int]) -> int:
        counters = collections.Counter(predictions)
        if counters[self._normal_label] < counters[self._outlier_label]:
            vote = self._outlier_label
        elif counters[self._normal_label] > counters[self._outlier_label]:
            vote = self._normal_label
        else:
            vote = self._tie_breaker
        return vote
    super().__init__(agg_func=inner, **kwargs)

Aggregates anomaly labels using majority voting.

    This `AggregationFn` implements a majority voting strategy to combine
    anomaly labels from multiple `AnomalyPrediction` objects. It counts the
    occurrences of normal and outlier labels and selects the label with the
    higher count as the aggregated label. In case of a tie, a tie-breaker
    label is used.

Example:
        If input labels are [normal, outlier, outlier, normal, outlier], and
        normal_label=0, outlier_label=1, then the aggregated label will be
        outlier (1) because outliers have a majority (3 vs 2).

Args:
        normal_label (int): The integer label for normal predictions. Defaults to 0.
        outlier_label (int): The integer label for outlier predictions. Defaults to
        1.
        tie_breaker (int): The label to return if there is a tie in votes.
        Defaults to 0 (normal_label).
        **kwargs: Additional keyword arguments to pass to the base
        `LabelAggregation` class.


def run(argv=None, save_main_session=True, test_pipeline=None) -> PipelineResult:
    known_args, pipeline_args = parse_known_args(argv)
    pipeline_options = PipelineOptions(pipeline_args)
    pipeline_options.view_as(SetupOptions).save_main_session = save_main_session
    milk_quality_data = pandas.read_csv(known_args.pipeline_input_data)
    start = time.mktime(time.strptime('2023/06/29 10:00:00', '%Y/%m/%d %H:%M:%S'))
    test_stream = TestStream()
    test_stream.advance_watermark_to(start)
    samples = [milk_quality_data.iloc[i:i + 1] for i in range(len(milk_quality_data))]
    for watermark_offset, sample in enumerate(samples, 1):
        test_stream.advance_watermark_to(start + watermark_offset)
        test_stream.add_elements([sample])
    test_stream.advance_watermark_to_infinity()
    model_handler = XGBoostModelHandlerPandas(model_class=xgboost.XGBClassifier, model_state=known_args.model_state)
    with beam.Pipeline() as p:
        _ = p | test_stream | 'window' >> beam.WindowInto(window.SlidingWindows(30, 5)) | 'RunInference' >> RunInference(model_handler) | 'Count number of elements in window' >> beam.CombineGlobally(AggregateMilkQualityResults()).without_defaults() | 'Print' >> beam.Map(print)

Args:
    argv: Command line arguments defined for this example.
    save_main_session: Used for internal testing.
    test_pipeline: Used for internal testing.


def broadcast_flat_values(self, rt, inner_dimensions=True):
    if ragged_tensor.is_ragged(rt):
        rt = rt.flat_values
    if self.target_shape.rank == 0:
        return rt
    inner_rank = self.target_shape.inner_rank
    if inner_rank > self._source_shape.rank:
        if self.source_shape.num_row_partitions > 0:
            rt = array_ops.reshape(rt, self.source_shape._alt_inner_shape(self.source_shape.rank))
        if inner_dimensions:
            return array_ops.broadcast_to(rt, self._target_inner_shape_int32())
        return rt
    else:
        if self._source_shape.inner_rank != inner_rank:
            rt = array_ops.reshape(rt, self._source_shape._alt_inner_shape(inner_rank))
        flat_broadcaster = self._layer_broadcasters[-inner_rank]
        rt = flat_broadcaster.broadcast_tensor(rt)
        if inner_dimensions:
            rt = array_ops.broadcast_to(rt, self._target_inner_shape_int32())
        return rt

flat_values of a ragged tensor broadcast to target_shape.

    If inner_dimensions==True, then the result is a dense tensor with shape
    target_shape.inner_shape, the flat values of the broadcasted shape.

    If you add target_shape.row_partitions, you will get the full broadcasted
    shape.

    If inner_dimensions==False, the result is a dense tensor that satsifies
    certain properties:
    1. broadcast_to(result, target_shape.inner_shape) will give the result
        if inner_dimensions==True.
    2. Either (a) (result.rank < target_shape.inner_rank)
        or (b) (result.shape[0] == target_shape.inner_shape[0]).
    3. result.rank = min(target_shape.inner_rank, rt.rank)
    4. For i < target_shape.inner_rank - 1, and i < rt.rank,
        and if rt.shape[-i]!=1, then result.shape[-i]=target_shape[-i].

Args:
        rt: a ragged or dense tensor.
        inner_dimensions: if true, broadcast the inner dimensions as well.

Returns:
        a dense tensor


def exp(x):
    if any_symbolic_tensors((x,)):
        return Exp().symbolic_call(x)
    return backend.numpy.exp(x)

Calculate the exponential of all elements in the input tensor.

Args:
        x: Input tensor.

Returns:
        Output tensor, element-wise exponential of `x`.


def register_binary_elementwise_assert_api(func):
    _BINARY_ELEMENTWISE_ASSERT_APIS.append(func)
    for args, handler in _ELEMENTWISE_API_HANDLERS.items():
        if len(args) == 3 and args[2] is _ASSERT_API_TAG:
            _add_dispatch_for_binary_elementwise_api(func, args[0], args[1], handler)
    return func

Decorator that registers a TensorFlow op as a binary elementwise assert API.

    Different from `dispatch_for_binary_elementwise_apis`, this decorator is used
    for assert apis, such as assert_equal, assert_none_equal, etc, which return
    None in eager mode and an op in graph mode.

Args:
        func: The function that implements the binary elementwise assert API.

Returns:
        `func`


def __init__(self):
    self.inputs = {}
    self.outputs = {}
    self.function_name = None
    self.uuid = None
    self.params = {}
    self.level = -1
    self.children_inputs_mappings = {}

Represent a TensorFlow Lite custom function.

    This is uses to accumulate found hints in the graphdef into a single
    conceptual unit.

Attributes:
        inputs: inputs to the op (hash from index # to argument)
        outputs: outputs to the op (hash from index # to argument)
        function_name: the tflite custom op name to use
        uuid: a unique call id for this particular call  (i.e. multiple function
        calls would have the same function_name but different uuids.
        params: A param name to key value for op constant data. I.e. for axis on a
        reduction, strides on a convolution, etc.
        level: Level of the OpHint.
        children_inputs_mappings: If the Ophint has children, children inputs
        mappings indicate how their inputs & outputs are mapped.


def batch_encode_plus(self, notes: Union[np.ndarray, List[pretty_midi.Note]], truncation_strategy: Optional[TruncationStrategy]=None, max_length: Optional[int]=None, **kwargs) -> BatchEncoding:
    encoded_batch_token_ids = []
    for i in range(len(notes)):
        encoded_batch_token_ids.append(self.encode_plus(notes[i], truncation_strategy=truncation_strategy, max_length=max_length, **kwargs)['token_ids'])
    return BatchEncoding({'token_ids': encoded_batch_token_ids})

This is the `batch_encode_plus` method for `Pop2PianoTokenizer`. It converts the midi notes to the transformer
    generated token ids. It works on multiple batches by calling `encode_plus` multiple times in a loop.

Args:
        notes (`numpy.ndarray` of shape `[batch_size, sequence_length, 4]` or `list` of `pretty_midi.Note` objects):
            This represents the midi notes. If `notes` is a `numpy.ndarray`:
                - Each sequence must have 4 values, they are `onset idx`, `offset idx`, `pitch` and `velocity`.
            If `notes` is a `list` containing `pretty_midi.Note` objects:
                - Each sequence must have 4 attributes, they are `start`, `end`, `pitch` and `velocity`.
        truncation_strategy ([`~tokenization_utils_base.TruncationStrategy`], *optional*):
            Indicates the truncation strategy that is going to be used during truncation.
        max_length (`int`, *optional*):
            Maximum length of the returned list and optionally padding length (see above).

Returns:
        `BatchEncoding` containing the tokens ids.


def _copy_source(s, graph, op_map, handle_captures, inverse_captures, base_graph):
    if handle_captures and s in inverse_captures:
        copied_placeholder = graph.capture(inverse_captures[s], name=s.op.name)
    elif s.op.type == 'PlaceholderWithDefault' and _constant_inputs(s):
        default_value = s.op.inputs[0]
        unavailable_inputs, unavailable_control_inputs = _copy_non_source(op=default_value.op, graph=graph, op_map=op_map, base_graph=base_graph)
        if unavailable_inputs or unavailable_control_inputs:
            raise AssertionError('Could not copy source node {} because it has inputs.'.format(default_value))
        with ops.device(s.op.device):
            copied_placeholder = array_ops.placeholder_with_default(input=op_map[default_value], shape=s.shape, name=s.op.name)
    else:
        with ops.device(s.op.device):
            copied_placeholder = array_ops.placeholder(dtype=s.dtype, shape=s.shape, name=s.op.name)
    base_handle = resource_variable_ops.get_resource_handle_data(s)
    if base_handle.shape_and_type:
        resource_variable_ops._set_handle_shapes_and_types(copied_placeholder, base_handle, graph_mode=True)
    op_map[s] = copied_placeholder
    op_map[s.op] = copied_placeholder.op

Create a source in a graph based on a Tensor from a different graph.

    This function creates a placeholder analog of `s` in a graph with the
    following behavior:

    1) If s is a captured Tensor or Variable and handle_captures is set to True,
        simply capture it in the new graph as well.

    2) If s is a PlaceholderWithDefault whose default is a constant, preserve
        said default in the new graph.

    3) When applicable, copy resource variable metadata from `s` to the newly
        created placeholder.

Args:
        s: The source of interest.
        graph: The destination graph.
        op_map: A dict mapping ops and tensors in the old graph to the new one.
        handle_captures: A boolean indicating whether to re-capture s in the new
        graph or simply create a vanilla placeholder.
        inverse_captures: A dict mapping s back to the Tensor or Variable that it
        captures.
        base_graph: The graph being copied from.


def print_tensor(x, message='', summarize=3):
    if isinstance(x, tensor_lib.Tensor) and hasattr(x, 'graph'):
        with get_graph().as_default():
            op = logging_ops.print_v2(message, x, output_stream=sys.stdout, summarize=summarize)
            with ops.control_dependencies([op]):
                return array_ops.identity(x)
    else:
        logging_ops.print_v2(message, x, output_stream=sys.stdout, summarize=summarize)
        return x

Prints `message` and the tensor value when evaluated.

    Note that `print_tensor` returns a new tensor identical to `x`
    which should be used in the following code. Otherwise the
    print operation is not taken into account during evaluation.

Example:
    >>> x = tf.constant([[1.0, 2.0], [3.0, 4.0]])
    >>> _ = tf.keras.backend.print_tensor(x)
    [[1 2]
    [3 4]]

Args:
        x: Tensor to print.
        message: Message to print jointly with the tensor.
        summarize: The first and last `summarize` elements within each dimension
            are recursively printed per Tensor. If None, then the first 3 and last
            3 elements of each dimension are printed for each tensor. If set to
            -1, it will print all elements of every tensor.

Returns:
        The same tensor `x`, unchanged.


def generate_file(filename: str, config: Config, nrows: int) -> None:
    fake = Faker(use_weighting=False)
    columns = [column.bq_name for column in config]
    with open(filename, 'w+') as f:
        csv = DictWriter(f, fieldnames=columns)
        csv.writeheader()
        buffer_: List[Row] = []
        buffer_size = nrows // 100
        buffer = Buffer[Row](buffer_, buffer_size, csv.writerows)
        for _ in range(nrows + 1):
            buffer.push(generate_row(fake, config))
        buffer.flush(force=True)

Generate a test data file conforming to the given config.

Args:
        * filename: Local path to csv file to be generated
        * config: Config key from configs.CONFIGS dictionary
        * nrows: number of rows to generate


def _extract_next_file(archive_file: io.BufferedIOBase) -> Iterator[Tuple[str, bytes]]:
    while True:
        header = archive_file.read(60)
        if not header:
            return
        elif len(header) < 60:
            raise RuntimeError('Invalid file header format.')
        name, _, _, _, _, size, end = struct.unpack('=16s12s6s6s8s10s2s', header)
        if end != b'`\n':
            raise RuntimeError('Invalid file header format.')
        name = name.decode('ascii').strip()
        size = int(size, base=10)
        odd_size = size % 2 == 1
        if name.startswith('#1/'):
            filename_size = int(name[3:])
            name = archive_file.read(filename_size).decode('utf-8').strip(' \x00')
            size -= filename_size
        file_content = archive_file.read(size)
        if odd_size:
            archive_file.read(1)
        yield (name, file_content)

Extracts the next available file from the archive.

    Reads the next available file header section and yields its filename and
    content in bytes as a tuple. Stops when there are no more available files in
    the provided archive_file.

Args:
        archive_file: The archive file object, of which cursor is pointing to the
        next available file header section.

Yields:
        The name and content of the next available file in the given archive file.

Raises:
        RuntimeError: The archive_file is in an unknown format.


def build_estimator(tf_transform_output, config, hidden_units=None):
    transformed_feature_spec = tf_transform_output.transformed_feature_spec().copy()
    transformed_feature_spec.pop(taxi.transformed_name(taxi.LABEL_KEY))
    real_valued_columns = [tf.feature_column.numeric_column(key, shape=()) for key in taxi.transformed_names(taxi.DENSE_FLOAT_FEATURE_KEYS)]
    categorical_columns = [tf.feature_column.categorical_column_with_identity(key, num_buckets=taxi.VOCAB_SIZE + taxi.OOV_SIZE, default_value=0) for key in taxi.transformed_names(taxi.VOCAB_FEATURE_KEYS)]
    categorical_columns += [tf.feature_column.categorical_column_with_identity(key, num_buckets=taxi.FEATURE_BUCKET_COUNT, default_value=0) for key in taxi.transformed_names(taxi.BUCKET_FEATURE_KEYS)]
    categorical_columns += [tf.feature_column.categorical_column_with_identity(key, num_buckets=num_buckets, default_value=0) for key, num_buckets in zip(taxi.transformed_names(taxi.CATEGORICAL_FEATURE_KEYS), taxi.MAX_CATEGORICAL_FEATURE_VALUES)]
    return tf_estimator.DNNLinearCombinedClassifier(config=config, linear_feature_columns=categorical_columns, dnn_feature_columns=real_valued_columns, dnn_hidden_units=hidden_units or [100, 70, 50, 25])

Build an estimator for predicting the tipping behavior of taxi riders.

Args:
        tf_transform_output: A TFTransformOutput.
        config: tf.contrib.learn.RunConfig defining the runtime environment for the
        estimator (including model_dir).
        hidden_units: [int], the layer sizes of the DNN (input layer first)

Returns:
        Resulting DNNLinearCombinedClassifier.


def __init__(self, default: Any=utils.MISSING_VALUE):
    self.default = default

Value placeholder for arguments whose value will be provided by presets.

Example:
        def foo(x, y=pg.PresetArgValue(default=1))
        return x + y

        with pg.preset_args(y=2):
        print(foo(x=1))  # 3: y=2
        print(foo(x=1))  # 2: y=1


def from_question_encoder_generator_configs(cls, question_encoder_config: PretrainedConfig, generator_config: PretrainedConfig, **kwargs) -> PretrainedConfig:
    return cls(question_encoder=question_encoder_config.to_dict(), generator=generator_config.to_dict(), **kwargs)

Instantiate a [`EncoderDecoderConfig`] (or a derived class) from a pre-trained encoder model configuration and
    decoder model configuration.

Returns:
        [`EncoderDecoderConfig`]: An instance of a configuration object


def update_state(self, y_true, y_pred, sample_weight=None):
    y_true = ops.convert_to_tensor(y_true, dtype=self._dtype)
    y_pred = ops.convert_to_tensor(y_pred, dtype=self._dtype)
    y_true, y_pred = squeeze_or_expand_to_same_rank(y_true, y_pred)
    if not self._built:
        self._build(y_true.shape, y_pred.shape)
    if sample_weight is None:
        sample_weight = 1
    sample_weight = ops.convert_to_tensor(sample_weight, dtype=self.dtype)
    if len(sample_weight.shape) == 1:
        sample_weight = ops.expand_dims(sample_weight, axis=1)
    sample_weight = ops.broadcast_to(sample_weight, ops.shape(y_true))
    weighted_y_true = y_true * ops.cast(sample_weight, y_true.dtype)
    self.sum.assign(self.sum + ops.sum(weighted_y_true, axis=0))
    self.squared_sum.assign(self.squared_sum + ops.sum(y_true * weighted_y_true, axis=0))
    self.total_mse.assign(self.total_mse + ops.sum((y_true - y_pred) ** 2 * ops.cast(sample_weight, y_true.dtype), axis=0))
    self.count.assign(self.count + ops.sum(sample_weight, axis=0))
    self.num_samples.assign(self.num_samples + ops.size(y_true))

Accumulates root mean squared error statistics.

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

Returns:
        Update op.


def imag(input, name=None):
    with ops.name_scope(name, 'Imag', [input]) as name:
        input = ops.convert_to_tensor(input, name='input')
        if input.dtype.is_complex:
            return gen_math_ops.imag(input, Tout=input.dtype.real_dtype, name=name)
        else:
            return array_ops.zeros_like(input)

Returns the imaginary part of a complex (or real) tensor.

    Given a tensor `input`, this operation returns a tensor of type `float` that
    is the imaginary part of each element in `input` considered as a complex
    number. If `input` is real, a tensor of all zeros is returned.

    For example:

    ```python
    x = tf.constant([-2.25 + 4.75j, 3.25 + 5.75j])
    tf.math.imag(x)  # [4.75, 5.75]
    ```

Args:
        input: A `Tensor`. Must be one of the following types: `float`, `double`,
        `complex64`, `complex128`.
        name: A name for the operation (optional).

Returns:
        A `Tensor` of type `float32` or `float64`.


def _convert(self, value, dtype):
    if isinstance(value, resource_variable_ops.ResourceVariable):
        raise RuntimeError(f'Attempting to return a variable from an eagerly executed py_func. Only numeric data structures like Tensors or NumPy arrays should be returned; to return the value of a variable, make sure to obtain the Tensor backing it by calling `.read_value()` on the variable in question: {value}')
    if value is None and self._is_grad_func:
        return constant_op.constant(0.0, dtype=dtype)
    return ops.convert_to_tensor(value, dtype=dtype)

Converts `value` to a tensor of type `dtype`, with error checking.

Args:
        value: The tensor to convert.
        dtype: The desired dtype.

Returns:
        A tensor of type `dtype`, or a zeros tensor if value is None and
        this function is in fact a gradient function.

Raises:
        RuntimeError: if `value` is a variable.