Skip to content

unitorch.cli.models.bert¤

BertProcessor¤

Tip

core/process/bert is the section for configuration of BertProcessor.

Bases: BertProcessor

Processor for BERT models.

Source code in src/unitorch/cli/models/bert/processing.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def __init__(
    self,
    vocab_path: str,
    max_seq_length: Optional[int] = 128,
    special_input_ids: Optional[Dict] = dict(),
    do_lower_case: Optional[bool] = True,
    do_basic_tokenize: Optional[bool] = True,
    do_whole_word_mask: Optional[bool] = True,
    masked_lm_prob: Optional[float] = 0.15,
    max_predictions_per_seq: Optional[int] = 20,
):
    super().__init__(
        vocab_path=vocab_path,
        max_seq_length=max_seq_length,
        special_input_ids=special_input_ids,
        do_lower_case=do_lower_case,
        do_basic_tokenize=do_basic_tokenize,
        do_whole_word_mask=do_whole_word_mask,
        masked_lm_prob=masked_lm_prob,
        max_predictions_per_seq=max_predictions_per_seq,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/bert/processing.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@classmethod
@config_defaults_init("core/process/bert")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/process/bert")
    pretrained_name = config.getoption("pretrained_name", "bert-base-uncased")
    vocab_path = config.getoption("vocab_path", None)
    vocab_path = pop_value(
        vocab_path,
        nested_dict_value(pretrained_bert_infos, pretrained_name, "vocab"),
    )
    vocab_path = cached_path(vocab_path)

    return {
        "vocab_path": vocab_path,
    }

_classification ¤

_classification(
    text: str,
    text_pair: Optional[str] = None,
    max_seq_length: Optional[int] = None,
)
Source code in src/unitorch/cli/models/bert/processing.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
@register_process("core/process/bert/classification")
def _classification(
    self,
    text: str,
    text_pair: Optional[str] = None,
    max_seq_length: Optional[int] = None,
):
    outputs = super().classification(
        text=text,
        text_pair=text_pair,
        max_seq_length=max_seq_length,
    )
    return TensorInputs(
        input_ids=outputs.input_ids,
        attention_mask=outputs.attention_mask,
        token_type_ids=outputs.token_type_ids,
        position_ids=outputs.position_ids,
    )

_pretrain ¤

_pretrain(
    text: str,
    text_pair: str,
    nsp_label: int,
    max_seq_length: Optional[int] = None,
    masked_lm_prob: Optional[float] = None,
    do_whole_word_mask: Optional[bool] = None,
    max_predictions_per_seq: Optional[int] = None,
)
Source code in src/unitorch/cli/models/bert/processing.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
103
104
@register_process("core/process/bert/pretrain")
def _pretrain(
    self,
    text: str,
    text_pair: str,
    nsp_label: int,
    max_seq_length: Optional[int] = None,
    masked_lm_prob: Optional[float] = None,
    do_whole_word_mask: Optional[bool] = None,
    max_predictions_per_seq: Optional[int] = None,
):
    outputs = super().pretrain(
        text=text,
        text_pair=text_pair,
        nsp_label=nsp_label,
        max_seq_length=max_seq_length,
        masked_lm_prob=masked_lm_prob,
        do_whole_word_mask=do_whole_word_mask,
        max_predictions_per_seq=max_predictions_per_seq,
    )
    return TensorInputs(
        input_ids=outputs.input_ids,
        attention_mask=outputs.attention_mask,
        token_type_ids=outputs.token_type_ids,
        position_ids=outputs.position_ids,
        nsp_label=outputs.nsp_label,
        mlm_label=outputs.mlm_label,
        mlm_label_mask=outputs.mlm_label_mask,
    )

BertForClassification¤

Tip

core/model/classification/bert is the section for configuration of BertForClassification.

Bases: BertForClassification

BERT model for classification tasks.

Source code in src/unitorch/cli/models/bert/modeling.py
22
23
24
25
26
27
28
29
30
31
32
def __init__(
    self,
    config_path: str,
    num_classes: Optional[int] = 1,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        num_classes=num_classes,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/bert/modeling.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@classmethod
@config_defaults_init("core/model/classification/bert")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/bert")
    pretrained_name = config.getoption("pretrained_name", "bert-base-uncased")
    config_path = config.getoption("config_path", None)
    num_classes = config.getoption("num_classes", 1)

    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_bert_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_bert_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if weight_path is not None:
        inst.from_pretrained(weight_path)

    return inst

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    token_type_ids: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/bert/modeling.py
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@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,
):
    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)