-
Notifications
You must be signed in to change notification settings - Fork 85
/
0.aws-batch-distributed-training.yaml
426 lines (389 loc) · 13.2 KB
/
0.aws-batch-distributed-training.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
AWSTemplateFormatVersion: '2010-09-09'
Description: >
Setup for large scale AWS Batch with CEs, JQ, ECR and Job Definition.
Author: Pierre-Yves Aquilanti - pierreya@
Parameters:
VPCStackParameter:
Type: String
Description: Private subnets will be retrieved for the compute environment
Default: 'LargeScaleVPC'
AMI:
Description: Default ECS Optimized AMI or your own AMI ID for the Compute Environment
Type: AWS::EC2::Image::Id
Default: ''
CapacityReservationId:
Description: Leave empty if unused or if using Capacity Reservation Resource Group
Type: String
Default: ''
CapacityReservationResourceGroup:
Description: Leave empty if unused or if using Capacity Reservation ID
Type: String
Default: ''
EC2KeyPair:
Description: Select your keypair or leave as default, useful for debug
Type: AWS::EC2::KeyPair::KeyName
CreatePlacementGroup:
AllowedValues:
- 'true'
- 'false'
Default: 'false'
Description:
Create a new placement group with cluster placement
Type: String
Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: General configuration
Parameters:
- VPCStackParameter
- Label:
default: AWS Batch Configuration
Parameters:
- AMI
- CapacityReservationId
- CapacityReservationResourceGroup
- CreatePlacementGroup
- EC2KeyPair
ParameterLabels:
VPCStackParameter:
default: Name of the VPC Stack
AMI:
default: ECS Compatible AMI
CapacityReservationId:
default: Capacity Reservation ID
CapacityReservationResourceGroup:
default: Capacity Reservation Resource Group ARN
CreatePlacementGroup:
default: Create Placement Group
EC2KeyPair:
default: EC2 Keypair
Conditions:
ImageID: !Equals [ !Ref AMI, '' ]
CrID: !Equals [ !Ref CapacityReservationId, '' ]
CrRG: !Equals [ !Ref CapacityReservationResourceGroup, '' ]
PG: !Equals [ !Ref CreatePlacementGroup, 'true' ]
KeyPair: !Equals [ !Ref EC2KeyPair, '' ]
Resources:
###################
## EC2 Resources ##
###################
DistributedDeepLearningLT:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
CapacityReservationSpecification:
CapacityReservationTarget:
CapacityReservationId: !If [ CrID, !Ref 'AWS::NoValue', !Ref CapacityReservationId]
CapacityReservationResourceGroupArn: !If [ CrRG, !Ref 'AWS::NoValue', !Ref CapacityReservationResourceGroup]
NetworkInterfaces:
- Description: EFA Interface 0
Groups: !Split
- ','
- Fn::ImportValue: !Sub ${VPCStackParameter}-SecurityGroup
NetworkCardIndex: 0
DeviceIndex: 0
DeleteOnTermination: true
InterfaceType: efa
- Description: EFA Interface 1
Groups: !Split
- ','
- Fn::ImportValue: !Sub ${VPCStackParameter}-SecurityGroup
NetworkCardIndex: 1
DeviceIndex: 1
DeleteOnTermination: true
InterfaceType: efa
- Description: EFA Interface 2
Groups: !Split
- ','
- Fn::ImportValue: !Sub ${VPCStackParameter}-SecurityGroup
NetworkCardIndex: 2
DeviceIndex: 2
DeleteOnTermination: true
InterfaceType: efa
- Description: EFA Interface 3
Groups: !Split
- ','
- Fn::ImportValue: !Sub ${VPCStackParameter}-SecurityGroup
NetworkCardIndex: 3
DeviceIndex: 3
DeleteOnTermination: true
InterfaceType: efa
UserData:
Fn::Base64:
!Sub |
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="==MYBOUNDARY=="
--==MYBOUNDARY==
Content-Type: text/x-shellscript; charset="us-ascii"
#!/bin/bash
# # log userdata to console
# exec >>(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
# # Format NVMe drives as RAID0 volume and mount on /scratch
# N_DRIVES=8 # max number of drives
# for DISK in $(seq 1 $N_DRIVES)
# do
# sed -e 's/\s*\([\+0-9a-zA-Z]*\).*/\1/' << EOF | sudo fdisk /dev/nvme${!DISK}n1
# o # clear the in memory partition table
# n # new partition
# p # primary partition
# 1 # partition number 1
# # default - start at beginning of disk
# # default - stop at end of disk
# p # print the in-memory partition table
# t # change partition type
# fd #
# p # print the in-memory partition table
# w # save
# q # and we're done
# EOF
# done
# # do some checking
# mdadm --examine /dev/nvme[1-$N_DRIVES]n1
# # create the raid devise, echo y for the create array validation
# echo y | mdadm --create /dev/md0 -l raid0 -n $N_DRIVES /dev/nvme[1-$N_DRIVES]n1
# # display some information
# cat /proc/mdstat
# mdadm --examine /dev/nvme[1-$N_DRIVES]n1
# mdadm --detail /dev/md0
# # format the file system
# yum install xfsprogs -y
# # use 256k block size:
# mkfs.xfs -d su=256k -d sw=$N_DRIVES /dev/md0
# # and mount
# mkdir /scratch
# mount /dev/md0 /scratch
# echo '/dev/md0 /scratch xfs defaults 0 0' | sudo tee -a /etc/fstab
# # saves the raid configurations
# mdadm --detail --scan --verbose | sudo tee -a /etc/mdadm.conf
# cat /etc/mdadm.conf
# # dirty
# chmod 777 /scratch
# echo ECS_ENABLED_GPU_SUPPORT=true>>/etc/ecs/ecs.config
--==MYBOUNDARY==--
ClusterPlacementGroup:
Type: AWS::EC2::PlacementGroup
Properties:
Strategy: cluster
########################
## Batch Architecture ##
########################
##
## IAM Roles for AWS Batch
##
# Configure IAM roles for Batch
BatchInstanceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- batch.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole
ECSTaskServiceRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
-
Effect: Allow
Principal:
Service:
- ec2.amazonaws.com
Action:
- sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role
ECSTaskInstanceProfile:
Type: AWS::IAM::InstanceProfile
Properties:
Path: /
Roles:
- !Ref ECSTaskServiceRole
InstanceProfileName: !Join [ "", [ "ECSTaskInstanceProfileIAM-", !Ref AWS::StackName ] ]
##
## Compute Environment and Job Definition
##
# Build the AWS Batch CEs
DistributedDeepLearningCE:
Type: AWS::Batch::ComputeEnvironment
Properties:
Type: MANAGED
ServiceRole: !Ref BatchInstanceRole
ComputeResources:
AllocationStrategy: BEST_FIT
MaxvCpus: 100000
DesiredvCpus: 0
MinvCpus: 0
PlacementGroup: !If [ PG, !Ref AWS::NoValue, !Ref ClusterPlacementGroup]
Subnets: !Split
- ','
- Fn::ImportValue: !Sub ${VPCStackParameter}-PrivateSubnet
Type: EC2
Ec2KeyPair: !If [ KeyPair, !Ref AWS::NoValue, !Ref EC2KeyPair ]
ImageId: !If [ ImageID, '/aws/service/ecs/optimized-ami/amazon-linux-2/gpu/recommended/image_id', !Ref AMI]
InstanceRole: !Ref ECSTaskInstanceProfile
LaunchTemplate:
LaunchTemplateId: !Ref DistributedDeepLearningLT
Version: $Latest
InstanceTypes:
- p4d.24xlarge
State: ENABLED
Tags:
Name: Batch Deep Learning
# Job Queue
DistributedDeepLearningJQ:
Type: AWS::Batch::JobQueue
Properties:
ComputeEnvironmentOrder:
- ComputeEnvironment: !Ref DistributedDeepLearningCE
Order: 1
Priority: 1
State: "ENABLED"
##
## ECR and AWS Batch Job definition
##
DistributedDeepLearningRepository:
Type: AWS::ECR::Repository
StressJDSingleInstance:
Type: AWS::Batch::JobDefinition
Properties:
Type: container
ContainerProperties:
Command:
- /workspace/gpu-nccl-sanity.sh
Image: !Join ['', [!Ref 'AWS::AccountId','.dkr.ecr.', !Ref 'AWS::Region', '.amazonaws.com/', !Ref DistributedDeepLearningRepository ] ]
ResourceRequirements:
- Type: VCPU
Value: 1
- Type: GPU
Value: 8
- Type: MEMORY
Value: 16384
Ulimits:
- Name: memlock
HardLimit: -1
SoftLimit: -1
- Name: stack
HardLimit: 67108864
SoftLimit: 67108864
- Name: nofile
HardLimit: 1024000
SoftLimit: 1024000
LinuxParameters:
SharedMemorySize: 8192
Devices:
- HostPath: /dev/infiniband/uverbs0
ContainerPath: /dev/infiniband/uverbs0
Permissions:
- READ
- WRITE
- MKNOD
- HostPath: /dev/infiniband/uverbs1
ContainerPath: /dev/infiniband/uverbs1
Permissions:
- READ
- WRITE
- MKNOD
- HostPath: /dev/infiniband/uverbs2
ContainerPath: /dev/infiniband/uverbs2
Permissions:
- READ
- WRITE
- MKNOD
- HostPath: /dev/infiniband/uverbs3
ContainerPath: /dev/infiniband/uverbs3
Permissions:
- READ
- WRITE
- MKNOD
PropagateTags: true
RetryStrategy:
Attempts: 10
StressJDMultiInstances:
Type: AWS::Batch::JobDefinition
Properties:
Type: multinode
NodeProperties:
MainNode: 0
NumNodes: 2
NodeRangeProperties:
- TargetNodes: '0:'
Container:
InstanceType: p4d.24xlarge
Image: !Join ['', [!Ref 'AWS::AccountId','.dkr.ecr.', !Ref 'AWS::Region', '.amazonaws.com/', !Ref DistributedDeepLearningRepository ] ]
Command:
- /workspace/gpu-nccl-sanity.sh
ResourceRequirements:
- Type: VCPU
Value: 1
- Type: GPU
Value: 8
- Type: MEMORY
Value: 16384
Ulimits:
- Name: memlock
HardLimit: -1
SoftLimit: -1
- Name: stack
HardLimit: 67108864
SoftLimit: 67108864
- Name: nofile
HardLimit: 1024000
SoftLimit: 1024000
LinuxParameters:
SharedMemorySize: 8192
Devices:
- HostPath: /dev/infiniband/uverbs0
ContainerPath: /dev/infiniband/uverbs0
Permissions:
- READ
- WRITE
- MKNOD
- HostPath: /dev/infiniband/uverbs1
ContainerPath: /dev/infiniband/uverbs1
Permissions:
- READ
- WRITE
- MKNOD
- HostPath: /dev/infiniband/uverbs2
ContainerPath: /dev/infiniband/uverbs2
Permissions:
- READ
- WRITE
- MKNOD
- HostPath: /dev/infiniband/uverbs3
ContainerPath: /dev/infiniband/uverbs3
Permissions:
- READ
- WRITE
- MKNOD
PropagateTags: true
RetryStrategy:
Attempts: 10
Outputs:
ECRRepository:
Description: ECR Repository for the containers
Value: !Ref DistributedDeepLearningRepository
ECRRepositoryUrl:
Description: ECR Repository for the containers
Value: !Join ['', [!Ref 'AWS::AccountId','.dkr.ecr.', !Ref 'AWS::Region', '.amazonaws.com/', !Ref DistributedDeepLearningRepository ] ]
JobDefinitionSingleInstance:
Description: Job definition for single instance Jobs
Value: !Ref StressJDSingleInstance
JobDefinitionMultiInstance:
Description: Job definition for Multi-node Parallel Jobs
Value: !Ref StressJDMultiInstances
DistributedDeepLearningJQ:
Description: Job Queue
Value: !Ref DistributedDeepLearningJQ