With the Generative AI (GenAI) revolution in full swing, text-generation with open-source transformer models like Llama 2 has become the talk of the town. AI enthusiasts as well as developers are looking to leverage the generative abilities of such models for their own use cases and applications. This article shows how easy it is to generate text with the Llama 2 family of models (7b, 13b and 70b) using Optimum Habana and a custom pipeline class – you'll be able to run the models with just a few lines of code!
In a recent development, this custom pipeline class has been designed to offer great flexibility and ease of use. Moreover, it provides a high level of abstraction and performs end-to-end text-generation which involves pre-processing and post-processing. There are multiple ways to use the pipeline - you can run the run_pipeline.py script from the Optimum Habana repository, add the pipeline class to your own python scripts, or initialize LangChain classes with it. (Related: Breaking: University rolls out $5m smart system that ch...)
Since the Llama 2 models are part of a gated repo, you need to request access if you haven't done it already. First, you have to visit the Meta website and accept the terms and conditions. After you are granted access by Meta (it can take a day or two), you have to request access in Hugging Face, using the same email address you provided in the Meta form.
After you are granted access, please login to your Hugging Face account by running the following command (you will need an access token, which you can get from your user profile page):. (Related: Intel Diamond Rapids shows signs of life in SC26 demo r...)
Sources indicate that you also need to install the latest version of Optimum Habana and clone the repo to access the pipeline script. Here are the commands to do so:.
It has been revealed that in case you are planning to run distributed inference, install DeepSpeed depending on your SynapseAI version. In this case, I am using SynapseAI 1.14.0.
Now you are all set to perform text-generation with the pipeline!
First, go to the following directory in your optimum-habana checkout where the pipeline scripts are located, and follow the instructions in the README to update your PYTHONPATH.
If you wish to generate a sequence of text from a prompt of your choice, here is a sample command.
You can also pass multiple prompts as input and change the temperature and top_p values for generation as follows.
For generating text with large models such as Llama-2-70b, here is a sample command to launch the pipeline with DeepSpeed.
You can use the pipeline class in your own scripts as shown in the example below. Run the following sample script from optimum-habana/examples/text-generation/text-generation-pipeline.
Recent findings suggest that you will have to run the above script with python .py --model_name_or_path a_model_name as --model_name_or_path is a required argument. However, the model name can be programmatically changed as shown in the python snippet.
This shows us that the pipeline class operates on a string input and performs data pre-processing as well as post-processing for us.
In a recent development, the text-generation pipeline can be fed as input to LangChain classes via the use_with_langchain constructor argument. You can install LangChain as follows.
Here is a sample script that indicates how the pipeline class can be used with LangChain.
The pipeline class has been validated for LangChain version 0.0.191 and may not work with other versions of the package.
We presented a custom text-generation pipeline on Intel® Gaudi® 2 AI accelerator that accepts single or multiple prompts as input. This pipeline offers great flexibility in terms of model size as well as parameters affecting text-generation quality. Furthermore, it is also very easy to use and to plug into your scripts, and is compatible with LangChain.
It has been revealed that use of the pretrained model is subject to compliance with third party licenses, including the “Llama 2 Community License Agreement” (LLAMAV2). For guidance on the intended use of the LLAMA2 model, what will be considered misuse and out-of-scope uses, who are the intended users and additional terms please review and read the instructions in this link https://ai.meta.com/llama/license/. Users bear sole liability and responsibility to follow and comply with any third party licenses, and Habana Labs disclaims and will bear no liability with respect to users’ use or compliance with third party licenses. To be able to run gated models like this Llama-2-70b-hf, you need the following:.