Tuning and Testing Llama 2, Flan-T5, and GPT-J with LoRA, Sematic, and Gradio
This workflow is less about choosing a single model and more about operationalizing an adaptable training system. For IT teams, the key architectural point is that LoRA keeps the base model intact while externalizing task-specific deltas. That separation makes it easier to version, compare, and roll back changes, but it also means production design must explicitly manage two artifacts: the foundation model and the adapter weights.
The tooling stack matters because it reduces glue code across the ML lifecycle. Hugging Face provides model and dataset portability, while Sematic adds orchestration, lineage, and run tracking around the training pipeline. In practice, that combination helps teams move from local experimentation to repeatable execution without rewriting the workflow. The trade-off is that integration depth increases: teams need consistent dataset schemas, reproducible environments, and storage conventions for checkpoints, metrics, and exports.
Compute constraints shape both model selection and operational risk. Sequence-to-sequence and causal models can both be adapted, but they do not impose the same training behavior or memory profile. That means infra teams have to plan for GPU availability, experiment duration, and failure recovery differently depending on the chosen base model and tuning configuration. Even with parameter-efficient methods, the path from notebook to reliable pipeline still depends on hardware fit and disciplined resource management.
The Gradio step is also an important systems pattern, not just a demo convenience. An interactive UI gives fast feedback during prompt and output testing, but it creates a human-in-the-loop surface that should be isolated from the training job itself. Keeping that boundary clear helps avoid coupling model evaluation to the lifecycle of the training process, which is important when the same pipeline may later be promoted into a broader production or batch inference workflow.
The Goal
Let’s set a well-defined goal for ourselves: building a tool that can summarize information into a shorter representation. Summarization is a broad topic, with different properties for models that would be good at summarizing news stories, academic papers, software documentation, and more. Rather than focusing on a specific domain, let’s create a tool that can be used for various summarization tasks, while being willing to invest computing power to make it work better in a given subdomain. Letโs set a few more criteria. Our tool should:- Be able to pull from a variety of kinds of data to improve performance on a specific sub-domain of summarization
- Run on our own devices (including possibly VMs in the cloud that weโve specified)
- Allow us to experiment using only a single machine
- Put us on the path to scale up to a cluster when weโre ready
- Be capable of leveraging state-of-the-art models for a given set of compute constraints
- Make it easy to experiment with different configurations so we can search for the right setup for a given domain
- Enable us to export our resulting model for usage in a production setting
Fine-tuning
Looking at our goal of being able to achieve good performance on a specific sub-domain, there are a few options that might occur to you. We could:- Train our own model from scratch
- Use an existing model โoff the shelfโ
- Take an existing model and โtweakโ it a bit for our custom purposes
- Make the entire model โflexibleโ during training, allowing it to explore the full parameter space that it did for its initial training
- Train a smaller number of parameters than were used in the original model
LoRA
A visual representation of LoRA, courtesy of this article There are several mechanisms for PEFT, but one method that seems to achieve some of the best overall performance as of this writing is referred to as โLow Rank Adaptation,โ or LoRA. If youโd like a detailed description, hereโs a great explainer. Or if youโre academically inclined, you can go straight to the original paper on the technique. Modern language models have many layers that perform different operations. Each one takes the the output tensors of the previous layers to produce the output tensors for the layers that follow. Many (though not all) of these layers have one or more trainable matrices that control the specific transformation they will apply. Considering just a single such layer with one trainable matrix W, we can consider our fine-tuning to be looking for a matrix we can add to the original, W , to get the weights for the final model: Wโ = W + W. If we just looked to find W directly, weโd have to use just as many parameters as were in the original layer. But if we instead define W as the product of two smaller matrices W = A X B, we can potentially have far fewer parameters to learn. To see how the numbers work out, letโs say W is an NxN matrix. Given the rules of matrix multiplication, A must have N rows, and B must have N columns. But we get to choose the number of columns in A and the number of rows in B as we see fit (so long as they match up!). So A is an Nxr matrix and B is an rxN matrix. The number of parameters in W is Nยฒ, but the number of parameters in A & B is Nr + rN = 2Nr. By choosing an r thatโs much less than N, we can reduce the number of parameters we need to learn significantly! So why not just always choose r=1? Well, the smaller r is, the less โfreedomโ there is for what W can look like (formally, the less independent the parameters of W will be). So for very small r values, we might not be able to capture the nuances of our problem domain. In practice, we can typically achieve significant reductions in learnable parameters without sacrificing performance on our target problem. As one final aside down this technical section (no more math after this, I promise!), you could imagine that after tuning we might want to actually represent W as W = โบ**(AXB)**, with โบ as a scaling factor for our decomposed weights. Setting it to 1 would leave us with the same ratio of โoriginal modelโ behavior to โtuned modelโ behavior as we had during training. But we might want to amplify or suppress these behaviors relative to one another in prod. The above should help give you some intuition for what youโre doing as you play around with the hyperparameters for LoRA, but to summarize at a high level, LoRA will require the following hyperparameters that will have to be determined via experimentation:- r: the free dimension for decomposing the weight matrices into smaller factors. Higher values will increase the generalization of the fine-tuning, but at the cost of increasing the computational resources (compute, memory, and storage) required for the tuning. In practice, values as low as 1 can do the trick, and values greater than around 64 generally seem to add little to the final performance.
- layer selection: as mentioned, not all layers can be tuned at all, nor do all layers have a 2d tensor (aka a matrix) as their parameters. Even for the layers that do meet our requirements, we may or may not want/need to fine-tune all of them.
- โบ: a factor controlling how much of the tuned behavior will be amplified or suppressed once our model is done training and ready to perform evaluation.
Selecting a Model
Now that weโve decided to fine-tune an existing model using LoRA, we need to choose which model(s) we will be tuning. In our goals, we mentioned working with different compute constraints. We also decided that we would be focusing on summarization tasks. ย Rather than simply extending a sequence of text (so called โcausal language modeling,โ the default approach used by the GPT class of models), this task looks more like taking one input sequence (the thing to summarize) and producing one output sequence (the summary). Thus we might require less fine-tuning if we pick a model designed for โsequence to sequenceโ language modeling out of the box. However, many of the most powerful language models available today use Causal Language Modeling, so we might want to consider something using that approach and rely on fine-tuning and clever prompting to teach the model that we want it to produce an output sequence that relates to the input one.FLAN-T5
Google has released a language model known as FLAN-T5 that:- Is trained on a variety of sequence-to-sequence tasks
- Comes in a variety of sizes, from something that comfortably runs on an M1 Mac to something large enough to score well on competitive benchmarks for complex tasks
- Is licensed for open-source usage (Apache 2)
- Has achieved โstate-of-the-art performance on several benchmarksโ (source)
Llama 2
While this model is a causal language model, and thus might require more fine-tuning, it:- Has ranked at the top of many benchmarks for models with comparable numbers of parameters
- Is licensed for open-source usage (Apache 2)
- Comes in a variety of sizes, to suit different use cases and constraints
GPT-J 6B
This model is another causal language model. It:- comes from the well-known GPT class of models
- has achieved solid performance on benchmarks
- and has a number of parameters that puts it solidly in the class of large language models while remaining small enough to play around with on a single cloud VM without breaking the bank
Selecting some frameworks
Now that we have all the academic stuff out of the way, its time for the rubber to meet the road with some actual tooling. Our goals cover a lot of territory. We need to find tools that help us:- Manage (retrieve, store, track) our models
- Interface with hardware
- Perform the fine-tuning
- Perform some experimentation as we go through the fine-tuning process. This might include:
- tracking the experiments weโve performed
- visualizing the elements of our experiments
- keeping references between our configurations, models, and evaluation results
- allowing for a rapid โtry a prompt and get the outputโ loop
- Prepare us for productionizing the process that produces our final model
Hugging Face
The biggest workhorse in our suite of tools will be Hugging Face. They have been in the language modeling space since long before โLLMโ was on everyoneโs lips, and theyโve put together a suite of interoperable libraries that have continued to evolve along with the cutting edge.The Hub
One of Hugging Faceโs most central products is the Hugging Face Hub. What GitHub is for source code, Hugging Face Hub is for models, datasets, and more. Indeed, it actually uses git (plus git-lfs) to store the objects it tracks. It takes the familiar concepts of repositories, repository owners, and even pull-requests, and uses them in the context of datasets and models. Hereโs the repository tree for the base FLAN-T5 model, for example. Many state-of-the-art models and datasets are hosted on this hub.Transformers
Another keystone in the Hugging Face suite is their transformers library. It provides a suite of abstractions around downloading and using pre-trained models from their hub. It wraps lower-level modeling frameworks like PyTorch, TensorFlow, and JAX, and can provide interoperability between them.Accelerate
The next piece of the Hugging Face toolkit weโll be using is their Accelerate library, which will help us be able to effectively leverage the resource provided by different hardware configurations without too much extra configuration. If youโre interested, accelerate can also be used to enable distributed training when starting from non-distributed PyTorch code.PEFT
A new kid on the proverbial Hugging Face block is PEFT. Recall this acronym for โParameter Efficient Fine Tuningโ from above? This library will allow us to work with LoRA for fine tuning, and treat the matrices that generate the weight deltas as models (sometimes referred to as adaptors) in their own right. That means we can upload them to the Hugging Face Hub once weโre satisfied with the results. It also supports other fine-tuning methods, but for our purposes weโll stick with LoRA.Sematic
Sematic will help us track & visualize our experiments, keep references between our configurations/models/evaluation results, and prepare us for productionization. Sematic not only handles experiment management, but is also a fully-featured cloud orchestration engine targeted at ML use cases. If we start with it for our local development, we can move our train/eval/export pipeline to the cloud once weโre ready to do so without much overhead.Gradio
Thereโs still one piece missing: ideally once weโve trained a model and gotten some initial evaluation results, weโd like to be able to interactively feed the model inputs and see what it produces. Gradio is ideally suited for this task, as it will allow us to develop a simple app hooked up to our model with just a few lines of python.Tying it all together
Armed with this impressive arsenal of tooling, how do we put it all together? We can use Sematic to define and chain together the steps in our workflow just using regular python functions, decorated with the @sematic.func decorator. The Sematic code for defining our pipeline. The rest of the code defining the steps in our workflow can be found here. This will give us:- A graph view to monitor execution of the experiment as it progresses through the various steps
- A dashboard to keep track of our experiments, notes, inputs, outputs, source code, and more. This includes links to the resources weโre using/producing on Hugging Face Hub, navigable configuration & result displays. Sematic EE users can get access to even more, like live metrics produced during training and evaluation.
- A search UI to track down specific experiments we might be interested in
- The basic structure we need to scale our pipeline up to cloud scale. When weโre ready, we can even add distributed inference using Sematicโs integration with Ray.
Results
Weโve set up this script so that via the command line we use to launch, we can change:- The model (selecting from one of the FLAN-T5 variants or GPT-J 6B)
- The training hyperparameters
- The dataset used
- The Hugging Face repo to export the result to, if we even want to export the result
CNN Daily Mail Article Summarization
The default dataset used by our pipeline is cnn_dailymail, from Hugging Face. This contains some articles from CNN paired with summaries of those articles. Using FLAN-T5 large variant, we were able to produce some good summaries, such as the one below. Not all results were perfect though. For example, the one below contains some repetition and misses some key information in the summary (like the name of the headliners).Amazon Review Headline Suggestion
To demonstrate the flexibility that can be achieved with fine-tuning, we also used a fairly different use case for our second tuning. This time we leveraged the amazon_us_reviews dataset, pairing a review with the reviewโs headline, which could be considered a summary of the reviewโs content.Try it out yourself!
Think this example might actually be useful to you? Itโs free and open-source! All you need to do to use it is install Sematic 0.32.0$ pip install sematic
$ sematic start
$ sematic run examples/summarization_finetune -- --help
Enjoyed this article? Sign up for our newsletter to receive regular insights and stay connected.
