Text Classification
Transformers
Safetensors
vulfixminer
feature-extraction
code
codebert
security
vulnerability-fix-detection
commit-classification
custom_code
Instructions to use andstor/VulFixMiner with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use andstor/VulFixMiner with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="andstor/VulFixMiner", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("andstor/VulFixMiner", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download tests/test_model.py from andstor/VulFixMiner: direct link, hf CLI and curl.
- Browser
- Download file 6.52 kB
-
https://huggingface.co/andstor/VulFixMiner/resolve/main/tests/test_model.py
- Command line
-
hf download hf://andstor/VulFixMiner/tests/test_model.py
-
curl -L -o test_model.py https://huggingface.co/andstor/VulFixMiner/resolve/main/tests/test_model.py
6.52 kB
| import unittest | |
| import torch | |
| from transformers import RobertaConfig | |
| from modeling_vulfixminer import ( | |
| EmptyCommitError, | |
| VulFixMinerConfig, | |
| VulFixMinerModel, | |
| extract_file_diffs, | |
| get_code_version, | |
| prepare_commit, | |
| ) | |
| from tests.fixtures import DIFF | |
| class PreprocessingTests(unittest.TestCase): | |
| def test_file_order_and_header_exclusion(self): | |
| files = extract_file_diffs(DIFF) | |
| self.assertEqual(list(files), ["a.c", "b.c"]) | |
| self.assertTrue(all(value.startswith("@@") for value in files.values())) | |
| self.assertEqual( | |
| prepare_commit(DIFF, "</s>"), | |
| [ | |
| "fixed();\n</s>old();\n", | |
| "safe();\n</s>unsafe();\n", | |
| ], | |
| ) | |
| def test_comment_prefixes_and_blank_line_baseline_quirk(self): | |
| self.assertEqual(get_code_version("+//comment\n+#define X\n+ \n+code", True), "\ncode\n") | |
| def test_empty_binary_and_comment_only(self): | |
| for diff in ( | |
| "", | |
| "ordinary text", | |
| "diff --git a/a b/a\nBinary files differ", | |
| "diff --git a/a b/a\n@@ -1 +1 @@\n-// old\n+// new", | |
| ): | |
| with self.subTest(diff=diff), self.assertRaises(EmptyCommitError): | |
| prepare_commit(diff, "</s>") | |
| def test_multihunk_and_one_sided_change(self): | |
| diff = "diff --git a/a b/a\n@@ -0,0 +1 @@\n+one\n@@ -2 +3 @@\n+two" | |
| self.assertEqual(prepare_commit(diff, "</s>"), ["one\ntwo\n</s>"]) | |
| def test_invalid_input_type(self): | |
| with self.assertRaises(TypeError): | |
| prepare_commit({}, "</s>") | |
| with self.assertRaises(ValueError): | |
| prepare_commit(DIFF, "") | |
| class ConfigTests(unittest.TestCase): | |
| def test_invalid_lengths_and_dropout(self): | |
| for length in (0, 1, True, 1.5, 513): | |
| with self.subTest(length=length), self.assertRaises(ValueError): | |
| VulFixMinerConfig(max_file_length=length) | |
| for dropout in (-0.1, 1.1, float("nan"), float("inf"), True, "0.1"): | |
| with self.subTest(dropout=dropout), self.assertRaises(ValueError): | |
| VulFixMinerConfig(hidden_dropout_prob=dropout) | |
| def test_encoder_config_is_copied(self): | |
| original = RobertaConfig().to_dict() | |
| config = VulFixMinerConfig(encoder_config=original) | |
| original["hidden_size"] = 1 | |
| self.assertEqual(config.hidden_size, 768) | |
| class ModelTests(unittest.TestCase): | |
| def setUpClass(cls): | |
| torch.manual_seed(7) | |
| config = VulFixMinerConfig( | |
| encoder_config=RobertaConfig( | |
| vocab_size=32, | |
| hidden_size=16, | |
| num_hidden_layers=1, | |
| num_attention_heads=2, | |
| intermediate_size=32, | |
| max_position_embeddings=32, | |
| type_vocab_size=1, | |
| ), | |
| max_file_length=16, | |
| ) | |
| cls.model = VulFixMinerModel(config).eval() | |
| cls.inputs = torch.randint(3, 32, (3, 8)) | |
| cls.mask = torch.ones_like(cls.inputs) | |
| def test_single_commit_matches_reference_reduction(self): | |
| with torch.inference_mode(): | |
| embeddings = self.model.code_bert(self.inputs, self.mask).last_hidden_state[:, 0, :] | |
| expected = self.model.out_proj(self.model.linear(embeddings.mean(dim=0, keepdim=True))) | |
| actual = self.model(self.inputs, self.mask).logits | |
| torch.testing.assert_close(actual, expected, rtol=0, atol=0) | |
| def test_multiple_commits_match_independent_predictions(self): | |
| with torch.inference_mode(): | |
| grouped = self.model(self.inputs, self.mask, commit_ids=torch.tensor([0, 0, 1])).logits | |
| separate = torch.cat( | |
| [ | |
| self.model(self.inputs[:2], self.mask[:2]).logits, | |
| self.model(self.inputs[2:], self.mask[2:]).logits, | |
| ] | |
| ) | |
| torch.testing.assert_close(grouped, separate) | |
| def test_chunked_file_encoding(self): | |
| with torch.inference_mode(): | |
| full = self.model(self.inputs, self.mask).logits | |
| chunked = self.model(self.inputs, self.mask, file_batch_size=1).logits | |
| torch.testing.assert_close(full, chunked) | |
| def test_invalid_grouping(self): | |
| for ids in ([1, 1, 1], [0, 0, 2], [-1, 0, 0], [0], [0.0, 0.0, 0.0]): | |
| with self.subTest(ids=ids), self.assertRaises(ValueError): | |
| self.model(self.inputs, self.mask, commit_ids=ids) | |
| def test_empty_input_and_invalid_chunk_size(self): | |
| with self.assertRaises(ValueError): | |
| self.model(self.inputs[:0]) | |
| with self.assertRaises(ValueError): | |
| self.model(self.inputs, file_batch_size=0) | |
| def test_invalid_token_shapes_and_types(self): | |
| for inputs in ( | |
| None, | |
| [], | |
| self.inputs[0], | |
| self.inputs[:, :0], | |
| self.inputs.float(), | |
| torch.zeros(1, 17, dtype=torch.long), | |
| ): | |
| with self.subTest(inputs=inputs), self.assertRaises(ValueError): | |
| self.model(inputs) | |
| with self.assertRaises(ValueError): | |
| self.model(self.inputs, attention_mask=self.mask[:1]) | |
| with self.assertRaises(ValueError): | |
| self.model(self.inputs, token_type_ids=self.inputs[:1]) | |
| def test_invalid_labels_and_unsupported_parameters(self): | |
| for labels in ( | |
| torch.tensor([2]), | |
| torch.tensor([-1]), | |
| torch.tensor([1.0]), | |
| torch.tensor([0, 1]), | |
| ): | |
| with self.subTest(labels=labels), self.assertRaises(ValueError): | |
| self.model(self.inputs, labels=labels) | |
| with self.assertRaises(TypeError): | |
| self.model(self.inputs, unsupported=True) | |
| def test_token_type_ids_are_forwarded(self): | |
| with torch.inference_mode(): | |
| expected = self.model(self.inputs, self.mask).logits | |
| actual = self.model( | |
| self.inputs, self.mask, token_type_ids=torch.zeros_like(self.inputs) | |
| ).logits | |
| torch.testing.assert_close(actual, expected, rtol=0, atol=0) | |
| def test_loss_and_tuple_output(self): | |
| with torch.inference_mode(): | |
| output = self.model(self.inputs, labels=torch.tensor([1])) | |
| result = self.model(self.inputs, return_dict=False) | |
| self.assertEqual(output.loss.ndim, 0) | |
| self.assertEqual(result[0].shape, (1, 2)) | |
| if __name__ == "__main__": | |
| unittest.main() | |