Datasets:
File size: 20,021 Bytes
1c5f5f7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | <!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8"/>
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=no" name="viewport"/>
<link as="font" crossorigin="anonymous" href="/assets/webfonts/38C7DA_4_0.woff2" rel="preload"/>
<link as="font" crossorigin="anonymous" href="/assets/webfonts/38C7DA_1_0.woff2" rel="preload"/>
<link crossorigin="anonymous" href="/_cdn/unpkg.com/purecss@1.0.0/build/pure-min.css" rel="stylesheet"/>
<link href="/_cdn/unpkg.com/purecss@1.0.0/build/grids-responsive-min.css" rel="stylesheet"/>
<link href="/_cdn/cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css" rel="stylesheet"/>
<link href="/favicon.ico" rel="shortcut icon" type="image/x-icon"/>
<link href="/assets/css/MyFontsWebfontsKit.css" rel="stylesheet" type="text/css"/>
<link href="/assets/css/styles.css?v=1695754995" rel="stylesheet"/>
<link crossorigin="anonymous" href="/_cdn/use.fontawesome.com/releases/v5.6.3/css/all.css" rel="stylesheet"/>
<link href="/feed.xml" rel="alternate" title="Gradyn" type="application/atom+xml"/>
<title>Running Batch Inference on Gradyn Part 1: Computing Text Embeddings | Gradyn AI</title>
<meta content="Jekyll v3.9.3" name="generator"/>
<meta content="Use Batch Inference in Gradyn Part 1: Text Embedding Computation" property="og:title"/>
<meta content="isha_ghodgaonkar" name="author"/>
<meta content="en_US" property="og:locale"/>
<meta content="Announcing our new Batch Processing API built for batch inference jobs!" name="description"/>
<meta content="Introducing a new Batch Processing API for batch inference use cases!" property="og:description"/>
<link href="/blog/batch-processing-api" rel="canonical"/>
<meta content="/blog/batch-processing-api" property="og:url"/>
<meta content="Gradyn AI" property="og:site_name"/>
<meta content="article" property="og:type"/>
<meta content="2023-07-25T00:00:00+00:00" property="article:published_time"/>
<meta content="summary" name="twitter:card"/>
<meta content="Use Batch Inference in Gradyn Part 1: Text Embedding Computation" property="twitter:title"/>
<meta content="@gradynai" name="twitter:site"/>
<meta content="@isha_ghodgaonkar" name="twitter:creator"/>
<meta content="/assets/images/logo_text_transparent_crop_high_res_with_padding2.png" property="og:image"/>
<meta content="image/png" property="og:image:type"/>
<style>*,*::before,*::after{animation-duration:0s!important;animation-delay:0s!important;transition-duration:0s!important;transition-delay:0s!important;scroll-behavior:auto!important;caret-color:transparent!important}</style></head>
<body>
<div class="default-layout">
<nav class="navbar-outer" id="top-nav">
<div class="navbar container">
<div class="nav-brand">
<a href="/">
<img alt="Gradyn Logo" class="brand-img pure-img dark" src="/assets/images/R4_Logo.svg"/>
<img alt="Gradyn Logo" class="brand-img pure-img light" src="/assets/images/R4_Logo-Reversed.svg"/>
</a>
<div id="top-nav-burger">
<i class="fas fa-bars hamburger"></i>
<i class="fas fa-times close"></i>
</div>
</div>
<ul class="nav-links">
<li class="nav-link-outer">
<a class="nav-link text-link" href="/project/">Project</a>
</li>
<li class="nav-link-outer">
<a class="nav-link active text-link" href="/blog/">Blog</a>
</li>
<li class="nav-link-outer">
<a class="nav-link text-link" href="/resources/">Resources</a>
</li>
<li class="nav-link-outer">
<a class="nav-link" href="/_404.html" target="_blank">
<button class="btn btn-primary btn-sm">the code repo <i class="fas fa-external-link-alt"></i></button>
</a>
</li>
<li class="nav-link-outer">
<a class="nav-link" href="/_404.html" target="_blank">
<button class="btn btn-primary btn-inverted btn-sm">community chat <i class="fas fa-external-link-alt"></i></button>
</a>
</li>
<li class="nav-link-outer">
<a class="nav-link" href="/_404.html" target="_blank">
<button class="btn btn-primary btn-inverted btn-sm">Docs <i class="fas fa-external-link-alt"></i></button>
</a>
</li>
</ul>
</div>
</nav>
<div class="post-outer">
<div class="jumbotron">
<div class="container">
<h1>Running Batch Inference on Gradyn Part 1: Computing Text Embeddings</h1>
<div class="post-info-container">
<div class="author-images-outer">
<img alt="Image of Priya Anand, Marco Reyes, Lena Vossberg" class="author-image" src="/assets/images/isha.jpg"/>
<img alt="Image of Priya Anand, Marco Reyes, Lena Vossberg" class="author-image" src="/assets/images/ran.jpg"/>
<img alt="Image of Priya Anand, Marco Reyes, Lena Vossberg" class="author-image" src="/assets/images/szewai.jpeg"/>
</div>
<div>
<p class="author-names">By Priya Anand, Marco Reyes, Lena Vossberg</p>
<p class="post-date">July 25, 2023</p>
</div>
</div>
</div>
</div>
<section>
<div class="container">
<div class="post-container">
<p>We're excited to share the release of our brand-new Batch Processing API for batch inference jobs!</p>
<p>Historically, most of our APIs centered on training (<a href="/_404.html">documentation</a>). Although training the model is central to what a data scientist does, an end-to-end workflow rarely ends there. Data scientists routinely run inference with their trained models to generate embeddings or to carry out extra rounds of model validation. Frequently, the sheer scale of data required for these jobs makes it necessary to run inference in parallel across several model replicas. </p>
<p>Our new Batch Processing API extends Gradyn beyond model training, bringing model inference parallelization within reach and making it genuinely simple to adopt.</p>
<p>Throughout this post, we'll present the Batch Processing API and dive deeper into one particular use case: computing text embeddings.</p>
<h1 id="why-is-the-batch-processing-api-needed">Why does the Batch Processing API matter?</h1>
<p>Our users have consistently raised three recurring pain points:</p>
<ol>
<li>
<p>No standardization : Until recently, there was no built-in, straightforward, Gradyn native way for our users to perform batch inference. A Core API based approach to batch inference demands a low-level grasp of parallel programming. Otherwise, users' own homegrown setups for batch inference went without native backing from Gradyn (e.g., preemption, experiment tracking, metrics reporting, and codebase upkeep).</p>
</li>
<li>
<p>Batch inference is computationally demanding : Batch inference typically runs over a large volume of data and is expensive to compute. Writing a script that runs inference with a single model may be easy, but the sheer scale of data usually forces you to run inference in parallel across many model replicas. Things get complicated fast.</p>
</li>
<li>
<p>Skipping batch inference stalls the model development workflow : Users end up running only small-scale inference while prototyping, and at times don't even recognize that batch inference is needed. When users bypass batch inference during prototyping and jump straight to productionizing, their model development slows down.</p>
</li>
</ol>
<h1 id="how-can-determineds-batch-processing-api-address-these-problems">How does Gradyn’s Batch Processing API tackle these problems?</h1>
<ul>
<li>
<p>Make batch inference a first-class citizen in your workflow : Much like a training experiment, the inference experiment shows up in the Gradyn Web UI, and its metrics are reported and visualized in the Gradyn UI. On top of that, users can pause and resume the inference experiment from the Gradyn UI, and checkpoints let you resume your batch inference experiment.</p>
</li>
<li>
<p><strong>Speed up batch inference by using multiple GPUs</strong>: The Batch Processing API hides low-level parallel programming and takes care of worker synchronization and preemption behind the scenes (see <a href="/_404.html">this example</a> for how the new API removes that complexity for you). </p>
</li>
<li>
<p>Boost prototyping velocity through flexibility : Batch inference can run on demand, no matter your development stage. That flexibility lifts prototyping velocity and keeps the user journey smooth all the way from prototyping to productionizing.</p>
</li>
</ul>
<h1 id="introducing-the-batch-processing-api">Introducing our Batch Processing API</h1>
<p>The Batch Processing API helps you 1) shard your dataset across many workers, 2) apply your processing logic to batches of data, and 3) track progress in case you pause and resume your experiment. In short, every capability Gradyn provides for training your model is now available through the Batch Processing API for non model training workloads too, including worker coordination and pause/resume functionality.</p>
<p><br/></p>
<p>The Batch Processing API behaves much like Gradyn Trial APIs for training. You define a processor class that inherits from TorchBatchProcessor :</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">class</span> <span class="nc">MyProcessor</span><span class="p">(</span><span class="n">TorchBatchProcessor</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">context</span><span class="p">):</span>
<span class="bp">self</span><span class="p">.</span><span class="n">context</span> <span class="o">=</span> <span class="n">context</span>
<span class="bp">self</span><span class="p">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">context</span><span class="p">.</span><span class="n">prepare_model_for_inference</span><span class="p">(</span><span class="n">get_model</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">process_batch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">batch</span><span class="p">,</span> <span class="n">batch_idx</span><span class="p">)</span> <span class="o">-></span> <span class="bp">None</span><span class="p">:</span>
<span class="n">predictions</span> <span class="o">=</span> <span class="bp">self</span><span class="p">.</span><span class="n">model</span><span class="p">(</span><span class="n">batch</span><span class="p">)</span>
</code></pre></div></div>
<p><br/></p>
<p>The two methods users are required to implement are __init__ , where resources such as models and tokenizers can be set up, and process_batch , which holds the processing logic applied to each batch of data.</p>
<p>Optionally, users may also implement <code class="language-plaintext highlighter-rouge">on_checkpoint_start</code> and <code class="language-plaintext highlighter-rouge">on_finish</code> methods that define logic to run at those particular lifecycle points. A template can be found <a href="/_404.html">here</a> for your convenience.</p>
<p>As an example, here's what a main function might look like if you wanted to run inference on a BeIR/scidocs dataset:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">"__main__"</span><span class="p">:</span>
<span class="n">dataset</span> <span class="o">=</span> <span class="n">load_dataset</span><span class="p">(</span><span class="s">"BeIR/scidocs"</span><span class="p">,</span> <span class="s">"corpus"</span><span class="p">,</span> <span class="n">split</span><span class="o">=</span><span class="s">"corpus"</span><span class="p">)</span>
<span class="n">experimental</span><span class="p">.</span><span class="n">torch_batch_process</span><span class="p">(</span>
<span class="n">EmbeddingProcessor</span><span class="p">,</span>
<span class="n">dataset</span><span class="p">,</span>
<span class="n">batch_size</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span>
<span class="n">checkpoint_interval</span><span class="o">=</span><span class="mi">10</span>
<span class="p">)</span>
</code></pre></div></div>
<h1 id="use-case-text-embeddings-computation">Use Case: Computing Text Embeddings</h1>
<h4 id="why-do-we-need-to-compute-document-embeddings">Why does computing document embeddings matter?</h4>
<p>Chat and question-and-answer tools built on LLMs are becoming ever more widespread. Prompts sent to the LLM are frequently enriched with relevant documents to boost the quality of the answers. Generating embeddings is the opening step of this workflow.</p>
<h4 id="what-is-vector-embedding">What is a vector embedding?</h4>
<p>Embedding computation describes a procedure in which high-dimensional data (such as words, phrases, or whole documents) is mapped onto vectors of real numbers within a lower-dimensional space (an embedding space). In information retrieval settings, vectors in this embedding space place semantically similar inputs (for instance, words that mean similar things; phrases carrying similar sentiment; images that look alike) near one another in the embedding space. Keeping these vectors in a vector database lets users capture the underlying structure and relationships of the original data.</p>
<p>Text embeddings for related documents sit closer together in the embedding space, so a vector database can surface relevant documents through vector similarity search techniques. Turning input data into the vector format that makes this search possible means running inference with an embedding model over a large volume of data, and that is exactly where our new Batch Processing API comes in.</p>
<h1 id="how-to-use-the-batch-processing-api-for-distributed-embedding-generation--">How can the Batch Processing API power distributed embedding generation?</h1>
<p>Use the diagram shown here to follow each stage of the embedding workflow:</p>
<ol>
<li>
<p>The Batch Processing API builds the vector embeddings for every document in a corpus</p>
</li>
<li>
<p>the embeddings then land in a vector database</p>
</li>
</ol>
<p>Later, real-time LLM applications query the vector database to find the documents that best match a user's question.</p>
<p><img alt="scidocs dataset inference" src="/assets/images/blogs/batchprocessingapi/batchprocessing3.png"/></p>
<center>
</center>
<p>You can find a full example <a href="/_404.html">here</a>. Here is a quick, high-level walkthrough of it: </p>
<p>Our implementation defines the EmbeddingProcessor, a subclass that extends TorchBatchProcessor.</p>
<ul>
<li>
<p>Inside the init function, every worker fetches and loads the embedding model into memory.</p>
</li>
<li>
<p>Within the process_batch function, each worker runs inference and produces the embeddings.</p>
</li>
<li>
<p>During the on_checkpoint_start function, every worker writes its embeddings to files on the shared file system.</p>
</li>
<li>
<p>Within the on_finish function, the chief worker sets up a vector database client and creates a collection. It then loads every embeddings file into memory and writes all of those embeddings into the vector database.</p>
</li>
</ul>
<p>You can now fetch relevant documents from the vector database for any given input. Just embed the query text with the same embedding model and search the vector database with the embeddings you generated.</p>
<p>As an example, here is the result returned for the input “Data mining techniques”:</p>
<div class="large-img">
<p><img alt="text" src="/assets/images/blogs/batchprocessingapi/batchprocessing4.png"/></p>
</div>
<h1 id="summary">Wrap-up</h1>
<p>The Batch Processing API tackles the pain points of batch inference workloads beyond model training, such as embedding computation, especially when your datasets are large. It makes batch inference Gradyn native, flexible, and simple to adopt.</p>
<p>The batch processing API is now ready for you to explore. To begin, please open the <a href="/_404.html">documentation</a> and join our open developer community <a href="/_404.html">chat channel!</a> As always, our team is here to help and welcomes any feedback you share.</p>
</div>
</div>
</section>
<section class="bg-light">
<div class="container">
<div class="recent-blogs">
<h1>Latest Posts</h1>
<div class="blogs pure-g">
<div class="blog pure-u-1 pure-u-sm-1-3">
<div class="blog-content">
<div class="upper-content">
<div class="all-caps">OCT 04, 2023</div>
<h2><a href="/blog/preemption">Optimizing Workloads: How Gradyn Finally Cracks the Preemption Puzzle</a></h2>
</div>
<div class="lower-content">
<a class="all-caps font-primary" href="/blog/preemption">READ MORE <i class="fas fa-chevron-right secondary"></i></a>
</div>
</div>
</div>
<div class="blog pure-u-1 pure-u-sm-1-3">
<div class="blog-content">
<div class="upper-content">
<div class="all-caps">SEP 18, 2023</div>
<h2><a href="/blog/pytorch-trainer">Iterate on your model code locally using the Trainer API</a></h2>
</div>
<div class="lower-content">
<a class="all-caps font-primary" href="/blog/pytorch-trainer">READ MORE <i class="fas fa-chevron-right secondary"></i></a>
</div>
</div>
</div>
<div class="blog pure-u-1 pure-u-sm-1-3">
<div class="blog-content">
<div class="upper-content">
<div class="all-caps">SEP 11, 2023</div>
<h2><a href="/blog/detached-mode">Detached Mode: Log metrics in Gradyn for workloads that run anywhere</a></h2>
</div>
<div class="lower-content">
<a class="all-caps font-primary" href="/blog/detached-mode">READ MORE <i class="fas fa-chevron-right secondary"></i></a>
</div>
</div>
</div>
</div>
</div>
<div class="stay-updated-form">
<div class="stay-updated-2-outer">
<div class="stay-updated-2-content">
<h2>Stay connected</h2>
<form action="https://go.pardot.com/l/659743/2019-01-15/w98" id="stay-updated-2-form" method="post">
<div class="pure-g stay-updated-2-form-content">
<div class="pure-u-1 pure-u-md-1-2">
<div class="input-group" id="email-input-group">
<input id="email" name="user_email" placeholder="Enter your email" type="email"/>
<label class="sr-only" for="email">Add your email to stay in the loop"</label>
</div>
</div>
<div class="pure-u-1 pure-u-md-1-2">
<button class="btn btn-primary" type="submit">SUBSCRIBE</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
</div>
<footer>
<div class="container">
<div class="pure-g">
<div class="pure-u-1 pure-u-sm-1-2">
<div class="copyright-content">
<div class="logo">
<img alt="Gradyn Logo" src="/assets/images/logo-alone-transparent-crop.png"/>
</div>
<div class="copyright caption desktop">
<div class="social-btns">
<a class="social-btn" href="/_404.html" target="_blank">
<img alt="twitter" src="/assets/images/Social_TW.svg"/>
</a>
<a class="social-btn" href="/_404.html" target="_blank">
<img alt="linkedin" src="/assets/images/Social_IN.svg"/>
</a>
</div>
<div>2023 Gradyn, Inc. All rights reserved.</div>
</div>
</div>
</div>
<div class="pure-u-1 pure-u-sm-1-2">
<div class="pure-g">
<div class="pure-u-1-2 pure-u-sm-1-3 link-wrap">
<a class="all-caps link" href="/project/">Project</a>
</div>
<div class="pure-u-1-2 pure-u-sm-1-3 link-wrap">
<a class="all-caps link" href="/careers/">Careers</a>
</div>
<div class="pure-u-1-2 pure-u-sm-1-3 link-wrap">
<a class="all-caps link" href="/contact/">Contact</a>
</div>
<div class="pure-u-1-2 pure-u-sm-1-3 link-wrap">
<a class="all-caps link" href="/_404.html">Docs</a>
</div>
<div class="pure-u-1-2 pure-u-sm-1-3 link-wrap">
<a class="all-caps link" href="/blog/">Blog</a>
</div>
<div class="pure-u-1-2 pure-u-sm-1-3 link-wrap">
<a class="all-caps link" href="/privacy/">Privacy</a>
</div>
</div>
</div>
<div class="pure-u-1">
<div class="copyright caption mobile">
<div class="social-btns">
<a class="social-btn" href="/_404.html" target="_blank">
<img alt="twitter" src="/assets/images/Social_TW.svg"/>
</a>
<a class="social-btn" href="/_404.html" target="_blank">
<img alt="linkedin" src="/assets/images/Social_IN.svg"/>
</a>
</div>
<div>2023 Gradyn, Inc. All rights reserved.</div>
</div>
</div>
</div>
</div>
</footer>
</div>
<script src="/cr-site.js"></script>
</body></html> |