Views
No views yet
google/gemma-4-E2B, fine-tuned for binary sarcasm detection on the en-AU portion of surrey-nlp/BESSTIE-CW-26.en-AU examples, rather than examples from the other BESSTIE varieties. This makes it useful for cross-variety transfer experiments: for example, evaluating an en-AU adapter on en-IN and en-UK test examples.google/gemma-4-E2BYes = sarcastic, No = not sarcasticen-AU100surrey-nlp/BESSTIE-CW-26surrey-nlp/BESSTIE-CW-26, a benchmark for sentiment and sarcasm classification across English varieties. The dataset includes text, variety, source, sentiment, and sarcasm fields.en-AU subset was used for training and validation.1Detect sarcasm in the following English user-generated text.
2Return Yes if sarcastic and No if not sarcastic.
3
4Text: <text>
5
6Label: No Yes| Setting | Value |
|---|---|
| Base model | google/gemma-4-E2B |
| Dataset | surrey-nlp/BESSTIE-CW-26 |
| Training variety | en-AU |
| Seed used for release | 100 |
| Epochs | 4 |
| Learning rate | 2e-4 |
| Per-device batch size | 4 |
| Gradient accumulation | 4 |
| Warmup ratio | 0.05 |
| Weight decay | 0.01 |
| Scheduler | cosine |
| Max sequence length | 512 |
| LoRA rank | 8 |
| LoRA alpha | 16 |
| LoRA dropout | 0.10 |
| Quantisation | 4-bit NF4 with double quantisation |
| Compute dtype | bfloat16 |
| Optimised metric during training | validation loss |
| Checkpoint selection | best checkpoint by validation loss |
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
3from peft import PeftModel
4
5base_model = "google/gemma-4-E2B"
6adapter_id = "omargrist/gemma-4-e2b-besstie-sarcasm-en-au"
7
8tokenizer = AutoTokenizer.from_pretrained(adapter_id, use_fast=True)
9
10if tokenizer.pad_token is None:
11 tokenizer.pad_token = tokenizer.eos_token
12
13quant_config = BitsAndBytesConfig(
14 load_in_4bit=True,
15 bnb_4bit_quant_type="nf4",
16 bnb_4bit_use_double_quant=True,
17 bnb_4bit_compute_dtype=torch.bfloat16,
18)
19
20model = AutoModelForCausalLM.from_pretrained(
21 base_model,
22 quantization_config=quant_config,
23 dtype=torch.bfloat16,
24 device_map="auto",
25)
26
27model = PeftModel.from_pretrained(model, adapter_id)
28model.eval()1import torch
2
3NEGATIVE_LABEL = " No"
4POSITIVE_LABEL = " Yes"
5
6def make_prompt(text):
7 return (
8 "Detect sarcasm in the following English user-generated text.\n"
9 "Return Yes if sarcastic and No if not sarcastic.\n\n"
10 f"Text: {text}\n\n"
11 "Label:"
12 )
13
14def label_token_ids(tokenizer):
15 no_ids = tokenizer(NEGATIVE_LABEL, add_special_tokens=False)["input_ids"]
16 yes_ids = tokenizer(POSITIVE_LABEL, add_special_tokens=False)["input_ids"]
17
18 if len(no_ids) != 1 or len(yes_ids) != 1:
19 raise ValueError("Expected single-token labels for No/Yes.")
20
21 return no_ids[0], yes_ids[0]
22
23@torch.inference_mode()
24def predict_sarcasm_prob(model, tokenizer, text):
25 id_no, id_yes = label_token_ids(tokenizer)
26 prompt = make_prompt(text)
27
28 batch = tokenizer(
29 prompt,
30 return_tensors="pt",
31 truncation=True,
32 max_length=512,
33 add_special_tokens=False,
34 ).to(model.device)
35
36 outputs = model(**batch, logits_to_keep=1)
37 logits = outputs.logits[:, -1, :]
38 label_logits = logits[:, [id_no, id_yes]]
39
40 return torch.softmax(label_logits.float(), dim=-1)[0, 1].item()
41
42text = "Oh brilliant, another meeting that could have been an email."
43prob_sarcastic = predict_sarcasm_prob(model, tokenizer, text)
44
45print({"prob_sarcastic": prob_sarcastic})en-AU, en-IN, and en-UK.| Adapter | Test subset | Macro-F1 |
|---|---|---|
en-AU | en-AU | 0.767 |
en-AU | en-IN | 0.495 |
en-AU | en-UK | 0.602 |
1@misc{besstie2024,
2 title={BESSTIE: A Benchmark for Sentiment and Sarcasm Classification for Varieties of English},
3 year={2024},
4 eprint={2412.04726},
5 archivePrefix={arXiv}
6}