Overview

Functions and models are the building blocks of creating applications deployed on Sieve.

Models are defined as Python classes with __setup__() and __predict()__ methods. The __setup()__ method is run once during initialization, and the __predict()__ method is run on every call to the model. The __setup__() method is typically used for heavy one time operations, such as loading machine learning model weights into memory.

Functions are just models under the hood, but are defined by just a single function. Use a function when a __setup__() call is not required.

All code is mounted by default into /src of the container.

Creating Functions and Models

Functions are defined with the @sieve.function decorator. Here is a minimal example of a function:

import sieve

@sieve.function(name="multiplier")
def multiply(x1: int, x2: int) -> int:
    return x1 * x2

Note the type hints in the function’s arguments. These are not strictly required, but are recommended. Type hints are picked up by the Sieve dashboard, and are used to generate the job submission form.

Similarly, models are marked with the @sieve.Model decorator. Here is a minimal example of a Model:

import sieve

@sieve.Model(name="adder")
class Adder:
    def __setup__(self):
        print("setting up!")
    def __predict__(self, x1: int, x2: int) -> int:
        return x1 + x2

Calling Functions and Models

Functions and models already deployed on sieve can be referenced by using

referenced_function = sieve.function.get("{username}/{function name}")

There are two options for running referenced_function on the Sieve cloud.

run

The .run() method blocks and waits for the function to complete, and directly returns the value returned by the function.

import sieve

mult = sieve.function.get("username/multiplier")
product1 = mult.run(2, 3)
product2 = mult.run(product1, 4)

push

The .push() method returns a concurrent.futures.future object, which resolves to the value returned by the function. You can later access the value of the future using the .result() method, for example. This is useful for when you are pushing several jobs at once, like when processing frames from a video. This ensures that you are not waiting for previous frames to finish processing before pushing the next ones, and are taking full advantage of parallel processing.

import sieve

mult = sieve.function.get("username/multiplier")
multiplication_inputs = [4, 8, 15, 16, 23, 42]
multiplication_jobs = []

# send all of the jobs first
for multiplication_input in multiplication_inputs:
    multiplication_jobs.append(mult.push(2, multiplication_input))

# then, once everything is submitted, grab the results
for multiplication_job in multiplication_jobs:
    print(multiplication_job.result())

You can call both .run() and .push() locally, or within another Sieve function or Model. Calling .run() or .push() within Sieve, allows you to chain function calls together, enabling powerful AI applications!

Specifying Additional Data for Functions and Models

By default, Sieve functions and models live inside Linux containers without a GPU and a limited number of python packages installed. You can use a number of key word arguments in the Model/function decorator in order to customize the environment.

ParameterTypeDescriptionDefault
namestrDefines the name of the functionThis field is required
gpusieve.gpuDetermines whether the function is deployed on a server with a GPU, which GPU it should be deployed to, . See the Parameter column in the table in GPU Acceleration for a list of options and for more information about GPU sharing. The default value runs on a machine with no GPU. Any other value is specified with a call to one of the sieve.gpu constructorsNone
python_versionstrDetermines the python version installedDefault is determined by python packages
python_packagesList[str]List of python packages to be installed during buildsieve and its dependencies will be installed by default
system_packagesList[str]List of Linux packages to be installed during build[]
cuda_versionstrVersion of cuda to be installed (for models with gpu enabled). See below for possible versions.Default is determined by python packages
run_commandsList[str]List of shell commands to be run during build. Note: these commands currently do not have access to the uploaded code[]
metadatasieve.MetadataExtra information about the model to be shown on the dashboard. See below for example.None

Using Metadata

Metadata can be used to provide additional information on the dashboard, including a full title, a description, a code reference, tags, and a readme. You can create a metadata object as follows:

metadata = sieve.Metadata(
    title="Adder",
    description="adds two numbers together",
    code_url="https://github.com/username/repo/blob/main/adder/main.py",
    image=sieve.Image(
        url="https://example.com/cover.gif"
    ),
    tags=["Math", "Example"],
    readme="This is an example readme",
)

Then, pass it into the decorator:

@sieve.Model(
		name="adder",
		metadata=metadata
)

Cuda Versions

The CUDA version can be specified with the cuda_version parameter in the function decorator. Possible values for cuda_version are:

'8.0'
'9.0'
'9.1'
'9.2'
'10.0'
'10.1'
'10.2'
'11.0.3'
'11.1.1'
'11.2.0'
'11.2.1'
'11.2.2'
'11.3.0'
'11.3.1'
'11.4.0'
'11.4.1'
'11.4.2'
'11.4.3'
'11.5.0'
'11.5.1'
'11.5.2'
'11.6.0'
'11.6.1'
'11.6.2'
'11.7.0'
'11.7.1'
'11.8.0'