Deploy Alpaca-LoRa

Updated at:

This topic uses the deployment of Alpaca-LoRa as an example to demonstrate how to use DataCache. By using DataCache to pre-fetch the llama-7b-hf model data and alpaca-lora-7b weight data, you can directly mount them when you create the Alpaca-LoRa application instance. This method eliminates the waiting time for pulling data within the instance and accelerates the application's startup.

Background information

Alpaca-LoRa is a lightweight language model that uses LoRA technology to fine-tune the LLaMA model. It can engage in natural language conversations, generate diverse text based on user instructions, and assist with tasks such as writing, translation, and coding.

Important

Alibaba Cloud does not guarantee the legality, security, or accuracy of third-party models and is not liable for any resulting damages. Comply with the user agreements, usage specifications, and applicable laws for any third-party model you use.

Prerequisites

Your VPC must have an Internet NAT Gateway and a configured SNAT entry to allow resources within the VPC or its vSwitches to access the internet.

Note

If your VPC does not have an Internet NAT Gateway, bind an EIP when you create the DataCache and deploy the application to pull data from the internet.

Steps

Build an Alpaca-LoRa image

For legal compliance reasons, the Alpaca-LoRa image cannot be distributed directly. Build the image from the alpaca-lora repository, and then push it to an image repository.

  1. Clone the alpaca-lora repository to your local machine.

  2. Modify the requirements.txt and Dockerfile files in the repository as follows.

    requirements.txt

    accelerate
    appdirs
    loralib
    bitsandbytes
    black
    black[jupyter]
    datasets
    fire
    git+https://github.com/huggingface/peft.git
    transformers>=4.28.0
    sentencepiece
    gradio
    scipy

    Dockerfile

    FROM nvidia/cuda:11.8.0-devel-ubuntu22.04
    ARG DEBIAN_FRONTEND=noninteractive
    RUN apt-get update && apt-get install -y \
        git \
        curl \
        software-properties-common \
        && add-apt-repository ppa:deadsnakes/ppa \
        && apt install -y python3.10 \
        && rm -rf /var/lib/apt/lists/*
    WORKDIR /workspace
    COPY requirements.txt requirements.txt
    RUN curl -sS https://bootstrap.pypa.io/get-pip.py | python3.10 \
        && python3.10 -m pip install -r requirements.txt \
        && python3.10 -m pip install numpy --pre torch --force-reinstall --index-url https://download.pytorch.org/whl/nightly/cu118 \
        && python3.10 -m pip install --upgrade typing-extensions
    COPY . .
    EXPOSE 7860
  3. Use the Dockerfile to build the image.

  4. Push the image to an image repository.

Create DataCaches

  1. Get the model IDs from Hugging Face.

    This tutorial uses the following two models. After you find a model on Hugging Face, copy the model ID from the top of the model details page.

    • decapoda-research/llama-7b-hf

    • tloen/alpaca-lora-7b

  2. Create the DataCaches.

    Note

    For deployments in Chinese mainland regions, we recommend creating the DataCache in the China (Hong Kong) region and then copying it to your target region.

    1. Create a DataCache for llama-7b-hf.

      The following code provides example parameters for calling the CreateDataCache operation. These parameters pull the llama-7b-hf model data from Hugging Face and save it to the /model/llama-7b-hf directory in a bucket named test. The DataCache is named llama-7b-hf and is retained for one day.

      Important

      If you use an SDK to create a DataCache, you do not need to prefix parameters in DataSource.Options with their name length. For example, #10#repoSource becomes repoSource, and #6#repoId becomes repoId.

      {
        "RegionId": "cn-beijing",
        "SecurityGroupId": "sg-2ze63v3jtm8e6syi****",
        "VSwitchId": "vsw-2ze94pjtfuj9vaymf****",
        "Bucket": "test",
        "Path": "/model/llama-7b-hf",
        "Name": "llama-7b-hf",
        "DataSource": {
          "Type": "URL",
          "Options": {
            "#10#repoSource": "HuggingFace/Model",
            "#6#repoId": "decapoda-research/llama-7b-hf"
          }
        },
        "RetentionDays": 1
      }
    2. Create a DataCache for alpaca-lora-7b.

      The following code provides example parameters for calling the CreateDataCache operation. These parameters pull the alpaca-lora-7b model data from Hugging Face and save it to the /model/alpaca-lora-7b directory in a bucket named test. The DataCache is named alpaca-lora-7b and is retained for one day.

      Important

      If you use an SDK to create a DataCache, you do not need to prefix parameters in DataSource.Options with their name length. For example, #10#repoSource becomes repoSource, and #6#repoId becomes repoId.

      {
        "RegionId": "cn-beijing",
        "SecurityGroupId": "sg-2ze63v3jtm8e6syi****",
        "VSwitchId": "vsw-2ze94pjtfuj9vaymf****",
        "Bucket": "test",
        "Path": "/model/alpaca-lora-7b",
        "Name": "alpaca-lora-7b",
        "DataSource": {
          "Type": "URL",
          "Options": {
            "#10#repoSource": "HuggingFace/Model",
            "#6#repoId": "tloen/alpaca-lora-7b"
          }
        },
        "RetentionDays": 1
      }
  3. Query the DataCache status.

    Use the returned DataCache ID to call the DescribeDataCaches API operation to check the DataCache status. The DataCache is ready when its status (DataCaches.Status) is Available.

Deploy the Alpaca-LoRa application

  1. Use the DataCache to create an ECI instance and deploy the Alpaca-lora application.

    The following code provides example parameters for calling the CreateContainerGroup operation. These parameters create an ECI instance with an additional 20 GiB of temporary storage space and mount the llama-7b-hf and alpaca-lora-7b model data. The container in the instance uses the prepared Alpaca-lora image. The model data is mounted to /data/llama-7b-hf and /data/alpaca-lora-7b inside the container. After the container starts, it runs the command python3.10 generate.py --load_8bit --base_model /data/llama-7b-hf --lora_weights /data/alpaca-lora-7b.

    {
      "RegionId": "cn-beijing",
      "SecurityGroupId": "sg-2ze63v3jtm8e6syi****",
      "VSwitchId": "vsw-2ze94pjtfuj9vaymf****",
      "ContainerGroupName": "alpacalora",
      "DataCacheBucket": "test",
      "EphemeralStorage": 20,
      "Container": [
        {
          "Name": "alpacalora",
          "Image": "registry-vpc.cn-beijing.aliyuncs.com/******/***-registry:v1.0",
          "Command": [
            "/bin/sh",
            "-c"
          ],
          "Arg": [
            "python3.10 generate.py --load_8bit --base_model /data/llama-7b-hf --lora_weights /data/alpaca-lora-7b"
          ],
          "Cpu": 16,
          "Memory": 64,
          "Port": [
            {
              "Port": 7860,
              "Protocol": "TCP"
            }
          ],
          "VolumeMount": [
            {
              "Name": "llama-model",
              "MountPath": "/data/llama-7b-hf"
            },
            {
              "MountPath": "/data/alpaca-lora-7b",
              "Name": "alpacalora-weight"
            }
          ]
        }
      ],
      "Volume": [
        {
          "Type": "HostPathVolume",
          "HostPathVolume.Path": "/model/llama-7b-hf",
          "Name": "llama-model"
        },
        {
          "Type": "HostPathVolume",
          "HostPathVolume.Path": "/model/alpaca-lora-7b",
          "Name": "alpacalora-weight"
        }
      ]
    }
  2. Confirm the application deployment status.

    Use the returned instance ID to call DescribeContainerGroupStatus to query the status of the instance and its containers. When the instance status (Status) and the container status (ContainerStatuses.State) are both Running, the instance is created and the container is running.

  3. Expose the application to the internet.

    If the ECI instance's VPC is associated with a public NAT gateway, you need to create a DNAT entry to expose the ECI instance to the internet. For more information, see Create and manage DNAT entries.

    Note

    If the VPC is not associated with a public NAT gateway, you must associate an EIP with the ECI instance when you create it. In this case, you can skip this step.

    Configure the DNAT entry parameters as follows:

    • Public IP: Select the EIP address associated with the public NAT gateway.

    • Private IP Address: Select the ECI instance running the Alpaca-lora application.

    • Port Settings: Set the public port to 80, the private port to 7860, and the protocol to TCP.

Test the model

  1. Add an inbound rule to the ECI instance's security group to open the application's port.

  2. Open a browser and access the Alpaca-LoRa application.

    If you have configured a DNAT entry, enter the public IP address and public port specified in the DNAT entry. Example: 47.94.XX.XX:80.

    Note

    If the ECI instance is associated with a standalone EIP, enter the EIP address and the port exposed by the container. Example: 47.94.XX.XX:7860.

  3. Enter text to test the model.

    In Alpaca-LoRA's Gradio inference UI, enter Tell me about Alpaca-LoRA in the Instruction field and none in the Input field. Adjust the following parameters: set Temperature to 0.1, Top p to 0.75, Top k to 40, Beams to 4, and Max tokens to 128. Click Submit. The Output area displays the model-generated text that describes the Alpaca-LoRA project.