Skip to content

unitorch.cli.models.bart¤

BartProcessor¤

Tip

core/process/bart is the section for configuration of BartProcessor.

Bases: BartProcessor

Class for processing data with BART model.

Initialize BartProcessor.

Parameters:

Name Type Description Default
vocab_path str

The path to the vocabulary file.

required
merge_path str

The path to the merge file.

required
special_input_ids Dict

Special input IDs. Defaults to an empty dictionary.

dict()
max_seq_length int

The maximum sequence length. Defaults to 128.

128
max_gen_seq_length int

The maximum generation sequence length. Defaults to 48.

48
Source code in src/unitorch/cli/models/bart/processing.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def __init__(
    self,
    vocab_path: str,
    merge_path: str,
    special_input_ids: Optional[Dict] = dict(),
    max_seq_length: Optional[int] = 128,
    max_gen_seq_length: Optional[int] = 48,
):
    """
    Initialize BartProcessor.

    Args:
        vocab_path (str): The path to the vocabulary file.
        merge_path (str): The path to the merge file.
        special_input_ids (Dict, optional): Special input IDs. Defaults to an empty dictionary.
        max_seq_length (int, optional): The maximum sequence length. Defaults to 128.
        max_gen_seq_length (int, optional): The maximum generation sequence length. Defaults to 48.
    """
    super().__init__(
        vocab_path=vocab_path,
        merge_path=merge_path,
        special_input_ids=special_input_ids,
        max_seq_length=max_seq_length,
        max_gen_seq_length=max_gen_seq_length,
    )

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of BartProcessor from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
BartProcessor

An instance of BartProcessor.

Source code in src/unitorch/cli/models/bart/processing.py
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
81
82
83
@classmethod
@add_default_section_for_init("core/process/bart")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of BartProcessor from a core configuration.

    Args:
        config: The core configuration.
        **kwargs: Additional keyword arguments.

    Returns:
        BartProcessor: An instance of BartProcessor.
    """
    config.set_default_section("core/process/bart")
    pretrained_name = config.getoption("pretrained_name", "bart-base")
    vocab_path = config.getoption("vocab_path", None)
    vocab_path = pop_value(
        vocab_path,
        nested_dict_value(pretrained_bart_infos, pretrained_name, "vocab"),
    )
    vocab_path = cached_path(vocab_path)

    merge_path = config.getoption("merge_path", None)
    merge_path = pop_value(
        merge_path,
        nested_dict_value(pretrained_bart_infos, pretrained_name, "merge"),
    )
    merge_path = cached_path(merge_path)

    return {
        "vocab_path": vocab_path,
        "merge_path": merge_path,
    }

BartForGeneration¤

Tip

core/model/generation/bart is the section for configuration of BartForGeneration.

Bases: BartForGeneration

BART model for generation tasks.

Initialize the BartForGeneration model.

Parameters:

Name Type Description Default
config_path str

Path to the model configuration file.

required
gradient_checkpointing bool

Whether to use gradient checkpointing for memory optimization.

False
Source code in src/unitorch/cli/models/bart/modeling.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def __init__(
    self,
    config_path: str,
    gradient_checkpointing: Optional[bool] = False,
):
    """
    Initialize the BartForGeneration model.

    Args:
        config_path (str): Path to the model configuration file.
        gradient_checkpointing (bool, optional): Whether to use gradient checkpointing for memory optimization.
    """
    super().__init__(
        config_path=config_path, gradient_checkpointing=gradient_checkpointing
    )

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Tensor,
    decoder_input_ids: Tensor,
    decoder_attention_mask: Tensor,
)

Forward pass of the BartForGeneration model.

Parameters:

Name Type Description Default
input_ids Tensor

Input IDs.

required
attention_mask Tensor

Attention mask.

required
decoder_input_ids Tensor

Decoder input IDs.

required
decoder_attention_mask Tensor

Decoder attention mask.

required

Returns:

Name Type Description
GenerationOutputs

The generated sequences and their scores.

Source code in src/unitorch/cli/models/bart/modeling.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: torch.Tensor,
    decoder_input_ids: torch.Tensor,
    decoder_attention_mask: torch.Tensor,
):
    """
    Forward pass of the BartForGeneration model.

    Args:
        input_ids (torch.Tensor): Input IDs.
        attention_mask (torch.Tensor): Attention mask.
        decoder_input_ids (torch.Tensor): Decoder input IDs.
        decoder_attention_mask (torch.Tensor): Decoder attention mask.

    Returns:
        GenerationOutputs: The generated sequences and their scores.
    """
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        decoder_input_ids=decoder_input_ids,
        decoder_attention_mask=decoder_attention_mask,
    )
    return GenerationOutputs(sequences=outputs)

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of BartForGeneration from core configuration.

Parameters:

Name Type Description Default
config

The core configuration object.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
BartForGeneration

The initialized BartForGeneration instance.

Source code in src/unitorch/cli/models/bart/modeling.py
40
41
42
43
44
45
46
47
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
@classmethod
@add_default_section_for_init("core/model/generation/bart")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of BartForGeneration from core configuration.

    Args:
        config: The core configuration object.
        **kwargs: Additional keyword arguments.

    Returns:
        BartForGeneration: The initialized BartForGeneration instance.
    """
    config.set_default_section("core/model/generation/bart")
    pretrained_name = config.getoption("pretrained_name", "default-bart")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_bart_infos, pretrained_name, "config"),
    )

    config_path = cached_path(config_path)
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)

    inst = cls(config_path, gradient_checkpointing)
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_bart_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

generate ¤

generate(
    input_ids: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 2,
    decoder_end_token_id: Optional[
        Union[int, List[int]]
    ] = 2,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 48,
    repetition_penalty: Optional[float] = 1.0,
    no_repeat_ngram_size: Optional[int] = 0,
    early_stopping: Optional[bool] = True,
    length_penalty: Optional[float] = 1.0,
    num_beam_groups: Optional[int] = 1,
    diversity_penalty: Optional[float] = 0.0,
    do_sample: Optional[bool] = False,
    temperature: Optional[float] = 1.0,
    top_k: Optional[int] = 50,
    top_p: Optional[float] = 1.0,
)

Generate sequences using the BartForGeneration model.

Parameters:

Name Type Description Default
input_ids Tensor

Input IDs.

required
num_beams int

Number of beams for beam search.

5
decoder_start_token_id int

ID of the decoder start token.

2
decoder_end_token_id int or List[int]

ID of the decoder end token.

2
num_return_sequences int

Number of generated sequences to return.

1
min_gen_seq_length int

Minimum length of generated sequences.

0
max_gen_seq_length int

Maximum length of generated sequences.

48
repetition_penalty float

Repetition penalty.

1.0
no_repeat_ngram_size int

Size of n-grams to avoid repeating.

0
early_stopping bool

Whether to stop generation early.

True
length_penalty float

Length penalty for generated sequences.

1.0
num_beam_groups int

Number of groups for diverse beam search.

1
diversity_penalty float

Diversity penalty for diverse beam search.

0.0
do_sample bool

Whether to use sampling for generation.

False
temperature float

Sampling temperature.

1.0
top_k int

Top-k sampling parameter.

50
top_p float

Top-p sampling parameter.

1.0

Returns:

Name Type Description
GenerationOutputs

The generated sequences and their scores.

Source code in src/unitorch/cli/models/bart/modeling.py
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
@add_default_section_for_function("core/model/generation/bart")
@torch.no_grad()
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def generate(
    self,
    input_ids: torch.Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 2,
    decoder_end_token_id: Optional[Union[int, List[int]]] = 2,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 48,
    repetition_penalty: Optional[float] = 1.0,
    no_repeat_ngram_size: Optional[int] = 0,
    early_stopping: Optional[bool] = True,
    length_penalty: Optional[float] = 1.0,
    num_beam_groups: Optional[int] = 1,
    diversity_penalty: Optional[float] = 0.0,
    do_sample: Optional[bool] = False,
    temperature: Optional[float] = 1.0,
    top_k: Optional[int] = 50,
    top_p: Optional[float] = 1.0,
):
    """
    Generate sequences using the BartForGeneration model.

    Args:
        input_ids (torch.Tensor): Input IDs.
        num_beams (int, optional): Number of beams for beam search.
        decoder_start_token_id (int, optional): ID of the decoder start token.
        decoder_end_token_id (int or List[int], optional): ID of the decoder end token.
        num_return_sequences (int, optional): Number of generated sequences to return.
        min_gen_seq_length (int, optional): Minimum length of generated sequences.
        max_gen_seq_length (int, optional): Maximum length of generated sequences.
        repetition_penalty (float, optional): Repetition penalty.
        no_repeat_ngram_size (int, optional): Size of n-grams to avoid repeating.
        early_stopping (bool, optional): Whether to stop generation early.
        length_penalty (float, optional): Length penalty for generated sequences.
        num_beam_groups (int, optional): Number of groups for diverse beam search.
        diversity_penalty (float, optional): Diversity penalty for diverse beam search.
        do_sample (bool, optional): Whether to use sampling for generation.
        temperature (float, optional): Sampling temperature.
        top_k (int, optional): Top-k sampling parameter.
        top_p (float, optional): Top-p sampling parameter.

    Returns:
        GenerationOutputs: The generated sequences and their scores.
    """
    outputs = super().generate(
        input_ids=input_ids,
        num_beams=num_beams,
        decoder_start_token_id=decoder_start_token_id,
        decoder_end_token_id=decoder_end_token_id,
        num_return_sequences=num_return_sequences,
        min_gen_seq_length=min_gen_seq_length,
        max_gen_seq_length=max_gen_seq_length,
        repetition_penalty=repetition_penalty,
        no_repeat_ngram_size=no_repeat_ngram_size,
        early_stopping=early_stopping,
        length_penalty=length_penalty,
        num_beam_groups=num_beam_groups,
        diversity_penalty=diversity_penalty,
        do_sample=do_sample,
        temperature=temperature,
        top_k=top_k,
        top_p=top_p,
    )

    return GenerationOutputs(
        sequences=outputs.sequences,
        sequences_scores=outputs.sequences_scores,
    )