Skip to content

Commit 33d4e6b

Browse files
authored
docs: add docs for enable batching in custom functions (#1238)
docs: add docs for enable batching in custom fucntions
1 parent 194dd90 commit 33d4e6b

File tree

1 file changed

+62
-26
lines changed

1 file changed

+62
-26
lines changed

docs/docs/custom_ops/custom_functions.mdx

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import TabItem from '@theme/TabItem';
88

99
A custom function can be defined in one of the following ways:
1010

11-
* A standalone function. It's simpler and doesn't allow additional configurations and setup logic.
12-
* A function spec and an executor. It's more powerful, allows additional configurations and setup logic.
11+
* A standalone function. It's simpler and doesn't allow additional configurations and setup logic.
12+
* A function spec and an executor. It's more powerful, allows additional configurations and setup logic.
1313

1414
## Option 1: By a standalone function
1515

@@ -31,9 +31,9 @@ def compute_something(arg1: str, arg2: int | None = None) -> str:
3131

3232
Notes:
3333

34-
* The `cocoindex.op.function()` function decorator also takes optional parameters.
34+
* The `cocoindex.op.function()` function decorator also takes optional parameters.
3535
See [Parameters for custom functions](#parameters-for-custom-functions) for details.
36-
* Types of arguments and the return value must be annotated, so that CocoIndex will have information about data types of the operation's output fields.
36+
* Types of arguments and the return value must be annotated, so that CocoIndex will have information about data types of the operation's output fields.
3737
See [Data Types](/docs/core/data_types) for supported types.
3838

3939
</TabItem>
@@ -43,12 +43,11 @@ Notes:
4343

4444
The cocoindex repository contains the following examples of custom functions defined in this way:
4545

46-
* In the [code_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/code_embedding/main.py) example,
46+
* In the [code_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/code_embedding/main.py) example,
4747
`extract_extension` is a custom function to extract the extension of a file name.
48-
* In the [manuals_llm_extraction](https://github.com/cocoindex-io/cocoindex/blob/main/examples/manuals_llm_extraction/main.py) example,
48+
* In the [manuals_llm_extraction](https://github.com/cocoindex-io/cocoindex/blob/main/examples/manuals_llm_extraction/main.py) example,
4949
`summarize_manuals` is a custom function to summarize structured information of a manual page.
5050

51-
5251
## Option 2: By a function spec and an executor
5352

5453
This is more advanced and flexible way to define a custom function.
@@ -75,30 +74,29 @@ class ComputeSomething(cocoindex.op.FunctionSpec):
7574
```
7675

7776
Notes:
78-
* All fields of the spec must have a type serializable / deserializable by the `json` module.
79-
* All subclasses of `FunctionSpec` can be instantiated similar to a dataclass, i.e. `ClassName(param1=value1, param2=value2, ...)`.
77+
78+
* All fields of the spec must have a type serializable / deserializable by the `json` module.
79+
* All subclasses of `FunctionSpec` can be instantiated similar to a dataclass, i.e. `ClassName(param1=value1, param2=value2, ...)`.
8080

8181
</TabItem>
8282
</Tabs>
8383

84-
8584
### Function Executor
8685

8786
A function executor defines behavior of a function. It's instantiated for each operation that uses this function.
8887

8988
The function executor is responsible for:
9089

91-
* *Prepare* for the function execution, based on the spec.
90+
* *Prepare* for the function execution, based on the spec.
9291
It happens once and only once before execution.
9392
e.g. if the function calls a machine learning model, the model name can be a parameter as a field of the spec, and we may load the model in this phase.
94-
* *Run* the function, for each specific input arguments. This happens multiple times, for each specific row of data.
93+
* *Run* the function, for each specific input arguments. This happens multiple times, for each specific row of data.
9594

9695
<Tabs>
9796
<TabItem value="python" label="Python" default>
9897

9998
A function executor is defined as a class decorated by `@cocoindex.op.executor_class()`.
10099

101-
102100
```python
103101
@cocoindex.op.executor_class(...)
104102
class ComputeSomethingExecutor:
@@ -114,12 +112,12 @@ class ComputeSomethingExecutor:
114112

115113
Notes:
116114

117-
* The `cocoindex.op.executor_class()` class decorator also takes optional parameters.
115+
* The `cocoindex.op.executor_class()` class decorator also takes optional parameters.
118116
See [Parameters for custom functions](#parameters-for-custom-functions) for details.
119117

120-
* A `spec` field must be present in the class, and must be annotated with the spec class name.
121-
* The `prepare()` method is optional. It's executed once and only once before any `__call__` execution, to prepare the function execution.
122-
* The `__call__()` method is required. It's executed for each specific rows of data.
118+
* A `spec` field must be present in the class, and must be annotated with the spec class name.
119+
* The `prepare()` method is optional. It's executed once and only once before any `__call__` execution, to prepare the function execution.
120+
* The `__call__()` method is required. It's executed for each specific rows of data.
123121
Types of arugments and the return value must be decorated, so that CocoIndex will have information about data types of the operation's output fields.
124122
See [Data Types](/docs/core/data_types) for supported types.
125123

@@ -130,34 +128,37 @@ Notes:
130128

131129
The cocoindex repository contains the following examples of custom functions defined in this way:
132130

133-
* In the [pdf_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/pdf_embedding/main.py) example, we define a custom function `PdfToMarkdown`
134-
* The `SentenceTransformerEmbed` function shipped with the CocoIndex Python package is defined by Python SDK.
131+
* In the [pdf_embedding](https://github.com/cocoindex-io/cocoindex/blob/main/examples/pdf_embedding/main.py) example, we define a custom function `PdfToMarkdown`
132+
* The `SentenceTransformerEmbed` function shipped with the CocoIndex Python package is defined by Python SDK.
135133
Search for [`SentenceTransformerEmbedExecutor`](https://github.com/search?q=repo%3Acocoindex-io%2Fcocoindex+lang%3Apython+SentenceTransformerEmbedExecutor&type=code) to see the code.
136134

137135
## Parameters for custom functions
138136

139137
Custom functions take the following additional parameters:
140138

141-
* `gpu: bool`: Whether the executor will use GPU. It will affect the way the function is scheduled.
139+
* `gpu: bool`: Whether the executor will use GPU. It will affect the way the function is scheduled.
142140

143-
* `cache: bool`: Whether the executor will enable cache for this function.
141+
* `cache: bool`: Whether the executor will enable cache for this function.
144142
When `True`, the executor will cache the result of the function for reuse during reprocessing.
145143
We recommend to set this to `True` for any function that is computationally intensive.
146144

147-
* `behavior_version: int`: The version of the behavior of the function.
145+
* `batching: bool`: Whether the executor will consume requests in batch.
146+
See the [Batching](#batching) section below for details.
147+
148+
* `behavior_version: int`: The version of the behavior of the function.
148149
When the version is changed, the function will be re-executed even if cache is enabled.
149150
It's required to be set if `cache` is `True`.
150151

151-
* `arg_relationship: tuple[ArgRelationship, str]`: It specifies the relationship between an input argument and the output,
152+
* `arg_relationship: tuple[ArgRelationship, str]`: It specifies the relationship between an input argument and the output,
152153
e.g. `(ArgRelationship.CHUNKS_BASE_TEXT, "content")` means the output is chunks for the text represented by the
153154
input argument with name `content`.
154155
This provides metadata for tools, e.g. CocoInsight.
155156
Currently the following attributes are supported:
156157

157-
* `ArgRelationship.CHUNKS_BASE_TEXT`:
158+
* `ArgRelationship.CHUNKS_BASE_TEXT`:
158159
The output is chunks for the text represented by the input argument. In this case, the output is expected to be a *Table*, whose each row represents a text chunk, and the first column has type *Range*, representing the range of the text chunk.
159-
* `ArgRelationship.EMBEDDING_ORIGIN_TEXT`: The output is embedding vector for the text represented by the input argument. The output is expected to be a *Vector*.
160-
* `ArgRelationship.RECTS_BASE_IMAGE`: The output is rectangles for the image represented by the input argument. The output is expected to be a *Table*, whose each row represents a rectangle, and the first column has type *Struct*, with fields `min_x`, `min_y`, `max_x`, `max_y` to represent the coordinates of the rectangle.
160+
* `ArgRelationship.EMBEDDING_ORIGIN_TEXT`: The output is embedding vector for the text represented by the input argument. The output is expected to be a *Vector*.
161+
* `ArgRelationship.RECTS_BASE_IMAGE`: The output is rectangles for the image represented by the input argument. The output is expected to be a *Table*, whose each row represents a rectangle, and the first column has type *Struct*, with fields `min_x`, `min_y`, `max_x`, `max_y` to represent the coordinates of the rectangle.
161162

162163
For example:
163164

@@ -187,3 +188,38 @@ class ComputeSomethingExecutor:
187188

188189
</TabItem>
189190
</Tabs>
191+
192+
## Batching
193+
194+
Batching allows a function executor to process multiple function calls in batch.
195+
Sometimes batching is more efficient than processing them one by one, e.g. running inference on GPU, calling remote APIs with quota limits, etc.
196+
197+
Batching can be enabled by setting the `batching` parameter to `True` in custom function parameters.
198+
Once it's set to `True`, type of the argument and return value must be a `list`.
199+
Currently we only support batching functions taking a single argument.
200+
201+
<Tabs>
202+
<TabItem value="python" label="Python" default>
203+
204+
For example, for a CocoIndex custom function taking `str` as argument and returning `str` as result, it can be defined as:
205+
206+
```python
207+
@cocoindex.op.function(batching=True)
208+
def compute_something(args: list[str]) -> list[str]:
209+
...
210+
```
211+
212+
or for such a function defined by an executor:
213+
214+
```python
215+
@cocoindex.op.executor_class(batching=True)
216+
class ComputeSomethingExecutor:
217+
spec: ComputeSomething
218+
...
219+
220+
def __call__(self, args: list[str]) -> list[str]:
221+
...
222+
```
223+
224+
</TabItem>
225+
</Tabs>

0 commit comments

Comments
 (0)