forked from numaproj/numaflow-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: batch-map implementation (numaproj#177)
Signed-off-by: Sidhant Kohli <[email protected]>
- Loading branch information
Showing
51 changed files
with
1,770 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
## BatchMap Interface | ||
The BatchMap interface allows developers to | ||
process multiple data items together in a single UDF handler. | ||
|
||
|
||
### What is BatchMap? | ||
BatchMap is an interface that allows developers to process multiple data items | ||
in a UDF single call, rather than each item in separate calls. | ||
|
||
|
||
The BatchMap interface can be helpful in scenarios | ||
where performing operations on a group of data can be more efficient. | ||
|
||
|
||
### Understanding the User Interface | ||
The BatchMap interface requires developers to implement a handler with a specific signature. | ||
Here is the signature of the BatchMap handler: | ||
|
||
```python | ||
async def handler(datums: AsyncIterable[Datum]) -> BatchResponses: | ||
``` | ||
The handler takes an iterable of `Datum` objects and returns | ||
`BatchResponses`. | ||
The `BatchResponses` object is a list of the *same length* as the input | ||
datums, with each item corresponding to the response for one request datum. | ||
|
||
To clarify, let's say we have three data items: | ||
|
||
``` | ||
data_1 = {"name": "John", "age": 25} | ||
data_2 = {"name": "Jane", "age": 30} | ||
data_3 = {"name": "Bob", "age": 45} | ||
``` | ||
|
||
These data items will be grouped together by numaflow and | ||
passed to the handler as an iterable: | ||
|
||
```python | ||
result = await handler([data_1, data_2, data_3]) | ||
``` | ||
|
||
The result will be a BatchResponses object, which is a list of responses corresponding to each input data item's processing. | ||
|
||
### Important Considerations | ||
When using BatchMap, there are a few important considerations to keep in mind: | ||
|
||
- Ensure that the `BatchResponses` object is tagged with the *correct request ID*. | ||
Each Datum has a unique ID tag, which will be used by Numaflow to ensure correctness. | ||
|
||
```python | ||
async for datum in datums: | ||
batch_response = BatchResponse.from_id(datum.id) | ||
``` | ||
|
||
|
||
- Ensure that the length of the `BatchResponses` | ||
list is equal to the number of requests received. | ||
**This means that for every input data item**, there should be a corresponding | ||
response in the BatchResponses list. | ||
|
||
Use batch processing only when it makes sense. In some | ||
scenarios, batch processing may not be the most | ||
efficient approach, and processing data items one by one | ||
could be a better option. | ||
The burden of concurrent processing of the data will rely on the | ||
UDF implementation in this use case. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#################################################################################################### | ||
# builder: install needed dependencies | ||
#################################################################################################### | ||
|
||
FROM python:3.10-slim-bullseye AS builder | ||
|
||
ENV PYTHONFAULTHANDLER=1 \ | ||
PYTHONUNBUFFERED=1 \ | ||
PYTHONHASHSEED=random \ | ||
PIP_NO_CACHE_DIR=on \ | ||
PIP_DISABLE_PIP_VERSION_CHECK=on \ | ||
PIP_DEFAULT_TIMEOUT=100 \ | ||
POETRY_VERSION=1.2.2 \ | ||
POETRY_HOME="/opt/poetry" \ | ||
POETRY_VIRTUALENVS_IN_PROJECT=true \ | ||
POETRY_NO_INTERACTION=1 \ | ||
PYSETUP_PATH="/opt/pysetup" | ||
|
||
ENV EXAMPLE_PATH="$PYSETUP_PATH/examples/batchmap/flatmap" | ||
ENV VENV_PATH="$EXAMPLE_PATH/.venv" | ||
ENV PATH="$POETRY_HOME/bin:$VENV_PATH/bin:$PATH" | ||
|
||
RUN apt-get update \ | ||
&& apt-get install --no-install-recommends -y \ | ||
curl \ | ||
wget \ | ||
# deps for building python deps | ||
build-essential \ | ||
&& apt-get install -y git \ | ||
&& apt-get clean && rm -rf /var/lib/apt/lists/* \ | ||
\ | ||
# install dumb-init | ||
&& wget -O /dumb-init https://github.com/Yelp/dumb-init/releases/download/v1.2.5/dumb-init_1.2.5_x86_64 \ | ||
&& chmod +x /dumb-init \ | ||
&& curl -sSL https://install.python-poetry.org | python3 - | ||
|
||
#################################################################################################### | ||
# udf: used for running the udf vertices | ||
#################################################################################################### | ||
FROM builder AS udf | ||
|
||
WORKDIR $PYSETUP_PATH | ||
COPY ./ ./ | ||
|
||
WORKDIR $EXAMPLE_PATH | ||
RUN poetry install --no-cache --no-root && \ | ||
rm -rf ~/.cache/pypoetry/ | ||
|
||
RUN chmod +x entry.sh | ||
|
||
ENTRYPOINT ["/dumb-init", "--"] | ||
CMD ["sh", "-c", "$EXAMPLE_PATH/entry.sh"] | ||
|
||
EXPOSE 5000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
TAG ?= stable | ||
PUSH ?= false | ||
IMAGE_REGISTRY = quay.io/numaio/numaflow-python/batch-map-flatmap:${TAG} | ||
DOCKER_FILE_PATH = examples/batchmap/flatmap/Dockerfile | ||
|
||
.PHONY: update | ||
update: | ||
poetry update -vv | ||
|
||
.PHONY: image-push | ||
image-push: update | ||
cd ../../../ && docker buildx build \ | ||
-f ${DOCKER_FILE_PATH} \ | ||
-t ${IMAGE_REGISTRY} \ | ||
--platform linux/amd64,linux/arm64 . --push | ||
|
||
.PHONY: image | ||
image: update | ||
cd ../../../ && docker build \ | ||
-f ${DOCKER_FILE_PATH} \ | ||
-t ${IMAGE_REGISTRY} . | ||
@if [ "$(PUSH)" = "true" ]; then docker push ${IMAGE_REGISTRY}; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/sh | ||
set -eux | ||
|
||
python example.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from collections.abc import AsyncIterable | ||
|
||
from pynumaflow.batchmapper import ( | ||
Message, | ||
Datum, | ||
BatchMapper, | ||
BatchMapAsyncServer, | ||
BatchResponses, | ||
BatchResponse, | ||
) | ||
|
||
|
||
class Flatmap(BatchMapper): | ||
""" | ||
This is a class that inherits from the BatchMapper class. | ||
It implements a flatmap operation over a batch of input messages | ||
""" | ||
|
||
async def handler( | ||
self, | ||
datums: AsyncIterable[Datum], | ||
) -> BatchResponses: | ||
batch_responses = BatchResponses() | ||
async for datum in datums: | ||
val = datum.value | ||
_ = datum.event_time | ||
_ = datum.watermark | ||
strs = val.decode("utf-8").split(",") | ||
batch_response = BatchResponse.from_id(datum.id) | ||
if len(strs) == 0: | ||
batch_response.append(Message.to_drop()) | ||
else: | ||
for s in strs: | ||
batch_response.append(Message(str.encode(s))) | ||
batch_responses.append(batch_response) | ||
|
||
return batch_responses | ||
|
||
|
||
if __name__ == "__main__": | ||
""" | ||
This example shows how to use the Batch Map Flatmap. | ||
We use a class as handler, but a function can be used as well. | ||
""" | ||
grpc_server = BatchMapAsyncServer(Flatmap()) | ||
grpc_server.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
apiVersion: numaflow.numaproj.io/v1alpha1 | ||
kind: Pipeline | ||
metadata: | ||
name: flatmap | ||
spec: | ||
vertices: | ||
- name: in | ||
source: | ||
# A self data generating source | ||
generator: | ||
rpu: 500 | ||
duration: 1s | ||
- name: batch-flatmap | ||
partitions: 2 | ||
metadata: | ||
annotations: | ||
numaflow.numaproj.io/batch-map: "true" | ||
scale: | ||
min: 1 | ||
udf: | ||
container: | ||
image: quay.io/numaio/numaflow-python/batch-map-flatmap:stable | ||
imagePullPolicy: Always | ||
- name: sink | ||
scale: | ||
min: 1 | ||
sink: | ||
log: {} | ||
edges: | ||
- from: in | ||
to: batch-flatmap | ||
- from: batch-flatmap | ||
to: sink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[tool.poetry] | ||
name = "batch-map-flatmap" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["Numaflow developers"] | ||
|
||
[tool.poetry.dependencies] | ||
python = "~3.10" | ||
pynumaflow = { path = "../../../"} | ||
|
||
[tool.poetry.dev-dependencies] | ||
|
||
[build-system] | ||
requires = ["poetry-core>=1.0.0"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from pynumaflow._constants import DROP | ||
|
||
from pynumaflow.batchmapper._dtypes import ( | ||
Message, | ||
Datum, | ||
BatchMapper, | ||
BatchResponses, | ||
BatchResponse, | ||
) | ||
from pynumaflow.batchmapper.async_server import BatchMapAsyncServer | ||
|
||
__all__ = [ | ||
"Message", | ||
"Datum", | ||
"DROP", | ||
"BatchMapAsyncServer", | ||
"BatchMapper", | ||
"BatchResponses", | ||
"BatchResponse", | ||
] |
Oops, something went wrong.