MCPcopy Create free account
hub / github.com/cpystan/SD-VLM / preprocess

Function preprocess

llava/train/train.py:618–664  ·  view source on GitHub ↗

Given a list of sources, each is a conversation list. This transform: 1. Add signal '### ' at the beginning each sentence, with end signal '\n'; 2. Concatenate conversations together; 3. Tokenize the concatenated conversation; 4. Make a deepcopy as the target. Mask human words w

(
    sources: Sequence[str],
    tokenizer: transformers.PreTrainedTokenizer,
    has_image: bool = False
)

Source from the content-addressed store, hash-verified

616
617
618def preprocess(
619 sources: Sequence[str],
620 tokenizer: transformers.PreTrainedTokenizer,
621 has_image: bool = False
622) -> Dict:
623 """
624 Given a list of sources, each is a conversation list. This transform:
625 1. Add signal '### ' at the beginning each sentence, with end signal '\n';
626 2. Concatenate conversations together;
627 3. Tokenize the concatenated conversation;
628 4. Make a deepcopy as the target. Mask human words with IGNORE_INDEX.
629 """
630
631 if conversation_lib.default_conversation.sep_style == conversation_lib.SeparatorStyle.PLAIN:
632 return preprocess_plain(sources, tokenizer)
633 if conversation_lib.default_conversation.sep_style == conversation_lib.SeparatorStyle.LLAMA_2:
634 return preprocess_llama_2(sources, tokenizer, has_image=has_image)
635 if conversation_lib.default_conversation.version.startswith("v1"):
636 return preprocess_v1(sources, tokenizer, has_image=has_image)
637 if conversation_lib.default_conversation.version == "mpt":
638 return preprocess_mpt(sources, tokenizer, has_image=has_image)
639 # add end signal and concatenate together
640 conversations = []
641 for source in sources:
642 header = f"{conversation_lib.default_conversation.system}\n\n"
643 conversation = _add_speaker_and_signal(header, source)
644 conversations.append(conversation)
645 # tokenize conversations
646 def get_tokenize_len(prompts):
647 return [len(tokenizer_image_token(prompt, tokenizer)) for prompt in prompts]
648
649 if has_image:
650 input_ids = [tokenizer_image_token(prompt, tokenizer, return_tensors='pt') for prompt in conversations]
651 else:
652 conversations_tokenized = _tokenize_fn(conversations, tokenizer)
653 input_ids = conversations_tokenized["input_ids"]
654
655 targets = copy.deepcopy(input_ids)
656 for target, source in zip(targets, sources):
657 if has_image:
658 tokenized_lens = get_tokenize_len([header] + [s["value"] for s in source])
659 else:
660 tokenized_lens = _tokenize_fn([header] + [s["value"] for s in source], tokenizer)["input_ids_lens"]
661 speakers = [sentence["from"] for sentence in source]
662 _mask_targets(target, tokenized_lens, speakers)
663
664 return dict(input_ids=input_ids, labels=targets)
665
666
667class LazySupervisedDataset(Dataset):

Callers 1

__getitem__Method · 0.85

Calls 10

tokenizer_image_tokenFunction · 0.90
preprocess_plainFunction · 0.85
preprocess_llama_2Function · 0.85
preprocess_v1Function · 0.85
preprocess_mptFunction · 0.85
_add_speaker_and_signalFunction · 0.85
_tokenize_fnFunction · 0.85
get_tokenize_lenFunction · 0.85
_mask_targetsFunction · 0.85
appendMethod · 0.45

Tested by

no test coverage detected