July 25, 2023
We're excited to share the release of our brand-new Batch Processing API for batch inference jobs!
Historically, most of our APIs centered on training (documentation). 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.
Our new Batch Processing API extends Gradyn beyond model training, bringing model inference parallelization within reach and making it genuinely simple to adopt.
Throughout this post, we'll present the Batch Processing API and dive deeper into one particular use case: computing text embeddings.
Our users have consistently raised three recurring pain points:
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).
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.
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.
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.
Speed up batch inference by using multiple GPUs: The Batch Processing API hides low-level parallel programming and takes care of worker synchronization and preemption behind the scenes (see this example for how the new API removes that complexity for you).
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.
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.
The Batch Processing API behaves much like Gradyn Trial APIs for training. You define a processor class that inherits from TorchBatchProcessor :
class MyProcessor(TorchBatchProcessor):
def __init__(self, context):
self.context = context
self.model = context.prepare_model_for_inference(get_model())
def process_batch(self, batch, batch_idx) -> None:
predictions = self.model(batch)
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.
Optionally, users may also implement on_checkpoint_start and on_finish methods that define logic to run at those particular lifecycle points. A template can be found here for your convenience.
As an example, here's what a main function might look like if you wanted to run inference on a BeIR/scidocs dataset:
if __name__ == "__main__":
dataset = load_dataset("BeIR/scidocs", "corpus", split="corpus")
experimental.torch_batch_process(
EmbeddingProcessor,
dataset,
batch_size=64,
checkpoint_interval=10
)
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.
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.
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.
Use the diagram shown here to follow each stage of the embedding workflow:
The Batch Processing API builds the vector embeddings for every document in a corpus
the embeddings then land in a vector database
Later, real-time LLM applications query the vector database to find the documents that best match a user's question.

You can find a full example here. Here is a quick, high-level walkthrough of it:
Our implementation defines the EmbeddingProcessor, a subclass that extends TorchBatchProcessor.
Inside the init function, every worker fetches and loads the embedding model into memory.
Within the process_batch function, each worker runs inference and produces the embeddings.
During the on_checkpoint_start function, every worker writes its embeddings to files on the shared file system.
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.
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.
As an example, here is the result returned for the input “Data mining techniques”:

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.
The batch processing API is now ready for you to explore. To begin, please open the documentation and join our open developer community chat channel! As always, our team is here to help and welcomes any feedback you share.