File size: 3,550 Bytes
2449566
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch


def get_span_hidden_states(inputs, hidden_states, attentions, safe_idx, pooler_mask, attention_mask,

                           hidden_layer_fineturn, weight_pooling=True, span_weight=True, is_causal=False):
    batch_size, seq_length = inputs['input_ids'].size()
    batch_idxs = torch.arange(batch_size, device=inputs['input_ids'].device)[:, None, None]

    # batch_size, max_seg, max_len_all = safe_idx.size()
    # g_safe_idx = safe_idx.view(batch_size, max_seg // 4, -1)
    # g_pooler_mask = pooler_mask.view(batch_size, max_seg // 4, -1)

    mask_2d = attention_mask.unsqueeze(1) * attention_mask.unsqueeze(2)   # (B, N, N)
    mask_4d = mask_2d.unsqueeze(1)

    g_safe_idx = safe_idx
    g_pooler_mask = pooler_mask

    hidden_state_pools = []
    span_weights = []
    for i in hidden_layer_fineturn:
        # weights = attentions[i].sum(dim=(1, 2))
        if is_causal:
            weights = attentions[i-1].sum(dim=1)[:, -1].detach()
        else:
            weights = (attentions[i-1] * mask_4d).sum(dim=(1, 2)).detach()

        weights = weights / weights.sum(-1, keepdim=True)
        weights = weights.unsqueeze(-1)[batch_idxs, g_safe_idx] * g_pooler_mask.unsqueeze(-1)

        gathered = hidden_states[i][batch_idxs, g_safe_idx] * g_pooler_mask.unsqueeze(-1)
        gathered = gathered * weights

        hidden_state_mean = gathered.sum(2) / weights.sum(2).clamp(min=1e-5)
        hidden_state_pools.append(hidden_state_mean)
        span_weights.append(weights.sum(2))

    span_hidden_states = torch.stack(hidden_state_pools)
    span_weights = torch.stack(span_weights)

    return span_hidden_states, span_weights

def get_span_hidden_states_custom(inputs, hidden_states, attentions, safe_idx, pooler_mask, attention_mask,

                                  hidden_layer_fineturn, weight_pooling=False, span_weight=False, is_causal=False):
    batch_size, seq_length = inputs['input_ids'].size()
    batch_idxs = torch.arange(batch_size, device=inputs['input_ids'].device)[:, None, None]

    g_safe_idx = safe_idx
    g_pooler_mask = pooler_mask

    mask_2d = attention_mask.unsqueeze(1) * attention_mask.unsqueeze(2)   # (B, N, N)
    mask_4d = mask_2d.unsqueeze(1)

    hidden_state_pools = []
    span_weights = []
    for i in hidden_layer_fineturn:
        # weights = attentions[i].sum(dim=(1, 2))
        if is_causal:
            weights = attentions[i-1].sum(dim=1)[:, -1].detach()
        else:
            weights = (attentions[i-1] * mask_4d).sum(dim=(1, 2)).detach()

        weights = weights / weights.sum(-1, keepdim=True)
        weights = weights.unsqueeze(-1)[batch_idxs, g_safe_idx] * g_pooler_mask.unsqueeze(-1)

        gathered = hidden_states[i][batch_idxs, g_safe_idx] * g_pooler_mask.unsqueeze(-1)
        if weight_pooling:
            gathered = gathered * weights
            hidden_state_mean = gathered.sum(2) / weights.sum(2).clamp(min=1e-5)
        else:
            hidden_state_mean = gathered.sum(2) / g_pooler_mask.sum(2, keepdim=True).clamp(min=1e-5)

        hidden_state_pools.append(hidden_state_mean)

        if span_weight:
            span_weights.append(weights.sum(2))
        else:
            # span_weights.append(g_pooler_mask.sum(2, keepdim=True).clamp(max=1.0))
            span_weights.append(weights.sum(2) ** 1e-5)

    span_hidden_states = torch.stack(hidden_state_pools)
    span_weights = torch.stack(span_weights)

    return span_hidden_states, span_weights