Maison  >  Article  >  Périphériques technologiques  >  Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

WBOY
WBOYoriginal
2024-08-07 18:04:03336parcourir

Il y a sept ans, l'article « L'attention est tout ce dont vous avez besoin » proposait l'architecture du transformateur, bouleversant tout le domaine de l'apprentissage profond.

De nos jours, tous les principaux modèles sont basés sur l'architecture du transformateur, mais le principe de fonctionnement interne du transformateur reste un mystère non résolu.

L'année dernière, Llion Jones, l'un des auteurs de l'article Transformer, a annoncé la création de la société d'intelligence artificielle Sakana AI. Récemment, Sakana AI a publié un article intitulé "Transformer Layers as Painters", qui a exploré le flux d'informations dans les transformateurs pré-entraînés et mené une série d'expériences sur des modèles de transformateurs gelés avec décodeur uniquement et encodeur uniquement. Notez que cette étude n'a effectué aucun type de réglage fin sur le modèle pré-entraîné.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

Adresse papier : https://arxiv.org/pdf/2407.09298v1

Cette recherche estime que le mécanisme interne du transformateur (en particulier la couche intermédiaire) peut être compris par analogie avec le pipeline de peinture d'un peintre.

Un pipeline de peinture transmet généralement la toile (entrée) à une série de peintres. Certains peintres sont doués pour peindre des oiseaux, tandis que d'autres sont doués pour peindre des roues. Chaque peintre reçoit la toile du peintre en dessous, puis décide d'ajouter quelques traits au tableau, ou simplement de la transmettre au peintre au-dessus (en utilisant des connexions résiduelles).

Cette analogie n'est pas une théorie stricte, mais un outil pour réfléchir aux couches de transformateur. Inspirée par cette analogie, l'étude a testé et vérifié quelques hypothèses :

  • Est-ce que toutes les couches utilisent le même espace de représentation ?

  • Toutes les couches sont-elles nécessaires ?

  • Les couches intermédiaires remplissent-elles toutes la même fonction ?

  • L'ordre des couches est-il important ?

  • Ces couches peuvent-elles fonctionner en parallèle ?

  • Pour certaines tâches, l'ordre est-il plus important que d'autres facteurs ?

  • Les boucles aident-elles à superposer le parallélisme ?

  • Quelles variantes ont le moins d'impact sur les performances du modèle ?

L'étude a mené une série d'expériences sur des LLM pré-entraînés, qui comprenaient l'expérimentation de variantes de la stratégie d'exécution standard du transformateur et la mesure de ces changements sur divers benchmarks pour les modèles à décodeur uniquement (Llama) et à encodeur uniquement (BERT). impact sur les performances du modèle.

Tous les calques utilisent-ils le même espace de représentation ?

Pour savoir si différentes couches utilisent le même espace de représentation, les auteurs ont testé si Transformer est robuste lorsqu'il saute des couches spécifiques ou change l'ordre des couches adjacentes. Par exemple, dans Llama2-7B, la couche 6 s'attend généralement à recevoir la sortie de la couche 5. Si la couche 6 reçoit la sortie de la couche 4, se comportera-t-elle « de manière catastrophique » ?

Sur la figure 2, nous pouvons voir que, à l'exception des première et dernière couches, les couches de Llama2-7B sont assez robustes au saut ou au changement de couche.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

Cette expérience montre que la couche intermédiaire partage un espace de représentation et a un espace de représentation différent des "couches périphériques" (la première et la dernière couche). Pour tester davantage cette hypothèse, les auteurs ont suivi des études antérieures et mesuré la similarité cosinusoïdale moyenne entre les activations d'états cachés à différentes couches des modèles de la ligne de base (Llama2-7B, Llama2-13B et BERT-Large). La figure 3 montre la cohérence entre toutes les couches intermédiaires.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

Cela montre que le modèle peut avoir trois espaces de représentation différents des couches "début", "milieu" et "fin". Réponse à la question 1 : Oui, les couches intermédiaires semblent partager un espace de représentation commun.

Toutes les couches sont-elles nécessaires ?

Pour tester davantage si l'espace de redirection de la couche intermédiaire est réellement partagé (en plus d'avoir une similitude cosinus étroite), cette étude a essayé de "sauter la couche", c'est-à-dire d'envoyer la sortie de la Nième couche directement au N +Mième couche (où M > 1), « sautant » ainsi la couche M − 1, comme le montre la figure 1a. L'expérience visait à voir si la couche N + M pouvait comprendre les activations de la couche N même si elle n'était formée qu'à partir de l'entrée de la couche N + M − 1 . La figure 4 montre que Llama2-7B et BERT-Large subissent une légère dégradation des performances sur de nombreux tests. En répondant à la question 2, toutes les couches sont-elles nécessaires : 

Non, au moins certaines des couches intermédiaires peuvent être retirées sans défaillance catastrophique.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

Les couches intermédiaires remplissent-elles toutes la même fonction ?

Si les couches intermédiaires partagent toutes un espace de représentation commun, cela signifie-t-il que les autres couches intermédiaires sont redondantes ? Pour tester cela, les chercheurs ont réexécuté l'expérience "saut" de la sous-section précédente, en remplaçant les poids de la couche intermédiaire par les poids de la couche centrale, en bouclant effectivement T - 2N sur chaque couche remplacée + 1 fois. où T est le nombre total de couches (32 couches pour Llama2-7B et 24 couches pour BERT-Large).

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

As shown in Figure 5, it can be observed that as the number of replaced layers increases, the model’s score in the benchmark test drops rapidly. As shown in Figure 11 below, this method of replacing layers is worse than any other method the researchers tried. Therefore, the researchers concluded that the middle layers perform different functions, and it is not feasible to share weights between middle layers.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

Is the order of layers important?

Previous experiments showed that middle layers share a representation space but are responsible for different functions in this space. The next question to address is what the order of these functions means. To solve this problem, the researchers designed two sets of experiments. First, run the intermediate layers in the reverse order in which they were trained. Specifically, take the output of the T - Nth layer, input it to the T - N - 1th layer, then input the output of this layer to the T - N - 2th layer, and so on, until the Nth layer layer, and then send the output of this layer to the subsequent T - N layer. In a second set of experiments, the researchers ran the middle layers in random order and averaged them over 10 seed values.

Figures 6 and 7 show the results of running the middle layers in reverse and in random order respectively, with the model showing a gradual downward trend across all base test sets. This also shows that although the order of the layers is of some importance to the model, the layers can still function even if the order is changed.

Even more interestingly, randomly shuffling the order of the layers works better than doing the exact other way around. This may be because randomly shuffling the order in some ways preserves some of the original relationships between layers (i.e. layer i follows layer j, where i > j), while the complete reverse completely breaks these relationships.

Can these layers run in parallel?

To verify that the existence of the layers themselves is more important than the order of execution, the researchers designed an experiment to run the intermediate layers in parallel and send their average results to the final N layers.

As shown in Figure 8, the model's performance in all benchmark tests shows a gentle downward trend. However, this trend does not apply to the mathematical word problems in GSM8K.

Experimental results show that this method is effective in most cases, but some complex mathematical problems are not handled well. This method of parallel processing is worse than skipping layers, but not as good as running the layers in reverse order. Based on this, the researchers concluded that the parallel operation layer is feasible in general cases, but for mathematical problems that require sequential logical understanding, this method may not be suitable.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

For some tasks, is order more important than other factors?

For most "retrofitted" models, they tend to show the steepest downward trend when faced with abstract reasoning (ARC) or mathematical reasoning (GSM8K) benchmarks. This phenomenon may stem from the fact that step-by-step reasoning tasks are much more sensitive to the order of model levels than those common sense tasks that mainly rely on semantic understanding. Unlike those tasks that can be accomplished by understanding semantics alone, reasoning tasks require the model to grasp both structure and meaning. This observation is consistent with the hypothesis that the model may perform some degree of order-dependent inference during a single processing session.

The researchers used a metaphor to illustrate: if you are painting a collage composed of many different elements, then the order of the paintings may not be so important; but if you are painting a precise architectural scene, then the order of each stroke The order becomes very important. Based on this, the researchers concluded that mathematics and reasoning tasks have a higher dependence on the order of model layers, while for those tasks that mainly rely on semantic understanding, the impact of order is relatively small.

Do loops help parallelize between layers?

Following the metaphor of painting in the previous section, when a painter paints a painting, he does not paint everything at the beginning, but paints a part first, such as the car body, and then adds other things based on this part. , such as wheels. In the AI ​​model, the layers are so-called painters, and processing information is painting. If the correct information is obtained first, the so-called car body is drawn first, then they can complete their work better and contribute to the painting. Add wheels.

For the transformer, when given the appropriate input, the layer may only contribute in the forward propagation, rather than "passing" the input through the residual connection. If this is the case, then iterating the parallel layers from the previous experiment should improve the model's performance more than executing the parallel layers a single time. Based on this, the researchers tested this by feeding the average output of the parallel layer into the same layer for a fixed number of iterations.

Figure 9 shows the result of looping the parallel layer 3 times. The results of parallelizing the loop 3 times are significantly better than a single iteration (parallel layer). When the starting layer N is set to 15 (for the Llama2-7B model) or 11 (for the BERT model), which is at the extreme left end of each case, only a single layer is affected. In this particular case, the effect of parallelizing the loop three times is equivalent to simply repeating the middle layer three times. At the same time, the performance of the parallel layer at this point is indistinguishable from the full model.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

The researchers also repeated the same experiment with different numbers of iterations. Figure 10 shows the performance of Llama2-7B as a function of the number of parallelization layers M and the number of iterations. The highest performing iteration number for each M is marked with a red box. The optimal number of iterations scales roughly linearly with the number of parallelized layers, except for M=29 and M=31 (where almost all layers are parallelized). Therefore, the researchers concluded that the optimal number of iterations is proportional to the number of parallelization layers.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

How to adjust layers with the least impact on model performance?

Finally, in Figure 11, the researchers compared the "transformations" of Transformer in all experiments, showing the median or average of all benchmarks on a chart.

Huit questions et huit réponses pour comprendre le fonctionnement interne de Transformer

Intermediate duplication – replacing the middle layer with the same number of copies of the middle layer – performed the worst, quickly dropping to random baseline performance. In contrast, loop parallelism and random layer order have minimal impact. Therefore, the researchers concluded that duplication of a single layer has the most severe impact. Randomizing layer order and loop parallelism have minimal impact.

These experiments show a gentle performance degradation overall, but researchers still don’t know why these layers can maintain a certain degree of robustness under most perturbations. This issue needs to be further explored in future research.

Please see the original paper for more details.

Reference link: https://arxiv.org/pdf/2407.09298v1

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn