Tensorlake functions are the building blocks of workflows. They are Python functions decorated with the @tensorlake_function decorator.
from tensorlake import tensorlake_function

image = Image()
  .run("pip install transformers")
  .build()

@tensorlake_function(image=image, input_encoding="json")
def my_function(data: str) -> int:
    return len(data)

Functions

Functions decorated with @tensorlake_function are the most basic way to express compute in Tensorlake Workflows. The decorator allows you to specify the following attributes:
  1. image - The image to use for the function.
  2. input_encoding - The encoding to use for the input of the function.
  3. output_encoding - The encoding to use for the output of the function.
  4. secrets - The secrets to use for the function.
  5. cpus - The number of CPUs to use for the function.
  6. memory - The memory to use for the function.
  7. timeout - The timeout for the function.
  8. retries - The number of retries for the function.
  9. disk - The disk to use for the function.

Classes

Sometimes you might want to do something expensive at the start of your function, like loading a large model into memory. You can express compute as a class, instead of a function to make use of constructors to fully customize the behavior of your functions. Under the hood, tensorlake_function converts your functions into a TensorlakeCompute object in the runtime.
class MyCompute(TensorlakeCompute):
    image = ...
    input_encoding = ...

    def __init__(self):
        pass

    def run(self, data: str) -> int:
        return len(data)
The same set of attributes that the tensorlake_function decorator allows you to specify are available to the TensorlakeCompute class.

Input and Output Encoding

Inputs and Outputs to functions are serialized and deserialized as JSON by default. This is a good default since the workflows are exposed as HTTP endpoints, thus making it possible to call them from any programming language. You can also change the serialization format to cloudpickle if you want to complex Python objects between functions, such as Pandas dataframes, Pytorch Tensors, PIL images, etc. The input_encoding and output_encoding attributes can be used to change the serialization format. Currently supported formats are:
  • json - JSON serialization
  • cloudpickle - Cloudpickle serialization

Request Context

Functions are injected with the request context, which allows you to access some information about the request, such as the request ID, Tensorlake Project ID where the graph is deployed, etc.
@tensorlake_function(use_ctx=True)
def my_function(ctx: RequestContext, data: str) -> int:
    return len(data)
The use_ctx flag is used to indicate that the function should be injected with the request context. It is always injected with the ctx variable in your function.

Graphs

You can string together multiple functions to form a workflow.
g = Graph(start_node=my_function, name="my_workflow", description="My workflow")
g.add_edge(my_function, my_function1)
g.add_edge(my_function, my_function2)
In the above example, my_function is the start node of the workflow. The input to the workflow is passed to my_function. Workflows are exposed as HTTP endpoints, the body of the request will be passed to the start node of the workflow, in this case my_function.
curl -X POST https://api.tensorlake.com/workflows/my_workflow \
  -H "Content-Type: application/json" \
  -d '{"data": "Hello, world!"}'

Retrieving Output

Tensorlake workflows allow retrieving the outputs of any function in the workflow.
from tensorlake import RemoteGraph

g = RemoteGraph(name="my_workflow")

g.outputs(my_function1)

Streaming Progress

You can stream the progress of your requests for interactive use-cases, to notify users about the progress of the request.
curl -N -X POST https://api.tensorlake.com/workflows/my_workflow/stream \
  -H "Content-Type: text/event-stream" \