Skip to main content

XManager: A framework for managing machine learning experiments 🧑‍🔬

NOTE: If you are looking for the private preview of XManager on Cloud (XMC), reach out to your GCP account team or xmanager_cloud@google.com. This repository only contains the client code, and not the server/UI code required for full integration.

XManager is a platform for packaging, running and keeping track of machine learning experiments. It currently enables one to launch experiments locally or on Google Cloud Platform (GCP). Interaction with experiments is done via XManager's APIs through Python launch scripts. Check out these slides for a more detailed introduction.

To get started, install XManager, its prerequisites if needed and follow the tutorial or a codelab (Colab Notebook / Jupyter Notebook) to create and run a launch script.

See CONTRIBUTING.md for guidance on contributions.

Install XManager

pip install git+https://github.com/deepmind/xmanager.git

Or, alternatively, a PyPI project is also available.

pip install xmanager

On Debian-based systems, XManager and all its dependencies can be installed and set up by cloning this repository and then running

cd xmanager/setup_scripts && chmod +x setup_all.sh && . ./setup_all.sh

Prerequisites

The codebase assumes Python 3.10+.

Install Docker (optional)

If you use xmanager.xm.PythonDocker to run XManager experiments, you need to install Docker.

  1. Follow the steps to install Docker.

  2. And if you are a Linux user, follow the steps to enable sudoless Docker.

Install Bazel (optional)

If you use xmanager.xm_local.BazelContainer or xmanager.xm_local.BazelBinary to run XManager experiments, you need to install Bazel.

  1. Follow the steps to install Bazel.

Create a GCP project (optional)

If you use xm_local.Vertex (Vertex AI) to run XManager experiments, you need to have a GCP project in order to be able to access Vertex AI to run jobs.

  1. Create a GCP project.

  2. Install gcloud.

  3. Associate your Google Account (Gmail account) with your GCP project by running:

    export GCP_PROJECT=<GCP PROJECT ID>
    gcloud auth login
    gcloud auth application-default login
    gcloud config set project $GCP_PROJECT
    
  4. Set up gcloud to work with Docker by running:

    gcloud auth configure-docker
    
  5. Enable Google Cloud Platform APIs.

  6. Create a staging bucket in us-central1 if you do not already have one. This bucket should be used to save experiment artifacts like TensorFlow log files, which can be read by TensorBoard. This bucket may also be used to stage files to build your Docker image if you build your images remotely.

    export GOOGLE_CLOUD_BUCKET_NAME=<GOOGLE_CLOUD_BUCKET_NAME>
    gcloud storage buckets create --location=us-central1 gs://$GOOGLE_CLOUD_BUCKET_NAME
    

    Add GOOGLE_CLOUD_BUCKET_NAME to the environment variables or your .bashrc:

    export GOOGLE_CLOUD_BUCKET_NAME=<GOOGLE_CLOUD_BUCKET_NAME>
    

Writing XManager launch scripts

A snippet for the impatient 🙂
# Contains core primitives and APIs.
from xmanager import xm
# Implementation of those core concepts for what we call 'the local backend',
# which means all executables are sent for execution from this machine,
# independently of whether they are actually executed on our machine or on GCP.
from xmanager import xm_local
#
# Creates an experiment context and saves its metadata to the database, which we
# can reuse later via `xm_local.list_experiments`, for example. Note that
# `experiment` has tracking properties such as `id`.
with xm_local.create_experiment(experiment_title='cifar10') as experiment:
  # Packaging prepares a given *executable spec* for running with a concrete
  # *executor spec*: depending on the combination, that may involve building
  # steps and / or copying the results somewhere. For example, a
  # `xm.python_container` designed to run on `Kubernetes` will be built via
  #`docker build`, and the new image will be uploaded to the container registry.
  # But for our simple case where we have a prebuilt Linux binary designed to
  # run locally only some validations are performed -- for example, that the
  # file exists.
  #
  # `executable` contains all the necessary information needed to launch the
  # packaged blob via `.add`, see below.
  [executable] = experiment.package([
      xm.binary(
          # What we are going to run.
          path='/home/user/project/a.out',
          # Where we are going to run it.
          executor_spec=xm_local.Local.Spec(),
      )
  ])
  #
  # Let's find out which `batch_size` is best -- presumably our jobs write the
  # results somewhere.
  for batch_size in [64, 1024]:
    # `add` creates a new *experiment unit*, which is usually a collection of
    # semantically united jobs, and sends them for execution. To pass an actual
    # collection one may want to use `JobGroup`s (more about it later in the
    # documentation), but for our purposes we are going to pass just one job.
    experiment.add(xm.Job(
        # The `a.out` we packaged earlier.
        executable=executable,
        # We are using the default settings here, but executors have plenty of
        # arguments available to control execution.
        executor=xm_local.Local(),
        # Time to pass the batch size as a command-line argument!
        args={'batch_size': batch_size},
        # We can also pass environment variables.
        env_vars={'HEAPPROFILE': '/tmp/a_out.hprof'},
    ))
  #
  # The context will wait for locally run things (but not for remote things such
  # as jobs sent to GCP, although they can be explicitly awaited via
  # `wait_for_completion`).

The basic structure of an XManager launch script can be summarized by these steps:

  1. Create an experiment and acquire its context.

    from xmanager import xm
    from xmanager import xm_local
    
    with xm_local.create_experiment(experiment_title='cifar10') as experiment:
    
  2. Define specifications of executables you want to run.

    spec = xm.PythonContainer(
        path='/path/to/python/folder',
        entrypoint=xm.ModuleName('cifar10'),
    )
    
  3. Package your executables.

    [executable] = experiment.package([
      xm.Packageable(
        executable_spec=spec,
        executor_spec=xm_local.Vertex.Spec(),
      ),
    ])
    
  4. Define your hyperparameters.

    import itertools
    
    batch_sizes = [64, 1024]
    learning_rates = [0.1, 0.001]
    trials = list(
      dict([('batch_size', bs), ('learning_rate', lr)])
      for (bs, lr) in itertools.product(batch_sizes, learning_rates)
    )
    
  5. Define resource requirements for each job.

    requirements = xm.JobRequirements(T4=1)
    
  6. For each trial, add a job / job groups to launch them.

    for hyperparameters in trials:
      experiment.add(xm.Job(
          executable=executable,
          executor=xm_local.Vertex(requirements=requirements),
          args=hyperparameters,
        ))
    

Now we should be ready to run the launch script.

To learn more about different executables and executors follow 'Components'.

Run XManager

xmanager launch ./xmanager/examples/cifar10_tensorflow/launcher.py

In order to run multi-job experiments, the --xm_wrap_late_bindings flag might be required:

xmanager launch ./xmanager/examples/cifar10_tensorflow/launcher.py -- --xm_wrap_late_bindings

Components

Executable specifications

XManager executable specifications define what should be packaged in the form of binaries, source files, and other input dependencies required for job execution. Executable specifications are reusable and generally platform-independent.

See executable_specs.md for details on each executable specification.

Name Description
xmanager.xm.Container A pre-built .tar image.
xmanager.xm.BazelContainer A Bazel target producing a .tar image.
xmanager.xm.Binary A pre-built binary.
xmanager.xm.BazelBinary A Bazel target producing a self-contained binary.
xmanager.xm.PythonContainer A directory with Python modules to be packaged as a Docker container.

Executors

XManager executors define a platform where the job runs and resource requirements for the job.

Each executor also has a specification which describes how an executable specification should be prepared and packaged.

See executors.md for details on each executor.

Name Description
xmanager.xm_local.Local Runs a binary or a container locally.
xmanager.xm_local.Vertex Runs a container on Vertex AI.
xmanager.xm_local.Kubernetes Runs a container on Kubernetes.

Job / JobGroup

A Job represents a single executable on a particular executor, while a JobGroup unites a group of Jobs providing a gang scheduling concept: Jobs inside them are scheduled / descheduled simultaneously. Same Job and JobGroup instances can be added multiple times.

Job

A Job accepts an executable and an executor along with hyperparameters which can either be command-line arguments or environment variables.

Command-line arguments can be passed in list form, [arg1, arg2, arg3]:

binary arg1 arg2 arg3

They can also be passed in dictionary form, {key1: value1, key2: value2}:

binary --key1=value1 --key2=value2

Environment variables are always passed in Dict[str, str] form:

export KEY=VALUE

Jobs are defined like this:

[executable] = xm.Package(...)

executor = xm_local.Vertex(...)

xm.Job(
    executable=executable,
    executor=executor,
    args={
        'batch_size': 64,
    },
    env_vars={
        'NCCL_DEBUG': 'INFO',
    },
)

JobGroup

A JobGroup accepts jobs in a kwargs form. The keyword can be any valid Python identifier. For example, you can call your jobs 'agent' and 'observer'.

agent_job = xm.Job(...)
observer_job = xm.Job(...)

xm.JobGroup(agent=agent_job, observer=observer_job)

Release files for xmanager 0.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xmanager 0.8.1
File Size Uploaded
xmanager-0.8.1.tar.gz 245.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xmanager 0.8.1
File Interpreter ABI Platform
xmanager-0.8.1-py3-none-any.whl Python 3 none any Details

Total release size: 585.3 kB

Release files / xmanager-0.8.1.tar.gz

Download URL xmanager-0.8.1.tar.gz
Size 245.2 kB
Tags Source
SHA-256 checksum
How to use checksums
386f39983f53935a5d69f221529691ae6b37a63386a225ec1b335aadcdfed963
BLAKE2b-256 checksum
How to use checksums
222f35fa113ffca778390e18118633aca38062ecb2326fb1f925531490eb9b01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.15

Release files / xmanager-0.8.1-py3-none-any.whl

Download URL xmanager-0.8.1-py3-none-any.whl
Size 340.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fb824cf938480460393e2349f1d7701f18f8e66efac940b9732b3d6cd135d124
BLAKE2b-256 checksum
How to use checksums
bc7a9a235f26ad84b079d488435e32e65d1bd978ece9de1f7d3ddcbbafcbf9c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.15

Release history Release notifications | RSS feed

0.8.2

2 release files

This release

0.8.1 This release

2 release files

0.8.0

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page