Spaces:
Build error
Build error
File size: 21,839 Bytes
861c889 dd434b4 861c889 dd434b4 861c889 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
import csv
from dataclasses import dataclass
import io
import json
import logging
import random
import sys
from typing import Dict, List
import pandas as pd
import streamlit as st
import torch
import transformers
from tqdm import tqdm
from autoprompt import utils
import autoprompt.create_trigger as ct
# logging.getLogger("streamlit.caching").addHandler(logging.StreamHandler(sys.stdout))
# logging.getLogger("streamlit.caching").setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
with open('assets/sst2_train.jsonl', 'r') as f:
DEFAULT_TRAIN = [json.loads(line) for line in f]
@dataclass
class CacheTest:
"""
Stores whether the train button has been pressed for a given
set of inputs to run_autoprompt.
"""
is_test: bool
class CacheMiss(Exception):
pass
def css_hack():
"""
Inject some style into this app. ヽ(⌐■_■)ノ
"""
st.markdown(
"""
<style>
code {
color: #eec66d;
}
.css-gtmd9c a {
color: #6f98af;
}
</style>
""",
unsafe_allow_html=True
)
# Setting eq and frozen ensures that a __hash__ method is generated which is needed for caching to
# properly respond to changed args.
@dataclass(eq=True, frozen=True)
class Args:
# Configurable
template: str
model_name: str
iters: int
num_cand: int
accumulation_steps: int
# Non-Configurable
seed = 0
sentence_size = 64
tokenize_labels = True
filter = False
initial_trigger = None
label_field = "label"
bsz = 32
eval_size = 1
@classmethod
def from_streamlit(cls):
st.sidebar.image('assets/icon.png', width=150)
st.sidebar.markdown('### Training Parameters')
model_name = st.sidebar.selectbox(
"Model",
options=['roberta-large', 'bert-base-cased'],
help="Language model used for training and evaluation."
)
iters = int(st.sidebar.number_input(
"Iterations",
value=10,
min_value=1,
max_value=100,
help="Number of trigger search iterations. Larger values may yield better results."
))
num_cand = int(st.sidebar.number_input(
"Number of Candidates",
value=25,
min_value=1,
max_value=100,
help="Number of candidate trigger token replacements to evaluate during each search "
"iteration. Larger values may yield better results."
))
accumulation_steps = int(st.sidebar.number_input(
"Gradient Accumulation Steps",
value=1,
min_value=1,
max_value=10,
help="Number of gradient accumulation steps used during training. Larger values may yield "
"better results. Cannot be larger than half the dataset size."
))
st.sidebar.markdown(
"""
### Template
Templates define how task-specific inputs are combined with trigger tokens to create
the prompt. They should contain the following placeholders:
- `{sentence}`: Placeholders for the task-specific input fields contain the field name
between curly brackets. For manually entered data the field name is `{sentence}`. For
uploaded csv's, field names should correspond to columns in the csv.
- `[T]`: Placeholder for a trigger token. These are learned from the training data.
- `[P]`: Placeholder for where to insert the [MASK] token that the model will predict
on.
Templates can also include manually written text (such as the
period in the default example below).
"""
)
template = st.sidebar.text_input("Template", "{sentence} [T] [T] [T] [P].")
return cls(
template=template,
model_name=model_name,
iters=iters,
num_cand=num_cand,
accumulation_steps=accumulation_steps,
)
# TODO(rloganiv): This probably could use a better name...
@dataclass
class GlobalData:
device: torch.device
config: transformers.PretrainedConfig
model: transformers.PreTrainedModel
tokenizer: transformers.PreTrainedTokenizer
embeddings: torch.nn.Module
embedding_gradient: ct.GradientStorage
predictor: ct.PredictWrapper
@classmethod
@st.cache(allow_output_mutation=True)
def from_pretrained(cls, model_name):
logger.info(f'Loading pretrained model: {model_name}')
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
if torch.cuda.is_available():
st.write('CUDA is available')
else:
st.write('CUDA not available')
config, model, tokenizer = ct.load_pretrained(model_name)
model.to(device)
embeddings = ct.get_embeddings(model, config)
embedding_gradient = ct.GradientStorage(embeddings)
predictor = ct.PredictWrapper(model)
return cls(
device,
config,
model,
tokenizer,
embeddings,
embedding_gradient,
predictor
)
@dataclass
class Dataset:
train: List[int]
label_map: Dict[str, str]
def load_trigger_dataset(dataset, templatizer):
instances = []
for x in dataset:
instances.append(templatizer(x))
return instances
@st.cache(suppress_st_warning=True, allow_output_mutation=True, hash_funcs={CacheTest: lambda o: 0})
def run_autoprompt(args, dataset, cache_test):
if cache_test.is_test:
raise CacheMiss()
ct.set_seed(args.seed)
global_data = GlobalData.from_pretrained(args.model_name)
templatizer = utils.TriggerTemplatizer(
args.template,
global_data.config,
global_data.tokenizer,
label_field=args.label_field,
label_map=dataset.label_map,
tokenize_labels=args.tokenize_labels,
add_special_tokens=True,
)
evaluation_fn = ct.AccuracyFn(global_data.tokenizer, dataset.label_map, global_data.device,
tokenize_labels=args.tokenize_labels)
# Do not allow for initial trigger specification.
trigger_ids = [global_data.tokenizer.mask_token_id] * templatizer.num_trigger_tokens
trigger_ids = torch.tensor(trigger_ids, device=global_data.device).unsqueeze(0)
best_trigger_ids = trigger_ids.clone()
# Load datasets
logger.info('Loading datasets')
collator = utils.Collator(pad_token_id=global_data.tokenizer.pad_token_id)
try:
train_dataset = load_trigger_dataset(dataset.train, templatizer)
except KeyError as e:
raise RuntimeError(
'A field in your template is not present in the uploaded dataset. '
f'Check that there is a column with the name: {e}'
)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.bsz, shuffle=True, collate_fn=collator)
progress = st.progress(0.0)
trigger_placeholder = st.empty()
best_dev_metric = -float('inf')
for i in range(args.iters):
logger.info(f'Iteration: {i}')
progress.progress(float(i)/args.iters)
current_trigger = ','.join(global_data.tokenizer.convert_ids_to_tokens(best_trigger_ids.squeeze(0)))
trigger_placeholder.markdown(f'**Current trigger**: {current_trigger}')
global_data.model.zero_grad()
train_iter = iter(train_loader)
averaged_grad = None
# Compute gradient of loss
for step in range(args.accumulation_steps):
try:
model_inputs, labels = next(train_iter)
except:
logger.warning(
'Insufficient data for number of accumulation steps. '
'Effective batch size will be smaller than specified.'
)
break
model_inputs = {k: v.to(global_data.device) for k, v in model_inputs.items()}
labels = labels.to(global_data.device)
predict_logits = global_data.predictor(model_inputs, trigger_ids)
loss = ct.get_loss(predict_logits, labels).mean()
loss.backward()
grad = global_data.embedding_gradient.get()
bsz, _, emb_dim = grad.size()
selection_mask = model_inputs['trigger_mask'].unsqueeze(-1)
grad = torch.masked_select(grad, selection_mask)
grad = grad.view(bsz, templatizer.num_trigger_tokens, emb_dim)
if averaged_grad is None:
averaged_grad = grad.sum(dim=0) / args.accumulation_steps
else:
averaged_grad += grad.sum(dim=0) / args.accumulation_steps
logger.info('Evaluating Candidates')
pbar = tqdm(range(args.accumulation_steps))
train_iter = iter(train_loader)
token_to_flip = i % templatizer.num_trigger_tokens
candidates = ct.hotflip_attack(averaged_grad[token_to_flip],
global_data.embeddings.weight,
increase_loss=False,
num_candidates=args.num_cand)
current_score = 0
candidate_scores = torch.zeros(args.num_cand, device=global_data.device)
denom = 0
for step in pbar:
try:
model_inputs, labels = next(train_iter)
except:
logger.warning(
'Insufficient data for number of accumulation steps. '
'Effective batch size will be smaller than specified.'
)
break
model_inputs = {k: v.to(global_data.device) for k, v in model_inputs.items()}
labels = labels.to(global_data.device)
with torch.no_grad():
predict_logits = global_data.predictor(model_inputs, trigger_ids)
eval_metric = evaluation_fn(predict_logits, labels)
# Update current score
current_score += eval_metric.sum()
denom += labels.size(0)
# NOTE: Instead of iterating over tokens to flip we randomly change just one each
# time so the gradients don't get stale.
for i, candidate in enumerate(candidates):
# if candidate.item() in filter_candidates:
# candidate_scores[i] = -1e32
# continue
temp_trigger = trigger_ids.clone()
temp_trigger[:, token_to_flip] = candidate
with torch.no_grad():
predict_logits = global_data.predictor(model_inputs, temp_trigger)
eval_metric = evaluation_fn(predict_logits, labels)
candidate_scores[i] += eval_metric.sum()
if (candidate_scores >= current_score).any():
logger.info('Better trigger detected.')
best_candidate_score = candidate_scores.max()
best_candidate_idx = candidate_scores.argmax()
trigger_ids[:, token_to_flip] = candidates[best_candidate_idx]
logger.info(f'Train metric: {best_candidate_score / (denom + 1e-13): 0.4f}')
# Skip eval
best_trigger_ids = trigger_ids.clone()
progress.progress(1.0)
current_trigger = ','.join(global_data.tokenizer.convert_ids_to_tokens(best_trigger_ids.squeeze(0)))
trigger_placeholder.markdown(f'**Current trigger**: {current_trigger}')
best_trigger_tokens = global_data.tokenizer.convert_ids_to_tokens(best_trigger_ids.squeeze(0))
train_output = predict_test(map(lambda x: x['sentence'], dataset.train), dataset.label_map,
templatizer, best_trigger_ids, global_data.tokenizer, global_data.predictor, args)
# Streamlit does not like accessing widgets across functions, which is
# problematic for this "live updating" widget which we want to still
# display even if the train output is cached. To get around this, we're
# going to delete the widget and replace it with a very similar looking
# widget outside the function...no one will ever notice ;)
trigger_placeholder.empty()
return (
best_trigger_tokens,
current_score/denom,
dataset.label_map,
templatizer,
best_trigger_ids,
global_data.tokenizer,
global_data.predictor,
args,
train_output
)
def predict_test(sentences, label_map, templatizer, best_trigger_ids, tokenizer, predictor, args):
# Evaluate clean
output = { 'sentences': [] }
any_label = None
for label in label_map.values():
output[label] = []
any_label = label
output['prompt'] = []
for sentence in sentences:
model_inputs, _ = templatizer({'sentence': sentence, 'label': any_label})
model_inputs = {k: v.to(best_trigger_ids.device) for k, v in model_inputs.items()}
prompt_ids = ct.replace_trigger_tokens(
model_inputs, best_trigger_ids, model_inputs['trigger_mask'])
prompt = ' '.join(tokenizer.convert_ids_to_tokens(prompt_ids['input_ids'][0]))
output['prompt'].append(prompt)
predict_logits = predictor(model_inputs, best_trigger_ids)
output['sentences'].append(sentence)
for label in label_map.values():
label_id = utils.encode_label(tokenizer=tokenizer, label=label, tokenize=args.tokenize_labels)
label_id = label_id.to(best_trigger_ids.device)
label_loss = ct.get_loss(predict_logits, label_id)
# st.write(sentence, label, label_loss)
output[label].append(label_loss.item())
return output
def manual_dataset(use_defaults):
num_train_instances = st.slider("Number of Train Instances", 4, 32, 8)
any_empty = False
dataset = []
data_col, label_col = st.beta_columns([3,1])
for i in range(num_train_instances):
default_data = DEFAULT_TRAIN[i]['sentence'] if use_defaults else ''
default_label = DEFAULT_TRAIN[i]['label'] if use_defaults else ''
with data_col:
data = st.text_input("Train Instance " + str(i+1), default_data)
with label_col:
label = st.text_input("Train Label " + str(i+1), default_label, max_chars=20)
if data == "" or label == "":
any_empty = True
dataset.append({'sentence': data, 'label': label})
label_set = list(set(map(lambda x: x['label'], dataset)))
label_idx = {x: i for i, x in enumerate(label_set)}
label_map = dict(map(lambda x: (x, x), label_set))
if any_empty:
st.warning('Waiting for data to be added')
st.stop()
if len(label_set) < 2:
st.warning('Not enough labels')
st.stop()
return Dataset(
train=dataset,
label_map=label_map
)
def csv_dataset():
st.markdown("""
Please upload your training and evaluation csv files.
Format restrictions:
- The file is required to have a header
- The column name of the output field should be `label`.
- Each file should contain no more than 64 rows.
""")
train_csv = st.file_uploader('Train', accept_multiple_files=False)
if train_csv is None:
st.stop()
with io.StringIO(train_csv.getvalue().decode('utf-8')) as f:
reader = csv.DictReader(f)
train_dataset = list(reader)
if len(train_dataset) > 64:
raise ValueError('Train dataset is too large. Please limit the number '
'of examples to 64 or less.')
labels = set(x['label'] for x in train_dataset)
label_map = {x: x for x in labels}
return Dataset(
train=train_dataset,
label_map=label_map
)
def run():
css_hack()
st.title('AutoPrompt Demo')
st.markdown('''
For many years, the predominant approach for training machine learning
models to solve NLP tasks has been to use supervised training data to
estimate model parameters using maximum likelihood estimation or some
similar paradigm. Whether fitting a logistic regression model over a
bag-of-words, an LSTM over a sequence of GloVe embeddings, or finetuning a
language model such as ELMo or BERT, the approach is essentially the same.
However, as language models have become more and more capable of accurately
generating plausible text a new possibility for solving classification
tasks has emerged...
## Prompting
Prompting is the method of converting classification tasks into
*fill-in-the-blanks* problems that can be solved by a language model **without
modifying the model's internals**. For example, to perform sentiment analysis,
we may take the sentence we wish to classify and append the text "Overall, this
movie was ____." and feed it into a language model like so:
''')
st.image('assets/bert-mouth.png', use_column_width=True)
st.markdown('''
By measuring whether the language model assigns a higher probability to
words that are associated with a **positive** sentiment ("good", "great",
and "fantastic") vs. words that are associated with a **negative**
sentiment ("bad", "terrible", or "awful") we can infer the
predicted label for the given input. So in this example, because the word "good"
has a higher probability than "bad", the predicted label is **positive**.
## AutoPrompt
One issue that arises when using prompts is that it is not usually clear
how to best pose a task as a fill-in-the-blanks problem in a way that gets
the most performance from the language model. Even for a simple problem
like sentiment analysis, we don't know whether it is better to ask whether
a movie is good/bad, or whether you feel great/terrible about it, and for
more abstract problems like natural language inference it is difficult to
even know where to start.
To cure this writer's block we introduce **AutoPrompt**, a data-driven
approach for automatic prompt construction. The basic idea is
straightfoward: instead of writing a prompt, a user need only write a
**template** that specfies where the *task inputs* go along with placeholders for
a number of *trigger tokens* that will automatically be learned by the
model and the *predict token* that the model will fill in:
''')
st.image('assets/template.png', use_column_width=True)
st.markdown(
'''
In each iteration of the search process:
1. The template is instantiated using a batch of training inputs.
2. The loss of the model on each input is measured and used to identify a
number of candidate replacements for the current trigger tokens.
3. The performance of each candidate is measured on another batch of
training data, and the best performing candidate is used in the next
iteration.
### Demo
To give a better sense of how AutoPrompt works, we have provided a simple
interactive demo. You can generate a prompt using the training data we have
pre-populated for you, or alternatively write your own training/evaluation
instances or upload them using a csv below. In addition, you can vary
some of the training parameters, as well as the template using the sidebar
on the left.
'''
)
args = Args.from_streamlit()
dataset_mode = st.radio('How would you like to input your training data?',
options=['Example Data', 'Manual Input', 'From CSV'])
if dataset_mode == 'Example Data':
dataset = manual_dataset(use_defaults=True)
elif dataset_mode == 'Manual Input':
dataset = manual_dataset(use_defaults=False)
else:
dataset = csv_dataset()
button = st.empty()
clicked = button.button('Train')
if clicked:
trigger_tokens, eval_metric, label_map, templatizer, best_trigger_ids, tokenizer, predictor, args, train_output = run_autoprompt(args, dataset, cache_test=CacheTest(False))
else:
try:
trigger_tokens, eval_metric, label_map, templatizer, best_trigger_ids, tokenizer, predictor, args, train_output = run_autoprompt(args, dataset, cache_test=CacheTest(True))
except CacheMiss:
st.stop()
else:
button.empty()
st.markdown(f'**Final trigger**: {", ".join(trigger_tokens)}')
st.dataframe(pd.DataFrame(train_output).style.highlight_min(axis=1, color='#94666b'))
logger.debug('Dev metric')
st.write('Accuracy: ' + str(round(eval_metric.item()*100, 1)))
st.write("""
Et voila, you've now effectively finetuned a classifier using just a few
kilobytes of parameters (the tokens in the prompt). If you like you can
write down your "model" on the back of a napkin and take it with you.
### Try it out yourself!
""")
sentence = st.text_input("Sentence", 'Enter a test input here')
pred_output = predict_test([sentence], label_map ,templatizer, best_trigger_ids, tokenizer, predictor, args)
st.dataframe(pd.DataFrame(pred_output).style.highlight_min(axis=1, color='#94666b'))
st.markdown('''
## Where can I learn more?
If you are interested in learning more about AutoPrompt we recommend
[reading our paper](https://arxiv.org/abs/2010.15980) and [checking out our
code](https://github.com/ucinlp/autoprompt), or if you'd like you can also
watch our presentation at EMNLP 2020:
''')
st.components.v1.iframe(
src="https://www.youtube.com/embed/IBMT_oOCBbc",
height=400,
)
st.markdown('Thanks!')
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
stream=sys.stdout)
run()
|