Initialize optimizer research workspace with official baselines and history-free coverage

Vendor pinned MeZO, AGZO, LOZO and LoREN sources with independent environments. Add direct in-place fix/adjust/node training, derived history-free basis gradients, development-best checkpoints and experiment tooling. Keep models, datasets, caches and credentials outside Git.

Validation: 93 project tests passed; 521 vendored author files verified.
This commit is contained in:
gloamxun committed 2026-11-12 23:32:33 +08:00
commit e0feabec8f
596 files changed
+87356

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
from .glue import GLUEDataModule, GLUE_TASKS
from .superglue import SuperGLUEDataModule, SUPERGLUE_TASKS
__all__ = ['GLUEDataModule', 'GLUE_TASKS', 'SuperGLUEDataModule', 'SUPERGLUE_TASKS']
def get_data_module(args):
if args.task in GLUE_TASKS:
dm_class = GLUEDataModule
elif args.task in SUPERGLUE_TASKS:
dm_class = SuperGLUEDataModule
else:
raise ValueError(f"Task {args.task} is not supported")
dm = dm_class(
model_name_or_path=args.model_name,
task_name=args.task,
max_seq_length=args.max_seq_length,
sample_size=args.samplesize,
train_batch_size=args.batchsize,
validation_sample_size=args.samplesize_validation,
eval_batch_size=args.batchsize,
soft_prompt=args.soft_prompt,
hf_token=args.hf_token
)
dm.setup(stage='fit')
return dm
+184
View File
@@ -0,0 +1,184 @@
from typing import Optional
import datasets
import lightning as L
from torch.utils.data import DataLoader, Subset
from transformers import AutoTokenizer
from dataset.mezo_dataset import tokenize_multipart_input
GLUE_TASKS = ["cola", "sst2", "mnli", "qnli"]
class GLUEDataModule(L.LightningDataModule):
task_text_field_map = {
"cola": ["sentence"],
"sst2": ["sentence"],
"mnli": ["premise", "hypothesis"],
"qnli": ["question", "sentence"],
}
glue_task_num_labels = {
"cola": 2,
"sst2": 2,
"mnli": 3,
"qnli": 2,
}
loader_columns = [
"datasets_idx",
"input_ids",
"token_type_ids",
"attention_mask",
"start_positions",
"end_positions",
"labels"
]
glue_task_label_mapping = {
'sst2': {
0: 'terrible',
1: 'great'
}
}
def __init__(
self,
model_name_or_path: str,
task_name: str = "mrpc",
max_seq_length: int = 512,
train_batch_size: int = 32,
eval_batch_size: int = 32,
sample_size: int = 128,
validation_sample_size: int = 128,
soft_prompt: bool = False,
hf_token: Optional[str] = None,
**kwargs,
):
super().__init__()
self.model_name_or_path = model_name_or_path
self.task_name = task_name
self.max_seq_length = max_seq_length
self.train_batch_size = train_batch_size
self.eval_batch_size = eval_batch_size
self.sample_size = sample_size
self.validation_sample_size = validation_sample_size
self.text_fields = self.task_text_field_map[task_name]
self.num_labels = self.glue_task_num_labels[task_name]
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name_or_path, use_fast=True, token=hf_token)
self.soft_prompt = soft_prompt
if soft_prompt is True:
self.loader_columns.append("mask_pos")
def setup(self, stage: str = 'fit', randomize=False, seed=None):
self.dataset = datasets.load_dataset("glue", self.task_name)
for split in self.dataset.keys():
if split == "test":
continue
if self.soft_prompt is True:
self.dataset[split] = self.dataset[split].map(
self.convert_to_features_soft_prompt,
batched=False,
remove_columns=["label"],
load_from_cache_file=True
)
else:
self.dataset[split] = self.dataset[split].map(
self.convert_to_features,
batched=True,
remove_columns=["label"],
load_from_cache_file=True
)
self.columns = [c for c in self.dataset[split].column_names if c in self.loader_columns]
self.dataset[split].set_format(type="torch", columns=self.columns)
self.eval_splits = [x for x in self.dataset.keys() if "validation" in x]
self.subset_indices = list(range(self.sample_size))
self.subset_indices_val = list(range(self.validation_sample_size))
self.subset_train_dataset = Subset(self.dataset["train"], self.subset_indices)
if len(self.eval_splits) == 1:
self.subset_val_dataset = Subset(self.dataset["validation"], self.subset_indices_val)
else:
self.subset_val_dataset = Subset(self.dataset["validation_matched"], self.subset_indices_val)
def prepare_data(self):
datasets.load_dataset("glue", self.task_name)
AutoTokenizer.from_pretrained(self.model_name_or_path, use_fast=True)
def train_dataloader(self):
return DataLoader(self.subset_train_dataset, batch_size=self.train_batch_size, shuffle=True)
def train_full_dataloader(self):
return DataLoader(self.subset_train_dataset, batch_size=self.sample_size, shuffle=True)
def val_dataloader(self):
return DataLoader(self.subset_val_dataset, batch_size=self.eval_batch_size)
def test_dataloader(self):
if len(self.eval_splits) == 1:
return DataLoader(self.dataset["test"], batch_size=self.eval_batch_size)
elif len(self.eval_splits) > 1:
return [DataLoader(self.dataset[x], batch_size=self.eval_batch_size) for x in self.eval_splits]
def convert_to_features_soft_prompt(self, example_batch, indices=None):
if self.task_name == 'sst2':
inputs = tokenize_multipart_input(
input_text_list=[example_batch[self.text_fields[0]]],
max_length=self.max_seq_length,
tokenizer=self.tokenizer,
task_name=self.task_name,
prompt=True,
template='*cls**sent_0*_It_was*mask*.*sep+*',
label_word_list={0: ' terrible', 1: ' great'}
)
inputs['labels'] = example_batch["label"]
return inputs
elif self.task_name == 'mnli':
inputs = tokenize_multipart_input(
input_text_list=[example_batch[self.text_fields[0]], example_batch[self.text_fields[1]]],
max_length=256,
tokenizer=self.tokenizer,
task_name=self.task_name,
prompt=True,
template='*cls**sent-_0*?*mask*,*+sentl_1**sep+*',
label_word_list={'contradiction': 'No', 'entailment': 'Yes', 'neutral': 'Maybe'},
first_sent_limit=240
)
inputs['labels'] = example_batch["label"]
return inputs
def convert_to_features(self, example_batch, indices=None):
# Either encode single sentence or sentence pairs
if len(self.text_fields) > 1:
texts_or_text_pairs = list(zip(example_batch[self.text_fields[0]],
example_batch[self.text_fields[1]]))
else:
texts_or_text_pairs = example_batch[self.text_fields[0]]
# Tokenize the text/text pairs
if (self.model_name_or_path == 'distilbert-base-cased' or
self.model_name_or_path == 'roberta-large'):
features = self.tokenizer.batch_encode_plus(texts_or_text_pairs,
max_length=self.max_seq_length,
pad_to_max_length=True,
truncation=True)
# Rename label to labels to make it easier to pass to model forward
features["labels"] = example_batch["label"]
elif 'gpt2' in self.model_name_or_path or 'opt' in self.model_name_or_path:
if self.tokenizer.pad_token_id is None:
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
features = self.tokenizer.batch_encode_plus(texts_or_text_pairs,
max_length=self.max_seq_length,
pad_to_max_length=True,
truncation=True)
features["labels"] = example_batch["label"]
return features
+696
View File
@@ -0,0 +1,696 @@
"""Dataset utils for different data settings for GLUE."""
import os
import torch
import numpy as np
import time
from filelock import FileLock
import json
# from src.processors import processors_mapping, num_labels_mapping, output_modes_mapping, compute_metrics_mapping, median_mapping
from transformers.data.processors.utils import InputFeatures
import dataclasses
from dataclasses import dataclass
from typing import List, Optional, Union
import pandas as pd
from common.logging import logger
# logger = logging.getLogger(__name__)
# logger.setLevel(logging.INFO)
@dataclass(frozen=True)
class OurInputFeatures(InputFeatures):
"""
Inherit from Transformers' InputFeatuers.
"""
input_ids: List[int]
attention_mask: Optional[List[int]] = None
token_type_ids: Optional[List[int]] = None
label: Optional[Union[int, float]] = None
mask_pos: Optional[List[int]] = None # Position of the mask token
label_word_list: Optional[List[int]] = None # Label word mapping (dynamic)
# For icl sfc
sfc_input_ids: List[int] = None
sfc_attention_mask: Optional[List[int]] = None
sfc_mask_pos: Optional[List[int]] = None
def to_json_string(self):
"""Serializes this instance to a JSON string."""
return json.dumps(dataclasses.asdict(self)) + "\n"
def input_example_to_string(example, sep_token):
if example.text_b is None:
return example.text_a
else:
# Warning: very simple hack here
return example.text_a + ' ' + sep_token + ' ' + example.text_b
def input_example_to_tuple(example):
if example.text_b is None:
if pd.isna(example.text_a) or example.text_a is None:
return ['']
logger.warn("Empty input")
else:
return [example.text_a]
else:
return [example.text_a, example.text_b]
def tokenize_multipart_input(
input_text_list,
max_length,
tokenizer,
task_name=None,
prompt=False,
template=None,
label_word_list=None,
first_sent_limit=None,
other_sent_limit=None,
gpt3=False,
truncate_head=False,
support_labels=None,
):
def enc(text):
return tokenizer.encode(text, add_special_tokens=False)
input_ids = []
attention_mask = []
token_type_ids = [] # Only for BERT
mask_pos = None # Position of the mask token
if prompt:
"""
Concatenate all sentences and prompts based on the provided template.
Template example: '*cls*It was*mask*.*sent_0**<sep>*label_0:*sent_1**<sep>**label_1*:*sent_2**<sep>*'
*xx* represent variables:
*cls*: cls_token
*mask*: mask_token
*sep*: sep_token
*sep+*: sep_token, also means +1 for segment id
*sent_i*: sentence i (input_text_list[i])
*sent-_i*: same as above, but delete the last token
*sentl_i*: same as above, but use lower case for the first word
*sentl-_i*: same as above, but use lower case for the first word and delete the last token
*+sent_i*: same as above, but add a space before the sentence
*+sentl_i*: same as above, but add a space before the sentence and use lower case for the first word
*label_i*: label_word_list[i]
*label_x*: label depends on the example id (support_labels needed). this is only used in GPT-3's in-context learning
Use "_" to replace space.
PAY ATTENTION TO SPACE!! DO NOT leave space before variables, for this will lead to extra space token.
"""
assert template is not None
special_token_mapping = {
'bos': tokenizer.bos_token_id, 'cls': tokenizer.cls_token_id, 'eos': tokenizer.eos_token_id, 'mask': tokenizer.mask_token_id, 'sep': tokenizer.sep_token_id, 'sep+': tokenizer.sep_token_id,
}
template_list = template.split('*') # Get variable list in the template
segment_id = 0 # Current segment id. Segment id +1 if encountering sep+.
for part_id, part in enumerate(template_list):
new_tokens = []
segment_plus_1_flag = False
if part in special_token_mapping:
if (part == 'cls' or part == 'bos'): # and ('T5' in type(tokenizer).__name__ or tokenizer.model_type == "gpt2"):
# T5 or GPT-2 do not have cls token
continue
new_tokens.append(special_token_mapping[part])
if part == 'sep+':
segment_plus_1_flag = True
elif part[:6] == 'label_':
# Note that label_word_list already has extra space, so do not add more space ahead of it.
label_id = int(part.split('_')[1])
label_word = label_word_list[label_id]
new_tokens.append(label_word)
elif part[:7] == 'labelx_':
instance_id = int(part.split('_')[1])
label_id = support_labels[instance_id]
label_word = label_word_list[label_id]
new_tokens.append(label_word)
elif part[:5] == 'sent_':
sent_id = int(part.split('_')[1])
new_tokens += enc(input_text_list[sent_id])
elif part[:6] == '+sent_':
# Add space
sent_id = int(part.split('_')[1])
new_tokens += enc(' ' + input_text_list[sent_id])
elif part[:6] == 'sent-_':
# Delete the last token
sent_id = int(part.split('_')[1])
new_tokens += enc(input_text_list[sent_id][:-1])
elif part[:6] == 'sentl_':
# Lower case the first token
sent_id = int(part.split('_')[1])
text = input_text_list[sent_id]
text = text[:1].lower() + text[1:]
new_tokens += enc(text)
elif part[:7] == '+sentl_':
# Lower case the first token and add space
sent_id = int(part.split('_')[1])
text = input_text_list[sent_id]
text = text[:1].lower() + text[1:]
new_tokens += enc(' ' + text)
elif part[:7] == 'sentl-_':
# Lower case the first token and discard the last token
sent_id = int(part.split('_')[1])
text = input_text_list[sent_id]
text = text[:1].lower() + text[1:]
new_tokens += enc(text[:-1])
elif part[:6] == 'sentu_':
# Upper case the first token
sent_id = int(part.split('_')[1])
text = input_text_list[sent_id]
text = text[:1].upper() + text[1:]
new_tokens += enc(text)
elif part[:7] == '+sentu_':
# Upper case the first token and add space
sent_id = int(part.split('_')[1])
text = input_text_list[sent_id]
text = text[:1].upper() + text[1:]
new_tokens += enc(' ' + text)
elif part[:8] == '+sentu-_':
# Upper case the first token and add space
sent_id = int(part.split('_')[1])
text = input_text_list[sent_id]
text = text[:1].upper() + text[1:]
new_tokens += enc(' ' + text[:-1])
else:
# Just natural language prompt
part = part.replace('_', ' ')
# handle special case when T5 tokenizer might add an extra space
if len(part) == 1:
new_tokens.append(tokenizer.convert_tokens_to_ids(part))
else:
new_tokens += enc(part)
if part[:4] == 'sent' or part[1:5] == 'sent':
# If this part is the sentence, limit the sentence length
sent_id = int(part.split('_')[1])
if sent_id == 0:
if first_sent_limit is not None:
new_tokens = new_tokens[:first_sent_limit]
else:
if other_sent_limit is not None:
new_tokens = new_tokens[:other_sent_limit]
input_ids += new_tokens
attention_mask += [1 for i in range(len(new_tokens))]
token_type_ids += [segment_id for i in range(len(new_tokens))]
if segment_plus_1_flag:
segment_id += 1
else:
if tokenizer.cls_token_id is not None:
input_ids = [tokenizer.cls_token_id]
attention_mask = [1]
token_type_ids = [0]
else:
input_ids = []
attention_mask = []
token_type_ids = []
for sent_id, input_text in enumerate(input_text_list):
if input_text is None:
# Do not have text_b
continue
if pd.isna(input_text) or input_text is None:
# Empty input
input_text = ''
input_tokens = enc(input_text) + [tokenizer.sep_token_id]
input_ids += input_tokens
attention_mask += [1 for i in range(len(input_tokens))]
token_type_ids += [sent_id for i in range(len(input_tokens))]
if 'T5' in type(tokenizer).__name__: # T5 does not have CLS token
input_ids = input_ids[1:]
attention_mask = attention_mask[1:]
token_type_ids = token_type_ids[1:]
# Padding
if first_sent_limit is not None and len(input_ids) > max_length:
# If using sentence limit, the total length still exceeds the maximum limit, report a warning
logger.warn("Input exceeds max_length limit: {}".format(tokenizer.decode(input_ids)))
# Code below is commented out, because we use dynamic padding rather than static padding to max_length
while len(input_ids) < max_length:
input_ids.append(tokenizer.pad_token_id)
attention_mask.append(0)
token_type_ids.append(0)
# Truncate
if len(input_ids) > max_length:
if truncate_head:
input_ids = input_ids[-max_length:]
attention_mask = attention_mask[-max_length:]
token_type_ids = token_type_ids[-max_length:]
else:
# Default is to truncate the tail
input_ids = input_ids[:max_length]
attention_mask = attention_mask[:max_length]
token_type_ids = token_type_ids[:max_length]
# Find mask token
if prompt and tokenizer.mask_token_id is not None:
# Make sure that the masked position is inside the max_length
assert tokenizer.mask_token_id in input_ids, \
"Mask token not found for input: {} {}".format(input_text_list, input_ids)
mask_pos = [input_ids.index(tokenizer.mask_token_id)]
assert mask_pos[0] < max_length
elif prompt and tokenizer.mask_token_id is None:
# autoregressive model
mask_pos = [len(input_ids) - 1]
result = {'input_ids': input_ids, 'attention_mask': attention_mask}
if 'BERT' in type(tokenizer).__name__:
# Only provide token type ids for BERT
result['token_type_ids'] = token_type_ids
if prompt:
result['mask_pos'] = mask_pos
return result
class FewShotDataset(torch.utils.data.Dataset):
"""Few-shot dataset."""
def __init__(self, args, tokenizer, cache_dir=None, mode="train", use_demo=False):
self.args = args
self.task_name = args.task_name
self.processor = processors_mapping[args.task_name]
self.tokenizer = tokenizer
self.mode = mode
# If not using demonstrations, use use_demo=True
self.use_demo = use_demo
if self.use_demo:
print("Use demonstrations")
assert mode in ["train", "dev", "test"]
# Get label list and (for prompt) label word list
self.label_list = self.processor.get_labels()
self.num_labels = len(self.label_list)
if args.prompt and args.mapping is not None:
self.label_to_word = eval(args.mapping)
for key in self.label_to_word:
# For RoBERTa/BART/T5, tokenization also considers space, so we use space+word as label words.
if self.label_to_word[key][0] not in ['<', '[', '.', ',']:
# Make sure space+word is in the vocabulary
assert len(tokenizer.tokenize(' ' + self.label_to_word[key])) == 1
self.label_to_word[key] = tokenizer.convert_tokens_to_ids(tokenizer.tokenize(' ' + self.label_to_word[key])[0])
else:
self.label_to_word[key] = tokenizer.convert_tokens_to_ids(self.label_to_word[key])
print("Label {} to word {} ({})".format(key, tokenizer.convert_ids_to_tokens(self.label_to_word[key]), self.label_to_word[key]))
if len(self.label_list) > 1:
self.label_word_list = [self.label_to_word[label] for label in self.label_list]
else:
# Regression task
# '0' represents low polarity and '1' represents high polarity.
self.label_word_list = [self.label_to_word[label] for label in ['0', '1']]
else:
self.label_to_word = None
self.label_word_list = None
# Multiple sampling: when using demonstrations, we sample different combinations of demonstrations during
# inference and aggregate the results by averaging the logits. The number of different samples is num_sample.
if (mode == "train") or not self.use_demo:
# We do not do multiple sampling when not using demonstrations or when it's the training mode
self.num_sample = 1
else:
self.num_sample = args.num_sample
# If we use multiple templates, we also need to do multiple sampling during inference.
if args.prompt and args.template_list is not None:
print("There are %d templates. Multiply num_sample by %d" % (len(args.template_list), len(args.template_list)))
self.num_sample *= len(args.template_list)
print("Total num_sample for mode %s: %d" % (mode, self.num_sample))
# Load cache
# Cache name distinguishes mode, task name, tokenizer, and length. So if you change anything beyond these elements, make sure to clear your cache.
cached_features_file = os.path.join(
cache_dir if cache_dir is not None else args.data_dir,
"cached_{}_{}_{}_{}".format(
mode,
tokenizer.__class__.__name__ + "-" + tokenizer.model_type,
str(args.max_seq_length),
args.task_name,
),
)
print(f"Creating/loading examples from dataset file at {args.data_dir}")
lock_path = cached_features_file + ".lock"
with FileLock(lock_path):
if os.path.exists(cached_features_file) and not args.overwrite_cache:
start = time.time()
self.support_examples, self.query_examples = torch.load(cached_features_file)
print(
f"Loading features from cached file {cached_features_file} [took %.3f s]", time.time() - start
)
else:
print(f"Creating features from dataset file at {args.data_dir}")
# The support examples are sourced from the training set.
self.support_examples = self.processor.get_train_examples(args.data_dir)
if mode == "dev":
self.query_examples = self.processor.get_dev_examples(args.data_dir)
elif mode == "test":
self.query_examples = self.processor.get_test_examples(args.data_dir)
else:
self.query_examples = self.support_examples
start = time.time()
torch.save([self.support_examples, self.query_examples], cached_features_file)
# ^ This seems to take a lot of time so I want to investigate why and how we can improve.
print(
"Saving features into cached file %s [took %.3f s]", cached_features_file, time.time() - start
)
# For filtering in using demonstrations, load pre-calculated embeddings
if self.use_demo and args.demo_filter:
split_name = ''
if mode == 'train':
split_name = 'train'
elif mode == 'dev':
if args.task_name == 'mnli':
split_name = 'dev_matched'
elif args.task_name == 'mnli-mm':
split_name = 'dev_mismatched'
else:
split_name = 'dev'
elif mode == 'test':
if args.task_name == 'mnli':
split_name = 'test_matched'
elif args.task_name == 'mnli-mm':
split_name = 'test_mismatched'
else:
split_name = 'test'
else:
raise NotImplementedError
self.support_emb = np.load(os.path.join(args.data_dir, "train_{}.npy".format(args.demo_filter_model)))
self.query_emb = np.load(os.path.join(args.data_dir, "{}_{}.npy".format(split_name, args.demo_filter_model)))
print("Load embeddings (for demonstration filtering) from {}".format(os.path.join(args.data_dir, "{}_{}.npy".format(split_name, args.demo_filter_model))))
assert len(self.support_emb) == len(self.support_examples)
assert len(self.query_emb) == len(self.query_examples)
# Size is expanded by num_sample
self.size = len(self.query_examples) * self.num_sample
# Prepare examples (especially for using demonstrations)
support_indices = list(range(len(self.support_examples)))
self.example_idx = []
for sample_idx in range(self.num_sample):
for query_idx in range(len(self.query_examples)):
# If training, exclude the current example. Else keep all.
if self.use_demo and args.demo_filter:
# Need sentence_transformers for demonstrations,
# which is not included in the requirements for us, but see original LM-BFF repo.
from sentence_transformers import SentenceTransformer, util
# Demonstration filtering
candidate = [support_idx for support_idx in support_indices
if support_idx != query_idx or mode != "train"]
sim_score = []
for support_idx in candidate:
sim_score.append((support_idx, util.pytorch_cos_sim(self.support_emb[support_idx], self.query_emb[query_idx])))
sim_score.sort(key=lambda x: x[1], reverse=True)
if self.num_labels == 1:
# Regression task
limit_each_label = int(len(sim_score) // 2 * args.demo_filter_rate)
count_each_label = {'0': 0, '1': 0}
context_indices = []
if args.debug_mode:
print("Query %s: %s" % (self.query_examples[query_idx].label, self.query_examples[query_idx].text_a)) # debug
for support_idx, score in sim_score:
if count_each_label['0' if float(self.support_examples[support_idx].label) <= median_mapping[args.task_name] else '1'] < limit_each_label:
count_each_label['0' if float(self.support_examples[support_idx].label) <= median_mapping[args.task_name] else '1'] += 1
context_indices.append(support_idx)
if args.debug_mode:
print(" %.4f %s | %s" % (score, self.support_examples[support_idx].label, self.support_examples[support_idx].text_a)) # debug
else:
limit_each_label = int(len(sim_score) // self.num_labels * args.demo_filter_rate)
count_each_label = {label: 0 for label in self.label_list}
context_indices = []
if args.debug_mode:
print("Query %s: %s" % (self.query_examples[query_idx].label, self.query_examples[query_idx].text_a)) # debug
for support_idx, score in sim_score:
if count_each_label[self.support_examples[support_idx].label] < limit_each_label:
count_each_label[self.support_examples[support_idx].label] += 1
context_indices.append(support_idx)
if args.debug_mode:
print(" %.4f %s | %s" % (score, self.support_examples[support_idx].label, self.support_examples[support_idx].text_a)) # debug
else:
# Using demonstrations without filtering
context_indices = [support_idx for support_idx in support_indices
if support_idx != query_idx or mode != "train"]
# We'll subsample context_indices further later.
self.example_idx.append((query_idx, context_indices, sample_idx))
# If it is not training, we pre-process the data; otherwise, we process the data online.
if mode != "train":
self.features = []
_ = 0
for query_idx, context_indices, bootstrap_idx in self.example_idx:
# The input (query) example
example = self.query_examples[query_idx]
# The demonstrations
supports = self.select_context([self.support_examples[i] for i in context_indices])
if args.template_list is not None:
template = args.template_list[sample_idx % len(args.template_list)] # Use template in order
else:
template = args.template
self.features.append(self.convert_fn(
example=example,
supports=supports,
use_demo=self.use_demo,
label_list=self.label_list,
prompt=args.prompt,
template=template,
sfc_template=getattr(args, "icl_sfc_prompt", None),
label_word_list=self.label_word_list,
verbose=True if _ == 0 else False,
))
_ += 1
else:
self.features = None
def select_context(self, context_examples):
"""
Select demonstrations from provided examples.
"""
max_demo_per_label = 1
counts = {k: 0 for k in self.label_list}
if len(self.label_list) == 1:
# Regression
counts = {'0': 0, '1': 0}
selection = []
if self.args.gpt3_in_context_head or self.args.gpt3_in_context_tail:
# For GPT-3's in-context learning, we sample gpt3_in_context_num demonstrations randomly.
order = np.random.permutation(len(context_examples))
for i in range(min(self.args.gpt3_in_context_num, len(order))):
selection.append(context_examples[order[i]])
else:
# Our sampling strategy
order = np.random.permutation(len(context_examples))
for i in order:
label = context_examples[i].label
if len(self.label_list) == 1:
# Regression
label = '0' if float(label) <= median_mapping[self.args.task_name] else '1'
if counts[label] < max_demo_per_label:
selection.append(context_examples[i])
counts[label] += 1
if sum(counts.values()) == len(counts) * max_demo_per_label:
break
assert len(selection) > 0
return selection
def __len__(self):
return self.size
def __getitem__(self, i):
if self.features is None:
query_idx, context_indices, bootstrap_idx = self.example_idx[i]
# The input (query) example
example = self.query_examples[query_idx]
# The demonstrations
supports = self.select_context([self.support_examples[i] for i in context_indices])
if self.args.template_list is not None:
template = self.args.template_list[sample_idx % len(self.args.template_list)]
else:
template = self.args.template
features = self.convert_fn(
example=example,
supports=supports,
use_demo=self.use_demo,
label_list=self.label_list,
prompt=self.args.prompt,
template=template,
label_word_list=self.label_word_list,
verbose=False,
)
else:
features = self.features[i]
return features
def get_labels(self):
return self.label_list
def convert_fn(
self,
example,
supports,
use_demo=False,
label_list=None,
prompt=False,
template=None,
sfc_template=None,
label_word_list=None,
verbose=False
):
"""
Returns a list of processed "InputFeatures".
"""
max_length = self.args.max_seq_length
# Prepare labels
label_map = {label: i for i, label in enumerate(label_list)} # Mapping the label names to label ids
if len(label_list) == 1:
# Regression
label_map = {'0': 0, '1': 1}
# Get example's label id (for training/inference)
if example.label is None:
example_label = None
elif len(label_list) == 1:
# Regerssion
example_label = float(example.label)
else:
example_label = label_map[example.label]
# Prepare other features
if not use_demo:
# No using demonstrations
inputs = tokenize_multipart_input(
input_text_list=input_example_to_tuple(example),
max_length=max_length,
tokenizer=self.tokenizer,
task_name=self.args.task_name,
prompt=prompt,
template=template,
label_word_list=label_word_list,
first_sent_limit=self.args.first_sent_limit,
other_sent_limit=self.args.other_sent_limit,
)
features = OurInputFeatures(**inputs, label=example_label)
else:
# Using demonstrations
# Max length
if self.args.double_demo:
# When using demonstrations, double the maximum length
# Note that in this case, args.max_seq_length is the maximum length for a single sentence
max_length = max_length * 2
if self.args.gpt3_in_context_head or self.args.gpt3_in_context_tail:
# When using GPT-3's in-context learning, take the maximum tokenization length of the model (512)
if self.tokenizer.model_type == "gpt2":
max_length = 1024
elif self.tokenizer.model_type == "opt":
max_length = 2048
else:
max_length = 512
# All input sentences, including the query and the demonstrations, are put into augmented_examples,
# and are numbered based on the order (starting from 0). For single sentence tasks, the input (query)
# is the sentence 0; for sentence-pair tasks, the input (query) is the sentence 0 and 1. Note that for GPT-3's
# in-context learning, the input (query) might be at the end instead of the beginning (gpt3_in_context_head)
augmented_example = []
query_text = input_example_to_tuple(example) # Input sentence list for query
support_by_label = [[] for i in range(len(label_map))]
if self.args.gpt3_in_context_head or self.args.gpt3_in_context_tail:
support_labels = []
augmented_example = query_text
for support_example in supports:
augmented_example += input_example_to_tuple(support_example)
current_label = support_example.label
if len(label_list) == 1:
current_label = '0' if float(current_label) <= median_mapping[self.args.task_name] else '1' # Regression
support_labels.append(label_map[current_label])
else:
# Group support examples by label
for label_name, label_id in label_map.items():
if len(label_list) == 1:
# Regression
for support_example in filter(lambda s: ('0' if float(s.label) <= median_mapping[self.args.task_name] else '1') == label_name, supports):
support_by_label[label_id] += input_example_to_tuple(support_example)
else:
for support_example in filter(lambda s: s.label == label_name, supports):
support_by_label[label_id] += input_example_to_tuple(support_example)
augmented_example = query_text
for label_id in range(len(label_map)):
augmented_example += support_by_label[label_id]
# Tokenization (based on the template)
inputs = tokenize_multipart_input(
input_text_list=augmented_example,
max_length=max_length,
tokenizer=self.tokenizer,
task_name=self.args.task_name,
prompt=prompt,
template=template,
label_word_list=label_word_list,
first_sent_limit=self.args.first_sent_limit,
other_sent_limit=self.args.other_sent_limit,
truncate_head=self.args.truncate_head,
gpt3=self.args.gpt3_in_context_head or self.args.gpt3_in_context_tail,
support_labels=None if not (self.args.gpt3_in_context_head or self.args.gpt3_in_context_tail) else support_labels
)
if sfc_template is not None:
# Process sfc example
if verbose:
print("*** SFC Example ***")
sfc_feature = self.convert_fn(
example, supports, use_demo=use_demo, label_list=label_list, prompt=prompt, template=sfc_template, label_word_list=label_word_list, verbose=verbose
)
features = OurInputFeatures(**inputs, label=example_label, sfc_input_ids=sfc_feature.input_ids, sfc_attention_mask=sfc_feature.attention_mask, sfc_mask_pos=sfc_feature.mask_pos)
else:
features = OurInputFeatures(**inputs, label=example_label)
if verbose:
print("*** Example ***")
print("guid: %s" % (example.guid))
print("features: %s" % features)
print("text: %s" % self.tokenizer.decode(features.input_ids))
return features
+183
View File
@@ -0,0 +1,183 @@
import datasets
import numpy as np
import pytorch_lightning as pl
from datasets import Dataset, DatasetDict
from torch.utils.data import DataLoader, Subset
from transformers import AutoTokenizer
from common.utils import temp_seed
SUPERGLUE_TASKS = ["rte", "wic", "copa", "boolq", "cb", "wsc"]
class SuperGLUEDataModule(pl.LightningDataModule):
task_num_labels = {
"rte": 2,
"wic": 2,
"copa": 2,
"boolq": 2,
"cb": 3,
"wsc": 2,
}
loader_columns = [
"datasets_idx",
"input_ids",
"token_type_ids",
"attention_mask",
"start_positions",
"end_positions",
"labels"
]
def __init__(
self,
model_name_or_path: str,
task_name: str = "rte",
max_seq_length: int = 512,
train_batch_size: int = 32,
eval_batch_size: int = 32,
sample_size: int = 128,
validation_sample_size: int = 128,
**kwargs,
):
super().__init__()
self.model_name_or_path = model_name_or_path
self.task_name = task_name
self.max_seq_length = max_seq_length
self.train_batch_size = train_batch_size
self.eval_batch_size = eval_batch_size
self.sample_size = sample_size
self.validation_sample_size = validation_sample_size
self.num_labels = self.task_num_labels[task_name]
self.tokenizer = AutoTokenizer.from_pretrained(self.model_name_or_path, use_fast=True)
def sample_subset(self, split="train", seed=0, num=100):
with temp_seed(seed):
lens = len(self.dataset[split])
index = np.random.permutation(lens).tolist()[:num + 1]
index = index[:num]
return index
def load_dataset(self, task_name):
hf_dataset = datasets.load_dataset("super_glue", task_name)
dataset = DatasetDict()
if task_name == "rte":
dataset = hf_dataset.map(lambda x: {"input": (x["premise"], x["hypothesis"])})
elif task_name == "boolq":
dataset = hf_dataset.map(lambda x: {"input": (x["passage"], x["question"]+"?" if x["question"][-1] != "?" else x["question"])})
elif task_name == "copa":
dataset = DatasetDict()
for split in hf_dataset.keys():
examples = []
for example in hf_dataset[split]:
premise = example["premise"]
choice1 = example["choice1"]
choice2 = example["choice2"]
question = example["question"]
if question == "cause":
examples.append({
"input": [choice1, premise],
"label": int(example["label"] == 0) if example["label"] is not None else None
})
examples.append({
"input": [choice2, premise],
"label": int(example["label"] == 1) if example["label"] is not None else None
})
else:
examples.append({
"input": [premise, choice1],
"label": int(example["label"] == 0) if example["label"] is not None else None
})
examples.append({
"input": [premise, choice2],
"label": int(example["label"] == 1) if example["label"] is not None else None
})
dataset[split] = Dataset.from_list(examples)
elif task_name == "cb":
dataset = hf_dataset.map(lambda x: {"input": (x["premise"], x["hypothesis"])})
elif task_name == "wic":
dataset = hf_dataset.map(lambda x: {"input": (f"{x['word']} : {x['sentence1']}", x['sentence2'])})
elif task_name == "wsc":
def wsc_formatter(example):
text = example['text']
span1_index = example['span1_index']
span2_index = example['span2_index']
span1_text = example['span1_text']
span2_text = example['span2_text']
words = text.split()
span1_word_len = len(span1_text.split())
span2_word_len = len(span2_text.split())
words[span1_index:span1_index + span1_word_len] = [f"# {span1_text} #"]
if span1_word_len > 1:
# If the span is more than one word, we need to adjust the index of the second span since the first more than one word has been replaced by a single word
span2_index -= span1_word_len - 1
words[span2_index:span2_index + span2_word_len] = [f"* {span2_text} *"]
return " ".join(words)
dataset = hf_dataset.map(lambda x: {"input": wsc_formatter(x)})
return dataset
def setup(self, stage='fit', randomize=False, seed=None):
self.dataset = self.load_dataset(self.task_name)
for split in self.dataset.keys():
if split == "test":
continue
self.dataset[split] = self.dataset[split].map(
self.convert_to_features,
batched=True,
remove_columns=["label"],
load_from_cache_file=True
)
self.columns = [c for c in self.dataset[split].column_names if c in self.loader_columns]
self.dataset[split].set_format(type="torch", columns=self.columns)
if len(self.dataset["train"]) < self.sample_size:
self.sample_size = len(self.dataset["train"])
self.eval_splits = ["validation"]
if len(self.dataset[self.eval_splits[0]]) < self.validation_sample_size:
self.validation_sample_size = len(self.dataset[self.eval_splits[0]])
self.subset_indices = list(range(self.sample_size))
self.subset_indices_val = list(range(self.validation_sample_size))
self.subset_train_dataset = Subset(self.dataset["train"], self.subset_indices)
self.subset_val_dataset = Subset(self.dataset["validation"], self.subset_indices_val)
def prepare_data(self):
datasets.load_dataset("super_glue", self.task_name)
AutoTokenizer.from_pretrained(self.model_name_or_path, use_fast=True)
def train_dataloader(self):
return DataLoader(self.subset_train_dataset, batch_size=self.train_batch_size, shuffle=True)
def train_full_dataloader(self):
return DataLoader(self.subset_train_dataset, batch_size=self.sample_size, shuffle=True)
def val_dataloader(self):
return DataLoader(self.subset_val_dataset, batch_size=self.eval_batch_size)
def test_dataloader(self):
return DataLoader(self.dataset["test"], batch_size=self.eval_batch_size)
def convert_to_features(self, example_batch, indices=None):
if self.tokenizer.pad_token_id is None:
# GPT2 and OPT
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
features = self.tokenizer.batch_encode_plus(
example_batch["input"], max_length=self.max_seq_length, pad_to_max_length=True, truncation=True
)
features["labels"] = example_batch["label"]
return features