MXNet Training

Instructions for using MXNet

Alpha

This Kubeflow component has alpha status with limited support. See the Kubeflow versioning policies. The Kubeflow team is interested in your feedback about the usability of the feature.

This guide walks you through using Apache MXNet (incubating) with Kubeflow.

MXNet Operator provides a Kubernetes custom resource MXJob that makes it easy to run distributed or non-distributed Apache MXNet jobs (training and tuning) and other extended framework like BytePS jobs on Kubernetes. Using a Custom Resource Definition (CRD) gives users the ability to create and manage Apache MXNet jobs just like built-in K8S resources.

Installing the MXJob CRD and operator on your k8s cluster

Deploy MXJob CRD and Apache MXNet Operator

  1. kustomize build manifests/overlays/v1 | kubectl apply -f -

Verify that MXJob CRD and Apache MXNet Operator are installed

Check that the Apache MXNet custom resource is installed via:

  1. kubectl get crd

The output should include mxjobs.kubeflow.org like the following:

  1. NAME AGE
  2. ...
  3. mxjobs.kubeflow.org 4d
  4. ...

Check that the Apache MXNet operator is running via:

  1. kubectl get pods

The output should include mxnet-operaror-xxx like the following:

  1. NAME READY STATUS RESTARTS AGE
  2. mxnet-operator-d466b46bc-xbqvs 1/1 Running 0 4m37s

Creating a Apache MXNet training job

You create a training job by defining a MXJob with MXTrain mode and then creating it with.

  1. kubectl create -f examples/train/mx_job_dist_gpu_v1.yaml

Each replicaSpec defines a set of Apache MXNet processes. The mxReplicaType defines the semantics for the set of processes. The semantics are as follows:

scheduler

  • A job must have 1 and only 1 scheduler
  • The pod must contain a container named mxnet
  • The overall status of the MXJob is determined by the exit code of the mxnet container
    • 0 = success
    • 1 || 2 || 126 || 127 || 128 || 139 = permanent errors:
      • 1: general errors
      • 2: misuse of shell builtins
      • 126: command invoked cannot execute
      • 127: command not found
      • 128: invalid argument to exit
      • 139: container terminated by SIGSEGV(Invalid memory reference)
    • 130 || 137 || 143 = retryable error for unexpected system signals:
      • 130: container terminated by Control-C
      • 137: container received a SIGKILL
      • 143: container received a SIGTERM
    • 138 = reserved in tf-operator for user specified retryable errors
    • others = undefined and no guarantee

worker

  • A job can have 0 to N workers
  • The pod must contain a container named mxnet
  • Workers are automatically restarted if they exit

server

  • A job can have 0 to N servers
  • parameter servers are automatically restarted if they exit

For each replica you define a template which is a K8S PodTemplateSpec. The template allows you to specify the containers, volumes, etc… that should be created for each replica.

Creating a TVM tuning job (AutoTVM)

TVM is a end to end deep learning compiler stack, you can easily run AutoTVM with mxnet-operator. You can create a auto tuning job by define a type of MXTune job and then creating it with

  1. kubectl create -f examples/tune/mx_job_tune_gpu_v1.yaml

Before you use the auto-tuning example, there is some preparatory work need to be finished in advance. To let TVM tune your network, you should create a docker image which has TVM module. Then, you need a auto-tuning script to specify which network will be tuned and set the auto-tuning parameters. For more details, please see tutorials. Finally, you need a startup script to start the auto-tuning program. In fact, mxnet-operator will set all the parameters as environment variables and the startup script need to reed these variable and then transmit them to auto-tuning script. We provide an example under examples/tune/, tuning result will be saved in a log file like resnet-18.log in the example we gave. You can refer it for details.

Using GPUs

MXNet Operator supports training with GPUs.

Please verify your image is available for distributed training with GPUs.

For example, if you have the following, MXNet Operator will arrange the pod to nodes to satisfy the GPU limit.

  1. command: ["python"]
  2. args: ["/incubator-mxnet/example/image-classification/train_mnist.py","--num-epochs","1","--num-layers","2","--kv-store","dist_device_sync","--gpus","0"]
  3. resources:
  4. limits:
  5. nvidia.com/gpu: 1

Monitoring your Apache MXNet job

To get the status of your job

  1. kubectl get -o yaml mxjobs $JOB

Here is sample output for an example job

  1. apiVersion: kubeflow.org/v1
  2. kind: MXJob
  3. metadata:
  4. creationTimestamp: 2021-03-24T15:37:27Z
  5. generation: 1
  6. name: mxnet-job
  7. namespace: default
  8. resourceVersion: "5123435"
  9. selfLink: /apis/kubeflow.org/v1/namespaces/default/mxjobs/mxnet-job
  10. uid: xx11013b-4a28-11e9-s5a1-704d7bb912f91
  11. spec:
  12. cleanPodPolicy: All
  13. jobMode: MXTrain
  14. mxReplicaSpecs:
  15. Scheduler:
  16. replicas: 1
  17. restartPolicy: Never
  18. template:
  19. metadata:
  20. creationTimestamp: null
  21. spec:
  22. containers:
  23. - image: mxjob/mxnet:gpu
  24. name: mxnet
  25. ports:
  26. - containerPort: 9091
  27. name: mxjob-port
  28. resources: {}
  29. Server:
  30. replicas: 1
  31. restartPolicy: Never
  32. template:
  33. metadata:
  34. creationTimestamp: null
  35. spec:
  36. containers:
  37. - image: mxjob/mxnet:gpu
  38. name: mxnet
  39. ports:
  40. - containerPort: 9091
  41. name: mxjob-port
  42. resources: {}
  43. Worker:
  44. replicas: 1
  45. restartPolicy: Never
  46. template:
  47. metadata:
  48. creationTimestamp: null
  49. spec:
  50. containers:
  51. - args:
  52. - /incubator-mxnet/example/image-classification/train_mnist.py
  53. - --num-epochs
  54. - "10"
  55. - --num-layers
  56. - "2"
  57. - --kv-store
  58. - dist_device_sync
  59. - --gpus
  60. - "0"
  61. command:
  62. - python
  63. image: mxjob/mxnet:gpu
  64. name: mxnet
  65. ports:
  66. - containerPort: 9091
  67. name: mxjob-port
  68. resources:
  69. limits:
  70. nvidia.com/gpu: "1"
  71. status:
  72. completionTime: 2021-03-24T09:25:11Z
  73. conditions:
  74. - lastTransitionTime: 2021-03-24T15:37:27Z
  75. lastUpdateTime: 2021-03-24T15:37:27Z
  76. message: MXJob mxnet-job is created.
  77. reason: MXJobCreated
  78. status: "True"
  79. type: Created
  80. - lastTransitionTime: 2021-03-24T15:37:27Z
  81. lastUpdateTime: 2021-03-24T15:37:29Z
  82. message: MXJob mxnet-job is running.
  83. reason: MXJobRunning
  84. status: "False"
  85. type: Running
  86. - lastTransitionTime: 2021-03-24T15:37:27Z
  87. lastUpdateTime: 2021-03-24T09:25:11Z
  88. message: MXJob mxnet-job is successfully completed.
  89. reason: MXJobSucceeded
  90. status: "True"
  91. type: Succeeded
  92. mxReplicaStatuses:
  93. Scheduler: {}
  94. Server: {}
  95. Worker: {}
  96. startTime: 2021-03-24T15:37:29Z

The first thing to note is the RuntimeId. This is a random unique string which is used to give names to all the K8s resouces (e.g Job controllers & services) that are created by the MXJob.

As with other K8S resources status provides information about the state of the resource.

phase - Indicates the phase of a job and will be one of

  • Creating
  • Running
  • CleanUp
  • Failed
  • Done

state - Provides the overall status of the job and will be one of

  • Running
  • Succeeded
  • Failed

For each replica type in the job, there will be a ReplicaStatus that provides the number of replicas of that type in each state.

For each replica type, the job creates a set of K8s Job Controllers named

  1. ${REPLICA-TYPE}-${RUNTIME_ID}-${INDEX}

For example, if you have 2 servers and the runtime id is “76n0”, then MXJob will create the following two jobs:

  1. server-76no-0
  2. server-76no-1

Contributing

Please refer to the this document for contributing guidelines.

Community

Please check out Kubeflow community page for more information on how to get involved in our community.

Last modified 06.04.2021: docs: Update MPI and MXNet operator pages (#2586) (420450dc)