Skip to content

Commit a986b22

Browse files
committed
adding documentation for python UDF's
1 parent 9181305 commit a986b22

File tree

1 file changed

+74
-5
lines changed

1 file changed

+74
-5
lines changed

docs/user_guides/fs/transformation_functions.md

+74-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ In AI systems, [transformation functions](https://www.hopsworks.ai/dictionary/tr
55

66
## Custom Transformation Function Creation
77

8-
User-defined transformation functions can be created in Hopsworks using the [`@udf`](http://docs.hopsworks.ai/hopsworks-api/{{{hopsworks_version}}}/generated/api/udf/) decorator. These functions should be designed as Pandas functions, meaning they must take input features as a [Pandas Series](https://pandas.pydata.org/docs/reference/api/pandas.Series.html) and return either a Pandas Series or a [Pandas DataFrame](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html). Hopsworks automatically executes the defined transformation function as a [`pandas_udf`](https://spark.apache.org/docs/3.1.2/api/python/reference/api/pyspark.sql.functions.pandas_udf.html) in a PySpark application and as Pandas functions in Python clients.
8+
User-defined transformation functions can be created in Hopsworks using the [`@udf`](http://docs.hopsworks.ai/hopsworks-api/{{{hopsworks_version}}}/generated/api/udf/) decorator. These functions can be either vectorized or implemented as pure Python or Pandas UDFs (User-Defined Functions).
9+
10+
Hopsworks provides three execution modes to control the execution of transformation functions during training dataset generations, batch inference, and online inference. By default, Hopsworks assumes that the defined transformation function is vectorized. It will execute the function as a Python UDF during online inference and as a Pandas UDF during batch inference and training dataset generation. While Python UDFs are faster for small data volumes, Pandas UDFs offer better performance for large datasets. This execution mode provides the optimal balance based on the data size across training dataset generations, batch inference, and online inference. You can also explicitly specify the execution mode as either `python` or `pandas`, forcing the transformation function to always run as a Python or Pandas UDF, respectively.
11+
12+
A Pandas UDF in Hopsworks accepts one or more Pandas Series as input and can return either one or more Series or a Pandas DataFrame. When integrated with PySpark applications, Hopsworks automatically executes Pandas UDFs using PySpark’s [`pandas_udf`](https://spark.apache.org/docs/3.4.1/api/python/reference/pyspark.sql/api/pyspark.sql.functions.pandas_udf.html), enabling the transformation functions to efficiently scale for large datasets.
913

1014
!!! warning "Java/Scala support"
1115

@@ -18,7 +22,9 @@ Transformation functions created in Hopsworks can be directly attached to featur
1822
Definition transformation function within a Jupyter notebook is only supported in Python Kernel. In a PySpark Kernel transformation function have to defined as modules or added when starting a Jupyter notebook.
1923

2024

21-
The `@udf` decorator in Hopsworks creates a metadata class called [`HopsworksUdf`](http://docs.hopsworks.ai/hopsworks-api/{{{hopsworks_version}}}/generated/api/hopsworks_udf/). This class manages the necessary operations to execute the transformation function. The decorator has two arguments `return_type` and `drop`. The `return_type` is a mandatory argument and denotes the data types of the features returned by the transformation function. It can be a single Python type if the transformation function returns a single transformed feature or a list of Python types if it returns multiple transformed features. The supported types include `str`, `int`, `float`, `bool`, `datetime.datetime`, `datetime.date`, and `datetime.time`. The `drop` argument is optional and specifies the input arguments to remove from the final output after all transformation functions are applied. By default, all input arguments are retained in the final transformed output. The supported python types that be used with the `return_type` argument are provided as a table below
25+
The `@udf` decorator in Hopsworks creates a metadata class called [`HopsworksUdf`](http://docs.hopsworks.ai/hopsworks-api/{{{hopsworks_version}}}/generated/api/hopsworks_udf/). This class manages the necessary operations to execute the transformation function. The decorator has three arguments `return_type`, `drop` and `mode`.
26+
27+
The `return_type` is a mandatory argument and denotes the data types of the features returned by the transformation function. It can be a single Python type if the transformation function returns a single transformed feature or a list of Python types if it returns multiple transformed features. The supported types include `str`, `int`, `float`, `bool`, `datetime.datetime`, `datetime.date`, and `datetime.time`. The supported python types that be used with the `return_type` argument are provided as a table below
2228

2329
| Supported Python Types |
2430
|--------------------------|
@@ -30,8 +36,11 @@ The `@udf` decorator in Hopsworks creates a metadata class called [`HopsworksUdf
3036
| datetime.date |
3137
| datetime.time |
3238

39+
The `drop` argument is optional and specifies the input arguments to remove from the final output after all transformation functions are applied. By default, all input arguments are retained in the final transformed output.
3340

34-
Hopsworks supports four types of transformation functions:
41+
The `mode` argument controls the execution mode of transformation functions and accepts three values: `default`, `python`, or `pandas`. The `default` mode assumes the function can be executed as both a Python and Pandas UDF, the transformation function in this mode is executed as a Python UDF for online inference and as a Pandas UDF for batch inference and training dataset generation. Setting mode to `pandas` forces the function to always run as a Pandas UDF, while setting the mode to `python` ensures it always runs as a Python UDF.
42+
43+
Hopsworks supports four types of transformation functions across all execution modes:
3544

3645
1. One-to-one: Transforms one feature into one transformed feature.
3746
2. One-to-many: Transforms one feature into multiple transformed features.
@@ -80,7 +89,7 @@ To create a one-to-many transformation function, the Hopsworks `@udf` decorato
8089

8190
@udf(return_type=[int, int])
8291
def add_one_and_two(feature1):
83-
return pd.DataFrame({"add_one":feature1 + 1, "add_two":feature1 + 2})
92+
return feature1 + 1, feature1 + 2
8493
```
8594

8695
### Many-to-many transformations
@@ -94,8 +103,68 @@ The creation of a many-to-many transformation function is similar to that of a o
94103
import pandas as pd
95104

96105
@udf(return_type=[int, int, int])
106+
def add_one_multiple(feature1, feature2, feature3):
107+
return feature1 + 1, feature2 + 1, feature3 + 1
108+
```
109+
110+
### Specifying execution modes
111+
112+
The `mode` parameter of the `@udf` decorator can be used to specify the execution mode of the transformation function.
113+
114+
#### Default
115+
This execution mode assumes that the transformation function is vectorized and can be executed as both a Pandas UDF and a Python UDF. It serves as the default mode used when the `mode` parameter is not specified. In this mode, the transformation function is executed as a Pandas UDF during training and in the batch inference pipeline, while it operates as a Python UDF during online inference.
116+
117+
118+
=== "Python"
119+
!!! example "Creating a many to many transformations function using the default execution mode"
120+
```python
121+
from hopsworks import udf
122+
import pandas as pd
123+
124+
# "default" mode is used if the parameter `mode` is not explicitly set.
125+
@udf(return_type=[int, int, int], drop=["feature1", "feature3"])
126+
def add_one_multiple(feature1, feature2, feature3):
127+
return feature1 + 1, feature2 + 1, feature3 + 1
128+
129+
@udf(return_type=[int, int, int], mode="default" drop=["feature1", "feature3"])
130+
def add_two_multiple(feature1, feature2, feature3):
131+
return feature1 + 2, feature2 + 2, feature3 + 2
132+
```
133+
134+
#### Python
135+
The transformation function can be configured to always execute as a Python UDF by setting the `mode` parameter of the `@udf` decorator to `python`.
136+
137+
138+
=== "Python"
139+
!!! example "Creating a many to many transformation function as a Python UDF"
140+
```python
141+
from hopsworks import udf
142+
import pandas as pd
143+
144+
@udf(return_type=[int, int, int], mode = "python", drop=["feature1", "feature3"])
145+
def add_one_multiple(feature1, feature2, feature3):
146+
return feature1 + 1, feature2 + 1, feature3 + 1
147+
```
148+
149+
#### Pandas
150+
The transformation function can be configured to always execute as a Pandas UDF by setting the `mode` parameter of the `@udf` decorator to `pandas`.
151+
152+
153+
=== "Python"
154+
!!! example "Creating a many to many transformations function as a Pandas UDF"
155+
```python
156+
from hopsworks import udf
157+
import pandas as pd
158+
159+
# A Pandas UDF returning a Pandas DataFrame
160+
@udf(return_type=[int, int, int], mode = "pandas", drop=["feature1", "feature3"])
97161
def add_one_multiple(feature1, feature2, feature3):
98162
return pd.DataFrame({"add_one_feature1":feature1 + 1, "add_one_feature2":feature2 + 1, "add_one_feature3":feature3 + 1})
163+
164+
# A Pandas UDF returning multiple Pandas Series
165+
@udf(return_type=[int, int, int], mode="pandas" drop=["feature1", "feature3"])
166+
def add_two_multiple(feature1, feature2, feature3):
167+
return feature1 + 2, feature2 + 2, feature3 + 2
99168
```
100169

101170
### Dropping input features
@@ -111,7 +180,7 @@ The `drop` parameter of the `@udf` decorator is used to drop specific column
111180

112181
@udf(return_type=[int, int, int], drop=["feature1", "feature3"])
113182
def add_one_multiple(feature1, feature2, feature3):
114-
return pd.DataFrame({"add_one_feature1":feature1 + 1, "add_one_feature2":feature2 + 1, "add_one_feature3":feature3 + 1})
183+
return feature1 + 1, feature2 + 1, feature3 + 1
115184
```
116185

117186
### Training dataset statistics

0 commit comments

Comments
 (0)