Hey Chatgpt, can you please tell me what this function does? I'm aware it implements a…
- User
Hey Chatgpt, can you please tell me what this function does? I'm aware it implements a sliding window of some sort, but I need a break-down and then I'd like to ask a follow up question or two.
Here is the code as below:
codedef group_texts(examples): # Concatenate all texts. concatenated_examples = {k: sum(examples[k], []) for k in examples.keys()} total_length = len(concatenated_examples[list(examples.keys())[0]]) # We drop the small remainder, we could add padding if the model supported it instead of this drop, you can # customize this part to your needs. if total_length >= block_size: total_length = (total_length // block_size) * block_size input_ids = [] attention_mask = [] labels = [] # We implement a sliding window, so all tokens have a non-zero context in their prediction. # We then mask the duplicate tokens' labels, to not count any token twice in the loss. for i in tqdm(range(0, total_length, data_args.stride), total=total_length): begin_loc = max(i + data_args.stride - block_size, 0) end_loc = min(i + data_args.stride, total_length) trg_len = end_loc - i cur_input_ids = concatenated_examples['input_ids'][begin_loc:end_loc] cur_labels = list(cur_input_ids) cur_labels[:-trg_len] = [padding_index] * (len(cur_labels) - trg_len) if len(cur_input_ids) < block_size: padding_size = block_size - len(cur_input_ids) pad_token_id = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else tokenizer.eos_token_id cur_input_ids += [pad_token_id] * padding_size cur_labels += [padding_index] * padding_size input_ids.append(cur_input_ids) attention_mask.append([1] * len(cur_labels)) labels.append(cur_labels) result = {'input_ids': input_ids, 'labels': labels, 'attention_mask': attention_mask} return result - Assistant
- User
So the function in question makes it such that for example, if I have a block size of 1024 and a stride step of 512, it'll be padded to the left with 512 tokens of -100, with 512 tokens of the labels.
When I use this code with Trainer.evaluate (this is from a larger program), the issue is that creates a scenario where the labels are actually 512(as 512 tokens are pad tokens which are ignored) when the predictions are 1024 tokens. Is this intentional? I'm not sure as to how sliding window attention is improving things such as perplexity outside of basically amputating part of the label and moving the goal posts with a longer prediction output vs the label.
- Assistant