---
title: Stable Code Large Language Model Inference Workload on Vultr Cloud GPU
url: https://docs.vultr.com/stable-code-large-language-model-inference-workload-on-vultr-cloud-gpu
description: Learn how to efficiently deploy and run Stable Code large language model inference workloads on Vultr Cloud GPU infrastructure for optimal performance and cost-effectiveness.
publish_date: 2024-01-22T14:21:57.884569Z
last_updated: 2024-01-21T18:30:00Z
author: Mirdul Swarup
header_image: https://assets.vultrdevrel.com/docs-main-doc-assets-1/1743/78a8d2ffd019926e.png
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

## Introduction

[Stable Code 3B](https://huggingface.co/stabilityai/stable-code-3b) by Stability AI is a 3 billion parameter model capable of performing on devices with hardware limitations, Its performance is comparable to counterparts double the size. It also has [fill-in-the-middle](https://arxiv.org/abs/2207.14255) capability and is trained in 18 programming languages.

[StableCode Instruct Alpha 3B](https://huggingface.co/stabilityai/stablecode-instruct-alpha-3b) and [StableCode Completion Alpha 3B](https://huggingface.co/stabilityai/stablecode-completion-alpha-3b) by Stability AI are two more coding LLMs that can perform inference workloads on edge devices. The former can generate code according to the instructions provided and later has the capability of code completion. All three models are under non commercial license that means you cannot use these models for any other purposes than research without enrolling for StabilityAI membership.

In this article, you are to perform inference workloads on the 3 models mentioned above, and compare their tokens per second generating capability and their VRAM consumptions respectively.

## Prerequisites

* Deploy a fresh [Ubuntu 22.04 A100 Vultr Cloud GPU server](https://www.vultr.com/products/cloud-gpu/nvidia-a100/) with at least `80 GB` GPU RAM.
* Securely [access the server using SSH](https://www.vultr.com/docs/how-to-use-ssh-with-vultr-servers/) as a [non-root sudo user](https://www.vultr.com/docs/how-to-use-sudo-on-a-vultr-cloud-server/).
* [Update the server.](https://www.vultr.com/docs/how-to-update-a-vultr-cloud-server/)
* Install JupyterLab and PyTorch.

## Stable Code 3B Inference

In the section, you are to perform inference workload on the Stablecode 3B model by installing the required dependencies, initializing the model and tokenizer, providing a response to the model for code completion, and calculating tokens per second generated by the model.

You can follow along with the demonstration by using the commands given in the [Stable-code-3B](https://colab.research.google.com/drive/1IU5Cu7Qyf9TuCYLCa_Z75qp_8guFF9E-?usp=sharing) Jupyter Notebook.

1. Install the dependency packages.

    ```console
    $ pip install tiktoken transformers accelerate
    ```

1. Open a terminal within the Jupyter lab interface.

    ![Image of terminal](https://assets.vultrdevrel.com/docs-main-doc-assets-1/1743/0174b02a1f0432d4.png)

1. Open a new Notebook session, and set its name to `Stable-code-3b`.

    ![Image of new notebook](https://assets.vultrdevrel.com/docs-main-doc-assets-1/1743/c406cb9fd692878e.png)

1. Initialize the model and tokenizer.

    ```python
    import torch
    from transformers import AutoModelForCausalLM, AutoTokenizer
    import time

    tokenizer = AutoTokenizer.from_pretrained("stabilityai/stable-code-3b", trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(
        "stabilityai/stable-code-3b",
        trust_remote_code=True,
        torch_dtype="auto",
    )
    model.cuda()
    ```

    In the above code block, you are initializing the `stable-code-3b`  model with its tokenizer using the HuggingFace pipeline.

1. Give input and tokenize the input.

    ```python
    input_text = "import torch\nimport torch.nn as nn"
    inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
    ```

    The model will try to complete the prompt provided above in the code block.

1. Define model parameters and initialize time calculation.

    ```python
    start_time = time.time()
    tokens = model.generate(
        **inputs,
        max_new_tokens=48,
        temperature=0.2,
        do_sample=True,
    )
    end_time = time.time()
    ```

    In the above code block, you are initializing time calculation functions that will calculate tokens per second, and you are initializing model parameters that can be modified to change model response.

1. Print generated text.

    ```python
    generated_text = tokenizer.decode(tokens[0], skip_special_tokens=True)
    print(generated_text)
    ```

1. Calculate and print tokens per second

    ```python
    num_tokens_generated = len(tokens[0])
    execution_time = end_time - start_time
    tokens_per_second = num_tokens_generated / execution_time

    print(f"Tokens per second: {tokens_per_second:.2f}")
    ```

    In the above code block, you are calculating tokens per second generated by the model.

    Stablecode 3B generates an average of **35 tokens per second**.

## Access HuggingFace Hub and Gated Repositories

StableCode Instruct Alpha 3B and StableCode Completion Alpha 3B both are gated models so to perform inference workloads you need to have access to their repositories.

1. Visit the settings menu of your HuggingFace account.

1. Select Access tokens from the left-hand side of the menu.

1. Click **New token** and type in a name for the token.

1. Click **Generate a token**, and copy and paste the token into your clipboard.

1. In your existing collab notebook, install the required dependency.

    ```python
    !pip install --upgrade huggingface-hub
    ```

1. Gain access to the models

    ```python
    from huggingface_hub import login
    login(token="YOUR_HF_TOKEN")
    ```

    Please make sure to replace the token field with the actual value in your clipboard, not doing so will result in authentication errors in the steps performed further.

## Stable Code 3B Instruct Alpha Inference

In the section, you are to perform inference workload on the Stablecode Instruct Alpha 3B model by installing the required dependencies, initializing the model and tokenizer, providing a response to the model to generate code according to the instruction provided, and calculating tokens per second generated by the model.

You can follow along with the demonstration by using the commands given in the [Stablecode-instruct-alpha-3B](https://colab.research.google.com/drive/1T1CMUBctno8lD5VhtT7NBH8a28dqw32P?usp=sharing) Jupyter Notebook.

1. Open a new Notebook and set its name to `Stablecode-instruct-alpha-3b`.

    ![Image of new notebook](https://assets.vultrdevrel.com/docs-main-doc-assets-1/1743/c406cb9fd692878e.png)

1. Navigate to the Kernel menu option in your Jupyter Notebook, and click **Shutdown Down All Kernels** to clear GPU memory.

    ![Notebook image](https://assets.vultrdevrel.com/docs-main-doc-assets-1/1743/4fab16dc17578a34.png)

1. Initialize the model and tokenizer.

    ```python
    from transformers import AutoModelForCausalLM, AutoTokenizer
    import time

    tokenizer = AutoTokenizer.from_pretrained("stabilityai/stablecode-instruct-alpha-3b")
    model = AutoModelForCausalLM.from_pretrained(
        "stabilityai/stablecode-instruct-alpha-3b",
        trust_remote_code=True,
        torch_dtype="auto",
    )
    model.cuda()
    ```

    In the above code block, you are initializing the `stablecode-instruct-alpha-3b` model along with its tokenizer.

1. Give input and tokenize the input.

    ```python
    instruction = "###Instruction\nGenerate a python function to find number of CPU cores###Response\n"
    inputs = tokenizer(instruction, return_tensors="pt", return_token_type_ids=False).to("cuda")
    ```

    The model will try to generate an output according to the instructions provided.

1. Define model parameters and initialize time calculation.

    ```python
    start_time = time.time()
    tokens = model.generate(
        **inputs,
        max_new_tokens=48,
        temperature=0.2,
        do_sample=True,
    )
    end_time = time.time()
    ```

1. Print generated response.

    ```python
    generated_code = tokenizer.decode(tokens[0], skip_special_tokens=True)
    print(generated_code)
    ```

1. Calculate and print tokens per second.

    ```python
    num_tokens_generated = len(tokens[0])
    execution_time = end_time - start_time
    tokens_per_second = num_tokens_generated / execution_time

    print(f"Tokens per second: {tokens_per_second:.2f}")
    ```

    The tokens per second generated by the StableCode Instruct Alpha 3B model is an average of **30 tokens per second**.

## Stable Code 3B Completion Alpha Inference

In the section, you are to perform inference workload on the StableCode Completion Alpha 3B model by installing the required dependencies, initializing the model and tokenizer, providing a response to the model for code completion, and calculating tokens per second generated by the model.

You can follow along with the demonstration by using the commands given in the [Stablecode-completion-alpha-3B](https://colab.research.google.com/drive/1u4RzOVvxFtVKsPDTy2HkNINnJG5FMB2x?usp=sharing) Jupyter Notebook.

1. Open a new Notebook and set its name to `Stablecode-completion-alpha-3b`.

    ![Image of new notebook](https://assets.vultrdevrel.com/docs-main-doc-assets-1/1743/c406cb9fd692878e.png)

1. Navigate to the Kernel menu option in your Jupyter Notebook, and click **Shutdown Down All Kernels** to clear GPU memory.

    ![Notebook image](https://assets.vultrdevrel.com/docs-main-doc-assets-1/1743/4fab16dc17578a34.png)

1. Initialize the model and tokenizer.

    ```python
    import torch
    from transformers import AutoModelForCausalLM, AutoTokenizer
    import time

    
    tokenizer = AutoTokenizer.from_pretrained("stabilityai/stablecode-completion-alpha-3b")
    model = AutoModelForCausalLM.from_pretrained(
        "stabilityai/stablecode-completion-alpha-3b",
        trust_remote_code=True,
        torch_dtype="auto",
    )
    model.cuda()
    ```

1. Provide a prompt and tokenize it.

    ```python
    inputs = tokenizer("import torch\nimport torch.nn as nn", return_tensors="pt", return_token_type_ids=False).to("cuda")
    ```

1. Initialize model parameters and time functions.

    ```python
    start_time = time.time()
    tokens = model.generate(
        **inputs,
        max_new_tokens=48,
        temperature=0.2,
        do_sample=True,
    )
    end_time = time.time()
    ```

1. Calculate and measure tokens per second.

    ```python
    num_tokens = tokens.size(1)
    elapsed_time = end_time - start_time
    tokens_per_second = num_tokens / elapsed_time


    print("Generated text:", tokenizer.decode(tokens[0], skip_special_tokens=True))
    print(f"Tokens per second: {tokens_per_second:.2f}")
    ```

    StableCode Completion Alpha 3B generates an average of **25 tokens per second**

## GPU Usage and VRAM Requirements

Following are the VRAM consumptions of both the models demonstrated in this article:

* Stable Code 3B: **6.4 GB**
* StableCode Instruct Alpha 3B: **14.7 GB**
* StableCode Completion Alpha 3B: **6.4 GB**

## Conclusions

In this article, you performed inference workloads on Stable Code 3B, StableCode Instruct Alpha 3B, and StableCode Completion Alpha 3B and compared their tokens per second generating capability and their VRAM consumptions respectively.
