Vocabulary
In this overview, we'll cover the key terminology and concepts used in large language model (LLM) training and prompting. This will be beneficial for experienced Python programmers looking to utilize and adapt foundational models such as LLAMA and Alpaca for their specific use cases.
Foundational models: Foundational models, like GPT-4, are pre-trained LLMs that have been trained on vast amounts of text data from diverse sources. They serve as a starting point for developing more specialized models through fine-tuning.
Pre-training: This is the initial training process for a foundational model. The model learns language representations by predicting the next word in a sentence. The resulting model captures general language understanding, including grammar, facts, and some reasoning abilities.
Fine-tuning: After pre-training, the foundational model is adapted to specific tasks or domains through fine-tuning. This involves training the model on a smaller, task-specific dataset. Fine-tuning allows the model to learn from the unique characteristics of the domain or task while retaining the general language understanding acquired during pre-training.
Parameters: Parameters, or weights, are the values in a neural network that are learned and adjusted during training. In LLMs, these parameters help capture the statistical properties of the training data, allowing the model to generate meaningful output.
Tokens: Tokens are the basic units of text data that an LLM processes. In English, tokens can be words or subwords. Tokenization is the process of breaking down text into tokens, which the LLM then uses to understand and generate language.
Context: The context is the sequence of tokens that the LLM considers when generating an output. The context window size, determined by the model's architecture, dictates the maximum number of tokens the model can process at once. Longer context windows enable the model to understand more complex inputs but can be computationally expensive.
Prompts: Prompts are input text sequences provided to an LLM to guide its response. They are a way to communicate your intentions or the desired outcome to the model. Carefully designed prompts can improve the model's performance on specific tasks.
API: The Application Programming Interface (API) allows developers to interact with LLMs programmatically, enabling the integration of the models' capabilities into custom applications and services.
have you (GPT4) been fine-tuned or just pre-trained?
when do the parameters get learned?
As an AI language model, I (GPT-4) have undergone both pre-training and fine-tuning. Pre-training involves learning from a vast amount of text data to capture general language understanding, including grammar, facts, and some reasoning abilities. Fine-tuning, on the other hand, adapts the foundational model to specific tasks or domains using smaller, task-specific datasets.
Parameters, or weights, are learned and adjusted during both pre-training and fine-tuning. In pre-training, the model learns the parameters by predicting the next word in a sentence, capturing the statistical properties of the training data. During fine-tuning, the model fine-tunes these parameters using the task-specific dataset, allowing it to perform well on the specific tasks or domains it has been fine-tuned for.
pre-training is much more expensive than fine-tuning is much more expensive than running/prompting
In general, pre-training is the most computationally expensive part of the process, as it involves training the model on a vast amount of text data to capture general language understanding. This requires significant computational resources, such as powerful GPUs or TPUs, and can take days, weeks, or even months to complete, depending on the size of the model and the dataset.
Fine-tuning is less computationally expensive compared to pre-training, as it involves training the model on a smaller, task-specific dataset. This process can still require a substantial amount of resources but is generally faster and less resource-intensive than pre-training.
Running or prompting the model is the least computationally expensive part of the process. Once the model has been pre-trained and fine-tuned, it can be used to generate responses or perform tasks by providing prompts. This step typically requires far fewer resources than either pre-training or fine-tuning, making it faster and more cost-effective.
GPUs
Are GPUs useful in running/prompt or are they usually only needed in the pre-training and fine tuning?
GPUs can be useful for running and prompting large language models, especially when dealing with large inputs, long context lengths, or when generating multiple responses in parallel. However, for many applications and use cases, especially those involving smaller models or single-response generation, running the model on a CPU might be sufficient.
In summary, GPUs are most crucial during the pre-training and fine-tuning phases, where they can significantly speed up the computation. While they can also be beneficial for running and prompting large language models, their necessity depends on the specific application and requirements. If you're working with smaller models or generating single responses, a CPU might be adequate for running the model.
Single prompts versus conversation
Some interfaces are simple:
But, GPT4 is able to remember conversation from on prompt to the next. Is this done by feeding the existing conversation (or summaries of the existing conversation) back in as the context of the next prompt?
Yes, that's correct. In order to maintain context and provide coherent responses across multiple prompts in a conversation, the previous conversation history is typically included as part of the input for the next prompt. By doing so, the language model can take into account the prior exchanges when generating a response, resulting in more contextually relevant and coherent replies.
To achieve this, you can concatenate the conversation history with the new prompt, separating them with an appropriate delimiter (such as a newline character). Keep in mind that the combined input should not exceed the model's maximum token limit (e.g., 2048 tokens for GPT-3), as the model will not be able to process inputs larger than its context window. If the conversation history is too long, you might need to truncate, omit, or summarize parts of the conversation to fit within the token limit.
Here's an example:
User: What is the capital of France?AI: The capital of France is Paris. User: What is the population of Paris?AI: As of my last knowledge update in 2021, the population of Paris is approximately 2.1 million. Please note that this information might be outdated and I recommend checking the latest data for the most accurate figure.When prompting the model for the second question, you would include the entire conversation like this:
What is the capital of France?The capital of France is Paris.What is the population of Paris?The model would then generate a response that takes the context of the prior
conversation into account.
Words versus "tokens"
How many words can a single prompt to GPT4 use?
The maximum number of words in a single prompt to GPT-4 depends on the token limit of the model. A token can represent a word or part of a word, and different models may have different token limits based on their architecture. For example, GPT-3 has a token limit of 2048 tokens.
When creating a prompt, you should consider the number of tokens rather than the number of words, as some words might be split into multiple tokens during tokenization. The total number of tokens, including the conversation history and the new prompt, should not exceed the model's maximum token limit.
To give a rough estimate, assuming an average word length of 4-6 characters plus a space, you could potentially use around 300-500 words within the token limit of 2048 tokens. However, this is only an approximation, and the actual number of words will depend on the specific words and their tokenization. It's always a good idea to check the token count of your input to ensure it doesn't exceed the model's limit.
Tokenization breaks down text into smaller units, such as words or subwords, depending on the language and the tokenization method used. In the case of GPT models, a byte-pair encoding (BPE) tokenizer is employed. BPE merges the most frequently occurring character sequences, forming subword units that balance between character-level and word-level tokenization.
Counting tokens
GPT models don't inherently provide a way to count tokens in a prompt, but you can use the tokenizer provided by the respective model library to count tokens before sending the prompt. For instance, if you are using the OpenAI API with GPT-3, you can use OpenAI's tiktoken Python library to count tokens in a text string without making an API call.
If you're using the Hugging Face Transformers library, you can use the tokenizer associated with the specific GPT model to count tokens. Here's an example using GPT-3:
from transformers import GPT2Tokenizer tokenizer = GPT2Tokenizer.from_pretrained("gpt2") text = "The quick brown fox jumps over the lazy dog."tokens = tokenizer.tokenize(text)token_count = len(tokens) print(f"Token count: {token_count}")
This code snippet demonstrates how to count tokens for the given text using the GPT-2 tokenizer from the Hugging Face Transformers library. Note that this example uses GPT-2, but the process is similar for GPT-4 or other GPT models.
By counting tokens before sending a prompt, you can ensure that your input does not exceed the model's maximum token limit.
Maximum token limit
if each prompt can contain 2048 tokens for GPT3, the internal context limit must be many more tokens? What is this limit?
For GPT-3, the maximum token limit for both input and internal context is the same, which is 2048 tokens. This means that when you provide a prompt to GPT-3, the total number of tokens, including conversation history, new prompt, and any additional formatting, should not exceed 2048 tokens. The model processes and maintains context within this token limit.
If a conversation history or prompt exceeds this limit, you will need to truncate, omit, or summarize parts of the text to fit within the token constraint. Keep in mind that removing or truncating parts of the conversation may cause the model to lose some context, potentially affecting the coherence and relevance of the generated response.
the responses of the AI are also fed back in to the next prompt?
Yes, that's correct. When using ChatGPT in a multi-turn conversation, the responses of the AI are also included in the context for the next prompt. This allows the model to maintain context and generate more relevant and coherent responses.
To achieve this, you would concatenate the conversation history, including both user inputs and AI-generated responses, with the new prompt. This combined input should not exceed the model's maximum token limit (e.g., 2048 tokens for GPT-3).
Scaling token limit
What are the scaling properties of increasing the token limit?
Say I have a machine that is running a model that can generate a response to a 1000-token prompt in 5 seconds. Let's say I want to increase this to 100,000 tokens.
Would this take a lot more memory, CPU, GPU, time? Would it be linear, logarithmic or quadratic?
When increasing the token limit for a language model, several factors come into play, including memory requirements, computational power, and the time it takes to process the input. The scaling properties of these factors depend on the specific implementation and hardware but generally exhibit a linear relationship with the token count.
- Memory: The memory requirements for processing an input will increase linearly with the token count. A larger token limit will require more memory to store the model's activations during the forward and backward passes in training or just the forward pass during inference.
Computational Power (CPU/GPU): The computational power needed to process the input will also increase linearly with the token count. This is because the model performs more calculations when the input size is larger, which translates to a higher number of operations executed on the CPU or GPU.
Time: The time it takes to process an input is directly proportional to the token count. As the number of tokens increases, the model needs to perform more calculations, which takes more time. The relationship between time and token count is generally linear.
However, it's important to note that increasing the token limit to a large number like 100,000 tokens can be challenging in practice. For very large inputs, you may need to consider techniques like model parallelism or gradient checkpointing to fit the model into memory during training. Additionally, you might face practical limitations, such as GPU memory constraints, which could restrict how much you can scale the token limit.
In summary, increasing the token limit generally leads to linear increases in memory, computational power, and processing time. However, practical limitations and the specific implementation may affect the scaling properties.
Adding 100,000 tokens of "context"
So, if you need to add say a spec with 100,000 tokens to the context of the interaction, the best phase to do this is at the "fine-tuning"?
One use case might be turning a large API specification into an interactive, natural language chatbot.
Yes, if you have a large amount of context like a 100,000-token API specification, incorporating it during the fine-tuning phase is a more effective approach. By doing so, the model can learn to understand and generate responses relevant to the API specification.
To achieve this, you would create a custom dataset with examples based on the API specification. These examples should contain questions, commands, or prompts related to the API, along with their appropriate responses. The dataset should cover various aspects of the API, such as functionality, input and output formats, and potential error handling scenarios.
Once you have created the dataset, you can use it to fine-tune the model. During fine-tuning, the model will learn to associate the API specification with the relevant prompts and responses. This will enable the model to generate more accurate and contextually appropriate responses when prompted with questions or commands related to the API.
After fine-tuning, you can use the model in an interactive, natural language chatbot. Although the model won't have the entire API specification within its immediate context window, it should still be able to generate relevant responses based on the knowledge it acquired during fine-tuning. Keep in mind that, depending on the complexity of the API and the quality of the fine-tuning dataset, some prompts may still require additional context or clarification from the user to generate accurate responses.
To create a custom dataset for fine-tuning based on a simple OpenAPI specification, you'll want to structure your examples in a question-response format. Here's a minimal OpenAPI specification for a fictional Frozznazzle and Bar API:
openapi: "3.0.0"info: title: "Frozznazzle and Bar API" version: "1.0.0"paths: /frozznazzles: get: summary: "Get a list of Frozznazzles" responses: 200: description: "A list of Frozznazzles" /bar/{id}: get: summary: "Get Bar details by ID" parameters: - name: "id" in: "path" required: true schema: type: "integer" responses: 200: description: "Bar details"security: - ApiKeyAuth: []components: securitySchemes: ApiKeyAuth: type: "apiKey" in: "header" name: "X-API-KEY"
Here is how the training data might look:
[ { "input": "How do I get a list of Frozznazzles using Golang?", "output": "To get a list of Frozznazzles using Golang, you can make an HTTP GET request to the '/frozznazzles' endpoint. Here's a code snippet using the 'net/http' package:import ( \"fmt\" \"io/ioutil\" \"net/http\")func main() { apiKey := \"your_api_key\" req, _ := http.NewRequest(\"GET\", \"https://api.example.com/frozznazzles\", nil) req.Header.Set(\"X-API-KEY\", apiKey) client := &http.Client{} resp, _ := client.Do(req) body, _ := ioutil.ReadAll(resp.Body) fmt.Println(string(body))}" }, { "input": "How do I get my Bar details with curl?", "output": "To get Bar details with curl, you can make an HTTP GET request to the '/bar/{id}' endpoint. Replace '{id}' with the actual Bar ID. Also, include the 'X-API-KEY' header with your API key. Here's an example:curl -H 'X-API-KEY: your_api_key' 'https://api.example.com/bar/123'" }, { "input": "How do I authenticate?", "output": "To authenticate with the Frozznazzle and Bar API, you need to provide your API key in the 'X-API-KEY' header for each request. For example:X-API-KEY: your_api_keyReplace 'your_api_key' with your actual API key. This header should be included in each request to the API." }]
This JSON-formatted training data contains examples of user prompts and their corresponding responses. The dataset should cover various aspects of the API, as mentioned earlier. You can extend this dataset with more examples for better fine-tuning results.
How many examples to fine-tune a model?
The number of examples required for fine-tuning a model can vary significantly depending on several factors, such as:
The complexity of the domain: If the domain or use case is relatively simple and narrow, fewer examples may be sufficient. For more complex domains, you may need a larger dataset to cover the various aspects and nuances.
The base model's performance: If the pre-trained model already performs well on your domain or use case, you may need fewer examples for fine-tuning. In contrast, if the base model's knowledge is limited in the domain, more examples might be required.
Desired level of accuracy: The more examples you have, the better your fine-tuned model is likely to perform. However, diminishing returns may be observed as the dataset size increases. Striking a balance between dataset size and performance is important.
As a rough estimate, a few hundred to a few thousand high-quality examples can be a good starting point for fine-tuning. The key is to ensure that the dataset covers a wide range of scenarios and edge cases related to the domain.
It's essential to monitor the model's performance on a validation set during the fine-tuning process. This can help you identify if more examples are needed or if the model is overfitting to the training data. You may need to experiment and iterate on the dataset size and model hyperparameters to achieve the desired performance for your specific use case.
Millions of lines of context
Say I have a whole repository with millions of lines of proprietary code. In this case, it would be more appropriate to bake in this "context" to the foundational model or into the pre-training rather than trying to use "fine-tuning"?
Fine-tuning versus pre-training
If you're running everything on your own hardware and not sharing data with any third party, you have more flexibility in how you incorporate your proprietary code into the language model. Here are the two primary approaches in this scenario:
Pre-training from scratch: If you have sufficient computational resources, you can create a new language model from scratch using your proprietary code as part of the training corpus. During the pre-training phase, the model will learn the general structure, patterns, and information present in the code. This approach might be more suitable if you want the model to have a strong foundation in understanding your codebase.
Fine-tuning on an existing model: If you have access to a pre-trained model, you can fine-tune it on a representative sample or even the entire codebase if your hardware allows it. Fine-tuning on your code will enable the model to learn specific patterns and knowledge related to your codebase.
In both cases, since you're using your own hardware and not sharing data, you can maintain control over the confidentiality and security of your proprietary code.
Ultimately, the choice between pre-training from scratch and fine-tuning on an existing model depends on your specific use case, computational resources, and desired performance.

How expensive is pre-training from scratch?
Pre-training a large language model from scratch can be quite computationally expensive. If you already have a 7B-parameter foundational model and want to incorporate your codebase, you'll need to train a new model with a similar architecture and size. The computational cost depends on various factors, including the size of your codebase, the model architecture, and training parameters such as learning rate, batch size, and number of training steps.
For reference, the training of OpenAI's GPT-3, which has 175 billion parameters, required several thousand GPU-years of computation. While a 7B-parameter model would be smaller and less computationally expensive, it would still require a substantial amount of resources. The cost could be on the order of several hundred GPU-years, depending on the specific configuration and setup.
Given the high computational cost, it might be more practical to fine-tune your existing 7B-parameter foundational model on a representative sample of your codebase, as mentioned in the previous responses. This approach would be significantly less resource-intensive and time-consuming compared to pre-training from scratch. Fine-tuning can still enable your model to learn specific patterns and knowledge related to your codebase while leveraging the general knowledge present in the foundational model.
In summary, pre-training a 7B-parameter model from scratch is computationally expensive, potentially requiring hundreds of GPU-years. Fine-tuning the existing model on a representative sample of your codebase is a more practical and resource-efficient approach.
Further reading and resources:
