Skip to content

logits_process

mindnlp.transformers.generation.logits_process

Logits process

mindnlp.transformers.generation.logits_process.AlternatingCodebooksLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] enforcing alternated generation between the two codebooks of [Bark]'s fine submodel.

PARAMETER DESCRIPTION
input_start_len

The length of the initial input sequence.

TYPE: `int`

semantic_vocab_size

Vocabulary size of the semantic part, i.e number of tokens associated to the semantic vocabulary.

TYPE: `int`

codebook_size

Number of tokens associated to the codebook.

TYPE: `int`

Source code in mindnlp/transformers/generation/logits_process.py
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
class AlternatingCodebooksLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] enforcing alternated generation between the two codebooks of [`Bark`]'s fine submodel.

    Args:
        input_start_len (`int`):
            The length of the initial input sequence.
        semantic_vocab_size (`int`):
            Vocabulary size of the semantic part, i.e number of tokens associated to the semantic vocabulary.
        codebook_size (`int`):
            Number of tokens associated to the codebook.
    """
    def __init__(self, input_start_len: int, semantic_vocab_size: int, codebook_size: int):
        """
        Initializes an instance of the AlternatingCodebooksLogitsProcessor class.

        Args:
            self: The instance of the class.
            input_start_len (int): The starting length of the input sequence.
                Must be a non-negative integer.
            semantic_vocab_size (int): The size of the semantic vocabulary.
            codebook_size (int): The size of the codebook.

        Returns:
            None.

        Raises:
            ValueError: If `input_start_len` is not an integer or if it is a negative value.

        """
        if not isinstance(input_start_len, int) or input_start_len < 0:
            raise ValueError(f"`input_starting_length` has to be a non-negative integer, but is {input_start_len}")

        self.input_start_len = input_start_len
        self.semantic_vocab_size = semantic_vocab_size
        self.codebook_size = codebook_size

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        The '__call__' method in the 'AlternatingCodebooksLogitsProcessor' class processes the input tensors to
        manipulate the scores based on alternating codebooks.

        Args:
            self: An instance of the 'AlternatingCodebooksLogitsProcessor' class.
            input_ids (mindspore.Tensor):
                A tensor containing the input IDs.

                - Shape: (batch_size, sequence_length).
                - The sequence length represents the length of the input IDs.
            scores (mindspore.Tensor):
                A tensor containing the scores.

                - Shape: (batch_size, vocabulary_size).
                - The vocabulary size represents the total number of elements in the vocabulary.

        Returns:
            mindspore.Tensor:
                A tensor representing the modified scores.

                - Shape: (batch_size, vocabulary_size).

        Raises:
            None.
        """
        curr_len = input_ids.shape[-1]

        # even -> first codebook, odd -> second codebook
        is_first_codebook = ((curr_len - self.input_start_len) % 2) == 0

        if is_first_codebook:
            scores[:, : self.semantic_vocab_size] = -float("inf")
            scores[:, self.semantic_vocab_size + self.codebook_size :] = -float("inf")
        else:
            scores[:, : self.semantic_vocab_size + self.codebook_size] = -float("inf")

        return scores

mindnlp.transformers.generation.logits_process.AlternatingCodebooksLogitsProcessor.__call__(input_ids, scores)

The 'call' method in the 'AlternatingCodebooksLogitsProcessor' class processes the input tensors to manipulate the scores based on alternating codebooks.

PARAMETER DESCRIPTION
self

An instance of the 'AlternatingCodebooksLogitsProcessor' class.

input_ids

A tensor containing the input IDs.

  • Shape: (batch_size, sequence_length).
  • The sequence length represents the length of the input IDs.

TYPE: Tensor

scores

A tensor containing the scores.

  • Shape: (batch_size, vocabulary_size).
  • The vocabulary size represents the total number of elements in the vocabulary.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor representing the modified scores.

  • Shape: (batch_size, vocabulary_size).
Source code in mindnlp/transformers/generation/logits_process.py
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    The '__call__' method in the 'AlternatingCodebooksLogitsProcessor' class processes the input tensors to
    manipulate the scores based on alternating codebooks.

    Args:
        self: An instance of the 'AlternatingCodebooksLogitsProcessor' class.
        input_ids (mindspore.Tensor):
            A tensor containing the input IDs.

            - Shape: (batch_size, sequence_length).
            - The sequence length represents the length of the input IDs.
        scores (mindspore.Tensor):
            A tensor containing the scores.

            - Shape: (batch_size, vocabulary_size).
            - The vocabulary size represents the total number of elements in the vocabulary.

    Returns:
        mindspore.Tensor:
            A tensor representing the modified scores.

            - Shape: (batch_size, vocabulary_size).

    Raises:
        None.
    """
    curr_len = input_ids.shape[-1]

    # even -> first codebook, odd -> second codebook
    is_first_codebook = ((curr_len - self.input_start_len) % 2) == 0

    if is_first_codebook:
        scores[:, : self.semantic_vocab_size] = -float("inf")
        scores[:, self.semantic_vocab_size + self.codebook_size :] = -float("inf")
    else:
        scores[:, : self.semantic_vocab_size + self.codebook_size] = -float("inf")

    return scores

mindnlp.transformers.generation.logits_process.AlternatingCodebooksLogitsProcessor.__init__(input_start_len, semantic_vocab_size, codebook_size)

Initializes an instance of the AlternatingCodebooksLogitsProcessor class.

PARAMETER DESCRIPTION
self

The instance of the class.

input_start_len

The starting length of the input sequence. Must be a non-negative integer.

TYPE: int

semantic_vocab_size

The size of the semantic vocabulary.

TYPE: int

codebook_size

The size of the codebook.

TYPE: int

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If input_start_len is not an integer or if it is a negative value.

Source code in mindnlp/transformers/generation/logits_process.py
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
def __init__(self, input_start_len: int, semantic_vocab_size: int, codebook_size: int):
    """
    Initializes an instance of the AlternatingCodebooksLogitsProcessor class.

    Args:
        self: The instance of the class.
        input_start_len (int): The starting length of the input sequence.
            Must be a non-negative integer.
        semantic_vocab_size (int): The size of the semantic vocabulary.
        codebook_size (int): The size of the codebook.

    Returns:
        None.

    Raises:
        ValueError: If `input_start_len` is not an integer or if it is a negative value.

    """
    if not isinstance(input_start_len, int) or input_start_len < 0:
        raise ValueError(f"`input_starting_length` has to be a non-negative integer, but is {input_start_len}")

    self.input_start_len = input_start_len
    self.semantic_vocab_size = semantic_vocab_size
    self.codebook_size = codebook_size

mindnlp.transformers.generation.logits_process.BarkEosPrioritizerLogitsProcessor

Bases: LogitsProcessor

This processor ensures that the EOS token is selected if its probability is greater than the min_eos_p.

This logits processor is exclusively compatible with Bark. See the model documentation for examples.

PARAMETER DESCRIPTION
eos_token_id

The id of the end-of-sequence token. Optionally, use a list to set multiple end-of-sequence tokens.

TYPE: `Union[int, List[int]]`

min_eos_p

Minimum end of speech threshold.

TYPE: `float`, *optional*

Source code in mindnlp/transformers/generation/logits_process.py
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
class BarkEosPrioritizerLogitsProcessor(LogitsProcessor):
    r"""This processor ensures that the EOS token is selected if its probability is greater than the `min_eos_p`.

    <Tip warning={true}>

    This logits processor is exclusively compatible with
    [Bark](https://hf-mirror.com/docs/transformers/en/model_doc/bark). See the model documentation for examples.

    </Tip>

    Args:
        eos_token_id (`Union[int, List[int]]`):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
        min_eos_p (`float`, *optional*):
            Minimum end of speech threshold.
    """
    def __init__(self, eos_token_id: Union[int, List[int]], min_eos_p: float):
        """
        Initializes an instance of the BarkEosPrioritizerLogitsProcessor class.

        Args:
            self: The instance of the class.
            eos_token_id (Union[int, List[int]]): An integer or a list of integers representing the end-of-sequence token ID(s).
                If an integer is provided, it will be converted to a list with that integer as its only element.
            min_eos_p (float): The minimum value for the end-of-sequence probability. Must be a positive float.
                If not None, it should be greater than 0. Otherwise, a ValueError will be raised.

        Returns:
            None.

        Raises:
            ValueError: Raised if min_eos_p is not a positive float or if it is None.
        """
        if isinstance(eos_token_id, int):
            eos_token_id = [eos_token_id]
        self.eos_token_id = eos_token_id
        if min_eos_p is not None and min_eos_p <= 0:
            raise ValueError(f"`min_eos_p` has to be a positive float, but is {min_eos_p}")
        self.min_eos_p = min_eos_p

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method processes input logits for early stopping based on a minimum probability threshold for the
        end-of-sequence token.

        Args:
            self (BarkEosPrioritizerLogitsProcessor): An instance of the BarkEosPrioritizerLogitsProcessor class.
            input_ids (mindspore.Tensor): A Tensor containing input IDs.
            scores (mindspore.Tensor): A Tensor containing logits scores.

        Returns:
            mindspore.Tensor: Processed logits scores with early stopping applied based on the minimum end-of-sequence
                probability threshold.

        Raises:
            None
        """
        if self.min_eos_p:
            probs = ops.softmax(scores.float(), dim=-1)
            # create scores full of -inf except for the eos_token_id
            early_stop_scores = ops.ones_like(scores) * -float("inf")
            early_stop_scores[:, self.eos_token_id] = scores[:, self.eos_token_id]

            do_early_stop = probs[:, self.eos_token_id] > self.min_eos_p
            do_early_stop = ops.any(do_early_stop, dim=1, keepdim=True)
            scores = ops.where(do_early_stop, early_stop_scores, scores)

        return scores

mindnlp.transformers.generation.logits_process.BarkEosPrioritizerLogitsProcessor.__call__(input_ids, scores)

This method processes input logits for early stopping based on a minimum probability threshold for the end-of-sequence token.

PARAMETER DESCRIPTION
self

An instance of the BarkEosPrioritizerLogitsProcessor class.

TYPE: BarkEosPrioritizerLogitsProcessor

input_ids

A Tensor containing input IDs.

TYPE: Tensor

scores

A Tensor containing logits scores.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: Processed logits scores with early stopping applied based on the minimum end-of-sequence probability threshold.

Source code in mindnlp/transformers/generation/logits_process.py
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method processes input logits for early stopping based on a minimum probability threshold for the
    end-of-sequence token.

    Args:
        self (BarkEosPrioritizerLogitsProcessor): An instance of the BarkEosPrioritizerLogitsProcessor class.
        input_ids (mindspore.Tensor): A Tensor containing input IDs.
        scores (mindspore.Tensor): A Tensor containing logits scores.

    Returns:
        mindspore.Tensor: Processed logits scores with early stopping applied based on the minimum end-of-sequence
            probability threshold.

    Raises:
        None
    """
    if self.min_eos_p:
        probs = ops.softmax(scores.float(), dim=-1)
        # create scores full of -inf except for the eos_token_id
        early_stop_scores = ops.ones_like(scores) * -float("inf")
        early_stop_scores[:, self.eos_token_id] = scores[:, self.eos_token_id]

        do_early_stop = probs[:, self.eos_token_id] > self.min_eos_p
        do_early_stop = ops.any(do_early_stop, dim=1, keepdim=True)
        scores = ops.where(do_early_stop, early_stop_scores, scores)

    return scores

mindnlp.transformers.generation.logits_process.BarkEosPrioritizerLogitsProcessor.__init__(eos_token_id, min_eos_p)

Initializes an instance of the BarkEosPrioritizerLogitsProcessor class.

PARAMETER DESCRIPTION
self

The instance of the class.

eos_token_id

An integer or a list of integers representing the end-of-sequence token ID(s). If an integer is provided, it will be converted to a list with that integer as its only element.

TYPE: Union[int, List[int]]

min_eos_p

The minimum value for the end-of-sequence probability. Must be a positive float. If not None, it should be greater than 0. Otherwise, a ValueError will be raised.

TYPE: float

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

Raised if min_eos_p is not a positive float or if it is None.

Source code in mindnlp/transformers/generation/logits_process.py
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
def __init__(self, eos_token_id: Union[int, List[int]], min_eos_p: float):
    """
    Initializes an instance of the BarkEosPrioritizerLogitsProcessor class.

    Args:
        self: The instance of the class.
        eos_token_id (Union[int, List[int]]): An integer or a list of integers representing the end-of-sequence token ID(s).
            If an integer is provided, it will be converted to a list with that integer as its only element.
        min_eos_p (float): The minimum value for the end-of-sequence probability. Must be a positive float.
            If not None, it should be greater than 0. Otherwise, a ValueError will be raised.

    Returns:
        None.

    Raises:
        ValueError: Raised if min_eos_p is not a positive float or if it is None.
    """
    if isinstance(eos_token_id, int):
        eos_token_id = [eos_token_id]
    self.eos_token_id = eos_token_id
    if min_eos_p is not None and min_eos_p <= 0:
        raise ValueError(f"`min_eos_p` has to be a positive float, but is {min_eos_p}")
    self.min_eos_p = min_eos_p

mindnlp.transformers.generation.logits_process.ClassifierFreeGuidanceLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] for classifier free guidance (CFG). The scores are split over the batch dimension, where the first half correspond to the conditional logits (predicted from the input prompt) and the second half correspond to the unconditional logits (predicted from an empty or 'null' prompt). The processor computes a weighted average across the conditional and unconditional logits, parameterised by the guidance_scale.

See the paper for more information.

This logits processor is exclusively compatible with MusicGen

PARAMETER DESCRIPTION
guidance_scale

The guidance scale for classifier free guidance (CFG). CFG is enabled by setting guidance_scale > 1. Higher guidance scale encourages the model to generate samples that are more closely linked to the input prompt, usually at the expense of poorer quality.

TYPE: float

Example
>>> from transformers import AutoProcessor, MusicgenForConditionalGeneration
...
>>> processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
>>> model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
...
>>> inputs = processor(
...     text=["80s pop track with bassy drums and synth", "90s rock song with loud guitars and heavy drums"],
...     padding=True,
...     return_tensors="pt",
... )
>>> audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256)
Source code in mindnlp/transformers/generation/logits_process.py
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
class ClassifierFreeGuidanceLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] for classifier free guidance (CFG). The scores are split over the batch dimension,
    where the first half correspond to the conditional logits (predicted from the input prompt) and the second half
    correspond to the unconditional logits (predicted from an empty or 'null' prompt). The processor computes a
    weighted average across the conditional and unconditional logits, parameterised by the `guidance_scale`.

    See [the paper](https://arxiv.org/abs/2306.05284) for more information.

    <Tip warning={true}>

    This logits processor is exclusively compatible with
    [MusicGen](https://hf-mirror.com/docs/transformers/main/en/model_doc/musicgen)

    </Tip>

    Args:
        guidance_scale (float):
            The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale > 1`.
            Higher guidance scale encourages the model to generate samples that are more closely linked to the input
            prompt, usually at the expense of poorer quality.

    Example:
        ```python
        >>> from transformers import AutoProcessor, MusicgenForConditionalGeneration
        ...
        >>> processor = AutoProcessor.from_pretrained("facebook/musicgen-small")
        >>> model = MusicgenForConditionalGeneration.from_pretrained("facebook/musicgen-small")
        ...
        >>> inputs = processor(
        ...     text=["80s pop track with bassy drums and synth", "90s rock song with loud guitars and heavy drums"],
        ...     padding=True,
        ...     return_tensors="pt",
        ... )
        >>> audio_values = model.generate(**inputs, do_sample=True, guidance_scale=3, max_new_tokens=256)
        ```
        """

    def __init__(self, guidance_scale):
        """
        Initializes a new instance of the ClassifierFreeGuidanceLogitsProcessor class.

        Args:
            self: The instance of the class.
            guidance_scale (int): The scale of guidance for the classifier-free guidance processor. Must be greater than 1.

        Returns:
            None.

        Raises:
            ValueError: If the guidance_scale is not greater than 1, a ValueError is raised indicating the requirement
                for guidance_scale > 1 to use the classifier-free guidance processor.
        """
        if guidance_scale > 1:
            self.guidance_scale = guidance_scale
        else:
            raise ValueError(
                "Require guidance scale >1 to use the classifier free guidance processor, got guidance scale "
                f"{guidance_scale}."
            )

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        Performs processing on logits to generate processed scores for a classifier with free guidance.

        Args:
            self (ClassifierFreeGuidanceLogitsProcessor): An instance of the ClassifierFreeGuidanceLogitsProcessor class.
            input_ids (mindspore.Tensor): A tensor containing input IDs for the classifier.
            scores (mindspore.Tensor): A tensor containing logits for the classifier.

        Returns:
            mindspore.Tensor: A tensor containing the processed scores for the classifier.

        Raises:
            ValueError: If the shape of the scores tensor does not meet the required conditions.

        The '__call__' method processes the logits to generate processed scores for a classifier with free guidance.
        It expects two parameters: 'input_ids' and 'scores'. The method returns a tensor of type 'mindspore.Tensor'
        which contains the processed scores.

        The 'input_ids' parameter is a tensor that holds the input IDs for the classifier. It is used to determine the
        batch size and shape of the scores tensor. There are no specific restrictions on this parameter.

        The 'scores' parameter is a tensor that holds the logits for the classifier. It is expected to have twice the
        batch size of the input IDs tensor, with the first half of the batches corresponding to the conditional inputs
        and the second half corresponding to the unconditional inputs. The shape of the scores tensor should be
        (2 * input_ids.shape[0], ...). The method raises a ValueError if the shape of the scores tensor does not meet
        this requirement.

        The method splits the scores tensor into two parts: 'cond_logits' and 'uncond_logits'. 'cond_logits' represents
        the logits for the conditional inputs, while 'uncond_logits' represents the logits for the unconditional inputs.
        These logits are then processed using the guidance scale specified in the instance of the
        ClassifierFreeGuidanceLogitsProcessor class. The final processed scores are obtained by adding 'uncond_logits'
        to the difference between 'cond_logits' and 'uncond_logits', multiplied by the guidance scale.

        Note:
            This method assumes that the 'split' function splits the tensor into two parts along the first dimension (dim=0).
        """
        # simple check to make sure we have compatible batch sizes between our
        # logits scores (cond + uncond) and input ids (cond only)
        if scores.shape[0] != 2 * input_ids.shape[0]:
            raise ValueError(
                f"Logits should have twice the batch size of the input ids, the first half of batches corresponding to "
                f"the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got "
                f"batch size {scores.shape[0]} for the logits and {input_ids.shape[0]} for the input ids."
            )
        unguided_bsz = scores.shape[0] // 2
        cond_logits, uncond_logits = scores.split(unguided_bsz, dim=0)
        scores_processed = uncond_logits + (cond_logits - uncond_logits) * self.guidance_scale
        return scores_processed

mindnlp.transformers.generation.logits_process.ClassifierFreeGuidanceLogitsProcessor.__call__(input_ids, scores)

Performs processing on logits to generate processed scores for a classifier with free guidance.

PARAMETER DESCRIPTION
self

An instance of the ClassifierFreeGuidanceLogitsProcessor class.

TYPE: ClassifierFreeGuidanceLogitsProcessor

input_ids

A tensor containing input IDs for the classifier.

TYPE: Tensor

scores

A tensor containing logits for the classifier.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the processed scores for the classifier.

RAISES DESCRIPTION
ValueError

If the shape of the scores tensor does not meet the required conditions.

The 'call' method processes the logits to generate processed scores for a classifier with free guidance. It expects two parameters: 'input_ids' and 'scores'. The method returns a tensor of type 'mindspore.Tensor' which contains the processed scores.

The 'input_ids' parameter is a tensor that holds the input IDs for the classifier. It is used to determine the batch size and shape of the scores tensor. There are no specific restrictions on this parameter.

The 'scores' parameter is a tensor that holds the logits for the classifier. It is expected to have twice the batch size of the input IDs tensor, with the first half of the batches corresponding to the conditional inputs and the second half corresponding to the unconditional inputs. The shape of the scores tensor should be (2 * input_ids.shape[0], ...). The method raises a ValueError if the shape of the scores tensor does not meet this requirement.

The method splits the scores tensor into two parts: 'cond_logits' and 'uncond_logits'. 'cond_logits' represents the logits for the conditional inputs, while 'uncond_logits' represents the logits for the unconditional inputs. These logits are then processed using the guidance scale specified in the instance of the ClassifierFreeGuidanceLogitsProcessor class. The final processed scores are obtained by adding 'uncond_logits' to the difference between 'cond_logits' and 'uncond_logits', multiplied by the guidance scale.

Note

This method assumes that the 'split' function splits the tensor into two parts along the first dimension (dim=0).

Source code in mindnlp/transformers/generation/logits_process.py
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    Performs processing on logits to generate processed scores for a classifier with free guidance.

    Args:
        self (ClassifierFreeGuidanceLogitsProcessor): An instance of the ClassifierFreeGuidanceLogitsProcessor class.
        input_ids (mindspore.Tensor): A tensor containing input IDs for the classifier.
        scores (mindspore.Tensor): A tensor containing logits for the classifier.

    Returns:
        mindspore.Tensor: A tensor containing the processed scores for the classifier.

    Raises:
        ValueError: If the shape of the scores tensor does not meet the required conditions.

    The '__call__' method processes the logits to generate processed scores for a classifier with free guidance.
    It expects two parameters: 'input_ids' and 'scores'. The method returns a tensor of type 'mindspore.Tensor'
    which contains the processed scores.

    The 'input_ids' parameter is a tensor that holds the input IDs for the classifier. It is used to determine the
    batch size and shape of the scores tensor. There are no specific restrictions on this parameter.

    The 'scores' parameter is a tensor that holds the logits for the classifier. It is expected to have twice the
    batch size of the input IDs tensor, with the first half of the batches corresponding to the conditional inputs
    and the second half corresponding to the unconditional inputs. The shape of the scores tensor should be
    (2 * input_ids.shape[0], ...). The method raises a ValueError if the shape of the scores tensor does not meet
    this requirement.

    The method splits the scores tensor into two parts: 'cond_logits' and 'uncond_logits'. 'cond_logits' represents
    the logits for the conditional inputs, while 'uncond_logits' represents the logits for the unconditional inputs.
    These logits are then processed using the guidance scale specified in the instance of the
    ClassifierFreeGuidanceLogitsProcessor class. The final processed scores are obtained by adding 'uncond_logits'
    to the difference between 'cond_logits' and 'uncond_logits', multiplied by the guidance scale.

    Note:
        This method assumes that the 'split' function splits the tensor into two parts along the first dimension (dim=0).
    """
    # simple check to make sure we have compatible batch sizes between our
    # logits scores (cond + uncond) and input ids (cond only)
    if scores.shape[0] != 2 * input_ids.shape[0]:
        raise ValueError(
            f"Logits should have twice the batch size of the input ids, the first half of batches corresponding to "
            f"the conditional inputs, and the second half of batches corresponding to the unconditional inputs. Got "
            f"batch size {scores.shape[0]} for the logits and {input_ids.shape[0]} for the input ids."
        )
    unguided_bsz = scores.shape[0] // 2
    cond_logits, uncond_logits = scores.split(unguided_bsz, dim=0)
    scores_processed = uncond_logits + (cond_logits - uncond_logits) * self.guidance_scale
    return scores_processed

mindnlp.transformers.generation.logits_process.ClassifierFreeGuidanceLogitsProcessor.__init__(guidance_scale)

Initializes a new instance of the ClassifierFreeGuidanceLogitsProcessor class.

PARAMETER DESCRIPTION
self

The instance of the class.

guidance_scale

The scale of guidance for the classifier-free guidance processor. Must be greater than 1.

TYPE: int

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If the guidance_scale is not greater than 1, a ValueError is raised indicating the requirement for guidance_scale > 1 to use the classifier-free guidance processor.

Source code in mindnlp/transformers/generation/logits_process.py
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
def __init__(self, guidance_scale):
    """
    Initializes a new instance of the ClassifierFreeGuidanceLogitsProcessor class.

    Args:
        self: The instance of the class.
        guidance_scale (int): The scale of guidance for the classifier-free guidance processor. Must be greater than 1.

    Returns:
        None.

    Raises:
        ValueError: If the guidance_scale is not greater than 1, a ValueError is raised indicating the requirement
            for guidance_scale > 1 to use the classifier-free guidance processor.
    """
    if guidance_scale > 1:
        self.guidance_scale = guidance_scale
    else:
        raise ValueError(
            "Require guidance scale >1 to use the classifier free guidance processor, got guidance scale "
            f"{guidance_scale}."
        )

mindnlp.transformers.generation.logits_process.EncoderNoRepeatNGramLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that enforces no repetition of encoder input ids n-grams for the decoder ids. See ParlAI.

PARAMETER DESCRIPTION
encoder_ngram_size

All ngrams of size ngram_size can only occur within the encoder input ids.

TYPE: `int`

encoder_input_ids

The encoder_input_ids that should not be repeated within the decoder ids.

TYPE: `int`

Source code in mindnlp/transformers/generation/logits_process.py
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
class EncoderNoRepeatNGramLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that enforces no repetition of encoder input ids n-grams for the decoder ids. See
    [ParlAI](https://github.com/facebookresearch/ParlAI/blob/master/parlai/core/torch_generator_agent.py#L1350).

    Args:
        encoder_ngram_size (`int`):
            All ngrams of size `ngram_size` can only occur within the encoder input ids.
        encoder_input_ids (`int`):
            The encoder_input_ids that should not be repeated within the decoder ids.
    """
    def __init__(self, encoder_ngram_size: int, encoder_input_ids: mindspore.Tensor):
        """
        Initializes the EncoderNoRepeatNGramLogitsProcessor.

        Args:
            encoder_ngram_size (int): The size of the n-grams for encoding. Must be a strictly positive integer.
            encoder_input_ids (mindspore.Tensor): The input tensor for the encoder. If it has shape (N,),
                it will be unsqueezed to shape (1, N).

        Returns:
            None.

        Raises:
            ValueError: If `encoder_ngram_size` is not a strictly positive integer.
        """
        if not isinstance(encoder_ngram_size, int) or encoder_ngram_size <= 0:
            raise ValueError(
                f"`encoder_ngram_size` has to be a strictly positive integer, but is {encoder_ngram_size}"
            )
        self.ngram_size = encoder_ngram_size
        if len(encoder_input_ids.shape) == 1:
            encoder_input_ids = encoder_input_ids.unsqueeze(0)
        self.batch_size = encoder_input_ids.shape[0]
        self.generated_ngrams = _get_ngrams(encoder_ngram_size, encoder_input_ids, self.batch_size)

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method processes logits to prevent generation of n-grams that have already appeared in the input sequence.

        Args:
            self (EncoderNoRepeatNGramLogitsProcessor): An instance of the EncoderNoRepeatNGramLogitsProcessor class.
            input_ids (mindspore.Tensor): A tensor containing the input token IDs.
                Shape: (batch_size, sequence_length)
            scores (mindspore.Tensor): A tensor containing the logits scores for each token.
                Shape: (num_hypos, vocab_size)

        Returns:
            mindspore.Tensor: A tensor containing the updated logits scores after processing.
                Shape: (num_hypos, vocab_size)

        Raises:
            TypeError: If the input_ids or scores parameters are not of type mindspore.Tensor.
            ValueError: If the dimensions of input_ids and scores do not match the expected shapes.
        """
        # B x num_beams
        num_hypos = scores.shape[0]
        num_beams = num_hypos // self.batch_size
        cur_len = input_ids.shape[-1]
        banned_batch_tokens = [
            _get_generated_ngrams(
                self.generated_ngrams[hypo_idx // num_beams], input_ids[hypo_idx], self.ngram_size, cur_len
            )
            for hypo_idx in range(num_hypos)
        ]

        for i, banned_tokens in enumerate(banned_batch_tokens):
            scores[i, banned_tokens] = -float("inf")

        return scores

mindnlp.transformers.generation.logits_process.EncoderNoRepeatNGramLogitsProcessor.__call__(input_ids, scores)

This method processes logits to prevent generation of n-grams that have already appeared in the input sequence.

PARAMETER DESCRIPTION
self

An instance of the EncoderNoRepeatNGramLogitsProcessor class.

TYPE: EncoderNoRepeatNGramLogitsProcessor

input_ids

A tensor containing the input token IDs. Shape: (batch_size, sequence_length)

TYPE: Tensor

scores

A tensor containing the logits scores for each token. Shape: (num_hypos, vocab_size)

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the updated logits scores after processing. Shape: (num_hypos, vocab_size)

RAISES DESCRIPTION
TypeError

If the input_ids or scores parameters are not of type mindspore.Tensor.

ValueError

If the dimensions of input_ids and scores do not match the expected shapes.

Source code in mindnlp/transformers/generation/logits_process.py
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method processes logits to prevent generation of n-grams that have already appeared in the input sequence.

    Args:
        self (EncoderNoRepeatNGramLogitsProcessor): An instance of the EncoderNoRepeatNGramLogitsProcessor class.
        input_ids (mindspore.Tensor): A tensor containing the input token IDs.
            Shape: (batch_size, sequence_length)
        scores (mindspore.Tensor): A tensor containing the logits scores for each token.
            Shape: (num_hypos, vocab_size)

    Returns:
        mindspore.Tensor: A tensor containing the updated logits scores after processing.
            Shape: (num_hypos, vocab_size)

    Raises:
        TypeError: If the input_ids or scores parameters are not of type mindspore.Tensor.
        ValueError: If the dimensions of input_ids and scores do not match the expected shapes.
    """
    # B x num_beams
    num_hypos = scores.shape[0]
    num_beams = num_hypos // self.batch_size
    cur_len = input_ids.shape[-1]
    banned_batch_tokens = [
        _get_generated_ngrams(
            self.generated_ngrams[hypo_idx // num_beams], input_ids[hypo_idx], self.ngram_size, cur_len
        )
        for hypo_idx in range(num_hypos)
    ]

    for i, banned_tokens in enumerate(banned_batch_tokens):
        scores[i, banned_tokens] = -float("inf")

    return scores

mindnlp.transformers.generation.logits_process.EncoderNoRepeatNGramLogitsProcessor.__init__(encoder_ngram_size, encoder_input_ids)

Initializes the EncoderNoRepeatNGramLogitsProcessor.

PARAMETER DESCRIPTION
encoder_ngram_size

The size of the n-grams for encoding. Must be a strictly positive integer.

TYPE: int

encoder_input_ids

The input tensor for the encoder. If it has shape (N,), it will be unsqueezed to shape (1, N).

TYPE: Tensor

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If encoder_ngram_size is not a strictly positive integer.

Source code in mindnlp/transformers/generation/logits_process.py
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
def __init__(self, encoder_ngram_size: int, encoder_input_ids: mindspore.Tensor):
    """
    Initializes the EncoderNoRepeatNGramLogitsProcessor.

    Args:
        encoder_ngram_size (int): The size of the n-grams for encoding. Must be a strictly positive integer.
        encoder_input_ids (mindspore.Tensor): The input tensor for the encoder. If it has shape (N,),
            it will be unsqueezed to shape (1, N).

    Returns:
        None.

    Raises:
        ValueError: If `encoder_ngram_size` is not a strictly positive integer.
    """
    if not isinstance(encoder_ngram_size, int) or encoder_ngram_size <= 0:
        raise ValueError(
            f"`encoder_ngram_size` has to be a strictly positive integer, but is {encoder_ngram_size}"
        )
    self.ngram_size = encoder_ngram_size
    if len(encoder_input_ids.shape) == 1:
        encoder_input_ids = encoder_input_ids.unsqueeze(0)
    self.batch_size = encoder_input_ids.shape[0]
    self.generated_ngrams = _get_ngrams(encoder_ngram_size, encoder_input_ids, self.batch_size)

mindnlp.transformers.generation.logits_process.EncoderRepetitionPenaltyLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] enforcing an exponential penalty on tokens that are not in the original input.

PARAMETER DESCRIPTION
hallucination_penalty

The parameter for hallucination penalty. 1.0 means no penalty.

TYPE: `float`

encoder_input_ids

The encoder_input_ids that should not be repeated within the decoder ids.

TYPE: `mindspore.Tensor`

Source code in mindnlp/transformers/generation/logits_process.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
class EncoderRepetitionPenaltyLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] enforcing an exponential penalty on tokens that are not in the original input.

    Args:
        hallucination_penalty (`float`):
            The parameter for hallucination penalty. 1.0 means no penalty.
        encoder_input_ids (`mindspore.Tensor`):
            The encoder_input_ids that should not be repeated within the decoder ids.
    """
    def __init__(self, penalty: float, encoder_input_ids: mindspore.Tensor):
        """
        Initializes an instance of the EncoderRepetitionPenaltyLogitsProcessor class.

        Args:
            self: The instance of the class.
            penalty (float): The penalty value for repetition. Must be a strictly positive float.
            encoder_input_ids (mindspore.Tensor): The input tensor of the encoder.

        Returns:
            None.

        Raises:
            ValueError: If `penalty` is not a strictly positive float.

        """
        if not isinstance(penalty, float) or (penalty <= 0):
            raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}")

        self.penalty = 1 / penalty
        self.encoder_input_ids = encoder_input_ids

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method calculates and applies repetition penalty to the logits based on the input scores.

        Args:
            self (EncoderRepetitionPenaltyLogitsProcessor): The instance of the EncoderRepetitionPenaltyLogitsProcessor
                class.
            input_ids (mindspore.Tensor): The input tensor containing the token ids.
            scores (mindspore.Tensor): The input tensor containing the original scores.

        Returns:
            mindspore.Tensor: Returns a tensor with repetition penalty applied to the logits.

        Raises:
            ValueError: If the dimensions of input_ids and scores do not match.
            TypeError: If the input_ids or scores are not instances of mindspore.Tensor.
            RuntimeError: If there is an issue with the scatter operation during processing.
        """
        score = ops.gather(scores, 1, self.encoder_input_ids)

        # if score < 0 then repetition penalty has to be multiplied to reduce the previous token probability
        score = ops.where(score < 0, score * self.penalty, score / self.penalty)

        scores = ops.scatter(scores, 1, self.encoder_input_ids, score)
        return scores

mindnlp.transformers.generation.logits_process.EncoderRepetitionPenaltyLogitsProcessor.__call__(input_ids, scores)

This method calculates and applies repetition penalty to the logits based on the input scores.

PARAMETER DESCRIPTION
self

The instance of the EncoderRepetitionPenaltyLogitsProcessor class.

TYPE: EncoderRepetitionPenaltyLogitsProcessor

input_ids

The input tensor containing the token ids.

TYPE: Tensor

scores

The input tensor containing the original scores.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: Returns a tensor with repetition penalty applied to the logits.

RAISES DESCRIPTION
ValueError

If the dimensions of input_ids and scores do not match.

TypeError

If the input_ids or scores are not instances of mindspore.Tensor.

RuntimeError

If there is an issue with the scatter operation during processing.

Source code in mindnlp/transformers/generation/logits_process.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method calculates and applies repetition penalty to the logits based on the input scores.

    Args:
        self (EncoderRepetitionPenaltyLogitsProcessor): The instance of the EncoderRepetitionPenaltyLogitsProcessor
            class.
        input_ids (mindspore.Tensor): The input tensor containing the token ids.
        scores (mindspore.Tensor): The input tensor containing the original scores.

    Returns:
        mindspore.Tensor: Returns a tensor with repetition penalty applied to the logits.

    Raises:
        ValueError: If the dimensions of input_ids and scores do not match.
        TypeError: If the input_ids or scores are not instances of mindspore.Tensor.
        RuntimeError: If there is an issue with the scatter operation during processing.
    """
    score = ops.gather(scores, 1, self.encoder_input_ids)

    # if score < 0 then repetition penalty has to be multiplied to reduce the previous token probability
    score = ops.where(score < 0, score * self.penalty, score / self.penalty)

    scores = ops.scatter(scores, 1, self.encoder_input_ids, score)
    return scores

mindnlp.transformers.generation.logits_process.EncoderRepetitionPenaltyLogitsProcessor.__init__(penalty, encoder_input_ids)

Initializes an instance of the EncoderRepetitionPenaltyLogitsProcessor class.

PARAMETER DESCRIPTION
self

The instance of the class.

penalty

The penalty value for repetition. Must be a strictly positive float.

TYPE: float

encoder_input_ids

The input tensor of the encoder.

TYPE: Tensor

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If penalty is not a strictly positive float.

Source code in mindnlp/transformers/generation/logits_process.py
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
def __init__(self, penalty: float, encoder_input_ids: mindspore.Tensor):
    """
    Initializes an instance of the EncoderRepetitionPenaltyLogitsProcessor class.

    Args:
        self: The instance of the class.
        penalty (float): The penalty value for repetition. Must be a strictly positive float.
        encoder_input_ids (mindspore.Tensor): The input tensor of the encoder.

    Returns:
        None.

    Raises:
        ValueError: If `penalty` is not a strictly positive float.

    """
    if not isinstance(penalty, float) or (penalty <= 0):
        raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}")

    self.penalty = 1 / penalty
    self.encoder_input_ids = encoder_input_ids

mindnlp.transformers.generation.logits_process.EpsilonLogitsWarper

Bases: LogitsWarper

[LogitsWarper] that performs epsilon-sampling, i.e. restricting to tokens with prob >= epsilon. Takes the largest min_tokens_to_keep tokens if no tokens satisfy this constraint. See Truncation Sampling as Language Model Desmoothing for more information.

PARAMETER DESCRIPTION
epsilon

If set to > 0, only the most tokens with probabilities epsilon or higher are kept for generation.

TYPE: `float`

filter_value

All filtered values will be set to this float value.

TYPE: `float`, *optional*, defaults to -inf DEFAULT: -float('Inf')

min_tokens_to_keep

Minimum number of tokens that cannot be filtered.

TYPE: `int`, *optional*, defaults to 1 DEFAULT: 1

Example
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
...
>>> set_seed(0)
>>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
...
>>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
...
>>> # With sampling, the output is unexpected -- sometimes too unexpected.
>>> outputs = model.generate(**inputs, do_sample=True)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2
...
>>> # With epsilon sampling, the output gets restricted to high-probability tokens. Note that this is similar to
>>> # Top P sampling, which restricts tokens based on their cumulative probability.
>>> # Pro tip: The paper recomends using `epsilon_cutoff` values between 3e-4 and 9e-4
>>> outputs = model.generate(**inputs, do_sample=True, epsilon_cutoff=0.1)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
Source code in mindnlp/transformers/generation/logits_process.py
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
class EpsilonLogitsWarper(LogitsWarper):
    r"""
    [`LogitsWarper`] that performs epsilon-sampling, i.e. restricting to tokens with `prob >= epsilon`. Takes the
    largest min_tokens_to_keep tokens if no tokens satisfy this constraint. See [Truncation Sampling as Language Model
    Desmoothing](https://arxiv.org/abs/2210.15191) for more information.

    Args:
        epsilon (`float`):
            If set to > 0, only the most tokens with probabilities `epsilon` or higher are kept for generation.
        filter_value (`float`, *optional*, defaults to -inf):
            All filtered values will be set to this float value.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Minimum number of tokens that cannot be filtered.

    Example:
        ```python
        >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
        ...
        >>> set_seed(0)
        >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
        >>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
        ...
        >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
        ...
        >>> # With sampling, the output is unexpected -- sometimes too unexpected.
        >>> outputs = model.generate(**inputs, do_sample=True)
        >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
        A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2
        ...
        >>> # With epsilon sampling, the output gets restricted to high-probability tokens. Note that this is similar to
        >>> # Top P sampling, which restricts tokens based on their cumulative probability.
        >>> # Pro tip: The paper recomends using `epsilon_cutoff` values between 3e-4 and 9e-4
        >>> outputs = model.generate(**inputs, do_sample=True, epsilon_cutoff=0.1)
        >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
        A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
        ```
    """
    def __init__(self, epsilon: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
        """
        Initializes an instance of the EpsilonLogitsWarper class.

        Args:
            self: The instance of the class.
            epsilon (float): The value used for epsilon cutoff.
                It should be a float greater than 0 and less than 1.
            filter_value (float, optional): The filter value for the warping operation.
                Defaults to negative infinity.
            min_tokens_to_keep (int, optional): The minimum number of tokens to keep.
                It should be a strictly positive integer.

        Returns:
            None.

        Raises:
            ValueError: If epsilon is not within the range (0, 1) or if min_tokens_to_keep is less than 1.
        """
        epsilon = float(epsilon)
        if epsilon <= 0 or epsilon >= 1:
            raise ValueError(f"`epsilon_cutoff` has to be a float > 0 and < 1, but is {epsilon}")

        min_tokens_to_keep = int(min_tokens_to_keep)
        if min_tokens_to_keep < 1:
            raise ValueError(
                f"`min_tokens_to_keep` has to be a strictly positive integer, but is {min_tokens_to_keep}"
            )

        self.epsilon = epsilon
        self.filter_value = filter_value
        self.min_tokens_to_keep = min_tokens_to_keep

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        '''
        This method takes three parameters:
        Args:
            self (EpsilonLogitsWarper): The instance of the EpsilonLogitsWarper class.
            input_ids (mindspore.Tensor): The input tensor containing the IDs.
            scores (mindspore.Tensor): The input tensor containing the scores.

        Returns:
            mindspore.Tensor: A tensor containing the modified scores after applying the epsilon logits warping.

        Raises:
            None.
        '''
        # Determine which indices to remove
        probabilities = ops.softmax(scores, dim=-1)
        indices_to_remove = probabilities < self.epsilon

        # Keep the words with the 'min_tokens_to_keep'-highest probabilities
        top_k = min(self.min_tokens_to_keep, scores.size(-1))  # Safety check
        indices_to_remove = indices_to_remove & (scores < ops.topk(scores, top_k)[0][..., -1, None])

        scores = scores.masked_fill(indices_to_remove, self.filter_value)
        return scores

mindnlp.transformers.generation.logits_process.EpsilonLogitsWarper.__call__(input_ids, scores)

This method takes three parameters: Args: self (EpsilonLogitsWarper): The instance of the EpsilonLogitsWarper class. input_ids (mindspore.Tensor): The input tensor containing the IDs. scores (mindspore.Tensor): The input tensor containing the scores.

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the modified scores after applying the epsilon logits warping.

Source code in mindnlp/transformers/generation/logits_process.py
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    '''
    This method takes three parameters:
    Args:
        self (EpsilonLogitsWarper): The instance of the EpsilonLogitsWarper class.
        input_ids (mindspore.Tensor): The input tensor containing the IDs.
        scores (mindspore.Tensor): The input tensor containing the scores.

    Returns:
        mindspore.Tensor: A tensor containing the modified scores after applying the epsilon logits warping.

    Raises:
        None.
    '''
    # Determine which indices to remove
    probabilities = ops.softmax(scores, dim=-1)
    indices_to_remove = probabilities < self.epsilon

    # Keep the words with the 'min_tokens_to_keep'-highest probabilities
    top_k = min(self.min_tokens_to_keep, scores.size(-1))  # Safety check
    indices_to_remove = indices_to_remove & (scores < ops.topk(scores, top_k)[0][..., -1, None])

    scores = scores.masked_fill(indices_to_remove, self.filter_value)
    return scores

mindnlp.transformers.generation.logits_process.EpsilonLogitsWarper.__init__(epsilon, filter_value=-float('Inf'), min_tokens_to_keep=1)

Initializes an instance of the EpsilonLogitsWarper class.

PARAMETER DESCRIPTION
self

The instance of the class.

epsilon

The value used for epsilon cutoff. It should be a float greater than 0 and less than 1.

TYPE: float

filter_value

The filter value for the warping operation. Defaults to negative infinity.

TYPE: float DEFAULT: -float('Inf')

min_tokens_to_keep

The minimum number of tokens to keep. It should be a strictly positive integer.

TYPE: int DEFAULT: 1

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If epsilon is not within the range (0, 1) or if min_tokens_to_keep is less than 1.

Source code in mindnlp/transformers/generation/logits_process.py
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
def __init__(self, epsilon: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
    """
    Initializes an instance of the EpsilonLogitsWarper class.

    Args:
        self: The instance of the class.
        epsilon (float): The value used for epsilon cutoff.
            It should be a float greater than 0 and less than 1.
        filter_value (float, optional): The filter value for the warping operation.
            Defaults to negative infinity.
        min_tokens_to_keep (int, optional): The minimum number of tokens to keep.
            It should be a strictly positive integer.

    Returns:
        None.

    Raises:
        ValueError: If epsilon is not within the range (0, 1) or if min_tokens_to_keep is less than 1.
    """
    epsilon = float(epsilon)
    if epsilon <= 0 or epsilon >= 1:
        raise ValueError(f"`epsilon_cutoff` has to be a float > 0 and < 1, but is {epsilon}")

    min_tokens_to_keep = int(min_tokens_to_keep)
    if min_tokens_to_keep < 1:
        raise ValueError(
            f"`min_tokens_to_keep` has to be a strictly positive integer, but is {min_tokens_to_keep}"
        )

    self.epsilon = epsilon
    self.filter_value = filter_value
    self.min_tokens_to_keep = min_tokens_to_keep

mindnlp.transformers.generation.logits_process.EtaLogitsWarper

Bases: LogitsWarper

[LogitsWarper] that performs eta-sampling, a technique to filter out tokens with probabilities below a dynamic cutoff value, eta, which is calculated based on a combination of the hyperparameter epsilon and the entropy of the token probabilities, i.e. eta := min(epsilon, sqrt(epsilon * e^-entropy(probabilities))). Takes the largest min_tokens_to_keep tokens if no tokens satisfy this constraint. It addresses the issue of poor quality in long samples of text generated by neural language models leading to more coherent and fluent text. See Truncation Sampling as Language Model Desmoothing for more information. Note: do_sample must be set to True for this LogitsWarper to work.

PARAMETER DESCRIPTION
epsilon

A float value in the range (0, 1). Hyperparameter used to calculate the dynamic cutoff value, eta. The suggested values from the paper ranges from 3e-4 to 4e-3 depending on the size of the model.

TYPE: `float`

filter_value

All values that are found to be below the dynamic cutoff value, eta, are set to this float value. This parameter is useful when logits need to be modified for very low probability tokens that should be excluded from generation entirely.

TYPE: `float`, *optional*, defaults to -inf DEFAULT: -float('Inf')

min_tokens_to_keep

Specifies the minimum number of tokens that must be kept for generation, regardless of their probabilities. For example, if min_tokens_to_keep is set to 1, at least one token will always be kept for generation, even if all tokens have probabilities below the cutoff eta.

TYPE: `int`, *optional*, defaults to 1 DEFAULT: 1

Example
>>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
...
>>> set_seed(0)
>>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
...
>>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
...
>>> # With sampling, the output is unexpected -- sometimes too unexpected.
>>> outputs = model.generate(**inputs, do_sample=True)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2
...
>>> # With eta sampling, the output gets restricted to high-probability tokens. You can see it as a dynamic form of
>>> # epsilon sampling that adapts its cutoff probability based on the entropy (high entropy = lower cutoff).
>>> # Pro tip: The paper recomends using `eta_cutoff` values between 3e-4 to 4e-3
>>> outputs = model.generate(**inputs, do_sample=True, eta_cutoff=0.1)
>>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
Source code in mindnlp/transformers/generation/logits_process.py
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
class EtaLogitsWarper(LogitsWarper):
    r"""
    [`LogitsWarper`] that performs eta-sampling, a technique to filter out tokens with probabilities below a dynamic
    cutoff value, `eta`, which is calculated based on a combination of the hyperparameter `epsilon` and the entropy of
    the token probabilities, i.e. `eta := min(epsilon, sqrt(epsilon * e^-entropy(probabilities)))`. Takes the largest
    min_tokens_to_keep tokens if no tokens satisfy this constraint. It addresses the issue of poor quality in long
    samples of text generated by neural language models leading to more coherent and fluent text. See [Truncation
    Sampling as Language Model Desmoothing](https://arxiv.org/abs/2210.15191) for more information. Note: `do_sample`
    must be set to `True` for this `LogitsWarper` to work.


    Args:
        epsilon (`float`):
            A float value in the range (0, 1). Hyperparameter used to calculate the dynamic cutoff value, `eta`. The
            suggested values from the paper ranges from 3e-4 to 4e-3 depending on the size of the model.
        filter_value (`float`, *optional*, defaults to -inf):
            All values that are found to be below the dynamic cutoff value, `eta`, are set to this float value. This
            parameter is useful when logits need to be modified for very low probability tokens that should be excluded
            from generation entirely.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Specifies the minimum number of tokens that must be kept for generation, regardless of their probabilities.
            For example, if `min_tokens_to_keep` is set to 1, at least one token will always be kept for generation,
            even if all tokens have probabilities below the cutoff `eta`.

    Example:
        ```python
        >>> from transformers import AutoTokenizer, AutoModelForCausalLM, set_seed
        ...
        >>> set_seed(0)
        >>> model = AutoModelForCausalLM.from_pretrained("distilgpt2")
        >>> tokenizer = AutoTokenizer.from_pretrained("distilgpt2")
        ...
        >>> inputs = tokenizer("A sequence: 1, 2", return_tensors="pt")
        ...
        >>> # With sampling, the output is unexpected -- sometimes too unexpected.
        >>> outputs = model.generate(**inputs, do_sample=True)
        >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
        A sequence: 1, 2, 0, 2, 2. 2, 2, 2, 2
        ...
        >>> # With eta sampling, the output gets restricted to high-probability tokens. You can see it as a dynamic form of
        >>> # epsilon sampling that adapts its cutoff probability based on the entropy (high entropy = lower cutoff).
        >>> # Pro tip: The paper recomends using `eta_cutoff` values between 3e-4 to 4e-3
        >>> outputs = model.generate(**inputs, do_sample=True, eta_cutoff=0.1)
        >>> print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
        A sequence: 1, 2, 3, 4, 5, 6, 7, 8, 9
        ```
    """
    def __init__(self, epsilon: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
        """Initialize a new instance of the EtaLogitsWarper class.

        Args:
            self: The object itself.
            epsilon (float): The value to be used as epsilon. It should be a float between 0 and 1.
            filter_value (float, optional): The value to be used for filtering. Defaults to -float('Inf').
            min_tokens_to_keep (int, optional): The minimum number of tokens to keep. Defaults to 1.

        Returns:
            None.

        Raises:
            ValueError: If epsilon is not a float between 0 and 1.
            ValueError: If min_tokens_to_keep is not a positive integer.
        """
        epsilon = float(epsilon)
        if epsilon <= 0 or epsilon >= 1:
            raise ValueError(f"`eta_cutoff` has to be a float > 0 and < 1, but is {epsilon}")

        min_tokens_to_keep = int(min_tokens_to_keep)
        if min_tokens_to_keep < 1:
            raise ValueError(
                f"`min_tokens_to_keep` has to be a strictly positive integer, but is {min_tokens_to_keep}"
            )

        self.epsilon = mindspore.tensor(epsilon, mindspore.float32)
        self.filter_value = filter_value
        self.min_tokens_to_keep = min_tokens_to_keep

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method '__call__' in the class 'EtaLogitsWarper' takes three parameters:

        Args:
            self: The instance of the class.
            input_ids (mindspore.Tensor): The input tensor containing the IDs.
            scores (mindspore.Tensor): The input tensor containing the scores.

        Returns:
            mindspore.Tensor: Returns a tensor after applying certain operations on the input scores.

        Raises:
            None
        """
        # Calculate the adaptive cutoff
        probabilities = scores.softmax(dim=-1)
        entropy = mindspore.nn.probability.distribution.Categorical().entropy(scores)
        eta = ops.min(self.epsilon, ops.sqrt(self.epsilon) * ops.exp(-entropy))[..., None]
        indices_to_remove = probabilities < eta

        # Keep the words with the 'min_tokens_to_keep'-highest probabilities
        top_k = min(self.min_tokens_to_keep, scores.size(-1))  # Safety check
        indices_to_remove = indices_to_remove & (scores < ops.topk(scores, top_k)[0][..., -1, None])

        scores = scores.masked_fill(indices_to_remove, self.filter_value)
        return scores

mindnlp.transformers.generation.logits_process.EtaLogitsWarper.__call__(input_ids, scores)

This method 'call' in the class 'EtaLogitsWarper' takes three parameters:

PARAMETER DESCRIPTION
self

The instance of the class.

input_ids

The input tensor containing the IDs.

TYPE: Tensor

scores

The input tensor containing the scores.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: Returns a tensor after applying certain operations on the input scores.

Source code in mindnlp/transformers/generation/logits_process.py
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method '__call__' in the class 'EtaLogitsWarper' takes three parameters:

    Args:
        self: The instance of the class.
        input_ids (mindspore.Tensor): The input tensor containing the IDs.
        scores (mindspore.Tensor): The input tensor containing the scores.

    Returns:
        mindspore.Tensor: Returns a tensor after applying certain operations on the input scores.

    Raises:
        None
    """
    # Calculate the adaptive cutoff
    probabilities = scores.softmax(dim=-1)
    entropy = mindspore.nn.probability.distribution.Categorical().entropy(scores)
    eta = ops.min(self.epsilon, ops.sqrt(self.epsilon) * ops.exp(-entropy))[..., None]
    indices_to_remove = probabilities < eta

    # Keep the words with the 'min_tokens_to_keep'-highest probabilities
    top_k = min(self.min_tokens_to_keep, scores.size(-1))  # Safety check
    indices_to_remove = indices_to_remove & (scores < ops.topk(scores, top_k)[0][..., -1, None])

    scores = scores.masked_fill(indices_to_remove, self.filter_value)
    return scores

mindnlp.transformers.generation.logits_process.EtaLogitsWarper.__init__(epsilon, filter_value=-float('Inf'), min_tokens_to_keep=1)

Initialize a new instance of the EtaLogitsWarper class.

PARAMETER DESCRIPTION
self

The object itself.

epsilon

The value to be used as epsilon. It should be a float between 0 and 1.

TYPE: float

filter_value

The value to be used for filtering. Defaults to -float('Inf').

TYPE: float DEFAULT: -float('Inf')

min_tokens_to_keep

The minimum number of tokens to keep. Defaults to 1.

TYPE: int DEFAULT: 1

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If epsilon is not a float between 0 and 1.

ValueError

If min_tokens_to_keep is not a positive integer.

Source code in mindnlp/transformers/generation/logits_process.py
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
def __init__(self, epsilon: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
    """Initialize a new instance of the EtaLogitsWarper class.

    Args:
        self: The object itself.
        epsilon (float): The value to be used as epsilon. It should be a float between 0 and 1.
        filter_value (float, optional): The value to be used for filtering. Defaults to -float('Inf').
        min_tokens_to_keep (int, optional): The minimum number of tokens to keep. Defaults to 1.

    Returns:
        None.

    Raises:
        ValueError: If epsilon is not a float between 0 and 1.
        ValueError: If min_tokens_to_keep is not a positive integer.
    """
    epsilon = float(epsilon)
    if epsilon <= 0 or epsilon >= 1:
        raise ValueError(f"`eta_cutoff` has to be a float > 0 and < 1, but is {epsilon}")

    min_tokens_to_keep = int(min_tokens_to_keep)
    if min_tokens_to_keep < 1:
        raise ValueError(
            f"`min_tokens_to_keep` has to be a strictly positive integer, but is {min_tokens_to_keep}"
        )

    self.epsilon = mindspore.tensor(epsilon, mindspore.float32)
    self.filter_value = filter_value
    self.min_tokens_to_keep = min_tokens_to_keep

mindnlp.transformers.generation.logits_process.ExponentialDecayLengthPenalty

Bases: LogitsProcessor

[LogitsProcessor] that exponentially increases the score of the eos_token_id after regulation_start has been reached.

PARAMETER DESCRIPTION
exponential_decay_length_penalty

This tuple shall consist of: (start_index, decay_factor) where start_index indicates where penalty starts and decay_factor represents the factor of exponential decay

TYPE: `tuple(int, float)`, *optional*

eos_token_id

The id of the end-of-sequence token. Optionally, use a list to set multiple end-of-sequence tokens.

TYPE: `Union[int, List[int]]`

input_ids_seq_length

The length of the input sequence.

TYPE: `int`

Source code in mindnlp/transformers/generation/logits_process.py
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
class ExponentialDecayLengthPenalty(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that exponentially increases the score of the eos_token_id after regulation_start has been
    reached.

    Args:
        exponential_decay_length_penalty (`tuple(int, float)`, *optional*):
            This tuple shall consist of: `(start_index, decay_factor)` where `start_index` indicates where penalty
            starts and `decay_factor` represents the factor of exponential decay
        eos_token_id (`Union[int, List[int]]`):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
        input_ids_seq_length (`int`):
            The length of the input sequence.
    """
    def __init__(
            self, exponential_decay_length_penalty: Tuple, eos_token_id: Union[int, List[int]],
            input_ids_seq_length: int
    ):
        """
        Initializes an instance of the ExponentialDecayLengthPenalty class with the specified parameters.

        Args:
            self: The instance of the class.
            exponential_decay_length_penalty (Tuple):
                A tuple containing two elements:

                - The start point for the exponential decay length penalty regulation.
                - The factor for the exponential decay length penalty regulation.
            eos_token_id (Union[int, List[int]]): The ID or list of IDs representing the end-of-sequence token(s).
            input_ids_seq_length (int): The length of the input sequence.

        Returns:
            None.

        Raises:
            TypeError: If the 'exponential_decay_length_penalty' parameter is not a tuple.
            ValueError: If the 'exponential_decay_length_penalty' tuple does not contain exactly two elements.
            ValueError: If the 'input_ids_seq_length' parameter is not an integer.
            ValueError: If the 'eos_token_id' parameter is not an integer or a list of integers.
        """
        self.regulation_start = exponential_decay_length_penalty[0] + input_ids_seq_length
        self.regulation_factor = exponential_decay_length_penalty[1]
        if isinstance(eos_token_id, int):
            eos_token_id = [eos_token_id]
        self.eos_token_id = eos_token_id

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method calculates exponential decay length penalty for input scores based on the length of input_ids.

        Args:
            self (ExponentialDecayLengthPenalty): An instance of the ExponentialDecayLengthPenalty class.
            input_ids (mindspore.Tensor): A tensor containing input IDs.
                This tensor represents the input sequence for which the length penalty is to be applied.
            scores (mindspore.Tensor): A tensor containing scores to be adjusted based on the length of input_ids.
                The scores represent the probability distribution for each token in the input sequence.

        Returns:
            mindspore.Tensor: A tensor containing the adjusted scores after applying the exponential decay length penalty.
                The returned tensor has the same shape as the input 'scores'.

        Raises:
            ValueError: If the length of input_ids is not consistent with the shape of scores.
            TypeError: If input_ids or scores are not of type mindspore.Tensor.
        """
        cur_len = input_ids.shape[-1]
        if cur_len > self.regulation_start:
            for i in self.eos_token_id:
                scores[:, i] = scores[:, i] * pow(self.regulation_factor, cur_len - self.regulation_start)
        return scores

mindnlp.transformers.generation.logits_process.ExponentialDecayLengthPenalty.__call__(input_ids, scores)

This method calculates exponential decay length penalty for input scores based on the length of input_ids.

PARAMETER DESCRIPTION
self

An instance of the ExponentialDecayLengthPenalty class.

TYPE: ExponentialDecayLengthPenalty

input_ids

A tensor containing input IDs. This tensor represents the input sequence for which the length penalty is to be applied.

TYPE: Tensor

scores

A tensor containing scores to be adjusted based on the length of input_ids. The scores represent the probability distribution for each token in the input sequence.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the adjusted scores after applying the exponential decay length penalty. The returned tensor has the same shape as the input 'scores'.

RAISES DESCRIPTION
ValueError

If the length of input_ids is not consistent with the shape of scores.

TypeError

If input_ids or scores are not of type mindspore.Tensor.

Source code in mindnlp/transformers/generation/logits_process.py
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method calculates exponential decay length penalty for input scores based on the length of input_ids.

    Args:
        self (ExponentialDecayLengthPenalty): An instance of the ExponentialDecayLengthPenalty class.
        input_ids (mindspore.Tensor): A tensor containing input IDs.
            This tensor represents the input sequence for which the length penalty is to be applied.
        scores (mindspore.Tensor): A tensor containing scores to be adjusted based on the length of input_ids.
            The scores represent the probability distribution for each token in the input sequence.

    Returns:
        mindspore.Tensor: A tensor containing the adjusted scores after applying the exponential decay length penalty.
            The returned tensor has the same shape as the input 'scores'.

    Raises:
        ValueError: If the length of input_ids is not consistent with the shape of scores.
        TypeError: If input_ids or scores are not of type mindspore.Tensor.
    """
    cur_len = input_ids.shape[-1]
    if cur_len > self.regulation_start:
        for i in self.eos_token_id:
            scores[:, i] = scores[:, i] * pow(self.regulation_factor, cur_len - self.regulation_start)
    return scores

mindnlp.transformers.generation.logits_process.ExponentialDecayLengthPenalty.__init__(exponential_decay_length_penalty, eos_token_id, input_ids_seq_length)

Initializes an instance of the ExponentialDecayLengthPenalty class with the specified parameters.

PARAMETER DESCRIPTION
self

The instance of the class.

exponential_decay_length_penalty

A tuple containing two elements:

  • The start point for the exponential decay length penalty regulation.
  • The factor for the exponential decay length penalty regulation.

TYPE: Tuple

eos_token_id

The ID or list of IDs representing the end-of-sequence token(s).

TYPE: Union[int, List[int]]

input_ids_seq_length

The length of the input sequence.

TYPE: int

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
TypeError

If the 'exponential_decay_length_penalty' parameter is not a tuple.

ValueError

If the 'exponential_decay_length_penalty' tuple does not contain exactly two elements.

ValueError

If the 'input_ids_seq_length' parameter is not an integer.

ValueError

If the 'eos_token_id' parameter is not an integer or a list of integers.

Source code in mindnlp/transformers/generation/logits_process.py
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
def __init__(
        self, exponential_decay_length_penalty: Tuple, eos_token_id: Union[int, List[int]],
        input_ids_seq_length: int
):
    """
    Initializes an instance of the ExponentialDecayLengthPenalty class with the specified parameters.

    Args:
        self: The instance of the class.
        exponential_decay_length_penalty (Tuple):
            A tuple containing two elements:

            - The start point for the exponential decay length penalty regulation.
            - The factor for the exponential decay length penalty regulation.
        eos_token_id (Union[int, List[int]]): The ID or list of IDs representing the end-of-sequence token(s).
        input_ids_seq_length (int): The length of the input sequence.

    Returns:
        None.

    Raises:
        TypeError: If the 'exponential_decay_length_penalty' parameter is not a tuple.
        ValueError: If the 'exponential_decay_length_penalty' tuple does not contain exactly two elements.
        ValueError: If the 'input_ids_seq_length' parameter is not an integer.
        ValueError: If the 'eos_token_id' parameter is not an integer or a list of integers.
    """
    self.regulation_start = exponential_decay_length_penalty[0] + input_ids_seq_length
    self.regulation_factor = exponential_decay_length_penalty[1]
    if isinstance(eos_token_id, int):
        eos_token_id = [eos_token_id]
    self.eos_token_id = eos_token_id

mindnlp.transformers.generation.logits_process.ForceTokensLogitsProcessor

Bases: LogitsProcessor

This processor takes a list of pairs of integers which indicates a mapping from generation indices to token indices that will be forced before sampling. The processor will set their log probs to inf so that they are sampled at their corresponding index.

Source code in mindnlp/transformers/generation/logits_process.py
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
class ForceTokensLogitsProcessor(LogitsProcessor):
    r"""This processor takes a list of pairs of integers which indicates a mapping from generation indices to token
    indices that will be forced before sampling. The processor will set their log probs to `inf` so that they are
    sampled at their corresponding index."""
    def __init__(self, force_token_map: List[List[int]]):
        """
        Initializes a new instance of the ForceTokensLogitsProcessor class.

        Args:
            self: The instance of the class.
            force_token_map (List[List[int]]):
                A list of lists containing integer values representing the force token map.

        Returns:
            None.

        Raises:
            None.
        """
        self.force_token_map = dict(force_token_map)

    def __call__(self, input_ids, scores):
        """
        This method modifies the scores of input tokens based on a predefined set of force tokens.

        Args:
            self (ForceTokensLogitsProcessor): An instance of the ForceTokensLogitsProcessor class.
            input_ids (torch.Tensor): A tensor containing the input token IDs with shape (batch_size, sequence_length).
            scores (torch.Tensor): A tensor containing the scores for each token with shape (batch_size, sequence_length).

        Returns:
            None

        Raises:
            None
        """
        generation_idx = input_ids.shape[-1]
        current_token = self.force_token_map.get(generation_idx, None)
        if current_token is not None:
            scores[:, :] = -float("inf")
            scores[:, current_token] = 0
        return scores

mindnlp.transformers.generation.logits_process.ForceTokensLogitsProcessor.__call__(input_ids, scores)

This method modifies the scores of input tokens based on a predefined set of force tokens.

PARAMETER DESCRIPTION
self

An instance of the ForceTokensLogitsProcessor class.

TYPE: ForceTokensLogitsProcessor

input_ids

A tensor containing the input token IDs with shape (batch_size, sequence_length).

TYPE: Tensor

scores

A tensor containing the scores for each token with shape (batch_size, sequence_length).

TYPE: Tensor

RETURNS DESCRIPTION

None

Source code in mindnlp/transformers/generation/logits_process.py
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
def __call__(self, input_ids, scores):
    """
    This method modifies the scores of input tokens based on a predefined set of force tokens.

    Args:
        self (ForceTokensLogitsProcessor): An instance of the ForceTokensLogitsProcessor class.
        input_ids (torch.Tensor): A tensor containing the input token IDs with shape (batch_size, sequence_length).
        scores (torch.Tensor): A tensor containing the scores for each token with shape (batch_size, sequence_length).

    Returns:
        None

    Raises:
        None
    """
    generation_idx = input_ids.shape[-1]
    current_token = self.force_token_map.get(generation_idx, None)
    if current_token is not None:
        scores[:, :] = -float("inf")
        scores[:, current_token] = 0
    return scores

mindnlp.transformers.generation.logits_process.ForceTokensLogitsProcessor.__init__(force_token_map)

Initializes a new instance of the ForceTokensLogitsProcessor class.

PARAMETER DESCRIPTION
self

The instance of the class.

force_token_map

A list of lists containing integer values representing the force token map.

TYPE: List[List[int]]

RETURNS DESCRIPTION

None.

Source code in mindnlp/transformers/generation/logits_process.py
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
def __init__(self, force_token_map: List[List[int]]):
    """
    Initializes a new instance of the ForceTokensLogitsProcessor class.

    Args:
        self: The instance of the class.
        force_token_map (List[List[int]]):
            A list of lists containing integer values representing the force token map.

    Returns:
        None.

    Raises:
        None.
    """
    self.force_token_map = dict(force_token_map)

mindnlp.transformers.generation.logits_process.ForcedBOSTokenLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that enforces the specified token as the first generated token.

PARAMETER DESCRIPTION
bos_token_id

The id of the token to force as the first generated token.

TYPE: `int`

Source code in mindnlp/transformers/generation/logits_process.py
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
class ForcedBOSTokenLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that enforces the specified token as the first generated token.

    Args:
        bos_token_id (`int`):
            The id of the token to force as the first generated token.
    """
    def __init__(self, bos_token_id: int):
        """
        Initializes a new instance of the ForcedBOSTokenLogitsProcessor class.

        Args:
            self: The object itself.
            bos_token_id (int): The token ID for the beginning of sentence (BOS) token.
                This ID is used to identify the BOS token in the input sequence.

        Returns:
            None.

        Raises:
            None.
        """
        self.bos_token_id = bos_token_id

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method, '__call__', is a part of the 'ForcedBOSTokenLogitsProcessor' class.
        It takes three parameters: self, input_ids, and scores. The method returns a value of type 'mindspore.Tensor'.

        Args:
            self: The instance of the 'ForcedBOSTokenLogitsProcessor' class.
            input_ids (mindspore.Tensor): The input tensor containing the IDs of the tokens.
            scores (mindspore.Tensor): The tensor containing the scores for each token.

        Returns:
            mindspore.Tensor: The tensor containing the modified scores.

        Raises:
            None

        This method modifies the scores tensor by adjusting the scores based on the input IDs.
        If the length of the input_ids tensor is 1, the scores for all tokens except the 'bos_token_id' are set to
        negative infinity, and the score for the 'bos_token_id' is set to 0. The modified scores tensor is then returned.
        """
        cur_len = input_ids.shape[-1]
        if cur_len == 1:
            num_tokens = scores.shape[1]
            scores[:, [i for i in range(num_tokens) if i != self.bos_token_id]] = -float("inf")
            scores[:, self.bos_token_id] = 0
        return scores

mindnlp.transformers.generation.logits_process.ForcedBOSTokenLogitsProcessor.__call__(input_ids, scores)

This method, 'call', is a part of the 'ForcedBOSTokenLogitsProcessor' class. It takes three parameters: self, input_ids, and scores. The method returns a value of type 'mindspore.Tensor'.

PARAMETER DESCRIPTION
self

The instance of the 'ForcedBOSTokenLogitsProcessor' class.

input_ids

The input tensor containing the IDs of the tokens.

TYPE: Tensor

scores

The tensor containing the scores for each token.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: The tensor containing the modified scores.

This method modifies the scores tensor by adjusting the scores based on the input IDs. If the length of the input_ids tensor is 1, the scores for all tokens except the 'bos_token_id' are set to negative infinity, and the score for the 'bos_token_id' is set to 0. The modified scores tensor is then returned.

Source code in mindnlp/transformers/generation/logits_process.py
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method, '__call__', is a part of the 'ForcedBOSTokenLogitsProcessor' class.
    It takes three parameters: self, input_ids, and scores. The method returns a value of type 'mindspore.Tensor'.

    Args:
        self: The instance of the 'ForcedBOSTokenLogitsProcessor' class.
        input_ids (mindspore.Tensor): The input tensor containing the IDs of the tokens.
        scores (mindspore.Tensor): The tensor containing the scores for each token.

    Returns:
        mindspore.Tensor: The tensor containing the modified scores.

    Raises:
        None

    This method modifies the scores tensor by adjusting the scores based on the input IDs.
    If the length of the input_ids tensor is 1, the scores for all tokens except the 'bos_token_id' are set to
    negative infinity, and the score for the 'bos_token_id' is set to 0. The modified scores tensor is then returned.
    """
    cur_len = input_ids.shape[-1]
    if cur_len == 1:
        num_tokens = scores.shape[1]
        scores[:, [i for i in range(num_tokens) if i != self.bos_token_id]] = -float("inf")
        scores[:, self.bos_token_id] = 0
    return scores

mindnlp.transformers.generation.logits_process.ForcedBOSTokenLogitsProcessor.__init__(bos_token_id)

Initializes a new instance of the ForcedBOSTokenLogitsProcessor class.

PARAMETER DESCRIPTION
self

The object itself.

bos_token_id

The token ID for the beginning of sentence (BOS) token. This ID is used to identify the BOS token in the input sequence.

TYPE: int

RETURNS DESCRIPTION

None.

Source code in mindnlp/transformers/generation/logits_process.py
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
def __init__(self, bos_token_id: int):
    """
    Initializes a new instance of the ForcedBOSTokenLogitsProcessor class.

    Args:
        self: The object itself.
        bos_token_id (int): The token ID for the beginning of sentence (BOS) token.
            This ID is used to identify the BOS token in the input sequence.

    Returns:
        None.

    Raises:
        None.
    """
    self.bos_token_id = bos_token_id

mindnlp.transformers.generation.logits_process.ForcedEOSTokenLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that enforces the specified token as the last generated token when max_length is reached.

PARAMETER DESCRIPTION
max_length

The maximum length of the sequence to be generated.

TYPE: `int`

eos_token_id

The id of the token to force as the last generated token when max_length is reached. Optionally, use a list to set multiple end-of-sequence tokens.

TYPE: `Union[int, List[int]]`

Source code in mindnlp/transformers/generation/logits_process.py
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
class ForcedEOSTokenLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that enforces the specified token as the last generated token when `max_length` is reached.

    Args:
        max_length (`int`):
            The maximum length of the sequence to be generated.
        eos_token_id (`Union[int, List[int]]`):
            The id of the token to force as the last generated token when `max_length` is reached. Optionally, use a
            list to set multiple *end-of-sequence* tokens.
    """
    def __init__(self, max_length: int, eos_token_id: Union[int, List[int]]):
        """
        Initializes a ForcedEOSTokenLogitsProcessor object with the specified parameters.

        Args:
            max_length (int): The maximum length for processing logits. Must be a positive integer.
            eos_token_id (Union[int, List[int]]): The end-of-sequence token ID(s) to be considered.
                If a single integer is provided, it will be converted to a list.

        Returns:
            None.

        Raises:
            None.
        """
        self.max_length = max_length
        if isinstance(eos_token_id, int):
            eos_token_id = [eos_token_id]
        self.eos_token_id = eos_token_id

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        '''
        This method processes the logits by forcing end-of-sequence (EOS) tokens and returns the updated scores.

        Args:
            self (ForcedEOSTokenLogitsProcessor): The instance of the ForcedEOSTokenLogitsProcessor class.
            input_ids (mindspore.Tensor): The input tensor containing token IDs.
            scores (mindspore.Tensor): The tensor containing the scores/logits for each token.

        Returns:
            mindspore.Tensor: Returns the updated scores after processing.

        Raises:
            None.
        '''
        cur_len = input_ids.shape[-1]
        if cur_len == self.max_length - 1:
            num_tokens = scores.shape[1]
            scores[:, [i for i in range(num_tokens) if i not in self.eos_token_id]] = \
                float(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).min)
            for i in self.eos_token_id:
                scores[:, i] = 0
        return scores

mindnlp.transformers.generation.logits_process.ForcedEOSTokenLogitsProcessor.__call__(input_ids, scores)

This method processes the logits by forcing end-of-sequence (EOS) tokens and returns the updated scores.

PARAMETER DESCRIPTION
self

The instance of the ForcedEOSTokenLogitsProcessor class.

TYPE: ForcedEOSTokenLogitsProcessor

input_ids

The input tensor containing token IDs.

TYPE: Tensor

scores

The tensor containing the scores/logits for each token.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: Returns the updated scores after processing.

Source code in mindnlp/transformers/generation/logits_process.py
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    '''
    This method processes the logits by forcing end-of-sequence (EOS) tokens and returns the updated scores.

    Args:
        self (ForcedEOSTokenLogitsProcessor): The instance of the ForcedEOSTokenLogitsProcessor class.
        input_ids (mindspore.Tensor): The input tensor containing token IDs.
        scores (mindspore.Tensor): The tensor containing the scores/logits for each token.

    Returns:
        mindspore.Tensor: Returns the updated scores after processing.

    Raises:
        None.
    '''
    cur_len = input_ids.shape[-1]
    if cur_len == self.max_length - 1:
        num_tokens = scores.shape[1]
        scores[:, [i for i in range(num_tokens) if i not in self.eos_token_id]] = \
            float(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).min)
        for i in self.eos_token_id:
            scores[:, i] = 0
    return scores

mindnlp.transformers.generation.logits_process.ForcedEOSTokenLogitsProcessor.__init__(max_length, eos_token_id)

Initializes a ForcedEOSTokenLogitsProcessor object with the specified parameters.

PARAMETER DESCRIPTION
max_length

The maximum length for processing logits. Must be a positive integer.

TYPE: int

eos_token_id

The end-of-sequence token ID(s) to be considered. If a single integer is provided, it will be converted to a list.

TYPE: Union[int, List[int]]

RETURNS DESCRIPTION

None.

Source code in mindnlp/transformers/generation/logits_process.py
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
def __init__(self, max_length: int, eos_token_id: Union[int, List[int]]):
    """
    Initializes a ForcedEOSTokenLogitsProcessor object with the specified parameters.

    Args:
        max_length (int): The maximum length for processing logits. Must be a positive integer.
        eos_token_id (Union[int, List[int]]): The end-of-sequence token ID(s) to be considered.
            If a single integer is provided, it will be converted to a list.

    Returns:
        None.

    Raises:
        None.
    """
    self.max_length = max_length
    if isinstance(eos_token_id, int):
        eos_token_id = [eos_token_id]
    self.eos_token_id = eos_token_id

mindnlp.transformers.generation.logits_process.HammingDiversityLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that enforces diverse beam search. Note that this logits processor is only effective for [PreTrainedModel.group_beam_search]. See Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence Models for more details.

PARAMETER DESCRIPTION
diversity_penalty

This value is subtracted from a beam's score if it generates a token same as any beam from other group at a particular time. Note that diversity_penalty is only effective if group beam search is enabled.

TYPE: `float`

num_beams

Number of beams used for group beam search. See this paper for more details.

TYPE: `int`

num_beam_groups

Number of groups to divide num_beams into in order to ensure diversity among different groups of beams. See this paper for more details.

TYPE: `int`

Source code in mindnlp/transformers/generation/logits_process.py
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
class HammingDiversityLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that enforces diverse beam search. Note that this logits processor is only effective for
    [`PreTrainedModel.group_beam_search`]. See [Diverse Beam Search: Decoding Diverse Solutions from Neural Sequence
    Models](https://arxiv.org/pdf/1610.02424.pdf) for more details.

    Args:
        diversity_penalty (`float`):
            This value is subtracted from a beam's score if it generates a token same as any beam from other group at a
            particular time. Note that `diversity_penalty` is only effective if `group beam search` is enabled.
        num_beams (`int`):
            Number of beams used for group beam search. See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more
            details.
        num_beam_groups (`int`):
            Number of groups to divide `num_beams` into in order to ensure diversity among different groups of beams.
            See [this paper](https://arxiv.org/pdf/1610.02424.pdf) for more details.
    """
    def __init__(self, diversity_penalty: float, num_beams: int, num_beam_groups: int):
        """
        Initializes a new instance of the HammingDiversityLogitsProcessor class.

        Args:
            self: The instance of the class.
            diversity_penalty (float): The penalty factor for diversity. It should be a positive floating-point number.
            num_beams (int): The number of beams to use in the beam search. It should be an integer greater than 1.
            num_beam_groups (int): The number of beam groups. It should be an integer greater than 1 and less than or
                equal to num_beams.

        Returns:
            None.

        Raises:
            ValueError: If diversity_penalty is not a float or is not strictly larger than 0.
            ValueError: If num_beams is not an integer or is not strictly larger than 1.
            ValueError: If num_beam_groups is not an integer or is not strictly larger than 1.
            ValueError: If num_beam_groups is larger than num_beams.
        """
        if not isinstance(diversity_penalty, float) or (not diversity_penalty > 0.0):
            raise ValueError("`diversity_penalty` should be a float strictly larger than 0.")
        self._diversity_penalty = diversity_penalty
        if not isinstance(num_beams, int) or num_beams < 2:
            raise ValueError("`num_beams` should be an integer strictly larger than 1.")
        self._num_beams = num_beams
        if not isinstance(num_beam_groups, int) or num_beam_groups < 2:
            raise ValueError("`num_beam_groups` should be an integer strictly larger than 1.")
        if num_beam_groups > num_beams:
            raise ValueError("`beam_groups` has to be smaller or equal to `num_beams`.")
        self._num_sub_beams = num_beams // num_beam_groups

    def __call__(
            self,
            input_ids: mindspore.Tensor,
            scores: mindspore.Tensor,
            current_tokens: mindspore.Tensor,
            beam_group_idx: int,
    ) -> mindspore.Tensor:
        """
        This method calculates the diversity penalty and updates the input scores based on the previous group tokens.

        Args:
            self: The instance of the HammingDiversityLogitsProcessor class.
            input_ids (mindspore.Tensor): The input tensor representing the tokenized input.
            scores (mindspore.Tensor): The tensor containing scores for each token.
            current_tokens (mindspore.Tensor): The tensor containing the current tokens.
            beam_group_idx (int): The index of the beam group.

        Returns:
            mindspore.Tensor: Returns the updated scores tensor after applying the diversity penalty.

        Raises:
            ValueError: If the input_ids, scores, current_tokens, or beam_group_idx are of incorrect or incompatible types.
            IndexError: If the beam_group_idx is out of range.
            RuntimeError: If there is an issue with the calculation or update process.
        """
        # hamming diversity: penalise using same token in current group which was used in previous groups at
        # the same time step
        batch_size = current_tokens.shape[0] // self._num_beams
        group_start_idx = beam_group_idx * self._num_sub_beams
        group_end_idx = min(group_start_idx + self._num_sub_beams, self._num_beams)
        group_size = group_end_idx - group_start_idx
        vocab_size = scores.shape[-1]

        if group_start_idx == 0:
            return scores

        for batch_idx in range(batch_size):
            # predicted tokens of last time step of previous groups
            previous_group_tokens = current_tokens[
                                    batch_idx * self._num_beams: batch_idx * self._num_beams + group_start_idx
                                    ]
            token_frequency = ops.bincount(previous_group_tokens, minlength=vocab_size)
            scores[batch_idx * group_size: (batch_idx + 1) * group_size] -= self._diversity_penalty * token_frequency

        return scores

mindnlp.transformers.generation.logits_process.HammingDiversityLogitsProcessor.__call__(input_ids, scores, current_tokens, beam_group_idx)

This method calculates the diversity penalty and updates the input scores based on the previous group tokens.

PARAMETER DESCRIPTION
self

The instance of the HammingDiversityLogitsProcessor class.

input_ids

The input tensor representing the tokenized input.

TYPE: Tensor

scores

The tensor containing scores for each token.

TYPE: Tensor

current_tokens

The tensor containing the current tokens.

TYPE: Tensor

beam_group_idx

The index of the beam group.

TYPE: int

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: Returns the updated scores tensor after applying the diversity penalty.

RAISES DESCRIPTION
ValueError

If the input_ids, scores, current_tokens, or beam_group_idx are of incorrect or incompatible types.

IndexError

If the beam_group_idx is out of range.

RuntimeError

If there is an issue with the calculation or update process.

Source code in mindnlp/transformers/generation/logits_process.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def __call__(
        self,
        input_ids: mindspore.Tensor,
        scores: mindspore.Tensor,
        current_tokens: mindspore.Tensor,
        beam_group_idx: int,
) -> mindspore.Tensor:
    """
    This method calculates the diversity penalty and updates the input scores based on the previous group tokens.

    Args:
        self: The instance of the HammingDiversityLogitsProcessor class.
        input_ids (mindspore.Tensor): The input tensor representing the tokenized input.
        scores (mindspore.Tensor): The tensor containing scores for each token.
        current_tokens (mindspore.Tensor): The tensor containing the current tokens.
        beam_group_idx (int): The index of the beam group.

    Returns:
        mindspore.Tensor: Returns the updated scores tensor after applying the diversity penalty.

    Raises:
        ValueError: If the input_ids, scores, current_tokens, or beam_group_idx are of incorrect or incompatible types.
        IndexError: If the beam_group_idx is out of range.
        RuntimeError: If there is an issue with the calculation or update process.
    """
    # hamming diversity: penalise using same token in current group which was used in previous groups at
    # the same time step
    batch_size = current_tokens.shape[0] // self._num_beams
    group_start_idx = beam_group_idx * self._num_sub_beams
    group_end_idx = min(group_start_idx + self._num_sub_beams, self._num_beams)
    group_size = group_end_idx - group_start_idx
    vocab_size = scores.shape[-1]

    if group_start_idx == 0:
        return scores

    for batch_idx in range(batch_size):
        # predicted tokens of last time step of previous groups
        previous_group_tokens = current_tokens[
                                batch_idx * self._num_beams: batch_idx * self._num_beams + group_start_idx
                                ]
        token_frequency = ops.bincount(previous_group_tokens, minlength=vocab_size)
        scores[batch_idx * group_size: (batch_idx + 1) * group_size] -= self._diversity_penalty * token_frequency

    return scores

mindnlp.transformers.generation.logits_process.HammingDiversityLogitsProcessor.__init__(diversity_penalty, num_beams, num_beam_groups)

Initializes a new instance of the HammingDiversityLogitsProcessor class.

PARAMETER DESCRIPTION
self

The instance of the class.

diversity_penalty

The penalty factor for diversity. It should be a positive floating-point number.

TYPE: float

num_beams

The number of beams to use in the beam search. It should be an integer greater than 1.

TYPE: int

num_beam_groups

The number of beam groups. It should be an integer greater than 1 and less than or equal to num_beams.

TYPE: int

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If diversity_penalty is not a float or is not strictly larger than 0.

ValueError

If num_beams is not an integer or is not strictly larger than 1.

ValueError

If num_beam_groups is not an integer or is not strictly larger than 1.

ValueError

If num_beam_groups is larger than num_beams.

Source code in mindnlp/transformers/generation/logits_process.py
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def __init__(self, diversity_penalty: float, num_beams: int, num_beam_groups: int):
    """
    Initializes a new instance of the HammingDiversityLogitsProcessor class.

    Args:
        self: The instance of the class.
        diversity_penalty (float): The penalty factor for diversity. It should be a positive floating-point number.
        num_beams (int): The number of beams to use in the beam search. It should be an integer greater than 1.
        num_beam_groups (int): The number of beam groups. It should be an integer greater than 1 and less than or
            equal to num_beams.

    Returns:
        None.

    Raises:
        ValueError: If diversity_penalty is not a float or is not strictly larger than 0.
        ValueError: If num_beams is not an integer or is not strictly larger than 1.
        ValueError: If num_beam_groups is not an integer or is not strictly larger than 1.
        ValueError: If num_beam_groups is larger than num_beams.
    """
    if not isinstance(diversity_penalty, float) or (not diversity_penalty > 0.0):
        raise ValueError("`diversity_penalty` should be a float strictly larger than 0.")
    self._diversity_penalty = diversity_penalty
    if not isinstance(num_beams, int) or num_beams < 2:
        raise ValueError("`num_beams` should be an integer strictly larger than 1.")
    self._num_beams = num_beams
    if not isinstance(num_beam_groups, int) or num_beam_groups < 2:
        raise ValueError("`num_beam_groups` should be an integer strictly larger than 1.")
    if num_beam_groups > num_beams:
        raise ValueError("`beam_groups` has to be smaller or equal to `num_beams`.")
    self._num_sub_beams = num_beams // num_beam_groups

mindnlp.transformers.generation.logits_process.InfNanRemoveLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that removes all nan and inf values to avoid the generation method to fail. Note that using the logits processor should only be used if necessary since it can slow down the generation method. max_length is reached.

Source code in mindnlp/transformers/generation/logits_process.py
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
class InfNanRemoveLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that removes all `nan` and `inf` values to avoid the generation method to fail. Note that using
    the logits processor should only be used if necessary since it can slow down the generation method. `max_length` is
    reached.
    """
    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method '__call__' in the class 'InfNanRemoveLogitsProcessor' processes input scores by replacing
        infinite and NaN values.

        Args:
            self: An instance of the InfNanRemoveLogitsProcessor class.
            input_ids (mindspore.Tensor): A tensor containing the input IDs.
            scores (mindspore.Tensor): A tensor containing the scores to be processed.
                Any NaN values in the scores will be replaced with 0.0, and any infinite values will be replaced with
                the maximum value for the data type.

        Returns:
            mindspore.Tensor: A tensor containing the processed scores after replacing NaN and infinite values.

        Raises:
            None.
        """
        # set all nan values to 0.0
        scores[ops.isnan(scores)] = 0.0

        # set all inf values to max possible value
        scores[scores == float("inf")] = float(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).max)
        return scores

mindnlp.transformers.generation.logits_process.InfNanRemoveLogitsProcessor.__call__(input_ids, scores)

This method 'call' in the class 'InfNanRemoveLogitsProcessor' processes input scores by replacing infinite and NaN values.

PARAMETER DESCRIPTION
self

An instance of the InfNanRemoveLogitsProcessor class.

input_ids

A tensor containing the input IDs.

TYPE: Tensor

scores

A tensor containing the scores to be processed. Any NaN values in the scores will be replaced with 0.0, and any infinite values will be replaced with the maximum value for the data type.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the processed scores after replacing NaN and infinite values.

Source code in mindnlp/transformers/generation/logits_process.py
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method '__call__' in the class 'InfNanRemoveLogitsProcessor' processes input scores by replacing
    infinite and NaN values.

    Args:
        self: An instance of the InfNanRemoveLogitsProcessor class.
        input_ids (mindspore.Tensor): A tensor containing the input IDs.
        scores (mindspore.Tensor): A tensor containing the scores to be processed.
            Any NaN values in the scores will be replaced with 0.0, and any infinite values will be replaced with
            the maximum value for the data type.

    Returns:
        mindspore.Tensor: A tensor containing the processed scores after replacing NaN and infinite values.

    Raises:
        None.
    """
    # set all nan values to 0.0
    scores[ops.isnan(scores)] = 0.0

    # set all inf values to max possible value
    scores[scores == float("inf")] = float(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).max)
    return scores

mindnlp.transformers.generation.logits_process.LogitNormalization

Bases: LogitsProcessor, LogitsWarper

[LogitsWarper] and [LogitsProcessor] for normalizing the scores using log-softmax. It's important to normalize the scores during beam search, after applying the logits processors or warpers, since the search algorithm used in this library doesn't do it (it only does it before, but they may need re-normalization) but it still supposes that the scores are normalized when comparing the hypotheses.

Source code in mindnlp/transformers/generation/logits_process.py
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
class LogitNormalization(LogitsProcessor, LogitsWarper):
    r"""
    [`LogitsWarper`] and [`LogitsProcessor`] for normalizing the scores using log-softmax. It's important to normalize
    the scores during beam search, after applying the logits processors or warpers, since the search algorithm used in
    this library doesn't do it (it only does it before, but they may need re-normalization) but it still supposes that
    the scores are normalized when comparing the hypotheses.
    """
    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """

        Description:
            This class provides a method for logit normalization.

        Args:
            self (LogitNormalization): The instance of the LogitNormalization class.
            input_ids (mindspore.Tensor): The input tensor containing the IDs.
                This tensor is used as an input to calculate the log softmax.
            scores (mindspore.Tensor): The tensor containing the scores to be normalized.
                The scores are normalized using log softmax along the last dimension.

        Returns:
            mindspore.Tensor: Returns the normalized scores in the form of a Tensor.
                The normalized scores are obtained by applying log softmax to the input scores.

        Raises:
            None
        """
        scores = F.log_softmax(scores, dim=-1)
        return scores

mindnlp.transformers.generation.logits_process.LogitNormalization.__call__(input_ids, scores)

Description

This class provides a method for logit normalization.

PARAMETER DESCRIPTION
self

The instance of the LogitNormalization class.

TYPE: LogitNormalization

input_ids

The input tensor containing the IDs. This tensor is used as an input to calculate the log softmax.

TYPE: Tensor

scores

The tensor containing the scores to be normalized. The scores are normalized using log softmax along the last dimension.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: Returns the normalized scores in the form of a Tensor. The normalized scores are obtained by applying log softmax to the input scores.

Source code in mindnlp/transformers/generation/logits_process.py
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """

    Description:
        This class provides a method for logit normalization.

    Args:
        self (LogitNormalization): The instance of the LogitNormalization class.
        input_ids (mindspore.Tensor): The input tensor containing the IDs.
            This tensor is used as an input to calculate the log softmax.
        scores (mindspore.Tensor): The tensor containing the scores to be normalized.
            The scores are normalized using log softmax along the last dimension.

    Returns:
        mindspore.Tensor: Returns the normalized scores in the form of a Tensor.
            The normalized scores are obtained by applying log softmax to the input scores.

    Raises:
        None
    """
    scores = F.log_softmax(scores, dim=-1)
    return scores

mindnlp.transformers.generation.logits_process.LogitsProcessor

Abstract base class for all logit processors that can be applied during generation.

Source code in mindnlp/transformers/generation/logits_process.py
30
31
32
33
34
35
36
class LogitsProcessor:
    """Abstract base class for all logit processors that can be applied during generation."""
    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """Torch method for processing logits."""
        raise NotImplementedError(
            f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."
        )

mindnlp.transformers.generation.logits_process.LogitsProcessor.__call__(input_ids, scores)

Torch method for processing logits.

Source code in mindnlp/transformers/generation/logits_process.py
32
33
34
35
36
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """Torch method for processing logits."""
    raise NotImplementedError(
        f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."
    )

mindnlp.transformers.generation.logits_process.LogitsProcessorList

Bases: list

This class can be used to create a list of [LogitsProcessor] or [LogitsWarper] to subsequently process a scores input tensor. This class inherits from list and adds a specific call method to apply each [LogitsProcessor] or [LogitsWarper] to the inputs.

Source code in mindnlp/transformers/generation/logits_process.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class LogitsProcessorList(list):
    """
    This class can be used to create a list of [`LogitsProcessor`] or [`LogitsWarper`] to subsequently process a
    `scores` input tensor. This class inherits from list and adds a specific *__call__* method to apply each
    [`LogitsProcessor`] or [`LogitsWarper`] to the inputs.
    """
    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor, **kwargs) -> mindspore.Tensor:
        """
        This method processes input_ids and scores using a list of processors.

        Args:
            self (LogitsProcessorList): The instance of the LogitsProcessorList class.
            input_ids (mindspore.Tensor): The input tensor containing the IDs of tokens to be processed.
            scores (mindspore.Tensor): The input tensor containing the scores to be processed.

        Returns:
            mindspore.Tensor: The processed scores tensor.

        Raises:
            ValueError: If not all the required parameters for a processor are passed to the logits processor.
        """
        for processor in self:
            function_args = inspect.signature(processor.__call__).parameters
            if len(function_args) > 2:
                if not all(arg in kwargs for arg in list(function_args.keys())[2:]):
                    raise ValueError(
                        f"Make sure that all the required parameters: {list(function_args.keys())} for "
                        f"{processor.__class__} are passed to the logits processor."
                    )
                scores = processor(input_ids, scores, **kwargs)
            else:
                scores = processor(input_ids, scores)
        return scores

mindnlp.transformers.generation.logits_process.LogitsProcessorList.__call__(input_ids, scores, **kwargs)

This method processes input_ids and scores using a list of processors.

PARAMETER DESCRIPTION
self

The instance of the LogitsProcessorList class.

TYPE: LogitsProcessorList

input_ids

The input tensor containing the IDs of tokens to be processed.

TYPE: Tensor

scores

The input tensor containing the scores to be processed.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: The processed scores tensor.

RAISES DESCRIPTION
ValueError

If not all the required parameters for a processor are passed to the logits processor.

Source code in mindnlp/transformers/generation/logits_process.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor, **kwargs) -> mindspore.Tensor:
    """
    This method processes input_ids and scores using a list of processors.

    Args:
        self (LogitsProcessorList): The instance of the LogitsProcessorList class.
        input_ids (mindspore.Tensor): The input tensor containing the IDs of tokens to be processed.
        scores (mindspore.Tensor): The input tensor containing the scores to be processed.

    Returns:
        mindspore.Tensor: The processed scores tensor.

    Raises:
        ValueError: If not all the required parameters for a processor are passed to the logits processor.
    """
    for processor in self:
        function_args = inspect.signature(processor.__call__).parameters
        if len(function_args) > 2:
            if not all(arg in kwargs for arg in list(function_args.keys())[2:]):
                raise ValueError(
                    f"Make sure that all the required parameters: {list(function_args.keys())} for "
                    f"{processor.__class__} are passed to the logits processor."
                )
            scores = processor(input_ids, scores, **kwargs)
        else:
            scores = processor(input_ids, scores)
    return scores

mindnlp.transformers.generation.logits_process.LogitsWarper

Abstract base class for all logit warpers that can be applied during generation with multinomial sampling.

Source code in mindnlp/transformers/generation/logits_process.py
39
40
41
42
43
44
45
class LogitsWarper:
    """Abstract base class for all logit warpers that can be applied during generation with multinomial sampling."""
    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """Torch method for warping logits."""
        raise NotImplementedError(
            f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."
        )

mindnlp.transformers.generation.logits_process.LogitsWarper.__call__(input_ids, scores)

Torch method for warping logits.

Source code in mindnlp/transformers/generation/logits_process.py
41
42
43
44
45
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """Torch method for warping logits."""
    raise NotImplementedError(
        f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."
    )

mindnlp.transformers.generation.logits_process.MinLengthLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] enforcing a min-length by setting EOS probability to 0.

PARAMETER DESCRIPTION
min_length

The minimum length below which the score of eos_token_id is set to -float("Inf").

TYPE: `int`

eos_token_id

The id of the end-of-sequence token. Optionally, use a list to set multiple end-of-sequence tokens.

TYPE: `Union[int, List[int]]`

Source code in mindnlp/transformers/generation/logits_process.py
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
class MinLengthLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] enforcing a min-length by setting EOS probability to 0.

    Args:
        min_length (`int`):
            The minimum length below which the score of `eos_token_id` is set to `-float("Inf")`.
        eos_token_id (`Union[int, List[int]]`):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
    """
    def __init__(self, min_length: int, eos_token_id: Union[int, List[int]]):
        """
        Initializes an instance of the MinLengthLogitsProcessor class.

        Args:
            self (MinLengthLogitsProcessor): The current instance of the MinLengthLogitsProcessor class.
            min_length (int): The minimum length of the processed logits. It must be a positive integer.
            eos_token_id (Union[int, List[int]]): The end-of-sequence token ID or a list of end-of-sequence token IDs.
                If an integer is provided, it will be converted to a list. It must be a list of positive integers.

        Returns:
            None.

        Raises:
            ValueError: If `min_length` is not a positive integer.
            ValueError: If `eos_token_id` is not a list of positive integers.
        """
        if not isinstance(min_length, int) or min_length < 0:
            raise ValueError(f"`min_length` has to be a positive integer, but is {min_length}")

        if isinstance(eos_token_id, int):
            eos_token_id = [eos_token_id]
        if not all(isinstance(i, int) for i in eos_token_id) or any(i < 0 for i in eos_token_id):
            raise ValueError(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}")

        self.min_length = min_length
        self.eos_token_id = eos_token_id

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        The '__call__' method processes the scores of a given input and applies a minimum length constraint to the logits.
        It takes three parameters: self, input_ids, and scores.

        Args:
            self: An instance of the MinLengthLogitsProcessor class.
            input_ids (mindspore.Tensor): The input tensor representing the tokenized input sequence.
            scores (mindspore.Tensor): The tensor containing the logits scores.

        Returns:
            mindspore.Tensor: The processed scores tensor after applying the minimum length constraint.

        Raises:
            None.
        """
        vocab_tensor = ops.arange(scores.shape[-1])
        eos_token_id = mindspore.tensor(self.eos_token_id)
        eos_token_mask = vocab_tensor == eos_token_id
        scores_processed = scores.copy()
        if input_ids.shape[-1] < self.min_length:
            scores_processed = ops.where(eos_token_mask, -math.inf, scores)
        return scores_processed

mindnlp.transformers.generation.logits_process.MinLengthLogitsProcessor.__call__(input_ids, scores)

The 'call' method processes the scores of a given input and applies a minimum length constraint to the logits. It takes three parameters: self, input_ids, and scores.

PARAMETER DESCRIPTION
self

An instance of the MinLengthLogitsProcessor class.

input_ids

The input tensor representing the tokenized input sequence.

TYPE: Tensor

scores

The tensor containing the logits scores.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: The processed scores tensor after applying the minimum length constraint.

Source code in mindnlp/transformers/generation/logits_process.py
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    The '__call__' method processes the scores of a given input and applies a minimum length constraint to the logits.
    It takes three parameters: self, input_ids, and scores.

    Args:
        self: An instance of the MinLengthLogitsProcessor class.
        input_ids (mindspore.Tensor): The input tensor representing the tokenized input sequence.
        scores (mindspore.Tensor): The tensor containing the logits scores.

    Returns:
        mindspore.Tensor: The processed scores tensor after applying the minimum length constraint.

    Raises:
        None.
    """
    vocab_tensor = ops.arange(scores.shape[-1])
    eos_token_id = mindspore.tensor(self.eos_token_id)
    eos_token_mask = vocab_tensor == eos_token_id
    scores_processed = scores.copy()
    if input_ids.shape[-1] < self.min_length:
        scores_processed = ops.where(eos_token_mask, -math.inf, scores)
    return scores_processed

mindnlp.transformers.generation.logits_process.MinLengthLogitsProcessor.__init__(min_length, eos_token_id)

Initializes an instance of the MinLengthLogitsProcessor class.

PARAMETER DESCRIPTION
self

The current instance of the MinLengthLogitsProcessor class.

TYPE: MinLengthLogitsProcessor

min_length

The minimum length of the processed logits. It must be a positive integer.

TYPE: int

eos_token_id

The end-of-sequence token ID or a list of end-of-sequence token IDs. If an integer is provided, it will be converted to a list. It must be a list of positive integers.

TYPE: Union[int, List[int]]

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If min_length is not a positive integer.

ValueError

If eos_token_id is not a list of positive integers.

Source code in mindnlp/transformers/generation/logits_process.py
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
def __init__(self, min_length: int, eos_token_id: Union[int, List[int]]):
    """
    Initializes an instance of the MinLengthLogitsProcessor class.

    Args:
        self (MinLengthLogitsProcessor): The current instance of the MinLengthLogitsProcessor class.
        min_length (int): The minimum length of the processed logits. It must be a positive integer.
        eos_token_id (Union[int, List[int]]): The end-of-sequence token ID or a list of end-of-sequence token IDs.
            If an integer is provided, it will be converted to a list. It must be a list of positive integers.

    Returns:
        None.

    Raises:
        ValueError: If `min_length` is not a positive integer.
        ValueError: If `eos_token_id` is not a list of positive integers.
    """
    if not isinstance(min_length, int) or min_length < 0:
        raise ValueError(f"`min_length` has to be a positive integer, but is {min_length}")

    if isinstance(eos_token_id, int):
        eos_token_id = [eos_token_id]
    if not all(isinstance(i, int) for i in eos_token_id) or any(i < 0 for i in eos_token_id):
        raise ValueError(f"`eos_token_id` has to be a list of positive integers, but is {eos_token_id}")

    self.min_length = min_length
    self.eos_token_id = eos_token_id

mindnlp.transformers.generation.logits_process.MinNewTokensLengthLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] enforcing a min-length of new tokens by setting EOS (End-Of-Sequence) token probability to 0.

PARAMETER DESCRIPTION
prompt_length_to_skip

The input tokens length.

TYPE: `int`

min_new_tokens

The minimum new tokens length below which the score of eos_token_id is set to -float("Inf").

TYPE: `int`

eos_token_id

The id of the end-of-sequence token.

TYPE: `int`

Source code in mindnlp/transformers/generation/logits_process.py
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
class MinNewTokensLengthLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] enforcing a min-length of new tokens by setting EOS (End-Of-Sequence) token probability to 0.

    Args:
        prompt_length_to_skip (`int`):
            The input tokens length.
        min_new_tokens (`int`):
            The minimum *new* tokens length below which the score of `eos_token_id` is set to `-float("Inf")`.
        eos_token_id (`int`):
            The id of the *end-of-sequence* token.
    """
    def __init__(self, prompt_length_to_skip: int, min_new_tokens: int, eos_token_id: int):
        """
        __init__

        Args:
            prompt_length_to_skip (int): The length of prompt to skip for processing.
            min_new_tokens (int): The minimum number of new tokens to consider for processing.
            eos_token_id (int): The ID of the end-of-sequence token.

        Returns:
            None.

        Raises:
            ValueError: If prompt_length_to_skip, min_new_tokens, or eos_token_id is not a positive integer.
        """
        for arg_name, arg_value in [
            ("prompt_length_to_skip", prompt_length_to_skip),
            ("min_new_tokens", min_new_tokens),
            ("eos_token_id", eos_token_id),
        ]:
            if not isinstance(arg_value, int) or arg_value < 0:
                raise ValueError(f"`{arg_name}` has to be a positive integer, but is {arg_value}")

        self.prompt_length_to_skip = prompt_length_to_skip
        self.min_new_tokens = min_new_tokens
        self.eos_token_id = eos_token_id

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method '__call__' in the class 'MinNewTokensLengthLogitsProcessor' processes input_ids and scores to adjust
        scores based on the length of new tokens.

        Args:
            self (MinNewTokensLengthLogitsProcessor): The instance of the MinNewTokensLengthLogitsProcessor class.
            input_ids (mindspore.Tensor): The input tensor containing token IDs.
            scores (mindspore.Tensor): The input tensor containing the scores associated with each token.

        Returns:
            mindspore.Tensor: Returns the updated scores tensor after processing based on the input_ids and
                prompt_length_to_skip attribute.

        Raises:
            ValueError: If the new_tokens_length is calculated to be less than the min_new_tokens threshold.
        """
        new_tokens_length = input_ids.shape[-1] - self.prompt_length_to_skip
        if new_tokens_length < self.min_new_tokens:
            scores[:, self.eos_token_id] = -float("inf")

        return scores

mindnlp.transformers.generation.logits_process.MinNewTokensLengthLogitsProcessor.__call__(input_ids, scores)

This method 'call' in the class 'MinNewTokensLengthLogitsProcessor' processes input_ids and scores to adjust scores based on the length of new tokens.

PARAMETER DESCRIPTION
self

The instance of the MinNewTokensLengthLogitsProcessor class.

TYPE: MinNewTokensLengthLogitsProcessor

input_ids

The input tensor containing token IDs.

TYPE: Tensor

scores

The input tensor containing the scores associated with each token.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: Returns the updated scores tensor after processing based on the input_ids and prompt_length_to_skip attribute.

RAISES DESCRIPTION
ValueError

If the new_tokens_length is calculated to be less than the min_new_tokens threshold.

Source code in mindnlp/transformers/generation/logits_process.py
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method '__call__' in the class 'MinNewTokensLengthLogitsProcessor' processes input_ids and scores to adjust
    scores based on the length of new tokens.

    Args:
        self (MinNewTokensLengthLogitsProcessor): The instance of the MinNewTokensLengthLogitsProcessor class.
        input_ids (mindspore.Tensor): The input tensor containing token IDs.
        scores (mindspore.Tensor): The input tensor containing the scores associated with each token.

    Returns:
        mindspore.Tensor: Returns the updated scores tensor after processing based on the input_ids and
            prompt_length_to_skip attribute.

    Raises:
        ValueError: If the new_tokens_length is calculated to be less than the min_new_tokens threshold.
    """
    new_tokens_length = input_ids.shape[-1] - self.prompt_length_to_skip
    if new_tokens_length < self.min_new_tokens:
        scores[:, self.eos_token_id] = -float("inf")

    return scores

mindnlp.transformers.generation.logits_process.MinNewTokensLengthLogitsProcessor.__init__(prompt_length_to_skip, min_new_tokens, eos_token_id)

init

PARAMETER DESCRIPTION
prompt_length_to_skip

The length of prompt to skip for processing.

TYPE: int

min_new_tokens

The minimum number of new tokens to consider for processing.

TYPE: int

eos_token_id

The ID of the end-of-sequence token.

TYPE: int

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If prompt_length_to_skip, min_new_tokens, or eos_token_id is not a positive integer.

Source code in mindnlp/transformers/generation/logits_process.py
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
def __init__(self, prompt_length_to_skip: int, min_new_tokens: int, eos_token_id: int):
    """
    __init__

    Args:
        prompt_length_to_skip (int): The length of prompt to skip for processing.
        min_new_tokens (int): The minimum number of new tokens to consider for processing.
        eos_token_id (int): The ID of the end-of-sequence token.

    Returns:
        None.

    Raises:
        ValueError: If prompt_length_to_skip, min_new_tokens, or eos_token_id is not a positive integer.
    """
    for arg_name, arg_value in [
        ("prompt_length_to_skip", prompt_length_to_skip),
        ("min_new_tokens", min_new_tokens),
        ("eos_token_id", eos_token_id),
    ]:
        if not isinstance(arg_value, int) or arg_value < 0:
            raise ValueError(f"`{arg_name}` has to be a positive integer, but is {arg_value}")

    self.prompt_length_to_skip = prompt_length_to_skip
    self.min_new_tokens = min_new_tokens
    self.eos_token_id = eos_token_id

mindnlp.transformers.generation.logits_process.NoBadWordsLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that enforces that specified sequences will never be sampled.

PARAMETER DESCRIPTION
bad_words_ids

List of list of token ids that are not allowed to be generated. In order to get the token ids of the words that should not appear in the generated text, use tokenizer(bad_words, add_prefix_space=True, add_special_tokens=False).input_ids.

TYPE: `List[List[int]]`

eos_token_id

The id of the end-of-sequence token. Optionally, use a list to set multiple end-of-sequence tokens.

TYPE: `Union[int, List[int]]`

Source code in mindnlp/transformers/generation/logits_process.py
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
class NoBadWordsLogitsProcessor(LogitsProcessor):
    """
    [`LogitsProcessor`] that enforces that specified sequences will never be sampled.

    Args:
        bad_words_ids (`List[List[int]]`):
            List of list of token ids that are not allowed to be generated. In order to get the token ids of the words
            that should not appear in the generated text, use `tokenizer(bad_words, add_prefix_space=True,
            add_special_tokens=False).input_ids`.
        eos_token_id (`Union[int, List[int]]`):
            The id of the *end-of-sequence* token. Optionally, use a list to set multiple *end-of-sequence* tokens.
    """
    def __init__(self, bad_words_ids: List[List[int]], eos_token_id: Union[int, List[int]]):
        """
        This method initializes an instance of the NoBadWordsLogitsProcessor class.

        Args:
            self: The instance of the class.
            bad_words_ids (List[List[int]]): A list of lists containing the IDs of bad words. Each inner list represents
                a sequence of bad word IDs. The outer list contains multiple sequences of bad word IDs. The parameter
                is expected to be a non-empty list of lists of positive integers.
            eos_token_id (Union[int, List[int]]): An integer or a list of integers representing the end-of-sequence
                token ID(s). If a single integer is provided, it is converted to a list with a single element.
                If this parameter is None, it is automatically assigned an empty list. It is expected to be a positive
                integer or a list of positive integers.

        Returns:
            None.

        Raises:
            ValueError:
                - If `bad_words_ids` is not a non-empty list.
                - If `bad_words_ids` is not a list of lists.
                - If any list in `bad_words_ids` is not a list of positive integers.
                - If `eos_token_id` is not a positive integer or a list of positive integers.
                - If the banned words token sequences cannot have an empty list.
        """
        if not isinstance(bad_words_ids, List) or len(bad_words_ids) == 0:
            raise ValueError(f"`bad_words_ids` has to be a non-empty list, but is {bad_words_ids}.")
        if any(not isinstance(bad_word_ids, list) for bad_word_ids in bad_words_ids):
            raise ValueError(f"`bad_words_ids` has to be a list of lists, but is {bad_words_ids}.")
        if any(
                any((not isinstance(token_id, (int, np.integer)) or token_id < 0) for token_id in bad_word_ids)
                for bad_word_ids in bad_words_ids
        ):
            raise ValueError(
                f"Each list in `bad_words_ids` has to be a list of positive integers, but is {bad_words_ids}."
            )

        if eos_token_id is None:
            eos_token_id = []
        if isinstance(eos_token_id, int):
            eos_token_id = [eos_token_id]

        bad_words_ids = list(
            filter(lambda bad_token_seq: all(bad_token_seq != [i] for i in eos_token_id), bad_words_ids)
        )
        self.bad_words_id_length_1 = []
        self.bad_words_id_length_greater_than_1 = []
        for word in bad_words_ids:
            if len(word) == 1:
                self.bad_words_id_length_1.append(word[0])
            else:
                self.bad_words_id_length_greater_than_1.append(word)

        self.static_bad_words_mask: Optional[mindspore.Tensor] = None

        for banned_token_seq in self.bad_words_id_length_greater_than_1:
            if len(banned_token_seq) == 0:
                raise ValueError(f"Banned words token sequences {bad_words_ids} cannot have an empty list")

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method is a part of the 'NoBadWordsLogitsProcessor' class and is called '__call__'.
        It processes the input tensors and applies the 'No Bad Words' logic to the scores.

        Args:
            self: An instance of the 'NoBadWordsLogitsProcessor' class.
            input_ids (mindspore.Tensor):
                A tensor containing the input IDs.

                - Type: mindspore.Tensor
                - Purpose: Holds the input IDs for processing.
            scores (mindspore.Tensor):
                A tensor containing the scores.

                - Type: mindspore.Tensor
                - Purpose: Represents the scores to be processed.

        Returns:
            mindspore.Tensor:
                A tensor containing the processed scores.

                - Type: mindspore.Tensor
                - Purpose: Represents the scores after applying the 'No Bad Words' logic.

        Raises:
            None.
        """
        if self.static_bad_words_mask is None and len(self.bad_words_id_length_1) > 0:
            self.static_bad_words_mask = self._calc_static_bad_word_mask(scores)

        dynamic_banned_tokens = self._calc_banned_bad_words_ids(input_ids.tolist())
        scores = self._set_scores_to_inf_for_banned_tokens(scores, dynamic_banned_tokens)

        return scores

    def _calc_static_bad_word_mask(self, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method calculates a static bad word mask based on the given scores in the NoBadWordsLogitsProcessor class.

        Args:
            self (NoBadWordsLogitsProcessor): The instance of the NoBadWordsLogitsProcessor class.
            scores (mindspore.Tensor): A tensor containing scores used to calculate the static bad word mask.

        Returns:
            mindspore.Tensor: A boolean tensor representing the static bad word mask. The mask is created by setting
                the value to 1 at specific indices defined by 'bad_words_id_length_1' in the 'scores' tensor.

        Raises:
            None
        """
        static_bad_words_mask = ops.zeros(scores.shape[1])
        static_bad_words_mask[self.bad_words_id_length_1] = 1
        return static_bad_words_mask.unsqueeze(0).bool()

    def _tokens_match(self, prev_tokens: List[int], tokens: List[int]) -> bool:
        """
        Method _tokens_match in the class NoBadWordsLogitsProcessor checks if a sequence of tokens matches the previous tokens.

        Args:
            self: Instance of the NoBadWordsLogitsProcessor class.
            prev_tokens (List[int]): List of integers representing previous tokens to compare against.
            tokens (List[int]): List of integers representing tokens to check for a match.

        Returns:
            bool: Returns True if the tokens match the previous tokens, otherwise False.

        Raises:
            None.
        """
        if len(tokens) == 0:
            # if bad word tokens is just one token always ban it
            return True
        if len(tokens) > len(prev_tokens):
            # if bad word tokens are longer then prev input_ids they can't be equal
            return False
        return prev_tokens[-len(tokens):] == tokens

    def _calc_banned_bad_words_ids(self, prev_input_ids: List[List[int]]) -> Iterable[int]:
        """
        Calculates the banned bad word IDs based on the previous input IDs.

        Args:
            self (NoBadWordsLogitsProcessor): An instance of the NoBadWordsLogitsProcessor class.
            prev_input_ids (List[List[int]]): A list of lists representing the previous input IDs.
                Each inner list contains integers representing token IDs.

        Returns:
            Iterable[int]: An iterable containing the banned bad word IDs.

        Raises:
            None

        Description:
            This method takes the previous input IDs and calculates the banned bad word IDs based on the pre-defined
            bad word sequences. It iterates over each slice of the previous input IDs and checks if any of the bad word
            sequences match with the preceding tokens. If a match is found, the corresponding bad word ID is added to
            the list of banned tokens. The banned tokens for each slice are then appended to the final list of
            banned tokens.

        Note:
            - The bad word sequences are specified in the 'bad_words_id_length_greater_than_1' attribute of the
            NoBadWordsLogitsProcessor class.

        Example:
            ```python
            >>> processor = NoBadWordsLogitsProcessor()
            >>> prev_input_ids = [[1, 2, 3], [4, 5, 6]]
            >>> banned_tokens = processor._calc_banned_bad_words_ids(prev_input_ids)
            >>> # banned_tokens will contain the banned bad word IDs based on the previous input IDs.
            ```
        """
        banned_tokens = []
        for prev_input_ids_slice in prev_input_ids:
            banned_tokens_slice = []
            for banned_token_seq in self.bad_words_id_length_greater_than_1:
                if self._tokens_match(prev_input_ids_slice, banned_token_seq[:-1]):
                    banned_tokens_slice.append(banned_token_seq[-1])

            banned_tokens.append(banned_tokens_slice)

        return banned_tokens

    def _set_scores_to_inf_for_banned_tokens(
            self, scores: mindspore.Tensor, banned_tokens: List[List[int]]
    ) -> mindspore.Tensor:
        """
        Modifies the scores in place by setting the banned token positions to `-inf`. Banned token is expected to be a
        list of list of banned tokens to ban in the format [[batch index, vocabulary position],...

        Args:
            scores: logits distribution of shape (batch size, vocabulary size)
            banned_tokens: list of list of tokens to ban of length (batch_size)
        """
        banned_mask_list = []
        for idx, batch_banned_tokens in enumerate(banned_tokens):
            for token in batch_banned_tokens:
                # Eliminates invalid bad word IDs that are over the vocabulary size.
                if token <= scores.shape[1]:
                    banned_mask_list.append([idx, token])
                else:
                    logging.error(
                        "An invalid bad word ID is defined: %d. This ID is not contained in the "
                        "vocabulary, and is therefore ignored.", token
                    )
        if not banned_mask_list and self.static_bad_words_mask is None:
            return scores

        if banned_mask_list:
            banned_mask = mindspore.Tensor(banned_mask_list)
            indices = ops.ones(len(banned_mask))
            # A sparse tensor is generated from a list of coordinates: [[0, 1], [0, 2], [2, 0]]. A conversion to dense tensor generates:
            # [ 0  1  1 ]
            # [ 0  0  0 ]
            # [ 1  0  0 ]

            banned_mask = (
                mindspore.COOTensor(banned_mask,
                                    indices, scores.shape)
                .to_dense()
                .bool()
            )

            if self.static_bad_words_mask is not None:
                banned_mask = ops.bitwise_or(banned_mask, self.static_bad_words_mask)
        else:
            banned_mask = self.static_bad_words_mask

        scores = scores.masked_fill(banned_mask, -float("inf"))
        return scores

mindnlp.transformers.generation.logits_process.NoBadWordsLogitsProcessor.__call__(input_ids, scores)

This method is a part of the 'NoBadWordsLogitsProcessor' class and is called 'call'. It processes the input tensors and applies the 'No Bad Words' logic to the scores.

PARAMETER DESCRIPTION
self

An instance of the 'NoBadWordsLogitsProcessor' class.

input_ids

A tensor containing the input IDs.

  • Type: mindspore.Tensor
  • Purpose: Holds the input IDs for processing.

TYPE: Tensor

scores

A tensor containing the scores.

  • Type: mindspore.Tensor
  • Purpose: Represents the scores to be processed.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the processed scores.

  • Type: mindspore.Tensor
  • Purpose: Represents the scores after applying the 'No Bad Words' logic.
Source code in mindnlp/transformers/generation/logits_process.py
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method is a part of the 'NoBadWordsLogitsProcessor' class and is called '__call__'.
    It processes the input tensors and applies the 'No Bad Words' logic to the scores.

    Args:
        self: An instance of the 'NoBadWordsLogitsProcessor' class.
        input_ids (mindspore.Tensor):
            A tensor containing the input IDs.

            - Type: mindspore.Tensor
            - Purpose: Holds the input IDs for processing.
        scores (mindspore.Tensor):
            A tensor containing the scores.

            - Type: mindspore.Tensor
            - Purpose: Represents the scores to be processed.

    Returns:
        mindspore.Tensor:
            A tensor containing the processed scores.

            - Type: mindspore.Tensor
            - Purpose: Represents the scores after applying the 'No Bad Words' logic.

    Raises:
        None.
    """
    if self.static_bad_words_mask is None and len(self.bad_words_id_length_1) > 0:
        self.static_bad_words_mask = self._calc_static_bad_word_mask(scores)

    dynamic_banned_tokens = self._calc_banned_bad_words_ids(input_ids.tolist())
    scores = self._set_scores_to_inf_for_banned_tokens(scores, dynamic_banned_tokens)

    return scores

mindnlp.transformers.generation.logits_process.NoBadWordsLogitsProcessor.__init__(bad_words_ids, eos_token_id)

This method initializes an instance of the NoBadWordsLogitsProcessor class.

PARAMETER DESCRIPTION
self

The instance of the class.

bad_words_ids

A list of lists containing the IDs of bad words. Each inner list represents a sequence of bad word IDs. The outer list contains multiple sequences of bad word IDs. The parameter is expected to be a non-empty list of lists of positive integers.

TYPE: List[List[int]]

eos_token_id

An integer or a list of integers representing the end-of-sequence token ID(s). If a single integer is provided, it is converted to a list with a single element. If this parameter is None, it is automatically assigned an empty list. It is expected to be a positive integer or a list of positive integers.

TYPE: Union[int, List[int]]

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError
  • If bad_words_ids is not a non-empty list.
  • If bad_words_ids is not a list of lists.
  • If any list in bad_words_ids is not a list of positive integers.
  • If eos_token_id is not a positive integer or a list of positive integers.
  • If the banned words token sequences cannot have an empty list.
Source code in mindnlp/transformers/generation/logits_process.py
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
def __init__(self, bad_words_ids: List[List[int]], eos_token_id: Union[int, List[int]]):
    """
    This method initializes an instance of the NoBadWordsLogitsProcessor class.

    Args:
        self: The instance of the class.
        bad_words_ids (List[List[int]]): A list of lists containing the IDs of bad words. Each inner list represents
            a sequence of bad word IDs. The outer list contains multiple sequences of bad word IDs. The parameter
            is expected to be a non-empty list of lists of positive integers.
        eos_token_id (Union[int, List[int]]): An integer or a list of integers representing the end-of-sequence
            token ID(s). If a single integer is provided, it is converted to a list with a single element.
            If this parameter is None, it is automatically assigned an empty list. It is expected to be a positive
            integer or a list of positive integers.

    Returns:
        None.

    Raises:
        ValueError:
            - If `bad_words_ids` is not a non-empty list.
            - If `bad_words_ids` is not a list of lists.
            - If any list in `bad_words_ids` is not a list of positive integers.
            - If `eos_token_id` is not a positive integer or a list of positive integers.
            - If the banned words token sequences cannot have an empty list.
    """
    if not isinstance(bad_words_ids, List) or len(bad_words_ids) == 0:
        raise ValueError(f"`bad_words_ids` has to be a non-empty list, but is {bad_words_ids}.")
    if any(not isinstance(bad_word_ids, list) for bad_word_ids in bad_words_ids):
        raise ValueError(f"`bad_words_ids` has to be a list of lists, but is {bad_words_ids}.")
    if any(
            any((not isinstance(token_id, (int, np.integer)) or token_id < 0) for token_id in bad_word_ids)
            for bad_word_ids in bad_words_ids
    ):
        raise ValueError(
            f"Each list in `bad_words_ids` has to be a list of positive integers, but is {bad_words_ids}."
        )

    if eos_token_id is None:
        eos_token_id = []
    if isinstance(eos_token_id, int):
        eos_token_id = [eos_token_id]

    bad_words_ids = list(
        filter(lambda bad_token_seq: all(bad_token_seq != [i] for i in eos_token_id), bad_words_ids)
    )
    self.bad_words_id_length_1 = []
    self.bad_words_id_length_greater_than_1 = []
    for word in bad_words_ids:
        if len(word) == 1:
            self.bad_words_id_length_1.append(word[0])
        else:
            self.bad_words_id_length_greater_than_1.append(word)

    self.static_bad_words_mask: Optional[mindspore.Tensor] = None

    for banned_token_seq in self.bad_words_id_length_greater_than_1:
        if len(banned_token_seq) == 0:
            raise ValueError(f"Banned words token sequences {bad_words_ids} cannot have an empty list")

mindnlp.transformers.generation.logits_process.NoRepeatNGramLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that enforces no repetition of n-grams. See Fairseq.

PARAMETER DESCRIPTION
ngram_size

All ngrams of size ngram_size can only occur once.

TYPE: `int`

Source code in mindnlp/transformers/generation/logits_process.py
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
class NoRepeatNGramLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that enforces no repetition of n-grams. See
    [Fairseq](https://github.com/pytorch/fairseq/blob/a07cb6f40480928c9e0548b737aadd36ee66ac76/fairseq/sequence_generator.py#L345).

    Args:
        ngram_size (`int`):
            All ngrams of size `ngram_size` can only occur once.
    """
    def __init__(self, ngram_size: int):
        """
        Initializes a NoRepeatNGramLogitsProcessor object with the specified ngram size.

        Args:
            self: The instance of the class.
            ngram_size (int): The size of the n-gram to be used for processing the logits.
                It should be a strictly positive integer.

        Returns:
            None.

        Raises:
            ValueError: If the ngram_size is not an integer or is less than or equal to 0, a ValueError is raised
                with a descriptive error message.
        """
        if not isinstance(ngram_size, int) or ngram_size <= 0:
            raise ValueError(f"`ngram_size` has to be a strictly positive integer, but is {ngram_size}")
        self.ngram_size = ngram_size

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method processes the logits for generating token sequences without repeating n-grams.

        Args:
            self (NoRepeatNGramLogitsProcessor): An instance of the NoRepeatNGramLogitsProcessor class.
            input_ids (mindspore.Tensor): A tensor containing input token IDs for the current batch.
                The shape of the tensor should be compatible with the model's input requirements.
            scores (mindspore.Tensor): A tensor containing the logits scores for each token in the vocabulary.
                The shape of the tensor should be compatible with the model's output logits.

        Returns:
            mindspore.Tensor: A tensor containing the modified logits scores after applying the no repeat n-gram processing.
                The modified scores ensure that tokens forming prohibited n-grams have their logits set to negative infinity.

        Raises:
            ValueError: If the shape of input_ids or scores is incompatible.
            TypeError: If input_ids or scores are not of type mindspore.Tensor.
        """
        num_batch_hypotheses = scores.shape[0]
        cur_len = input_ids.shape[-1]
        banned_batch_tokens = _calc_banned_ngram_tokens(self.ngram_size, input_ids, num_batch_hypotheses, cur_len)

        for i, banned_tokens in enumerate(banned_batch_tokens):
            scores[i, banned_tokens] = -float("inf")

        return scores

mindnlp.transformers.generation.logits_process.NoRepeatNGramLogitsProcessor.__call__(input_ids, scores)

This method processes the logits for generating token sequences without repeating n-grams.

PARAMETER DESCRIPTION
self

An instance of the NoRepeatNGramLogitsProcessor class.

TYPE: NoRepeatNGramLogitsProcessor

input_ids

A tensor containing input token IDs for the current batch. The shape of the tensor should be compatible with the model's input requirements.

TYPE: Tensor

scores

A tensor containing the logits scores for each token in the vocabulary. The shape of the tensor should be compatible with the model's output logits.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the modified logits scores after applying the no repeat n-gram processing. The modified scores ensure that tokens forming prohibited n-grams have their logits set to negative infinity.

RAISES DESCRIPTION
ValueError

If the shape of input_ids or scores is incompatible.

TypeError

If input_ids or scores are not of type mindspore.Tensor.

Source code in mindnlp/transformers/generation/logits_process.py
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method processes the logits for generating token sequences without repeating n-grams.

    Args:
        self (NoRepeatNGramLogitsProcessor): An instance of the NoRepeatNGramLogitsProcessor class.
        input_ids (mindspore.Tensor): A tensor containing input token IDs for the current batch.
            The shape of the tensor should be compatible with the model's input requirements.
        scores (mindspore.Tensor): A tensor containing the logits scores for each token in the vocabulary.
            The shape of the tensor should be compatible with the model's output logits.

    Returns:
        mindspore.Tensor: A tensor containing the modified logits scores after applying the no repeat n-gram processing.
            The modified scores ensure that tokens forming prohibited n-grams have their logits set to negative infinity.

    Raises:
        ValueError: If the shape of input_ids or scores is incompatible.
        TypeError: If input_ids or scores are not of type mindspore.Tensor.
    """
    num_batch_hypotheses = scores.shape[0]
    cur_len = input_ids.shape[-1]
    banned_batch_tokens = _calc_banned_ngram_tokens(self.ngram_size, input_ids, num_batch_hypotheses, cur_len)

    for i, banned_tokens in enumerate(banned_batch_tokens):
        scores[i, banned_tokens] = -float("inf")

    return scores

mindnlp.transformers.generation.logits_process.NoRepeatNGramLogitsProcessor.__init__(ngram_size)

Initializes a NoRepeatNGramLogitsProcessor object with the specified ngram size.

PARAMETER DESCRIPTION
self

The instance of the class.

ngram_size

The size of the n-gram to be used for processing the logits. It should be a strictly positive integer.

TYPE: int

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If the ngram_size is not an integer or is less than or equal to 0, a ValueError is raised with a descriptive error message.

Source code in mindnlp/transformers/generation/logits_process.py
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
def __init__(self, ngram_size: int):
    """
    Initializes a NoRepeatNGramLogitsProcessor object with the specified ngram size.

    Args:
        self: The instance of the class.
        ngram_size (int): The size of the n-gram to be used for processing the logits.
            It should be a strictly positive integer.

    Returns:
        None.

    Raises:
        ValueError: If the ngram_size is not an integer or is less than or equal to 0, a ValueError is raised
            with a descriptive error message.
    """
    if not isinstance(ngram_size, int) or ngram_size <= 0:
        raise ValueError(f"`ngram_size` has to be a strictly positive integer, but is {ngram_size}")
    self.ngram_size = ngram_size

mindnlp.transformers.generation.logits_process.PrefixConstrainedLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that enforces constrained generation and is useful for prefix-conditioned constrained generation. See Autoregressive Entity Retrieval for more information.

PARAMETER DESCRIPTION
prefix_allowed_tokens_fn

(Callable[[int, torch.Tensor], List[int]]): This function constraints the beam search to allowed tokens only at each step. This function takes 2 arguments inputs_ids and the batch ID batch_id. It has to return a list with the allowed tokens for the next generation step conditioned on the previously generated tokens inputs_ids and the batch ID batch_id.

TYPE: Callable[[int, Tensor], List[int]]

Source code in mindnlp/transformers/generation/logits_process.py
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
class PrefixConstrainedLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] that enforces constrained generation and is useful for prefix-conditioned constrained
    generation. See [Autoregressive Entity Retrieval](https://arxiv.org/abs/2010.00904) for more information.

    Args:
        prefix_allowed_tokens_fn: (`Callable[[int, torch.Tensor], List[int]]`):
            This function constraints the beam search to allowed tokens only at each step. This function takes 2
            arguments `inputs_ids` and the batch ID `batch_id`. It has to return a list with the allowed tokens for the
            next generation step conditioned on the previously generated tokens `inputs_ids` and the batch ID
            `batch_id`.
    """
    def __init__(self, prefix_allowed_tokens_fn: Callable[[int, mindspore.Tensor], List[int]], num_beams: int):
        """
        Initialize the PrefixConstrainedLogitsProcessor object.

        Args:
            self: The object instance.
            prefix_allowed_tokens_fn (Callable[[int, mindspore.Tensor], List[int]]):
                A function that defines the allowed tokens for a given prefix. It takes an integer representing the
                batch size and a Tensor as input, and returns a list of integers representing the allowed tokens.
            num_beams (int): The number of beams to use during processing.

        Returns:
            None.

        Raises:
            TypeError: If prefix_allowed_tokens_fn is not a callable object or if num_beams is not an integer.
            ValueError: If num_beams is less than or equal to zero.
        """
        self._prefix_allowed_tokens_fn = prefix_allowed_tokens_fn
        self._num_beams = num_beams

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        '''
        Method '__call__' in the class 'PrefixConstrainedLogitsProcessor'.

        This method takes 3 parameters:

        Args:
            self: The instance of the class.
            input_ids (mindspore.Tensor): The input tensor containing token IDs.
                It is used to identify the batch and beam ID.
            scores (mindspore.Tensor): The input tensor containing scores for each token.

        Returns:
            mindspore.Tensor: Returns the processed tensor with added mask values.

        Raises:
            None
        '''
        mask = ops.full_like(scores, -math.inf)
        for batch_id, beam_sent in enumerate(input_ids.view(-1, self._num_beams, input_ids.shape[-1])):
            for beam_id, sent in enumerate(beam_sent):
                mask[batch_id * self._num_beams + beam_id, self._prefix_allowed_tokens_fn(batch_id, sent)] = 0

        return scores + mask

mindnlp.transformers.generation.logits_process.PrefixConstrainedLogitsProcessor.__call__(input_ids, scores)

Method 'call' in the class 'PrefixConstrainedLogitsProcessor'.

This method takes 3 parameters:

PARAMETER DESCRIPTION
self

The instance of the class.

input_ids

The input tensor containing token IDs. It is used to identify the batch and beam ID.

TYPE: Tensor

scores

The input tensor containing scores for each token.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: Returns the processed tensor with added mask values.

Source code in mindnlp/transformers/generation/logits_process.py
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    '''
    Method '__call__' in the class 'PrefixConstrainedLogitsProcessor'.

    This method takes 3 parameters:

    Args:
        self: The instance of the class.
        input_ids (mindspore.Tensor): The input tensor containing token IDs.
            It is used to identify the batch and beam ID.
        scores (mindspore.Tensor): The input tensor containing scores for each token.

    Returns:
        mindspore.Tensor: Returns the processed tensor with added mask values.

    Raises:
        None
    '''
    mask = ops.full_like(scores, -math.inf)
    for batch_id, beam_sent in enumerate(input_ids.view(-1, self._num_beams, input_ids.shape[-1])):
        for beam_id, sent in enumerate(beam_sent):
            mask[batch_id * self._num_beams + beam_id, self._prefix_allowed_tokens_fn(batch_id, sent)] = 0

    return scores + mask

mindnlp.transformers.generation.logits_process.PrefixConstrainedLogitsProcessor.__init__(prefix_allowed_tokens_fn, num_beams)

Initialize the PrefixConstrainedLogitsProcessor object.

PARAMETER DESCRIPTION
self

The object instance.

prefix_allowed_tokens_fn

A function that defines the allowed tokens for a given prefix. It takes an integer representing the batch size and a Tensor as input, and returns a list of integers representing the allowed tokens.

TYPE: Callable[[int, Tensor], List[int]]

num_beams

The number of beams to use during processing.

TYPE: int

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
TypeError

If prefix_allowed_tokens_fn is not a callable object or if num_beams is not an integer.

ValueError

If num_beams is less than or equal to zero.

Source code in mindnlp/transformers/generation/logits_process.py
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
def __init__(self, prefix_allowed_tokens_fn: Callable[[int, mindspore.Tensor], List[int]], num_beams: int):
    """
    Initialize the PrefixConstrainedLogitsProcessor object.

    Args:
        self: The object instance.
        prefix_allowed_tokens_fn (Callable[[int, mindspore.Tensor], List[int]]):
            A function that defines the allowed tokens for a given prefix. It takes an integer representing the
            batch size and a Tensor as input, and returns a list of integers representing the allowed tokens.
        num_beams (int): The number of beams to use during processing.

    Returns:
        None.

    Raises:
        TypeError: If prefix_allowed_tokens_fn is not a callable object or if num_beams is not an integer.
        ValueError: If num_beams is less than or equal to zero.
    """
    self._prefix_allowed_tokens_fn = prefix_allowed_tokens_fn
    self._num_beams = num_beams

mindnlp.transformers.generation.logits_process.RepetitionPenaltyLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] enforcing an exponential penalty on repeated sequences.

PARAMETER DESCRIPTION
repetition_penalty

The parameter for repetition penalty. 1.0 means no penalty. See this paper for more details.

TYPE: `float`

Source code in mindnlp/transformers/generation/logits_process.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
class RepetitionPenaltyLogitsProcessor(LogitsProcessor):
    r"""
    [`LogitsProcessor`] enforcing an exponential penalty on repeated sequences.

    Args:
        repetition_penalty (`float`):
            The parameter for repetition penalty. 1.0 means no penalty. See [this
            paper](https://arxiv.org/pdf/1909.05858.pdf) for more details.
    """
    def __init__(self, penalty: float):
        """
        Initializes a new RepetitionPenaltyLogitsProcessor with the specified penalty.

        Args:
            penalty (float): The penalty value to be applied to logits. It should be a strictly positive float.

        Returns:
            None.

        Raises:
            ValueError: If the penalty is not a float or if it is less than or equal to 0, a ValueError is raised.
        """
        if not isinstance(penalty, float) or (penalty <= 0):
            raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}")

        self.penalty = penalty

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method applies repetition penalty to the input logits based on the given input_ids and scores.

        Args:
            self (RepetitionPenaltyLogitsProcessor): An instance of the RepetitionPenaltyLogitsProcessor class.
            input_ids (mindspore.Tensor): A Tensor representing the input ids for which repetition penalty is applied.
            scores (mindspore.Tensor): A Tensor containing the scores used for applying repetition penalty.

        Returns:
            mindspore.Tensor: A Tensor with repetition penalty applied to the input logits.

        Raises:
            ValueError: If the input_ids and scores are not of the expected shape or type.
            IndexError: If there is an indexing error while processing the input_ids or scores.
            RuntimeError: If there is any runtime issue during the processing of the repetition penalty.

        Note:
            The repetition penalty factor is controlled by the 'penalty' attribute of the RepetitionPenaltyLogitsProcessor instance.
        """
        input_ids = ops.where(input_ids >= scores.shape[1], input_ids - scores.shape[1], input_ids)
        score = ops.gather(scores, 1, input_ids)

        # if score < 0 then repetition penalty has to be multiplied to reduce the previous token probability
        score = ops.where(score < 0, score * self.penalty, score / self.penalty)

        scores = ops.scatter(scores, 1, input_ids, score)
        return scores

mindnlp.transformers.generation.logits_process.RepetitionPenaltyLogitsProcessor.__call__(input_ids, scores)

This method applies repetition penalty to the input logits based on the given input_ids and scores.

PARAMETER DESCRIPTION
self

An instance of the RepetitionPenaltyLogitsProcessor class.

TYPE: RepetitionPenaltyLogitsProcessor

input_ids

A Tensor representing the input ids for which repetition penalty is applied.

TYPE: Tensor

scores

A Tensor containing the scores used for applying repetition penalty.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A Tensor with repetition penalty applied to the input logits.

RAISES DESCRIPTION
ValueError

If the input_ids and scores are not of the expected shape or type.

IndexError

If there is an indexing error while processing the input_ids or scores.

RuntimeError

If there is any runtime issue during the processing of the repetition penalty.

Note

The repetition penalty factor is controlled by the 'penalty' attribute of the RepetitionPenaltyLogitsProcessor instance.

Source code in mindnlp/transformers/generation/logits_process.py
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method applies repetition penalty to the input logits based on the given input_ids and scores.

    Args:
        self (RepetitionPenaltyLogitsProcessor): An instance of the RepetitionPenaltyLogitsProcessor class.
        input_ids (mindspore.Tensor): A Tensor representing the input ids for which repetition penalty is applied.
        scores (mindspore.Tensor): A Tensor containing the scores used for applying repetition penalty.

    Returns:
        mindspore.Tensor: A Tensor with repetition penalty applied to the input logits.

    Raises:
        ValueError: If the input_ids and scores are not of the expected shape or type.
        IndexError: If there is an indexing error while processing the input_ids or scores.
        RuntimeError: If there is any runtime issue during the processing of the repetition penalty.

    Note:
        The repetition penalty factor is controlled by the 'penalty' attribute of the RepetitionPenaltyLogitsProcessor instance.
    """
    input_ids = ops.where(input_ids >= scores.shape[1], input_ids - scores.shape[1], input_ids)
    score = ops.gather(scores, 1, input_ids)

    # if score < 0 then repetition penalty has to be multiplied to reduce the previous token probability
    score = ops.where(score < 0, score * self.penalty, score / self.penalty)

    scores = ops.scatter(scores, 1, input_ids, score)
    return scores

mindnlp.transformers.generation.logits_process.RepetitionPenaltyLogitsProcessor.__init__(penalty)

Initializes a new RepetitionPenaltyLogitsProcessor with the specified penalty.

PARAMETER DESCRIPTION
penalty

The penalty value to be applied to logits. It should be a strictly positive float.

TYPE: float

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If the penalty is not a float or if it is less than or equal to 0, a ValueError is raised.

Source code in mindnlp/transformers/generation/logits_process.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def __init__(self, penalty: float):
    """
    Initializes a new RepetitionPenaltyLogitsProcessor with the specified penalty.

    Args:
        penalty (float): The penalty value to be applied to logits. It should be a strictly positive float.

    Returns:
        None.

    Raises:
        ValueError: If the penalty is not a float or if it is less than or equal to 0, a ValueError is raised.
    """
    if not isinstance(penalty, float) or (penalty <= 0):
        raise ValueError(f"`penalty` has to be a strictly positive float, but is {penalty}")

    self.penalty = penalty

mindnlp.transformers.generation.logits_process.SequenceBiasLogitsProcessor

Bases: LogitsProcessor

[LogitsProcessor] that applies an additive bias on sequences. The bias is applied to the last token of a sequence when the next generated token can complete it. Consequently, to take the most of biasing sequences with more than one token, consider using beam methods (to gracefully work around partially completed sequences that have a negative bias) and applying the bias to their prefixes (to ensure the bias is applied earlier).

In order to get the token ids of the sequences that you want to bias, make sure to set add_prefix_space=True when initializing the tokenizer, and use tokenizer(bad_words, add_special_tokens=False).input_ids. The add_prefix_space argument is only supported for some slow tokenizers, as fast tokenizers' prefixing behaviours come from pre tokenizers. Read more here.

PARAMETER DESCRIPTION
sequence_bias

Dictionary that maps a sequence of tokens to its bias term. Positive biases increase the odds of the sequence being selected, while negative biases do the opposite. If a sequence has a length of 1, its bias will always be applied. Otherwise, the bias will only be applied if the sequence in question is about to be completed (in the token selection step after this processor is applied).

TYPE: `Dict[Tuple[int], float]`

Example
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
...
>>> model = AutoModelForCausalLM.from_pretrained("gpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
>>> inputs = tokenizer(["The full name of Donald is Donald"], return_tensors="pt")
...
>>> summary_ids = model.generate(inputs["input_ids"], max_new_tokens=4)
>>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0])
The full name of Donald is Donald J. Trump Jr
...
>>> # Now let's control generation through a bias. Please note that the tokenizer is initialized differently!
>>> tokenizer_with_prefix_space = AutoTokenizer.from_pretrained("gpt2", add_prefix_space=True)
...
...
>>> def get_tokens_as_tuple(word):
...     return tuple(tokenizer_with_prefix_space([word], add_special_tokens=False).input_ids[0])
...
...
>>> # If we add a negative bias without beam search, it may become "stuck" in a prefix without good continuations
>>> sequence_bias = {get_tokens_as_tuple("Trump"): -10.0}
>>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, sequence_bias=sequence_bias)
>>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
The full name of Donald is Donald J. Donald,
>>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, num_beams=4, sequence_bias=sequence_bias)
>>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
The full name of Donald is Donald Rumsfeld,
>>> # We can also add a positive bias to nudge the model towards specific tokens or continuations
>>> sequence_bias = {get_tokens_as_tuple("Donald Duck"): 10.0}
>>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, num_beams=4, sequence_bias=sequence_bias)
>>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
The full name of Donald is Donald Duck.
Source code in mindnlp/transformers/generation/logits_process.py
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
class SequenceBiasLogitsProcessor(LogitsProcessor):
    """
    [`LogitsProcessor`] that applies an additive bias on sequences. The bias is applied to the last token of a sequence
    when the next generated token can complete it. Consequently, to take the most of biasing sequences with more than
    one token, consider using beam methods (to gracefully work around partially completed sequences that have a
    negative bias) and applying the bias to their prefixes (to ensure the bias is applied earlier).

    <Tip>

    In order to get the token ids of the sequences that you want to bias, make sure to set `add_prefix_space=True` when
    initializing the tokenizer, and use `tokenizer(bad_words, add_special_tokens=False).input_ids`. The
    `add_prefix_space` argument is only supported for some slow tokenizers, as fast tokenizers' prefixing behaviours
    come from `pre tokenizers`. Read more [here](https://hf-mirror.com/docs/tokenizers/api/pre-tokenizers).

    </Tip>

    Args:
        sequence_bias (`Dict[Tuple[int], float]`):
            Dictionary that maps a sequence of tokens to its bias term. Positive biases increase the odds of the
            sequence being selected, while negative biases do the opposite. If a sequence has a length of 1, its bias
            will always be applied. Otherwise, the bias will only be applied if the sequence in question is about to be
            completed (in the token selection step after this processor is applied).

    Example:
        ```python
        >>> from transformers import AutoTokenizer, AutoModelForCausalLM
        ...
        >>> model = AutoModelForCausalLM.from_pretrained("gpt2")
        >>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
        >>> inputs = tokenizer(["The full name of Donald is Donald"], return_tensors="pt")
        ...
        >>> summary_ids = model.generate(inputs["input_ids"], max_new_tokens=4)
        >>> print(tokenizer.batch_decode(summary_ids, skip_special_tokens=True)[0])
        The full name of Donald is Donald J. Trump Jr
        ...
        >>> # Now let's control generation through a bias. Please note that the tokenizer is initialized differently!
        >>> tokenizer_with_prefix_space = AutoTokenizer.from_pretrained("gpt2", add_prefix_space=True)
        ...
        ...
        >>> def get_tokens_as_tuple(word):
        ...     return tuple(tokenizer_with_prefix_space([word], add_special_tokens=False).input_ids[0])
        ...
        ...
        >>> # If we add a negative bias without beam search, it may become "stuck" in a prefix without good continuations
        >>> sequence_bias = {get_tokens_as_tuple("Trump"): -10.0}
        >>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, sequence_bias=sequence_bias)
        >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
        The full name of Donald is Donald J. Donald,
        >>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, num_beams=4, sequence_bias=sequence_bias)
        >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
        The full name of Donald is Donald Rumsfeld,
        >>> # We can also add a positive bias to nudge the model towards specific tokens or continuations
        >>> sequence_bias = {get_tokens_as_tuple("Donald Duck"): 10.0}
        >>> biased_ids = model.generate(inputs["input_ids"], max_new_tokens=4, num_beams=4, sequence_bias=sequence_bias)
        >>> print(tokenizer.batch_decode(biased_ids, skip_special_tokens=True)[0])
        The full name of Donald is Donald Duck.
        ```
    """
    def __init__(self, sequence_bias: Dict[Tuple[int], float]):
        """
        Initializes an instance of the SequenceBiasLogitsProcessor class.

        Args:
            self: The object instance.
            sequence_bias (Dict[Tuple[int], float]): A dictionary containing the sequence bias values.
                The keys are tuples of integers representing the sequence positions, and the values are floats
                representing the bias for each position.

        Returns:
            None.

        Raises:
            None.
        """
        self.sequence_bias = sequence_bias
        self._validate_arguments()

        # Bias variables that will be populated on the first call (for retrocompatibility purposes, the vocabulary size
        # is infered in the first usage, which inhibits initializing here)
        self.length_1_bias = None
        self.prepared_bias_variables = False

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        __call__

        This method processes the input_ids and scores to apply sequence bias to the logits.

        Args:
            self (SequenceBiasLogitsProcessor): The instance of the SequenceBiasLogitsProcessor class.
            input_ids (mindspore.Tensor): The input tensor containing the tokenized input sequence.
            scores (mindspore.Tensor): The input tensor containing the scores/logits to be processed.

        Returns:
            mindspore.Tensor: The processed scores/logits after applying sequence bias.

        Raises:
            ValueError: If the input_ids or scores are not of type mindspore.Tensor.
            ValueError: If the input_ids and scores do not have compatible shapes for processing.
        """
        # 1 - Prepares the bias tensors. This is only needed the first time the logit processor is called.
        if not self.prepared_bias_variables:
            self._prepare_bias_variables(scores)

        # 2 - prepares an empty bias to add
        bias = ops.zeros_like(scores)

        # 3 - include the bias from length = 1
        bias += self.length_1_bias

        # 4 - include the bias from length > 1, after determining which biased sequences may be completed.
        for sequence_ids, sequence_bias in self.sequence_bias.items():
            if len(sequence_ids) == 1:  # the sequence is of length 1, already applied
                continue
            if len(sequence_ids) > input_ids.shape[1]:  # the sequence is longer than the context, ignore
                continue
            prefix_length = len(sequence_ids) - 1
            last_token = sequence_ids[-1]
            matching_rows = ops.eq(
                input_ids[:, -prefix_length:],
                mindspore.tensor(sequence_ids[:-1], dtype=input_ids.dtype),
            ).prod(dim=1)
            bias[:, last_token] += ops.where(
                matching_rows.bool(),
                mindspore.tensor(sequence_bias),
                mindspore.tensor(0.0),
            )

        # 5 - apply the bias to the scores
        scores = scores + bias
        return scores

mindnlp.transformers.generation.logits_process.SequenceBiasLogitsProcessor.__call__(input_ids, scores)

call

This method processes the input_ids and scores to apply sequence bias to the logits.

PARAMETER DESCRIPTION
self

The instance of the SequenceBiasLogitsProcessor class.

TYPE: SequenceBiasLogitsProcessor

input_ids

The input tensor containing the tokenized input sequence.

TYPE: Tensor

scores

The input tensor containing the scores/logits to be processed.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: The processed scores/logits after applying sequence bias.

RAISES DESCRIPTION
ValueError

If the input_ids or scores are not of type mindspore.Tensor.

ValueError

If the input_ids and scores do not have compatible shapes for processing.

Source code in mindnlp/transformers/generation/logits_process.py
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    __call__

    This method processes the input_ids and scores to apply sequence bias to the logits.

    Args:
        self (SequenceBiasLogitsProcessor): The instance of the SequenceBiasLogitsProcessor class.
        input_ids (mindspore.Tensor): The input tensor containing the tokenized input sequence.
        scores (mindspore.Tensor): The input tensor containing the scores/logits to be processed.

    Returns:
        mindspore.Tensor: The processed scores/logits after applying sequence bias.

    Raises:
        ValueError: If the input_ids or scores are not of type mindspore.Tensor.
        ValueError: If the input_ids and scores do not have compatible shapes for processing.
    """
    # 1 - Prepares the bias tensors. This is only needed the first time the logit processor is called.
    if not self.prepared_bias_variables:
        self._prepare_bias_variables(scores)

    # 2 - prepares an empty bias to add
    bias = ops.zeros_like(scores)

    # 3 - include the bias from length = 1
    bias += self.length_1_bias

    # 4 - include the bias from length > 1, after determining which biased sequences may be completed.
    for sequence_ids, sequence_bias in self.sequence_bias.items():
        if len(sequence_ids) == 1:  # the sequence is of length 1, already applied
            continue
        if len(sequence_ids) > input_ids.shape[1]:  # the sequence is longer than the context, ignore
            continue
        prefix_length = len(sequence_ids) - 1
        last_token = sequence_ids[-1]
        matching_rows = ops.eq(
            input_ids[:, -prefix_length:],
            mindspore.tensor(sequence_ids[:-1], dtype=input_ids.dtype),
        ).prod(dim=1)
        bias[:, last_token] += ops.where(
            matching_rows.bool(),
            mindspore.tensor(sequence_bias),
            mindspore.tensor(0.0),
        )

    # 5 - apply the bias to the scores
    scores = scores + bias
    return scores

mindnlp.transformers.generation.logits_process.SequenceBiasLogitsProcessor.__init__(sequence_bias)

Initializes an instance of the SequenceBiasLogitsProcessor class.

PARAMETER DESCRIPTION
self

The object instance.

sequence_bias

A dictionary containing the sequence bias values. The keys are tuples of integers representing the sequence positions, and the values are floats representing the bias for each position.

TYPE: Dict[Tuple[int], float]

RETURNS DESCRIPTION

None.

Source code in mindnlp/transformers/generation/logits_process.py
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
def __init__(self, sequence_bias: Dict[Tuple[int], float]):
    """
    Initializes an instance of the SequenceBiasLogitsProcessor class.

    Args:
        self: The object instance.
        sequence_bias (Dict[Tuple[int], float]): A dictionary containing the sequence bias values.
            The keys are tuples of integers representing the sequence positions, and the values are floats
            representing the bias for each position.

    Returns:
        None.

    Raises:
        None.
    """
    self.sequence_bias = sequence_bias
    self._validate_arguments()

    # Bias variables that will be populated on the first call (for retrocompatibility purposes, the vocabulary size
    # is infered in the first usage, which inhibits initializing here)
    self.length_1_bias = None
    self.prepared_bias_variables = False

mindnlp.transformers.generation.logits_process.SuppressTokensAtBeginLogitsProcessor

Bases: LogitsProcessor

[SuppressTokensAtBeginLogitsProcessor] supresses a list of tokens as soon as the generate function starts generating using begin_index tokens. This should ensure that the tokens defined by begin_suppress_tokens at not sampled at the begining of the generation.

Source code in mindnlp/transformers/generation/logits_process.py
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
class SuppressTokensAtBeginLogitsProcessor(LogitsProcessor):
    r"""
    [`SuppressTokensAtBeginLogitsProcessor`] supresses a list of tokens as soon as the `generate` function starts
    generating using `begin_index` tokens. This should ensure that the tokens defined by `begin_suppress_tokens` at not
    sampled at the begining of the generation.
    """
    def __init__(self, begin_suppress_tokens, begin_index):
        """
        Initializes a new instance of the SuppressTokensAtBeginLogitsProcessor class.

        Args:
            self (SuppressTokensAtBeginLogitsProcessor): The current instance of the class.
            begin_suppress_tokens (list): A list of tokens to suppress at the beginning of the logits.
            begin_index (int): The index indicating the beginning of the logits.

        Returns:
            None.

        Raises:
            None.
        """
        self.begin_suppress_tokens = list(begin_suppress_tokens)
        self.begin_index = begin_index

    def __call__(self, input_ids, scores):
        """
        This method __call__ is a part of the class SuppressTokensAtBeginLogitsProcessor and is used to process
        input_ids and scores by suppressing certain tokens at the beginning.

        Args:
            self (object): The instance of the class SuppressTokensAtBeginLogitsProcessor.
            input_ids (numpy.ndarray): The input token IDs, expected to be a 2D array.
            scores (numpy.ndarray): The input logits scores, expected to be a 2D array.

        Returns:
            None: This method directly modifies the 'scores' array in place.

        Raises:
            None.
        """
        if input_ids.shape[1] == self.begin_index:
            scores[:, self.begin_suppress_tokens] = -float("inf")

        return scores

mindnlp.transformers.generation.logits_process.SuppressTokensAtBeginLogitsProcessor.__call__(input_ids, scores)

This method call is a part of the class SuppressTokensAtBeginLogitsProcessor and is used to process input_ids and scores by suppressing certain tokens at the beginning.

PARAMETER DESCRIPTION
self

The instance of the class SuppressTokensAtBeginLogitsProcessor.

TYPE: object

input_ids

The input token IDs, expected to be a 2D array.

TYPE: ndarray

scores

The input logits scores, expected to be a 2D array.

TYPE: ndarray

RETURNS DESCRIPTION
None

This method directly modifies the 'scores' array in place.

Source code in mindnlp/transformers/generation/logits_process.py
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
def __call__(self, input_ids, scores):
    """
    This method __call__ is a part of the class SuppressTokensAtBeginLogitsProcessor and is used to process
    input_ids and scores by suppressing certain tokens at the beginning.

    Args:
        self (object): The instance of the class SuppressTokensAtBeginLogitsProcessor.
        input_ids (numpy.ndarray): The input token IDs, expected to be a 2D array.
        scores (numpy.ndarray): The input logits scores, expected to be a 2D array.

    Returns:
        None: This method directly modifies the 'scores' array in place.

    Raises:
        None.
    """
    if input_ids.shape[1] == self.begin_index:
        scores[:, self.begin_suppress_tokens] = -float("inf")

    return scores

mindnlp.transformers.generation.logits_process.SuppressTokensAtBeginLogitsProcessor.__init__(begin_suppress_tokens, begin_index)

Initializes a new instance of the SuppressTokensAtBeginLogitsProcessor class.

PARAMETER DESCRIPTION
self

The current instance of the class.

TYPE: SuppressTokensAtBeginLogitsProcessor

begin_suppress_tokens

A list of tokens to suppress at the beginning of the logits.

TYPE: list

begin_index

The index indicating the beginning of the logits.

TYPE: int

RETURNS DESCRIPTION

None.

Source code in mindnlp/transformers/generation/logits_process.py
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
def __init__(self, begin_suppress_tokens, begin_index):
    """
    Initializes a new instance of the SuppressTokensAtBeginLogitsProcessor class.

    Args:
        self (SuppressTokensAtBeginLogitsProcessor): The current instance of the class.
        begin_suppress_tokens (list): A list of tokens to suppress at the beginning of the logits.
        begin_index (int): The index indicating the beginning of the logits.

    Returns:
        None.

    Raises:
        None.
    """
    self.begin_suppress_tokens = list(begin_suppress_tokens)
    self.begin_index = begin_index

mindnlp.transformers.generation.logits_process.SuppressTokensLogitsProcessor

Bases: LogitsProcessor

This processor can be used to suppress a list of tokens. The processor will set their log probs to -inf so that they are not sampled.

Source code in mindnlp/transformers/generation/logits_process.py
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
class SuppressTokensLogitsProcessor(LogitsProcessor):
    r"""
    This processor can be used to suppress a list of tokens. The processor will set their log probs to `-inf`
    so that they are not sampled.
    """
    def __init__(self, suppress_tokens):
        """
        Initializes an instance of the SuppressTokensLogitsProcessor class.

        Args:
            self (object): The instance of the class.
            suppress_tokens (iterable): A list of tokens to be suppressed.

        Returns:
            None.

        Raises:
            TypeError: If the suppress_tokens parameter is not an iterable.
        """
        self.suppress_tokens = list(suppress_tokens)

    def __call__(self, input_ids, scores):
        """
        The '__call__' method in the 'SuppressTokensLogitsProcessor' class modifies the 'scores' array by setting
        the values of specific tokens to negative infinity. It takes three parameters: 'self', 'input_ids', and 'scores'.
        The method does not return any value.

        Args:
            self (SuppressTokensLogitsProcessor): An instance of the 'SuppressTokensLogitsProcessor' class.
            input_ids (Tensor): A tensor containing the input token IDs.
            scores (Tensor): A tensor containing the scores for each token.

        Returns:
            None: The method modifies the 'scores' array in-place.

        Raises:
            None.
        """
        scores[:, self.suppress_tokens] = -float("inf")
        return scores

mindnlp.transformers.generation.logits_process.SuppressTokensLogitsProcessor.__call__(input_ids, scores)

The 'call' method in the 'SuppressTokensLogitsProcessor' class modifies the 'scores' array by setting the values of specific tokens to negative infinity. It takes three parameters: 'self', 'input_ids', and 'scores'. The method does not return any value.

PARAMETER DESCRIPTION
self

An instance of the 'SuppressTokensLogitsProcessor' class.

TYPE: SuppressTokensLogitsProcessor

input_ids

A tensor containing the input token IDs.

TYPE: Tensor

scores

A tensor containing the scores for each token.

TYPE: Tensor

RETURNS DESCRIPTION
None

The method modifies the 'scores' array in-place.

Source code in mindnlp/transformers/generation/logits_process.py
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
def __call__(self, input_ids, scores):
    """
    The '__call__' method in the 'SuppressTokensLogitsProcessor' class modifies the 'scores' array by setting
    the values of specific tokens to negative infinity. It takes three parameters: 'self', 'input_ids', and 'scores'.
    The method does not return any value.

    Args:
        self (SuppressTokensLogitsProcessor): An instance of the 'SuppressTokensLogitsProcessor' class.
        input_ids (Tensor): A tensor containing the input token IDs.
        scores (Tensor): A tensor containing the scores for each token.

    Returns:
        None: The method modifies the 'scores' array in-place.

    Raises:
        None.
    """
    scores[:, self.suppress_tokens] = -float("inf")
    return scores

mindnlp.transformers.generation.logits_process.SuppressTokensLogitsProcessor.__init__(suppress_tokens)

Initializes an instance of the SuppressTokensLogitsProcessor class.

PARAMETER DESCRIPTION
self

The instance of the class.

TYPE: object

suppress_tokens

A list of tokens to be suppressed.

TYPE: iterable

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
TypeError

If the suppress_tokens parameter is not an iterable.

Source code in mindnlp/transformers/generation/logits_process.py
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
def __init__(self, suppress_tokens):
    """
    Initializes an instance of the SuppressTokensLogitsProcessor class.

    Args:
        self (object): The instance of the class.
        suppress_tokens (iterable): A list of tokens to be suppressed.

    Returns:
        None.

    Raises:
        TypeError: If the suppress_tokens parameter is not an iterable.
    """
    self.suppress_tokens = list(suppress_tokens)

mindnlp.transformers.generation.logits_process.TemperatureLogitsWarper

Bases: LogitsWarper

[TemperatureLogitsWarper] for temperature (exponential scaling output probability distribution). Args: temperature (:obj:float): The value used to module the logits distribution.

Source code in mindnlp/transformers/generation/logits_process.py
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
class TemperatureLogitsWarper(LogitsWarper):
    r"""
    [`TemperatureLogitsWarper`] for temperature (exponential scaling output probability distribution).
    Args:
        temperature (:obj:`float`):
            The value used to module the logits distribution.
    """
    def __init__(self, temperature: float):
        """
        Initializes a TemperatureLogitsWarper object with the provided temperature.

        Args:
            self: The object instance itself.
            temperature (float): The temperature value to be set for the TemperatureLogitsWarper object.
                Must be a strictly positive float.

        Returns:
            None.

        Raises:
            ValueError: If the provided temperature is not a float or is not strictly greater than 0.
        """
        if not isinstance(temperature, float) or not temperature > 0:
            raise ValueError(f"`temperature` has to be a strictly positive float, but is {temperature}")

        self.temperature = temperature

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method adjusts the input 'scores' by dividing them by a temperature value and returns the adjusted scores.

        Args:
            self (TemperatureLogitsWarper): The instance of the TemperatureLogitsWarper class.
            input_ids (mindspore.Tensor): The input tensor containing the IDs of the input data.
            scores (mindspore.Tensor): The input tensor containing the scores to be adjusted.

        Returns:
            mindspore.Tensor: A tensor containing the adjusted scores after dividing them by the temperature value.

        Raises:
            None
        """
        scores = scores / self.temperature
        return scores

mindnlp.transformers.generation.logits_process.TemperatureLogitsWarper.__call__(input_ids, scores)

This method adjusts the input 'scores' by dividing them by a temperature value and returns the adjusted scores.

PARAMETER DESCRIPTION
self

The instance of the TemperatureLogitsWarper class.

TYPE: TemperatureLogitsWarper

input_ids

The input tensor containing the IDs of the input data.

TYPE: Tensor

scores

The input tensor containing the scores to be adjusted.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the adjusted scores after dividing them by the temperature value.

Source code in mindnlp/transformers/generation/logits_process.py
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method adjusts the input 'scores' by dividing them by a temperature value and returns the adjusted scores.

    Args:
        self (TemperatureLogitsWarper): The instance of the TemperatureLogitsWarper class.
        input_ids (mindspore.Tensor): The input tensor containing the IDs of the input data.
        scores (mindspore.Tensor): The input tensor containing the scores to be adjusted.

    Returns:
        mindspore.Tensor: A tensor containing the adjusted scores after dividing them by the temperature value.

    Raises:
        None
    """
    scores = scores / self.temperature
    return scores

mindnlp.transformers.generation.logits_process.TemperatureLogitsWarper.__init__(temperature)

Initializes a TemperatureLogitsWarper object with the provided temperature.

PARAMETER DESCRIPTION
self

The object instance itself.

temperature

The temperature value to be set for the TemperatureLogitsWarper object. Must be a strictly positive float.

TYPE: float

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If the provided temperature is not a float or is not strictly greater than 0.

Source code in mindnlp/transformers/generation/logits_process.py
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
def __init__(self, temperature: float):
    """
    Initializes a TemperatureLogitsWarper object with the provided temperature.

    Args:
        self: The object instance itself.
        temperature (float): The temperature value to be set for the TemperatureLogitsWarper object.
            Must be a strictly positive float.

    Returns:
        None.

    Raises:
        ValueError: If the provided temperature is not a float or is not strictly greater than 0.
    """
    if not isinstance(temperature, float) or not temperature > 0:
        raise ValueError(f"`temperature` has to be a strictly positive float, but is {temperature}")

    self.temperature = temperature

mindnlp.transformers.generation.logits_process.TopKLogitsWarper

Bases: LogitsWarper

[LogitsWarper] that performs top-k, i.e. restricting to the k highest probability elements.

PARAMETER DESCRIPTION
top_k

The number of highest probability vocabulary tokens to keep for top-k-filtering.

TYPE: `int`

filter_value

All filtered values will be set to this float value.

TYPE: `float`, *optional*, defaults to `-float("Inf")` DEFAULT: -float('Inf')

min_tokens_to_keep

Minimum number of tokens that cannot be filtered.

TYPE: `int`, *optional*, defaults to 1 DEFAULT: 1

Source code in mindnlp/transformers/generation/logits_process.py
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
class TopKLogitsWarper(LogitsWarper):
    r"""
    [`LogitsWarper`] that performs top-k, i.e. restricting to the k highest probability elements.

    Args:
        top_k (`int`):
            The number of highest probability vocabulary tokens to keep for top-k-filtering.
        filter_value (`float`, *optional*, defaults to `-float("Inf")`):
            All filtered values will be set to this float value.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Minimum number of tokens that cannot be filtered.
    """
    def __init__(self, top_k: int, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
        """Initialize the TopKLogitsWarper object.

        Args:
            top_k (int): The number of top logits to keep. Must be a positive integer.
            filter_value (float, optional): The value used to filter logits. Defaults to negative infinity.
            min_tokens_to_keep (int, optional): The minimum number of tokens to keep. Defaults to 1.

        Returns:
            None.

        Raises:
            ValueError: If top_k is not a positive integer.
        """
        if not isinstance(top_k, int) or top_k <= 0:
            raise ValueError(f"`top_k` has to be a strictly positive integer, but is {top_k}")

        self.top_k = max(top_k, min_tokens_to_keep)
        self.filter_value = filter_value

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method, named '__call__', is defined within the 'TopKLogitsWarper' class and is used to process
        input_ids and scores to obtain a mindspore.Tensor result.

        Args:
            self: The instance of the 'TopKLogitsWarper' class.
            input_ids (mindspore.Tensor): The input tensor containing the input IDs.
            scores (mindspore.Tensor): The tensor containing the scores.

        Returns:
            mindspore.Tensor: A tensor containing the processed scores after applying the top-k warping.

        Raises:
            ValueError: If the filter_value is not set to -float('Inf').
            TypeError: If the input_ids or scores are not of type mindspore.Tensor.
            RuntimeError: If an error occurs during the execution of the method.
        """
        if self.filter_value == -float("Inf"):
            self.filter_value = float(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).min)
        top_k = min(self.top_k, scores.shape[-1])  # Safety check
        # Remove all tokens with a probability less than the last token of the top-k
        indices_to_remove = scores < ops.topk(scores, top_k)[0][..., -1, None]
        scores = scores.masked_fill(indices_to_remove, self.filter_value)
        return scores

mindnlp.transformers.generation.logits_process.TopKLogitsWarper.__call__(input_ids, scores)

This method, named 'call', is defined within the 'TopKLogitsWarper' class and is used to process input_ids and scores to obtain a mindspore.Tensor result.

PARAMETER DESCRIPTION
self

The instance of the 'TopKLogitsWarper' class.

input_ids

The input tensor containing the input IDs.

TYPE: Tensor

scores

The tensor containing the scores.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the processed scores after applying the top-k warping.

RAISES DESCRIPTION
ValueError

If the filter_value is not set to -float('Inf').

TypeError

If the input_ids or scores are not of type mindspore.Tensor.

RuntimeError

If an error occurs during the execution of the method.

Source code in mindnlp/transformers/generation/logits_process.py
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method, named '__call__', is defined within the 'TopKLogitsWarper' class and is used to process
    input_ids and scores to obtain a mindspore.Tensor result.

    Args:
        self: The instance of the 'TopKLogitsWarper' class.
        input_ids (mindspore.Tensor): The input tensor containing the input IDs.
        scores (mindspore.Tensor): The tensor containing the scores.

    Returns:
        mindspore.Tensor: A tensor containing the processed scores after applying the top-k warping.

    Raises:
        ValueError: If the filter_value is not set to -float('Inf').
        TypeError: If the input_ids or scores are not of type mindspore.Tensor.
        RuntimeError: If an error occurs during the execution of the method.
    """
    if self.filter_value == -float("Inf"):
        self.filter_value = float(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).min)
    top_k = min(self.top_k, scores.shape[-1])  # Safety check
    # Remove all tokens with a probability less than the last token of the top-k
    indices_to_remove = scores < ops.topk(scores, top_k)[0][..., -1, None]
    scores = scores.masked_fill(indices_to_remove, self.filter_value)
    return scores

mindnlp.transformers.generation.logits_process.TopKLogitsWarper.__init__(top_k, filter_value=-float('Inf'), min_tokens_to_keep=1)

Initialize the TopKLogitsWarper object.

PARAMETER DESCRIPTION
top_k

The number of top logits to keep. Must be a positive integer.

TYPE: int

filter_value

The value used to filter logits. Defaults to negative infinity.

TYPE: float DEFAULT: -float('Inf')

min_tokens_to_keep

The minimum number of tokens to keep. Defaults to 1.

TYPE: int DEFAULT: 1

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError

If top_k is not a positive integer.

Source code in mindnlp/transformers/generation/logits_process.py
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
def __init__(self, top_k: int, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
    """Initialize the TopKLogitsWarper object.

    Args:
        top_k (int): The number of top logits to keep. Must be a positive integer.
        filter_value (float, optional): The value used to filter logits. Defaults to negative infinity.
        min_tokens_to_keep (int, optional): The minimum number of tokens to keep. Defaults to 1.

    Returns:
        None.

    Raises:
        ValueError: If top_k is not a positive integer.
    """
    if not isinstance(top_k, int) or top_k <= 0:
        raise ValueError(f"`top_k` has to be a strictly positive integer, but is {top_k}")

    self.top_k = max(top_k, min_tokens_to_keep)
    self.filter_value = filter_value

mindnlp.transformers.generation.logits_process.TopPLogitsWarper

Bases: LogitsWarper

[LogitsWarper] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off.

PARAMETER DESCRIPTION
top_p

If set to < 1, only the smallest set of most probable tokens with probabilities that add up to top_p or higher are kept for generation.

TYPE: `float`

filter_value

All filtered values will be set to this float value.

TYPE: `float`, *optional*, defaults to `-float("Inf")` DEFAULT: -float('Inf')

min_tokens_to_keep

Minimum number of tokens that cannot be filtered.

TYPE: `int`, *optional*, defaults to 1 DEFAULT: 1

Source code in mindnlp/transformers/generation/logits_process.py
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
class TopPLogitsWarper(LogitsWarper):
    """
    [`LogitsWarper`] that performs top-p, i.e. restricting to top tokens summing to prob_cut_off <= prob_cut_off.

    Args:
        top_p (`float`):
            If set to < 1, only the smallest set of most probable tokens with probabilities that add up to `top_p` or
            higher are kept for generation.
        filter_value (`float`, *optional*, defaults to `-float("Inf")`):
            All filtered values will be set to this float value.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Minimum number of tokens that cannot be filtered.
    """
    def __init__(self, top_p: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
        """
        Initializes an instance of the TopPLogitsWarper class.

        Args:
            top_p (float): The value representing the top cumulative probability for truncation.
                Must be a float greater than 0 and less than 1.
            filter_value (float, optional): The filter value used for truncation. Defaults to negative infinity.
            min_tokens_to_keep (int, optional): The minimum number of tokens to keep after truncation.
                Must be a positive integer greater than or equal to 1.

        Returns:
            None.

        Raises:
            ValueError:
                - If top_p is not a float greater than 0 or less than 1.
                - If min_tokens_to_keep is not a positive integer.
        """
        top_p = float(top_p)
        if top_p < 0 or top_p > 1.0:
            raise ValueError(f"`top_p` has to be a float > 0 and < 1, but is {top_p}")
        if not isinstance(min_tokens_to_keep, int) or min_tokens_to_keep < 1:
            raise ValueError(f"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}")

        self.top_p = top_p
        self.filter_value = filter_value
        self.min_tokens_to_keep = min_tokens_to_keep

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method '__call__' in the class 'TopPLogitsWarper' applies the Top-p sampling strategy to filter out
        low probability tokens from the input scores tensor.

        Args:
            self: An instance of the TopPLogitsWarper class.
            input_ids (mindspore.Tensor): The input tensor containing the token IDs.
            scores (mindspore.Tensor): The input tensor containing the model scores for each token.

        Returns:
            mindspore.Tensor: A tensor representing the filtered scores after applying the Top-p sampling strategy.

        Raises:
            ValueError: If the filter_value is set to negative infinity.
            TypeError: If the input tensors are not of type mindspore.Tensor.
            RuntimeError: If an error occurs during the sorting, softmax calculation, or masking of scores.
        """
        if self.filter_value == -float("Inf"):
            self.filter_value = float(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).min)
        # scores = ops.select(ops.isneginf(scores), mindspore.tensor(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).min), scores)
        sorted_logits, sorted_indices = ops.sort(scores, descending=False)
        cumulative_probs = ops.cumsum(ops.softmax(sorted_logits, dim=-1), dim=-1)

        # Remove tokens with cumulative top_p above the threshold (token with 0 are kept)
        sorted_indices_to_remove = cumulative_probs <= (1 - self.top_p)
        # Keep at least min_tokens_to_keep
        sorted_indices_to_remove[..., -self.min_tokens_to_keep:] = 0
        sorted_indices_to_remove = sorted_indices_to_remove.astype(mindspore.int32)

        # scatter sorted tensors to original indexing
        indices_to_remove = ops.scatter(sorted_indices_to_remove, 1, sorted_indices, sorted_indices_to_remove)
        scores = scores.masked_fill(indices_to_remove.astype(mindspore.bool_), self.filter_value)
        return scores

mindnlp.transformers.generation.logits_process.TopPLogitsWarper.__call__(input_ids, scores)

This method 'call' in the class 'TopPLogitsWarper' applies the Top-p sampling strategy to filter out low probability tokens from the input scores tensor.

PARAMETER DESCRIPTION
self

An instance of the TopPLogitsWarper class.

input_ids

The input tensor containing the token IDs.

TYPE: Tensor

scores

The input tensor containing the model scores for each token.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor representing the filtered scores after applying the Top-p sampling strategy.

RAISES DESCRIPTION
ValueError

If the filter_value is set to negative infinity.

TypeError

If the input tensors are not of type mindspore.Tensor.

RuntimeError

If an error occurs during the sorting, softmax calculation, or masking of scores.

Source code in mindnlp/transformers/generation/logits_process.py
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method '__call__' in the class 'TopPLogitsWarper' applies the Top-p sampling strategy to filter out
    low probability tokens from the input scores tensor.

    Args:
        self: An instance of the TopPLogitsWarper class.
        input_ids (mindspore.Tensor): The input tensor containing the token IDs.
        scores (mindspore.Tensor): The input tensor containing the model scores for each token.

    Returns:
        mindspore.Tensor: A tensor representing the filtered scores after applying the Top-p sampling strategy.

    Raises:
        ValueError: If the filter_value is set to negative infinity.
        TypeError: If the input tensors are not of type mindspore.Tensor.
        RuntimeError: If an error occurs during the sorting, softmax calculation, or masking of scores.
    """
    if self.filter_value == -float("Inf"):
        self.filter_value = float(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).min)
    # scores = ops.select(ops.isneginf(scores), mindspore.tensor(np.finfo(mindspore.dtype_to_nptype(scores.dtype)).min), scores)
    sorted_logits, sorted_indices = ops.sort(scores, descending=False)
    cumulative_probs = ops.cumsum(ops.softmax(sorted_logits, dim=-1), dim=-1)

    # Remove tokens with cumulative top_p above the threshold (token with 0 are kept)
    sorted_indices_to_remove = cumulative_probs <= (1 - self.top_p)
    # Keep at least min_tokens_to_keep
    sorted_indices_to_remove[..., -self.min_tokens_to_keep:] = 0
    sorted_indices_to_remove = sorted_indices_to_remove.astype(mindspore.int32)

    # scatter sorted tensors to original indexing
    indices_to_remove = ops.scatter(sorted_indices_to_remove, 1, sorted_indices, sorted_indices_to_remove)
    scores = scores.masked_fill(indices_to_remove.astype(mindspore.bool_), self.filter_value)
    return scores

mindnlp.transformers.generation.logits_process.TopPLogitsWarper.__init__(top_p, filter_value=-float('Inf'), min_tokens_to_keep=1)

Initializes an instance of the TopPLogitsWarper class.

PARAMETER DESCRIPTION
top_p

The value representing the top cumulative probability for truncation. Must be a float greater than 0 and less than 1.

TYPE: float

filter_value

The filter value used for truncation. Defaults to negative infinity.

TYPE: float DEFAULT: -float('Inf')

min_tokens_to_keep

The minimum number of tokens to keep after truncation. Must be a positive integer greater than or equal to 1.

TYPE: int DEFAULT: 1

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError
  • If top_p is not a float greater than 0 or less than 1.
  • If min_tokens_to_keep is not a positive integer.
Source code in mindnlp/transformers/generation/logits_process.py
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
def __init__(self, top_p: float, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
    """
    Initializes an instance of the TopPLogitsWarper class.

    Args:
        top_p (float): The value representing the top cumulative probability for truncation.
            Must be a float greater than 0 and less than 1.
        filter_value (float, optional): The filter value used for truncation. Defaults to negative infinity.
        min_tokens_to_keep (int, optional): The minimum number of tokens to keep after truncation.
            Must be a positive integer greater than or equal to 1.

    Returns:
        None.

    Raises:
        ValueError:
            - If top_p is not a float greater than 0 or less than 1.
            - If min_tokens_to_keep is not a positive integer.
    """
    top_p = float(top_p)
    if top_p < 0 or top_p > 1.0:
        raise ValueError(f"`top_p` has to be a float > 0 and < 1, but is {top_p}")
    if not isinstance(min_tokens_to_keep, int) or min_tokens_to_keep < 1:
        raise ValueError(f"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}")

    self.top_p = top_p
    self.filter_value = filter_value
    self.min_tokens_to_keep = min_tokens_to_keep

mindnlp.transformers.generation.logits_process.TypicalLogitsWarper

Bases: LogitsWarper

[LogitsWarper] that performs typical decoding. See Typical Decoding for Natural Language Generation for more information.

PARAMETER DESCRIPTION
mass

Value of typical_p between 0 and 1 inclusive, defaults to 0.9.

TYPE: `float`, *optional*, defaults to 0.9 DEFAULT: 0.9

filter_value

All filtered values will be set to this float value.

TYPE: `float`, *optional*, defaults to -inf DEFAULT: -float('Inf')

min_tokens_to_keep

Minimum number of tokens that cannot be filtered.

TYPE: `int`, *optional*, defaults to 1 DEFAULT: 1

Source code in mindnlp/transformers/generation/logits_process.py
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
class TypicalLogitsWarper(LogitsWarper):
    r"""
    [`LogitsWarper`] that performs typical decoding. See [Typical Decoding for Natural Language
    Generation](https://arxiv.org/abs/2202.00666) for more information.

    Args:
        mass (`float`, *optional*, defaults to 0.9):
            Value of typical_p between 0 and 1 inclusive, defaults to 0.9.
        filter_value (`float`, *optional*, defaults to -inf):
            All filtered values will be set to this float value.
        min_tokens_to_keep (`int`, *optional*, defaults to 1):
            Minimum number of tokens that cannot be filtered.
    """
    def __init__(self, mass: float = 0.9, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
        """
        Initializes an instance of TypicalLogitsWarper.

        Args:
            mass (float, optional): The mass parameter representing the typicality weight.
                It should be a float between 0 and 1 exclusive. Defaults to 0.9.
            filter_value (float, optional): The filter value for logits. Defaults to negative infinity.
            min_tokens_to_keep (int, optional): The minimum number of tokens to keep.
                Should be a positive integer greater than or equal to 1. Defaults to 1.

        Returns:
            None.

        Raises:
            ValueError:
                - If mass is not a float between 0 and 1 exclusive.
                - If min_tokens_to_keep is not a positive integer.
        """
        mass = float(mass)
        if mass <= 0 or mass >= 1:
            raise ValueError(f"`typical_p` has to be a float > 0 and < 1, but is {mass}")
        if not isinstance(min_tokens_to_keep, int) or (min_tokens_to_keep < 1):
            raise ValueError(f"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}")

        self.filter_value = filter_value
        self.mass = mass
        self.min_tokens_to_keep = min_tokens_to_keep

    def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
        """
        This method applies a warping function to the input scores to filter out low-confidence tokens based on
        their probability distribution.

        Args:
            self (TypicalLogitsWarper): The instance of the TypicalLogitsWarper class.
            input_ids (mindspore.Tensor): The input tensor containing the token IDs.
            scores (mindspore.Tensor): The input tensor containing the logits.

        Returns:
            mindspore.Tensor: A tensor containing the warped scores after filtering out low-confidence tokens.

        Raises:
            ValueError: If the input_ids and scores have incompatible shapes or types.
            RuntimeError: If an error occurs during the warping process.
        """
        # calculate entropy
        normalized = F.log_softmax(scores, dim=-1)
        p = ops.exp(normalized)
        ent = -(normalized * p).nansum(-1, keepdim=True)

        # shift and sort
        shifted_scores = ops.abs((-normalized) - ent)
        sorted_scores, sorted_indices = ops.sort(shifted_scores, descending=False)
        sorted_logits = scores.gather(-1, sorted_indices)
        cumulative_probs = ops.cumsum(ops.softmax(sorted_logits, dim=-1), dim=-1)

        # Remove tokens with cumulative mass above the threshold
        last_ind = (cumulative_probs < self.mass).axis(dim=1)
        last_ind.clamp_(max=sorted_scores.shape[-1] - 1)
        sorted_indices_to_remove = sorted_scores > sorted_scores.gather(1, last_ind.view(-1, 1))
        sorted_indices_to_remove[..., : self.min_tokens_to_keep] = 0
        indices_to_remove = ops.scatter(sorted_indices_to_remove, 1, sorted_indices, sorted_indices_to_remove)

        scores = scores.masked_fill(indices_to_remove, self.filter_value)
        return scores

mindnlp.transformers.generation.logits_process.TypicalLogitsWarper.__call__(input_ids, scores)

This method applies a warping function to the input scores to filter out low-confidence tokens based on their probability distribution.

PARAMETER DESCRIPTION
self

The instance of the TypicalLogitsWarper class.

TYPE: TypicalLogitsWarper

input_ids

The input tensor containing the token IDs.

TYPE: Tensor

scores

The input tensor containing the logits.

TYPE: Tensor

RETURNS DESCRIPTION
Tensor

mindspore.Tensor: A tensor containing the warped scores after filtering out low-confidence tokens.

RAISES DESCRIPTION
ValueError

If the input_ids and scores have incompatible shapes or types.

RuntimeError

If an error occurs during the warping process.

Source code in mindnlp/transformers/generation/logits_process.py
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
def __call__(self, input_ids: mindspore.Tensor, scores: mindspore.Tensor) -> mindspore.Tensor:
    """
    This method applies a warping function to the input scores to filter out low-confidence tokens based on
    their probability distribution.

    Args:
        self (TypicalLogitsWarper): The instance of the TypicalLogitsWarper class.
        input_ids (mindspore.Tensor): The input tensor containing the token IDs.
        scores (mindspore.Tensor): The input tensor containing the logits.

    Returns:
        mindspore.Tensor: A tensor containing the warped scores after filtering out low-confidence tokens.

    Raises:
        ValueError: If the input_ids and scores have incompatible shapes or types.
        RuntimeError: If an error occurs during the warping process.
    """
    # calculate entropy
    normalized = F.log_softmax(scores, dim=-1)
    p = ops.exp(normalized)
    ent = -(normalized * p).nansum(-1, keepdim=True)

    # shift and sort
    shifted_scores = ops.abs((-normalized) - ent)
    sorted_scores, sorted_indices = ops.sort(shifted_scores, descending=False)
    sorted_logits = scores.gather(-1, sorted_indices)
    cumulative_probs = ops.cumsum(ops.softmax(sorted_logits, dim=-1), dim=-1)

    # Remove tokens with cumulative mass above the threshold
    last_ind = (cumulative_probs < self.mass).axis(dim=1)
    last_ind.clamp_(max=sorted_scores.shape[-1] - 1)
    sorted_indices_to_remove = sorted_scores > sorted_scores.gather(1, last_ind.view(-1, 1))
    sorted_indices_to_remove[..., : self.min_tokens_to_keep] = 0
    indices_to_remove = ops.scatter(sorted_indices_to_remove, 1, sorted_indices, sorted_indices_to_remove)

    scores = scores.masked_fill(indices_to_remove, self.filter_value)
    return scores

mindnlp.transformers.generation.logits_process.TypicalLogitsWarper.__init__(mass=0.9, filter_value=-float('Inf'), min_tokens_to_keep=1)

Initializes an instance of TypicalLogitsWarper.

PARAMETER DESCRIPTION
mass

The mass parameter representing the typicality weight. It should be a float between 0 and 1 exclusive. Defaults to 0.9.

TYPE: float DEFAULT: 0.9

filter_value

The filter value for logits. Defaults to negative infinity.

TYPE: float DEFAULT: -float('Inf')

min_tokens_to_keep

The minimum number of tokens to keep. Should be a positive integer greater than or equal to 1. Defaults to 1.

TYPE: int DEFAULT: 1

RETURNS DESCRIPTION

None.

RAISES DESCRIPTION
ValueError
  • If mass is not a float between 0 and 1 exclusive.
  • If min_tokens_to_keep is not a positive integer.
Source code in mindnlp/transformers/generation/logits_process.py
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
def __init__(self, mass: float = 0.9, filter_value: float = -float("Inf"), min_tokens_to_keep: int = 1):
    """
    Initializes an instance of TypicalLogitsWarper.

    Args:
        mass (float, optional): The mass parameter representing the typicality weight.
            It should be a float between 0 and 1 exclusive. Defaults to 0.9.
        filter_value (float, optional): The filter value for logits. Defaults to negative infinity.
        min_tokens_to_keep (int, optional): The minimum number of tokens to keep.
            Should be a positive integer greater than or equal to 1. Defaults to 1.

    Returns:
        None.

    Raises:
        ValueError:
            - If mass is not a float between 0 and 1 exclusive.
            - If min_tokens_to_keep is not a positive integer.
    """
    mass = float(mass)
    if mass <= 0 or mass >= 1:
        raise ValueError(f"`typical_p` has to be a float > 0 and < 1, but is {mass}")
    if not isinstance(min_tokens_to_keep, int) or (min_tokens_to_keep < 1):
        raise ValueError(f"`min_tokens_to_keep` has to be a positive integer, but is {min_tokens_to_keep}")

    self.filter_value = filter_value
    self.mass = mass
    self.min_tokens_to_keep = min_tokens_to_keep

mindnlp.transformers.generation.logits_process.UnbatchedClassifierFreeGuidanceLogitsProcessor

Bases: LogitsProcessor

Logits processor for Classifier-Free Guidance (CFG). The processors computes a weighted average across scores from prompt conditional and prompt unconditional (or negative) logits, parameterized by the guidance_scale. The unconditional scores are computed internally by prompting model with the unconditional_ids branch.

See the paper for more information.

PARAMETER DESCRIPTION
guidance_scale

The guidance scale for classifier free guidance (CFG). CFG is enabled by setting guidance_scale != 1. Higher guidance scale encourages the model to generate samples that are more closely linked to the input prompt, usually at the expense of poorer quality. A value smaller than 1 has the opposite effect, while making the negative prompt provided with negative_prompt_ids (if any) act as a positive prompt.

TYPE: `float`

model

The model computing the unconditional scores. Supposedly the same as the one computing the conditional scores. Both models must use the same tokenizer.

TYPE: `PreTrainedModel`

unconditional_ids

Indices of input sequence tokens in the vocabulary for the unconditional branch. If unset, will default to the last token of the prompt.

TYPE: `mindspore.Tensor` of shape `(batch_size, sequence_length)`, *optional* DEFAULT: None

unconditional_attention_mask

Attention mask for unconditional_ids.

TYPE: `mindspore.Tensor` of shape `(batch_size, sequence_length)`, *optional* DEFAULT: None

use_cache

Whether to cache key/values during the negative prompt forward pass.

TYPE: `bool`, *optional*, defaults to `True` DEFAULT: True

Example
>>> from transformers import AutoTokenizer, AutoModelForCausalLM
...
>>> model = AutoModelForCausalLM.from_pretrained("gpt2")
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
>>> inputs = tokenizer(["Today, a dragon flew over Paris, France,"], return_tensors="pt")
>>> out = model.generate(inputs["input_ids"], guidance_scale=1.5)
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
'Today, a dragon flew over Paris, France, killing at least 50 people and injuring more than 100'
>>> # with a negative prompt
>>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
>>> out = model.generate(inputs["input_ids"], guidance_scale=2, negative_prompt_ids=neg_inputs["input_ids"])
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
'Today, a dragon flew over Paris, France, killing at least 130 people. French media reported that'
>>> # with a positive prompt
>>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
>>> out = model.generate(inputs["input_ids"], guidance_scale=0, negative_prompt_ids=neg_inputs["input_ids"])
>>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
"Today, a dragon flew over Paris, France, and I'm very happy to be here. I"
Source code in mindnlp/transformers/generation/logits_process.py
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
class UnbatchedClassifierFreeGuidanceLogitsProcessor(LogitsProcessor):
    r"""Logits processor for Classifier-Free Guidance (CFG). The processors
    computes a weighted average across scores from prompt conditional and prompt unconditional (or negative) logits,
    parameterized by the `guidance_scale`. The unconditional scores are computed internally by prompting `model` with
    the `unconditional_ids` branch.

    See [the paper](https://arxiv.org/abs/2306.17806) for more information.

    Args:
        guidance_scale (`float`):
            The guidance scale for classifier free guidance (CFG). CFG is enabled by setting `guidance_scale != 1`.
            Higher guidance scale encourages the model to generate samples that are more closely linked to the input
            prompt, usually at the expense of poorer quality. A value smaller than 1 has the opposite effect, while
            making the negative prompt provided with negative_prompt_ids (if any) act as a positive prompt.
        model (`PreTrainedModel`):
            The model computing the unconditional scores. Supposedly the same as the one computing the conditional
            scores. Both models must use the same tokenizer.
        unconditional_ids (`mindspore.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of input sequence tokens in the vocabulary for the unconditional branch. If unset, will default to
            the last token of the prompt.
        unconditional_attention_mask (`mindspore.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Attention mask for unconditional_ids.
        use_cache (`bool`, *optional*, defaults to `True`):
            Whether to cache key/values during the negative prompt forward pass.


    Example:
        ```python
        >>> from transformers import AutoTokenizer, AutoModelForCausalLM
        ...
        >>> model = AutoModelForCausalLM.from_pretrained("gpt2")
        >>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
        >>> inputs = tokenizer(["Today, a dragon flew over Paris, France,"], return_tensors="pt")
        >>> out = model.generate(inputs["input_ids"], guidance_scale=1.5)
        >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
        'Today, a dragon flew over Paris, France, killing at least 50 people and injuring more than 100'
        >>> # with a negative prompt
        >>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
        >>> out = model.generate(inputs["input_ids"], guidance_scale=2, negative_prompt_ids=neg_inputs["input_ids"])
        >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
        'Today, a dragon flew over Paris, France, killing at least 130 people. French media reported that'
        >>> # with a positive prompt
        >>> neg_inputs = tokenizer(["A very happy event happened,"], return_tensors="pt")
        >>> out = model.generate(inputs["input_ids"], guidance_scale=0, negative_prompt_ids=neg_inputs["input_ids"])
        >>> tokenizer.batch_decode(out, skip_special_tokens=True)[0]
        "Today, a dragon flew over Paris, France, and I'm very happy to be here. I"
        ```
    """
    def __init__(
        self,
        guidance_scale: float,
        model,
        unconditional_ids: Optional[mindspore.Tensor] = None,
        unconditional_attention_mask: Optional[mindspore.Tensor] = None,
        use_cache: Optional[bool] = True,
    ):
        """
        Initializes the UnbatchedClassifierFreeGuidanceLogitsProcessor.

        Args:
            guidance_scale (float): The scale factor for guidance logits.
            model: The model used for processing.
            unconditional_ids (Optional[mindspore.Tensor], optional): The input tensor for unconditional context.
                Default is None.
            unconditional_attention_mask (Optional[mindspore.Tensor], optional): The attention mask for unconditional
                context. Default is None.
            use_cache (Optional[bool], optional): Flag to indicate whether to use caching. Default is True.

        Returns:
            None.

        Raises:
            None.
        """
        self.guidance_scale = guidance_scale
        self.model = model
        self.unconditional_context = {
            "input_ids": unconditional_ids,
            "attention_mask": unconditional_attention_mask,
            "use_cache": use_cache,
            "past_key_values": None,
            "first_pass": True,
        }

    def get_unconditional_logits(self, input_ids):
        """get_unconditional_logits"""
        if self.unconditional_context["first_pass"]:
            if self.unconditional_context["input_ids"] is None:
                self.unconditional_context["input_ids"] = input_ids[:, -1:]
            if self.unconditional_context["attention_mask"] is None:
                self.unconditional_context["attention_mask"] = ops.ones_like(
                    self.unconditional_context["input_ids"], dtype=mindspore.int64
                )
            input_ids = self.unconditional_context["input_ids"]
            attention_mask = self.unconditional_context["attention_mask"]
            self.unconditional_context["first_pass"] = False
        else:
            attention_mask = ops.cat(
                [
                    self.unconditional_context["attention_mask"],
                    ops.ones_like(input_ids[:, -1:], dtype=mindspore.int64),
                ],
                dim=1,
            )
            if not self.unconditional_context["use_cache"]:
                input_ids = ops.cat([self.unconditional_context["input_ids"], input_ids[:, -1:]], dim=1)
            else:
                input_ids = input_ids[:, -1:]
            self.unconditional_context["input_ids"] = input_ids
            self.unconditional_context["attention_mask"] = attention_mask

        out = self.model(
            input_ids,
            attention_mask=attention_mask,
            use_cache=self.unconditional_context["use_cache"],
            past_key_values=self.unconditional_context["past_key_values"],
        )
        self.unconditional_context["past_key_values"] = out.get("past_key_values", None)

        return out.logits

    def __call__(self, input_ids, scores):
        """
        This method processes input_ids and scores to compute guidance logits in the
        UnbatchedClassifierFreeGuidanceLogi