Skip to content

unitorch.cli.models.xlm_roberta¤

XLMRobertaProcessor¤

Tip

core/process/xlm_roberta is the section for configuration of XLMRobertaProcessor.

Bases: XLMRobertaProcessor

XLM-RoBERTa Processor for handling text processing tasks.

Initialize XLMRobertaProcessor.

Parameters:

Name Type Description Default
vocab_path str

The path to the vocabulary file.

required
max_seq_length int

The maximum sequence length. Defaults to 128.

128
source_type_id int

The source type ID. Defaults to 0.

0
target_type_id int

The target type ID. Defaults to 0.

0
Source code in src/unitorch/cli/models/xlm_roberta/processing.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def __init__(
    self,
    vocab_path: str,
    max_seq_length: Optional[int] = 128,
    source_type_id: Optional[int] = 0,
    target_type_id: Optional[int] = 0,
):
    """
    Initialize XLMRobertaProcessor.

    Args:
        vocab_path (str): The path to the vocabulary file.
        max_seq_length (int, optional): The maximum sequence length. Defaults to 128.
        source_type_id (int, optional): The source type ID. Defaults to 0.
        target_type_id (int, optional): The target type ID. Defaults to 0.
    """
    super().__init__(
        vocab_path=vocab_path,
        max_seq_length=max_seq_length,
        source_type_id=source_type_id,
        target_type_id=target_type_id,
    )

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of XLMRobertaProcessor from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
dict

The processed arguments for initializing the processor.

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

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

    Returns:
        dict: The processed arguments for initializing the processor.
    """
    config.set_default_section("core/process/xlm_roberta")
    pretrained_name = config.getoption("pretrained_name", "xlm-roberta-base")
    vocab_path = config.getoption("vocab_path", None)
    vocab_path = pop_value(
        vocab_path,
        nested_dict_value(pretrained_xlm_roberta_infos, pretrained_name, "vocab"),
    )
    vocab_path = cached_path(vocab_path)

    return {
        "vocab_path": vocab_path,
    }

XLMRobertaForClassification¤

Tip

core/model/classification/xlm_roberta is the section for configuration of XLMRobertaForClassification.

Bases: XLMRobertaForClassification

XLM-RoBERTa model for classification tasks.

Initialize XLMRobertaForClassification.

Parameters:

Name Type Description Default
config_path str

The path to the model configuration file.

required
num_classes int

The number of classes for classification. Defaults to 1.

1
gradient_checkpointing bool

Whether to use gradient checkpointing. Defaults to False.

False
Source code in src/unitorch/cli/models/xlm_roberta/modeling.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def __init__(
    self,
    config_path: str,
    num_classes: Optional[int] = 1,
    gradient_checkpointing: Optional[bool] = False,
):
    """
    Initialize XLMRobertaForClassification.

    Args:
        config_path (str): The path to the model configuration file.
        num_classes (int, optional): The number of classes for classification. Defaults to 1.
        gradient_checkpointing (bool, optional): Whether to use gradient checkpointing. Defaults to False.
    """
    super().__init__(
        config_path=config_path,
        num_classes=num_classes,
        gradient_checkpointing=gradient_checkpointing,
    )

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    token_type_ids: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)

Perform a forward pass through the model.

Parameters:

Name Type Description Default
input_ids Tensor

Input token IDs.

required
attention_mask Tensor

Attention mask. Defaults to None.

None
token_type_ids Tensor

Token type IDs. Defaults to None.

None
position_ids Tensor

Position IDs. Defaults to None.

None

Returns:

Name Type Description
ClassificationOutputs

The classification outputs.

Source code in src/unitorch/cli/models/xlm_roberta/modeling.py
 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
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    token_type_ids: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    """
    Perform a forward pass through the model.

    Args:
        input_ids (torch.Tensor): Input token IDs.
        attention_mask (torch.Tensor, optional): Attention mask. Defaults to None.
        token_type_ids (torch.Tensor, optional): Token type IDs. Defaults to None.
        position_ids (torch.Tensor, optional): Position IDs. Defaults to None.

    Returns:
        ClassificationOutputs: The classification outputs.
    """
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        token_type_ids=token_type_ids,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of XLMRobertaForClassification from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
XLMRobertaForClassification

The instantiated XLMRobertaForClassification model.

Source code in src/unitorch/cli/models/xlm_roberta/modeling.py
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
75
76
77
78
79
80
81
82
@classmethod
@add_default_section_for_init("core/model/classification/xlm_roberta")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of XLMRobertaForClassification from a core configuration.

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

    Returns:
        XLMRobertaForClassification: The instantiated XLMRobertaForClassification model.
    """
    config.set_default_section("core/model/classification/xlm_roberta")
    pretrained_name = config.getoption("pretrained_name", "xlm-roberta-base")
    config_path = config.getoption("config_path", None)
    num_classes = config.getoption("num_classes", 1)

    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_xlm_roberta_infos, pretrained_name, "config"),
    )

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

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

    return inst

XLMRobertaXLForClassification¤

Tip

core/model/classification/xlm_roberta_xl is the section for configuration of XLMRobertaXLForClassification.

Bases: XLMRobertaXLForClassification

XLM-RoBERTa XL model for classification tasks.

Initialize XLMRobertaXLForClassification.

Parameters:

Name Type Description Default
config_path str

The path to the model configuration file.

required
num_classes int

The number of classes for classification. Defaults to 1.

1
gradient_checkpointing bool

Whether to use gradient checkpointing. Defaults to False.

False
Source code in src/unitorch/cli/models/xlm_roberta/modeling.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
def __init__(
    self,
    config_path: str,
    num_classes: Optional[int] = 1,
    gradient_checkpointing: Optional[bool] = False,
):
    """
    Initialize XLMRobertaXLForClassification.

    Args:
        config_path (str): The path to the model configuration file.
        num_classes (int, optional): The number of classes for classification. Defaults to 1.
        gradient_checkpointing (bool, optional): Whether to use gradient checkpointing. Defaults to False.
    """
    super().__init__(
        config_path=config_path,
        num_classes=num_classes,
        gradient_checkpointing=gradient_checkpointing,
    )

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    token_type_ids: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)

Perform a forward pass through the model.

Parameters:

Name Type Description Default
input_ids Tensor

Input token IDs.

required
attention_mask Tensor

Attention mask. Defaults to None.

None
token_type_ids Tensor

Token type IDs. Defaults to None.

None
position_ids Tensor

Position IDs. Defaults to None.

None

Returns:

Name Type Description
ClassificationOutputs

The classification outputs.

Source code in src/unitorch/cli/models/xlm_roberta/modeling.py
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    token_type_ids: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    """
    Perform a forward pass through the model.

    Args:
        input_ids (torch.Tensor): Input token IDs.
        attention_mask (torch.Tensor, optional): Attention mask. Defaults to None.
        token_type_ids (torch.Tensor, optional): Token type IDs. Defaults to None.
        position_ids (torch.Tensor, optional): Position IDs. Defaults to None.

    Returns:
        ClassificationOutputs: The classification outputs.
    """
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        token_type_ids=token_type_ids,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

from_core_configure classmethod ¤

from_core_configure(config, **kwargs)

Create an instance of XLMRobertaXLForClassification from a core configuration.

Parameters:

Name Type Description Default
config

The core configuration.

required
**kwargs

Additional keyword arguments.

{}

Returns:

Name Type Description
XLMRobertaXLForClassification

The instantiated XLMRobertaXLForClassification model.

Source code in src/unitorch/cli/models/xlm_roberta/modeling.py
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
@classmethod
@add_default_section_for_init("core/model/classification/xlm_roberta_xl")
def from_core_configure(cls, config, **kwargs):
    """
    Create an instance of XLMRobertaXLForClassification from a core configuration.

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

    Returns:
        XLMRobertaXLForClassification: The instantiated XLMRobertaXLForClassification model.
    """
    config.set_default_section("core/model/classification/xlm_roberta_xl")
    pretrained_name = config.getoption("pretrained_name", "xlm-roberta-xl")
    config_path = config.getoption("config_path", None)

    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_xlm_roberta_infos, pretrained_name, "config"),
    )

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

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

    return inst