On the In-context Generation of Language Models (2024)

Abstract: Large language models (LLMs) are found to have the ability of in-context generation (ICG): when they are fed with an in-context prompt concatenating a few somehow similar examples, they can implicitly recognize the pattern of them and then complete the prompt in the same pattern. ICG is curious, since language models are usually not explicitly trained in the same way as the in-context prompt, and the distribution of examples in the prompt differs from that of sequences in the pretrained corpora. This paper provides a systematic study of the ICG ability of language models, covering discussions about its source and influential factors, in the view of both theory and empirical experiments. Concretely, we first propose a plausible latent variable model to model the distribution of the pretrained corpora, and then formalize ICG as a problem of next topic prediction. With this framework, we can prove that the repetition nature of a few topics ensures the ICG ability on them theoretically. Then, we use this controllable pretrained distribution to generate several medium-scale synthetic datasets (token scale: 2.1B-3.9B) and experiment with different settings of Transformer architectures (parameter scale: 4M-234M). Our experimental results further offer insights into how the data and model architectures influence ICG.

## introduction as the data and parameter scale continue to increase, large language models (llms) have shown strikingly emergent abilities @xcite , where one of the most exciting ones is in-context learning (icl) @xcite . given an incontext prompt that concatenates a few in-context examples and a query input, llms can somehow implicitly guess the "topic" of those examples and complete the query input in the desired way. llms can actually achieve more: they can imitate those * corresponding author examples using the topic learned in context @xcite to generate new plausible examples, as shown in figure 1 . this in-context generation (icg) ability forms the foundation of multiple few-shot prompting methods like icl and other variants like chain-of-thoughts @xcite . intuitively, one might comment that llms learn the icg ability from data in the repetition mode, which roughly refers to a type of text concatenated with sequences under the same topic. this is true to some extent. as known, typical pretrained corpora contain (e.g. commoncrawl foot_0 ) internet data which has an unneglectable portion of array-page data such as imdb foot_1 review pages. after preprocessing, these pages are converted to repetition mode data, as shown in figure 1a . however, this isn't enough to explain the icg ability, since llms can also generate sequences of in-context learned topics that don't appear to repeat and even are unseen in the pretrained corpora. for example, figure 1 shows sampled completions of llama2-13b @xcite given in-context prompts of different types of topics: ## theoretical support intuitively, the pretrained distribution itself ensures the icg ability for repeated topics r. this can be explicitly formalized by the following theorem: theorem 1. given an in-context prompt x 1:n , where each sample x n ∼ p(x| β) and β ∈ r, the pretrained distribution has the following properties: if the language model p lm is expressive enough, it would gradually approach the pretrained distribution p with the increase of the number of training examples foot_4 . as a result, p lm would exhibit the same properties p as shown in theorem 1. therefore, the icg ability for repeated topics directly originates from the pretrained corpora. detailed theoretical results are provided in appendix b. here, we only present a proof sketch. proof sketch. according to section 2.1, ∀@xmath0, the data icg distribution is: therefore, the data icg distribution p(x|x 1:n ) is dominated by the topic predictive distribution p(β|x 1:n ), i.e., ictr. p(β|x 1:n ) can be further decomposed as the mixture of modes: firstly, we can prove that if β ∈ r, then plim @xmath1:n ) = 1 (corollary 1). therefore, the mixture in formula (11) focuses on the component of repetition mode p(β|x 1:n , α) when n is large: this form is exactly the bayesian posterior distribution, which is in accord with previous works connecting icl and bayesian statistics @xcite @xcite . likewise, it turns out that the if β ∈ r, then plim @xmath2:n , α) = 1 (corollary 3), thus establishing the first point of theorem 1. since the data icg distribution p(x|x 1:n ) depends on the topic predictive distribution p(β|x 1:n ), we can prove the second point of theorem 1 analogously foot_5 . in appendix b and c, we also present a detailed formula of the convergence, in which the convergence speed depends on the distinguishment of different modes and topics. ## experiments theory 1 can't ensure the icg ability for nonrepeated and unseen topics β ∈ c ∪ u because they have a zero repetition prior p(β|α) = 0 and so the posterior under repetition mode is also zero: p(β|x 1:n , α) = 0. then, the correct component p(x|β) would never be selected under the repetition mode, preventing the icg/icl ability as a consequence. however, this is contrary to the real case, where llms have the icg-generalization ability: they are able to generalize icg/icl abilities to nonrepeated and unseen topics β ∈ c ∪ u . we speculate that this might be caused by factors in both data and model side: we conduct rich experiments to verify the above arguments. ## conclusions this paper provides a systematic study of icg ability of language models. firstly, we propose a plausible latent variable pretrained distribution, formalizing icg as a problem of next topic prediction. then, we prove that the repetition nature of a few topics ensures the icg ability on them theoretically. we also conduct rich experiments to study the effects of different factors of data and model architectures on icg and icg-generalization. we believe this paper is beneficial to a better understanding of the icg ability, as well as large language models.