Builders

A Builder is a simple Runnable that can be used to create any Component post- Experiment, and export it to a local or remote location.

Builders decouple the inference logic with the experimentation logic, allowing users to iterate through inference contracts independently without needing to rerun an Experiment.

Hint

A Builder should be used to build inference engines that rely on previous experiments’ artifacts.

Motivation

Let’s assume that a user wants to train a binary classifier using an Experiment:

ext: /path/to/my/extensions
---
!Experiment
..
pipeline:
    ...
    model: !ext.MyBinaryClassifier

Now, the user needs to implement an inference object ClassifierEngine that has a method predict that performs the forward pass on the trained model:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from flambe.nn import Module
from flambe.compile import Component

class ClassifierEngine(Component):

   def __init__(self, model: Module):
      self.model = model

   def predict(self, **kwargs):
      # Custom code (for example, requests to APIs)
      p = self.model(feature)
      return {"POSITIVE": p, "NEGATIVE": 1-p}

By implementing Component, the user can use a Builder to build this object:

ext: /path/to/my/extensions
---
!Builder

storage: s3
destination: my-bucket

..
component: !ClassifierEngine
    ...
    model: !ext.MyBinaryClassifier.load_from_path:
      path: /path/to/saved/modeel

The inference object will be saved in s3://my-bucket. Then the user can:

1
2
3
4
5
import flambe

inference_engine = flambe.load("s3://my-bucket")
inference_engine.predict(...)
# >> {"POSITIVE": 0.9, "NEGATIVE": 0.1}

Important

Note that the inference logic is decoupled from the Experiment. If in the future the inference logic changes, there is no need of rerunning it.

Note

Why not just implement a plain Python class and use flambe.compile.serialization.load() to get the model? Because of being a Component, this object will have all the features Component has (YAML serialization, versioning, compatibility with other Runnable implementations, among others).

How to use a builder

Usage is really simple. The most important parameters for a Builder are the Component and the destination:

!Builder

storage: [ local | s3 ]
destination: path/to/location

..
component: !MyComponent
    params1: value1
    params2: value2
    ...
    paramsN: valueN

Important

For a full list of parameters, go to Builder.

Hint

If storage is “s3”, then the destination can be an S3 bucket folder. Flambé will take care of uploading the built artifacts.

Future Work

The goal is to develop builders for different technologies. For example, a DockerBuilder that is able to build a Docker container based on a Component.