Tensorlake applications
Tensorlake applications are the top-level decorators that define your applications. You can define as many applications as you want in your project. Each one of them will be assigned a unique HTTP entry point based on the name of the Python function.Configuring Tensorlake applications
The@application decorator allows you to specify the following attributes:
tags- dict of tags to categorize the application.retries- Retry policy for every function in the application unless a function specifies its own retry policy. No retries by default if function failed. See Retries.region- The region where every function in the application will be deployed unless a function specifies its own region. Eitherus-east-1oreu-west-1. The default is any of the regions.input_deserializer- The deserializer to use for the application input. Eitherjsonorpickle. The default isjson.output_serializer- The serializer to use for the application output. Eitherjsonorpickle. The default isjson.
Tensorlake functions
Tensorlake functions are the building blocks of applications. They are Python functions decorated with the@function decorator. They can be used to perform any isolated computation.
Calling other Tensorlake functions
Tensorlake functions can call other Tensorlake functions. The called function is executed in its own function container. The function call blocks until the called function returns its output to the calling function.Application input and output
Application functions decorated with@application() always take a single argument which is the current request input.
The input value is deserialized using input_deserializer (JSON by default).
For example if your application function takes a single str argument, and the application function input_deserializer is JSON, then the request input should be a JSON string:
request input
request output
request input
response output
Non-application function parameters and return types
Functions that don’t have the@application() decorator can take multiple arguments and return multiple values.
Such functions use Python pickle serialization format to serialize and deserialize their arguments and returned values.
So you can use any Python data type when calling non-application functions, including complex nested structures as long as they
are picklable which is true in most cases. Some notable exceptions are file objects, database connections, threads, etc.
Any valid function call signature is supported including positional and keyword arguments.
Configuring Tensorlake functions
The@function decorator allows you to set the following attributes:
description- A description of the function’s purpose and behavior.cpu- The number of CPUs available to the function. The default is1.0CPU. See CPU.memory- The memory GB available to the function. The default is1.0GB. See Memory.ephemeral_disk- The ephemeral/tmpdisk space available to the function in GB. The default is2.0GB. See Ephemeral Disk.gpu- The GPU model available to the function. The default isNone(no GPU). Please contactsupport@tensorlake.aito enable GPU support.timeout- The timeout for the function in seconds. The default is 5 minutes. See Timeouts.image- The image to use for the function container. A basic Debian based image by default. See Images.secrets- The secrets available to the function in its environment variables. No secrets by default. See Secrets.retries- Retry policy for the function. No retries by default if function failed. See Retries.region- The region where the function will be deployed. Eitherus-east-1oreu-west-1. The default is any of the regions.
Classes
Sometimes a function needs one time initialization, like loading a large model into memory. This is acheived by defining a class using@cls decorator. Classes use their __init__(self) constructor to run any initialization code once on function container startup.
The constructor can not have any arguments other than self. Any number of class methods can be decorated with @function.
Timeouts
When a function runs longer than its timeout, it is terminated and marked as failed. The timeout in seconds is set using thetimeout attribute.
The default timeout is 300 (5 minutes). Minimum is 1, maximum is 172800 (48 hours). Progress updates can be sent by the function to extend the
timeout. See Request Context.
Retries
When a function fails by raising an exception or timing out, it gets retried according to its retry policy. The default retry policy is to not retry the function call. You can specify a custom retry policy using theretries attribute.
Request Context
Functions can use a request context to share state between function calls of the same request. The context has information about the current request and provides access to Tensorlake APIs for the current request. You can access the request context directly from theRequestContext class.
ctx.progress.update(...) after 2 minutes of execution,
then the timeout is reset to 4 minutes from that point, allowing the function to run for another 4 minutes.
CPU
The number of CPUs available to the function is set using thecpu attribute. Minimum is 1.0, maximum is 8.0.
The default is 1.0. This is usually sufficient for functions that only call external APIs and do simple data processing.
Adding more CPUs is recommended for functions that do complex data processing or work with large datasets.
If functions use large multy-gigabyte inputs or produce large multi-gigabyte outputs, then at least 3 CPUs are recommended.
This results in the fastest download and upload speeds for the data.
Memory
GB memory available to the function is set using thememory attribute. Minimum is 1.0, maximum is 32.0.
The default is 1.0. This is usually sufficient for functions that only call external APIs and do simple data processing.
Adding more memory is recommended for functions that do complex data processing or work with large datasets.
It’s recommended to set memory to at least 2x the size of the largest inputs and outputs of the function.
This is because when the inputs/outputs are deserialized/serialized both serialized and deserialized representations are
kept in memory.
Ephemeral disk
Ephemeral disk space is a temporary storage space available to functions at/tmp path. It gets erased when its
function container gets terminated. It’s optimal for storing temporary files that are not needed after the function
execution is completed. Ephemeral disks are backed by fast SSD drives. Using other filesystem paths like /home/ubuntu
for storing temporary files will result in slower performance. Temporary files created using Python modules like tempfile
are stored in ephemeral disk space inside /tmp.
GB of ephemeral disk space available to the function is set using ephemeral_disk attribute. Minimum is 2.0, maximum is 50.0.
The default is 2.0 GB. This is usually sufficient for functions that only call external APIs and do simple data processing.
If the function needs to temporarily store large files or datasets on disk, then the ephemeral_disk attribute should be increased
accordingly.