Orchestrate a Tracked Training Run
In this part of the docs, we will walk through how to execute a training job on a Kubernetes cluster using SigOpt. SigOpt should now be connected to a Kubernetes cluster of your choice.

Set Up

Then, test whether or not you are connected to a cluster with SigOpt by running:
1
$ sigopt cluster test
Copied!
SigOpt will output:
1
Successfully connected to kubernetes cluster: tiny-cluster
Copied!
If you're using a custom Kubernetes cluster, you will need to install plugins to get the controller image working:
1
$ sigopt cluster install-plugins
Copied!
SigOpt works when all of the files for your model are located in the same folder. So, please create an example directory (mkdir), and then change directories (cd) into that directory:
1
$ mkdir example && cd example
Copied!
Then auto-generate templates for a Dockerfile and an SigOpt Configuration YAML file
1
$ sigopt init
Copied!
Next, you will create some files and put them in this example directory.

Dockerfile: Define your model environment

For the tutorial, we'll be using a very simple Dockerfile. For instructions on how to specify more requirements see our guide on Dockerfiles. Please copy and paste the following snippet into the autogenerated file named Dockerfile.
1
FROM python:3.9
2
3
RUN pip install --no-cache-dir sigopt
4
5
RUN pip install --no-cache-dir scipy==1.7.1
6
RUN pip install --no-cache-dir scikit-learn==0.24.2
7
RUN pip install --no-cache-dir numpy==1.21.2
8
9
COPY . /sigopt
10
WORKDIR /sigopt
Copied!

Define a Model

This code defines a simple SGDClassifier model that measures accuracy classifying labels for the Iris flower dataset. Copy and paste the snippet below to a file titled model.py. Note the snippet below uses SigOpt’s Runs to track model attributes.
1
# model.py
2
# SGDClassifier example
3
4
# You'll use the SigOpt Training Runs API to communicate with SigOpt
5
# while your model is running on the cluster.
6
import sigopt
7
8
9
# These packages will need to be installed in order to run your model.
10
# To do this, define a requirements.txt file, and provide instructions
11
from sklearn import datasets
12
from sklearn.linear_model import SGDClassifier
13
from sklearn.model_selection import cross_val_score
14
import numpy
15
16
# https://en.wikipedia.org/wiki/Iris_flower_data_set
17
def load_data():
18
iris = datasets.load_iris()
19
return (iris.data, iris.target)
20
21
22
def evaluate_model(X, y):
23
sigopt.params.setdefaults(
24
loss="log",
25
penalty="elasticnet",
26
log_alpha=-4,
27
l1_ratio=0.15,
28
max_iter=1000,
29
tol=0.001,
30
)
31
classifier = SGDClassifier(
32
loss=sigopt.params.loss,
33
penalty=sigopt.params.penalty,
34
alpha=10 ** sigopt.params.log_alpha,
35
l1_ratio=sigopt.params.l1_ratio,
36
max_iter=sigopt.params.max_iter,
37
tol=sigopt.params.tol,
38
)
39
cv_accuracies = cross_val_score(classifier, X, y, cv=5)
40
return (numpy.mean(cv_accuracies), numpy.std(cv_accuracies))
41
42
43
# Each execution of model.py should represent one evaluation of your model.
44
# When this file is run, it loads data, evaluates the model using assignments
45
if __name__ == "__main__":
46
(X, y) = load_data()
47
(mean, std) = evaluate_model(X=X, y=y)
48
print("Accuracy: {} +/- {}".format(mean, std))
49
sigopt.log_metric("accuracy", mean, std)
Copied!

Notes on implementing your model

When your model runs on a node in the cluster it can use all of the CPUs on that node with multithreading. This is good for performance if your model is the only process running on the node, but in many cases it will need to share those CPUs with other processes (ex. other model runs). For this reason it is a good idea to limit the number of threads that your model library can create in conjunction with the amount of cpu specified in your resources_per_model. This varies by implementation, but some common libraries are listed below:
Numpy
Threads spawned by Numpy can be configured with environment variables, which can be set in your Dockerfile:
1
ENV MKL_NUM_THREADS=N
2
ENV NUMEXPR_NUM_THREADS=N
3
ENV OMP_NUM_THREADS=N
Copied!
Tensorflow/Keras
Can be configured in the Tensorflow module, see: https://www.tensorflow.org/api_docs/python/tf/config/threading
PyTorch
Can be configured in the PyTorch module, see: https://pytorch.org/docs/stable/generated/torch.set_num_threads.html

Create an orchestration configuration

Here's a sample SigOpt configuration file that specifies a training run for the model.py specified above on one CPU.
Please copy and paste the following to a file named run.yml.
1
# Choose a descriptive name for your model
2
name: SGD Classifier
3
4
# Here, we run the model
5
run: python model.py
6
resources:
7
requests:
8
cpu: 0.5
9
memory: 512Mi
10
limits:
11
cpu: 2
12
memory: 512Mi
13
14
# We don't need any GPUs for this example, so we'll leave this commented out
15
# gpus: 1
16
17
# SigOpt creates a container for your model. Since we're using an AWS
18
# cluster, it's easy to securely store the model in the Amazon Elastic Container Registry.
19
# Choose a descriptive and unique name for each new experiment configuration file.
20
image: sgd-classifier
Copied!

Execute

So far, SigOpt is connected to your cluster, the Dockerfile defines your model requirements, and you've updated the SigOpt configuration file. Now is a good time to test that you can create your run and verify that your model code works in the cluster.
1
$ sigopt cluster test-run -r run.yml
Copied!
Once you are confident that your runs will finish you can kick one off in the background and continue your experimentation.
1
$ sigopt cluster run -r run.yml
Copied!
Note that we can also directly run the python script we execute in the run section of run.yml.
1
$ sigopt cluster run python model.py
Copied!

Monitor

You can monitor the status of SigOpt Runs from the command line using the run name or the Run ID.
1
$ sigopt cluster status run/99999
Copied!
1
run/99999:
2
Run Name: run-jwc5fyyr
3
Link: https://app.sigopt.com/run/99999
4
State: failed
5
Experiment link: https://app.sigopt.com/experiment/111111
6
Suggestion id: 42613040
7
Observation id: 28531050
8
Pod phase: Deleted
9
Node name: ip-111-11-11-111.us-west-2.compute.internal
10
bFollow logs: sigopt cluster kubectl logs pod/run-jwc5fyyr -f
Copied!
The status will include a command that you can run in your terminal to follow the logs as they are generated by your code.
You can see all of the activity on your cluster with the following command:
1
$ sigopt cluster status
Copied!
1
You are currently connected to the cluster: test-cluster
2
Experiments: 1 total
3
Experiment 374876: 45 runs
4
Succeeded: 41 runs
5
Pending: 3 runs
6
run-868o4gou Pending
7
run-shb0yvxd Pending
8
run-t8co05nt Pending
9
Running: 1 runs
10
run-dba7gdlc Running
11
Nodes: 1 total
12
ip-111-11-11-111.us-west-2.compute.internal:
13
cpu:
14
Allocatable: 1.93 CPU
15
Requests: 250.00 mCPU, 12.95 %
16
Limits: 250.00 mCPU, 12.95 %
17
memory:
18
Allocatable: 7.44 GB
19
Requests: 1.07 GB, 14.44 %
20
Limits: 1.07 GB, 14.44 %
Copied!
1
$ sigopt cluster status
Copied!

Monitor progress in the web app

You can monitor training run progress on https://app.sigopt.com/run/[id].
At the top of the page under the training run name, you’ll find the status of the run. Once the run is completed, the Performance and Metric sections will fill in.

Stop

You can stop an in progress run and mark it as failed on SigOpt website by archiving it.
1
sigopt cluster stop run/<run-id>
Copied!