AIMaster: An elastic and automatic fault tolerance engine
Use the AIMaster-based fault tolerance monitoring feature provided by DLC to detect job exceptions and hangs and to restart failed jobs automatically.
Background
As models and datasets grow in scale, deep learning jobs typically run in distributed mode. As the number of job instances increases, exceptions in the software stack or hardware environment become more likely and can stop the job.
To keep large-scale distributed deep learning jobs running reliably, DLC provides the AIMaster-based fault tolerance monitoring feature. AIMaster is a job-level component. After you enable this feature, an AIMaster instance runs alongside the other instances of the job to monitor the job, make fault-tolerance decisions, and control resources.
Limitations
AIMaster currently supports the following frameworks: PyTorch, MPI, TensorFlow, and ElasticBatch.
Step 1: Configure fault tolerance monitoring parameters
The following sections describe all fault tolerance monitoring parameters. Use the parameter configuration examples to decide which parameters your job needs. When you enable fault tolerance monitoring, enter the parameters in the Other Cofiguration text box as needed.
All parameters
|
Category |
Feature |
Parameter |
Description |
Default |
|
General configuration |
Job execution mode |
--job-execution-mode |
The job execution mode. Valid values:
Fault tolerance behavior varies with the job type. For retryable errors:
|
Sync |
|
Job restart settings |
--enable-job-restart |
Specifies whether the job can restart when fault tolerance conditions are met or a runtime exception is detected. Valid values:
|
False |
|
|
--max-num-of-job-restart |
The maximum number of job restarts. If the number of restarts exceeds this value, the job is marked as failed. |
3 |
||
|
Runtime configuration Note
Applies when no instance fails. |
Job hang detection |
--enable-job-hang-detection |
Specifies whether to enable job hang detection at runtime. Only synchronous jobs are supported. Valid values:
|
False |
|
--job-hang-interval |
The maximum duration for which the job can stop execution. The value must be a positive integer. Unit: seconds. If the job stops for longer than this value, it is marked as abnormal and restarts. |
1800 |
||
|
|
Specifies whether to enable the C4D (Calibrating Collective Communication over Converged ethernet - Diagnosis) detection feature. C4D quickly identifies the slow nodes and faulty nodes that cause job hangs during job execution. Note
This parameter takes effect only when |
False |
||
|
Job exit hang detection |
--enable-job-exit-hang-detection |
Specifies whether to enable job exit hang detection, which detects hangs as the job exits. Only synchronous jobs are supported. Valid values:
|
False |
|
|
--job-exit-hang-interval |
The maximum duration for which the job can stop execution at exit. The value must be a positive integer. Unit: seconds. If the job exit takes longer than this value, it is marked as abnormal and restarts. |
600 |
||
|
Fault tolerance configuration Note
Applies when instances fail. |
Fault tolerance policy |
--fault-tolerant-policy |
The fault tolerance policy. Valid values:
|
ExitCodeAndErrorMsg |
|
Maximum occurrences of the same error |
--max-num-of-same-error |
The maximum number of times the same error can occur on a single instance. If the number of occurrences exceeds this value, the job is marked as failed. |
10 |
|
|
Maximum tolerated failure rate |
--max-tolerated-failure-rate |
The maximum tolerated failure rate. If the proportion of failed instances exceeds this value, the job is marked as failed. The default value, -1, disables this feature. Example: If you set the value to 0.3, the job is marked as failed when more than 30% of worker instances encounter errors. |
-1 |
Parameter configuration examples
The following examples show the fault tolerance monitoring parameters commonly used for different types of training jobs.
-
Synchronous training jobs (common for PyTorch jobs)
If an instance fails and the fault tolerance conditions are met, the job restarts.
--job-execution-mode=Sync --enable-job-restart=True --max-num-of-job-restart=3 --fault-tolerant-policy=ExitCodeAndErrorMsg -
Asynchronous training jobs (common for TensorFlow jobs)
For retryable errors, the failed worker instances are restarted. If a PS or Chief instance fails, the job is not restarted by default. To restart the job in this case, set --enable-job-restart=True.
--job-execution-mode=Async --fault-tolerant-policy=OnFailure -
Offline inference jobs (common for ElasticBatch jobs)
Instances are independent of each other, so the job behaves like an asynchronous job. If an instance fails, only that instance is restarted.
--job-execution-mode=Async --fault-tolerant-policy=OnFailure
Step 2: Enable fault tolerance monitoring
When you submit a DLC training job, you can enable fault tolerance monitoring in the console or with the SDK.
Enable the fault tolerance monitoring feature in the console
When you submit a DLC training job in the console, go to the Fault Tolerance and Diagnosis section, turn on the Automatic Fault Tolerance switch, and configure additional parameters. For more information, see Create a training job. The DLC job then runs an additional AIMaster role that monitors the job and performs fault tolerance when errors occur.

Note the following:
Configure additional parameters in the Other Cofiguration text box. For more information about the parameters, see Step 1: Configure fault tolerance monitoring parameters.
-
After you turn on Hanging Detection, you can enable the C4D Detection feature. C4D is an Alibaba Cloud diagnostic tool for slow jobs and job hangs in large model training. For more information, see Use C4D.
NoteC4D depends on the Alibaba Cloud high-performance collective communication library (ACCL). Make sure that ACCL is installed. For more information, see ACCL: Alibaba Cloud's proprietary high-performance collective communication library.
Currently, the C4D detection feature is available for DLC jobs that use Lingjun AI Computing Service resources.
After you turn on Hanging Detection, you can use the function call stack snapshot analysis tool to locate the exact line of code where the job hangs. You must also configure the hang detection threshold. For more information, see Use the function call stack snapshot analysis tool.
Use the DLC SDK to enable the fault tolerance monitoring feature
-
Use the Go SDK
Turn on the Automatic Fault Tolerance switch when you submit a job by using the Go SDK.
createJobRequest := &client.CreateJobRequest{} settings := &client.JobSettings{ EnableErrorMonitoringInAIMaster: tea.Bool(true), ErrorMonitoringArgs: tea.String("--job-execution-mode=Sync --enable-job-restart=True --enable-job-hang-detection=True --job-hang-interval=3600"), } createJobRequest.SetSettings(settings)The sample code uses the following parameters:
EnableErrorMonitoringInAIMaster: specifies whether to enable the fault tolerance monitoring feature.
ErrorMonitoringArgs: specifies the additional parameters for fault tolerance monitoring.
-
Use the Python SDK
Turn on the Automatic Fault Tolerance switch when you submit a job by using the Python SDK.
from alibabacloud_pai_dlc20201203.models import CreateJobRequest, JobSettings settings = JobSettings( enable_error_monitoring_in_aimaster = True, error_monitoring_args = "--job-execution-mode=Sync --enable-job-restart=True --enable-job-hang-detection=True --job-hang-interval=30 --enable-job-hang-auto-pystack=True" ) create_job_req = CreateJobRequest( ... settings = settings, )The sample code uses the following parameters:
enable_error_monitoring_in_aimaster: specifies whether to enable the fault tolerance monitoring feature.
error_monitoring_args: specifies the additional parameters for fault tolerance monitoring.
enable-job-hang-auto-pystack: specifies whether to capture function call stack information.
Step 3: Configure advanced features
Use the following advanced features based on the fault tolerance monitoring requirements of your job.
Configure fault tolerance notifications
After you enable fault tolerance monitoring for a job, you can receive a notification each time fault tolerance is performed. On the Workspace Details page, choose Configure Workspace > Configure Event Notification, click Create Event Rule, and set the event type to DLC task > Automatic Fault Tolerance. For more information, see Workspace notification.
When an exception such as a NaN loss occurs during training, you can use the AIMaster SDK in your code to send a custom notification message:
This feature requires the AIMaster wheel package. For more information, see FAQ.
from aimaster import job_monitor as jm
job_monitor_client = jm.Monitor(config=jm.PyTorchConfig())
...
if loss == Nan and rank == 0:
st = job_monitor_client.send_custom_message(content="The training loss of the job is NaN.")
if not st.ok():
print('failed to send message, error %s' % st.to_string())
The following are examples of notification messages sent to a DingTalk group:
|
|
Configure custom retryable error keywords
The fault tolerance monitoring feature has a built-in monitoring module for common retryable errors. To also trigger fault tolerance when specific keywords appear in the logs of failed instances, configure the keywords in your code in one of the following ways. After the configuration is complete, the fault tolerance monitoring module scans the tail of the logs from failed instances for matching keywords.
The fault tolerance policy must be set to ExitCodeAndErrorMsg.
-
Example of configuring custom retryable error keywords for a PyTorch job
from aimaster import job_monitor as jm jm_config_params = {} jm_config = jm.PyTorchConfig(**jm_config_params) monitor = jm.Monitor(config=jm_config) monitor.set_retryable_errors(["connect timeout", "error_yyy", "error_zzz"])The arguments that you pass to monitor.set_retryable_errors are the custom retryable error keywords.
-
Example of configuring custom retryable error keywords for a TensorFlow job
from aimaster import job_monitor as jm jm_config_params = {} jm_config = jm.TFConfig(**jm_config_params) monitor = jm.Monitor(config=jm_config) monitor.set_retryable_errors(["connect timeout", "error_yyy", "error_zzz"])
Staged job hang detection
Currently, the job hang detection configuration applies to the entire job, but a job runs in stages. For example, during job initialization, establishing communication between nodes can take a long time, whereas logs update quickly during the training stage. To quickly detect nodes that hang during training, DLC provides staged job hang detection, which lets you set a different hang detection duration for each training stage. Configure the feature as follows:
monitor.reset_config(jm_config_params)
# Example:
# monitor.reset_config(job_hang_interval=10)
# or
# config_params = {"job_hang_interval": 10, }
# monitor.reset_config(**config_params)
The following example shows staged job hang detection for a PyTorch job.
import torch
import torch.distributed as dist
from aimaster import job_monitor as jm
jm_config_params = {
"job_hang_interval": 1800 # global detection every 30 min.
}
jm_config = jm.PyTorchConfig(**jm_config_params)
monitor = jm.Monitor(config=jm_config)
dist.init_process_group('nccl')
...
# impl these two funcs in aimaster sdk
# user just need to add annotations to their func
def reset_hang_detect(hang_seconds):
jm_config_params = {
"job_hang_interval": hang_seconds
}
monitor.reset_config(**jm_config_params)
def hang_detect(interval):
reset_hang_detect(interval)
...
@hang_detect(180) # reset hang detect to 3 min, only for func scope
def train():
...
@hang_detect(-1) # disable hang detect temporarily, only for func scope
def test():
...
for epoch in range(0, 100):
train(epoch)
test(epoch)
self.scheduler.step()
Use C4D
C4D is a tool developed by Alibaba Cloud to diagnose slow jobs and job hangs in large model training. C4D depends on ACCL. Make sure that ACCL is installed and its environment variables are correctly configured. For more information, see ACCL: Alibaba Cloud's proprietary high-performance collective communication library. Currently, the C4D detection feature is available for DLC jobs that use Lingjun AI Computing Service resources.
Feature overview
C4D aggregates status information from all nodes in a job and determines whether any node has an issue at the communication layer or the non-communication layer. The following figure shows the C4D architecture:
All parameters
After you enable the C4D detection feature, configure the following parameters in the Other Configurations text box:
|
Parameter |
Description |
Example value |
|
--c4d-log-level |
The log level of C4D output. Valid values:
The default value, Warning, outputs logs at the Warning and Error levels. Use the default value for normal operation. To troubleshoot performance issues, set the value to Info. |
|
|
--c4d-common-envs |
The environment variables for C4D execution. Format:
|
|
Currently, for Error-level logs, AIMaster automatically isolates the reported node and restarts the job. Logs at each level are handled as follows:
|
Error level |
Description |
Actions |
|
Error |
By default, a hang at the communication layer that lasts longer than 3 minutes causes the job to fail. You can configure C4D_HANG_TIMEOUT and C4D_HANG_TIMES to change the default values. |
AIMaster automatically isolates the node reported in the log. |
|
Warn |
By default, a hang at the communication layer that lasts longer than 10 seconds affects performance but does not cause the job to fail. You can configure C4D_HANG_TIMEOUT to change the default value. |
For now, the node reported in the log is not isolated automatically. Manual confirmation is required. |
|
A hang at the non-communication layer that lasts longer than 10 seconds may cause the job to fail. |
For now, the node reported in the log is not isolated automatically. Manual confirmation is required. |
|
|
Info |
A node is slow at the communication layer or the non-communication layer. |
These diagnostic logs mainly indicate performance issues. Manual confirmation is required. |
If a DLC job is slow or hangs during execution, click the job name in the DLC job list to go to the job overview page. In the Instances section, view the AIMaster node logs to check the C4D diagnostic results. For more information, see Diagnostic result examples.
Diagnostic result examples
RankCommHang: indicates that a node hangs at the communication layer.

RankNonCommHang: indicates that a node hangs at the non-communication layer. For example, the hang occurs during computation.

RankCommSlow: indicates that a node is slow at the communication layer.

RankNonCommSlow: indicates that a node is slow at the non-communication layer.

Use the function call stack snapshot analysis tool
Job hangs are a common failure in large model training jobs. The most frequent type is the NVIDIA Collective Communications Library (NCCL) hang. A job with an NCCL hang fails and prints a log such as "Watchdog caught collective operation timeout". To help you quickly locate the root cause of such hangs, Alibaba Cloud developed the function call stack snapshot analysis tool for model training jobs. Use the tool as follows:
Step 1: Install pystack or py-spy
Check whether pystack or py-spy is installed in your container image. If neither is installed, install the tool before you run the job. The following command installs pystack:
pip install pystack -i https://mirrors.cloud.aliyuncs.com/pypi/simple/ --trusted-host mirrors.cloud.aliyuncs.com
Step 2: Turn on Hanging Detection in Automatic Fault Tolerance
For more information about how to turn on Hanging Detection, see Enable the fault tolerance monitoring feature in the console. After you turn it on, set the hang detection threshold so that the function call stack snapshot analysis tool works properly. First, check the timeout period of your model training job. The timeout period is usually recorded in the error log that is generated after a job hang. The following error log is an example:
Watchdog caught collective operation timeout: WorkNCCL(SeqNum=2143, OpType=ALLREDUCE, NumelIn=659, NumelOut=659, Timeout(ms)=600000) ran for 600535 milliseconds before timing out
The Timeout field in this error log indicates that the timeout period of the model training job is 600 seconds, or 10 minutes. In this case, set the hang detection threshold to 450 seconds. If the Timeout field in the error log of the job is 1800 seconds, set the hang detection threshold to 1500 seconds. Set the hang detection threshold to a value approximately 150–200 seconds less than the Timeout value.
After you configure the hang detection feature as described in the preceding steps, AIMaster automatically collects and analyzes the function call stack of the training process when a job hang occurs. You can view the analysis results in the logs of the AIMaster node. The following example shows the function call stack analysis result that the tool outputs after a job hang:

The analysis result contains three fields. The stack field shows the function call stack. The threads field lists the threads in which the stack occurs. The count field gives the number of threads that produced the same stack. Stacks with a count of 1 are the most likely cause of the job hang, so check them first.
Step 4: View restart reasons
-
View restart rounds: Job restarts are grouped into rounds. On the job details page, click a round to expand its details and view information such as the time the job spent in each stage. This helps you understand the job status more accurately.

-
View restart records: Click Restarts or the Restart records tab to view restart information, including the restart reason, restart result, and restart duration.

To view more information about a restart:
In the Restart records list, click Description to view the details of a restart, including Restarts, Restart Time, Node Name, Instance Name, Error Code, Error Message, and Error Source.
-
Click View Aggregation Fault Details to expand the full list of restart records.

FAQ
Q: How do I install the AIMaster SDK?
Run one of the following commands to install the wheel package for your Python version.
# Python 3.6
pip install -U http://odps-release.cn-hangzhou.oss.aliyun-inc.com/aimaster/pai_aimaster-1.2.1-cp36-cp36m-linux_x86_64.whl
# Python 3.8
pip install -U http://odps-release.cn-hangzhou.oss.aliyun-inc.com/aimaster/pai_aimaster-1.2.1-cp38-cp38-linux_x86_64.whl
# Python 3.10
pip install -U http://odps-release.cn-hangzhou.oss.aliyun-inc.com/aimaster/pai_aimaster-1.2.1-cp310-cp310-linux_x86_64.whl








