TL;DR
This paper introduces NLPLego, an automated test generation framework for NLP applications that leverages sentence parsing principles to create diverse and semantically related test cases. By disassembling seed sentences into templates and adjuncts, NLPLego generates new tests that facilitate metamorphic testing, effectively uncovering errors in state-of-the-art NLP models with high precision.
What Problem Does This Paper Address?
Modern NLP applications, powered by deep neural networks, have achieved impressive performance. However, like all software, they are susceptible to defects that can lead to significant consequences. Traditional evaluation methods, relying on fixed train-validation-test splits, often fail to capture real-world performance due to limited test coverage and the difficulty of creating comprehensive test oracles. This gap is particularly pronounced for complex NLP tasks like Natural Language Generation (NLG), where the vast output space makes manual oracle construction challenging. Existing task-specific testing methods lack generalizability, and while tools like CheckList offer some automation, they still require significant manual effort. This paper addresses the need for a general, automated, and effective test generation method for NLP applications that can produce diverse test cases and facilitate robust evaluation without relying on extensive manual annotation.
Methods
NLPLego's core innovation lies in its "assembling test generation" method, inspired by linguistic sentence parsing. The process involves:
- Sentence Disassembling: A seed sentence is broken down into a "derivation template" (basic sentence structure with slots) and a set of "adjuncts" (modifying phrases or clauses). This is achieved using dependency and constituency parsing, along with a sentence compression model.
- Grammatical Elements Assembling: The derivation template is then reassembled with adjuncts. To enhance diversity, an "adjunct mutation operator" is employed. This operator can substitute words within adjuncts with synonyms (preserving semantics) or use a masked language model (MLM) to find contextually appropriate replacements (potentially altering semantics). This process generates a "derivation tree" where each path represents a set of semantically related sentences with varying degrees of modification.
- Oracle Information Generation: The derivation trees and the inherent "intergenerational relationships" between generated sentences are used to construct metamorphic relations (MRs). These MRs define expected relationships between the outputs of an NLP model for related inputs, allowing for bug detection without explicit ground truth for each test case. For example, in sentiment analysis, adding a positive phrase to a sentence should ideally increase the probability of a positive sentiment.
Datasets
The paper utilizes three widely-used datasets as seed sets for generating tests:
- SQuAD 2.0: A dataset for machine reading comprehension, containing question-answer pairs, including unanswerable questions.
- Stanford Sentiment Treebank (SST): A corpus of movie reviews with sentiment labels, suitable for analyzing compositional sentiment.
- Quora Question Pairs (QQP): A dataset of question pairs used to train models for identifying semantic equivalence.
Key Insights and Ideas
- Linguistics as a Testing Framework: The paper demonstrates the power of applying linguistic principles, specifically sentence parsing and assembly, to automate test generation for NLP. This approach naturally creates semantically related but structurally diverse test cases.
- Derivation Trees for Metamorphic Testing: The concept of derivation trees, where sentences on a path share a "derivation relation," is crucial. This relation directly supports the construction of metamorphic relations, a key technique for testing when explicit oracles are unavailable.
- Adjunct Mutation for Diversity and Authenticity: The dual approach of synonym substitution and MLM-based mutation for adjuncts allows for controlled semantic variation and improved test authenticity, making generated tests more representative of real-world inputs.
- Generalizability Across NLP Tasks: The framework's ability to adapt to different NLP tasks (MRC, SA, SSM) by leveraging task-specific metamorphic relations highlights its broad applicability.
Key Results
NLPLego demonstrated significant effectiveness in detecting errors across three NLP tasks:
- Machine Reading Comprehension (MRC): Detected 1,732 incorrect behaviors with approximately 95.7% precision.
- Sentiment Analysis (SA): Detected 5,301 incorrect behaviors with approximately 95.7% precision.
- Semantic Similarity Measures (SSM): Detected 261,879 incorrect behaviors with approximately 92.7% precision.
The generated tests showed high authenticity, with 94.3% to 97.0% of sentences being grammatically and semantically correct. NLPLego also proved efficient, generating a large number of tests from a small seed set (e.g., 6.5-71.6 new tests per seed sentence). Compared to the baseline tool CheckList, NLPLego reported significantly more suspected bugs with comparable or higher precision.
Limitations and Future Work
- Threats to Validity: The authors acknowledge that the accuracy of the underlying NLP tools used for parsing and sentence compression (spaCy, Stanford CoreNLP, SLAHAN) can impact the authenticity of generated tests. While the generated tests are highly authentic (94.3%-97%), they are not perfect.
- False Positives: A common source of false positives is the presence of grammatical or semantic errors in the generated tests themselves, or when the metamorphic relation is not perfectly captured by the NLP model's output. For SA, issues arise when negation is introduced in a way that doesn't clearly reverse sentiment, or when both the model's prediction and the inserted adjunct are erroneous. For SSM, cases where semantics don't change significantly after adjunct insertion can lead to false positives.
- Future Work: The paper suggests that the core idea of interchangeability between concretization and abstraction could be applied to automated testing of other software types. The authors also highlight that NLP applications still struggle with nuanced language understanding, distinguishing between similar-sounding words in different contexts, and understanding logical relations, indicating areas for further model improvement.
Summary updated