# Rethinking Learning Rate Tuning in the Era of Language Models

The learning rate is a critical hyperparameter that determines the step size used to <mark style="color:yellow;">update the model's weights during optimization</mark>.&#x20;

The authors of this <mark style="color:blue;">**December 2023**</mark> paper highlight the recent success of LMs and the trend of fine-tuning pre-trained LMs for various applications due to the high costs associated with training LMs from scratch.

They emphasize the importance of the learning rate as a critical hyperparameter in LM fine-tuning, which directly impacts both fine-tuning efficiency and the quality of the fine-tuned LMs.

The authors argue that existing learning rate policies, primarily designed for <mark style="color:blue;">traditional deep neural networks (DNNs),</mark> may not work well for LM fine-tuning, and there is a need to reassess the research challenges and opportunities in learning rate tuning for LMs.

{% embed url="<https://arxiv.org/abs/2309.08859>" %}

Based on the analysis provided in the paper, there are several key challenges associated with choosing the right learning rate, both for traditional deep neural network (DNN) training and large language model (LLM) fine-tuning:

#### <mark style="color:green;">Learning rate directly impacts training effectiveness and model accuracy</mark>

The learning rate is one of the most critical hyperparameters in both DNN training and LM fine-tuning. It controls the <mark style="color:yellow;">magnitude of gradients updated on the model parameters,</mark> allowing the optimizer to adjust the learning speed for each iteration. Choosing an inappropriate learning rate can significantly impair the training process and the resulting model's performance.

#### <mark style="color:green;">Difficulty in finding the optimal learning rate</mark>

It can be a daunting task to identify a good learning rate. If the learning rate is too small, the model may fail to converge or progress very slowly. On the other hand, if the learning rate is too large, the model may overshoot the optimal solution, leading to suboptimal performance or even divergence.

#### <mark style="color:green;">Trial-and-error approach is time-consuming and expensive</mark>

The conventional method for finding the right learning rate involves manual tuning, where different learning rates are tried one at a time. This <mark style="color:yellow;">trial-and-error approach is tedious, time-consuming, and highly expensive,</mark> especially for LLMs with their high complexity and fine-tuning costs.

#### <mark style="color:green;">Lack of systematic studies for LLM fine-tuning</mark>

While there has been research on learning rate policies for traditional DNN training, there is a lack of systematic studies on how to achieve high efficiency and high accuracy in LLM fine-tuning.&#x20;

Most existing LLM fine-tuning methods still follow similar assumptions and principles to traditional deep learning training/fine-tuning, which may not be optimal given the unique characteristics of LLMs.

### <mark style="color:purple;">Key Differences between Language Models and Traditional Deep Neural Networks</mark>

The paper discusses several key differences between LMs and traditional DNNs, including:

<mark style="color:green;">Model complexity:</mark> LMs have <mark style="color:yellow;">billions of parameters,</mark> while traditional DNNs have millions

<mark style="color:green;">Training/fine-tuning costs:</mark> LM <mark style="color:yellow;">training/fine-tuning is much more expensive</mark> than traditional DNN training

<mark style="color:green;">Model initialization:</mark> LM fine-tuning often <mark style="color:yellow;">starts from pre-trained models,</mark> while traditional DNN training starts from random initialisation.&#x20;

<mark style="color:green;">Training epochs:</mark> LM training/fine-tuning <mark style="color:yellow;">requires fewer epochs (2-3)</mark> compared to traditional DNN training (100+).

<mark style="color:green;">Evaluation strategies:</mark> LM evaluation may involve time-consuming real-world tasks, while traditional DNNs can be quickly evaluated on testing data.

These differences highlight the need to rethink the learning rate tuning paradigm for LLMs.

### <mark style="color:purple;">LRBench++</mark>

The authors present LRBench++, a <mark style="color:yellow;">learning rate benchmarking and tuning tool</mark> for both traditional DNNs and LLMs.&#x20;

LRBench++ provides support for LLM implementations, iteration-based LR tuning, and systematic NLP learning tasks for evaluating LLMs.&#x20;

It also incorporates popular hyperparameter tuning algorithms, such as grid search and random search, and supports different LR tuning methods for various training/fine-tuning objectives.

<figure><img src="https://1839612753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FpV8SlQaC976K9PPsjApL%2Fuploads%2FLpIVe0NbxylUxqQ3Gwux%2Fimage.png?alt=media&#x26;token=12ea5a9c-c53f-4574-aa56-38f70d152ed1" alt=""><figcaption><p>Core Components of LRBench++</p></figcaption></figure>

The LRBench++ system is an enhanced version of LRBench designed for evaluating, selecting, and tuning learning rate (LR) policies to optimise both traditional deep neural network (DNN) training and language model (LM) fine-tuning.&#x20;

It incorporates several core components and features to address the unique challenges associated with LM training and fine-tuning. Let's dive into the details of each component and how they work together:

### <mark style="color:purple;">LR Schedule Monitor</mark>

The LR schedule monitor <mark style="color:yellow;">continuously tracks the training/fine-tuning status</mark> of the DNN/LLM and updates the learning rate value based on the specified LR policy.

<mark style="color:green;">Functionality:</mark> It closely monitors the progress of the training/fine-tuning process, including metrics such as the current epoch, iteration, and loss values. Based on this information and the selected LR policy, the monitor adjusts the learning rate accordingly. This ensures that the LR is dynamically adapted throughout the training/fine-tuning process.

### <mark style="color:purple;">LR Policy Database</mark>

The LR policy database serves as a <mark style="color:yellow;">repository to store and organise the LR tuning results</mark> for different learning tasks.

<mark style="color:green;">Functionality:</mark> It maintains a structured collection of LR tuning results, categorized by the specific learning task (e.g., image classification, object detection, sentiment analysis). This database allows users to access and leverage previous tuning results, facilitating knowledge sharing and reducing the need for redundant tuning efforts.

### <mark style="color:purple;">LR Value Range Test</mark>

The LR value range test <mark style="color:yellow;">employs a grid search approach to determine an appropriate range of LR values,</mark> effectively narrowing down the search space.

<mark style="color:green;">Functionality:</mark> It systematically explores a predefined grid of LR values and evaluates the model's performance for each value. By analysing the results, it identifies a suitable range of LR values that yield good performance. This range serves as a starting point for further fine-grained tuning, saving computational resources and time.

### <mark style="color:purple;">LR Policy Visualizer</mark>

The LR policy visualizer provides a <mark style="color:yellow;">visual representation of the DNN/LLM training/fine-tuning status</mark> and the corresponding LR values over time.

<mark style="color:green;">Functionality:</mark> It generates intuitive visualizations, such as plots and graphs, that display the evolution of the learning rate throughout the training/fine-tuning process. These visualizations help users gain insights into the behavior of different LR policies and their impact on model performance.

### <mark style="color:purple;">LR Policy Evaluator</mark>

The LR policy evaluator <mark style="color:yellow;">assesses and compares alternative LR policies</mark> to estimate optimal LR parameters and enables dynamic LR tuning.

<mark style="color:green;">Functionality:</mark> It conducts a comprehensive evaluation of different LR policies by running multiple training/fine-tuning experiments with varying LR configurations.&#x20;

By analysing the results, it identifies the most promising LR policies and estimates the best LR parameters for a given learning task.   Additionally, it supports dynamic LR tuning, allowing for real-time adjustments based on the model's performance during training/fine-tuning.

### <mark style="color:purple;">LR Tuning Optimisations</mark>

LRBench++ incorporates various optimisation techniques to enhance the efficiency and scalability of the LR tuning process.

* <mark style="color:green;">Grid Search:</mark> It performs an exhaustive search over a predefined grid of LR parameter values, evaluating all possible combinations to find the optimal configuration.
* <mark style="color:green;">Random Search:</mark> It randomly samples LR parameter values from a defined search space, allowing for a more efficient exploration compared to grid search.
* <mark style="color:green;">Distributed Parallel Tuning:</mark> Built on top of Ray Tune \[30], it enables distributed and parallel execution of LR tuning experiments, leveraging multiple computing resources to accelerate the tuning process.

### <mark style="color:purple;">LRBench++ offers three LR tuning methods with different objectives</mark>

<mark style="color:green;">Single Policy LR Selection</mark>

* Aims to identify the best single LR policy that maximizes model accuracy within given training/fine-tuning constraints, such as a predefined number of iterations.

<mark style="color:green;">Multi-policy LR Composition</mark>

* Focuses on composing multiple LR policies to achieve optimal model accuracy while adhering to the specified training/fine-tuning constraints.

<mark style="color:green;">Cost-effective LR Tuning</mark>

* Seeks to minimize training costs while reaching a target accuracy threshold, balancing performance and computational resources.

By leveraging these components and features, LRBench++ provides a comprehensive platform for benchmarking and tuning learning rate policies for both traditional DNNs and LLMs.&#x20;

Its modular design allows users to utilize different modules based on their specific requirements, making it adaptable to various real-world scenarios. LRBench++ aims to serve as a public platform for the community to collect, share, and leverage LR tuning results, fostering collaboration and advancing the field of deep learning and large language models.

### <mark style="color:purple;">Discussion and Future Directions</mark>

The authors summarise common practices in LM fine-tuning settings and discuss the research challenges and opportunities in learning rate tuning for LMs, including cost-effective LR tuning, benchmarking LRs, and evaluating LM performance during training/fine-tuning.

The authors present LRBench++, a valuable tool for benchmarking and tuning learning rates for both traditional DNNs and LLMs. The experimental analysis reveals key insights into the impact of learning rates on LLM fine-tuning and provides a foundation for future research in this area.

### <mark style="color:purple;">So what learning rate should I use?</mark>

When fine-tuning, it's often recommended to use a smaller learning rate than used during pre-training, as you want to make smaller updates to the already learned weights rather than learning from scratch.

We can't always use a high learning rate because it can cause the optimisation algorithm to overshoot the optimal values of the parameters, resulting in instability and slow convergence.&#x20;

Additionally, a high learning rate can cause the loss to fluctuate wildly, making it difficult to determine whether the model is improving or not.

The function to calculate new weights using a learning rate is:

<mark style="color:yellow;">new\_weights = old\_weights - learning\_rate \* gradient</mark>

The learning rate is an important parameter, which depends on the optimiser, the model, and many more other hyperparameters.

A usual good starting point is <mark style="color:yellow;">0.1 for SGD, and 1e-3 for Adam.</mark>

The deeper the model is, the lower the learning rate usually should be. For instance, Transformer models usually apply learning rates of 1e-5 to 1e-4 for Adam.

The lower your batch, the lower the learning rate should be. Consider using gradient accumulation if your batch size is getting too small (PyTorch Lightning supports this, see here).

Consider using the PyTorch Lightning learning rate finder toolkit for an initial good guess.

### <mark style="color:purple;">Examples of Learning Rates used in fine tuning:</mark>

**Alpaca-7B**: The learning rate is $$2×10−52×10−5$$ (2e-5). The LR Scheduler used is "<mark style="color:yellow;">WarmupDecayLR</mark>". It is trained for 3 epochs with a warmup percentage of 0.03.

**Vicuna-7B**: The learning rate is $$2×10−52×10−5$$ (2e-5). It uses the <mark style="color:yellow;">"cosine" learning rate</mark> scheduler. The training duration is 3 epochs with a warmup percentage of 0.03.

**WizardLM-7B**: Here, the learning rate is also $$2×10−52×10−5$$ (2e-5), with a <mark style="color:yellow;">"cosine" LR scheduler</mark>. It's trained for 3 epochs, but the warmup is different, being 2 (which might indicate 2% or 2 epochs, depending on the context).

**GPT4All-J-6B**: The learning rate is $$2×10−52×10−5$$ (2e-5) with a "<mark style="color:yellow;">WarmupLR" scheduler.</mark> The model is trained for 2 epochs with a warmup of 500, which likely refers to 500 steps.

**Dolly-7B**: This model has a slightly lower learning rate of $$5×10−65×10−6$$ (5e-6). It also uses the "<mark style="color:yellow;">WarmupLR" scheduler</mark>, is trained for 2 epochs, and has a warmup of 50 steps.

N.B. In scientific notation, "2×10^−5" means 2 times 10 raised to the power of negative 5. This is a way to represent very small or very large numbers succinctly. Specifically, 2×10^−5 is equal to <mark style="color:yellow;">0.00002.</mark>&#x20;
