Evnek Quest logo
← Back to all articles
AGENTIC AI · EVNEK QUEST

Unlocking the Power of LLM Inferencing: Real-Time AI Insights and Solutions

LLM inferencing uses pre-trained models to process inputs and generate outputs, leveraging AI's power for real-time text and data analysis.

Evnek Quest·3 December 2024·12 min read
Unlocking the Power of LLM Inferencing: Real-Time AI Insights and Solutions

What are Large Language Models (LLMs)?

A large language model is a type of artificial intelligence algorithm that applies neural network techniques with lots of parameters to process and understand human languages or text using self-supervised learning techniques. Tasks like text generation, machine translation, summary writing, and image generation from texts, machine coding, chat-bots, or Conversational AI are applications of the Large Language Model. Examples of such LLM models are Chat GPT by open AI, BERT (Bidirectional Encoder Representations from Transformers) by Google, etc.

What is LLM Inferencing?

LLM inferencing refers to the process of using a pre-trained Large Language Model (LLM) to generate outputs or predictions for a given input. It involves running the model in inference mode, where it takes user-provided text (or other input) and produces an appropriate response or result without modifying the model's learned parameters.

The LLM Inference API contains the following key features:

1.   Text-to-text generation - Generate text based on an input text prompt.

2.  LLM selection - Apply multiple models to tailor the app for your specific use cases. You can also retrain and apply customized weights to the model.

LoRA support - Extend and customize the LLM capability with LoRA model either by training on your all dataset or taking prepared prebuilt LoRA models from the open-source community (not compatible with models converted with the AI Edge Torch Generative API).

Key Steps in LLM Inferencing

  1. Input Tokenization:

    • The raw text input is tokenized into smaller units (tokens) using a tokenizer associated with the LLM. Tokens are typically numerical representations that the model can process.

  2. Model Processing:

    • The tokenized input is passed through the model, which uses its pre-trained parameters to analyze the input and compute predictions. The output is usually in the form of probabilities for the next possible tokens or a structured output, depending on the task.

  3. Decoding:

    • The output tokens are decoded back into human-readable text. This step often involves strategies like:

      • Greedy decoding: Selecting the token with the highest probability at each step.

      • Beam search: Exploring multiple possible sequences to find the most probable one.

      • Sampling: Introducing randomness to generate diverse outputs.

  4. Post-Processing:

    • Additional steps like formatting, summarizing, or structuring the model's response to suit the specific use case.

 

Use Cases of LLM Inferencing

 

Components Involved in Inferencing

  1. Pre-Trained LLM:

    • Examples include GPT, BERT, LLaMA, Falcon, or Qwen.

    • These models are pre-trained on large datasets and fine-tuned for specific tasks.

  2. Frameworks:

    • Tools like Hugging Face Transformers, OpenAI API, or LangChain help streamline inferencing workflows.

  3. Hardware:

    • Inferencing is computationally intensive, especially with large models. GPUs or TPUs are commonly used for faster processing.

 

Challenges in LLM Inferencing

  1. Latency:

    • Large models require significant computation, leading to delays in response times.

  2. Resource Requirements:

    • Inferencing requires powerful hardware, particularly for very large models (e.g., 65B or 175B parameters).

  3. Token Limit:

    • LLMs have a fixed maximum token limit for inputs and outputs, which can constrain inferencing for large documents.

  4. Cost:

    • Running inference on large-scale models can be expensive due to hardware and energy demands.

  5. Energy Consumption:

o   The computational requirements for inferencing are energy-intensive, raising concerns about environmental sustainability.

  1. Context Management:

o   LLMs have context length limitations, which may hinder their ability to process or generate very long inputs and outputs effectively. 

  1. Scalability:

o   Serving millions of inference requests simultaneously demands robust infrastructure and optimization strategies. 

Key Metrics for Evaluating LLM Inference

1.   Latency:

  1. Definition: The time it takes to generate a response.

  2. Components:

    • Time to First Token (TTFT): Time to start the response.

    • Time Per Output Token (TPOT): Time to generate each token after the first.

  3. Importance: Critical for real-time use cases like chatbots and translation.

2.   Throughput:

  1. Definition: How many requests or tokens can be processed in a given time?

  2. Measurements:

    • Requests per second: For handling many users.

    • Tokens per second: For measuring model efficiency.

  3. Importance: Useful for applications with high user demand.

 

Optimizing LLM Inference

1. KV Caching

2. Operator Fusion

3. Parallelization

4. Batching

These techniques collectively enhance LLM performance, balancing latency, throughput, and resource usage for different scenarios.

 

Optimizations for Efficient LLM Inferencing

  1. Quantization:

    • Reducing model precision (e.g., using int8 or bfloat16) to decrease memory and compute costs.

  2. Pruning:

    • Removing less important parts of the model to make it lighter and faster.

  3. Distillation:

    • Using a smaller "student" model trained to mimic the behavior of a larger model.

  4. Caching:

    • Storing intermediate results to avoid redundant computations in interactive applications.

  5. Batch Processing:

    • Processing multiple inputs simultaneously to maximize GPU utilization.

  6. Sparse Architectures:

o   Adopting models that activate only relevant parts of the network for each inference, reducing unnecessary computation.

  1. Hardware Acceleration:

o   Using specialized hardware like GPUs, TPUs, and FPGAs to expedite matrix computations.

 

Why Optimizing LLM Inference Matters

Large models like GPT or Falcon are powerful but resource heavy. Optimizing inference helps:

This makes LLMs practical for diverse applications, from chatbots to complex enterprise solutions.

 

Transformer-Based LLM Model Architectures

 

Transformer-based models, which have revolutionized natural language processing tasks, typically follow a general architecture that includes the following components:

 

1.   Input Embeddings: The input text is tokenized into smaller units, such as words or sub-words, and each token is embedded into a continuous vector representation. This embedding step captures the semantic and syntactic information of the input.

2.   Positional Encoding: Positional encoding is added to the input embeddings to provide information about the positions of the tokens because transformers do not naturally encode the order of the tokens. This enables the model to process the tokens while taking their sequential order into account.

3.   Encoder: Based on a neural network technique, the encoder analyses the input text and creates a number of hidden states that protect the context and meaning of text data. Multiple encoder layers make up the core of the transformer architecture. Self-attention mechanism and feed-forward neural network are the two fundamental sub-components of each encoder layer.

1.   Self-Attention Mechanism: Self-attention enables the model to weigh the importance of different tokens in the input sequence by computing attention scores. It allows the model to consider the dependencies and relationships between different tokens in a context-aware manner.

2.   Feed-Forward Neural Network: After the self-attention step, a feed-forward neural network is applied to each token independently. This network includes fully connected layers with non-linear activation functions, allowing the model to capture complex interactions between tokens.

4.   Decoder Layers: In some transformer-based models, a decoder component is included in addition to the encoder. The decoder layers enable autoregressive generation, where the model can generate sequential outputs by attending to the previously generated tokens.

5.   Multi-Head Attention: Transformers often employ multi-head attention, where self-attention is performed simultaneously with different learned attention weights. This allows the model to capture different types of relationships and attend to various parts of the input sequence simultaneously.

6.   Layer Normalization: Layer normalization is applied after each sub-component or layer in the transformer architecture. It helps stabilize the learning process and improves the model’s ability to generalize across different inputs.

7.   Output Layers: The output layers of the transformer model can vary depending on the specific task. For example, in language modeling, a linear projection followed by SoftMax activation is commonly used to generate the probability distribution over the next token.

 

How LLM Inference Works

LLM inference is the process through which large language models (LLMs) generate human-like responses to user prompts by predicting the most likely sequence of words. It happens in two main phases:

1. Prefill Phase

2. Decode Phase

Transformer Basics for Text Generation

A Transformer-based decoder generates text by processing input tokens to predict the next token.

Here's a breakdown of the process:

Key Components:

1.   Decoder Outputs Logits:

  1. The decoder produces logits (one for each vocabulary token) rather than tokens directly.

  2. A Language Model (LM) head is the final layer that outputs these logits.

2.   Decoding Strategies:

  1. Transform logits into tokens using strategies like:

    • Greedy Decoding: Select the token with the highest logit.

    • Sampling Decoding: Treat logits as probabilities, sampling tokens with modifications like temperature scaling, top-k, or top-p.

    • Beam Search and Contrastive Decoding: Use more sophisticated heuristics.

3.   Execution Engine:

  1. For practical purposes, decoding strategies are often integrated into the model as part of the inference engine.

                   Figure 1 —Outline of a Transformer decoder model

Phases of Text Generation

1. Initiation Phase (Pre-fill Phase):

Figure 2 — an overly simplified Transformer decoder model

Figure 3 — Tokenization step

 

2. Generation Phase (Decoding/Auto-Regressive Phase):

Figure 4 — Initiation and decoding phases of the token generation process

                                     Figure 5 — Detokenization step

 

                                                   

Advanced Techniques

 

Difference between Phases

 

Applications of LLM Inferencing 

- Customer Support: Automated chatbots powered by LLMs offer real-time support and guidance. 

- Code Generation: Models like GitHub Copilot assist developers by generating code snippets based on prompts. 

- Healthcare: LLMs provide medical professionals with quick insights from vast datasets and assist in drafting reports. 

- Education: AI tutors use inferencing to personalize learning experiences for students. 

 

Future Directions

1. Edge Inferencing: Deploying LLMs on edge devices to reduce reliance on cloud infrastructure and improve latency. 

2. Adaptive Models: Systems capable of dynamically adjusting model complexity based on the input's complexity. 

3. Federated Inferencing: Securely distributing inferencing tasks across decentralized systems to enhance privacy and scalability. 

4. Sustainability Initiatives: Exploring more energy-efficient hardware and algorithms to reduce carbon footprints. 

 

Example

Install Dependencies

Python code

! pip install transformers

This command installs the transformers library from Hugging Face, which is used to load pre-trained language models and perform various NLP tasks.

Import Libraries

Python code

from transformers import AutoTokenizer, AutoModelForCausalLM

import transformers

import torch

 

Load the Falcon Model

Python code

model = "tiiuae/falcon-7b-instruct"

tokenizer = AutoTokenizer.from_pretrained (model)

 

Define Text Generation Pipeline

Python code

falcon_pipeline = transformers.pipeline ("text-generation",

                                        model=model,

                                        tokenizer=tokenizer,

                                        torch_dtype=torch.bfloat16,

                                        trust_remote_code=True,

                                        device_map="auto"

                                        )

 

Define Completion Function

Python code

def get_completion_falcon (input):

  system = f"""

  You are an expert Physicist.

  You are good at explaining Physics concepts in simple words.

  Help as much as you can.

  """

  prompt = f"#### System: {system}\n#### User: \n{input}\n\n#### Response from falcon-7b-instruct:"

  print(prompt)

  1. system: Defines the role and behavior of the model, acting as an instruction to guide responses.

  2. prompt: Combines the system message and user input into a structured format. The use of prefixes (#### System, #### User, #### Response) ensures consistency in the model's interpretation.

  3. print(prompt): Displays the constructed prompt for debugging or inspection.

Python code

  falcon_response = falcon_pipeline (prompt,

                                    max_length=500,

                                    do_sample=True,

                                    top_k=10,

                                    num_return_sequences=1,

                                    eos_token_id=tokenizer.eos_token_id,

                                    )

  return falcon_response

 

Test the Model

Python code

prompt = "Explain to me the difference between nuclear fission and fusion."

# prompt = "Why is the Sky blue?"

response = get_completion_falcon (prompt)

print (response [0]['generated_text'])

  1. prompt: User-provided input text to query the model.

  2. response: The output of the get_completion_falcon function, containing generated text.

  3. response[0]['generated_text']: Extracts and prints the text portion of the generated output.

 

Sample Output

If the user prompt is “Explain to me the difference between nuclear fission and fusion.” the model would generate a detailed response explaining the concepts of nuclear fission and fusion in simple terms.

 

1. Transformers: The Foundation of LLM Inferencing

The Hugging Face Transformers library is one of the most widely used frameworks for working with pre-trained models. It provides a seamless interface for hundreds of models across various modalities, including text, vision, and multimodal.

Key Features for Inference

Example

Python code
from transformers import AutoModelForCausalLM, AutoTokenizer
 
# Load model and tokenizer
model_name = "gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
 
# Inference
input_text = "What is the capital of France?"
inputs = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**inputs, max_length=50)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

 

2. VLLM: High-Performance Inferencing

VLLM is an inference-optimized framework specifically designed to enhance the throughput of LLMs. It achieves this through continuous batching, which dynamically groups incoming requests to maximize GPU utilization, reducing latency without sacrificing model performance.

Advantages

Use Case

VLLM is ideal for serving LLMs in production environments with high traffic, such as chatbot applications or real-time recommendation systems.

Example

Python code
from vllm import LLM, SamplingParams
 
# Initialize the model
llm = LLM(model="gpt-3.5-turbo")
 
# Perform inference with optimized batching
outputs = llm.generate(
    ["Tell me a joke.", "Explain gravity in simple terms."],
    SamplingParams(temperature=0.7, max_tokens=100)
)
for output in outputs:
    print(output.text)

VLLM's dynamic batching ensures that even with multiple simultaneous requests, the inference remains efficient and responsive.

 

3. LangChain: Orchestrating LLM Workflows

LangChain is a framework designed to enhance the capabilities of LLMs by chaining together models, tools, and memory. It simplifies the development of applications that require complex reasoning, tool usage, or multi-step workflows.

Core Features

Example

Imagine you are building a conversational agent that retrieves information from a database and generates a summary.

Python code
from langchain.llms import HuggingFaceHub
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
 
# Initialize the model
llm = HuggingFaceHub(repo_id="gpt-3.5-turbo", model_kwargs={"temperature": 0.7})
 
# Define a prompt template
template = "Summarize the following for me: {text}"
prompt = PromptTemplate(template=template, input_variables=["text"])
 
# Create a chain
chain = LLMChain(llm=llm, prompt=prompt)
 
# Run the chain
summary = chain.run("The Eiffel Tower is located in Paris, France...")
print(summary)

LangChain excels in combining multiple steps or tools into a cohesive system, making it perfect for complex inferencing workflows.

 

Conclusion

LLM inferencing is at the heart of the AI revolution, enabling models to interact meaningfully with humans. While the challenges of cost, scalability, and energy consumption persist, advancements in optimization techniques and hardware are paving the way for more efficient and accessible inferencing solutions. As LLMs continue to grow in sophistication, their inferencing capabilities will play a pivotal role in defining the future of AI applications.

LLM inferencing is also the practical application of pre-trained language models to perform tasks like text generation, answering questions, and more. It leverages the model's learned representations to generate useful and relevant outputs for various applications.