<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Büşra Korkmaz on Medium]]></title>
        <description><![CDATA[Stories by Büşra Korkmaz on Medium]]></description>
        <link>https://medium.com/@bussra.krkmz?source=rss-29a27d08c548------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EaNSMHckloIHXoTfEDHp1g.jpeg</url>
            <title>Stories by Büşra Korkmaz on Medium</title>
            <link>https://medium.com/@bussra.krkmz?source=rss-29a27d08c548------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 07:25:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bussra.krkmz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Scalable Batch Inference on Large Language Models Using Ray]]></title>
            <link>https://medium.com/kocdigital/scalable-batch-inference-on-large-language-models-using-ray-ac3bf1cf1384?source=rss-29a27d08c548------2</link>
            <guid isPermaLink="false">https://medium.com/p/ac3bf1cf1384</guid>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[rays]]></category>
            <category><![CDATA[batch-inferences]]></category>
            <dc:creator><![CDATA[Büşra Korkmaz]]></dc:creator>
            <pubDate>Tue, 31 Oct 2023 09:52:39 GMT</pubDate>
            <atom:updated>2023-10-31T09:52:39.741Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Introduction</strong></p><p>Large language models (LLMs) have become very popular in recent years. To use these models effectively, we need to integrate current technologies. At Koc Digital, we have been using the Ray library in our projects since 2019 and achieving significant performance improvements. In this article we co-authored with Murat Koc, we explore the batch inference performance of an LLM model with a Ray library. We will show how we can do this using Ray Data, one of the Ray AI Runtime libraries. For this, we will create a Ray cluster to work on. We will compare two types of batch inference applications: sequential and distributed. In this study, we use the GP2-Large model for text-generation prediction. The data we use consists of repeated questions with 6480 lines.</p><p>In this post, we will discover the following topics:</p><ul><li>Inference with Ray AIR Datasets.</li><li>Get to know the inference task.</li><li>Implement sequential inference.</li><li>Learn about three distributed batch inference design patterns with Ray.</li><li>Implement distributed inference patterns.</li></ul><p><strong>1.Scaling Batch Inference with Ray</strong></p><p><strong>What is batch inference?</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*hJi4B9WIPs8FYUfwssNN8A.png" /><figcaption>Batch Inference Diagram</figcaption></figure><p>Batch inference, also known as offline inference, is the process of generating predictions about a set of observations. Estimates of batches, usually created according to recurring schedules, are stored in a database. This data can be made available to developers. For example, it can be processed and new predictions can be created using big data technologies.</p><p><strong>Batch inference using Ray AIR Data</strong></p><p>Ray Data is a utility for large-scale, distributed or sequential batch inference. Ray Data supports various predictors like TorchPredictor, HuggingFacePredictor or TFPredictor.</p><p><strong>2.Prepare Environment</strong></p><p>We ran the following scripts to create the Ray cluster.</p><pre>ray stop # If there is an active cluster, to stop it<br>export RAY_ENABLE_RECORD_ACTOR_TASK_LOGGING=1 # To view worker logs of actor classes in detail<br>export CUDA_VISIBLE_DEVICES=2,3,5 # To determine the GPUs that the Ray cluster will run on (we have 6 GPUs, we chose a few of them)<br>ray start --head --num-cpus=30 --num-gpus=3 --port=8266  --dashboard-port=8267 # To run the ray cluster with 3 gpu and 30 cpu</pre><p>We used the following script to submit the following python codes to the ray cluster. (ray_batch_inf.py is the file name we created)</p><pre>RAY_ADDRESS=&#39;http://127.0.0.1:8267&#39; ray job submit --working-dir . -- python ray_batch_inf.py</pre><p>Ray version: 2.7.1</p><p>Python version: 3.10.4</p><p>Using the <strong><em>ray status </em></strong>script, we displayed the cluster information we created as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aeCwwcefOpHHFtoyUdA3Uw.png" /><figcaption>Ray Cluster Info</figcaption></figure><p><strong>3. Sequential Batch Inference</strong></p><p>To begin with this batch inference task, we will take a basic approach with a worker sequentially generating predictions on groups. We will work with a single worker and process all the data by dividing it into batches and see how it uses time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QNDrmUaQ6Iy1CLw_EX9ABg.png" /></figure><p>We carried out two studies under the title of Sequential Batch Inference. In these studies, we observed the difference between GPU and CPU usage when performing sequential batch inference. You can change the estimation time by reducing data <strong><em>size</em></strong> with <strong><em>take_batch</em></strong> parameter in the <strong><em>map_batches</em></strong> function, or by increasing or decreasing <strong><em>batch_size</em></strong> parameter.</p><pre>from typing import Dict<br>import numpy as np<br>import time<br>import ray<br>from transformers import pipeline  <br> <br>ds = ray.data.read_text(&quot;data.txt&quot;)<br> <br>class HuggingFacePredictor:<br>  <br>    def __init__(self):<br>     <br>        start = time.time()<br>        # We added the device=&quot;cuda&quot; parameter to the function for GPU usage.<br>        self.model = pipeline(&quot;text-generation&quot;, model=&quot;gpt2-large&quot;,device=&quot;cuda&quot;)<br>        end = time.time()<br>  <br>        print(&quot;--------- MODEL LOADING TIME -------&quot;)<br>        print(end-start)<br>  <br>    def __call__(self, batch: Dict[str, np.ndarray]) -&gt; Dict[str, list]:<br>        print(&quot;CALL METHOD&quot;)<br>        start = time.time()<br>        predictions = self.model(list(batch[&quot;text&quot;]), max_length=20, num_return_sequences=1)<br>        batch[&quot;output&quot;] = [sequences[0][&quot;generated_text&quot;] for sequences in predictions]<br>        end = time.time()<br>         <br>        print(&quot;--------- MODEL INFERENCE TIME -------&quot;)<br>        print(end-start)<br>        return batch<br> <br>start = time.time()<br>  <br>predictions = ds.map_batches(<br>HuggingFacePredictor,<br>num_gpus=1, # GPU capacity of each worker<br>#num_cpus=24 # # CPU capacity of each worker<br>batch_size=250, # We will make predictions by dividing 6480 data by 250.<br>compute=ray.data.ActorPoolStrategy(size=1), # The number of Worker. Since it is sequential, we set it to run 1 worker.<br>).take_batch(6480) ## The number of data we want to predict in the dataset<br>  <br>end = time.time()<br>  <br>print(&quot;--------------- TOTAL TIME -----------------&quot;)<br>print(end-start)<br> <br>print(predictions)  print(&quot;--------------- TOTAL TIME -----------------&quot;)<br>print(predictions)<br>print(end-start)</pre><p><strong>Senario 1: 1 Worker &amp; 1 GPU &amp; 6480 Data &amp; 250 Batch Size</strong></p><p>As can be seen in the codes, by adding <strong><em>device=”cuda”</em></strong> parameter to the <strong><em>pipeline </em></strong>function, we enabled it to use GPU while making the prediction. For this purpose, we also determined the gpu usage capacity of the workers by adding the <strong><em>num_gpus</em></strong> parameter to the <strong><em>map_batches</em></strong> function. For Sequential Batch inference, we made the estimation with 1 worker.</p><p>When we view the Cluster status via the <strong>Cluster</strong> Tab of the Ray Dashboard, we can see that 1 GPU is used as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0IRUJ-Meh_SAHO0BOuq9lQ.png" /><figcaption>GPU Usage</figcaption></figure><p>We can see the running workers and their status through the <strong>Actor</strong> Tab of the Ray Dashboard as follows. We can also track the log separately for each worker from here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9muoSw1JysiLqYm7qmrk7A.png" /></figure><p>By using the <strong><em>nvidia-smi</em></strong> script on the machine, we can view the worker running on the machine and which GPU it is using. Since we give 1 GPU capacity to 1 Worker, we get an output like the following.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*IWUeqilNzBOxn9zJ7OS33A.png" /></figure><p>The estimation time of a worker with 1 GPU capacity working with 250 batches on 6480 data for text generation with the GPT2-Large model takes approximately 23 minutes as follows. In the next scenario, we will see the speed difference created by using CPU instead of GPU.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/726/1*8Or5QeHYe5cBYnl4NG4Ong.png" /></figure><p><strong>Senario 2:</strong> <strong>1 Worker &amp; 24 CPU &amp; 6480 Data &amp; 250 Batch Size</strong></p><p>When we remove <strong><em>device=”cuda”</em></strong> parameter in the <strong><em>pipeline </em></strong>function that creates the model and use the <strong><em>num_cpus</em></strong> parameter instead of <strong><em>num_gpus</em></strong> in the <strong><em>map_batches</em></strong> function, we disable the use of GPUs. When we examine the cluster tab of Ray Dasboard by giving 24 CPU capacity to 1 worker via CPU, we will see that the CPU usage is almost 100% as follows. We can also view the status and logs of running workers via the Actor tab as above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tiKVzzK9Sq7pKPsB3ofD0g.png" /><figcaption>CPU Usage</figcaption></figure><p>Since this job does not run on the GPU, we used the <strong><em>ps -ef | grep ray</em></strong> script to display the worker as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*NmUyJL3D7-TNMpUZ3r4UCw.png" /></figure><p>The estimation time of 1 worker with 24 CPU capacity working with 250 batches on 6480 data for text generation with the GPT2-Large model takes approximately 5 hours as follows. In the next steps, we will simulate distributed batch inference scenarios.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/691/1*L3_wpqMPYTcGSYtoAzk4rg.png" /></figure><p><strong>4. Distributed Batch Inference</strong></p><p>It allows you to perform batch inference by copying the existing model on multiple workers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/1*qFZuWdfZ87ZYaWHOAbZXtA.png" /></figure><p>We have completed two studies under the title Distributed Batch Inference. We clearly observed the difference in speed of estimation when Sequential or Distributed was run in these scenarios. We also examined the differences between GPU and CPU usage when performing distributed batch inference. You can change the estimation time by reducing data <strong><em>size</em></strong> with the <strong><em>take_batch</em></strong> parameter in the <strong><em>map_batches</em></strong> function or by increasing or decreasing <strong><em>batch_size</em></strong> parameter. In addition, you can increase or decrease the number of workers by changing <strong><em>size </em></strong>value of <strong><em>compute </em></strong>parameter in the <strong><em>map_batches </em></strong>function.</p><pre>from typing import Dict<br>import numpy as np<br>import time<br>import ray<br>from transformers import pipeline  <br> <br>ds = ray.data.read_text(&quot;data.txt&quot;)<br> <br>class HuggingFacePredictor:<br>  <br>    def __init__(self):<br>     <br>        start = time.time()<br>        # We added the device=&quot;cuda&quot; parameter to the function for GPU usage.<br>        self.model = pipeline(&quot;text-generation&quot;, model=&quot;gpt2-large&quot;, device=&quot;cuda&quot; )<br>        end = time.time()<br>  <br>        print(&quot;--------- MODEL LOADING TIME -------&quot;)<br>        print(end-start)<br>  <br>    def __call__(self, batch: Dict[str, np.ndarray]) -&gt; Dict[str, list]:<br>        print(&quot;CALL METHOD&quot;)<br>        start = time.time()<br>        predictions = self.model(list(batch[&quot;text&quot;]), max_length=20, num_return_sequences=1)<br>        batch[&quot;output&quot;] = [sequences[0][&quot;generated_text&quot;] for sequences in predictions]<br>        end = time.time()<br>         <br>        print(&quot;--------- MODEL INFERENCE TIME -------&quot;)<br>        print(end-start)<br>        return batch<br> <br>start = time.time()<br>  <br>predictions = ds.map_batches(<br>HuggingFacePredictor,<br>num_gpus=0.5, # GPU capacity of each worker<br>#num_cpus=1, # CPU capacity of each worker<br>batch_size=250, # We will make predictions by dividing 6480 data by 250.<br>compute=ray.data.ActorPoolStrategy(size=6),# The number of Worker. Since it is distributed, we set it to run 6 worker.<br>).take_batch(6480) ## The number of data we want to predict in the dataset<br>  <br>end = time.time()<br>  <br>print(&quot;--------------- TOTAL TIME -----------------&quot;)<br>print(end-start)<br> <br>print(predictions)  print(&quot;--------------- TOTAL TIME -----------------&quot;)<br>print(predictions)<br>print(end-start)</pre><p><strong>Senario 3:</strong> <strong>6 Worker &amp; 0.5 GPU &amp; 6480 Data &amp; 250 Batch Size</strong></p><p>To speed up the first scenario, we performed distributed batch inference by increasing the number of workers in this scenario. Thus, we accelerated the time in the first scenario even more. In the Cluster tab of the Ray Dashboard, we can see that all 3 GPUs are used at full capacity, as seen below. We can review all the workers we run and their status under the Actor Tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GHd7YqRNo-8RrXnLfyPKmw.png" /></figure><p>By using <strong><em>nvidia-smi</em></strong> script on the machine, we can view how the 6 workers running on the machine and how they are positioned on the GPUs. Since we give 0.5 gpu capacity to 6 Workers, we get an output as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*upG4iqtGp-Wz49AdJ8NXEg.png" /></figure><p>The estimation time of our 6 workers with 0.5 gpu capacity, working with 250 batches on 6480 data for text generation with the GPT2-Large model, takes approximately 6 minutes as follows. In the next scenario we will examine the usage of distributed batch inference on the CPU.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*5VtLwzZqY2_I4Ijdsft-gg.png" /></figure><p><strong>Senario 4:</strong> <strong>6 Worker &amp; 4 CPU &amp; 6480 Data &amp; 250 Batch Size</strong></p><p>When we remove <strong><em>device=”cuda”</em></strong> parameter in the <strong><em>pipeline </em></strong>function that creates the model and use the <strong><em>num_cpus</em></strong> parameter instead of <strong><em>num_gpus</em></strong> in the <strong><em>map_batches</em></strong> function, we inactivate the use of GPUs. When we examine the cluster tab of Ray Dasboard by giving 4 cpu capacity to 6 workers on cpu, we will see that CPU utilization is almost 100% as below. We can also view the status and logs of the running workers on the Actor tab as above.</p><p>Since this job does not run on the GPU, we used the <strong><em>ps -ef | grep ray</em></strong> script to display workers as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*2ZJcaMBO9tLcl-35x4XybA.png" /></figure><p>The estimation time of our 6 workers with 4 cpu capacity, working with 250 batches on 6480 data for text generation with the GPT2-Large model, takes approximately 1 hour as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*ogdXjKimuIIstbLiRHasWg.png" /></figure><p>Don’t forget to shutdown ray.</p><pre>ray.shutdown()</pre><p><strong>Conclusion</strong></p><p>Batch inference is the process of applying a trained model to a large dataset offline. In this post, we use the Ray Data library, part of the Ray AI Runtime, to perform batch inference with a large language model (LLM). We compare the performance of two different batch inference methods: sequential and distributed. We also examine how different parameters, such as GPU and CPU usage, affect the inference speed. We find that Ray Data offers a flexible and scalable solution for offline batch inference tasks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ac3bf1cf1384" width="1" height="1" alt=""><hr><p><a href="https://medium.com/kocdigital/scalable-batch-inference-on-large-language-models-using-ray-ac3bf1cf1384">Scalable Batch Inference on Large Language Models Using Ray</a> was originally published in <a href="https://medium.com/kocdigital">KoçDigital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>