[misc] lint (#9636)

This commit is contained in:
Yaowei Zheng
2025-12-20 16:19:39 +08:00
committed by GitHub
parent b0d49e137f
commit 0894b4f37e
6 changed files with 28 additions and 31 deletions

2
.gitignore vendored
View File

@@ -85,7 +85,7 @@ ipython_config.py
# pyenv # pyenv
# For a library or package, you might want to ignore these files since the code is # For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in: # intended to run in multiple environments; otherwise, check them in:
# .python-version .python-version
# pipenv # pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.

View File

@@ -1624,7 +1624,12 @@ class Qwen3VLPlugin(Qwen2VLPlugin):
for video, duration in zip(videos["videos"], videos["durations"]) for video, duration in zip(videos["videos"], videos["durations"])
] ]
mm_inputs.update( mm_inputs.update(
video_processor(videos=videos["videos"], video_metadata=video_metadata, fps=getattr(processor, "video_fps", 2.0), return_metadata=True) video_processor(
videos=videos["videos"],
video_metadata=video_metadata,
fps=getattr(processor, "video_fps", 2.0),
return_metadata=True,
)
) )
temporal_patch_size: int = getattr(image_processor, "temporal_patch_size", 2) temporal_patch_size: int = getattr(image_processor, "temporal_patch_size", 2)
if "second_per_grid_ts" in processor.model_input_names: if "second_per_grid_ts" in processor.model_input_names:

View File

@@ -15,32 +15,24 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING
import torch import torch
import torch.nn.functional as F from ktransformers.sft.lora import KTrainer # type: ignore
from transformers import Trainer
from trl import DPOTrainer
from trl.trainer import disable_dropout_in_model
from typing_extensions import override from typing_extensions import override
from ...extras.constants import IGNORE_INDEX from ..trainer_utils import get_batch_logps, nested_detach
from ...extras.packages import is_transformers_version_greater_than from .trainer import CustomDPOTrainer
from ..callbacks import SaveProcessorCallback
from ..trainer_utils import create_custom_optimizer, create_custom_scheduler, get_batch_logps, nested_detach
from .trainer import CustomDPOTrainer as BaseDPOTrainer
from ktransformers.sft.lora import KTrainer
if TYPE_CHECKING: if TYPE_CHECKING:
from transformers import PreTrainedModel, ProcessorMixin from transformers import PreTrainedModel
from ...hparams import FinetuningArguments
class CustomDPOTrainer(KTrainer, BaseDPOTrainer): class KDPOTrainer(KTrainer, CustomDPOTrainer):
@override @override
def concatenated_forward( def concatenated_forward(
self, model: "PreTrainedModel", batch: dict[str, "torch.Tensor"], is_ref_model: bool = False self, model: "PreTrainedModel", batch: dict[str, "torch.Tensor"], is_ref_model: bool = False
) -> tuple["torch.Tensor", "torch.Tensor", "torch.Tensor", "torch.Tensor", "torch.Tensor"]: ) -> tuple["torch.Tensor", "torch.Tensor", "torch.Tensor", "torch.Tensor", "torch.Tensor"]:
r"""Compute the sum log probabilities of the labels under given logits if loss_type is not IPO, ORPO or SimPO. r"""Compute the sum log probabilities of the labels under given logits if loss_type is not IPO, ORPO or SimPO.
@@ -48,9 +40,8 @@ class CustomDPOTrainer(KTrainer, BaseDPOTrainer):
""" """
if self.finetuning_args.use_ref_model: if self.finetuning_args.use_ref_model:
batch = nested_detach(batch, clone=True) # avoid error batch = nested_detach(batch, clone=True) # avoid error
labels = batch["labels"]
# dpo not need compute loss in forward, waste mem labels = batch.pop("labels") # dpo do not need compute loss in forward
del batch["labels"]
all_logits: torch.Tensor = model(**batch, return_dict=True, use_cache=False).logits.to(torch.float32) all_logits: torch.Tensor = model(**batch, return_dict=True, use_cache=False).logits.to(torch.float32)
all_logits = all_logits.to("cpu") all_logits = all_logits.to("cpu")
labels = labels.to(all_logits.device) labels = labels.to(all_logits.device)

View File

@@ -218,9 +218,10 @@ class CustomDPOTrainer(DPOTrainer):
if self.finetuning_args.use_ref_model: if self.finetuning_args.use_ref_model:
batch = nested_detach(batch, clone=True) # avoid error batch = nested_detach(batch, clone=True) # avoid error
labels = batch.pop("labels") # dpo do not need compute loss in forward
all_logits: torch.Tensor = model(**batch, return_dict=True, use_cache=False).logits.to(torch.float32) all_logits: torch.Tensor = model(**batch, return_dict=True, use_cache=False).logits.to(torch.float32)
all_logps, valid_length = get_batch_logps( all_logps, valid_length = get_batch_logps(
logits=all_logits, labels=batch["labels"], ld_alpha=(self.ld_alpha if not is_ref_model else None) logits=all_logits, labels=labels, ld_alpha=(self.ld_alpha if not is_ref_model else None)
) )
if self.loss_type in ["ipo", "orpo", "simpo"]: if self.loss_type in ["ipo", "orpo", "simpo"]:
all_logps = all_logps / valid_length all_logps = all_logps / valid_length

View File

@@ -62,13 +62,13 @@ def run_dpo(
else: else:
ref_model = None ref_model = None
if model_args.use_kt: if model_args.use_kt:
from ktransformers.util.globals import GLOBAL_CONFIG from ktransformers.util.globals import GLOBAL_CONFIG # type: ignore
from .ktrainer import KDPOTrainer as CustomDPOTrainer
GLOBAL_CONFIG._config["mod"] = "sft" GLOBAL_CONFIG._config["mod"] = "sft"
from .ktrainer import CustomDPOTrainer
else: else:
from .trainer import CustomDPOTrainer from .trainer import CustomDPOTrainer

View File

@@ -99,8 +99,8 @@ def run_sft(
# Initialize our Trainer # Initialize our Trainer
if model_args.use_kt: if model_args.use_kt:
from ktransformers.util.globals import GLOBAL_CONFIG from ktransformers.sft.lora import KTrainer # type: ignore
from ktransformers.sft.lora import KTrainer from ktransformers.util.globals import GLOBAL_CONFIG # type: ignore
GLOBAL_CONFIG._config["mod"] = "sft" GLOBAL_CONFIG._config["mod"] = "sft"