Systematicity, Compositionality and Transitivity of Deep NLP Models: a Metamorphic Testing Perspective (2022)

Abstract: Metamorphic testing has recently been used to check the safety of neural NLP models. Its main advantage is that it does not rely on a ground truth to generate test cases. However, existing studies are mostly concerned with robustness-like metamorphic relations, limiting the scope of linguistic properties they can test. We propose three new classes of metamorphic relations, which address the properties of systematicity, compositionality and transitivity. Unlike robustness, our relations are defined over multiple source inputs, thus increasing the number of test cases that we can produce by a polynomial factor. With them, we test the internal consistency of state-of-the-art NLP models, and show that they do not always behave according to their expected linguistic properties. Lastly, we introduce a novel graphical notation that efficiently summarises the inner structure of metamorphic relations.

## introduction many recent advances in neural models for nlp have been driven by the ability to learn from unlabeled data @xcite . this approach allows for training the models on large-scale corpora without the costly process of annotating them. as a result, the accuracy and complexity of state-of-the-art neural models for nlp have increased @xcite . this trend towards unlabeled data does not have a counterpart in testing nlp models. instead, both in-distribution testing and out-of-distribution testing @xcite rely on comparing the model's predictions to the ground truth. similarly, attempts at probing the internal computation of large nlp models use supervised classifiers as a diagnostic tool @xcite . in general, such extreme reliance on groundtruth data limits the quantity of test cases we can produce, which is a known problem in the software testing community @xcite . in this regard, a promising solution is metamorphic testing @xcite . under this paradigm, we test the internal consistency of an nlp model by checking whether it satisfies a necessary relation of its inputs and outputs @xcite . consequently, metamorphic testing relies on our ability to formally express our expectations on the behaviour of an nlp model. still, most of the metamorphic relations proposed in the literature target the same type of behaviour, as we show in this paper. indeed, the majority of them are robustness relations, which require that the output of an nlp model remains stable in the face of small input perturbations @xcite . these perturbations may involve simple typos @xcite @xcite , replacing individual words with a synonym @xcite @xcite , or adding irrelevant information to the input @xcite . due to their simple structure, robustness-like relations have been applied to the testing of several nlp tasks, including sentiment analysis @xcite , machine translation @xcite , and question answering @xcite . even testing the fairness of nlp models falls in this category @xcite . at the same time, we expect state-of-the-art nlp models to exhibit a broader range of linguistic properties than just robustness. first and foremost, nlp models should generalise systematically, i.e. their ability to understand some inputs should be intrinsically connected to their ability to understand related ones @xcite . while the exact definition of systematic behaviour varies in the literature @xcite , a common requirement is that the model's predictions are a result of a composition of syntactic and semantic constituents of the input @xcite . several supervised methods to test against such requirements exist @xcite , but they all rely on comparing the model's predictions to the ground truth. likewise, @xcite interprets systematicity as the ability to generalise over transitive relations. their supervised method shows that current models struggle to do so. in this paper, we propose three new classes of metamorphic relations, which are designed to test the systematicity, compositionality and transitivity of nlp models. in true metamorphic fashion, our relations do not rely on ground-truth data and scale up the generation of test cases by a polynomial factor. for each proposed relation, we provide an illustrative experiment where we test state-of-theart models for the expected linguistic behaviours. more in detail, our main original contributions are: lastly, in section 7 we conclude and outline possible future work. we discuss the ethical implications of our work in appendix a. we provide a quick-reference guide to our contribution in appendix b. the code of our experiments and reproducibility checklist are available at https: //doi.org/10.5281/zenodo.5703459 . ## a graphical notation for nlp metamorphic relations this section gives preliminary definitions and proposes a compact graphical notation for nlp metamorphic relations. definition 2.1 (nlp model). let f : @xmath0. here, we assume that f is a neural network, and y ≡ r k is either a k-dimensional embedding space or the soft-max output of a k-class classifier. in general, a metamorphic relation can be defined as (chen et al., 2018): definition 2.2 (metamorphic relation). a metamorphic relation r is a property of f across multiple inputs and outputs however, we are interested in the internal structure of such a relation. thus, let us discriminate between two types of inputs @xcite : definition 2.3 (source inputs). given a relation r with v inputs, let (x 1 , . . . , x u ) with @xmath1. these can be chosen freely, e.g. by extracting them from a dataset d. definition 2.4 (follow-up inputs). given a relation r with u source inputs, let (x u+1 , . . . , x v ) with @xmath2. these are computed by a transformation of the source inputs furthermore, all the relations in this paper prescribe specific conditions over the model's output: definition 2.5 (output property). define p ⊆ y 1 , . . . , y v as a relation over the output. here, we always write it in decidable first-order logic. altogether, the structure of an nlp metamorphic relation can be easily described in graphical form. to do so, we introduce the following compact notation (see example in figure 1 ). textual variables are represented as circles, whereas numerical variables (e.g. embeddings, softmax outputs) are squares. moreover, source inputs are shaded in grey, while all other nodes are in white. arrows represent the neural function f and the transformation t i . lastly, the output property p is linked to the relevant nodes with dashed lines. ## a taxonomy of existing nlp metamorphic relations most of the existing literature on nlp metamorphic testing proposes relations that fit in the structure of figure 1 . due to their reliance on just one source input, we refer to these metamorphic relations as single-input. the individual differences among them can be ascribed to the specific transformation t and property p . the present section derives a taxonomy of existing nlp relations by organising them along these two axes t and p . the transformation t is defined over the input text and thus allows for considerable creative freedom. a list of common options is presented here: similarly, swapping keywords in the context of a question-answer (qa) system can reveal inconsistent answers @xcite . in the same vein, fadaee and monz (2020) and @xcite shows the volatility of neural translation models to minor word-level transformations of the input. regarding the output property p , the current literature only offers three choices. we list them here, alongside their first-order logic formulation: for classification models, we can express it via the softmax output @xmath3, . . . , y c ) as: in sections 4, 5 and 6 we employ some of the transformations t and properties p defined here as building blocks for new metamorphic relations. ## pairwise nlp metamorphic relations for testing systematicity we introduce a new class of metamorphic relations to test the systematicity of nlp models. here, we take the general definition of systematicity in @xcite , which states that the predictions of an nlp model across related inputs should be intrinsically connected and express it as a metamorphic relation (see figure 2 ). since we do not want to rely on ground-truth data, we first establish a baseline for the model's behaviour by comparing its predictions across two different source inputs. then, we perturb both source inputs via the same transformation and test whether the model's behaviour changes accordingly. more formally, we define pairwise-systematicity relations as follows. let x 1 , x 2 ∈ d be a pair of source inputs, and x 1 , x 2 their corresponding follow-up inputs via transformation t . furthermore, denote with y 1 , y 2 , y 1 , y 2 the outputs produced by model f . finally, define the output property p in the following form: note that this definition does not rely on groundtruth data. in fact, we trust the model's predictions (y 1 , y 2 ) over the source inputs to establish our premise p src . the actual test checks whether transforming the source inputs with t produces outputs that satisfy the expected property p f wl . any violation of this property, i.e. when p src ∧ ¬p f wl , reveals an inconsistency in the model's predictions that breaks the user's expectation of systematic behaviour. in section 4.2, we give an intuitive geometrical explanation of the type of constraints imposed by pairwise-systematicity relations on the embedding space of a neural nlp model. a hidden advantage of metamorphic relations with multiple source inputs (see also sections 5 and 6) is that they naturally produce more test cases than single-input ones. in the case of pairwise systematicity, each input in the pair (x 1 , x 2 ) is extracted from the same dataset d. thus, a dataset with |d| = k entries generates an o(k 2 ) number of test cases, as opposed to o(k) for single-input relations. we see an example of this in section 4.1. ## pairwise nlp metamorphic relations for testing compositionality many probing works train simple supervised classifiers on top of the hidden representations of an nlp model (e.g. @xcite . these classifiers, called probes, can reveal whether the neural model has learnt to recognise some fundamental constituents of the input language early on. the presence of such building blocks can be a sign that an nlp model exhibits compositional behaviour @xcite . here, we propose to test the presence of compositional constituents in the hidden layers via metamorphic testing. to this end, we turn towards a stricter definition of mathematical compositionality of the neural network behaviour, rather than global linguistic compositionality, which is harder to define @xcite . consider the graph in figure 4 . there, the neural model is split into the mathematical composition of two functions f • g. more precisely, @xmath4, and @xmath5. now, let us define the output property p as follows: a relation in this form allows us to express whether specific precursor signals in z are expected to have a direct effect on y. in a similar way to the relations in section 4, both the premise p hid and hypothesis p out are established by comparing across pairs of inputs, rather than a ground-truth. in section 5.1, we show how our technique can reveal the presence (or absence) of compositional building blocks in an nlp model. ## three-way nlp metamorphic relations for testing transitivity an nlp model that generalises correctly should exhibit transitive behaviour under the right circumstances @xcite . that is, if the model predicts a transitive linguistic property over the input pairs (x 1 , x 2 ) and (x 2 , x 3 ), then it should also predict it for the pair (x 1 , x 3 ). here, we propose to test this behaviour in a metamorphic way. the three source inputs x 1 , x 2 , x 3 are combined into all possible pairs. if two pairs are predicted as true by model f , the third must be predicted true as well. more specifically, let us introduce the three-way transitivity relation in figure 5 . there, the three source inputs x 1 , x 2 , x 3 are combined to form all possible input pairs x @xmath6, x j ). then, we can test whether their corresponding outputs are transi-tive with the following output property: ## conclusions and future work in this paper, we presented three new classes on metamorphic relations. thanks to them, we could test the systematicity, compositionality and transitivity of state-of-the-art nlp models. the advantage of our approach is that it does not rely on ground-truth annotations. it can generate a polynomially larger number of test cases than supervised testing, revealing whether the nlp model under test is internally consistent. still, testing is only one side of the coin. like in recent work about robustness @xcite , the tested models have not been trained on a metamorphic objective (e.g. as an additional loss term). we believe that doing so could improve the safety and consistency of a model's predictions.