|
| 1 | +# Airflow DAG Definition: AI Training Run |
| 2 | +# |
| 3 | +# Steps: |
| 4 | +# 1. Data prep job |
| 5 | +# 2. Dataset snapshot (for traceability) |
| 6 | +# 3. Training job |
| 7 | +# 4. Model snapshot (for versioning/baselining) |
| 8 | +# 5. Inference validation job |
| 9 | + |
| 10 | + |
| 11 | +from airflow import DAG |
| 12 | +from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import KubernetesPodOperator |
| 13 | +from airflow.operators.python_operator import PythonOperator |
| 14 | +from airflow.utils.dates import days_ago |
| 15 | +from kubernetes.client import models as k8s |
| 16 | +import uuid |
| 17 | + |
| 18 | + |
| 19 | +##### DEFINE PARAMETERS: Modify parameter values in this section to match your environment ##### |
| 20 | + |
| 21 | +## Define default args for DAG |
| 22 | +ai_training_run_dag_default_args = { |
| 23 | + 'owner': 'NetApp' |
| 24 | +} |
| 25 | + |
| 26 | +## Define DAG details |
| 27 | +ai_training_run_dag = DAG( |
| 28 | + dag_id='ai_training_run', |
| 29 | + default_args=ai_training_run_dag_default_args, |
| 30 | + schedule_interval=None, |
| 31 | + start_date=days_ago(2), |
| 32 | + tags=['training'] |
| 33 | +) |
| 34 | + |
| 35 | +# Define Kubernetes namespace to execute DAG in |
| 36 | +namespace = 'airflow' |
| 37 | + |
| 38 | +## Define volume details (change values as necessary to match your environment) |
| 39 | + |
| 40 | +# Dataset volume |
| 41 | +dataset_volume_pvc_existing = 'dataset-vol' |
| 42 | +dataset_volume = k8s.V1Volume( |
| 43 | + name=dataset_volume_pvc_existing, |
| 44 | + persistent_volume_claim=k8s.V1PersistentVolumeClaimVolumeSource(claim_name=dataset_volume_pvc_existing), |
| 45 | +) |
| 46 | +dataset_volume_mount = k8s.V1VolumeMount( |
| 47 | + name=dataset_volume_pvc_existing, |
| 48 | + mount_path='/mnt/dataset', |
| 49 | + sub_path=None, |
| 50 | + read_only=False |
| 51 | +) |
| 52 | + |
| 53 | +# Model volume |
| 54 | +model_volume_pvc_existing = 'airflow-model-vol' |
| 55 | +model_volume = k8s.V1Volume( |
| 56 | + name=model_volume_pvc_existing, |
| 57 | + persistent_volume_claim=k8s.V1PersistentVolumeClaimVolumeSource(claim_name=model_volume_pvc_existing), |
| 58 | +) |
| 59 | +model_volume_mount = k8s.V1VolumeMount( |
| 60 | + name=model_volume_pvc_existing, |
| 61 | + mount_path='/mnt/model', |
| 62 | + sub_path=None, |
| 63 | + read_only=False |
| 64 | +) |
| 65 | + |
| 66 | +## Define job details (change values as needed) |
| 67 | + |
| 68 | +# Data prep step |
| 69 | +data_prep_step_container_image = "nvcr.io/nvidia/tensorflow:21.03-tf1-py3" |
| 70 | +data_prep_step_command = ["echo", "'No data prep command entered'"] # Replace this echo command with the data prep command that you wish to execute |
| 71 | +data_prep_step_resources = {} # Hint: To request that 1 GPU be allocated to job pod, change to: {'limit_gpu': 1} |
| 72 | + |
| 73 | +# Training step |
| 74 | +train_step_container_image = "nvcr.io/nvidia/tensorflow:21.03-tf1-py3" |
| 75 | +train_step_command = ["echo", "'No training command entered'"] # Replace this echo command with the training command that you wish to execute |
| 76 | +train_step_resources = {} # Hint: To request that 1 GPU be allocated to job pod, change to: {'limit_gpu': 1} |
| 77 | + |
| 78 | +# Inference validation step |
| 79 | +validate_step_container_image = "nvcr.io/nvidia/tensorflow:21.03-tf1-py3" |
| 80 | +validate_step_command = ["echo", "'No inference validation command entered'"] # Replace this echo command with the inference validation command that you wish to execute |
| 81 | +validate_step_resources = {} # Hint: To request that 1 GPU be allocated to job pod, change to: {'limit_gpu': 1} |
| 82 | + |
| 83 | +################################################################################################ |
| 84 | + |
| 85 | + |
| 86 | +# Define DAG steps/workflow |
| 87 | +with ai_training_run_dag as dag : |
| 88 | + |
| 89 | + # Define step to generate uuid for run |
| 90 | + generate_uuid = PythonOperator( |
| 91 | + task_id='generate-uuid', |
| 92 | + python_callable=lambda: str(uuid.uuid4()) |
| 93 | + ) |
| 94 | + |
| 95 | + # Define data prep step using Kubernetes Pod operator (https://airflow.apache.org/docs/stable/kubernetes.html#kubernetespodoperator) |
| 96 | + data_prep = KubernetesPodOperator( |
| 97 | + namespace=namespace, |
| 98 | + image=data_prep_step_container_image, |
| 99 | + cmds=data_prep_step_command, |
| 100 | + resources = data_prep_step_resources, |
| 101 | + volumes=[dataset_volume, model_volume], |
| 102 | + volume_mounts=[dataset_volume_mount, model_volume_mount], |
| 103 | + name="ai-training-run-data-prep", |
| 104 | + task_id="data-prep", |
| 105 | + is_delete_operator_pod=True, |
| 106 | + hostnetwork=False |
| 107 | + ) |
| 108 | + |
| 109 | + # Define step to take a snapshot of the dataset volume for traceability |
| 110 | + dataset_snapshot = KubernetesPodOperator( |
| 111 | + namespace=namespace, |
| 112 | + image="python:3", |
| 113 | + cmds=["/bin/bash", "-c"], |
| 114 | + arguments=["\ |
| 115 | + python3 -m pip install ipython kubernetes pandas tabulate && \ |
| 116 | + git clone https://github.com/NetApp/netapp-data-science-toolkit && \ |
| 117 | + mv /netapp-data-science-toolkit/Kubernetes/ntap_dsutil_k8s.py / && \ |
| 118 | + /ntap_dsutil_k8s.py create volume-snapshot --pvc-name=" + str(dataset_volume_pvc_existing) + " --snapshot-name=dataset-{{ task_instance.xcom_pull(task_ids='generate-uuid', dag_id='ai_training_run', key='return_value') }} --namespace=" + namespace], |
| 119 | + name="ai-training-run-dataset-snapshot", |
| 120 | + task_id="dataset-snapshot", |
| 121 | + is_delete_operator_pod=True, |
| 122 | + hostnetwork=False |
| 123 | + ) |
| 124 | + |
| 125 | + # State that the dataset snapshot should be created after the data prep job completes and the uuid job completes |
| 126 | + data_prep >> dataset_snapshot |
| 127 | + generate_uuid >> dataset_snapshot |
| 128 | + |
| 129 | + # Define training step using Kubernetes Pod operator (https://airflow.apache.org/docs/stable/kubernetes.html#kubernetespodoperator) |
| 130 | + train = KubernetesPodOperator( |
| 131 | + namespace=namespace, |
| 132 | + image=train_step_container_image, |
| 133 | + cmds=train_step_command, |
| 134 | + resources = train_step_resources, |
| 135 | + volumes=[dataset_volume, model_volume], |
| 136 | + volume_mounts=[dataset_volume_mount, model_volume_mount], |
| 137 | + name="ai-training-run-train", |
| 138 | + task_id="train", |
| 139 | + is_delete_operator_pod=True, |
| 140 | + hostnetwork=False |
| 141 | + ) |
| 142 | + |
| 143 | + # State that training job should be executed after dataset volume snapshot is taken |
| 144 | + dataset_snapshot >> train |
| 145 | + |
| 146 | + # Define step to take a snapshot of the model volume for versioning/baselining |
| 147 | + model_snapshot = KubernetesPodOperator( |
| 148 | + namespace=namespace, |
| 149 | + image="python:3", |
| 150 | + cmds=["/bin/bash", "-c"], |
| 151 | + arguments=["\ |
| 152 | + python3 -m pip install ipython kubernetes pandas tabulate && \ |
| 153 | + git clone https://github.com/NetApp/netapp-data-science-toolkit && \ |
| 154 | + mv /netapp-data-science-toolkit/Kubernetes/ntap_dsutil_k8s.py / && \ |
| 155 | + /ntap_dsutil_k8s.py create volume-snapshot --pvc-name=" + str(model_volume_pvc_existing) + " --snapshot-name=model-{{ task_instance.xcom_pull(task_ids='generate-uuid', dag_id='ai_training_run', key='return_value') }} --namespace=" + namespace], |
| 156 | + name="ai-training-run-model-snapshot", |
| 157 | + task_id="model-snapshot", |
| 158 | + is_delete_operator_pod=True, |
| 159 | + hostnetwork=False |
| 160 | + ) |
| 161 | + |
| 162 | + # State that the model snapshot should be created after the training job completes |
| 163 | + train >> model_snapshot |
| 164 | + |
| 165 | + # Define inference validation step using Kubernetes Pod operator (https://airflow.apache.org/docs/stable/kubernetes.html#kubernetespodoperator) |
| 166 | + validate = KubernetesPodOperator( |
| 167 | + namespace=namespace, |
| 168 | + image=validate_step_container_image, |
| 169 | + cmds=validate_step_command, |
| 170 | + resources = validate_step_resources, |
| 171 | + volumes=[dataset_volume, model_volume], |
| 172 | + volume_mounts=[dataset_volume_mount, model_volume_mount], |
| 173 | + name="ai-training-run-validate", |
| 174 | + task_id="validate", |
| 175 | + is_delete_operator_pod=True, |
| 176 | + hostnetwork=False |
| 177 | + ) |
| 178 | + |
| 179 | + # State that inference validation job should be executed after model volume snapshot is taken |
| 180 | + model_snapshot >> validate |
0 commit comments