forked from aws/amazon-sagemaker-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampling.py
71 lines (57 loc) · 2.45 KB
/
sampling.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
# 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 numpy as np
import torch
class SearchSpace(object):
"""
Setting the mask to 1 means we keep the corresponding head / unit
"""
def __init__(self, config, rng=None):
self.config = config
if config.model_type == "gpt2":
self.num_heads = config.n_head
self.num_layers = config.n_layer
self.intermediate_size = (
config.n_inner if config.n_inner is not None else 4 * config.hidden_size
)
else:
self.num_heads = config.num_attention_heads
self.num_layers = config.num_hidden_layers
self.intermediate_size = config.intermediate_size
if rng is None:
self.rng = np.random.RandomState(np.random.randint(2**32 - 1))
else:
self.rng = rng
def __call__(self, *args, **kwargs):
raise NotImplementedError
def get_smallest_sub_network(self):
raise NotImplementedError
class SmallSearchSpace(SearchSpace):
def __call__(self, *args, **kwargs):
num_layers = self.rng.randint(self.num_layers)
num_heads = self.rng.choice([int(self.num_heads / 2 ** i) for i in range(int(np.log2(self.num_heads)) + 1)])
num_units = self.rng.randint(1, self.intermediate_size)
return self._create_mask(num_layers, num_heads, num_units)
def _create_mask(self, num_layers, num_heads, num_units):
head_mask = torch.ones((self.num_layers, self.num_heads))
ffn_mask = torch.ones((self.num_layers, self.intermediate_size))
head_mask[num_layers:] = 0
head_mask[:num_layers, num_heads:] = 0
ffn_mask[num_layers:] = 0
ffn_mask[:num_layers, num_units:] = 0
return head_mask, ffn_mask
def get_smallest_sub_network(self):
num_layers = 1
num_heads = 1
num_units = 1
return self._create_mask(num_layers, num_heads, num_units)