Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).
Motivation
With widespread advances in machine learning (or deep learning), more and more enterprises are beginning to incorporate ML models across a number of products. Supporting the ML scenarios is one of Flink’s roadmap targets. GPU is widely used as the accelerator by people from the ML community. It is necessary to add GPU support.
Currently, Flink only supports to request GPU resource in Mesos integration while most users and enterprises deploying Flink on Yarn/Kubernetes or Standalone mode. Thus, we propose to add GPU support in Flink. As a first step, we propose to:
- Enable user to configure the GPU cores per task executor and forward such requirements to the external resource managers (for Kubernetes/Yarn/Mesos setups).
- Provide information of available GPU resources to operators.
Public Interfaces
Introduce the external resource framework for external resource allocation and management. The pattern of configuration options is:
- external-resources. Define the {resourceName} list of enabled external resources, split by delimiter ",".
- external-resource.{resourceName}.amount. Define the amount of external resources in a task executor.
- external-resource.{resourceName}.driver-factory.class. Define the class name of ExternalResourceDriverFactory.
- external-resource.{resourceName}.kubernetes.key. Optional config which defines the configuration key of that external resource in Kubernetes. If you want the Flink to request the external resource from Kubernetes(through its Device Plugin mechanism[3]), you need to explicitly set this key. Only valid for Kubernetes mode.
- external-resource.{resourceName}.yarn.key. Optional config which defines the configuration key of that external resource in Yarn. If you want the Flink to request the external resource from Yarn, you need to explicitly set this key. Only valid for Yarn mode.
- external-resource.{resourceName}.param.{params}. Each ExternalResourceDriver could define their specific configs following this pattern.
On the TaskExecutor side, introduce the ExternalResourceDriverFactory and ExternalResourceDriver interface, take the responsibility to manage and provide information of the external resource. User could implement their third party ExternalResourceDriver for other external resources they want to leverage.
public interface ExternalResourceDriverFactory { /** * Construct the ExternalResourceDriver from configuration. */ ExternalResourceDriver createExternalResourceDriver(Congiuration config); } public interface ExternalResourceDriver { /** * Retrieve the information of the external resources according to the amount. */ Set<? extends ExternalResourceInfo> retrieveResourceInfo(long amount); }
Introduce the ExternalResourceInfo class, which contains the information of the external resources.
public interface ExternalResourceInfo { }
Operators and functions could get that information from the RuntimeContext.
public interface RuntimeContext { /** * Get the specific external resource information by the resourceName. */ Set<ExternalResourceInfo> getExternalResourceInfos(String resourceName); }
For GPU resource, we introduce the following configuration options:
- “external-resource.gpu.amount”: Define how many GPUs in a task executor. The default value should be 0.
- “external-resource.gpu.param.discovery-script.path”: Define the path of the discovery script. See Discovery Script Section.
- “external-resource.gpu.param.discovery-script.args”: Define the arguments passed to the discovery script. If using default gpu discovery script, the allowed param is [--privilege] [--check-dead] and [--assign-file filePath]. See Discovery Script Section.
- external-resource.{resourceName}.kubernetes.key. Define the configuration key of GPU in Kubernetes. The default value is “nvidia.com/gpu”. If using amd GPU, user could set it to "amd.com/gpu"
- external-resource.{resourceName}.yarn.key. Define the configuration key of GPU in Yarn. The default value is "yarn.io/gpu".
We provide a GPUDriver which discovers the GPU resource through user-defined discovery script and provide the available GPU index.
For GPU resource, we introduce the GPUInforamtion class, which only contains the index of a GPU card.
Proposed Changes
Overview
- We introduce the external resource framework for external resource allocation and management.
- User sets the “external-resource.gpu.amount”, “external-resource.gpu.driver-factory.class” and specifies the “external-resource.gpu.param.discovery-script.[path|args]” if needed.
- For Yarn/Kubernetes mode, Flink maps the “external-resource.gpu.amount” to the corresponding field of resource requests to the external resource manager.
- Introduce a GPUDriver, which will execute the discovery script and get the available GPU resources from the output.
- Operators and functions get the GPU resource information from GPUDriver
Introduce external resource framework for external resource allocation and management
To provide extensibility and decouple the TaskExecutor/ResourceManager from the external resource management/allocation(following the separation of concern rule), we introduce the external resource framework for external resource allocation and management. This framework could be extended by third-party for other external resources they want to leverage.
The external resource framework drives the end-to-end workflow of external resource allocation and management. All enabled external resources should be added to "external-resource.list".
On the ResourceManager side, user defines the amount of the external resource. The framework takes the responsibility to allocate resources from external resource managers(Yarn/Kubernetes). User needs to specify the configuration key of that external resource on Yarn/Kubernetes. Then, Yarn/KubernetesResourceManager forward this external resource request to the external resource managers.
- For Yarn, the YarnResourceManager adds the external resource to the ContainerRequest.
- For Kubernetes, the KubernetesResourceManager adds the external resource to the pod for TaskExecutor(leverage the Device Plugin mechanism[3]).
On the TaskExecutor side, we introduce ExternalResourceDriver, which takes the responsibility to detect and provide information of external resources. TaskExecutor does not need to manage a specific external resource by itself, Operators and functions would get the ExternalResourceInfo from RuntimeConext.
Regarding the configuration, the common config keys are the amount of the external resources and the class name of ExternalResourceDriver. Besides, each driver could define their own configs following the specific pattern. In summary:
- external-resources. Define the {resourceName} list of enabled external resources with delimiter ",". If configured, ResourceManager and TaskExecutor would check if the relevant configs exist for resources in this list. ResourceManager will forward the request to the underlying external resource manager. TaskExecutor will launch the corresponding ExternalResourceDriver.
- external-resource.{resourceName}.amount. Define the amount of external resources in a task executor.
- external-resource.{resourceName}.driver-factory.class. Define the class name of ExternalResourceDriverFactory.
- external-resource.{resourceName}.kubernetes.key. Optional config which defines the configuration key of that external resource in Kubernetes. If you want the Flink to request the external resource from Kubernetes, you need to explicitly set this key. Only valid for Kubernetes mode.
- external-resource.{resourceName}.yarn.key. Optional config which defines the configuration key of that external resource in Yarn. If you want the Flink to request the external resource from Yarn, you need to explicitly set this key. Only valid for Yarn mode.
- external-resource.{resourceName}.param.{params}. Each ExternalResourceDriver could define their specific configs following this pattern.
The definition of ExternalResourceDriver and ExternalResourceInfo is:
public interface ExternalResourceDriverFactory { /** * Construct the ExternalResourceDriver from configuration. */ ExternalResourceDriver createExternalResourceDriver(Congiuration config); } public interface ExternalResourceDriver { /** * Retrieve the information of the external resources according to the amount. */ Set<? extends ExternalResourceInfo> retrieveResourceInfo(long amount); } public interface ExternalResourceInfo { String getProperty(String key); Collection<String> getKeys(); }
Guarantee the required GPU resources are accessible to task executors
On the ResourceManager side, Flink requires the environment in which task executors run has required GPU resources and the GPU resources are accessible to task executors.
Regarding the amount of GPU resources:
- We introduce the configuration option “external-resource.gpu.amount”, which defines how many GPU cores a task executor should have. Notice that this value will be passed to the discovery script as the first argument, See Discovery Script Section.
- For standalone mode, it should be guaranteed by user that there are required GPU resources in the environment where task executors run.
- For Yarn/Kubernetes mode, they will guarantee there are required amount of GPU resources in the container if we set the corresponding field in the request.
- For Yarn, Flink will set the corresponding field(external-resource.gpu.yarn.key) of container requests.
- For Kubernetes, Flink will set the corresponding field(external-resource.gpu.kubernetes.key) of pod requests.
Regarding the accessibility of the GPU resources:
- For standalone mode, it should be guaranteed by user, that the GPU indexes returned from discovery script (if any) exist and are accessible.
- For Yarn/Kubernetes mode, the Yarn/Kubernetes will guarantee the requested GPU resources are accessible to the container.
Note: To make GPU resources accessible, certain setups/preparation are needed depending on your environment. See External Requirements Section.
Introduce GPUDriver
Once the required GPU resources are accessible to task executors, GPUDriver needs to discover GPU resources and provide the GPU resource information to operators.
GPUDriver will execute the discovery script and get the available GPU resources from the output. Then, as one of the ExternalResourceDriver, operators and functions could get the GPU information from RuntimeContext.
The GPUInformation contains the specific index of the GPU.
Discovery Script
We introduce the configuration option “external-resource.gpu.param.discovery-script” and “external-resource.gpu.param.discovery-script.args”, which define the path of discovery script and its arguments. GPUDriver will execute the allocate function and get the available GPU resources from the output when it is opened and execute the deallocateAll function when it is closed.
When executed, the discovery script should:
- Return a list of the available GPU indexes, split by a comma.
- Exit with non-zero if the output does not meet the expectation. GPUDriver will throw exception in that case and then cause TaskExecutor initialization to fail.
- Flink passes the amount (external-resource.gpu.amount) as the first argument into the script. The user-defined arguments would be appended after it.
We provide a default script:
- The script would first get all the indexes of visible GPU resources, by using the “nvidia-smi”/“rocm-smi” command.
- It will return a list of indexes of discovered GPUs, split by a comma.
- The number of GPU indexes returned from the default script should always match the amount configured through “external-resource.gpu.amount”
- If there are more GPUs discovered than configured, the script returns only as many indexes as the configured amount.
- If there are not enough GPU discovered, the script will fail and exit with non-zero.
For achieving worker-level isolation in standalone mode, we provide a privilege option for the default script. User needs to add "--privilege" to the “external-resource.gpu.param.discovery-script.args” to turn it on. For more discussion of worker-level isolation, See Worker-level Isolation section.
User can also provide their own discovery scripts, addressing their custom requirements, e.g., dynamically deciding the amount of returned GPU indexes, etc.
GPU resource isolation
Unlike CPU, GPU has two dimensions resource: cores and video memory. While the cores could be shared by multiple jobs, video memory should be exclusive. Sharing the same GPU across multiple operators may result in OOMs and job failures if the total video memory limit is exceeded. Thus, we need isolation to make sure video memory usage does not exceed the physical limitation.
Worker-level Isolation
For Yarn/Kubernetes mode, the underlying system will guarantee that the visible GPU resources on the container should be strictly matching the requirement. Thus, worker-level isolation could be achieved without extra work.
For standalone mode, multiple task executors may be co-located on the same device, and each GPU is visible to all the task executors. To achieve worker-level isolation in such scenarios, we need decide which task executor uses which GPU in a cooperative way. We provide a privilege mode for this in the default script.
Privilege Mode of Default Discovery Script
In the privilege mode, we use a common assignment file to synchronize the GPU assignment across different task executors. After retrieving indexes of all visible GPUs, the script should open the assignment file, check which GPUs are already in use, and write down which GPUs it decides to use (choosing from the non-used ones) with the PID of task executor. We leverage the “flock” mechanism to guarantee accesses to the assignment file are exclusive. Since only one process can access the assignment file at the same time, we ensure no GPU will be used by multiple workers.
- The assignment file is “/var/tmp/flink-gpu-assignment” by default, user could set it through adding "--assign-file filePath" to the “external-resource.gpu.param.discovery-script.args”. User needs to ensure that it is same file for all the task executors in the same cluster and do not delete it before the cluster stopped. If the cluster is not stopped properly, this file needs to be deleted manually.
When task executor stopped, the record may not be clean. In this scenario, new task executors start after this will read the dirty data. That may cause task executor mistakenly reports there is no enough GPU resource. To address this issue, the script provides a “--check-dead” option. If it is added to in “external-resource.gpu.param.discovery-script.args”, in case of no enough non-recorded GPU, the allocate function will check whether the associated processes of exist records are still alive, and take over those GPUs whose associated processes are already dead.
For example, if user want to trigger privilege mode, they could set “external-resource.gpu.param.discovery-script.args” to "--privilege --check-dead --assign-file /tmp/flink-assign". This will execute the default discovery script in privilege mode, check if there is dead process occupy the GPU resources and locate the resource assignment file in "/tmp/flink-assign".
Note: This approach could not ensure the GPUs are isolated from other applications (another Flink cluster or non-flink applications).
Operator-level Isolation
We do not guarantee operator-level isolation in the first step. All the operators on the same task executor can see all the visible GPU indexes.
The approach for operator-level isolation depends on fine-grained resource management, which is not ready at the moment. We may revisit this once the fine-grained resource management approach is completed. An alternative solution to this problem is to have operators work cooperatively to share all GPU resources, but this is out of the scope of this proposal.
External Requirements
We list the external requirements for using GPU resources:
- For standalone mode, admins should install NVIDIA drivers on all the nodes in the cluster.
- For Yarn mode, admins should configure the cluster to enable GPU scheduling[1]. Notice the version requirement is 2.10+ and 3.1+. Notice that Yarn only supports the NVIDIA GPU at the moment.
- For Kubernetes mode, admins should install the NVIDIA GPU device plugin[2][3]. Notice the version requirement is 1.10+. At the moment, Kubernetes only support NVIDIA GPU and AMD GPU.
Implementation Steps
- Introduce the ExternalResourceDriver class and related config options.
- Let ResourceManager allocate external resources in container/Pod of Yarn/Kubernetes accordingly.
- Construct ExternalResourceDriver to the TaskExecutor and provide the external resource information through the RuntimeContext.
- Introduce GPUDriver.
- Add privilege option to the default discovery script.
Known Limitations
- Currently, Yarn only supports the NVIDIA GPU.
- There is no operator-level isolation for GPU resources at the moment. Meanwhile, there is no GPU cooperating mechanism among operators.
- The GPU resource of a task executor will be shared by all operators scheduled on it. If multiple operators scheduled to the same GPU, the video memory limitation may be exceeded and the job will fail with OOM.
- The usage distribution of GPU resources could be skew, e.g. all the operators may run their jobs in the same GPU while other GPUs are idle. The idle GPU resources are wasted in this scenario.
- The operator-level isolation and fine-grained GPU resource scheduling might be achieved in the future, when FLIP-56[4] is ready. Currently, we could rely on operators cooperatively share all GPU resources by themselves.
Test Plan
- This FLIP proposes can check by its test case.
- The GPU discovery logic could only be tested manually on the device with NVIDIA GPU.
Future work
- Add external resource information to RestAPI / WebAPI.
Reference
[1] Hadoop 3.1.0 – Using GPU On YARN
[2] NVIDIA/k8s-device-plugin: NVIDIA device plugin for Kubernetes
[3] Device Plugins
[4] FLIP-56: Dynamic Slot Allocation - Apache Flink
[5] cgroups(7) - Linux manual page