def get_sv_variants(self, chromosome=None, end_chromosome=None, sv_type=None, pos=None, end=None):
    query = {}
    if chromosome:
        query['chrom'] = chromosome
    if end_chromosome:
        query['end_chrom'] = end_chromosome
    if sv_type:
        query['sv_type'] = sv_type
    if pos:
        if (not ('$and' in query)):
            query['$and'] = []
        query['$and'].append({'pos_left': {'$lte': pos}})
        query['$and'].append({'pos_right': {'$gte': pos}})
    if end:
        if (not ('$and' in query)):
            query['$and'] = []
        query['$and'].append({'end_left': {'$lte': end}})
        query['$and'].append({'end_right': {'$gte': end}})
    LOG.info('Find all sv variants {}'.format(query))
    return self.db.structural_variant.find(query).sort([('chrom', ASCENDING), ('pos_left', ASCENDING)])

Return all structural variants in the database

Args:
        chromosome (str)
        end_chromosome (str)
        sv_type (str)
        pos (int): Left position of SV
        end (int): Right position of SV

Returns:
        variants (Iterable(Variant))


An exponential tween function that accelerates, reaches the midpoint, and then decelerates.

Args:
        n (float): The time progress, starting at 0.0 and ending at 1.0.

Returns:
        (float) The line progress, starting at 0.0 and ending at 1.0. Suitable for passing to getPointOnLine().


def _CheckIsPipe(self, file_entry):
    if (definitions.FILE_ENTRY_TYPE_PIPE not in self._file_entry_types):
        return False
    return file_entry.IsPipe()

Checks the is_pipe find specification.

Args:
        file_entry (FileEntry): file entry.

Returns:
        bool: True if the file entry matches the find specification, False if not.


muparser atan2 function

    Implements an atan2(y,x) function for older muparser versions (<2.1.0);
    atan2 was added as a built-in function in muparser 2.1.0

Args:
        y (str): y argument of the atan2(y,x) function
        x (str): x argument of the atan2(y,x) function

Returns:
        A muparser string that calculates atan2(y,x)


def load_pkl(filenames):
    if (not isinstance(filenames, (list, tuple))):
        filenames = [filenames]
    times = []
    for name in filenames:
        name = str(name)
        with open(name, 'rb') as file:
            loaded_obj = pickle.load(file)
            if (not isinstance(loaded_obj, Times)):
                raise TypeError('At least one loaded object is not a Times data object.')
            times.append(loaded_obj)
    return (times if (len(times) > 1) else times[0])

Unpickle file contents.

Args:
        filenames (str): Can be one or a list or tuple of filenames to retrieve.

Returns:
        Times: A single object, or from a collection of filenames, a list of Times objects.

Raises:
        TypeError: If any loaded object is not a Times object.


def add_sun_flare(img, flare_center_x, flare_center_y, src_radius, src_color, circles):
    non_rgb_warning(img)
    input_dtype = img.dtype
    needs_float = False
    if (input_dtype == np.float32):
        img = from_float(img, dtype=np.dtype('uint8'))
        needs_float = True
    elif (input_dtype not in (np.uint8, np.float32)):
        raise ValueError('Unexpected dtype {} for RandomSunFlareaugmentation'.format(input_dtype))
    overlay = img.copy()
    output = img.copy()
    for (alpha, (x, y), rad3, (r_color, g_color, b_color)) in circles:
        cv2.circle(overlay, (x, y), rad3, (r_color, g_color, b_color), (- 1))
        cv2.addWeighted(overlay, alpha, output, (1 - alpha), 0, output)
    point = (int(flare_center_x), int(flare_center_y))
    overlay = output.copy()
    num_times = (src_radius // 10)
    alpha = np.linspace(0.0, 1, num=num_times)
    rad = np.linspace(1, src_radius, num=num_times)
    for i in range(num_times):
        cv2.circle(overlay, point, int(rad[i]), src_color, (- 1))
        alp = ((alpha[((num_times - i) - 1)] * alpha[((num_times - i) - 1)]) * alpha[((num_times - i) - 1)])
        cv2.addWeighted(overlay, alp, output, (1 - alp), 0, output)
    image_rgb = output
    if needs_float:
        image_rgb = to_float(image_rgb, max_value=255)
    return image_rgb

Add sun flare.

    From https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library

Args:
        img (np.array):
        flare_center_x (float):
        flare_center_y (float):
        src_radius:
        src_color (int, int, int):
        circles (list):

    Returns:


def dtype_numpy(x):
    return dtypes_module.as_dtype(x.dtype).as_numpy_dtype

Returns the numpy dtype of a Keras tensor or variable.

Args:
        x: Tensor or variable.

Returns:
        numpy.dtype, dtype of `x`.


def _capture_by_ref(self, graph: Any, lam: Callable[[], Any], key: Hashable=None) -> Any:
    if key is not None and key in self._by_ref_internal:
        return self._by_ref_internal[key]
    if key is None:
        key = len(self._by_ref_internal)
        while key in self._by_ref_internal:
            key += 1
    value_nested = lam()
    capture_trace_type = trace_type.from_value(value_nested)
    ctx = trace_type.InternalPlaceholderContext(graph)
    internal = capture_trace_type.placeholder_value(ctx)

    def lam_fn():
        value = lam()
        return capture_trace_type.to_tensors(value)
    self._by_ref_external[key] = lam_fn
    self._by_ref_internal[key] = internal
    self._by_ref_tracetype[key] = capture_trace_type
    return self._by_ref_internal[key]

Used during tracing process to create/retrive by-ref captures.

Args:
        graph: The FuncGraph that captures this tensor.
        lam: A callable that takes no arguments and returns tensor captures.
        key: A hashable identifier.

Returns:
        Tensor from this FuncGraph.


def to_dataframe(self, view: views.View) -> pandas.DataFrame:
    resources = self._fetch_bundle_entries()
    flattened_view = self._flatten_fhir_to_view(view, resources)
    return pandas.json_normalize(flattened_view)

Returns a Pandas dataframe of the results, if Pandas is installed.

Args:
        view: the view that defines the desired format of the flattened FHIR
        output.

Returns:
        pandas.DataFrame: dataframe of the view contents.


def __init__(self, name, func, create_scope_now=False, custom_getter=None, create_graph_function=False):
    if not context.executing_eagerly():
        raise RuntimeError('{} objects can only be used when eager execution is enabled, use tf.Template for graph construction'.format(type(self)))
    super(EagerTemplate, self).__init__(name, func, create_scope_now, None, custom_getter, create_graph_function)
    if self._variable_scope is not None:
        variable_scope_name = self._variable_scope.name
    else:
        variable_scope_name = None
    self._template_store = _EagerTemplateVariableStore(variable_scope_name)
    self._variable_scope_context_manager = None

Creates a template for the given function.

Args:
        name: A name for the scope created by this template. The name will be made
        unique by appending `_N` to the it (see how
        `tf.compat.v1.variable_scope` treats the `default_name` for details).
        func: The function to apply each time.
        create_scope_now: Whether to create the scope at Template construction
        time, rather than first call. Defaults to false. Creating the scope at
        construction time may be more convenient if the template is passed
        through much lower level code, and you want to be sure of the scope name
        without knowing exactly where it will be first called. If set to True,
        the scope will be created in the constructor, and all subsequent times
        in `__call__`, leading to a trailing numeral being added to the names of
        all created Tensors. If set to False, the scope will be created at the
        first call location.
        custom_getter: optional custom getter to pass to `variable_scope()`
        create_graph_function: When True, `func` will be executed as a graph
        function. Enabling this flag allows the caller to reap the performance
        benefits associated with executing graphs, at the cost of sacrificing
        debuggability; however, not all Python functions can be compiled into
        graph functions. See the documentation for `function.defun` for details.

Raises:
        RuntimeError: if eager execution is not enabled.


def write_zip_data(self, temp_parfile, stored_resources):
    logging.debug('Storing Files...')
    with contextlib.closing(zipfile.ZipFile(temp_parfile, 'w', self.compression)) as z:
        items = sorted(stored_resources.items())
        for relative_path, resource in items:
            assert resource.zipinfo.filename == relative_path
            resource.store(z)

Write the second part of a parfile, consisting of ZIP data

Args:
        stored_resources: A dictionary mapping relative path to the
        content to store at that path.


def update_model_parameters(model: nn.Module) -> None:
    model._parameters = {}
    for name, module in model.named_modules():
        if name == '':
            continue
        param_name = name.replace('module.', '')
        if hasattr(module, 'weight') and module.weight is not None:
            model._parameters[f'{param_name}.weight'] = module.weight
        if hasattr(module, 'bias') and module.bias is not None:
            model._parameters[f'{param_name}.bias'] = module.bias

Update model._parameters using named_modules() to ensure all parameters are properly tracked.

Args:
        model (nn.Module): The model to update parameters for


def update_last_triggered_step(self, step):
    raise NotImplementedError

Update the last triggered time and step number.

Args:
        step: The current step.

Returns:
        A pair `(elapsed_time, elapsed_steps)`, where `elapsed_time` is the number
        of seconds between the current trigger and the last one (a float), and
        `elapsed_steps` is the number of steps between the current trigger and
        the last one. Both values will be set to `None` on the first trigger.


def __init__(self, name, freevars, extra_locals):
    self._name = name
    self._freevars = freevars
    self._extra_locals = extra_locals
    self._unbound_factory = None
    self.module = None
    self.source_map = None

Creates a new factory for a Python function.

Args:
        name: The function name.
        freevars: The list of non-global free variables for the function.
        extra_locals: Dict[Text, Any], names and values for custom variables that
        are accessible to the generated code as local variables.


def tuple(tensors, name=None, control_inputs=None):
    if context.executing_eagerly():
        return tensors
    with ops.name_scope(name, 'tuple', tensors) as name:
        tensors = [t if isinstance(t, ops.Operation) or tensor_util.is_tf_type(t) or t is None else ops.convert_to_tensor(t) for t in tensors]
        gating_ops = [t if isinstance(t, ops.Operation) else t.op for t in tensors if t is not None]
        if control_inputs:
            for c in control_inputs:
                if isinstance(c, tensor_lib.Tensor):
                    c = c.op
                elif not isinstance(c, ops.Operation):
                    raise TypeError(f"'control_inputs' must only contain Operation or Tensor. Received: {type(c)}")
                gating_ops.append(c)
        gating_ops = sorted(set(gating_ops), key=lambda op: op._id)
        if not gating_ops:
            raise ValueError(f"'tensors' must have at least one Tensor. Received: {tensors}.")
        gate = group(*gating_ops)
        tpl = []
        for t in tensors:
            if tensor_util.is_tf_type(t):
                tpl.append(with_dependencies([gate], t))
            elif isinstance(t, ops.Operation):
                with ops.control_dependencies([gate]):
                    tpl.append(group(t))
            else:
                tpl.append(None)
        return tpl

Group tensors together.

    This creates a tuple of tensors with the same values as the `tensors`
    argument, except that the value of each tensor is only returned after the
    values of all tensors have been computed.

    `control_inputs` contains additional ops that have to finish before this op
    finishes, but whose outputs are not returned.

    This can be used as a "join" mechanism for parallel computations: all the
    argument tensors can be computed in parallel, but the values of any tensor
    returned by `tuple` are only available after all the parallel computations
    are done.

    See also `tf.group` and
    `tf.control_dependencies`.

Args:
        tensors: A list of `Tensor`s or `IndexedSlices`, some entries can be `None`.
        name: (optional) A name to use as a `name_scope` for the operation.
        control_inputs: List of additional ops to finish before returning.

Returns:
        Same as `tensors`.

Raises:
        ValueError: If `tensors` does not contain any `Tensor` or `IndexedSlices`.
        TypeError: If `control_inputs` is not a list of `Operation` or `Tensor`
        objects.


def run(argv=None, save_main_session=True, test_pipeline=None) -> PipelineResult:
    known_args, pipeline_args = parse_known_args(argv)
    pipeline_options = PipelineOptions(pipeline_args)
    pipeline_options.view_as(SetupOptions).save_main_session = save_main_session
    pipeline = test_pipeline
    if not test_pipeline:
        pipeline = beam.Pipeline(options=pipeline_options)
    tokenizer = AutoTokenizer.from_pretrained(known_args.model_name)
    model_handler = HuggingFaceModelHandlerKeyedTensor(model_uri=known_args.model_name, model_class=known_args.model_class, framework='pt', max_batch_size=1, large_model=known_args.large_model)
    if not known_args.input:
        text = pipeline | 'CreateSentences' >> beam.Create(['The capital of France is Paris .', 'It is raining cats and dogs .', 'Today is Monday and tomorrow is Tuesday .', 'There are 5 coconuts on this palm tree .', 'The strongest person in the world is not famous .', 'The secret ingredient to his wonderful life was gratitude .', 'The biggest animal in the world is the whale .'])
    else:
        text = pipeline | 'ReadSentences' >> beam.io.ReadFromText(known_args.input)
    text_and_tokenized_text_tuple = text | 'FilterEmptyLines' >> beam.ParDo(filter_empty_lines) | 'AddMask' >> beam.Map(add_mask_to_last_word) | 'TokenizeSentence' >> beam.Map(lambda x: tokenize_sentence(x, tokenizer))
    output = text_and_tokenized_text_tuple | 'RunInference' >> RunInference(KeyedModelHandler(model_handler)) | 'ProcessOutput' >> beam.ParDo(PostProcessor(tokenizer=tokenizer))
    _ = output | 'WriteOutput' >> beam.io.WriteToText(known_args.output, shard_name_template='', append_trailing_newlines=True)
    result = pipeline.run()
    result.wait_until_finish()
    return result

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


def match_opcodes(opcode_traces, lineno, op_match_list):
    out = []
    for trace in opcode_traces[lineno]:
        for match_op, match_symbol in op_match_list:
            if trace.op == match_op and match_symbol in [None, trace.symbol]:
                out.append((trace.op, trace.symbol, trace.types))
    return out

Get all opcodes matching op_match_list on a given line.

Args:
        opcode_traces: traces
        lineno: line number to get ops from.
        op_match_list: [(opcode_name, symbol|None), ...]; None matches any symbol.

Returns:
        A list of matching opcodes.


def create_pull_response(responses):
    from google.cloud import pubsub
    from google.protobuf import timestamp_pb2
    res = pubsub.types.PullResponse()
    for response in responses:
        received_message = pubsub.types.ReceivedMessage()
        message = received_message.message
        message.data = response.data
        if response.attributes is not None:
            for k, v in response.attributes.items():
                message.attributes[k] = v
        publish_time = timestamp_pb2.Timestamp()
        if response.publish_time_secs is not None:
            publish_time.seconds = response.publish_time_secs
        if response.publish_time_nanos is not None:
            publish_time.nanos = response.publish_time_nanos
        message.publish_time = publish_time
        if response.ack_id is not None:
            received_message.ack_id = response.ack_id
        res.received_messages.append(received_message)
    return res

Create an instance of ``google.cloud.pubsub.types.ReceivedMessage``.

    Used to simulate the response from pubsub.SubscriberClient().pull().

Args:
        responses: list of ``PullResponseMessage``

Returns:
        An instance of ``google.cloud.pubsub.types.PullResponse`` populated with
        responses.


def from_signature(cls, sig: inspect.Signature, name: str, callable_type: CallableType, module_name: Optional[str]=None, qualname: Optional[str]=None, auto_typing: bool=False, docstr: Union[str, utils.DocStr, None]=None, parent_module: Optional[types.ModuleType]=None) -> 'Signature':
    args = []
    kwonly_args = []
    varargs = None
    varkw = None
    if isinstance(docstr, str):
        docstr = utils.DocStr.parse(docstr)

    def make_arg_spec(param: inspect.Parameter) -> Argument:
        """Makes argument spec from inspect.Parameter."""
        docstr_arg = docstr.parameter(param) if docstr else None
        return Argument.from_parameter(param, description=docstr_arg.description if docstr_arg else None, auto_typing=auto_typing, parent_module=parent_module)
    for param in sig.parameters.values():
        arg_spec = make_arg_spec(param)
        if param.kind == inspect.Parameter.POSITIONAL_ONLY or param.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD:
            args.append(arg_spec)
        elif param.kind == inspect.Parameter.KEYWORD_ONLY:
            kwonly_args.append(arg_spec)
        elif param.kind == inspect.Parameter.VAR_POSITIONAL:
            varargs = arg_spec
        else:
            assert param.kind == inspect.Parameter.VAR_KEYWORD, param.kind
            varkw = arg_spec
    return_value = None
    if sig.return_annotation is not inspect.Parameter.empty:
        return_value = class_schema.ValueSpec.from_annotation(sig.return_annotation, auto_typing=auto_typing, parent_module=parent_module)
    return cls(callable_type=callable_type, name=name, module_name=module_name, qualname=qualname, description=docstr.short_description if docstr else None, args=args, kwonlyargs=kwonly_args, varargs=varargs, varkw=varkw, return_value=return_value)

Returns PyGlove signature from Python signature.

Args:
        sig: Python signature.
        name: Name of the entity (class name or function/method name).
        callable_type: the type of this callable.
        module_name: Module name of the entity.
        qualname: (Optional) qualified name of the entity.
        auto_typing: If True, automatically convert argument annotations
        to PyGlove ValueSpec objects. Otherwise use pg.typing.Any()
        with annotations.
        docstr: (Optional) DocStr for this entity.
        parent_module: (Optional) Parent module from where the signature is
        derived. This is useful to infer classes with forward declarations.

Returns:
        A PyGlove Signature object.


def _batch_prepare_for_model(self, batch_text_or_text_pairs, is_pair: Optional[bool]=None, boxes: Optional[List[List[int]]]=None, word_labels: Optional[List[List[int]]]=None, add_special_tokens: bool=True, padding_strategy: PaddingStrategy=PaddingStrategy.DO_NOT_PAD, truncation_strategy: TruncationStrategy=TruncationStrategy.DO_NOT_TRUNCATE, max_length: Optional[int]=None, stride: int=0, pad_to_multiple_of: Optional[int]=None, padding_side: Optional[str]=None, return_tensors: Optional[str]=None, return_token_type_ids: Optional[bool]=None, return_attention_mask: Optional[bool]=None, return_overflowing_tokens: bool=False, return_special_tokens_mask: bool=False, return_length: bool=False, verbose: bool=True) -> BatchEncoding:
    batch_outputs = {}
    for idx, example in enumerate(zip(batch_text_or_text_pairs, boxes)):
        batch_text_or_text_pair, boxes_example = example
        outputs = self.prepare_for_model(batch_text_or_text_pair[0] if is_pair else batch_text_or_text_pair, batch_text_or_text_pair[1] if is_pair else None, boxes_example, word_labels=word_labels[idx] if word_labels is not None else None, add_special_tokens=add_special_tokens, padding=PaddingStrategy.DO_NOT_PAD.value, truncation=truncation_strategy.value, max_length=max_length, stride=stride, pad_to_multiple_of=None, padding_side=None, return_attention_mask=False, return_token_type_ids=return_token_type_ids, return_overflowing_tokens=return_overflowing_tokens, return_special_tokens_mask=return_special_tokens_mask, return_length=return_length, return_tensors=None, prepend_batch_axis=False, verbose=verbose)
        for key, value in outputs.items():
            if key not in batch_outputs:
                batch_outputs[key] = []
            batch_outputs[key].append(value)
    batch_outputs = self.pad(batch_outputs, padding=padding_strategy.value, max_length=max_length, pad_to_multiple_of=pad_to_multiple_of, padding_side=padding_side, return_attention_mask=return_attention_mask)
    batch_outputs = BatchEncoding(batch_outputs, tensor_type=return_tensors)
    return batch_outputs

Prepares a sequence of input id, or a pair of sequences of inputs ids so that it can be used by the model. It
    adds special tokens, truncates sequences if overflowing while taking into account the special tokens and
    manages a moving window (with user defined stride) for overflowing tokens.

Args:
        batch_ids_pairs: list of tokenized input ids or input ids pairs


def _show_inputs_outputs(saved_model_dir, tag_set, signature_def_key, indent=0):
    meta_graph_def = saved_model_utils.get_meta_graph_def(saved_model_dir, tag_set)
    _show_inputs_outputs_mgd(meta_graph_def, signature_def_key, indent)

Prints input and output TensorInfos.

    Prints the details of input and output TensorInfos for the SignatureDef mapped
    by the given signature_def_key.

Args:
        saved_model_dir: Directory containing the SavedModel to inspect.
        tag_set: Group of tag(s) of the MetaGraphDef, in string format, separated by
        ','. For tag-set contains multiple tags, all tags must be passed in.
        signature_def_key: A SignatureDef key string.
        indent: How far (in increments of 2 spaces) to indent each line of output.


def _num_image_tokens(image_size: Tuple[int, int], patch_size: Tuple[int, int]) -> int:
    height, width = image_size
    patch_height, patch_width = patch_size if isinstance(patch_size, (tuple, list)) else (patch_size, patch_size)
    num_width_tokens = (width - 1) // patch_width + 1
    num_height_tokens = (height - 1) // patch_height + 1
    return (num_height_tokens, num_width_tokens)

Calculate the number of image tokens given the image size and patch size.

Args:
        image_size (`Tuple[int, int]`):
            The size of the image as `(height, width)`.
        patch_size (`Tuple[int, int]`):
            The patch size as `(height, width)`.

Returns:
        `int`: The number of image tokens.


def _spectrum_to_circulant_2d(self, spectrum, shape, dtype):
    spectrum = _to_complex(spectrum)
    spectrum_shape = self._shape_to_spectrum_shape(shape)
    domain_dimension = spectrum_shape[-1]
    if not domain_dimension:
        return array_ops.zeros(shape, dtype)
    block_shape = spectrum_shape[-2:]
    matrix_rows = []
    for n0 in range(block_shape[0]):
        for n1 in range(block_shape[1]):
            x = np.zeros(block_shape)
            x[n0, n1] = 1.0
            fft_x = fft_ops.fft2d(math_ops.cast(x, spectrum.dtype))
            h_convolve_x = fft_ops.ifft2d(spectrum * fft_x)
            h_convolve_x = array_ops.reshape(h_convolve_x, shape[:-1])
            matrix_rows.append(h_convolve_x)
    matrix = array_ops_stack.stack(matrix_rows, axis=-1)
    return math_ops.cast(matrix, dtype)

Creates a block circulant matrix from a spectrum.

    Intentionally done in an explicit yet inefficient way.  This provides a
    cross check to the main code that uses fancy reshapes.

Args:
        spectrum: Float or complex `Tensor`.
        shape:  Python list.  Desired shape of returned matrix.
        dtype:  Type to cast the returned matrix to.

Returns:
        Block circulant (batch) matrix of desired `dtype`.


def Convert(self, input_file, output_file):
    for version, schema, raw_binary, _ in self._schemas:
        try:
            data_candidate = self._Read(input_file, schema, raw_binary)
        except RuntimeError:
            continue
        if 'version' not in data_candidate:
            data_candidate['version'] = 1
        elif data_candidate['version'] == 0:
            data_candidate['version'] = 1
        if data_candidate['version'] == version:
            self._PerformUpgrade(data_candidate)
            self._Write(data_candidate, output_file)
            return
    raise RuntimeError('No schema that the converter understands worked with the data file you provided.')

Perform schema conversion from input_file to output_file.

Args:
        input_file: Filename of TensorFlow Lite data to convert from. Must
        be `.json` or `.bin` extension files for JSON or Binary forms of
        the TensorFlow FlatBuffer schema.
        output_file: Filename to write to. Extension also must be `.json`
        or `.bin`.

Raises:
        RuntimeError: Generated when none of the upgrader supported schemas
        matche the `input_file` data.


def parallel_stack(values, name='parallel_stack'):
    if context.executing_eagerly():
        raise RuntimeError('tf.parallel_stack() is not compatible with eager execution.')
    with ops.name_scope(name):
        value_t = ops.convert_to_tensor(values[0])
        value_shape = ops.convert_to_tensor(value_t).get_shape()
        output_shape = tensor_shape.TensorShape([len(values)])
        output_shape = output_shape.concatenate(value_shape)
        return gen_array_ops.parallel_concat([expand_dims(value, 0) for value in values], shape=output_shape)

Stacks a list of rank-`R` tensors into one rank-`(R+1)` tensor in parallel.

    Requires that the shape of inputs be known at graph construction time.

    Packs the list of tensors in `values` into a tensor with rank one higher than
    each tensor in `values`, by packing them along the first dimension.
    Given a list of length `N` of tensors of shape `(A, B, C)`; the `output`
    tensor will have the shape `(N, A, B, C)`.

    For example:

    ```python
    x = tf.constant([1, 4])
    y = tf.constant([2, 5])
    z = tf.constant([3, 6])
    tf.parallel_stack([x, y, z])  # [[1, 4], [2, 5], [3, 6]]
    ```

    The difference between `stack` and `parallel_stack` is that `stack` requires
    all the inputs be computed before the operation will begin but doesn't require
    that the input shapes be known during graph construction.

    `parallel_stack` will copy pieces of the input into the output as they become
    available, in some situations this can provide a performance benefit.

    Unlike `stack`, `parallel_stack` does NOT support backpropagation.

    This is the opposite of unstack.  The numpy equivalent is

        tf.parallel_stack([x, y, z]) = np.asarray([x, y, z])

    @compatibility(eager)
    parallel_stack is not compatible with eager execution.
    @end_compatibility

Args:
        values: A list of `Tensor` objects with the same shape and type.
        name: A name for this operation (optional).

Returns:
        output: A stacked `Tensor` with the same type as `values`.

Raises:
        RuntimeError: if executed in eager mode.


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

Compute a single vector summary of a sequence hidden states.

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

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

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

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


def get_special_tokens_mask(self, token_ids_0: List[int], token_ids_1: Optional[List[int]]=None, already_has_special_tokens: bool=False) -> List[int]:
    if already_has_special_tokens:
        return super().get_special_tokens_mask(token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True)
    result = [1] + [0] * len(token_ids_0) + [1]
    if token_ids_1 is not None:
        result += [0] * len(token_ids_1) + [1]
    return result

Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
    special tokens using the tokenizer `prepare_for_model` method.

Args:
        token_ids_0 (`List[int]`):
            List of IDs.
        token_ids_1 (`List[int]`, *optional*):
            Optional second list of IDs for sequence pairs.
        already_has_special_tokens (`bool`, *optional*, defaults to `False`):
            Whether or not the token list is already formatted with special tokens for the model.

Returns:
        `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.


def int_shape(x):
    try:
        shape = x.shape
        if not isinstance(shape, tuple):
            shape = tuple(shape.as_list())
        return shape
    except ValueError:
        return None

Returns the shape of tensor or variable as a tuple of int or None entries.

Args:
        x: Tensor or variable.

Returns:
        A tuple of integers (or None entries).

Example:
    >>> input = tf.keras.backend.placeholder(shape=(2, 4, 5))
    >>> tf.keras.backend.int_shape(input)
    (2, 4, 5)
    >>> val = np.array([[1, 2], [3, 4]])
    >>> kvar = tf.keras.backend.variable(value=val)
    >>> tf.keras.backend.int_shape(kvar)
    (2, 2)


def _from_nested_row_partitions(cls, flat_values, nested_row_partitions, name=None, validate=True):
    if not isinstance(validate, bool):
        raise TypeError(f'Argument `validate` must have type bool. Received {validate}.')
    if isinstance(nested_row_partitions, RowPartition):
        raise TypeError(f'Argument `nested_row_partitions` must be a list of RowPartitions. Received {nested_row_partitions}.')
    if isinstance(nested_row_partitions, tensor_lib.Tensor):
        raise TypeError(f'Argument `nested_row_partitions` must be a list of RowPartitions. Received {nested_row_partitions}.')
    with ops.name_scope(name, 'RaggedFromNestedRowPartitions', [flat_values] + list(nested_row_partitions)):
        result = flat_values
        for partition in reversed(nested_row_partitions):
            result = cls._from_row_partition(result, partition, validate=validate)
        return result

Creates a `RaggedTensor` from a nested list of row partitions.

    Equivalent to:

    ```python
    result = flat_values
    for row_partition in reversed(nested_row_partitions):
        result = _from_row_partition(result, row_partition)
    ```

Args:
        flat_values: A potentially ragged tensor.
        nested_row_partitions: A list of row partitions.  The `i`th element is
        used as the row partition for the `i`th ragged dimension.
        name: A name prefix for the RaggedTensor (optional).
        validate: If true, then use assertions to check that the arguments form
        a valid `RaggedTensor`.  Note: these assertions incur a runtime cost,
        since they must be checked for each tensor value.

Returns:
        A `RaggedTensor` (or `flat_values` if `nested_row_lengths` is empty).


def is_torch_support_available(self) -> bool:
    if is_torch_available():
        from transformers.utils import get_torch_version
        return version.parse(get_torch_version()) >= self.torch_onnx_minimum_version
    else:
        return False

The minimum PyTorch version required to export the model.

Returns:
        `bool`: Whether the installed version of PyTorch is compatible with the model.


def generate(self, pixel_values: torch.FloatTensor, qformer_input_ids: Optional[torch.LongTensor]=None, qformer_attention_mask: Optional[torch.LongTensor]=None, input_ids: Optional[torch.LongTensor]=None, attention_mask: Optional[torch.LongTensor]=None, interpolate_pos_encoding: bool=False, **generate_kwargs) -> torch.LongTensor:
    if hasattr(self, 'hf_device_map'):
        self._preprocess_accelerate()
    batch_size = pixel_values.shape[0]
    language_model_inputs, vision_outputs, query_outputs = self.get_image_features(pixel_values, qformer_input_ids=qformer_input_ids, qformer_attention_mask=qformer_attention_mask, interpolate_pos_encoding=interpolate_pos_encoding, return_dict=True)
    language_attention_mask = torch.ones(language_model_inputs.size()[:-1], dtype=torch.long, device=language_model_inputs.device)
    if input_ids is None:
        start_tokens = [self.config.text_config.bos_token_id]
        if getattr(self.config, 'image_token_id', None) is not None:
            start_tokens = [self.config.image_token_id] * self.config.num_query_tokens + start_tokens
        input_ids = torch.tensor([start_tokens], dtype=torch.long, device=pixel_values.device)
        input_ids = input_ids.repeat(batch_size, 1)
    if attention_mask is None:
        attention_mask = torch.ones_like(input_ids)
    inputs_embeds = self.get_input_embeddings()(input_ids)
    if getattr(self.config, 'image_token_id', None) is not None:
        special_image_mask = (input_ids == self.config.image_token_id).unsqueeze(-1).expand_as(inputs_embeds)
        inputs_embeds[special_image_mask] = language_model_inputs.flatten().to(inputs_embeds.device)
    else:
        logger.warning_once('Expanding inputs for image tokens in InstructBLIP should be done in processing. Please follow instruction here (https://gist.github.com/zucchini-nlp/e9f20b054fa322f84ac9311d9ab67042) to update your InstructBLIP model. Using processors without these attributes in the config is deprecated and will throw an error in v4.50.')
        inputs_embeds = torch.cat([language_model_inputs, inputs_embeds.to(language_model_inputs.device)], dim=1)
        attention_mask = torch.cat([language_attention_mask, attention_mask.to(language_attention_mask.device)], dim=1)
        if not self.language_model.config.is_encoder_decoder:
            generate_kwargs['max_length'] = generate_kwargs.get('max_length', 20) + language_model_inputs.shape[1] - 1
            generate_kwargs['min_length'] = generate_kwargs.get('min_length', 0) + language_model_inputs.shape[1]
    inputs = {'inputs_embeds': inputs_embeds, 'attention_mask': attention_mask}
    if not self.language_model.config.is_encoder_decoder:
        inputs['input_ids'] = input_ids
    outputs = self.language_model.generate(**inputs, **generate_kwargs)
    return outputs

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

Args:
        pixel_values (`torch.FloatTensor` of shape (batch_size, num_channels, height, width)):
            Input images to be processed.
        qformer_input_ids (`torch.LongTensor` of shape (batch_size, sequence_length), *optional*):
            The sequence used as a prompt to be fed to the Q-Former module.
        qformer_attention_mask (`torch.LongTensor` of shape (batch_size, sequence_length), *optional*):
            Mask to avoid performing attention on padding token indices.
        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.
        interpolate_pos_encoding (`bool`, *optional*, defaults to `False`):
            Whether to interpolate the positional encoding of the image embeddings.

Returns:
        captions (list): A list of strings of length batch_size * num_captions.


def configure_callbacks(callbacks, model, do_validation=False, batch_size=None, epochs=None, steps_per_epoch=None, samples=None, verbose=1, count_mode='steps', mode=ModeKeys.TRAIN):
    if isinstance(callbacks, CallbackList):
        return callbacks
    if not callbacks:
        callbacks = []
    if mode == ModeKeys.TRAIN:
        model.history = History()
        callbacks = [BaseLogger()] + (callbacks or []) + [model.history]
        if verbose:
            callbacks.append(ProgbarLogger(count_mode))
    callback_list = CallbackList(callbacks)
    callback_model = model._get_callback_model()
    callback_list.set_model(callback_model)
    set_callback_parameters(callback_list, model, do_validation=do_validation, batch_size=batch_size, epochs=epochs, steps_per_epoch=steps_per_epoch, samples=samples, verbose=verbose, mode=mode)
    callback_list.model.stop_training = False
    return callback_list

Configures callbacks for use in various training loops.

Args:
        callbacks: List of Callbacks.
        model: Model being trained.
        do_validation: Whether or not validation loop will be run.
        batch_size: Number of samples per batch.
        epochs: Number of epoch to train.
        steps_per_epoch: Number of batches to run per training epoch.
        samples: Number of training samples.
        verbose: int, 0 or 1. Keras logging verbosity to pass to ProgbarLogger.
        count_mode: One of 'steps' or 'samples'. Per-batch or per-sample count.
        mode: String. One of ModeKeys.TRAIN, ModeKeys.TEST, or ModeKeys.PREDICT.
        Which loop mode to configure callbacks for.

Returns:
        Instance of CallbackList used to control all Callbacks.


def _infer_var_name(var):
    name_to_var_dict = saveable_object_util.op_list_to_dict(var)
    if len(name_to_var_dict) > 1:
        raise TypeError('`var` = %s passed as arg violates the constraints.  name_to_var_dict = %s' % (var, name_to_var_dict))
    return list(name_to_var_dict.keys())[0]

Returns name of the `var`.

Args:
        var: A list. The list can contain either of the following:
        (i) A single `Variable`
        (ii) A single `ResourceVariable`
        (iii) Multiple `Variable` objects which must be slices of the same larger
        variable.
        (iv) A single `PartitionedVariable`

Returns:
        Name of the `var`


def _lookup_tensor_name(self, tensor):
    return self._tensor_aliases.get(tensor.name, tensor.name)

Look up the name of a graph tensor.

    This method maps the name of a debugger-generated Identity or
    DebugIdentityV2 tensor to the name of the original instrumented tensor,
    if `tensor` is such a debugger-created tensor.
    Otherwise, it returns the name of `tensor` as is.

Args:
        tensor: The graph tensor to look up the name for.

Returns:
        Name of the original instrumented tensor as known to the debugger.


def trace_tpu(self, graph, tensor_fetches, op_fetches=None, num_replicas=None, num_replicas_per_host=None, num_hosts=None):
    if isinstance(graph, func_graph.FuncGraph) or isinstance(graph, function._FuncGraph):
        logging.warning('Tensor Tracer is not supported for tracing FuncGraphs. Ignoring tracing.')
        return tensor_fetches
    if graph in TensorTracer._traced_graphs:
        logging.warning('Graph is already rewritten with tensor tracer, ignoring multiple calls.')
        return tensor_fetches
    else:
        TensorTracer._traced_graphs.add(graph)
    self._parameters = tensor_tracer_flags.TTParameters()
    self._tt_config.device_type = _DEVICE_TYPE_TPU
    self._tt_config.num_replicas = num_replicas
    self._tt_config.num_replicas_per_host = num_replicas_per_host
    self._tt_config.num_hosts = num_hosts
    if self._tt_config.num_replicas is not None:
        if self._tt_config.num_replicas_per_host is None:
            self._tt_config.num_replicas_per_host = 8
        if self._tt_config.num_hosts is None:
            self._tt_config.num_hosts = num_replicas // self._tt_config.num_replicas_per_host + (num_replicas % self._tt_config.num_replicas_per_host > 0)
    if self._parameters.graph_dump_path:
        graph_io.write_graph(graph, self._parameters.graph_dump_path, 'graph_before_tt.pbtxt')
    with graph.as_default():
        self._add_replica_id_to_graph()
        tensor_fetches = self._trace_execution(graph, tensor_fetches, op_fetches, on_tpu=True)
    if self._parameters.graph_dump_path:
        graph_io.write_graph(graph, self._parameters.graph_dump_path, 'graph_after_tt.pbtxt')
    return tensor_fetches

Traces the tensors generated by TPU Ops in a TF graph.

Args:
        graph: the graph of Ops executed on the TPU.
        tensor_fetches: a (list,tuple,or a single object) of tensor fetches
        returned by model_fn given to session.run. Function must be provided
        with as least one tensor to fetch.
        op_fetches: A list of op fetches returned by model_fn given to
        session.run. op_fetches and tensor_fetches are used to determine the
        nodes that will be executed. Can be None.
        num_replicas: number of replicas used on the TPU.
        num_replicas_per_host: number of replicas per TPU host.
        num_hosts: total number of TPU hosts.

Returns:
        tensor_fetches: an exact copy of tensor_fetches that has additional
                    dependencies.


def __init__(self, dataset_file_map: Mapping[str, _RepresentativeDatasetFile]) -> None:
    self.dataset_file_map = dataset_file_map

Initializes TFRecord represenatative dataset loader.

Args:
        dataset_file_map: Signature key -> `RepresentativeDatasetFile` mapping.

Raises:
        DecodeError: If the sample is not RepresentativeDataSample.


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

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

    Formula:

    ```python
    loss = maximum(neg - pos + 1, 0)
    ```

    where `neg=maximum((1-y_true)*y_pred)` and `pos=sum(y_true*y_pred)`

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


def make_ordered_column_names(include_label=True):
    result = ['clicked'] if include_label else []
    for name in _INTEGER_COLUMN_NAMES:
        result.append(name)
    for name in _CATEGORICAL_COLUMN_NAMES:
        result.append(name)
    return result

Returns the column names in the dataset in the order as they appear.

Args:
        include_label: Indicates whether the label feature should be included.

Returns:
        A list of column names in the dataset.


def _post_process(self, feed_item, new_item):
    for third_party_url in feed_item.get('third_party_urls', []):
        third_party_url[FieldMap.CREATIVE_ID] = new_item['id']
        third_party_url[FieldMap.CREATIVE_NAME] = new_item['name']
    for association in feed_item.get('associations', []):
        association[FieldMap.CREATIVE_ID] = self.get(association)['id']
        association[FieldMap.CREATIVE_NAME] = self.get(association)['name']
        dcm_association = self.creative_asset_dao.get(association, required=True)
        if dcm_association:
            association[FieldMap.CREATIVE_ASSET_ID] = dcm_association.get('id', None)
            association[FieldMap.CREATIVE_ASSET_NAME] = dcm_association.get('name', None)
            backup_lp = self.landing_page_dao.get(feed_item, column_name=FieldMap.BACKUP_IMAGE_CLICK_THROUGH_LANDING_PAGE_ID)
            if backup_lp:
                association[FieldMap.BACKUP_IMAGE_CLICK_THROUGH_LANDING_PAGE_ID] = backup_lp['id']
                association[FieldMap.BACKUP_IMAGE_CLICK_THROUGH_LANDING_PAGE_NAME] = backup_lp['name']
            backup_asset = self.creative_asset_dao.get(association, column_name=FieldMap.CREATIVE_BACKUP_ASSET_ID)
            if backup_asset:
                association[FieldMap.CREATIVE_BACKUP_ASSET_ID] = backup_asset['id']
    for click_tag in feed_item.get('click_tags', []):
        click_tag[FieldMap.CREATIVE_ID] = new_item['id']
        click_tag[FieldMap.CREATIVE_NAME] = new_item['name']
        click_tag_lp = self.landing_page_dao.get(click_tag, column_name=FieldMap.CLICK_TAG_LANDING_PAGE_ID)
        if click_tag_lp:
            click_tag[FieldMap.CLICK_TAG_LANDING_PAGE_ID] = click_tag_lp['id']
            click_tag[FieldMap.CLICK_TAG_LANDING_PAGE_NAME] = click_tag_lp['name']
    backup_asset = self.creative_asset_dao.get(feed_item, column_name=FieldMap.CREATIVE_BACKUP_ASSET_ID)
    if backup_asset:
        feed_item[FieldMap.CREATIVE_BACKUP_ASSET_ID] = backup_asset['id']
    backup_lp = self.landing_page_dao.get(feed_item, column_name=FieldMap.BACKUP_IMAGE_CLICK_THROUGH_LANDING_PAGE_ID)
    if backup_lp:
        feed_item[FieldMap.BACKUP_IMAGE_CLICK_THROUGH_LANDING_PAGE_ID] = backup_lp['id']
        feed_item[FieldMap.BACKUP_IMAGE_CLICK_THROUGH_LANDING_PAGE_NAME] = backup_lp['name']

Maps ids and names of related entities so they can be updated in the Bulkdozer feed.

    When Bulkdozer is done processing an item, it writes back the updated names
    and ids of related objects, this method makes sure those are updated in the
    creative feed.

Args:
        feed_item: Feed item representing the creative from the Bulkdozer feed.
        item: The DCM creative being updated or created.


def str_of_constant(self, printer: 'Callable[[_base.BaseValue], str]') -> str:
    del printer
    return repr(self.pyval)

Get a string representation of this constant.

Args:
        printer: A BaseValue -> str function that will be used to print abstract
        values.

Returns:
        A string of self.pyval.


def sample(self, num_samples: int):
    return self._sample(num_samples)

Returns samples from the EBM corresponding to `self.energy`.

Args:
        num_samples: Number of samples to draw from the EBM.


def __init__(self, caption, content, enabled=True):
    self._caption = caption
    self._content = content
    self._enabled = enabled

Menu constructor.

    TODO(cais): Nested menu is currently not supported. Support it.

Args:
        caption: (str) caption of the menu item.
        content: Content of the menu item. For a menu item that triggers
        a command, for example, content is the command string.
        enabled: (bool) whether this menu item is enabled.


def from_dict(cls, config_dict: dict[str, Any], **kwargs) -> 'PretrainedConfig':
    return_unused_kwargs = kwargs.pop('return_unused_kwargs', False)
    kwargs.pop('_from_auto', None)
    kwargs.pop('_from_pipeline', None)
    if '_commit_hash' in kwargs and '_commit_hash' in config_dict:
        kwargs['_commit_hash'] = config_dict['_commit_hash']
    config_dict['attn_implementation'] = kwargs.pop('attn_implementation', None)
    config = cls(**config_dict)
    if hasattr(config, 'pruned_heads'):
        config.pruned_heads = {int(key): value for key, value in config.pruned_heads.items()}
    if 'num_labels' in kwargs and 'id2label' in kwargs:
        num_labels = kwargs['num_labels']
        id2label = kwargs['id2label'] if kwargs['id2label'] is not None else []
        if len(id2label) != num_labels:
            raise ValueError(f'You passed along `num_labels={num_labels}` with an incompatible id to label map: {kwargs['id2label']}. Since those arguments are inconsistent with each other, you should remove one of them.')
    to_remove = []
    for key, value in kwargs.items():
        if hasattr(config, key):
            current_attr = getattr(config, key)
            if isinstance(current_attr, PretrainedConfig) and isinstance(value, dict):
                value = current_attr.__class__(**value)
            setattr(config, key, value)
            if key != 'torch_dtype':
                to_remove.append(key)
    for key in to_remove:
        kwargs.pop(key, None)
    logger.info(f'Model config {config}')
    if return_unused_kwargs:
        return (config, kwargs)
    else:
        return config

Instantiates a [`PretrainedConfig`] from a Python dictionary of parameters.

Args:
        config_dict (`Dict[str, Any]`):
            Dictionary that will be used to instantiate the configuration object. Such a dictionary can be
            retrieved from a pretrained checkpoint by leveraging the [`~PretrainedConfig.get_config_dict`] method.
        kwargs (`Dict[str, Any]`):
            Additional parameters from which to initialize the configuration object.

Returns:
        [`PretrainedConfig`]: The configuration object instantiated from those parameters.


def get_task_states(self, job_configs):
    if self._context_handle:
        job_names, task_nums = zip(*job_configs)
        return pywrap_tfe.TFE_GetTaskStates(self._context_handle, job_names, task_nums)
    else:
        raise ValueError('Context is not initialized.')

Get task states from the Coordination Service.

Args:
        job_configs: A list of tuples of job name and task number.

Returns:
        A list of TF_Status.


def device_name(self):
    return self._device_name

Name of the device that the tensor belongs to.

Returns:
        (`str`) device name.


def sandbox_call(func: Callable[..., Any], *args, timeout: Optional[float]=None, **kwargs) -> Any:

    def _call(q, *args, **kwargs):

        def _run():
            r = func(*args, **kwargs)
            try:
                return pickle.dumps(r)
            except BaseException as e:
                raise errors.SerializationError(f'Cannot serialize sandbox result: {r}', e) from e
        try:
            q.put(_run())
        except Exception as e:
            q.put(e)
    q = multiprocessing.Queue()
    p = multiprocessing.Process(target=_call, args=tuple([q] + list(args)), kwargs=kwargs)
    try:
        p.start()
        x = q.get(timeout=timeout)
    except queue.Empty as e:
        if p.is_alive():
            p.kill()
        raise TimeoutError(f'Execution time exceed {timeout} seconds.') from e
    finally:
        q.close()
    if isinstance(x, Exception):
        raise x
    try:
        return pickle.loads(x)
    except Exception as e:
        raise errors.SerializationError('Cannot deserialize the output from sandbox.', e) from e

Calls a function with sandboxing.

Args:
        func: Function to call.
        *args: Positional arguments for `func`
        timeout: Execution timeout in seconds. If None, wait `func` to complete.
        **kwargs: Keyword arguments for `func`.

Returns:
        Return value from `func`.

Raises:
        TimeoutError: If the execution time exceeds the timeout.
        Exception: Exception raised from `func`.


def forward(self, inputs_embeds, attention_mask: Optional[torch.Tensor]=None, position_embeddings: Optional[Tuple[torch.Tensor, torch.Tensor]]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, return_dict: Optional[bool]=None, **kwargs: Unpack[FlashAttentionKwargs]) -> Union[Tuple, BaseModelOutput]:
    output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
    output_hidden_states = output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    encoder_states = () if output_hidden_states else None
    all_attentions = () if output_attentions else None
    hidden_states = inputs_embeds
    for encoder_layer in self.layers:
        if output_hidden_states:
            encoder_states = encoder_states + (hidden_states,)
        if self.gradient_checkpointing and self.training:
            layer_outputs = self._gradient_checkpointing_func(encoder_layer.__call__, hidden_states, attention_mask, position_embeddings, output_attentions)
        else:
            layer_outputs = encoder_layer(hidden_states, attention_mask, position_embeddings=position_embeddings, output_attentions=output_attentions, **kwargs)
        hidden_states = layer_outputs[0]
        if output_attentions:
            all_attentions = all_attentions + (layer_outputs[1],)
    if output_hidden_states:
        encoder_states = encoder_states + (hidden_states,)
    if not return_dict:
        return tuple((v for v in [hidden_states, encoder_states, all_attentions] if v is not None))
    return BaseModelOutput(last_hidden_state=hidden_states, hidden_states=encoder_states, attentions=all_attentions)

Args:
    inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
        Embeddings which serve as input to the Transformer.
    attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
        Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

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

        [What are attention masks?](../glossary#attention-mask)
    output_attentions (`bool`, *optional*):
        Whether or not to return the attentions tensors of all attention layers. See `attentions` under
        returned tensors for more detail.
    output_hidden_states (`bool`, *optional*):
        Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
        for more detail.
    return_dict (`bool`, *optional*):
        Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.


def variable_op_v2(shape, dtype, name='Variable', container='', shared_name=''):
    return gen_state_ops.variable_v2(shape=shape, dtype=dtype, name=name, container=container, shared_name=shared_name)

Create a variable Operation.

    See also variables.Variable.

Args:
        shape: The shape of the tensor managed by this variable
        dtype: The underlying type of the tensor values.
        name: optional name to use for the variable op.
        container: An optional string. Defaults to "".
        If non-empty, this variable is placed in the given container.
        Otherwise, a default container is used.
        shared_name: An optional string. Defaults to "".
        If non-empty, this variable is named in the given bucket
        with this shared_name. Otherwise, the node name is used instead.

Returns:
        A variable tensor.


def get_change_point_config(params: Dict[str, Any]) -> ChangePointConfig:
    return ChangePointConfig(min_runs_between_change_points=params.get('min_runs_between_change_points', constants._DEFAULT_MIN_RUNS_BETWEEN_CHANGE_POINTS), num_runs_in_change_point_window=params.get('num_runs_in_change_point_window', constants._DEFAULT_NUM_RUMS_IN_CHANGE_POINT_WINDOW))

Args:
    params: Dict containing parameters to run change point analysis.

Returns:
    ChangePointConfig object containing change point analysis parameters.


def most_specific_common_supertype(self, others: Sequence[trace.TraceType]) -> Optional['TensorShape']:
    if any((not isinstance(other, TensorShape) for other in others)):
        return None
    if self.rank is None:
        return unknown_shape()
    if any((other.dims is None or self.rank != other.rank for other in others)):
        return unknown_shape()
    dims = [dim if all((dim == other._dims[i] for other in others)) else None for i, dim in enumerate(self._dims)]
    return TensorShape(dims)

Returns the most specific supertype `TensorShape` of self and others.

    * `TensorShape([None, 1])` is the most specific `TensorShape` supertyping
        both `TensorShape([2, 1])` and `TensorShape([5, 1])`. Note that
        `TensorShape(None)` is also a supertype but it is not "most specific".

    * `TensorShape([1, 2, 3])` is the most specific `TensorShape` supertyping
        both `TensorShape([1, 2, 3])` and `TensorShape([1, 2, 3]`). There are
        other less specific TensorShapes that supertype above mentioned
        TensorShapes, e.g. `TensorShape([1, 2, None])`, `TensorShape(None)`.

    * `TensorShape([None, None])` is the most specific `TensorShape`
        supertyping both `TensorShape([2, None])` and `TensorShape([None, 3])`.
        As always, `TensorShape(None)` is also a supertype but not the most
        specific one.

    * `TensorShape(None`) is the only `TensorShape` supertyping both
        `TensorShape([1, 2, 3])` and `TensorShape([1, 2])`. In general, any two
        shapes that have different ranks will only have `TensorShape(None)`
        as a common supertype.

    * `TensorShape(None)` is the only `TensorShape` supertyping both
        `TensorShape([1, 2, 3])` and `TensorShape(None)`. In general, the common
        supertype of any shape with `TensorShape(None)` is `TensorShape(None)`.

Args:
        others: Sequence of `TensorShape`.

Returns:
        A `TensorShape` which is the most specific supertype shape of `self`
        and `others`. None if it does not exist.


def ConvNeXtBlock(projection_dim, drop_path_rate=0.0, layer_scale_init_value=1e-06, name=None):
    if name is None:
        name = 'prestem' + str(backend.get_uid('prestem'))

    def apply(inputs):
        x = inputs
        x = layers.Conv2D(filters=projection_dim, kernel_size=7, padding='same', groups=projection_dim, name=name + '_depthwise_conv')(x)
        x = layers.LayerNormalization(epsilon=1e-06, name=name + '_layernorm')(x)
        x = layers.Dense(4 * projection_dim, name=name + '_pointwise_conv_1')(x)
        x = layers.Activation('gelu', name=name + '_gelu')(x)
        x = layers.Dense(projection_dim, name=name + '_pointwise_conv_2')(x)
        if layer_scale_init_value is not None:
            x = LayerScale(layer_scale_init_value, projection_dim, name=name + '_layer_scale')(x)
        if drop_path_rate:
            layer = StochasticDepth(drop_path_rate, name=name + '_stochastic_depth')
        else:
            layer = layers.Activation('linear', name=name + '_identity')
        return inputs + layer(x)
    return apply

ConvNeXt block.

    References:
    - https://arxiv.org/abs/2201.03545
    - https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py

Note:
        In the original ConvNeXt implementation (linked above), the authors use
        `Dense` layers for pointwise convolutions for increased efficiency.
        Following that, this implementation also uses the same.

Args:
        projection_dim (int): Number of filters for convolution layers. In the
            ConvNeXt paper, this is referred to as projection dimension.
        drop_path_rate (float): Probability of dropping paths. Should be within
            [0, 1].
        layer_scale_init_value (float): Layer scale value.
            Should be a small float number.
        name: name to path to the keras layer.

Returns:
        A function representing a ConvNeXtBlock block.


def value_container(val):
    container = None
    if not isinstance(val, values_lib.DistributedVariable):
        if hasattr(val, '_distributed_container'):
            container = val._distributed_container()
        elif isinstance(val, composite_tensor.CompositeTensor) and hasattr(val, 'handle') and hasattr(val.handle, '_distributed_container'):
            container = val.handle._distributed_container()
    return container if container is not None else val

Returns the container that this per-replica `value` belongs to.

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

Returns:
        A container that `value` belongs to.
        If value does not belong to any container (including the case of
        container having been destroyed), returns the value itself.


def __init__(self, pool_size, strides, padding='valid', data_format='channels_last', name=None, **kwargs):
    if strides is None:
        raise ValueError('Argument `strides` must not be None.')
    super(MaxPooling2D, self).__init__(pool_size=pool_size, strides=strides, padding=padding, data_format=data_format, name=name, **kwargs)

Max pooling layer for 2D inputs (e.g. images).

Args:
        pool_size: An integer or tuple/list of 2 integers: (pool_height, pool_width)
        specifying the size of the pooling window.
        Can be a single integer to specify the same value for
        all spatial dimensions.
        strides: An integer or tuple/list of 2 integers,
        specifying the strides of the pooling operation.
        Can be a single integer to specify the same value for
        all spatial dimensions.
        padding: A string. The padding method, either 'valid' or 'same'.
        Case-insensitive.
        data_format: A string. The ordering of the dimensions in the inputs.
        `channels_last` (default) and `channels_first` are supported.
        `channels_last` corresponds to inputs with shape
        `(batch, height, width, channels)` while `channels_first` corresponds to
        inputs with shape `(batch, channels, height, width)`.
        name: A string, the name of the layer.


def upsample_filters(filters, rate):
    num_spatial_dims = len(rate)
    spatial_shape = np.array(filters.shape[:num_spatial_dims])
    output_spatial_shape = (spatial_shape - 1) * rate + 1
    output = np.zeros(tuple(output_spatial_shape) + tuple(filters.shape[-2:]), filters.dtype)
    output[tuple((np.s_[::rate[i]] for i in range(num_spatial_dims)))] = filters
    return output

Upsamples the filters by a factor of rate along the spatial dimensions.

Args:
        filters: spatial_shape + [in_channels, out_channels]
        Original filters.
        rate: A list of len(spatial_shape) positive ints, specifying the
        upsampling rate.

Returns:
        filters_up: output_spatial_shape + [in_channels, out_channels].
        Upsampled filters with
        output_spatial_shape[i] = (spatial_shape[i] - 1) * rate[i] + 1
        containing (rate[i] - 1) zeros between consecutive filter values along
        spatial dimension i.


def __init__(self, features, location_id=None, metadata=None, timeout=120, context_side_input=None):
    super().__init__()
    self.features = features
    self.location_id = location_id
    self.metadata = metadata
    self.timeout = timeout
    self.context_side_input = context_side_input

Args:
    features: (List[``videointelligence_v1.Feature``]) Required.
        The Video Intelligence API features to detect
    location_id: (str) Optional.
        Cloud region where annotation should take place.
        If no region is specified, a region will be determined
        based on video file location.
    metadata: (Sequence[Tuple[str, str]]) Optional.
        Additional metadata that is provided to the method.
    timeout: (int) Optional.
        The time in seconds to wait for the response from the
        Video Intelligence API
    context_side_input: (beam.pvalue.AsDict) Optional.
        An ``AsDict`` of a PCollection to be passed to the
        _VideoAnnotateFn as the video context mapping containing additional
        video context and/or feature-specific parameters.
        Example usage::

        video_contexts =
        [('gs://cloud-samples-data/video/cat.mp4', Union[dict,
        ``videointelligence_v1.VideoContext``]),
        ('gs://some-other-video/sample.mp4', Union[dict,
        ``videointelligence_v1.VideoContext``]),]

        context_side_input =
        (
            p
            | "Video contexts" >> beam.Create(video_contexts)
        )

        videointelligenceml.AnnotateVideo(features,
        context_side_input=beam.pvalue.AsDict(context_side_input)))


def _search_for_child_node(self, parent_id, path_to_child):
    if not path_to_child:
        return parent_id
    for child in self._proto.nodes[parent_id].children:
        if child.local_name == path_to_child[0]:
            return self._search_for_child_node(child.node_id, path_to_child[1:])
    return None

Returns node id of child node.

    A helper method for traversing the object graph proto.

    As an example, say that the object graph proto in the SavedModel contains an
    object with the following child and grandchild attributes:

    `parent.child_a.child_b`

    This method can be used to retrieve the node id of `child_b` using the
    parent's node id by calling:

    `_search_for_child_node(parent_id, ['child_a', 'child_b'])`.

Args:
        parent_id: node id of parent node
        path_to_child: list of children names.

Returns:
        node_id of child, or None if child isn't found.


def _get_summary_signatures(self):
    signatures = self._flag_value_as_list(FLAG_NAME_SUMMARY_SIGNATURES)
    supported_signatures = self._supported_signatures()
    tt_signatures = []
    for signature in signatures:
        signature_with_prefix = '%s_%s' % (_TT_PREFIX, signature)
        if signature in supported_signatures:
            tt_signatures.append(signature)
        elif signature_with_prefix in supported_signatures:
            tt_signatures.append(signature_with_prefix)
        else:
            logging.warning('Unknown signature:%s. Supported signatures: %s' % (signature, supported_signatures))
    if not tt_signatures:
        return {TT_SUMMARY_MAX_ABS: 0, TT_SUMMARY_NORM: 1}
    else:
        return {signature: idx for idx, signature in enumerate(tt_signatures)}

Verifies and returns the summary signatures.

Returns:
        A dictionary of the signature identifiers {signature: index} that will be
        computed when trace_mode is summary.


def _filter_out_metaclasses(bases, ctx):
    non_meta = []
    meta = None
    for base in bases:
        with_metaclass = False
        for b in base.data:
            if isinstance(b, metaclass.WithMetaclassInstance):
                with_metaclass = True
                if not meta:
                    meta = b.cls.to_variable(ctx.root_node)
                non_meta.extend(b.bases)
        if not with_metaclass:
            non_meta.append(base)
    return (meta, non_meta)

Process the temporary classes created by six.with_metaclass.

    six.with_metaclass constructs an anonymous class holding a metaclass and a
    list of base classes; if we find instances in `bases`, store the first
    metaclass we find and remove all metaclasses from `bases`.

Args:
        bases: The list of base classes for the class being constructed.
        ctx: The current context.

Returns:
        A tuple of (metaclass, base classes)


def broadcast_dynamic_shape(shape_x, shape_y):
    if not isinstance(shape_x, RaggedTensorDynamicShape):
        raise TypeError('shape_x must be a RaggedTensorDynamicShape')
    if not isinstance(shape_y, RaggedTensorDynamicShape):
        raise TypeError('shape_y must be a RaggedTensorDynamicShape')
    if shape_x.rank is None or shape_y.rank is None:
        raise ValueError('Unable to broadcast: unknown rank')
    broadcast_rank = max(shape_x.rank, shape_y.rank)
    shape_x = shape_x.broadcast_to_rank(broadcast_rank)
    shape_y = shape_y.broadcast_to_rank(broadcast_rank)
    for axis in range(broadcast_rank):
        shape_x = shape_x.broadcast_dimension(axis, shape_y.dimension_size(axis))
        shape_y = shape_y.broadcast_dimension(axis, shape_x.dimension_size(axis))
    return shape_x

Returns the shape formed by broadcasting two shapes to be compatible.

Args:
        shape_x: A `RaggedTensorDynamicShape`
        shape_y: A `RaggedTensorDynamicShape`

Returns:
        A `RaggedTensorDynamicShape`.

Raises:
        ValueError: If `shape_x` and `shape_y` are not broadcast-compatible.


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

Returns the result as a boolean.

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


def broadcast_recv(shape, dtype, group_size, group_key, instance_key, communication_hint='auto', timeout=0):
    if group_size <= 1:
        raise ValueError(f'Parameter `group_size` to broadcast_send must be at least 2. Received: {group_size}.')
    return gen_collective_ops.collective_bcast_recv(shape=shape, T=dtype, group_size=group_size, group_key=group_key, instance_key=instance_key, communication_hint=communication_hint.lower(), timeout_seconds=timeout)

Receives a broadcasts tensor, across devices.

Args:
        shape: Shape of the tensor to be received.
        dtype: Type of the tensor to be received.
        group_size: one plus the number of receiving tensors, i.e. the total
        number of devices participating.  Each tensor must reside on a
        different device.
        group_key: an integer identifying the group of devices.
        instance_key: an integer identifying the participating group of Ops.
        communication_hint: preferred collective communication.  The implementation
        may fall back to another mechanism.  Options include `auto`, `ring`, and
        `nccl`.
        timeout: If set to a non zero, set a completion timeout to detect staleness.
        If the timer goes off, a DeadlineExceededError is raised.
        The timeout value in seconds. This feature is experimental.

Returns:
        An Op implementing the broadcast receive.

Raises:
        ValueError: if any of the input parameter constraints are not met.


def forward(self, pixel_values: Optional[torch.FloatTensor]=None, output_attentions: Optional[bool]=None, output_hidden_states: Optional[bool]=None, interpolate_pos_encoding: bool=False, return_dict: Optional[bool]=None) -> Union[Tuple, BaseModelOutputWithPooling]:
    return_dict = return_dict if return_dict is not None else self.config.use_return_dict
    return self.vision_model(pixel_values=pixel_values, output_attentions=output_attentions, output_hidden_states=output_hidden_states, interpolate_pos_encoding=interpolate_pos_encoding, return_dict=return_dict)

Examples:

    ```python
    >>> from PIL import Image
    >>> import requests
    >>> from transformers import CLIPProcessor, ChineseCLIPVisionModel

    >>> model = ChineseCLIPVisionModel.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16")
    >>> processor = CLIPProcessor.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16")

    >>> url = "https://clip-cn-beijing.oss-cn-beijing.aliyuncs.com/pokemon.jpeg"
    >>> image = Image.open(requests.get(url, stream=True).raw)

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

    >>> outputs = model(**inputs)
    >>> last_hidden_state = outputs.last_hidden_state
    >>> pooled_output = outputs.pooler_output  # pooled CLS states
    ```


def as_dimension(value):
    if isinstance(value, Dimension):
        return value
    else:
        return Dimension(value)

Converts the given value to a Dimension.

    A Dimension input will be returned unmodified.
    An input of `None` will be converted to an unknown Dimension.
    An integer input will be converted to a Dimension with that value.

Args:
        value: The value to be converted.

Returns:
        A Dimension corresponding to the given value.


def get_candidates(self, input_ids: torch.LongTensor) -> Tuple[torch.LongTensor, Optional[torch.FloatTensor]]:
    raise NotImplementedError(f'{self.__class__} is an abstract class. Only classes inheriting this class can call `get_candidates`.')

Fetches the candidates to be tried for the current input.

Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. [What are input IDs?](../glossary#input-ids)

Returns:
        `torch.LongTensor` of shape `(batch_size, candidate_length)` containing the candidate sequences to be
        assessed by the model and, optionally, a `torch.FloatTensor` of shape `(batch_size, candidate_length,
        vocabulary_size)` containing the logits associated to each candidate.


def should_run_validation(validation_freq, epoch):
    one_indexed_epoch = epoch + 1
    if isinstance(validation_freq, int):
        if validation_freq < 1:
            raise ValueError('`validation_freq` can not be less than 1.')
        return one_indexed_epoch % validation_freq == 0
    if not isinstance(validation_freq, collections.abc.Container):
        raise ValueError('`validation_freq` must be an Integer or `collections.abc.Container` (e.g. list, tuple, etc.)')
    return one_indexed_epoch in validation_freq

Checks if validation should be run this epoch.

Args:
        validation_freq: Integer or list. If an integer, specifies how many training
        epochs to run before a new validation run is performed. If a list,
        specifies the epochs on which to run validation.
        epoch: Integer, the number of the training epoch just completed.

Returns:
        Bool, True if validation should be run.

Raises:
        ValueError: if `validation_freq` is an Integer and less than 1, or if
        it is neither an Integer nor a Sequence.


def data_gen_sig1() -> repr_dataset.RepresentativeDataset:
    for _ in range(4):
        yield {'x1': random_ops.random_uniform(shape=in_placeholder_1.shape)}

Generates tuple-style samples.

    The first element of the tuple identifies the signature key the input data
    is for.

Yields:
        Representative samples for signature 'sig1'.


def update_checkpoint_state(save_dir, model_checkpoint_path, all_model_checkpoint_paths=None, latest_filename=None, all_model_checkpoint_timestamps=None, last_preserved_timestamp=None):
    update_checkpoint_state_internal(save_dir=save_dir, model_checkpoint_path=model_checkpoint_path, all_model_checkpoint_paths=all_model_checkpoint_paths, latest_filename=latest_filename, save_relative_paths=False, all_model_checkpoint_timestamps=all_model_checkpoint_timestamps, last_preserved_timestamp=last_preserved_timestamp)

Updates the content of the 'checkpoint' file.

    This updates the checkpoint file containing a CheckpointState
    proto.

Args:
        save_dir: Directory where the model was saved.
        model_checkpoint_path: The checkpoint file.
        all_model_checkpoint_paths: List of strings.  Paths to all not-yet-deleted
        checkpoints, sorted from oldest to newest.  If this is a non-empty list,
        the last element must be equal to model_checkpoint_path.  These paths
        are also saved in the CheckpointState proto.
        latest_filename: Optional name of the checkpoint file.  Default to
        'checkpoint'.
        all_model_checkpoint_timestamps: Optional list of timestamps (floats,
        seconds since the Epoch) indicating when the checkpoints in
        `all_model_checkpoint_paths` were created.
        last_preserved_timestamp: A float, indicating the number of seconds since
        the Epoch when the last preserved checkpoint was written, e.g. due to a
        `keep_checkpoint_every_n_hours` parameter (see
        `tf.train.CheckpointManager` for an implementation).

Raises:
        RuntimeError: If any of the model checkpoint paths conflict with the file
        containing CheckpointSate.


def convert(self):
    saved_model_convert_result = self._convert_as_saved_model()
    if saved_model_convert_result:
        return saved_model_convert_result
    graph_def, input_tensors, output_tensors, frozen_func = self._freeze_keras_model()
    graph_def = self._optimize_tf_model(graph_def, input_tensors, output_tensors, frozen_func)
    return super(TFLiteKerasModelConverterV2, self).convert(graph_def, input_tensors, output_tensors)

Converts a keras model based on instance variables.

Returns:
        The converted data in serialized format.

Raises:
        ValueError:
        Multiple concrete functions are specified.
        Input shape is not specified.
        Invalid quantization parameters.


def get_inlined_extension_url(field: descriptor.FieldDescriptor) -> str:
    options = annotation_utils.get_options(field)
    if options.HasExtension(annotations_pb2.fhir_inlined_extension_url):
        return options.Extensions[annotations_pb2.fhir_inlined_extension_url]
    return field.camelcase_name

Returns the FHIR inlined extension URL for a field.

Args:
        field: The FieldDescriptor to examine.

Returns:
        The FHIR inlined extension URL, if one exists, otherwise returns the camel-
        case name of the FieldDescriptor.


def __mod__(self, other):
    other = as_dimension(other)
    if self._value is None or other.value is None:
        return Dimension(None)
    else:
        return Dimension(self._value % other.value)

Returns `self` modulo `other`.

    Dimension modulo are computed as follows:

    ```python
    tf.compat.v1.Dimension(m)    % tf.compat.v1.Dimension(n)     ==
    tf.compat.v1.Dimension(m % n)
    tf.compat.v1.Dimension(m)    % tf.compat.v1.Dimension(None)  # equiv. to
    tf.compat.v1.Dimension(None)
    tf.compat.v1.Dimension(None) % tf.compat.v1.Dimension(n)     # equiv. to
    tf.compat.v1.Dimension(None)
    tf.compat.v1.Dimension(None) % tf.compat.v1.Dimension(None)  # equiv. to
    tf.compat.v1.Dimension(None)
    ```

Args:
        other: Another Dimension, or a value accepted by `as_dimension`.

Returns:
        A Dimension whose value is `self` modulo `other`.


def format_tensor(tensor, tensor_name, np_printoptions, print_all=False, tensor_slicing=None, highlight_options=None, include_numeric_summary=False, write_path=None):
    if tensor_slicing:
        value = command_parser.evaluate_tensor_slice(tensor, tensor_slicing)
        sliced_name = tensor_name + tensor_slicing
    else:
        value = tensor
        sliced_name = tensor_name
    auxiliary_message = None
    if write_path:
        with gfile.Open(write_path, 'wb') as output_file:
            np.save(output_file, value)
        line = debugger_cli_common.RichLine('Saved value to: ')
        line += debugger_cli_common.RichLine(write_path, font_attr='bold')
        line += ' (%sB)' % bytes_to_readable_str(gfile.Stat(write_path).length)
        auxiliary_message = debugger_cli_common.rich_text_lines_from_rich_line_list([line, debugger_cli_common.RichLine('')])
    if print_all:
        np_printoptions['threshold'] = value.size
    else:
        np_printoptions['threshold'] = DEFAULT_NDARRAY_DISPLAY_THRESHOLD
    return tensor_format.format_tensor(value, sliced_name, include_metadata=True, include_numeric_summary=include_numeric_summary, auxiliary_message=auxiliary_message, np_printoptions=np_printoptions, highlight_options=highlight_options)

Generate formatted str to represent a tensor or its slices.

Args:
        tensor: (numpy ndarray) The tensor value.
        tensor_name: (str) Name of the tensor, e.g., the tensor's debug watch key.
        np_printoptions: (dict) Numpy tensor formatting options.
        print_all: (bool) Whether the tensor is to be displayed in its entirety,
        instead of printing ellipses, even if its number of elements exceeds
        the default numpy display threshold.
        (Note: Even if this is set to true, the screen output can still be cut
        off by the UI frontend if it consist of more lines than the frontend
        can handle.)
        tensor_slicing: (str or None) Slicing of the tensor, e.g., "[:, 1]". If
        None, no slicing will be performed on the tensor.
        highlight_options: (tensor_format.HighlightOptions) options to highlight
        elements of the tensor. See the doc of tensor_format.format_tensor()
        for more details.
        include_numeric_summary: Whether a text summary of the numeric values (if
        applicable) will be included.
        write_path: A path to save the tensor value (after any slicing) to
        (optional). `numpy.save()` is used to save the value.

Returns:
        An instance of `debugger_cli_common.RichTextLines` representing the
        (potentially sliced) tensor.


def __init__(self, granularity: Granularity) -> None:
    """Initializes the HTML parser for the KNBC corpus.

Args:
  granularity: Granularity of the output chunks.
"""
    super().__init__()
    self.chunks = ['']
    self.row = 0
    self.col = 0
    self.current_word = ''
    self.on_split_row = False
    self.granularity = granularity

Parses the HTML files in the KNBC corpus to collect chunks.

Attributes:
        chunks: The collected chunks.
        row: The current row index.
        col: The current column index.
        current_word: The current word to process.
        on_split_row: Whether the scan is on the splitting row.
        granularity: Granularity of the output chunks.


def _TestGradient(self, nccl_reduce, numpy_fn):

    def _Gradient(tensors, devices):
        inputs = [array_ops.placeholder(t.dtype, t.shape) for t in tensors]
        reduce_tensors = nccl_reduce(inputs, devices)
        losses = _DeviceTensors(tensors, [t.device for t in reduce_tensors])
        grads = gradients.gradients(reduce_tensors, inputs, losses, colocate_gradients_with_ops=True)
        return [g for g in grads if g is not None]
    self._Test(_Gradient, numpy_fn)

Tests the gradient of nccl_reduce.

Args:
        nccl_reduce: A function taking a list of tensors and a list of devices,
        and returns a list of reduced tensors and a list of ops to perform the
        reduction.
        numpy_fn: A function taking two tensors and returning the gradient of the
        reduction of the two.


def _swap_where(condition, x, y):
    return (tf.where(condition, y, x), tf.where(condition, x, y))

Swaps the elements of `x` and `y` based on `condition`.

Args:
        condition: A `Tensor` of dtype bool.
        x: A `Tensor` with the same shape as `condition`.
        y: A `Tensor` with the same shape and dtype as `x`.

Returns:
        Two `Tensors` with the same shape as `x` and `y`.


def sort(x, axis=-1):
    if any_symbolic_tensors((x,)):
        return Sort(axis=axis).symbolic_call(x)
    return backend.numpy.sort(x, axis=axis)

Sorts the elements of `x` along a given axis in ascending order.

Args:
        x: Input tensor.
        axis: Axis along which to sort. If `None`, the tensor is flattened
            before sorting. Defaults to `-1`; the last axis.

Returns:
        Sorted tensor.


def _create_op_from_tf_operation(self, c_op, compute_device=True) -> 'Operation':
    self._check_not_finalized()
    ret = Operation._from_c_op(c_op=c_op, g=self)
    name_key = ret.name.lower()
    if name_key not in self._names_in_use:
        self._names_in_use[name_key] = 1
    self._create_op_helper(ret, compute_device=compute_device)
    return ret

Creates an `Operation` in this graph from the supplied TF_Operation.

    This method is like create_op() except the new Operation is constructed
    using `c_op`. The returned Operation will have `c_op` as its _c_op
    field. This is used to create Operation objects around TF_Operations created
    indirectly by the C API (e.g. by TF_ImportGraphDef, TF_FinishWhile).

    This function does not call Operation._control_flow_post_processing or
    Graph._control_dependencies_for_inputs (since the inputs may not be
    available yet). The caller is responsible for calling these methods.

Args:
        c_op: a wrapped TF_Operation
        compute_device: (Optional.) If True, device functions will be executed to
        compute the device property of the Operation.

Returns:
        An `Operation` object.


def constant(value, dtype=None, shape=None, name=None):
    if dtype is None:
        dtype = floatx()
    return constant_op.constant(value, dtype=dtype, shape=shape, name=name)

Creates a constant tensor.

Args:
        value: A constant value (or list)
        dtype: The type of the elements of the resulting tensor.
        shape: Optional dimensions of resulting tensor.
        name: Optional name for the tensor.

Returns:
        A Constant Tensor.


def __init__(self, cardinalities: List[int], embedding_dims: List[int]) -> None:
    super().__init__()
    self.num_features = len(cardinalities)
    self.embedders = nn.ModuleList([nn.Embedding(c, d) for c, d in zip(cardinalities, embedding_dims)])

Embed a sequence of categorical features.

Args:
        cardinalities (`list[int]`):
            List of cardinalities of the categorical features.
        embedding_dims (`list[int]`):
            List of embedding dimensions of the categorical features.


def fixed_size_partitioner(num_shards, axis=0):

    def _partitioner(shape, **unused_args):
        partitions_list = [1] * len(shape)
        partitions_list[axis] = min(num_shards, shape.dims[axis].value)
        return partitions_list
    return _partitioner

Partitioner to specify a fixed number of shards along given axis.

    @compatibility(TF2)
    This API is deprecated in TF2. In TF2, partitioner is no longer part of
    the variable declaration via `tf.Variable`.
    [ParameterServer Training]
    (https://www.tensorflow.org/tutorials/distribute/parameter_server_training)
    handles partitioning of variables. The corresponding TF2 partitioner class of
    `fixed_size_partitioner` is
    `tf.distribute.experimental.partitioners.FixedShardsPartitioner`.

    Check the [migration guide]
    (https://www.tensorflow.org/guide/migrate#2_use_python_objects_to_track_variables_and_losses)
    on the differences in treatment of variables and losses between TF1 and TF2.

    Before:

        ```
        x = tf.compat.v1.get_variable(
        "x", shape=(2,), partitioner=tf.compat.v1.fixed_size_partitioner(2)
        )
        ```
    After:

        ```
        partitioner = (
        tf.distribute.experimental.partitioners.FixedShardsPartitioner(
            num_shards=2)
        )
        strategy = tf.distribute.experimental.ParameterServerStrategy(
                    cluster_resolver=cluster_resolver,
                    variable_partitioner=partitioner)

        with strategy.scope():
        x = tf.Variable([1.0, 2.0])
        ```
    @end_compatibility

Args:
        num_shards: `int`, number of shards to partition variable.
        axis: `int`, axis to partition on.

Returns:
        A partition function usable as the `partitioner` argument to
        `variable_scope` and `get_variable`.


def zeros(shape, dtype=None):
    return backend.numpy.zeros(shape, dtype=dtype)

Return a new tensor of given shape and type, filled with zeros.

Args:
        shape: Shape of the new tensor.
        dtype: Desired data type of the tensor.

Returns:
        Tensor of zeros with the given shape and dtype.


def serialize_keras_object(instance):
    instance = inspect.unwrap(instance)
    if instance is None:
        return None
    if hasattr(instance, 'get_config'):
        name = object_registration.get_registered_name(instance.__class__)
        try:
            config = instance.get_config()
        except NotImplementedError as e:
            if _SKIP_FAILED_SERIALIZATION:
                return serialize_keras_class_and_config(name, {_LAYER_UNDEFINED_CONFIG_KEY: True})
            raise e
        serialization_config = {}
        for key, item in config.items():
            if isinstance(item, str):
                serialization_config[key] = item
                continue
            try:
                serialized_item = serialize_keras_object(item)
                if isinstance(serialized_item, dict) and (not isinstance(item, dict)):
                    serialized_item['__passive_serialization__'] = True
                serialization_config[key] = serialized_item
            except ValueError:
                serialization_config[key] = item
        name = object_registration.get_registered_name(instance.__class__)
        return serialize_keras_class_and_config(name, serialization_config, instance)
    if hasattr(instance, '__name__'):
        return object_registration.get_registered_name(instance)
    raise ValueError(f"Cannot serialize {instance} because it doesn't implement `get_config()`.")

Serialize a Keras object into a JSON-compatible representation.

    Calls to `serialize_keras_object` while underneath the
    `SharedObjectSavingScope` context manager will cause any objects re-used
    across multiple layers to be saved with a special shared object ID. This
    allows the network to be re-created properly during deserialization.

Args:
        instance: The object to serialize.

Returns:
        A dict-like, JSON-compatible representation of the object's config.


def get_num_bytes(self, batch: Sequence[pandas.DataFrame]) -> int:
    return sum((df.memory_usage(deep=True).sum() for df in batch))

Returns:
    The number of bytes of data for a batch.


def put(self, resource_json: Dict[str, Any], parent_bundle: Optional[Dict[str, Any]]=None) -> None:
    if parent_bundle is None:
        self.resources_by_uri[resource_json['url']] = resource_json
    else:
        self.resources_by_uri[resource_json['url']] = parent_bundle

Puts the given resource into this collection.

    Adds the resource represented by `resource_json` found inside
    `parent_bundle` into this collection for subsequent lookup via the Get
    method. `parent_bundle` may be None if `resource_json` is not located inside
    a bundle.

Args:
        resource_json: The JSON object representing the resource.
        parent_bundle: The bundle `resource_json` is located inside, if any.


def is_tf2_saved_model(export_dir):
    try:
        fingerprint = fingerprinting.read_fingerprint(export_dir)
        if fingerprint.saved_object_graph_hash != 0:
            logging.info('SavedModel at %s is a TF2 SavedModel', export_dir)
            return True
    except Exception:
        logging.info('Failed to read fingerprint from SavedModel. Parsing MetaGraph ...')
        saved_model_proto = loader_impl.parse_saved_model(export_dir)
        if len(saved_model_proto.meta_graphs) == 1 and saved_model_proto.meta_graphs[0].HasField('object_graph_def'):
            logging.info('SavedModel at %s is a TF2 SavedModel', export_dir)
            return True
    logging.info('SavedModel at %s is a TF1 SavedModel', export_dir)
    return False

Identifies if an exported SavedModel is a TF2 SavedModel.

    There are differences in SavedModel semantics between TF1 and TF2 that are
    documented here:
    https://www.tensorflow.org/guide/migrate/saved_model#savedmodel. This helper
    util function serves to distinguish the TF1 vs TF2 semantics used when
    exporting SavedModels.

Args:
        export_dir: The SavedModel directory to load from.

Returns:
        True if TF2 SavedModel semantics are used, False if TF1 SavedModel semantics
        are used.


def __init__(self, on_ui_exit=None, config=None):
    self._on_ui_exit = on_ui_exit
    self._command_handler_registry = debugger_cli_common.CommandHandlerRegistry()
    self._tab_completion_registry = debugger_cli_common.TabCompletionRegistry()
    self._tab_completion_registry.register_tab_comp_context([''], self.CLI_EXIT_COMMANDS + [debugger_cli_common.CommandHandlerRegistry.HELP_COMMAND] + debugger_cli_common.CommandHandlerRegistry.HELP_COMMAND_ALIASES)
    self._config = config or cli_config.CLIConfig()
    self._config_argparser = argparse.ArgumentParser(description='config command', usage=argparse.SUPPRESS)
    subparsers = self._config_argparser.add_subparsers()
    set_parser = subparsers.add_parser('set')
    set_parser.add_argument('property_name', type=str)
    set_parser.add_argument('property_value', type=str)
    set_parser = subparsers.add_parser('show')
    self.register_command_handler('config', self._config_command_handler, self._config_argparser.format_help(), prefix_aliases=['cfg'])

Constructor of the base class.

Args:
        on_ui_exit: (`Callable`) the callback to be called when the UI exits.
        config: An instance of `cli_config.CLIConfig()` carrying user-facing
        configurations.


def register_converter(src_type: Union[Type[Any], Tuple[Type[Any], ...]], dest_type: Union[Type[Any], Tuple[Type[Any], ...]], convert_fn: Callable[[Any], Any]) -> None:
    _TYPE_CONVERTER_REGISTRY.register(src_type, dest_type, convert_fn)

Register converter from source type to destination type.

    Examples::

        # Add converter from int to float.
        pg.typing.register_converter(int, float, float)

        assert pg.typing.Float().apply(1) is 1.0

        # Add converter from a dict to class A.
        def from_dict(d):
        return A(**d)

        assert isinstance(pg.typing.Object(A).apply({'x': 1, 'y': 2}), A)

Args:
        src_type: Source value type.
        dest_type: Target value type.
        convert_fn: Function that performs the conversion, in signature
        (src_type) -> dest_type.


def get_subtype(self):
    raise NotImplementedError('Base class should not be called directly!')

This function returns the sub-type of the sniffer.

Returns:
        The sub-type (string) of the sniffer. Corresponds to the 'SubType'
        key of the sniffer configuration.


def _calculate_scores(self, query, key):
    return NotImplementedError

Calculates attention scores.

Args:
        query: Query tensor of shape `[batch_size, Tq, dim]`.
        key: Key tensor of shape `[batch_size, Tv, dim]`.

Returns:
        Tensor of shape `[batch_size, Tq, Tv]`.


def avro_union_type_to_beam_type(union_type: List) -> schema_pb2.FieldType:
    if len(union_type) == 2 and 'null' in union_type:
        for avro_type in union_type:
            if avro_type in AVRO_PRIMITIVES_TO_BEAM_PRIMITIVES:
                return schema_pb2.FieldType(atomic_type=AVRO_PRIMITIVES_TO_BEAM_PRIMITIVES[avro_type], nullable=True)
        return schemas.typing_to_runner_api(Any)
    return schemas.typing_to_runner_api(Any)

convert an avro union type to a beam type

    if the union type is a nullable, and it is a nullable union of an avro
    primitive with a corresponding beam primitive then create a nullable beam
    field of the corresponding beam type, otherwise return an Any type.

Args:
        union_type: the avro union type to convert.

Returns:
        the beam type of the avro union.


def get_dense_tensor(self, transformation_cache, state_manager):
    return transformation_cache.get(self, state_manager)

Returns dense `Tensor` representing numeric feature.

Args:
        transformation_cache: A `FeatureTransformationCache` object to access
        features.
        state_manager: A `StateManager` to create / access resources such as
        lookup tables.

Returns:
        Dense `Tensor` created within `transform_feature`.


def event_shape_tensor(self, name='event_shape_tensor'):
    with self._name_scope(name):
        if self.event_shape.is_fully_defined():
            return ops.convert_to_tensor(self.event_shape.as_list(), dtype=dtypes.int32, name='event_shape')
        return self._event_shape_tensor()

Shape of a single sample from a single batch as a 1-D int32 `Tensor`.

Args:
        name: name to give to the op

Returns:
        event_shape: `Tensor`.


def create_datasets():
    if use_device:
        datasets = []
        for i in range(len(self.embedding_devices)):
            datasets.append(dataset_ops.DatasetV2.from_tensor_slices({'feature': [[[i % self._num_cores_per_replica]]]}).repeat())
        return datasets
    else:
        dataset = strategy.distribute_datasets_from_function(input_fn, options=distribute_lib.InputOptions(experimental_fetch_to_device=False))
        return [dataset]

Creates either a per-replica dataset, or multiple per-devices ones.

    This function explicitly creates per-device datasets because the strategy
    does not produce a distributed dataset in the model-parallel case; there
    is only one replica. Without this consideration, the embeddings would be
    read as [0, 0] instead of the expected [0, 1] since all the devices would
    receive the same value.

Returns:
        A list of one or more dataset(s).


def make_one_shot_iterator(self):
    return self._make_one_shot_iterator()

Get a one time use iterator for DistributedDatasetV1.

    Note: This API is deprecated. Please use `for ... in dataset:` to iterate
    over the dataset or `iter` to create an iterator.

Returns:
        A DistributedIteratorV1 instance.