-
Notifications
You must be signed in to change notification settings - Fork 0
/
version_aws.py
145 lines (132 loc) · 5.25 KB
/
version_aws.py
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
import gzip
import io
import pickle
import airflow.utils.dates
from airflow import DAG
from airflow.operators.python import PythonOperator
from airflow.providers.amazon.aws.hooks.s3 import S3Hook
from airflow.providers.amazon.aws.operators.s3_copy_object import S3CopyObjectOperator
from airflow.providers.amazon.aws.operators.sagemaker_endpoint import (
SageMakerEndpointOperator,
)
from airflow.providers.amazon.aws.operators.sagemaker_training import (
SageMakerTrainingOperator,
)
from sagemaker.amazon.common import write_numpy_to_dense_tensor
dag = DAG(
dag_id="version_aws",
schedule_interval=None,
start_date=airflow.utils.dates.days_ago(3),
)
download_mnist_data = S3CopyObjectOperator(
task_id="download_mnist_data",
source_bucket_name="sagemaker-sample-data-eu-west-1",
source_bucket_key="algorithms/kmeans/mnist/mnist.pkl.gz",
dest_bucket_name="your-bucket-aawse-eu-west-1",
dest_bucket_key="mnist.pkl.gz",
dag=dag,
verify=False,
)
def _extract_mnist_data():
s3hook = S3Hook()
# Download S3 dataset into memory
mnist_buffer = io.BytesIO()
mnist_obj = s3hook.get_key(bucket_name="your-bucket-aawse-eu-west-1", key="mnist.pkl.gz")
mnist_obj.download_fileobj(mnist_buffer)
# Unpack gzip file, extract dataset, convert to dense tensor, upload back to S3
mnist_buffer.seek(0)
with gzip.GzipFile(fileobj=mnist_buffer, mode="rb") as f:
train_set, _, _ = pickle.loads(f.read(), encoding="latin1")
output_buffer = io.BytesIO()
write_numpy_to_dense_tensor(
file=output_buffer, array=train_set[0], labels=train_set[1]
)
output_buffer.seek(0)
s3hook.load_file_obj(
output_buffer, key="mnist_data", bucket_name="your-bucket-aawse-eu-west-1", replace=True
)
extract_mnist_data = PythonOperator(
task_id="extract_mnist_data", python_callable=_extract_mnist_data, dag=dag
)
sagemaker_train_model = SageMakerTrainingOperator(
task_id="sagemaker_train_model",
config={
"TrainingJobName": "mnistclassifier-{{ execution_date.strftime('%Y-%m-%d-%H-%M-%S') }}",
"AlgorithmSpecification": {
"TrainingImage": "438346466558.dkr.ecr.eu-west-1.amazonaws.com/kmeans:1",
"TrainingInputMode": "File",
},
"HyperParameters": {"k": "10", "feature_dim": "784"},
"InputDataConfig": [
{
"ChannelName": "train",
"DataSource": {
"S3DataSource": {
"S3DataType": "S3Prefix",
"S3Uri": "s3://your-bucket-aawse-eu-west-1/mnist_data",
"S3DataDistributionType": "FullyReplicated",
}
},
}
],
"OutputDataConfig": {"S3OutputPath": "s3://your-bucket-aawse-eu-west-1/mnistclassifier-output"},
"ResourceConfig": {
"InstanceType": "ml.c4.xlarge",
"InstanceCount": 1,
"VolumeSizeInGB": 10,
},
"RoleArn": (
"arn:aws:iam::861327392019:role/SageMakerFull"
# "arn:aws:iam::AKIA4RCY47UJS6CSN4GE:role/service-role/"
# "AmazonSageMaker-ExecutionRole-20180905T153196"
),
"StoppingCondition": {"MaxRuntimeInSeconds": 24 * 60 * 60},
},
wait_for_completion=True,
print_log=True,
check_interval=10,
dag=dag,
)
sagemaker_deploy_model = SageMakerEndpointOperator(
task_id="sagemaker_deploy_model",
operation="update",
# operation="create",
wait_for_completion=True,
config={
"Model": {
"ModelName": "mnistclassifier-{{ execution_date.strftime('%Y-%m-%d-%H-%M-%S') }}",
"PrimaryContainer": {
"Image": "438346466558.dkr.ecr.eu-west-1.amazonaws.com/kmeans:1",
"ModelDataUrl": (
"s3://your-bucket-aawse-eu-west-1/mnistclassifier-output/mnistclassifier"
"-{{ execution_date.strftime('%Y-%m-%d-%H-%M-%S') }}/"
"output/model.tar.gz"
), # this will link the model and the training job
},
"ExecutionRoleArn": (
"arn:aws:iam::861327392019:role/SageMakerFull"
# "arn:aws:iam::AKIA4RCY47UJS6CSN4GE:role/service-role/"
# "AmazonSageMaker-ExecutionRole-20180905T153196"
),
},
"EndpointConfig": {
"EndpointConfigName": "mnistclassifier-{{ execution_date.strftime('%Y-%m-%d-%H-%M-%S') }}",
"ProductionVariants": [
{
"InitialInstanceCount": 1,
"InstanceType": "ml.t2.medium",
"ModelName": "mnistclassifier-{{ execution_date.strftime('%Y-%m-%d-%H-%M-%S') }}",
# "ModelName": "mnistclassifier",
"VariantName": "AllTraffic",
}
],
},
"Endpoint": {
"EndpointConfigName": "mnistclassifier-{{ execution_date.strftime('%Y-%m-%d-%H-%M-%S') }}",
"EndpointName": "mnistclassifier",
# "EndpointName": "mnistclassifier-{{ execution_date.strftime('%Y-%m-%d-%H-%M-%S') }}",
},
},
dag=dag,
)
download_mnist_data >> extract_mnist_data >> sagemaker_train_model >> sagemaker_deploy_model