def __init__(self, ctx: base.ContextType, name: str, param_names: tuple[str, ...], *, posonly_count: int=0, varargs_name: str | None=None, kwonly_params: tuple[str, ...]=(), kwargs_name: str | None=None, defaults: Mapping[str, base.BaseValue]=datatypes.EMPTY_MAP, annotations: Mapping[str, base.BaseValue]=datatypes.EMPTY_MAP):
    self._ctx = ctx
    self.name = name
    self.param_names = param_names
    self.posonly_count = posonly_count
    self.varargs_name = varargs_name
    self.kwonly_params = kwonly_params
    self.kwargs_name = kwargs_name
    self.defaults = defaults
    self.annotations = annotations

Representation of a Python function signature.

Attributes:
        name: Name of the function.
        param_names: A tuple of positional parameter names. This DOES include
        positional-only parameters and does NOT include keyword-only parameters.
        posonly_count: Number of positional-only parameters.
        varargs_name: Name of the varargs parameter. (The "args" in *args)
        kwonly_params: Tuple of keyword-only parameters.
        E.g. ("x", "y") for "def f(a, *, x, y=2)". These do NOT appear in
        param_names. Ordered like in the source file.
        kwargs_name: Name of the kwargs parameter. (The "kwargs" in **kwargs)
        defaults: Dictionary, name to value, for all parameters with default values.
        annotations: A dictionary of type annotations. (string to type)
        posonly_params: Tuple of positional-only parameters (i.e., the first
        posonly_count names in param_names).


def get_image_features(self, pixel_values_images: torch.FloatTensor, vision_feature_layer: Optional[Union[int, List[int]]]=None, vision_feature_select_strategy: Optional[str]=None):
    vision_feature_layer = vision_feature_layer if vision_feature_layer is not None else self.config.vision_feature_layer
    vision_feature_select_strategy = vision_feature_select_strategy if vision_feature_select_strategy is not None else self.config.vision_feature_select_strategy
    if vision_feature_select_strategy not in ['default', 'full']:
        raise ValueError(f'Unexpected select feature strategy: {self.config.vision_feature_select_strategy}')
    image_outputs = self.image_tower(pixel_values_images, output_hidden_states=True)
    if isinstance(vision_feature_layer, int):
        image_outputs = image_outputs.hidden_states[vision_feature_layer]
        if vision_feature_select_strategy == 'default':
            image_outputs = image_outputs[:, 1:]
    else:
        hs_pool = [image_outputs.hidden_states[layer_idx] for layer_idx in vision_feature_layer]
        if vision_feature_select_strategy == 'default':
            hs_pool = [hs[:, 1:] for hs in hs_pool]
        image_outputs = torch.cat(hs_pool, dim=-1)
    image_features = self.multi_modal_projector(image_outputs)
    return image_features

Obtains image last hidden states from the vision tower and apply multimodal projection.

Args:
        pixel_values_images (`torch.FloatTensor]` of shape `(batch_size, channels, height, width)`)
        The tensors corresponding to the input images.
        vision_feature_layer (`Union[int, List[int]]`, *optional*):
            The index of the layer to select the vision feature. If multiple indices are provided,
            the vision feature of the corresponding indices will be concatenated to form the
            vision features.
        vision_feature_select_strategy (`str`, *optional*):
            The feature selection strategy used to select the vision feature from the vision backbone.
            Can be one of `"default"` or `"full"`

Returns:
        image_features (`torch.Tensor`): Image feature tensor of shape `(num_images, image_length, embed_dim)`).


def GenerateCostReport(metagraph, per_node_report=False, verbose=False, cluster=None):
    if cluster is None:
        cluster = gcluster.Cluster(disable_detailed_stats=False)
    return tf_wrap.GenerateCostReport(metagraph.SerializeToString(), per_node_report, verbose, cluster.tf_cluster)

Analyze the cost of each TensorFlow op and node in the provided metagraph.

Args:
        metagraph: A TensorFlow MetaGraphDef.
        per_node_report: by default the report contains stats aggregated on a per op
        type basis, setting per_node_report to True adds results for each
        individual node to the report.
        verbose: Prints out the entire operation proto instead of a summary table.
        cluster: Analyze the costs using the specified cluster, or the local machine
        if no cluster was specified.

Returns:
        A string of cost report.


def list_files_by_mtime(dirpath):
    files = [f for f in os.listdir(dirpath) if is_real_file(dirpath, f)]
    return sorted(files, key=lambda f: get_mtime(dirpath, f))

Return a list of files in the directory, sorted in increasing "mtime".

    Return a list of files in the given directory, sorted from older to newer file
    according to their modification times.  Only return actual files, skipping
    directories, symbolic links, pipes, etc.

Args:
        dirpath: directory pathname

Returns:
        A list of file names relative to the given directory path.


def enter_loop_section(self, section_id, entry_node):
    assert section_id not in self.section_entry
    assert section_id not in self.continues
    self.continues[section_id] = set()
    node = self.add_ordinary_node(entry_node)
    self.section_entry[section_id] = node

Enters a loop section.

    Loop sections define an entry node. The end of the section always flows back
    to the entry node. These admit continue jump nodes which also flow to the
    entry node.

Args:
        section_id: Hashable, the same node that will be used in calls to the
        ast_node arg passed to add_continue_node
        entry_node: ast.AST, the entry node into the loop (e.g. the test node for
        while loops)


def forward(self, outputs, targets):
    outputs_without_aux = {k: v for k, v in outputs.items() if 'auxiliary_outputs' not in k}
    indices = self.matcher(outputs_without_aux, targets)
    num_boxes = sum((len(t['class_labels']) for t in targets))
    num_boxes = torch.as_tensor([num_boxes], dtype=torch.float, device=next(iter(outputs.values())).device)
    num_boxes = torch.clamp(num_boxes, min=1).item()
    losses = {}
    for loss in self.losses:
        l_dict = self.get_loss(loss, outputs, targets, indices, num_boxes)
        l_dict = {k: l_dict[k] * self.weight_dict[k] for k in l_dict if k in self.weight_dict}
        losses.update(l_dict)
    if 'auxiliary_outputs' in outputs:
        for i, auxiliary_outputs in enumerate(outputs['auxiliary_outputs']):
            indices = self.matcher(auxiliary_outputs, targets)
            for loss in self.losses:
                if loss == 'masks':
                    continue
                l_dict = self.get_loss(loss, auxiliary_outputs, targets, indices, num_boxes)
                l_dict = {k: l_dict[k] * self.weight_dict[k] for k in l_dict if k in self.weight_dict}
                l_dict = {k + f'_aux_{i}': v for k, v in l_dict.items()}
                losses.update(l_dict)
    if 'dn_auxiliary_outputs' in outputs:
        if 'denoising_meta_values' not in outputs:
            raise ValueError("The output must have the 'denoising_meta_values` key. Please, ensure that 'outputs' includes a 'denoising_meta_values' entry.")
        indices = self.get_cdn_matched_indices(outputs['denoising_meta_values'], targets)
        num_boxes = num_boxes * outputs['denoising_meta_values']['dn_num_group']
        for i, auxiliary_outputs in enumerate(outputs['dn_auxiliary_outputs']):
            for loss in self.losses:
                if loss == 'masks':
                    continue
                kwargs = {}
                l_dict = self.get_loss(loss, auxiliary_outputs, targets, indices, num_boxes, **kwargs)
                l_dict = {k: l_dict[k] * self.weight_dict[k] for k in l_dict if k in self.weight_dict}
                l_dict = {k + f'_dn_{i}': v for k, v in l_dict.items()}
                losses.update(l_dict)
    return losses

This performs the loss computation.

Args:
        outputs (`dict`, *optional*):
            Dictionary of tensors, see the output specification of the model for the format.
        targets (`List[dict]`, *optional*):
            List of dicts, such that `len(targets) == batch_size`. The expected keys in each dict depends on the
            losses applied, see each loss' doc.


def _convert_value(value, expected_type, path, context=_ConversionContext.VALUE):
    assert isinstance(path, tuple)
    if expected_type is None:
        expected_type = _NoneType
    if expected_type is tensor.Tensor:
        return _convert_tensor(value, path, context)
    elif isinstance(expected_type, type) and _issubclass(expected_type, composite_tensor.CompositeTensor):
        return _convert_composite_tensor(value, expected_type, path, context)
    elif expected_type is tensor_shape.TensorShape:
        try:
            return tensor_shape.as_shape(value)
        except TypeError as e:
            raise TypeError(f"{''.join(path)}: expected 'tf.TensorShape', got {type(value).__name__!r}") from e
    elif expected_type is dtypes.DType:
        try:
            return dtypes.as_dtype(value)
        except TypeError as e:
            raise TypeError(f"{''.join(path)}: expected 'tf.DType', got {type(value).__name__!r}") from e
    elif expected_type in (int, float, bool, str, bytes, _NoneType):
        if not isinstance(value, expected_type):
            raise TypeError(f'{''.join(path)}: expected {expected_type.__name__!r}, got {type(value).__name__!r}')
        return value
    elif type_annotations.is_generic_tuple(expected_type):
        return _convert_tuple(value, expected_type, path, context)
    elif type_annotations.is_generic_mapping(expected_type):
        return _convert_mapping(value, expected_type, path, context)
    elif type_annotations.is_generic_union(expected_type):
        return _convert_union(value, expected_type, path, context)
    else:
        raise TypeError(f'{''.join(path)}: Unsupported type annotation {expected_type!r}')

Type-checks and converts a value.

Args:
        value: The value to type-check.
        expected_type: The expected type for the value.
        path: Tuple of `str` naming the value (used for exception messages).
        context: _ConversionContext, indicates what kind of value we are converting.

Returns:
        A copy of `value`, converted to the expected type.

Raises:
        TypeError: If `value` can not be converted to the expected type.


def np_reshape(a, /, shape=None, *, newshape=None, order='C', copy=None):
    if shape is None:
        shape = newshape
    if np.lib.NumpyVersion(np.__version__) >= '2.1.0.rc0':
        if shape is None and newshape is None:
            return np.asarray(a, order=order, copy=copy)
        return np.reshape(a, shape, order=order, copy=copy)
    return np.reshape(a, shape, order=order)

Reshapes an array without changing its data.

    NumPy 2.1.0rc1 added shape and copy arguments to numpy.reshape. See
    https://github.com/numpy/numpy/pull/26292. Both newshape and shape keywords
    are supported, but newshape is going to be deprecated. Use `shape` instead.

    Besides, shape cannot be None now. See
    https://github.com/numpy/numpy/blob/v2.1.0rc1/numpy/_core/fromnumeric.py#L309.
    Previously, np.reshape with newshape=None returned a copy. To maintain this
    behavior, we now use asarray to create an ndarray.

Args:
        a: Array_like. Array to be reshaped.
        shape: The new shape of the array.
        newshape: The new shape of the array (deprecated).
        order: {‘C’, ‘F’, ‘K’}.
        copy: bool. If True, then the array data is copied. If None, a copy will
        only be made if it’s required by order. For False it raises a ValueError if
        a copy cannot be avoided.

Returns:
        This will be a new view object if possible; otherwise, it will be a copy.


def __init__(self, name):
    NamedObject.__init__(self, name)
    self.values = []

Expressions to expand to other objects.

    Messages, events, states, transitions can be parameterized. A parameterized
    object can be expanded with values.
    Note that Resolve() is only used to expand parameterized messages. Other
    parameterized objects are expanded differently.

Attributes:
        values: List of values to be used when expanding to other object.


def column_type(self, agencyId: int, advertiserId: int, column: str) -> str:
    if column not in self.columns:
        for saved_column in API_SearchAds(self.config, self.auth, iterate=True).savedcolumns().list(agencyId=agencyId, advertiserId=advertiserId).execute():
            self.columns[saved_column['savedColumnName']] = SA_TYPES.get(saved_column['type'], 'STRING')
    return self.columns.get(column, 'STRING')

Return the column type for the given column name.

    Intended mostly as an internl helper function but left open for convenience.
    Leverages both saved columns and standard columns.
    Does not distinguish saved from standard, will this be a problem?

Args:
    agencyId - required only for saved columns, usually derived from report
    advertiserid - required only for saved columns, usually derived from report

Returns:
    Column type as defnined by BigQuery. Defaults to STRING if not found.


def resize(self, image: np.ndarray, size: Dict[str, int], resample: PILImageResampling=PILImageResampling.BICUBIC, data_format: Optional[Union[str, ChannelDimension]]=None, input_data_format: Optional[Union[str, ChannelDimension]]=None, **kwargs) -> np.ndarray:
    size = get_size_dict(size)
    if 'height' not in size or 'width' not in size:
        raise ValueError(f'The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}')
    output_size = (size['height'], size['width'])
    return resize(image, size=output_size, resample=resample, data_format=data_format, input_data_format=input_data_format, **kwargs)

Resize an image to `(size["height"], size["width"])`.

Args:
        image (`np.ndarray`):
            Image to resize.
        size (`Dict[str, int]`):
            Dictionary in the format `{"height": int, "width": int}` specifying the size of the output image.
        resample (`PILImageResampling`, *optional*, defaults to `PILImageResampling.BICUBIC`):
            `PILImageResampling` filter to use when resizing the image e.g. `PILImageResampling.BICUBIC`.
        data_format (`ChannelDimension` or `str`, *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.
            - `"none"` or `ChannelDimension.NONE`: image in (height, width) 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.
            - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.

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


def _contains_composite_function_call(self, graphdef: graph_pb2.GraphDef) -> bool:
    return any(map(self._is_composite_function, graphdef.library.function))

Determines if the graph def has composite function call.

Args:
        graphdef: A GraphDef object.

Returns:
        True if and only if the graph def contains a composite function call.


def __init__(self, callable_type: CallableType, name: str, module_name: str, args: Optional[List[Argument]]=None, kwonlyargs: Optional[List[Argument]]=None, varargs: Optional[Argument]=None, varkw: Optional[Argument]=None, return_value: Optional[class_schema.ValueSpec]=None, qualname: Optional[str]=None, description: Optional[str]=None):
    args = args or []
    self.callable_type = callable_type
    self.name = name
    self.module_name = module_name
    self.args = args or []
    self.kwonlyargs = kwonlyargs or []
    self.varargs = varargs
    self.varkw = varkw
    self.return_value = return_value
    self.qualname = qualname or name
    self.description = description

Constructor.

Args:
        callable_type: Type of callable.
        name: Function name.
        module_name: Module name.
        args: Specification for positional arguments
        kwonlyargs: Specification for keyword only arguments (PY3).
        varargs: Specification for wildcard list argument, e.g, 'args' is the name
        for `*args`.
        varkw: Specification for wildcard keyword argument, e.g, 'kwargs' is the
        name for `**kwargs`.
        return_value: Optional value spec for return value.
        qualname: Optional qualified name.
        description: Optional description of the signature.


def unload_snippet(self, name):
    self.services.snippets.remove_snippet_client(name)

Stops a snippet apk.

Args:
        name: The attribute name the snippet server is attached with.

Raises:
        SnippetError: The given snippet name is not registered.


def add_timestamps(with_ms: bool=False, substream_name: str | None=None) -> processor.Processor:
    if substream_name is None:
        substream_name = ''
    return processor.processor_function(functools.partial(_add_timestamps, with_ms=with_ms, substream_name=substream_name))

Adds timestamps to image chunks.

    By default the timestamps are added with the format `mm:ss` where
    `mm` is the number of minutes, `ss` is the number of seconds.

Args:
        with_ms: Whether to add milliseconds to the timestamp. When `True`, the
        timestamp is added with the format `mm:ss.SSS` where `SSS` is the number
        of milliseconds.
        substream_name: The substream name to use for the timestamps.

Returns:
        A processor that adds timestamps after each image chunk.


def convert(self, calibration_inputs: Optional[Mapping[str, np.ndarray]]=None, num_runs=1) -> None:

Converts the model with TensorRT and calibrates if using INT8 precision mode.

Args:
        calibration_inputs: Mapping from input names to ndarrays in TF1. Or a
        sequence of tensors in TF2. Used as calibration data.
        num_runs: Number of calibration runs.


def as_decimal(self) -> decimal.Decimal:
    if len(self._messages) != 1:
        raise ValueError('FHIRPath did not evaluate to a single decimal.')
    return decimal.Decimal(proto_utils.get_value_at_field(self._messages[0], 'value'))

Returns the result as a decimal.

Raises:
        ValueError if the `EvaluationResult` is not a single decimal.


def unique_object_name(name, name_uid_map=None, avoid_names=None, namespace='', zero_based=False, avoid_observed_names=False):
    if name_uid_map is None:
        name_uid_map = get_default_graph_uid_map()
    if avoid_names is None:
        if avoid_observed_names:
            avoid_names = OBSERVED_NAMES
        else:
            avoid_names = set()
    proposed_name = None
    while proposed_name is None or proposed_name in avoid_names:
        name_key = (namespace, name)
        if zero_based:
            number = name_uid_map[name_key]
            if number:
                proposed_name = name + '_' + str(number)
            else:
                proposed_name = name
            name_uid_map[name_key] += 1
        else:
            name_uid_map[name_key] += 1
            proposed_name = name + '_' + str(name_uid_map[name_key])
    return proposed_name

Makes a object name (or arbitrary string) unique within a TensorFlow graph.

Args:
        name: String name to make unique.
        name_uid_map: An optional defaultdict(int) to use when creating unique
        names. If None (default), uses a per-Graph dictionary.
        avoid_names: An optional set or dict with names which should not be used. If
        None (default), don't avoid any names unless `avoid_observed_names` is
        True.
        namespace: Gets a name which is unique within the (graph, namespace). Layers
        which are not Networks use a blank namespace and so get graph-global
        names.
        zero_based: If True, name sequences start with no suffix (e.g. "dense",
        "dense_1"). If False, naming is one-based ("dense_1", "dense_2").
        avoid_observed_names: If True, avoid any names that have been observed by
        `backend.observe_object_name`.

Returns:
        Unique string name.

Example:
    unique_object_name('dense')  # dense_1
    unique_object_name('dense')  # dense_2


def pad(self, images: list['torch.Tensor'], constant_values: Union[float, Iterable[float]]=0, return_pixel_mask: bool=True) -> tuple:
    pad_size = get_max_height_width(images)
    grouped_images, grouped_images_index = group_images_by_shape(images)
    processed_images_grouped = {}
    processed_masks_grouped = {}
    for shape, stacked_images in grouped_images.items():
        stacked_images = self._pad_image(stacked_images, pad_size, constant_values=constant_values)
        processed_images_grouped[shape] = stacked_images
        if return_pixel_mask:
            stacked_masks = make_pixel_mask(image=stacked_images, output_size=pad_size)
            processed_masks_grouped[shape] = stacked_masks
    processed_images = reorder_images(processed_images_grouped, grouped_images_index)
    processed_masks = None
    if return_pixel_mask:
        processed_masks = reorder_images(processed_masks_grouped, grouped_images_index)
    return (processed_images, processed_masks)

Pads a batch of images to the bottom and right of the image with zeros to the size of largest height and width
    in the batch and optionally returns their corresponding pixel mask.

Args:
        image (`torch.Tensor`):
            Image to pad.
        constant_values (`float` or `Iterable[float]`, *optional*):
            The value to use for the padding if `mode` is `"constant"`.
        return_pixel_mask (`bool`, *optional*, defaults to `True`):
            Whether to return a pixel mask.
        return_tensors (`str` or `TensorType`, *optional*):
            The type of tensors to return. Can be one of:
                - Unset: Return a list of `np.ndarray`.
                - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.


def _load_data_file(filename, text):
    path = filename if path_utils.isabs(filename) else get_full_path(filename)
    loader = globals().get('__loader__', None)
    if loader:
        data = loader.get_data(path)
        if text:
            return re.sub('\r\n?', '\n', data.decode('utf-8'))
        return data
    with open(path, 'r' if text else 'rb') as fi:
        return fi.read()

Get the contents of a data file from the pytype installation.

Args:
        filename: the path, relative to "pytype/"
        text: whether to load the file as text or bytes.

Returns:
        The contents of the file as a bytestring

Raises:
        IOError: if file not found


def run_inference(self, batch: Sequence[str], model: _VLLMModelServer, inference_args: Optional[dict[str, Any]]=None) -> Iterable[PredictionResult]:
    return asyncio.run(self._async_run_inference(batch, model, inference_args))

Runs inferences on a batch of text strings.

Args:
        batch: A sequence of examples as text strings.
        model: A _VLLMModelServer containing info for connecting to the server.
        inference_args: Any additional arguments for an inference.

Returns:
        An Iterable of type PredictionResult.


def ctc_batch_cost(y_true, y_pred, input_length, label_length):
    label_length = math_ops.cast(array_ops.squeeze(label_length, axis=-1), dtypes_module.int32)
    input_length = math_ops.cast(array_ops.squeeze(input_length, axis=-1), dtypes_module.int32)
    sparse_labels = math_ops.cast(ctc_label_dense_to_sparse(y_true, label_length), dtypes_module.int32)
    y_pred = math_ops.log(array_ops.transpose(y_pred, perm=[1, 0, 2]) + epsilon())
    return array_ops.expand_dims(ctc.ctc_loss(inputs=y_pred, labels=sparse_labels, sequence_length=input_length), 1)

Runs CTC loss algorithm on each batch element.

Args:
        y_true: tensor `(samples, max_string_length)`
            containing the truth labels.
        y_pred: tensor `(samples, time_steps, num_categories)`
            containing the prediction, or output of the softmax.
        input_length: tensor `(samples, 1)` containing the sequence length for
            each batch item in `y_pred`.
        label_length: tensor `(samples, 1)` containing the sequence length for
            each batch item in `y_true`.

Returns:
        Tensor with shape (samples,1) containing the
            CTC loss of each element.


Make text compatible with Markdown formatting.

    This function makes various text formatting adjustments to make it compatible with Markdown.

Args:
        text (`str`):
            The input text to be made Markdown-compatible.

Returns:
        `str`: The Markdown-compatible text.


def output_shapes(self):
    return nest.map_structure(lambda component_spec: component_spec._to_legacy_output_shapes(), self._element_spec)

Returns the shape of each component of an element of this iterator.

Returns:
        A (nested) structure of `tf.TensorShape` objects corresponding to each
        component of an element of this dataset.


def from_dict(event_dict):
    return SnippetEvent(callback_id=event_dict['callbackId'], name=event_dict['name'], creation_time=event_dict['time'], data=event_dict['data'])

Create a SnippetEvent object from a dictionary.

    DEPRECATED: Use mobly.snippet.callback_event.from_dict instead.

Args:
        event_dict: a dictionary representing an event.

Returns:
        A SnippetEvent object.


def days(value: Union[int, float]) -> Duration:
    return float(value * 60 * 60 * 24)

Converts input value from number of days to a `Duration` in seconds.

Example:
        ```python
        >>> a = tp.event_set(
        ...    # Dates are converted to unix timestamps
        ...    timestamps=["2020-01-01", "2020-01-02", "2020-01-31"],
        ...    features={"f1": [1, 5, -5]}
        ... )

        >>> a.moving_sum(window_length=tp.duration.days(2))
        indexes: ...
                timestamps: ['2020-01-01T00:00:00' '2020-01-02T00:00:00'
                            '2020-01-31T00:00:00']
                'f1': [ 1 6 -5]
        ...

        ```

Args:
        value: number of days.

Returns:
        Equivalent number of seconds.


def _print_if_not_none(obj):
    if obj is not None:
        print(repr(obj))

Print like a notebook: Show the repr if the object is not None.

    `_patch_compile` Uses this on the final expression in each cell.

    This way the outputs feel like notebooks.

Args:
        obj: the object to print.


def _stop_profiler(self, save=True):
    if not self._profiler_started:
        return
    try:
        profiler.stop(save=save)
    except errors.UnavailableError as e:
        logging.error('Failed to stop profiler: %s', e.message)
    finally:
        self._profiler_started = False

Stops the profiler if currently active.

Args:
        save: Whether to save the profiler results to TensorBoard.


def _generate_unique_name(self, symbol_name: str) -> str:
    normalized_symbol_name = self._NON_SYMBOL_NAME_CHARS_REGEX.sub('_', symbol_name)
    normalized_name_conflict_count = self._num_symbols_with_normalized_name.get(normalized_symbol_name, 0)
    self._num_symbols_with_normalized_name[normalized_symbol_name] = normalized_name_conflict_count + 1
    optional_disambiguation_suffix = '' if normalized_name_conflict_count == 0 else f'_{normalized_name_conflict_count}'
    return f'{normalized_symbol_name}{optional_disambiguation_suffix}'

Translates a symbol name to a unique FileCheck capture name.

    Replaces all characters other than letters, numbers, and underscores with
    underscores. If the resulting name has already been used, appends a counter
    to disambiguate it. For example, this could result in the following sequence
    of replacements:
        1.) "foo.bar.baz" -> "foo_bar_baz"
        2.) "foo.bar_baz" -> "foo_bar_baz_1"
        3.) "foo_bar.baz" -> "foo_bar_baz_2"
        4.) "foo_bar_baz" -> "foo_bar_baz_3"

Args:
        symbol_name: The original symbol name.

Returns:
        The generated FileCheck capture name.


def __init__(self, in_group_size: int=8, out_group_size: int=1, num_codebooks: int=1, nbits_per_codebook: int=16, linear_weights_not_to_quantize: Optional[List[str]]=None, **kwargs):
    self.quant_method = QuantizationMethod.AQLM
    self.in_group_size = in_group_size
    self.out_group_size = out_group_size
    self.num_codebooks = num_codebooks
    self.nbits_per_codebook = nbits_per_codebook
    self.linear_weights_not_to_quantize = linear_weights_not_to_quantize
    self.post_init()

This is a wrapper class about `aqlm` parameters.

Args:
        in_group_size (`int`, *optional*, defaults to 8):
            The group size along the input dimension.
        out_group_size (`int`, *optional*, defaults to 1):
            The group size along the output dimension. It's recommended to always use 1.
        num_codebooks (`int`, *optional*, defaults to 1):
            Number of codebooks for the Additive Quantization procedure.
        nbits_per_codebook (`int`, *optional*, defaults to 16):
            Number of bits encoding a single codebook vector. Codebooks size is 2**nbits_per_codebook.
        linear_weights_not_to_quantize (`Optional[List[str]]`, *optional*):
            List of full paths of `nn.Linear` weight parameters that shall not be quantized.
        kwargs (`Dict[str, Any]`, *optional*):
            Additional parameters from which to initialize the configuration object.


def trace(x, name=None):
    with ops.name_scope(name, 'Trace', [x]) as name:
        x = ops.convert_to_tensor(x, name='x')
        return reduce_sum(array_ops.matrix_diag_part(x), [-1], name=name)

Compute the trace of a tensor `x`.

    `trace(x)` returns the sum along the main diagonal of each inner-most matrix
    in x. If x is of rank `k` with shape `[I, J, K, ..., L, M, N]`, then output
    is a tensor of rank `k-2` with dimensions `[I, J, K, ..., L]` where

    `output[i, j, k, ..., l] = trace(x[i, j, k, ..., l, :, :])`

    For example:

    ```python
    x = tf.constant([[1, 2], [3, 4]])
    tf.linalg.trace(x)  # 5

    x = tf.constant([[1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9]])
    tf.linalg.trace(x)  # 15

    x = tf.constant([[[1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9]],
                    [[-1, -2, -3],
                    [-4, -5, -6],
                    [-7, -8, -9]]])
    tf.linalg.trace(x)  # [15, -15]
    ```

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

Returns:
        The trace of input tensor.


def rank(input, name=None):
    return rank_internal(input, name, optimize=True)

Returns the rank of a tensor.

    See also `tf.shape`.

    Returns a 0-D `int32` `Tensor` representing the rank of `input`.

    For example:

    ```python
    # shape of tensor 't' is [2, 2, 3]
    t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
    tf.rank(t)  # 3
    ```

    **Note**: The rank of a tensor is not the same as the rank of a matrix. The
    rank of a tensor is the number of indices required to uniquely select each
    element of the tensor. Rank is also known as "order", "degree", or "ndims."

Args:
        input: A `Tensor` or `SparseTensor`.
        name: A name for the operation (optional).

Returns:
        A `Tensor` of type `int32`.

    @compatibility(numpy)
    Equivalent to np.ndim
    @end_compatibility


def validate_primitive_without_value(fhir_primitive: message.Message) -> None:
    name = fhir_primitive.DESCRIPTOR.full_name
    if len(extensions.get_fhir_extensions(fhir_primitive)) < 2:
        raise fhir_errors.InvalidFhirError(f'{name!r} must have either extensions or a value present.')
    for field, _ in fhir_primitive.ListFields():
        if field.name not in extensions.NON_VALUE_FIELDS:
            raise fhir_errors.InvalidFhirError(f'{name!r} contains PrimitiveHasNoValue but {field.name!r} is set.')

Validates a Message which has the PrimitiveWithoutValue extension.

    Given that there is a PrimitiveWithoutValue extension present, there must be
    at least one other extension. Otherwise, there is truly no value set other
    than id and/or extension (non-value fields).

Args:
        fhir_primitive: The FHIR primitive Message to validate.

Raises:
        fhir_errors.InvalidFhirError: In the event that there is less than one
        extension present, or there are values set other than id and/or extension.


def _parse(json_str: str, primitive_cls: Type[Date], *, default_timezone: str) -> Date:
    try:
        dt = datetime.datetime.strptime(json_str, '%Y')
        return _primitive_time_utils.build_date_like(dt, default_timezone, _primitive_time_utils.DateTimePrecision.YEAR, primitive_cls)
    except ValueError:
        pass
    try:
        dt = datetime.datetime.strptime(json_str, '%Y-%m')
        return _primitive_time_utils.build_date_like(dt, default_timezone, _primitive_time_utils.DateTimePrecision.MONTH, primitive_cls)
    except ValueError:
        pass
    try:
        dt = datetime.datetime.strptime(json_str, '%Y-%m-%d')
        return _primitive_time_utils.build_date_like(dt, default_timezone, _primitive_time_utils.DateTimePrecision.DAY, primitive_cls)
    except ValueError:
        pass
    raise fhir_errors.InvalidFhirError('Invalid Date.')

Parses the json_str into a Date FHIR primitive.

Args:
        json_str: The raw JSON string to parse.
        primitive_cls: The FHIR primitive to parse into.
        default_timezone: The default timezone to use when parsing in the event that
        no timezone information is present.

Returns:
        A FHIR primitive Date.

Raises:
        fhir_errors.InvalidFhirError: In the event that no datetime format was
        able to properly parse the json_str.


def forward(self, hidden_states: torch.Tensor, attention_mask: torch.Tensor, output_attentions: bool=False) -> torch.Tensor:
    residual = hidden_states
    hidden_states = self.self_attn_layer_norm(hidden_states)
    hidden_states, attn_weights, _ = self.self_attn(hidden_states=hidden_states, attention_mask=attention_mask, output_attentions=output_attentions)
    hidden_states = self.attn_dropout(hidden_states)
    hidden_states = residual + hidden_states
    residual = hidden_states
    hidden_states = self.ffn_layer_norm(hidden_states)
    hidden_states = self.ffn(hidden_states)
    hidden_states = self.ffn_dropout(hidden_states)
    hidden_states = residual + hidden_states
    outputs = (hidden_states,)
    if output_attentions:
        outputs += (attn_weights,)
    return outputs

Args:
    hidden_states (`torch.FloatTensor`):
        input to the layer of shape `(batch, seq_len, embed_dim)`
    attention_mask (`torch.FloatTensor`):
        attention mask of size `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very
        large negative values.


def run(self):
    if not self._test_run_infos:
        raise Error('No tests to execute.')
    self._test_run_metadata.set_start_point()
    utils.create_dir(self._test_run_metadata.root_output_path)
    summary_writer = records.TestSummaryWriter(self._test_run_metadata.summary_file_path)

    def sigterm_handler(*args):
        logging.warning('Test received a SIGTERM. Aborting all tests.')
        raise signals.TestAbortAll('Test received a SIGTERM.')
    signal.signal(signal.SIGTERM, sigterm_handler)
    try:
        for test_run_info in self._test_run_infos:
            test_config = test_run_info.config.copy()
            test_config.log_path = self._test_run_metadata.root_output_path
            test_config.summary_writer = summary_writer
            test_config.test_class_name_suffix = test_run_info.test_class_name_suffix
            try:
                self._run_test_class(config=test_config, test_class=test_run_info.test_class, tests=test_run_info.tests)
            except signals.TestAbortAll as e:
                logging.warning('Abort all subsequent test classes. Reason: %s', e)
                raise
    finally:
        summary_writer.dump(self.results.summary_dict(), records.TestSummaryEntryType.SUMMARY)
        self._test_run_metadata.set_end_point()
        summary_lines = [f'Summary for test run {self._test_run_metadata.run_id}:', f'Total time elapsed {self._test_run_metadata.time_elapsed_sec}s', f'Artifacts are saved in "{self._test_run_metadata.root_output_path}"', f'Test summary saved in "{self._test_run_metadata.summary_file_path}"', f'Test results: {self.results.summary_str()}']
        logging.info('\n'.join(summary_lines))

Executes tests.

    This will instantiate controller and test classes, execute tests, and
    print a summary.

    This meethod should usually be called within the runner's `mobly_logger`
    context. If you must use this method outside of the context, you should
    make sure `self._test_run_metadata.generate_test_run_log_path` is called
    before each invocation of `run`.

Raises:
        Error: if no tests have previously been added to this runner using
        add_test_class(...).


def check_target_module_exists(optim_target_modules, key: str, return_is_regex: bool=False):
    target_module_found = False
    is_regex = False
    if isinstance(optim_target_modules, str):
        target_module_found = bool(re.fullmatch(optim_target_modules, key))
        is_regex = True if not optim_target_modules == key else False
    elif key in optim_target_modules:
        target_module_found = True
    elif any((target_key in key for target_key in optim_target_modules)):
        target_module_found = True
    elif any((bool(re.fullmatch(optim_target_module, key)) for optim_target_module in optim_target_modules)):
        target_module_found = True
        is_regex = True
    if return_is_regex:
        return (target_module_found, is_regex)
    return target_module_found

A helper method to check if the passed module's key name matches any of the target modules in the optim_target_modules.

Args:
        optim_target_modules (`Union[str, List[str]]`):
            A list of strings to try to match. Can be also a full string.
        key (`str`):
            A key to search any matches in optim_target_modules
        return_is_regex (`bool`):
            If set to `True`, the method will return whether the passed `optim_target_modules`
            is a regex or not.

Returns:
        `bool` : True of match object if key matches any target modules from config, False or
        None if no match found
        `bool` : If the matched target module is a regex to silence out the warnings in Trainer
        for extra modules being found (only if `target_module_found=True` for an array of regex).


def events_from_file(filepath):
    records = list(tf_record.tf_record_iterator(filepath))
    result = []
    for r in records:
        event = event_pb2.Event()
        event.ParseFromString(r)
        result.append(event)
    return result

Returns all events in a single event file.

Args:
        filepath: Path to the event file.

Returns:
        A list of all tf.Event protos in the event file.


def set_tpu_core_ids(self, mesh_name, tpu_core_ids):
    _pywrap_dtensor_device.SetTPUCoreIDs(self._device_info, mesh_name, tpu_core_ids)

Sets the singleton global device ID-to-physical core ID map.

Args:
        mesh_name: The name of a mesh. If empty, set the default mapping.
        tpu_core_ids: TPU core IDs sorted by TF task/device ordinal.


def random_channel_shift(x, intensity_range, channel_axis=0):
    intensity = np.random.uniform(-intensity_range, intensity_range)
    return apply_channel_shift(x, intensity, channel_axis=channel_axis)

Performs a random channel shift.

    DEPRECATED.

Args:
        x: Input tensor. Must be 3D.
        intensity_range: Transformation intensity.
        channel_axis: Index of axis for channels in the input tensor.

Returns:
        Numpy image tensor.


def __init__(self, build_info_key, system_prop_key):
    self.build_info_key = build_info_key
    self.system_prop_key = system_prop_key

Enums for build info constants used for AndroidDevice build info.

Attributes:
        build_info_key: The key used for the build_info dictionary in AndroidDevice.
        system_prop_key: The key used for getting the build info from system
        properties.


def chunk_embedding_fn(chunk: Chunk) -> str:
    if chunk.embedding is None or chunk.embedding.dense_embedding is None:
        raise ValueError(f'Expected chunk to contain embedding. {chunk}')
    return '{' + ','.join((str(x) for x in chunk.embedding.dense_embedding)) + '}'

Convert embedding to PostgreSQL array string.

    Formats dense embedding as a PostgreSQL-compatible array string.
    Example: [1.0, 2.0] -> '{1.0,2.0}'

Args:
        chunk: Input Chunk object.

Returns:
        str: PostgreSQL array string representation of the embedding.

Raises:
        ValueError: If chunk has no dense embedding.


def conv1d(x, kernel, strides=1, padding='valid', data_format=None, dilation_rate=1):
    if data_format is None:
        data_format = image_data_format()
    if data_format not in {'channels_first', 'channels_last'}:
        raise ValueError('Unknown data_format: ' + str(data_format))
    kernel_shape = kernel.shape.as_list()
    if padding == 'causal':
        left_pad = dilation_rate * (kernel_shape[0] - 1)
        x = temporal_padding(x, (left_pad, 0))
        padding = 'valid'
    padding = _preprocess_padding(padding)
    x, tf_data_format = _preprocess_conv1d_input(x, data_format)
    x = nn.convolution(input=x, filter=kernel, dilation_rate=dilation_rate, strides=strides, padding=padding, data_format=tf_data_format)
    if data_format == 'channels_first' and tf_data_format == 'NWC':
        x = array_ops.transpose(x, (0, 2, 1))
    return x

1D convolution.

Args:
        x: Tensor or variable.
        kernel: kernel tensor.
        strides: stride integer.
        padding: string, `"same"`, `"causal"` or `"valid"`.
        data_format: string, one of "channels_last", "channels_first".
        dilation_rate: integer dilate rate.

Returns:
        A tensor, result of 1D convolution.

Raises:
        ValueError: if `data_format` is neither `channels_last` or
        `channels_first`.


def _verify_spec_compatible(input_name, spec_name, input_, spec):
    assert isinstance(spec, tensor_spec.TensorSpec)
    if input is None:
        raise ValueError('{} cannot be None'.format(input_name))
    if isinstance(input_, (bool, int, float, str, np.ndarray)):
        input_ = tensor_conversion.convert_to_tensor_v2(input_)
    input_dtype = getattr(input_, 'dtype', None)
    if input_dtype != spec.dtype:
        input_dtype_str = 'no dtype' if input_dtype is None else str(input_dtype)
        raise TypeError('{} must have the same dtype as {}. Expected {}, got {}'.format(input_name, spec_name, spec.dtype, input_dtype_str))

Verifies that a symbol has a type compatible vith a given spec.

    Here, compatibility is viewed in the general TensorFlow sense: that the dtypes
    are the same after implicit conversion, if both are tensors.

    This verifier ensures consistent treatment of types across AutoGraph.

Args:
        input_name: A name to use for `input_` in error messages.
        spec_name: A name to use for `spec` in error messages.
        input_: Any, value to verify.
        spec: TypeSpec that `input_` must be compatible with.

Raises:
        ValueError if the two types have been determined not to be compatible.


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

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

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

    The format definition of the pattern is:

    **pattern**: `{ term }`

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

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

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

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

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

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

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

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

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


def tensor_rank(self, name='tensor_rank'):
    with self._name_scope(name):
        return self.shape.ndims

Rank (in the sense of tensors) of matrix corresponding to this operator.

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

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

Returns:
        Python integer, or None if the tensor rank is undefined.


def is_extension_type(tensor):
    return isinstance(tensor, composite_tensor.CompositeTensor)

Returns whether a tensor is of an ExtensionType.

    github.com/tensorflow/community/pull/269
    Currently it works by checking if `tensor` is a `CompositeTensor` instance,
    but this will be changed to use an appropriate extensiontype protocol
    check once ExtensionType is made public.

Args:
        tensor: An object to test

Returns:
        True if the tensor is an extension type object, false if not.


The statically known shape of the RaggedTensor.

Example:
    >>> rt = tf.ragged.constant([[0], [1, 2]])
    >>> tf.type_spec_from_value(rt).shape
    TensorShape([2, None])

    >>> rt = tf.ragged.constant([[[0, 1]], [[1, 2], [3, 4]]], ragged_rank=1)
    >>> tf.type_spec_from_value(rt).shape
    TensorShape([2, None, 2])

Returns:
        A `tf.TensorShape` containing the statically known shape of the
        RaggedTensor. Ragged dimensions have a size of `None`.


def __init__(self, config: CLIPConfig, **kwargs):
    super().__init__(**kwargs)
    self.layers = [TFCLIPEncoderLayer(config, name=f'layers_._{i}') for i in range(config.num_hidden_layers)]

Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
    [`TFCLIPEncoderLayer`].

Args:
        config: CLIPConfig


def row_splits_to_segment_ids(splits, name=None, out_type=None):
    with ops.name_scope(name, 'RaggedSplitsToSegmentIds', [splits]) as name:
        splits = ops.convert_to_tensor(splits, name='splits', preferred_dtype=dtypes.int64)
        if splits.dtype not in (dtypes.int32, dtypes.int64):
            raise ValueError('splits must have dtype int32 or int64')
        splits.shape.assert_has_rank(1)
        if tensor_shape.dimension_value(splits.shape[0]) == 0:
            raise ValueError('Invalid row_splits: []')
        if out_type is None:
            out_type = splits.dtype
        else:
            out_type = dtypes.as_dtype(out_type)
        row_lengths = splits[1:] - splits[:-1]
        nrows = array_ops.shape(splits, out_type=out_type)[-1] - 1
        indices = math_ops.range(nrows)
        return ragged_util.repeat(indices, repeats=row_lengths, axis=0)

Generates the segmentation corresponding to a RaggedTensor `row_splits`.

    Returns an integer vector `segment_ids`, where `segment_ids[i] == j` if
    `splits[j] <= i < splits[j+1]`.  Example:

    >>> print(tf.ragged.row_splits_to_segment_ids([0, 3, 3, 5, 6, 9]))
    tf.Tensor([0 0 0 2 2 3 4 4 4], shape=(9,), dtype=int64)

Args:
        splits: A sorted 1-D integer Tensor.  `splits[0]` must be zero.
        name: A name prefix for the returned tensor (optional).
        out_type: The dtype for the return value.  Defaults to `splits.dtype`,
        or `tf.int64` if `splits` does not have a dtype.

Returns:
        A sorted 1-D integer Tensor, with `shape=[splits[-1]]`

Raises:
        ValueError: If `splits` is invalid.


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

Reports the status of dataflow WorkItems leased by a worker.

Args:
        request: (DataflowProjectsLocationsJobsWorkItemsReportStatusRequest) input message
        global_params: (StandardQueryParameters, default: None) global arguments

Returns:
        (ReportWorkItemStatusResponse) The response message.


def _print_primitive_field(self, field_name: str, field: descriptor.FieldDescriptor, value: Any) -> None:
    if proto_utils.field_is_repeated(field):
        string_values = []
        elements = []
        extensions_found = False
        nonnull_values_found = False
        for primitive in value:
            wrapper = self.primitive_handler.primitive_wrapper_from_primitive(primitive)
            string_values.append(wrapper.json_value())
            elements.append(wrapper.get_element())
            nonnull_values_found = nonnull_values_found or wrapper.has_value()
            extensions_found = extensions_found or wrapper.has_element()
        if nonnull_values_found:
            self.generator.add_field(field_name)
            self._print_list(string_values, self.generator.push)
        if extensions_found:
            if nonnull_values_found:
                self.generator.push(',')
                self.generator.add_newline()
            self.generator.add_field(f'_{field_name}')
            self._print_list(elements, self._print)
    elif self.json_format == _FhirJsonFormat.ANALYTIC and field.message_type.name == 'ReferenceId':
        str_value = proto_utils.get_value_at_field(value, 'value')
        self.generator.add_field(field_name, f'"{str_value}"')
    else:
        wrapper = self.primitive_handler.primitive_wrapper_from_primitive(value)
        if wrapper.has_value():
            self.generator.add_field(field_name, wrapper.json_value())
        if wrapper.has_element() and self.json_format == _FhirJsonFormat.PURE:
            if wrapper.has_value():
                self.generator.push(',')
                self.generator.add_newline()
            self.generator.add_field(f'_{field_name}')
            self._print(wrapper.get_element())

Prints the primitive field.

Args:
        field_name: The name of the field.
        field: The FielDescriptor whose contents to print.
        value: The value present at field to print.


def print_type_of_instance(self, t: types.BaseValue, instance=None) -> str:

Returns a string of the type of an instance of t.

    For example, if t is `int`, then this method returns "int".

Args:
        t: An abstract value.
        instance: A specific instance of t to print.


def __init__(self, base: ModelHandler[ExampleT, PredictionT, ModelT]):
    self._base = base
    self._env_vars = getattr(base, '_env_vars', {})

A ModelHandler that skips batching in RunInference.

Args:
        base: An implementation of the underlying model handler.


def GetSubClasses():
    return utils.invert_dict(GetSuperClasses())

Get a reverse Python type hierarchy mapping.

    This generates a dictionary that can be used to look up the (known)
    subclasses of a type in the abstract base class hierarchy.

Returns:
        A dictionary mapping a type, as string, to a list of direct
        subclasses (also as strings).
        E.g. "Sized" -> ["Set", "Mapping", "MappingView", "Sequence"].


def recipe_includes(recipe, path=None):
    expanded_tasks = []
    for task in recipe['tasks']:
        function, parameters = next(iter(task.items()))
        if function == 'include':
            if path:
                tasks = get_recipe(path + '/' + parameters['script'])['tasks']
                json_merge_fields(tasks, parameters['parameters'])
                expanded_tasks.extend(tasks)
            else:
                raise FileNotFoundError('Script contains INCLUDE but no include path specified: %s' % path + '/' + parameters['script'])
        else:
            expanded_tasks.append({function: parameters})
    recipe['tasks'] = expanded_tasks
    return recipe

Expands include block and merges fields.

    This ONLY transforms a recipe by merging fields not resolving them.
    All fields remain as fields but parameters in the include are written
    over the fields in the included recipe.

    For example:

        { "include":{
        "script":"scripts/dv360_segmentology.json",
        "parameters":{
        "auth_read":"user",
        "auth_write":"service",
        "recipe_name":{"field":{ "name":"test_run_id", "kind":"string", "description":"The name to use for the test.", "prefix":"StarThinker Test Segmentology DV ", "default": "Manual"}},
        "partners":{"field":{ "name":"partners", "kind":"integer_list", "default":[], "description":"DV360 partner id." }},
        }

    Generated by:

        python starthinker/tool/test.py -i scripts/dv360_segmentology.json

Args:
        - recipe: (dict) A json recipe that may include { "include":... } task.
        - path: (string) The file path where included recipes are to be read from.

Returns:
        Dictionary with tasks from all includes and fields merged.


def process_column(body: ProcessColumnRequest) -> ResponseReturnValue:
    credentials = get_credentials(body.auth_config)
    logger: Logger
    if not body.log_table:
        logger = PrintLogger()
    else:
        logger = BigQueryLogger(body.log_table, body.auth_config)
    logger.set_base_log(__version__, body.workflow_execution_id, body.display_source_table, datetime.utcnow())
    bq_read_client = get_bq_read_client(credentials)
    parser, usable_rules = map_parser_to_rules(body.column_config['parser'])
    rules = generate_selected_rules(body.column_config['rules'], usable_rules)
    column_name = body.column_config['column']
    cells_iterator = get_cells_iterator(bq_read_client, body.source_table, column_name)
    row_counter = 0
    parse_failures = 0
    rule_errors = 0
    check_violations = 0
    for cell in cells_iterator:
        try:
            value = parser(cell)
        except Exception as e:
            logger.parser(column_name, parser.__name__, str(e), cell)
            parse_failures += 1
        else:
            for rule in rules:
                try:
                    result = rule(value)
                except Exception as e:
                    logger.rule(column_name, rule.__name__, str(e), value, rule.__kwdefaults__)
                    rule_errors += 1
                else:
                    if result is not None:
                        logger.rule(column_name, rule.__name__, result, value, rule.__kwdefaults__)
                        check_violations += 1
        row_counter += 1
    logger.flush(force=True)
    if row_counter == 0:
        raise RuntimeError('Source table was empty.')
    message = f'DQM processed {row_counter} rows, with {parse_failures} parse failures, {rule_errors} rule errors, {check_violations} rule check violations.'
    return (DQMResponse(name='', description=message, code=200), 200)

Process a given column from the specified table.

Args:
        * body: ProcessColumnRequest HTTP request body

Returns:
        * DQMResponse for the run with a 200 status code

Raises:
        * MalformedConfigError: if the request body was malformed


def softsign(x):
    return nn.softsign(x)

Softsign of a tensor.

Args:
        x: A tensor or variable.

Returns:
        A tensor.


def process(self, element: tuple[str, prediction_log_pb2.PredictionLog]) -> Iterable[str]:
    filename, predict_log = (element[0], element[1].predict_log)
    output_value = predict_log.response.outputs
    output_tensor = tf.io.decode_raw(output_value['output_0'].tensor_content, out_type=tf.float32)
    max_index_output_tensor = tf.math.argmax(output_tensor, axis=0)
    yield (filename + ',' + str(tf.get_static_value(max_index_output_tensor)))

Args:
    element: Tuple of str, and PredictionLog. Inference can be parsed
        from prediction_log

Returns:
    str of filename and inference.


def concatenate_context_input(context_input, sequence_input):
    seq_rank_check = check_ops.assert_rank(sequence_input, 3, message='sequence_input must have rank 3', data=[array_ops.shape(sequence_input)])
    seq_type_check = check_ops.assert_type(sequence_input, dtypes.float32, message='sequence_input must have dtype float32; got {}.'.format(sequence_input.dtype))
    ctx_rank_check = check_ops.assert_rank(context_input, 2, message='context_input must have rank 2', data=[array_ops.shape(context_input)])
    ctx_type_check = check_ops.assert_type(context_input, dtypes.float32, message='context_input must have dtype float32; got {}.'.format(context_input.dtype))
    with ops.control_dependencies([seq_rank_check, seq_type_check, ctx_rank_check, ctx_type_check]):
        padded_length = array_ops.shape(sequence_input)[1]
        tiled_context_input = array_ops.tile(array_ops.expand_dims(context_input, 1), array_ops.concat([[1], [padded_length], [1]], 0))
    return array_ops.concat([sequence_input, tiled_context_input], 2)

Replicates `context_input` across all timesteps of `sequence_input`.

    Expands dimension 1 of `context_input` then tiles it `sequence_length` times.
    This value is appended to `sequence_input` on dimension 2 and the result is
    returned.

Args:
        context_input: A `Tensor` of dtype `float32` and shape `[batch_size, d1]`.
        sequence_input: A `Tensor` of dtype `float32` and shape `[batch_size,
        padded_length, d0]`.

Returns:
        A `Tensor` of dtype `float32` and shape `[batch_size, padded_length,
        d0 + d1]`.

Raises:
        ValueError: If `sequence_input` does not have rank 3 or `context_input` does
        not have rank 2.


def __init__(self, major, minor, patch, identifier_string, version_type):
    self.major = major
    self.minor = minor
    self.patch = patch
    self.identifier_string = identifier_string
    self.version_type = version_type
    self._update_string()

Constructor.

Args:
        major: major string eg. (1)
        minor: minor string eg. (3)
        patch: patch string eg. (1)
        identifier_string: extension string eg. (-rc0)
        version_type: version parameter ((SNAPSHOT|NIGHTLY)_VERSION)


def _TestCase(self, shape, indices, scatter_op=state_ops.scatter_add):
    super(ScatterAddSubTest, self).setUp()
    with self.cached_session(use_gpu=False):
        p_init = np.random.rand(*shape).astype('f')
        vals_shape = [len(indices)] + shape[1:]
        vals_init = np.random.rand(*vals_shape).astype('f')
        v_i = [float(x) for x in vals_init.ravel()]
        p = variables.Variable(p_init)
        vals = constant_op.constant(v_i, shape=vals_shape, name='vals')
        ind = constant_op.constant(indices, dtype=dtypes.int32)
        p2 = scatter_op(p, ind, vals, name='updated_p')
        self.evaluate(variables.global_variables_initializer())
        result = self.evaluate(p2)
    for i, ind in enumerate(indices):
        if scatter_op == state_ops.scatter_add:
            p_init.reshape(shape[0], -1)[ind, :] += vals_init.reshape(vals_shape[0], -1)[i, :]
        else:
            p_init.reshape(shape[0], -1)[ind, :] -= vals_init.reshape(vals_shape[0], -1)[i, :]
    self.assertTrue(all((p_init == result).ravel()))

Run a random test case with the given shape and indices.

Args:
        shape: Shape of the parameters array.
        indices: One-dimensional array of ints, the indices of the last dimension
            of the parameters to update.
        scatter_op: ScatterAdd or ScatterSub.


def floating_point_ops(self, inputs: dict[str, Union[torch.Tensor, Any]]):
    if hasattr(self.model, 'floating_point_ops'):
        return self.model.floating_point_ops(inputs)
    else:
        return 0

For models that inherit from [`PreTrainedModel`], uses that method to compute the number of floating point
    operations for every backward + forward pass. If using another model, either implement such a method in the
    model or subclass and override this method.

Args:
        inputs (`Dict[str, Union[torch.Tensor, Any]]`):
            The inputs and targets of the model.

Returns:
        `int`: The number of floating-point operations.


def partial_run_setup(self, fetches, feeds=None):

    def _feed_fn(feed):
        for tensor_type, _, _, feed_fn in _REGISTERED_EXPANSIONS:
            if isinstance(feed, tensor_type):
                return feed_fn(feed)
        raise TypeError(f'Feed argument {feed} has invalid type "{type(feed).__name__}"')
    if self._closed:
        raise RuntimeError('Attempted to use a closed Session.')
    if self.graph.version == 0:
        raise RuntimeError('The Session graph is empty. Add operations to the graph before calling run().')
    if feeds is None:
        feeds = []
    feed_list = []
    is_list_feed = isinstance(feeds, (list, tuple))
    if not is_list_feed:
        feeds = [feeds]
    for feed in feeds:
        for subfeed in _feed_fn(feed):
            try:
                subfeed_t = self.graph.as_graph_element(subfeed, allow_tensor=True, allow_operation=False)
                feed_list.append(subfeed_t._as_tf_output())
            except Exception as e:
                e.message = f'Cannot interpret argument `feed` key as Tensor: {e.message}'
                e.args = (e.message,)
                raise e
    fetch_handler = _FetchHandler(self._graph, fetches, {})

    def _setup_fn(session, feed_list, fetch_list, target_list):
        self._extend_graph()
        return tf_session.TF_SessionPRunSetup_wrapper(session, feed_list, fetch_list, target_list)
    final_fetches = [t._as_tf_output() for t in fetch_handler.fetches()]
    final_targets = [op._c_op for op in fetch_handler.targets()]
    return self._do_call(_setup_fn, self._session, feed_list, final_fetches, final_targets)

Sets up a graph with feeds and fetches for partial run.

    NOTE: This function is deprecated and we do not expect adding new
    functionality to it. Please do not have your code depending on this
    function.

    This is EXPERIMENTAL and subject to change.

    Note that contrary to `run`, `feeds` only specifies the graph elements.
    The tensors will be supplied by the subsequent `partial_run` calls.

Args:
        fetches: A single graph element, or a list of graph elements.
        feeds: A single graph element, or a list of graph elements.

Returns:
        A handle for partial run.

Raises:
        RuntimeError: If this `Session` is in an invalid state (e.g. has been
        closed).
        TypeError: If `fetches` or `feed_dict` keys are of an inappropriate type.
        tf.errors.OpError: Or one of its subclasses if a TensorFlow error happens.


def run(argv=None, save_main_session=True):
    known_args, pipeline_args = parse_known_args(argv)
    pipeline_options = PipelineOptions(pipeline_args)
    pipeline_options.view_as(SetupOptions).save_main_session = save_main_session
    engine_handler = KeyedModelHandler(TensorRTEngineHandlerNumPy(min_batch_size=1, max_batch_size=1, engine_path=known_args.engine_path))
    with beam.Pipeline(options=pipeline_options) as p:
        filename_value_pair = p | 'ReadImageNames' >> beam.io.ReadFromText(known_args.input) | 'ReadImageData' >> beam.Map(lambda image_name: read_image(image_file_name=image_name, path_to_dir=known_args.images_dir)) | 'AttachImageSizeToKey' >> beam.Map(attach_im_size_to_key) | 'PreprocessImages' >> beam.MapTuple(lambda file_name, data: (file_name, preprocess_image(data)))
        predictions = filename_value_pair | 'TensorRTRunInference' >> RunInference(engine_handler) | 'ProcessOutput' >> beam.ParDo(PostProcessor())
        _ = predictions | 'WriteOutputToGCS' >> beam.io.WriteToText(known_args.output, shard_name_template='', append_trailing_newlines=True)

Args:
    argv: Command line arguments defined for this example.


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

Create an empty dir to use for tests.

Args:
        test_name: Name of the test.

Returns:
        Absolute path to the test directory.


def Filter(fn, *args, **kwargs):
    if not callable(fn):
        raise TypeError('Filter can be used only with callable objects. Received %r instead.' % fn)
    wrapper = lambda x, *args, **kwargs: [x] if fn(x, *args, **kwargs) else []
    label = 'Filter(%s)' % ptransform.label_from_callable(fn)
    if hasattr(fn, '__name__'):
        wrapper.__name__ = fn.__name__
    fn_type_hints = typehints.decorators.IOTypeHints.from_callable(fn)
    if fn_type_hints is not None:
        fn_type_hints = fn_type_hints.with_output_types()
    type_hints = get_type_hints(fn).with_defaults(fn_type_hints)
    if type_hints.input_types is not None:
        wrapper = with_input_types(*type_hints.input_types[0], **type_hints.input_types[1])(wrapper)
    output_hint = type_hints.simple_output_type(label)
    if output_hint is None and get_type_hints(wrapper).input_types and get_type_hints(wrapper).input_types[0]:
        output_hint = get_type_hints(wrapper).input_types[0][0]
    if output_hint:
        wrapper = with_output_types(typehints.Iterable[_strip_output_annotations(output_hint)])(wrapper)
    wrapper._argspec_fn = fn
    pardo = FlatMap(wrapper, *args, **kwargs)
    pardo.label = label
    return pardo

:func:`Filter` is a :func:`FlatMap` with its callable filtering out
    elements.

    Filter accepts a function that keeps elements that return True, and filters
    out the remaining elements.

Args:
        fn (``Callable[..., bool]``): a callable object. First argument will be an
        element.
        *args: positional arguments passed to the transform callable.
        **kwargs: keyword arguments passed to the transform callable.

Returns:
        ~apache_beam.pvalue.PCollection:
        A :class:`~apache_beam.pvalue.PCollection` containing the
        :func:`Filter` outputs.

Raises:
        TypeError: If the **fn** passed as argument is not a callable.
        Typical error is to pass a :class:`DoFn` instance which is supported only
        for :class:`ParDo`.


def get_metrics_namespace(self) -> str:
    return 'BeamML_TF_Numpy'

Returns:
    A namespace for metrics collected by the RunInference transform.


def run_trials(runs):
    inside_runs = 0
    for _ in range(runs):
        x = random.uniform(0, 1)
        y = random.uniform(0, 1)
        inside_runs += 1 if x * x + y * y <= 1.0 else 0
    return (runs, inside_runs, 0)

Run trials and return a 3-tuple representing the results.

Args:
        runs: Number of trial runs to be executed.

Returns:
        A 3-tuple (total trials, inside trials, 0).

    The final zero is needed solely to make sure that the combine_results function
    has same type for inputs and outputs (a requirement for combiner functions).


def __init__(self, pya: pyaudio.PyAudio, substream_name: str='realtime', audio_format: AudioFormats=pyaudio.paInt16, channels: int=1, rate: int=24000):
    self._pya = pya
    self._format = audio_format
    self._channels = channels
    self._rate = rate
    self._substream_name = substream_name

Initializes the audio input processor.

Args:
        pya: The pyaudio object to use for capturing audio.
        substream_name: The name of the substream that will contain all the audio
        parts captured from the mic.
        audio_format: The audio format to use for the audio.
        channels: The number of channels in the audio.
        rate: The sample rate of the audio.


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

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

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

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

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

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


def lists_to_tuples(structure):
    return tree_impl.lists_to_tuples(structure)

Returns the structure with list instances changed to tuples.

Args:
        structure: Arbitrarily nested structure.

Returns:
        The same structure but with tuples instead of lists.


def _GenerateTestData(self, num_classes, dim, batch_size, num_true, labels, sampled, subtract_log_q):
    weights = np.random.randn(num_classes, dim).astype(np.float32)
    biases = np.random.randn(num_classes).astype(np.float32)
    hidden_acts = np.random.randn(batch_size, dim).astype(np.float32)
    true_exp = np.full([batch_size, 1], fill_value=0.5, dtype=np.float32)
    sampled_exp = np.full([len(sampled)], fill_value=0.5, dtype=np.float32)
    sampled_vals = (sampled, true_exp, sampled_exp)
    sampled_w, sampled_b = (weights[sampled], biases[sampled])
    true_w, true_b = (weights[labels], biases[labels])
    true_logits = np.sum(hidden_acts.reshape((batch_size, 1, dim)) * true_w.reshape((batch_size, num_true, dim)), axis=2)
    true_b = true_b.reshape((batch_size, num_true))
    true_logits += true_b
    sampled_logits = np.dot(hidden_acts, sampled_w.T) + sampled_b
    if subtract_log_q:
        true_logits -= np.log(true_exp)
        sampled_logits -= np.log(sampled_exp[np.newaxis, :])
    exp_logits = np.concatenate([true_logits, sampled_logits], axis=1)
    exp_labels = np.hstack((np.ones_like(true_logits) / num_true, np.zeros_like(sampled_logits)))
    return (weights, biases, hidden_acts, sampled_vals, exp_logits, exp_labels)

Randomly generates input/output data for a single test case.

    This function returns numpy constants for use in a test case.

Args:
        num_classes: An int. The number of embedding classes in the test case.
        dim: An int. The dimension of the embedding.
        batch_size: An int. The batch size.
        num_true: An int. The number of target classes per training example.
        labels: A list of batch_size * num_true ints. The target classes.
        sampled: A list of indices in [0, num_classes).
        subtract_log_q: A bool corresponding to the parameter in
        _compute_sampled_logits().

Returns:
        weights: Embedding weights to use as test input. It is a numpy array
        of shape [num_classes, dim]
        biases: Embedding biases to use as test input. It is a numpy array
        of shape [num_classes].
        hidden_acts: Forward activations of the network to use as test input.
        It is a numpy array of shape [batch_size, dim].
        sampled_vals: A tuple based on `sampled` to use as test input in the
        format returned by a *_candidate_sampler function.
        exp_logits: The output logits expected from _compute_sampled_logits().
        It is a numpy array of shape [batch_size, num_true + len(sampled)].
        exp_labels: The output labels expected from _compute_sampled_logits().
        It is a numpy array of shape [batch_size, num_true + len(sampled)].


def _flat_structure(self):
    return {'output_shapes': self._flat_shapes, 'output_types': self._flat_types}

Helper for setting `output_shapes` and `output_types` attrs of an op.

    Most dataset op constructors expect `output_shapes` and `output_types`
    arguments that represent the flattened structure of an element. This helper
    function generates these attrs as a keyword argument dictionary, allowing
    `Dataset._variant_tensor` implementations to pass `**self._flat_structure`
    to the op constructor.

Returns:
        A dictionary of keyword arguments that can be passed to a dataset op
        constructor.


def _flag_value_as_list(self, wanted_flag_name):
    string_value_list = []
    found, flag_value = self.get_flag_value(wanted_flag_name)
    if found:
        assert flag_value is not None
        string_value_list = flag_value.split(',')
    return string_value_list

Returns the string list of a TensorTracer flag.

Args:
        wanted_flag_name: the name of the flag we are looking for.

Returns:
        The list value of the flag.


def build_results(self, values):
    raise NotImplementedError('build_results must be implemented by subclasses')

Build results that match the original shape of the fetch.

Args:
        values: List of values returned by run(). The values correspond exactly to
        the list tensors or ops returned by unique_fetches().

Returns:
        A struct of the same shape as the original fetch object handled by
        this fetch mapper.  In the returned struct, the original fetches are
        replaced by their fetched values.


def _preprocess_op_time(self, op_time: str) -> None:
    if op_time == 'schedule':
        self._step_stats = self._origin_step_stats
        return
    self._step_stats = copy.deepcopy(self._origin_step_stats)
    stream_all_stats = []
    job_stats = []
    for stats in self._step_stats.dev_stats:
        if '/stream:all' in stats.device:
            stream_all_stats.append(stats)
        elif '/job' in stats.device:
            job_stats.append(stats)
    op_gpu_start = {}
    op_gpu_end = {}
    for stats in stream_all_stats:
        for kernel in stats.node_stats:
            name, _ = self._parse_kernel_label(kernel.timeline_label, kernel.node_name)
            start = kernel.all_start_micros
            end = kernel.all_start_micros + kernel.all_end_rel_micros
            if name in op_gpu_start:
                op_gpu_start[name] = min(op_gpu_start[name], start)
                op_gpu_end[name] = max(op_gpu_end[name], end)
            else:
                op_gpu_start[name] = start
                op_gpu_end[name] = end
    for stats in job_stats:
        for op in stats.node_stats:
            if op.node_name in op_gpu_start:
                end = max(op_gpu_end[op.node_name], op.all_start_micros + op.all_end_rel_micros)
                if op_time == 'gpu':
                    op.all_start_micros = op_gpu_start[op.node_name]
                op.all_end_rel_micros = end - op.all_start_micros

Update the start and end time of ops in step stats.

Args:
        op_time: How the execution time of op is shown in timeline. Possible
        values are "schedule", "gpu" and "all".  "schedule" will show op from
        the time it is scheduled to the end of the scheduling. Notice by the end
        of its scheduling its async kernels may not start yet. It is shown using
        the default value from step_stats. "gpu" will show op with the execution
        time of its kernels on GPU. "all" will show op from the start of its
        scheduling to the end of its last kernel.


def get_attribute(self, node, obj, name, valself=None):
    obj = abstract_utils.unwrap_final(obj)
    special_attribute = obj.get_special_attribute(node, name, valself)
    if special_attribute is not None:
        return (node, special_attribute)
    if isinstance(obj, abstract.Function):
        if name == '__get__':
            return (node, None)
        else:
            return self._get_instance_attribute(node, obj, name, valself)
    elif isinstance(obj, abstract.ParameterizedClass):
        return self.get_attribute(node, obj.base_cls, name, valself)
    elif isinstance(obj, abstract.Class):
        return self._get_class_attribute(node, obj, name, valself)
    elif isinstance(obj, overlay.Overlay):
        return self._get_module_attribute(node, obj.get_module(name), name, valself)
    elif isinstance(obj, abstract.Module):
        return self._get_module_attribute(node, obj, name, valself)
    elif isinstance(obj, abstract.SimpleValue):
        return self._get_instance_attribute(node, obj, name, valself)
    elif isinstance(obj, abstract.Union):
        if name == '__getitem__':
            return (node, self.ctx.new_unsolvable(node))
        nodes = []
        ret = self.ctx.program.NewVariable()
        for o in obj.options:
            node2, attr = self.get_attribute(node, o, name, valself)
            if attr is not None:
                ret.PasteVariable(attr, node2)
                nodes.append(node2)
        if ret.bindings:
            return (self.ctx.join_cfg_nodes(nodes), ret)
        else:
            return (node, None)
    elif isinstance(obj, special_builtins.SuperInstance):
        return self._get_attribute_from_super_instance(node, obj, name, valself)
    elif isinstance(obj, special_builtins.Super):
        return self.get_attribute(node, self.ctx.convert.super_type, name, valself)
    elif isinstance(obj, (abstract.StaticMethod, abstract.ClassMethod)):
        return self.get_attribute(node, obj.method, name, valself)
    elif isinstance(obj, abstract.BoundFunction):
        return self.get_attribute(node, obj.underlying, name, valself)
    elif isinstance(obj, abstract.TypeParameterInstance):
        param_var = obj.instance.get_instance_type_parameter(obj.name)
        if not param_var.bindings:
            param_var = obj.param.instantiate(self.ctx.root_node)
        results = []
        nodes = []
        for b in param_var.bindings:
            if b.data == obj:
                continue
            node2, ret = self.get_attribute(node, b.data, name, valself)
            if ret is None:
                if b.IsVisible(node):
                    return (node, None)
            else:
                results.append(ret)
                nodes.append(node2)
        if nodes:
            node = self.ctx.join_cfg_nodes(nodes)
            return (node, self.ctx.join_variables(node, results))
        else:
            return (node, self.ctx.new_unsolvable(node))
    elif isinstance(obj, abstract.Empty):
        return (node, None)
    elif isinstance(obj, abstract.ParamSpec):
        if name == 'args':
            return (node, abstract.ParamSpecArgs(obj, self.ctx).to_variable(node))
        elif name == 'kwargs':
            return (node, abstract.ParamSpecKwargs(obj, self.ctx).to_variable(node))
        else:
            return (node, None)
    else:
        return (node, None)

Get the named attribute from the given object.

Args:
        node: The current CFG node.
        obj: The object.
        name: The name of the attribute to retrieve.
        valself: A cfg.Binding to a self reference to include in the attribute's
        origins. If obj is an abstract.Class, valself can be a binding to:
        * an instance of obj - obj will be treated strictly as a class.
        * obj itself - obj will be treated as an instance of its metaclass.
        * None - if name == "__getitem__", obj is a type annotation; else, obj
            is strictly a class, but the attribute is left unbound.
        Else, valself is optional and should be a binding to obj when given.

Returns:
        A tuple (CFGNode, cfg.Variable). If this attribute doesn't exist,
        the Variable will be None.


def is_variable_initialized(ref, name=None):
    if ref.dtype._is_ref_dtype:
        return gen_state_ops.is_variable_initialized(ref=ref, name=name)
    return ref.is_initialized(name=name)

Checks whether a tensor has been initialized.

    Outputs boolean scalar indicating whether the tensor has been initialized.

Args:
        ref: A mutable `Tensor`.
        Should be from a `Variable` node. May be uninitialized.
        name: A name for the operation (optional).

Returns:
        A `Tensor` of type `bool`.


def _EmitLineUnformatted(state):
    while state.next_token:
        previous_token = state.next_token.previous_token
        previous_lineno = previous_token.lineno
        if previous_token.is_multiline_string or previous_token.is_string:
            previous_lineno += previous_token.value.count('\n')
        if previous_token.is_continuation:
            newline = False
        else:
            newline = state.next_token.lineno > previous_lineno
        state.AddTokenToState(newline=newline, dry_run=False)

Emit the line without formatting.

    The line contains code that if reformatted would break a non-syntactic
    convention. E.g., i18n comments and function calls are tightly bound by
    convention. Instead, we calculate when / if a newline should occur and honor
    that. But otherwise the code emitted will be the same as the original code.

Args:
        state: (format_decision_state.FormatDecisionState) The format decision
        state.


def freeze_graph(session, outputs):
    return convert_to_constants.convert_variables_to_constants(session, session.graph.as_graph_def(), [x.op.name for x in outputs])

Freeze the current graph.

Args:
        session: Tensorflow sessions containing the graph
        outputs: List of output tensors

Returns:
        The frozen graph_def.


def sequence_ids(self, batch_index: int=0) -> List[Optional[int]]:
    if not self._encodings:
        raise ValueError('sequence_ids() is not available when using non-fast tokenizers (e.g. instance of a `XxxTokenizerFast` class).')
    return self._encodings[batch_index].sequence_ids

Return a list mapping the tokens to the id of their original sentences:

    - `None` for special tokens added around or between sequences,
    - `0` for tokens corresponding to words in the first sequence,
    - `1` for tokens corresponding to words in the second sequence when a pair of sequences was jointly
        encoded.

Args:
    batch_index (`int`, *optional*, defaults to 0): The index to access in the batch.

Returns:
    `List[Optional[int]]`: A list indicating the sequence id corresponding to each token. Special tokens added
    by the tokenizer are mapped to `None` and other tokens are mapped to the index of their corresponding
    sequence.


def sparse_reduce_sum_sparse(sp_input, axis=None, keepdims=None, reduction_axes=None, keep_dims=None):
    keepdims = deprecation.deprecated_argument_lookup('keepdims', keepdims, 'keep_dims', keep_dims)
    axis = deprecation.deprecated_argument_lookup('axis', axis, 'reduction_axes', reduction_axes)
    if keepdims is None:
        keepdims = False
    output_ind, output_val, output_shape = gen_sparse_ops.sparse_reduce_sum_sparse(sp_input.indices, sp_input.values, sp_input.dense_shape, math_ops._ReductionDims(sp_input, axis), keepdims)
    return sparse_tensor.SparseTensor(output_ind, output_val, output_shape)

Computes the sum of elements across dimensions of a SparseTensor.

    This Op takes a SparseTensor and is the sparse counterpart to
    `tf.reduce_sum()`.  In contrast to SparseReduceSum, this Op returns a
    SparseTensor.

    Note: A gradient is not defined for this function, so it can't be used
    in training models that need gradient descent.

    Reduces `sp_input` along the dimensions given in `reduction_axes`.  Unless
    `keepdims` is true, the rank of the tensor is reduced by 1 for each entry in
    `reduction_axes`. If `keepdims` is true, the reduced dimensions are retained
    with length 1.

    If `reduction_axes` has no entries, all dimensions are reduced, and a tensor
    with a single element is returned.  Additionally, the axes can be negative,
    which are interpreted according to the indexing rules in Python.

Args:
        sp_input: The SparseTensor to reduce. Should have numeric type.
        axis: The dimensions to reduce; list or scalar. If `None` (the
        default), reduces all dimensions.
        keepdims: If true, retain reduced dimensions with length 1.
        reduction_axes: Deprecated name of axis.
        keep_dims: Deprecated alias for `keepdims`.

Returns:
        The reduced SparseTensor.


def row_starts(self):
    return self._row_splits[:-1]

Returns the start indices for rows in this row partition.

    These indices specify where the values for each row begin.
    `partition.row_starts()` is equal to `partition.row_splits()[:-1]`.

Returns:
        A 1-D integer Tensor with shape `[self.nrows()]`.
        The returned tensor is nonnegative, and is sorted in ascending order.
        `self.row_starts()[0] == 0`.
        `self.row_starts()[-1] <= self.nvals()`.


def __init__(self, matcher, focal_alpha, losses):
    nn.Module.__init__(self)
    self.matcher = matcher
    self.focal_alpha = focal_alpha
    self.losses = losses

This class computes the losses for `GroundingDinoForObjectDetection`. The process happens in two steps: 1) we
    compute hungarian assignment between ground truth boxes and the outputs of the model 2) we supervise each pair of
    matched ground-truth / prediction (supervise class and box).

Args:
        matcher (`GroundingDinoHungarianMatcher`):
            Module able to compute a matching between targets and proposals.
        focal_alpha (`float`):
            Alpha parameter in focal loss.
        losses (`List[str]`):
            List of all the losses to be applied. See `get_loss` for a list of all available losses.


def kl_divergence(y_true, y_pred):
    y_pred = tensor_conversion.convert_to_tensor_v2_with_dispatch(y_pred)
    y_true = math_ops.cast(y_true, y_pred.dtype)
    y_true = backend.clip(y_true, backend.epsilon(), 1)
    y_pred = backend.clip(y_pred, backend.epsilon(), 1)
    return math_ops.reduce_sum(y_true * math_ops.log(y_true / y_pred), axis=-1)

Computes Kullback-Leibler divergence loss between `y_true` and `y_pred`.

    `loss = y_true * log(y_true / y_pred)`

    See: https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence

    Standalone usage:

    >>> y_true = np.random.randint(0, 2, size=(2, 3)).astype(np.float64)
    >>> y_pred = np.random.random(size=(2, 3))
    >>> loss = tf.keras.losses.kullback_leibler_divergence(y_true, y_pred)
    >>> assert loss.shape == (2,)
    >>> y_true = tf.keras.backend.clip(y_true, 1e-7, 1)
    >>> y_pred = tf.keras.backend.clip(y_pred, 1e-7, 1)
    >>> assert np.array_equal(
    ...     loss.numpy(), np.sum(y_true * np.log(y_true / y_pred), axis=-1))

Args:
        y_true: Tensor of true targets.
        y_pred: Tensor of predicted targets.

Returns:
        A `Tensor` with loss.

Raises:
        TypeError: If `y_true` cannot be cast to the `y_pred.dtype`.


def _shard_counts(layout: layout_lib.Layout, batch_dim: Optional[str]=None) -> List[int]:
    shard_counts = []
    for spec in layout.sharding_specs:
        if spec in (batch_dim, layout_lib.UNSHARDED):
            shard_counts.append(1)
        else:
            shard_counts.append(layout.mesh.dim_size(spec))
    return shard_counts

Computes a list of the number of shards in each dimension of the layout.

    The shard counts are used to slice each dataset element. The batch dimension's
    count is overridden to 1 since we only consider how many shards to make
    locally (within each local replica). Sharding across clients is handled by
    either tf.data.Dataset's shard transformation (in the single-client case) or
    tf.data service's distribute function (in the multi-client case).

Args:
        layout: the layout to compute the shard counts for.
        batch_dim: the name of the batch dimension of the layout, if present.

Returns:
        A list of shard counts, one element per dimension of the layout.


def start(self):
    self._server.start()

Starts this server.

    >>> dispatcher = tf.data.experimental.service.DispatchServer(start=False)
    >>> dispatcher.start()

Raises:
        tf.errors.OpError: Or one of its subclasses if an error occurs while
        starting the server.


def request(self, body: dict) -> dict:
    if 'relativeTimeRange' in body:
        if body['relativeTimeRange'] == 'TODAY':
            body['timeRange'] = {'startDate': self.config.date, 'endDate': self.config.date}
        elif body['relativeTimeRange'] == 'YESTERDAY':
            body['timeRange'] = {'startDate': self.config.date - timedelta(days=1), 'endDate': self.config.date - timedelta(days=1)}
        elif body['relativeTimeRange'] == 'LAST_7_DAYS':
            body['timeRange'] = {'startDate': self.config.date - timedelta(days=6), 'endDate': self.config.date}
        elif body['relativeTimeRange'] == 'LAST_14_DAYS':
            body['timeRange'] = {'startDate': self.config.date - timedelta(days=13), 'endDate': self.config.date}
        elif body['relativeTimeRange'] == 'LAST_30_DAYS':
            body['timeRange'] = {'startDate': self.config.date - timedelta(days=29), 'endDate': self.config.date}
        elif body['relativeTimeRange'] == 'LAST_60_DAYS':
            body['timeRange'] = {'startDate': self.config.date - timedelta(days=59), 'endDate': self.config.date}
        elif body['relativeTimeRange'] == 'LAST_90_DAYS':
            body['timeRange'] = {'startDate': self.config.date - timedelta(days=89), 'endDate': self.config.date}
        elif body['relativeTimeRange'] == 'LAST_365_DAYS':
            body['timeRange'] = {'startDate': self.config.date - timedelta(days=364), 'endDate': self.config.date}
        elif body['relativeTimeRange'] == 'LAST_24_MONTHS':
            body['timeRange'] = {'startDate': self.config.date - timedelta(days=729), 'endDate': self.config.date}
        del body['relativeTimeRange']
    self.reportId = API_SearchAds(self.config, self.auth).reports().request(body=body).execute()['id']
    return self.reportId

Create a new SA360 report and return its JSON definition.

    If report is missing, date it will be added.
    Also added relativeTimeRange pattern from CM/DV reporting. If present, timeRange will be created.

Args:
        body - required, the JSON definition of a report: https://developers.google.com/search-ads/v2/reference/reports

Returns:
        A dictionary defining the newly created report and status.


def build_linear_positions(index_dims, output_range=(-1.0, 1.0)):

    def _linspace(n_xels_per_dim):
        return torch.linspace(start=output_range[0], end=output_range[1], steps=n_xels_per_dim, dtype=torch.float32)
    dim_ranges = [_linspace(n_xels_per_dim) for n_xels_per_dim in index_dims]
    array_index_grid = meshgrid(*dim_ranges, indexing='ij')
    return torch.stack(array_index_grid, dim=-1)

Generate an array of position indices for an N-D input array.

Args:
        index_dims (`List[int]`):
        The shape of the index dimensions of the input array.
        output_range (`Tuple[float]`, *optional*, defaults to `(-1.0, 1.0)`):
        The min and max values taken by each input index dimension.

Returns:
        `torch.FloatTensor` of shape `(index_dims[0], index_dims[1], .., index_dims[-1], N)`.


def __init__(self, name=None, reduction='sum_over_batch_size', dtype=None):
    self.name = name or auto_name(self.__class__.__name__)
    self.reduction = standardize_reduction(reduction)
    self._dtype_policy = dtype_policies.get(dtype or backend.floatx())
    self._dtype = self._dtype_policy.compute_dtype

Loss base class.

    This is the class to subclass in order to create new custom losses.

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

    To be implemented by subclasses:

    * `call()`: Contains the logic for loss calculation using `y_true`,
        `y_pred`.

    Example subclass implementation:

    ```python
    class MeanSquaredError(Loss):
        def call(self, y_true, y_pred):
            return ops.mean(ops.square(y_pred - y_true), axis=-1)
    ```


def recipe_email_cm_to_bigquery(config, auth_read, email, subject, dataset, table, is_incremental_load):
    email(config, {'auth': auth_read, 'read': {'from': 'noreply-cm@google.com', 'to': email, 'subject': subject, 'attachment': '.*'}, 'write': {'bigquery': {'dataset': dataset, 'table': table, 'header': True, 'is_incremental_load': is_incremental_load}}})

Pulls a CM Report from a gMail powered email account into BigQuery.

Args:
        auth_read (authentication) - Credentials used for reading data.
        email (string) - Email address report was sent to.
        subject (string) - Regular expression to match subject. Double escape backslashes.
        dataset (string) - Existing dataset in BigQuery.
        table (string) - Name of table to be written to.
        is_incremental_load (boolean) - Append report data to table based on date column, de-duplicates.


def _is_statically_shaped(element_spec):
    for spec in nest.flatten(element_spec):
        if isinstance(spec, (sparse_tensor.SparseTensorSpec, ragged_tensor.RaggedTensorSpec)):
            if spec.shape.rank > 0 and spec.shape.as_list()[0] is None:
                return False
        else:
            for component in spec._flat_tensor_specs:
                if not component.shape.is_fully_defined():
                    return False
    return True

Test if an iterator output is statically shaped.

    For sparse and ragged tensors this only tests the batch dimension.

Args:
        element_spec: a nest structure of `tf.TypeSpec`. The element spec of the
        dataset of the iterator.

Returns:
        True if the shape is static, false otherwise.


def generate(self, pixel_values: torch.FloatTensor, input_ids: Optional[torch.LongTensor]=None, attention_mask: Optional[torch.LongTensor]=None, interpolate_pos_encoding: bool=False, **generate_kwargs) -> torch.LongTensor:
    batch_size = pixel_values.shape[0]
    vision_outputs = self.vision_model(pixel_values=pixel_values, interpolate_pos_encoding=interpolate_pos_encoding)
    image_embeds = vision_outputs[0]
    image_attention_mask = torch.ones(image_embeds.size()[:-1], dtype=torch.long, device=image_embeds.device)
    if isinstance(input_ids, list):
        input_ids = torch.LongTensor(input_ids)
    elif input_ids is None:
        input_ids = torch.LongTensor([[self.decoder_input_ids, self.config.text_config.eos_token_id]]).repeat(batch_size, 1).to(image_embeds.device)
    input_ids[:, 0] = self.config.text_config.bos_token_id
    attention_mask = attention_mask[:, :-1] if attention_mask is not None else None
    outputs = self.text_decoder.generate(input_ids=input_ids[:, :-1], eos_token_id=self.config.text_config.sep_token_id, pad_token_id=self.config.text_config.pad_token_id, attention_mask=attention_mask, encoder_hidden_states=image_embeds, encoder_attention_mask=image_attention_mask, **generate_kwargs)
    return outputs

Overrides *generate* function to be able to use the model as a conditional generator

    Parameters:
        pixel_values (*torch.FloatTensor* of shape *(batch_size, num_channels, image_height, image_width)*:
            Input image to be processed
        input_ids (*torch.LongTensor* of shape *(batch_size, sequence_length)*, *optional*):
            The sequence used as a prompt for the generation.
        attention_mask (*torch.LongTensor* of shape *(batch_size, sequence_length)*, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

Example:
    ```python
    >>> from PIL import Image
    >>> import requests
    >>> from transformers import AutoProcessor, BlipForConditionalGeneration

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

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

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

    >>> outputs = model.generate(**inputs)
    >>> print(processor.decode(outputs[0], skip_special_tokens=True))
    two cats sleeping on a couch
    ```


def forward(self, input_ids: torch.Tensor, cache_position: torch.Tensor) -> torch.Tensor:
    batch_size = input_ids.shape[0]
    position_ids = cache_position.unsqueeze(0).expand(batch_size, -1)
    outputs = self.model(input_ids=input_ids, attention_mask=None, position_ids=position_ids, past_key_values=self.cache, use_cache=True, cache_position=cache_position)
    return outputs.logits

Forward pass of the module, which is compatible with the ExecuTorch llm runner.

Args:
        input_ids (`torch.Tensor`): Tensor representing current input token id to the module.
        cache_position (`torch.Tensor`): Tensor representing current input position in the cache.

Returns:
        torch.Tensor: Logits output from the model.