forked from aws/amazon-sagemaker-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate_subnetwork.py
188 lines (147 loc) · 5.48 KB
/
evaluate_subnetwork.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
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
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
import os
import logging
import sys
import tarfile
from dataclasses import dataclass, field
import numpy as np
import torch
import datasets
from transformers import (
AutoModelForSequenceClassification,
HfArgumentParser,
TrainingArguments,
set_seed,
)
from evaluate import load
from estimate_efficency import compute_parameters
from task_data import TASKINFO
from mask import mask_bert
from hf_args import DataTrainingArguments, ModelArguments
from load_glue_datasets import load_glue_datasets
task_to_keys = {
"cola": ("sentence", None),
"mnli": ("premise", "hypothesis"),
"mrpc": ("sentence1", "sentence2"),
"qnli": ("question", "sentence"),
"qqp": ("question1", "question2"),
"rte": ("sentence1", "sentence2"),
"sst2": ("sentence", None),
"stsb": ("sentence1", "sentence2"),
"wnli": ("sentence1", "sentence2"),
}
logger = logging.getLogger(__name__)
@dataclass
class SearchArguments:
"""
Arguments to define the search
"""
num_layers: int = field()
num_heads: int = field()
num_units: int = field()
checkpoint_dir_model: str = field(
metadata={"help": ""}, default=os.environ["SM_CHANNEL_MODEL"]
)
def main():
parser = HfArgumentParser(
(ModelArguments, DataTrainingArguments, TrainingArguments, SearchArguments)
)
(
model_args,
data_args,
training_args,
search_args,
) = parser.parse_args_into_dataclasses()
# Setup logging
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
handlers=[logging.StreamHandler(sys.stdout)],
)
log_level = training_args.get_process_log_level()
logger.setLevel(log_level)
datasets.utils.logging.set_verbosity(log_level)
# Set seed before initializing model
if int(training_args.seed) == -1:
training_args.seed = np.random.randint(2**32 - 1)
set_seed(training_args.seed)
# Extract tar ball
tar = tarfile.open(search_args.checkpoint_dir_model + '/model.tar.gz')
tar.extractall(training_args.output_dir)
tar.close()
model_type = model_args.model_name_or_path
is_regression = data_args.task_name == "stsb"
# Evaluation metric
metric = load("glue", data_args.task_name)
# Load data
_, eval_dataloader, test_dataloader = load_glue_datasets(
training_args=training_args, model_args=model_args, data_args=data_args
)
model = AutoModelForSequenceClassification.from_pretrained(
training_args.output_dir
)
config = model.config
if model_type.startswith("bert"):
attention_size = config.hidden_size
num_attention_heads = config.num_attention_heads
attention_head_size = int(attention_size / num_attention_heads)
num_layers = config.num_hidden_layers
intermediate_size = config.intermediate_size
mask = mask_bert
n_params_emb = sum(
p.numel() for p in model.bert.embeddings.parameters() if p.requires_grad
)
n_params_pooler = sum(
p.numel() for p in model.bert.pooler.parameters() if p.requires_grad
)
n_params_classifier = sum(
p.numel() for p in model.classifier.parameters() if p.requires_grad
)
n_params_classifier += n_params_pooler
else:
raise AttributeError(f'Model {model_type} is not supported at this point!')
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model.to(device)
metric_name = TASKINFO[data_args.task_name]["metric"]
head_mask = torch.ones((num_layers, num_attention_heads))
neuron_mask = torch.ones((num_layers, intermediate_size))
head_mask[search_args.num_layers:, :] = 0
head_mask[: search_args.num_layers:, search_args.num_heads:] = 0
neuron_mask[search_args.num_layers:, :] = 0
neuron_mask[: search_args.num_layers:, search_args.num_units:] = 0
head_mask = head_mask.to(device)
neuron_mask = neuron_mask.to(device)
n_params_model = compute_parameters(
dmodel=attention_size,
dhead=attention_head_size,
num_heads_per_layer=head_mask.sum(dim=1),
num_neurons_per_layer=neuron_mask.sum(dim=1),
)
n_params = n_params_emb + n_params_model + n_params_classifier
handles = mask(model, neuron_mask, head_mask)
model.eval()
for batch in eval_dataloader:
batch = {k: v.to(device) for k, v in batch.items()}
with torch.no_grad():
outputs = model(head_mask=head_mask, **batch)
logits = outputs.logits
predictions = (
torch.squeeze(logits) if is_regression else torch.argmax(logits, dim=-1)
)
metric.add_batch(predictions=predictions, references=batch["labels"])
eval_metric = metric.compute()
print(f"number of parameters: {n_params}")
print(f"validation error: {1 - eval_metric[metric_name]}")
if __name__ == "__main__":
main()