File size: 4,173 Bytes
25f0c96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from sagemaker.huggingface import HuggingFace
import logging
import sys
from contextlib import contextmanager
from io import StringIO
from streamlit.report_thread import REPORT_CONTEXT_ATTR_NAME
from threading import current_thread
import streamlit as st
import sys
import sagemaker
import boto3


@contextmanager
def st_redirect(src, dst):
    placeholder = st.empty()
    output_func = getattr(placeholder, dst)

    with StringIO() as buffer:
        old_write = src.write

        def new_write(b):
            if getattr(current_thread(), REPORT_CONTEXT_ATTR_NAME, None):
                buffer.write(b)
                output_func(buffer.getvalue())
            else:
                old_write(b)

        try:
            src.write = new_write
            yield
        finally:
            src.write = old_write


@contextmanager
def st_stdout(dst):
    with st_redirect(sys.stdout, dst):
        yield


@contextmanager
def st_stderr(dst):
    with st_redirect(sys.stderr, dst):
        yield


task2script = {
    "text-classification": {
        "entry_point": "run_glue.py",
        "source_dir": "examples/text-classification",
    },
    "token-classification": {
        "entry_point": "run_ner.py",
        "source_dir": "examples/token-classification",
    },
    "question-answering": {
        "entry_point": "run_qa.py",
        "source_dir": "examples/question-answering",
    },
    "summarization": {
        "entry_point": "run_summarization.py",
        "source_dir": "examples/seq2seq",
    },
    "translation": {
        "entry_point": "run_translation.py",
        "source_dir": "examples/seq2seq",
    },
    "causal-language-modeling": {
        "entry_point": "run_clm.py",
        "source_dir": "examples/language-modeling",
    },
    "masked-language-modeling": {
        "entry_point": "run_mlm.py",
        "source_dir": "examples/language-modeling",
    },
}


def train_estimtator(parameter, config):
    with st_stdout("code"):
        logger = logging.getLogger(__name__)

        logging.basicConfig(
            level=logging.getLevelName("INFO"),
            handlers=[logging.StreamHandler(sys.stdout)],
            format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        )
        logger.info = print

        # git configuration to download our fine-tuning script
        git_config = {"repo": "https://github.com/huggingface/transformers.git", "branch": "v4.4.2"}

        # creating fine-tuning script
        entry_point = task2script[parameter["task"]]["entry_point"]
        source_dir = task2script[parameter["task"]]["source_dir"]
        # create train file
        # iam configuration
        session = boto3.session.Session(
            aws_access_key_id=config["aws_access_key_id"],
            aws_secret_access_key=config["aws_secret_accesskey"],
            region_name=config["region"],
        )
        sess = sagemaker.Session(boto_session=session)

        iam = session.client(
            "iam", aws_access_key_id=config["aws_access_key_id"], aws_secret_access_key=config["aws_secret_accesskey"]
        )
        role = iam.get_role(RoleName=config["aws_sagemaker_role"])["Role"]["Arn"]

        logger.info(f"role: {role}")
        instance_type = config["instance_type"].split("|")[1].split("|")[0].strip()
        logger.info(f"instance_type: {instance_type}")

        hyperparameters = {
            "output_dir": "/opt/ml/model",
            "do_train": True,
            "do_eval": True,
            "do_predict": True,
            **parameter,
        }
        del hyperparameters["task"]
        # create estimator
        huggingface_estimator = HuggingFace(
            entry_point=entry_point,
            source_dir=source_dir,
            git_config=git_config,
            base_job_name=config["job_name"],
            instance_type=instance_type,
            sagemaker_session=sess,
            instance_count=config["instance_count"],
            role=role,
            transformers_version="4.4",
            pytorch_version="1.6",
            py_version="py36",
            hyperparameters=hyperparameters,
        )
        # train
        huggingface_estimator.fit()