Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add llama-2 template support for fine-tuning #2423

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions fastchat/train/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def preprocess(
sources,
tokenizer: transformers.PreTrainedTokenizer,
) -> Dict:
conv = get_conversation_template("vicuna")
conv = get_conversation_template("llama-2")
roles = {"human": conv.roles[0], "gpt": conv.roles[1]}

# Apply prompt templates
Expand All @@ -111,10 +111,10 @@ def preprocess(
).input_ids
targets = input_ids.clone()

assert conv.sep_style == SeparatorStyle.ADD_COLON_TWO
assert conv.sep_style == SeparatorStyle.LLAMA2

# Mask targets. Only compute loss on the assistant outputs.
sep = conv.sep + conv.roles[1] + ": "
sep = conv.sep
for conversation, target in zip(conversations, targets):
total_len = int(target.ne(tokenizer.pad_token_id).sum())

Expand All @@ -131,19 +131,20 @@ def preprocess(
break
parts[0] += sep
# "-2" is hardcoded for the LLaMA tokenizer to make the offset correct.
instruction_len = len(tokenizer(parts[0]).input_ids) - 2
instruction_len = len(tokenizer(parts[0]).input_ids) - 1

# Ignore the user instructions
target[cur_len : cur_len + instruction_len] = IGNORE_TOKEN_ID
cur_len += turn_len

target[cur_len:] = IGNORE_TOKEN_ID
target[cur_len+1:] = IGNORE_TOKEN_ID

if False: # Inspect and check the correctness of masking
z = target.clone()
z = torch.where(z == IGNORE_TOKEN_ID, tokenizer.unk_token_id, z)
rank0_print(tokenizer.decode(z))

cur_len += 2 #hackish fix to match the total length TODO(Karthik)
if cur_len < tokenizer.model_max_length:
if cur_len != total_len:
target[:] = IGNORE_TOKEN_ID
Expand Down