Setting up a Hugging Face local model is a straightforward process. To begin, you'll need to install the Transformers library, which can be done using pip with the command `pip install transformers`.
The Hugging Face local model setup involves specifying the model architecture, tokenizer, and device. This is achieved by importing the necessary modules and defining the model, tokenizer, and device using the `AutoModelForSequenceClassification`, `AutoTokenizer`, and `torch.device` classes, respectively.
To configure the model, you'll need to set the model's parameters, such as the hidden size and number of layers. This can be done using the `config` attribute of the model object, which provides access to the model's configuration.
The local model setup also requires specifying the data pipeline, which involves loading the dataset and preparing it for training. This can be achieved using the `Dataset` class from the `torch.utils.data` module.
A unique perspective: Velocity Model Prediciton Using Generative Ai
Loading and Configuration
Loading models from Hugging Face into LocalAI involves configuring LocalAI to point to a specific URL containing a YAML configuration file for the model you want to use. This process can be simplified by utilizing LocalAI's built-in model configurations.
You can use the following command to load the model using the YAML configuration: This command tells LocalAI to preload the models defined in your YAML file, ensuring they're ready for use when you start the application.
A YAML configuration file is essential for specifying model details, with a basic structure looking like this: This example points to a model hosted on GitHub, and you can customize the URL to point to any model configuration you have access to.
On a similar theme: How to Use Huggingface Models in Python
Loading
Loading models into LocalAI is a straightforward process, especially when using Hugging Face models. You can configure LocalAI to point to a specific URL that contains a YAML configuration file for the model you want to use.
To preload models defined in your YAML file, use the following command in LocalAI: This command ensures the models are ready for use when you start the application.
Loading a model from Hugging Face can be done using a simple code snippet that initializes LocalAI and loads the specified model. This allows you to start using the model for your applications.
Here's an interesting read: How to Use Hugging Face Models
Configuring LocalAI for external model sources like Hugging Face requires setting up a YAML configuration file that points to the desired model. This allows LocalAI to load models seamlessly without manual intervention.
If you frequently load a model from different or restarted clusters, consider caching the model in the DBFS root volume or on a mount point. This can decrease ingress costs and reduce the time to load the model on a new or restarted cluster.
To cache the model, set the TRANSFORMERS_CACHE environment variable in your code before loading the pipeline.
Additional reading: How to Load a Model in Mixed Precision in Huggingface
Configuration
Configuring LocalAI for optimal performance is a crucial step in unlocking its full potential. You can specify model parameters in a YAML file, which allows LocalAI to understand how to load and utilize the specified model.
A YAML configuration file is essential for specifying model details. It should have a basic structure that includes the model URL, as shown in the example.
LocalAI comes with several pre-built model configurations embedded in the binary, which simplifies the process of getting started with popular models. You can find these configurations in the LocalAI documentation under the Model customization section.
To effectively configure LocalAI for external model sources, you need to set up a YAML configuration file that points to the desired model. This allows LocalAI to load models seamlessly without manual intervention.
Here are some key considerations for configuring LocalAI:
- Specify model parameters in a YAML file.
- Use a YAML configuration file to specify model details.
- Take advantage of pre-built model configurations embedded in the binary.
- Set up a YAML configuration file for external model sources.
The Trainer classes in Hugging Face training configuration tools require you to provide metrics, a base model, and a training configuration. You can configure evaluation metrics in addition to the default loss metric that the Trainer computes.
Curious to learn more? Check out: Huggingface Training Service
Preparing Data
Preparing data is a crucial step in training a Hugging Face local model. You'll need to transform text labels into N-hot encoded arrays to classify images.
To start, identify the unique labels in your dataset. This will help you create the necessary arrays. The labels can be provided in a metadata.jsonl file, which is a convenient way to store image file names and their associated labels.
The dataset can be converted to the Arrow file format, which allows for quick loading during training and validation. This step can take a few minutes, but it's worth it for the efficiency boost.
A fresh viewpoint: Training an Ai Model
Create the Dataset
Creating the dataset is a crucial step in preparing your data for training. This process can take a few minutes because the entire dataset is being loaded and pre-processed.
To fine-tune a pre-trained model, your new dataset must have the same properties as the original dataset used for pre-training. This is done by using the AutoFeatureExtractor to load the original dataset information from a config file.
The X-ray images in your dataset need to be resized to the correct resolution, which is 224x224 pixels. They also need to be converted from grayscale to RGB.
For the model to run efficiently, images need to be batched. This is achieved by defining a batch_sampler function that returns batches of images and labels in a dictionary.
Batching images allows the model to process multiple images at once, which can significantly speed up the training process.
For another approach, see: Huggingface Interview Process
Preparing the Labels
Preparing the labels is a crucial step in training a model, and in this case, we're working with a dataset that has 14 diseases and a "No Finding" label. The goal is to transform the text labels into N-hot encoded arrays, which represent the multiple labels needed to classify each image.
We start by identifying the unique labels in the dataset, which can be done using the datasets.load_dataset function. This function allows us to load the data and extract the unique labels.
The labels need to be transformed into N-hot encoded arrays, which are lists of booleans indicating whether a label corresponds to the image or not. This is done to make the labels compatible with the Hugging Face library.
We have chosen to use a metadata.jsonl file to store the image file names and their associated labels, as the images in this dataset can have multiple labels. This approach allows us to easily load the data and access the labels.
See what others are reading: Huggingface Load Model from S3
Preparing the
To prepare your data for training, you'll need to import the model from Hugging Face. Importing the ViT model from Hugging Face is the first step.
Now we import the ViT model from Hugging Face, specifically the Graphcore/vit-base-ipu configuration, which can be found at https://huggingface.co/Graphcore.
The IPUConfig object gives control to all the parameters specific to Graphcore IPUs. We load the IPU configuration, IPUConfig, to use this model on the IPU.
To use the IPU, we need to set our training hyperparameters using IPUTrainingArguments. This subclasses the Hugging Face TrainingArguments class, adding parameters specific to the IPU and its execution characteristics.
Batch Size
Batch size is a crucial factor to consider when preparing data for a model. It's recommended to try various batch sizes to find the best performance.
A batch size of 1 might not use the resources available to the workers efficiently, so it's worth experimenting with larger batch sizes. Databricks suggests tuning the batch size to the model and hardware in the cluster.
To find the optimal batch size, you'll want to aim for a size that drives full GPU utilization without causing CUDA out-of-memory errors. This might require some trial and error.
Monitoring GPU performance is key to achieving this balance. You can do this by viewing live cluster metrics and choosing metrics like gpu0-util for GPU processor utilization or gpu0_mem_util for GPU memory utilization.
Detaching and reattaching the notebook can help release memory used by the model and data in the GPU when you encounter CUDA out-of-memory errors during tuning.
Explore further: Fastapi Huggingface Gpu
Training and Evaluation
To train and evaluate a Hugging Face local model, you need to run the evaluation using the validation dataset. This will give you the validation AUC_ROC score after 3 epochs.
You can configure the training configuration using Hugging Face training configuration tools. This requires providing metrics, a base model, and a training configuration. The Trainer classes need to be configured with these parameters.
The Trainer classes require metrics, a base model, and a training configuration to be provided. You can configure evaluation metrics in addition to the default loss metric. For text classification, use AutoModelForSequenceClassification to load a base model for text classification.
Here are the required parameters to configure the Trainer:
- Metrics
- A base model
- A training configuration
You can add accuracy as a metric to the Trainer by configuring the evaluation metrics. Using a data collator like DataCollatorWithPadding can give good baseline performance for text classification.
Run the Training
To run the training, you can load the last checkpoint if it exists. This can significantly accelerate the training process.
The Trainer classes require you to provide metrics, a base model, and a training configuration. You can configure evaluation metrics in addition to the default loss metric.
To add accuracy as a metric, you can use the TrainingArguments class. This class allows you to specify the output directory, evaluation strategy, learning rate, and other parameters.
For text classification, you can use AutoModelForSequenceClassification to load a base model. When creating the model, provide the number of classes and the label mappings created during dataset preparation.
Using a data collator batches input in training and evaluation datasets. DataCollatorWithPadding gives good baseline performance for text classification.
Here are the required parameters for creating a Trainer:
- Metrics
- A base model (e.g., AutoModelForSequenceClassification)
- A training configuration (e.g., TrainingArguments)
These parameters are essential for creating a Trainer that can run the training process efficiently.
Run the Evaluation
Now that you've trained your model, it's time to see how well it performs on unseen data.
After training the model, you can evaluate its ability to predict labels using the validation dataset. The metrics will show you the validation AUC_ROC score your tutorial achieves after a certain number of epochs.
There are several directions to explore to improve the accuracy of your model. One option is to train the model for longer.
Changing optimizers, learning rate, learning rate schedule, loss scaling, or using auto-loss scaling might also improve the validation performance.
Recommended read: Difference between Model and Algorithm in Machine Learning
Frequently Asked Questions
Are Hugging Face transformers local?
Yes, Hugging Face transformers are local, stored in the user's cache directory at ~/.cache/huggingface/hub. This local caching is managed by the TRANSFORMERS_CACHE environment variable.
Can you run Bert locally?
Yes, you can run a fine-tuned BERT model locally, and we've covered the steps to do so in our blog post.
How to use Hugging Face models offline?
To use Hugging Face models offline, download and save your files ahead of time using PreTrainedModel.from_pretrained() and PreTrainedModel.save_pretrained().
Sources
- https://www.restack.io/p/localai-answer-load-local-model-huggingface-cat-ai
- https://docs.databricks.com/ja/archive/machine-learning/train-model/model-inference-nlp.html
- https://docs.graphcore.ai/projects/tutorials/en/latest/pytorch/vit_model_training/README.html
- https://pytorch.org/blog/Accelerating-Hugging-Face-and-TIMM-models/
- https://docs.databricks.com/en/machine-learning/train-model/huggingface/fine-tune-model.html
Featured Images: pexels.com