mirror of
https://github.com/hiyouga/LLaMA-Factory.git
synced 2025-08-04 20:52:59 +08:00
parent
8fb1c94632
commit
6493f6d2e9
@ -249,7 +249,7 @@ register_template(
|
|||||||
"{{system}}"
|
"{{system}}"
|
||||||
],
|
],
|
||||||
prompt=[
|
prompt=[
|
||||||
"Human: {{query}}\nAssistant: "
|
"Human: {{query}}\nAssistant:"
|
||||||
],
|
],
|
||||||
system=(
|
system=(
|
||||||
"A chat between a curious user and an artificial intelligence assistant. "
|
"A chat between a curious user and an artificial intelligence assistant. "
|
||||||
@ -272,7 +272,7 @@ register_template(
|
|||||||
"<<SYS>>\n{{system}}\n<</SYS>>\n\n"
|
"<<SYS>>\n{{system}}\n<</SYS>>\n\n"
|
||||||
],
|
],
|
||||||
prompt=[
|
prompt=[
|
||||||
"[INST] {{query}} [/INST] "
|
"[INST] {{query}} [/INST]"
|
||||||
],
|
],
|
||||||
system=(
|
system=(
|
||||||
"You are a helpful, respectful and honest assistant. "
|
"You are a helpful, respectful and honest assistant. "
|
||||||
@ -298,7 +298,7 @@ register_template(
|
|||||||
"<<SYS>>\n{{system}}\n<</SYS>>\n\n"
|
"<<SYS>>\n{{system}}\n<</SYS>>\n\n"
|
||||||
],
|
],
|
||||||
prompt=[
|
prompt=[
|
||||||
"[INST] {{query}} [/INST] "
|
"[INST] {{query}} [/INST]"
|
||||||
],
|
],
|
||||||
system="You are a helpful assistant. 你是一个乐于助人的助手。",
|
system="You are a helpful assistant. 你是一个乐于助人的助手。",
|
||||||
sep=[]
|
sep=[]
|
||||||
|
@ -35,26 +35,18 @@ class CustomSeq2SeqTrainer(Seq2SeqTrainer):
|
|||||||
"""
|
"""
|
||||||
if self.args.predict_with_generate:
|
if self.args.predict_with_generate:
|
||||||
assert self.tokenizer.padding_side == "left", "This method only accepts left-padded tensor."
|
assert self.tokenizer.padding_side == "left", "This method only accepts left-padded tensor."
|
||||||
assert self.tokenizer.pad_token_id is not None, "Pad token is required."
|
|
||||||
prompt_len, label_len = inputs["input_ids"].size(-1), inputs["labels"].size(-1)
|
prompt_len, label_len = inputs["input_ids"].size(-1), inputs["labels"].size(-1)
|
||||||
|
labels = inputs["labels"].clone()
|
||||||
if prompt_len > label_len:
|
if prompt_len > label_len:
|
||||||
inputs["labels"] = self._pad_tensors_to_target_len(inputs["labels"], inputs["input_ids"])
|
inputs["labels"] = self._pad_tensors_to_target_len(inputs["labels"], inputs["input_ids"])
|
||||||
if label_len > prompt_len:
|
if label_len > prompt_len:
|
||||||
inputs["input_ids"] = self._pad_tensors_to_target_len(inputs["input_ids"], inputs["labels"])
|
inputs["labels"] = inputs["labels"][:, :prompt_len] # truncate the labels instead of padding the inputs
|
||||||
if "attention_mask" in inputs:
|
|
||||||
inputs["attention_mask"] = self._pad_tensors_to_target_len(
|
|
||||||
inputs["attention_mask"], inputs["labels"], pad_token_id=0
|
|
||||||
)
|
|
||||||
if "position_ids" in inputs:
|
|
||||||
inputs["position_ids"] = self._pad_tensors_to_target_len(
|
|
||||||
inputs["position_ids"], inputs["labels"], pad_token_id=0
|
|
||||||
)
|
|
||||||
|
|
||||||
loss, generated_tokens, labels = super().prediction_step(
|
loss, generated_tokens, _ = super().prediction_step(
|
||||||
model, inputs, prediction_loss_only=prediction_loss_only, ignore_keys=ignore_keys
|
model, inputs, prediction_loss_only=prediction_loss_only, ignore_keys=ignore_keys
|
||||||
)
|
)
|
||||||
if generated_tokens is not None and self.args.predict_with_generate:
|
if generated_tokens is not None and self.args.predict_with_generate:
|
||||||
generated_tokens[:, :max(prompt_len, label_len)] = self.tokenizer.pad_token_id
|
generated_tokens[:, :prompt_len] = self.tokenizer.pad_token_id
|
||||||
generated_tokens = generated_tokens.contiguous()
|
generated_tokens = generated_tokens.contiguous()
|
||||||
|
|
||||||
return loss, generated_tokens, labels
|
return loss, generated_tokens, labels
|
||||||
@ -62,14 +54,13 @@ class CustomSeq2SeqTrainer(Seq2SeqTrainer):
|
|||||||
def _pad_tensors_to_target_len(
|
def _pad_tensors_to_target_len(
|
||||||
self,
|
self,
|
||||||
src_tensor: torch.Tensor,
|
src_tensor: torch.Tensor,
|
||||||
tgt_tensor: torch.Tensor,
|
tgt_tensor: torch.Tensor
|
||||||
pad_token_id: Optional[int] = None
|
|
||||||
) -> torch.Tensor:
|
) -> torch.Tensor:
|
||||||
r"""
|
r"""
|
||||||
Pads the tensor to the same length as the target tensor.
|
Pads the tensor to the same length as the target tensor.
|
||||||
"""
|
"""
|
||||||
pad_token_id = pad_token_id if pad_token_id is not None else self.tokenizer.pad_token_id
|
assert self.tokenizer.pad_token_id is not None, "Pad token is required."
|
||||||
padded_tensor = pad_token_id * torch.ones_like(tgt_tensor)
|
padded_tensor = self.tokenizer.pad_token_id * torch.ones_like(tgt_tensor)
|
||||||
padded_tensor[:, -src_tensor.shape[-1]:] = src_tensor # adopt left-padding
|
padded_tensor[:, -src_tensor.shape[-1]:] = src_tensor # adopt left-padding
|
||||||
return padded_tensor.contiguous() # in contiguous memory
|
return padded_tensor.contiguous() # in contiguous memory
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ def run_sft(
|
|||||||
|
|
||||||
data_collator = DataCollatorForSeq2Seq(
|
data_collator = DataCollatorForSeq2Seq(
|
||||||
tokenizer=tokenizer,
|
tokenizer=tokenizer,
|
||||||
pad_to_multiple_of=4, # for shift short attention
|
pad_to_multiple_of=4 if tokenizer.padding_side == "right" else None, # for shift short attention
|
||||||
label_pad_token_id=IGNORE_INDEX if data_args.ignore_pad_token_for_loss else tokenizer.pad_token_id
|
label_pad_token_id=IGNORE_INDEX if data_args.ignore_pad_token_for_loss else tokenizer.pad_token_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user