Defining your Function
Ensuring that your function can be compiled successfully.
Function supports compiling a tiny-but-growing subset of Python language constructs. Below are requirements and guidelines for compiling a Python function with Function:
Function Signature
The prediction function must be a module-level function, and must have parameter and return type annotations:
The prediction function must not have any variable-length positional or keyword arguments.
Function supports a fixed set of predictor input and output value types. Below are supported type annotations:
Floating Point Values
Floating Point Values
Floating-point input and return values should be annotated with the float
built-in type.
Unlike Python which defaults to 64-bit floats, Function will always lower a Python float
to 32 bits.
For control over the binary width of the number, use the numpy.float[16,32,64]
types:
Integer Values
Integer Values
Integer input and return values should be annotated with the int
built-in type.
Unlike Python which supports arbitrary-precision integers, Function will always lower a Python int
to 32 bits.
For control over the binary width of the integer, use the numpy.int[8,16,32,64]
types:
Boolean Values
Boolean Values
Boolean input and return values must be annotated with the bool
built-in type.
Tensor Values
Tensor Values
Tensor input and return values must be annotated with the NumPy numpy.typing.NDArray[T]
type, where T
is
the tensor element type.
You can also annotate with the np.ndarray
type, but doing so will always assume a float32
element type (following
PyTorch semantics).
Below are the supported element types:
Numpy data type | Function data type |
---|---|
np.float16 | float16 |
np.float32 | float32 |
np.float64 | float64 |
np.int8 | int8 |
np.int16 | int16 |
np.int32 | int32 |
np.int64 | int64 |
np.uint8 | uint8 |
np.uint16 | uint16 |
np.uint32 | uint32 |
np.uint64 | uint64 |
bool | bool |
Function does not yet support complex numbers or tensors.
Function only supports, and will always assume, little-endian ordering for multi-byte element types.
String Values
String Values
String input and return values must be annotated with the str
built-in type.
List Values
List Values
List input and return values must be annotated with the list[T]
built-in type, where T
is the element type.
When the list element type T
is a Pydantic BaseModel
, a full JSON schema will be generated.
Providing an element type T
is optional but strongly recommended because it is used to generate a schema for the parameter or
return value.
Dictionary Values
Dictionary Values
Dictionary input and return values can be annotated in one of two ways:
- Using a Pydantic
BaseModel
subclass. - Using the
dict[str, T]
built-in type.
We strongly recommend the Pydantic BaseModel
annotation, as it allows us to generate a full JSON schema.
When using the dict
annotation, they key type must be str
. The value type T
can be any arbitrary type.
Image Values
Image Values
Image input and return values must be annotated with the Pillow
PIL.Image.Image
type.
Binary Values
Binary Values
Binary input and return values can be annotated in one of three ways:
- Using the
bytes
built-in type. - Using the
bytearray
built-in type. - Using the
io.BytesIO
type.
Function Body
The function body can contain arbitrary Python code. Given that the Function compiler is currently a proof of concept, it has limited coverage. Below is a list of Python language features that we either partially support, or do not support at all:
Functions
Functions
Statement | Status | Notes |
---|---|---|
Recursive functions | 🔨 | Recursive functions must have a return type annotation. |
Lambda expressions | 🚧 | Lambda expressions can be invoked, but cannot be used as objects. |
Literals
Literals
Collection | Status | Notes |
---|---|---|
List literals | 🔨 | List must contain primitive members (e.g. int , str ). |
Dictionary literals | 🔨 | Dictionary must contain primitive members (e.g. int , str ). |
Set literals | 🔨 | Set must contain primitive members (e.g. int , str ). |
Tuple literals | 🔨 | Tuple must contain primitive members (e.g. int , str ). |
Classes
Classes
Tracing through classes is not yet supported.
Exceptions
Exceptions
Statement | Status | Notes |
---|---|---|
raise statements | 🔨 | |
try..except statement | 🔨 |
Over time the list of unsupported language features will shrink and eventually, will be empty.
Library Coverage
We are adding support for popular libraries, across tensor frameworks, scientific computing, and more:
Supported Libraries
Supported Libraries
Below are libraries currently supported by our compiler:
If you need a specific library to be supported by the Function compiler, reach out to us.