CodeBERT flaky-test classifier, FlakeBench fold 2 (59.12% macro-F1, single-fold reproduction)
Browse filesFiles changed (9)
- README.mdAdded+126
- config.jsonAdded+49
- merges.txtAdded
- model.safetensorsAdded+3
- modeling_flakylens.pyAdded+41
- special_tokens_map.jsonAdded+51
- tokenizer.jsonAdded
- tokenizer_config.jsonAdded+57
- vocab.jsonAdded
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126---license: mitlanguage:- codebase_model: microsoft/codebert-basepipeline_tag: text-classificationtags:- flaky-tests- software-testing- code- codebert- reproduction---
# CodeBERT for Flaky Test Categorisation (FlakeBench, fold 2)
Classifies a Java/Kotlin test method into one of six categories: five kinds of flakytest plus non-flaky.
**This is a partial, single-fold reproduction of published work — not the authors' modeland not a match for their reported results.** Please read the limitations before using it.
## What this is
A fine-tune of `microsoft/codebert-base` on the FlakeBench dataset from[*Understanding and Improving Flaky Test Classification*](https://utexas.app.box.com/v/august-shi-OOPSLA2025)(OOPSLA 2025), trained as a reproduction exercise on a single 8 GB consumer GPU.
Trained on **one** of the paper's four project-disjoint folds (project group 2), so it is**not** comparable to the paper's four-fold headline number and should not be describedas reproducing it.
## Results
Held-out test set: 2,181 tests from 25 projects unseen during training.
| Category | This model | Paper (4-fold) ||---|---|---|| Async Wait | 55.38% | 58.37% || Concurrency | 10.53% | 35.92% || Time | 57.14% | 72.73% || Unordered Collections | 54.55% | 73.63% || Test Order Dependency | **77.11%** | 64.35% || Non-flaky | 100.00% | 100.00% || **Macro F1** | **59.12%** | **65.79%** |
Macro-F1 is **6.67 points below** the paper. Order-Dependency is the one category thatexceeds it. Concurrency is weak (10.53%): 12 of 17 Concurrency tests are misclassified asAsync Wait — the two categories share `Thread`/`await` vocabulary and the model does notseparate them.
Against a faithful baseline trained under the same compute budget without rebalancing(45.59%), this model is **+13.53 points** better — bootstrap 95% CI [+4.48, +21.23],p = 0.999, 2,000 resamples.
## Training
| | ||---|---|| Base | `microsoft/codebert-base` || Head | Linear(768→512) → ReLU → Dropout(0.3) → Linear(512→6) || Loss | Focal loss, γ=2.0, balanced class weights || Optimiser | AdamW, lr 1e-5, weight decay 0.01 || Batch / max length | 8 / 512 || Precision | fp16 || Epochs | 18 (best at 6, selected on validation macro-F1) || Rebalancing | non-flaky undersampled 4,972→800; each flaky class duplicated to 160 || Hardware | 1× RTX 4060 Laptop (8 GB) |
Class rebalancing is the one deviation from the paper's method, which trains on the rawdistribution (97% non-flaky). It is what produced the gain over the baseline.
## Usage
```pythonfrom transformers import AutoTokenizer, AutoModelForSequenceClassificationimport torch
name = "Ariful1904129/codebert-flakytest-fold2"tok = AutoTokenizer.from_pretrained(name)model = AutoModelForSequenceClassification.from_pretrained(name, trust_remote_code=True).eval()
code = """@Testpublic void testConnect() throws Exception { Thread.sleep(1000); assertTrue(client.isConnected());}"""
x = tok(code, return_tensors="pt", truncation=True, max_length=512)with torch.no_grad(): pred = model(**x).logits.argmax(-1).item()print(model.config.id2label[pred])```
`trust_remote_code=True` is required — the MLP head is a custom architecture defined in`modeling_flakylens.py`.
Labels: `0` async wait, `1` concurrency, `2` time, `3` unordered collections,`4` test order dependency, `5` non-flaky.
## Limitations
- **One fold, not four.** No evidence it generalises to folds 1, 3 or 4.- **Below the published result.** 59.12% vs 65.79%.- **Concurrency is unreliable** (10.53% F1) — it mostly predicts Async Wait instead.- **Noise floor is wide.** The test fold has 2,181 tests but only 103 flaky ones, so per-category F1 carries roughly ±6 points of uncertainty. Treat small differences as meaningless.- **Non-flaky dominates.** 95.3% of the test set is non-flaky and scores 100%, so overall accuracy (97%) is not informative — macro-F1 is the metric that matters.- Java/Kotlin test methods only; inputs longer than 512 tokens are truncated.
## Citation
Please cite the original paper. This model is a third-party reproduction and is notendorsed by its authors.
```bibtex@inproceedings{flakylens2025, title = {Understanding and Improving Flaky Test Classification}, booktitle = {OOPSLA}, year = {2025}}```
Dataset and method: [UT-SE-Research/FlakyLens](https://github.com/UT-SE-Research/FlakyLens).12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849{ "architectures": [ "FlakyLensForTestClassification" ], "attention_probs_dropout_prob": 0.1, "auto_map": { "AutoConfig": "modeling_flakylens.FlakyLensConfig", "AutoModelForSequenceClassification": "modeling_flakylens.FlakyLensForTestClassification" }, "bos_token_id": 0, "classifier_dropout": null, "eos_token_id": 2, "head_dropout": 0.3, "head_hidden": 512, "hidden_act": "gelu", "hidden_dropout_prob": 0.1, "hidden_size": 768, "id2label": { "0": "async wait", "1": "concurrency", "2": "time", "3": "unordered collections", "4": "test order dependency", "5": "non-flaky" }, "initializer_range": 0.02, "intermediate_size": 3072, "label2id": { "async wait": 0, "concurrency": 1, "non-flaky": 5, "test order dependency": 4, "time": 2, "unordered collections": 3 }, "layer_norm_eps": 1e-05, "max_position_embeddings": 514, "model_type": "flakylens", "num_attention_heads": 12, "num_hidden_layers": 12, "output_past": true, "pad_token_id": 1, "position_embedding_type": "absolute", "torch_dtype": "float32", "transformers_version": "4.40.1", "type_vocab_size": 1, "use_cache": true, "vocab_size": 50265}123version https://git-lfs.github.com/spec/v1oid sha256:dd7e599447b3fe15ac3ed2c09cf6c7b51343f0cb2bcfcf591cbbb8320f9c2c33size 5001940321234567891011121314151617181920212223242526272829303132333435363738394041"""CodeBERT + MLP head for flaky-test category classification.
Mirrors BERT_Arch from the FlakyLens artifact: RoBERTa pooled output -> Linear(768,512)-> ReLU -> Dropout -> Linear(512,6). The original applies LogSoftmax to the final layer;this returns raw logits instead, following the HF convention. argmax is identical eitherway, so predictions match exactly -- apply log_softmax if you need the original's values."""import torch.nn as nnfrom transformers import RobertaConfig, RobertaModel, RobertaPreTrainedModelfrom transformers.modeling_outputs import SequenceClassifierOutput
class FlakyLensConfig(RobertaConfig): model_type = "flakylens"
def __init__(self, head_hidden=512, head_dropout=0.3, **kwargs): super().__init__(**kwargs) self.head_hidden = head_hidden self.head_dropout = head_dropout
class FlakyLensForTestClassification(RobertaPreTrainedModel): config_class = FlakyLensConfig
def __init__(self, config): super().__init__(config) self.roberta = RobertaModel(config, add_pooling_layer=True) self.fc1 = nn.Linear(config.hidden_size, config.head_hidden) self.relu = nn.ReLU() self.dropout = nn.Dropout(config.head_dropout) self.fc2 = nn.Linear(config.head_hidden, config.num_labels) self.post_init()
def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs): outputs = self.roberta(input_ids=input_ids, attention_mask=attention_mask) pooled = outputs[1] logits = self.fc2(self.dropout(self.relu(self.fc1(pooled)))) loss = None if labels is not None: loss = nn.functional.cross_entropy(logits, labels) return SequenceClassifierOutput(loss=loss, logits=logits)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051{ "bos_token": { "content": "<s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "cls_token": { "content": "<s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "eos_token": { "content": "</s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "mask_token": { "content": "<mask>", "lstrip": true, "normalized": false, "rstrip": false, "single_word": false }, "pad_token": { "content": "<pad>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "sep_token": { "content": "</s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }, "unk_token": { "content": "<unk>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657{ "add_prefix_space": false, "added_tokens_decoder": { "0": { "content": "<s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": true }, "1": { "content": "<pad>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": true }, "2": { "content": "</s>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": true }, "3": { "content": "<unk>", "lstrip": false, "normalized": true, "rstrip": false, "single_word": false, "special": true }, "50264": { "content": "<mask>", "lstrip": true, "normalized": false, "rstrip": false, "single_word": false, "special": true } }, "bos_token": "<s>", "clean_up_tokenization_spaces": true, "cls_token": "<s>", "eos_token": "</s>", "errors": "replace", "mask_token": "<mask>", "model_max_length": 512, "pad_token": "<pad>", "sep_token": "</s>", "tokenizer_class": "RobertaTokenizer", "trim_offsets": true, "unk_token": "<unk>"}README.mdAdded+126
config.jsonAdded+49
merges.txtAdded
model.safetensorsAdded+3
modeling_flakylens.pyAdded+41
special_tokens_map.jsonAdded+51
tokenizer.jsonAdded
tokenizer_config.jsonAdded+57
vocab.jsonAdded