Skip to content

unitorch.cli.models.peft¤

ClipLoraForMatching¤

Tip

core/model/matching/peft/lora/clip is the section for configuration of ClipLoraForMatching.

Bases: ClipLoraForMatching

Source code in src/unitorch/cli/models/peft/modeling_clip.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
def __init__(
    self,
    config_path: str,
    projection_dim: Optional[int] = 512,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
):
    super().__init__(
        config_path=config_path,
        projection_dim=projection_dim,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_clip.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
@classmethod
@config_defaults_init("core/model/matching/peft/lora/clip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/matching/peft/lora/clip")
    pretrained_name = config.getoption("pretrained_name", "clip-vit-base-patch16")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_clip_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)
    projection_dim = config.getoption("projection_dim", 512)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

    inst = cls(
        config_path,
        projection_dim=projection_dim,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_clip_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Tensor,
    position_ids: Tensor,
)
Source code in src/unitorch/cli/models/peft/modeling_clip.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: torch.Tensor,
    position_ids: torch.Tensor,
):
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

LlamaLoraForClassification¤

Tip

core/model/classification/peft/lora/llama is the section for configuration of LlamaLoraForClassification.

Bases: LlamaLoraForClassification

LlamaLora model for classification tasks.

Source code in src/unitorch/cli/models/peft/modeling_llama.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    num_classes: Optional[int] = 1,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        num_classes=num_classes,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_llama.py
 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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
@classmethod
@config_defaults_init("core/model/classification/peft/lora/llama")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/peft/lora/llama")
    pretrained_name = config.getoption("pretrained_name", "llama-7b")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_llama_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

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

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        num_classes=num_classes,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_llama_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_llama.py
107
108
109
110
111
112
113
114
115
116
117
118
119
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

LlamaLoraForGeneration¤

Tip

core/model/generation/peft/lora/llama is the section for configuration of LlamaLoraForGeneration.

Bases: LlamaLoraForGeneration

LlamaLora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_llama.py
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_llama.py
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
@classmethod
@config_defaults_init("core/model/generation/peft/lora/llama")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/lora/llama")
    pretrained_name = config.getoption("pretrained_name", "llama-7b")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_llama_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

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

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_llama_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_llama.py
202
203
204
205
206
207
208
209
210
211
212
213
214
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return GenerationOutputs(sequences=outputs)

generate ¤

generate(
    input_ids: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 1,
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_llama.py
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
@config_defaults_method("core/model/generation/peft/lora/llama")
@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] = 1,
    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,
):
    outputs = super().generate(
        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,
    )

LlavaMistralClipLoraForClassification¤

Tip

core/model/classification/peft/lora/llava/mistral_clip is the section for configuration of LlavaMistralClipLoraForClassification.

Bases: LlavaMistralClipLoraForClassification

LlavaMistralClipLora model for classification tasks.

Source code in src/unitorch/cli/models/peft/modeling_llava.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def __init__(
    self,
    config_path: str,
    image_token_index: Optional[int] = 32000,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    num_classes: Optional[int] = 1,
    freeze_multi_modal_projector: Optional[bool] = True,
    freeze_classifer: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        image_token_index=image_token_index,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        num_classes=num_classes,
        freeze_multi_modal_projector=freeze_multi_modal_projector,
        freeze_classifer=freeze_classifer,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_llava.py
 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
 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
@classmethod
@config_defaults_init("core/model/classification/peft/lora/llava/mistral_clip")
def from_config(cls, config, **kwargs):
    config.set_default_section(
        "core/model/classification/peft/lora/llava/mistral_clip"
    )
    pretrained_name = config.getoption(
        "pretrained_name", "llava-v1.6-mistral-7b-hf"
    )
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_llava_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    image_token_index = config.getoption("image_token_index", 32000)
    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])
    freeze_multi_modal_projector = config.getoption(
        "freeze_multi_modal_projector", True
    )
    freeze_classifer = config.getoption("freeze_classifer", True)
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)
    num_classes = config.getoption("num_classes", 1)

    inst = cls(
        config_path,
        image_token_index=image_token_index,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        num_classes=num_classes,
        freeze_multi_modal_projector=freeze_multi_modal_projector,
        freeze_classifer=freeze_classifer,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_llava_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_llava.py
125
126
127
128
129
130
131
132
133
134
135
136
137
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
    )
    return ClassificationOutputs(outputs=outputs)

LlavaMistralClipLoraForGeneration¤

Tip

core/model/generation/peft/lora/llava/mistral_clip is the section for configuration of LlavaMistralClipLoraForGeneration.

Bases: LlavaMistralClipLoraForGeneration

LlavaMistralClipLora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_llava.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def __init__(
    self,
    config_path: str,
    image_token_index: Optional[int] = 32000,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    freeze_multi_modal_projector: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        image_token_index=image_token_index,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        freeze_multi_modal_projector=freeze_multi_modal_projector,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_llava.py
171
172
173
174
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
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
@classmethod
@config_defaults_init("core/model/generation/peft/lora/llava/mistral_clip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/lora/llava/mistral_clip")
    pretrained_name = config.getoption(
        "pretrained_name", "llava-v1.6-mistral-7b-hf"
    )
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_llava_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    image_token_index = config.getoption("image_token_index", 32000)
    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])
    freeze_multi_modal_projector = config.getoption(
        "freeze_multi_modal_projector", True
    )
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)

    inst = cls(
        config_path,
        image_token_index=image_token_index,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        freeze_multi_modal_projector=freeze_multi_modal_projector,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_llava_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_llava.py
234
235
236
237
238
239
240
241
242
243
244
245
246
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
    )
    return GenerationOutputs(sequences=outputs)

generate ¤

generate(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Optional[Tensor] = None,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 1,
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_llava.py
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
293
294
295
296
297
298
@config_defaults_method("core/model/generation/peft/lora/llava/mistral_clip")
@torch.no_grad()
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def generate(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 1,
    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,
):
    outputs = super().generate(
        input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
        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,
    )

LlavaLlamaSiglipLoraForGeneration¤

Tip

core/model/generation/peft/lora/llava/llama_siglip is the section for configuration of LlavaLlamaSiglipLoraForGeneration.

Bases: LlavaLlamaSiglipLoraForGeneration

LlavaMistralClipLora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_llava.py
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
def __init__(
    self,
    config_path: str,
    image_token_index: Optional[int] = 128077,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    freeze_multi_modal_projector: Optional[bool] = True,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        image_token_index=image_token_index,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        freeze_multi_modal_projector=freeze_multi_modal_projector,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_llava.py
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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
@classmethod
@config_defaults_init("core/model/generation/peft/lora/llava/llama_siglip")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/lora/llava/llama_siglip")
    pretrained_name = config.getoption("pretrained_name", "llava-v1.6-joycaption-2")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_llava_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    image_token_index = config.getoption("image_token_index", 128077)
    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])
    freeze_multi_modal_projector = config.getoption(
        "freeze_multi_modal_projector", True
    )
    gradient_checkpointing = config.getoption("gradient_checkpointing", False)

    inst = cls(
        config_path,
        image_token_index=image_token_index,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        freeze_multi_modal_projector=freeze_multi_modal_projector,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_llava_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_llava.py
393
394
395
396
397
398
399
400
401
402
403
404
405
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
    )
    return GenerationOutputs(sequences=outputs)

generate ¤

generate(
    input_ids: Tensor,
    pixel_values: Tensor,
    attention_mask: Optional[Tensor] = None,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 128000,
    decoder_end_token_id: Optional[
        Union[int, List[int]]
    ] = [128001, 128008, 128009],
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_llava.py
407
408
409
410
411
412
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
@config_defaults_method("core/model/generation/peft/lora/llava/llama_siglip")
@torch.no_grad()
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def generate(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 128000,
    decoder_end_token_id: Optional[Union[int, List[int]]] = [
        128001,
        128008,
        128009,
    ],
    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,
):
    outputs = super().generate(
        input_ids,
        pixel_values=pixel_values,
        attention_mask=attention_mask,
        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,
    )

MistralLoraForClassification¤

Tip

core/model/classification/peft/lora/mistral is the section for configuration of MistralLoraForClassification.

Bases: MistralLoraForClassification

MistralLora model for classification tasks.

Source code in src/unitorch/cli/models/peft/modeling_mistral.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    num_classes: Optional[int] = 1,
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        num_classes=num_classes,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_mistral.py
 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
 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
@classmethod
@config_defaults_init("core/model/classification/peft/lora/mistral")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/classification/peft/lora/mistral")
    pretrained_name = config.getoption(
        "pretrained_name", "mistral-7b-instruct-v0.1"
    )
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_mistral_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

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

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        num_classes=num_classes,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_mistral_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_mistral.py
109
110
111
112
113
114
115
116
117
118
119
120
121
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return ClassificationOutputs(outputs=outputs)

MistralLoraForGeneration¤

Tip

core/model/generation/peft/lora/mistral is the section for configuration of MistralLoraForGeneration.

Bases: MistralLoraForGeneration

MistralLora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_mistral.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_mistral.py
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
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
202
203
204
@classmethod
@config_defaults_init("core/model/generation/peft/lora/mistral")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/lora/mistral")
    pretrained_name = config.getoption(
        "pretrained_name", "mistral-7b-instruct-v0.1"
    )
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_mistral_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

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

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_mistral_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_mistral.py
206
207
208
209
210
211
212
213
214
215
216
217
218
@autocast(device_type=("cuda" if torch.cuda.is_available() else "cpu"))
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    position_ids: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
        position_ids=position_ids,
    )
    return GenerationOutputs(sequences=outputs)

generate ¤

generate(
    input_ids: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 1,
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_mistral.py
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
@config_defaults_method("core/model/generation/peft/lora/mistral")
@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] = 1,
    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,
):
    outputs = super().generate(
        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,
    )

QWen3LoraForGeneration¤

Tip

core/model/generation/peft/lora/qwen3 is the section for configuration of QWen3LoraForGeneration.

Bases: QWen3LoraForGeneration

QWen3Lora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_qwen.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_qwen.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
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
@classmethod
@config_defaults_init("core/model/generation/peft/lora/qwen3")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/lora/qwen3")
    pretrained_name = config.getoption("pretrained_name", "qwen3-4b-thinking")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

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

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def forward(
    self,
    input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        attention_mask=attention_mask,
    )
    return GenerationOutputs(sequences=outputs)

generate ¤

generate(
    input_ids: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[
        Union[int, List[int]]
    ] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen.py
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
@config_defaults_method("core/model/generation/peft/lora/qwen3")
@torch.no_grad()
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def generate(
    self,
    input_ids: torch.Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[Union[int, List[int]]] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
):
    outputs = super().generate(
        input_ids,
        num_beams=num_beams,
        decoder_start_token_id=decoder_start_token_id,
        decoder_end_token_id=decoder_end_token_id,
        decoder_pad_token_id=decoder_pad_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,
    )

QWen3DPOLoraForGeneration¤

Tip

core/model/generation/peft/dpo/lora/qwen3 is the section for configuration of QWen3DPOLoraForGeneration.

Bases: QWen3DPOLoraForGeneration

QWen3Lora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_qwen.py
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    gradient_checkpointing: Optional[bool] = False,
    dpo_beta: Optional[float] = 0.1,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
        dpo_beta=dpo_beta,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_qwen.py
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
@classmethod
@config_defaults_init("core/model/generation/peft/dpo/lora/qwen3")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/dpo/lora/qwen3")
    pretrained_name = config.getoption("pretrained_name", "qwen3-4b-thinking")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

    gradient_checkpointing = config.getoption("gradient_checkpointing", False)
    dpo_beta = config.getoption("dpo_beta", 0.1)

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
        dpo_beta=dpo_beta,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    win_input_ids: Tensor,
    lose_input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    win_attention_mask: Optional[Tensor] = None,
    lose_attention_mask: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen.py
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def forward(
    self,
    input_ids: torch.Tensor,
    win_input_ids: torch.Tensor,
    lose_input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    win_attention_mask: Optional[torch.Tensor] = None,
    lose_attention_mask: Optional[torch.Tensor] = None,
):
    loss = super().forward(
        input_ids=input_ids,
        win_input_ids=win_input_ids,
        lose_input_ids=lose_input_ids,
        attention_mask=attention_mask,
        win_attention_mask=win_attention_mask,
        lose_attention_mask=lose_attention_mask,
    )
    return LossOutputs(loss=loss)

generate ¤

generate(
    input_ids: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[
        Union[int, List[int]]
    ] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen.py
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
@config_defaults_method("core/model/generation/peft/dpo/lora/qwen3")
@torch.no_grad()
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def generate(
    self,
    input_ids: torch.Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[Union[int, List[int]]] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
):
    outputs = super().generate(
        input_ids,
        num_beams=num_beams,
        decoder_start_token_id=decoder_start_token_id,
        decoder_end_token_id=decoder_end_token_id,
        decoder_pad_token_id=decoder_pad_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,
    )

QWen3GRPOLoraForGeneration¤

Tip

core/model/generation/peft/grpo/lora/qwen3 is the section for configuration of QWen3GRPOLoraForGeneration.

Bases: QWen3GRPOLoraForGeneration

QWen3GRPOLora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_qwen.py
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_qwen.py
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
411
412
@classmethod
@config_defaults_init("core/model/generation/peft/grpo/lora/qwen3")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/grpo/lora/qwen3")
    pretrained_name = config.getoption("pretrained_name", "qwen3-4b-thinking")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

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

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)
    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )
    return inst

forward ¤

forward(
    input_ids: Tensor,
    sampled_ids: Tensor,
    sampled_rewards: Tensor,
    attention_mask: Optional[Tensor] = None,
    sampled_attention_mask: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen.py
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def forward(
    self,
    input_ids: torch.Tensor,
    sampled_ids: torch.Tensor,
    sampled_rewards: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    sampled_attention_mask: Optional[torch.Tensor] = None,
):
    loss = super().forward(
        input_ids=input_ids,
        sampled_ids=sampled_ids,
        sampled_rewards=sampled_rewards,
        attention_mask=attention_mask,
        sampled_attention_mask=sampled_attention_mask,
    )
    return LossOutputs(loss=loss)

generate ¤

generate(
    input_ids: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[
        Union[int, List[int]]
    ] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen.py
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
483
484
485
486
@config_defaults_method("core/model/generation/peft/grpo/lora/qwen3")
@torch.no_grad()
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def generate(
    self,
    input_ids: torch.Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[Union[int, List[int]]] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
):
    outputs = super().generate(
        input_ids,
        num_beams=num_beams,
        decoder_start_token_id=decoder_start_token_id,
        decoder_end_token_id=decoder_end_token_id,
        decoder_pad_token_id=decoder_pad_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,
    )

QWen3VLLoraForGeneration¤

Tip

core/model/generation/peft/lora/qwen3_vl is the section for configuration of QWen3VLLoraForGeneration.

Bases: QWen3VLLoraForGeneration

QWen3Lora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_qwen_vl.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    gradient_checkpointing: Optional[bool] = False,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_qwen_vl.py
 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
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@classmethod
@config_defaults_init("core/model/generation/peft/lora/qwen3_vl")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/lora/qwen3_vl")
    pretrained_name = config.getoption("pretrained_name", "qwen3-vl-8b-instruct")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

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

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    image_grid_thw: Tensor,
    attention_mask: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen_vl.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    image_grid_thw: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
):
    outputs = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        image_grid_thw=image_grid_thw,
        attention_mask=attention_mask,
    )
    return GenerationOutputs(sequences=outputs)

generate ¤

generate(
    input_ids: Tensor,
    pixel_values: Tensor,
    image_grid_thw: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[
        Union[int, List[int]]
    ] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen_vl.py
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
177
@config_defaults_method("core/model/generation/peft/lora/qwen3")
@torch.no_grad()
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def generate(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    image_grid_thw: torch.Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[Union[int, List[int]]] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
):
    outputs = super().generate(
        input_ids,
        pixel_values=pixel_values,
        image_grid_thw=image_grid_thw,
        num_beams=num_beams,
        decoder_start_token_id=decoder_start_token_id,
        decoder_end_token_id=decoder_end_token_id,
        decoder_pad_token_id=decoder_pad_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,
    )

QWen3VLDPOLoraForGeneration¤

Tip

core/model/generation/peft/dpo/lora/qwen3_vl is the section for configuration of QWen3VLDPOLoraForGeneration.

Bases: QWen3VLDPOLoraForGeneration

QWen3Lora model for generation tasks.

Source code in src/unitorch/cli/models/peft/modeling_qwen_vl.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
def __init__(
    self,
    config_path: str,
    lora_r: Optional[int] = 16,
    lora_alpha: Optional[int] = 32,
    lora_dropout: Optional[float] = 0.05,
    fan_in_fan_out: Optional[bool] = True,
    target_modules: Optional[Union[List[str], str]] = ["q_proj", "v_proj"],
    gradient_checkpointing: Optional[bool] = False,
    dpo_beta: Optional[float] = 0.1,
):
    super().__init__(
        config_path=config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
        dpo_beta=dpo_beta,
    )

from_config classmethod ¤

from_config(config, **kwargs)
Source code in src/unitorch/cli/models/peft/modeling_qwen_vl.py
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
236
237
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
@classmethod
@config_defaults_init("core/model/generation/peft/dpo/lora/qwen3_vl")
def from_config(cls, config, **kwargs):
    config.set_default_section("core/model/generation/peft/dpo/lora/qwen3_vl")
    pretrained_name = config.getoption("pretrained_name", "qwen3-vl-8b-instruct")
    config_path = config.getoption("config_path", None)
    config_path = pop_value(
        config_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "config"),
    )
    config_path = cached_path(config_path)

    lora_r = config.getoption("lora_r", 16)
    lora_alpha = config.getoption("lora_alpha", 32)
    lora_dropout = config.getoption("lora_dropout", 0.05)
    fan_in_fan_out = config.getoption("fan_in_fan_out", True)
    target_modules = config.getoption("target_modules", ["q_proj", "v_proj"])

    gradient_checkpointing = config.getoption("gradient_checkpointing", False)
    dpo_beta = config.getoption("dpo_beta", 0.1)

    inst = cls(
        config_path,
        lora_r=lora_r,
        lora_alpha=lora_alpha,
        lora_dropout=lora_dropout,
        fan_in_fan_out=fan_in_fan_out,
        target_modules=target_modules,
        gradient_checkpointing=gradient_checkpointing,
        dpo_beta=dpo_beta,
    )

    weight_path = []
    pretrained_weight_path = config.getoption("pretrained_weight_path", None)
    pretrained_weight_path = pop_value(
        pretrained_weight_path,
        nested_dict_value(pretrained_qwen_infos, pretrained_name, "weight"),
        check_none=False,
    )
    if pretrained_weight_path is not None:
        if isinstance(pretrained_weight_path, str):
            weight_path.append(pretrained_weight_path)
        elif isinstance(pretrained_weight_path, list):
            weight_path.extend(pretrained_weight_path)

    pretrained_lora_weight_path = config.getoption(
        "pretrained_lora_weight_path", None
    )
    if pretrained_lora_weight_path is not None:
        weight_path.append(pretrained_lora_weight_path)

    if len(weight_path) > 0:
        inst.from_pretrained(
            weight_path=weight_path,
        )

    return inst

forward ¤

forward(
    input_ids: Tensor,
    pixel_values: Tensor,
    image_grid_thw: Tensor,
    win_input_ids: Tensor,
    lose_input_ids: Tensor,
    attention_mask: Optional[Tensor] = None,
    win_attention_mask: Optional[Tensor] = None,
    lose_attention_mask: Optional[Tensor] = None,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen_vl.py
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
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def forward(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    image_grid_thw: torch.Tensor,
    win_input_ids: torch.Tensor,
    lose_input_ids: torch.Tensor,
    attention_mask: Optional[torch.Tensor] = None,
    win_attention_mask: Optional[torch.Tensor] = None,
    lose_attention_mask: Optional[torch.Tensor] = None,
):
    loss = super().forward(
        input_ids=input_ids,
        pixel_values=pixel_values,
        image_grid_thw=image_grid_thw,
        win_input_ids=win_input_ids,
        lose_input_ids=lose_input_ids,
        attention_mask=attention_mask,
        win_attention_mask=win_attention_mask,
        lose_attention_mask=lose_attention_mask,
    )
    return LossOutputs(loss=loss)

generate ¤

generate(
    input_ids: Tensor,
    pixel_values: Tensor,
    image_grid_thw: Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[
        Union[int, List[int]]
    ] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
)
Source code in src/unitorch/cli/models/peft/modeling_qwen_vl.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
@config_defaults_method("core/model/generation/peft/dpo/lora/qwen3")
@torch.no_grad()
@autocast(
    device_type=("cuda" if torch.cuda.is_available() else "cpu"),
    dtype=(torch.bfloat16 if is_bfloat16_available() else torch.float32),
)
def generate(
    self,
    input_ids: torch.Tensor,
    pixel_values: torch.Tensor,
    image_grid_thw: torch.Tensor,
    num_beams: Optional[int] = 5,
    decoder_start_token_id: Optional[int] = 151643,
    decoder_end_token_id: Optional[Union[int, List[int]]] = 151645,
    decoder_pad_token_id: Optional[int] = 151643,
    num_return_sequences: Optional[int] = 1,
    min_gen_seq_length: Optional[int] = 0,
    max_gen_seq_length: Optional[int] = 512,
    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,
):
    outputs = super().generate(
        input_ids,
        pixel_values=pixel_values,
        image_grid_thw=image_grid_thw,
        num_beams=num_beams,
        decoder_start_token_id=decoder_start_token_id,
        decoder_end_token_id=decoder_end_token_id,
        decoder_pad_token_id=decoder_pad_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,
    )