File size: 5,325 Bytes
5f10efc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Score complete title spans from a frozen character representation."""

import numpy as np
import torch
from torch import nn

from shared.dataset_schema import visible_value


TITLE_ACTIONS = ('absorb_fields', 'expand', 'shrink')
TITLE_POINTER_WIDTH = 96


class CompactTitlePointer(nn.Module):
    def __init__(self, width=TITLE_POINTER_WIDTH):
        super().__init__()
        self.character = nn.Linear(192,width)
        self.boundary = nn.Sequential(nn.LayerNorm(3*width+1),nn.Linear(3*width+1,width),nn.GELU())
        self.unary = nn.Linear(width,2)
        self.start_query = nn.Linear(width,32)
        self.end_key = nn.Linear(width,32)

    def forward(self, hidden, lengths):
        features = self.character(hidden)
        offsets = torch.arange(features.size(1)+1,device=features.device)
        mask = offsets[:-1][None,:] < lengths[:,None]
        features = features * mask[...,None]
        edge = torch.zeros_like(features[:,:1])
        before = torch.cat((edge,features),dim=1)
        after = torch.cat((features,edge),dim=1)
        context = (features.sum(1)/lengths[:,None])[:,None].expand_as(before)
        position = offsets[None,:].expand(hidden.size(0),-1) / lengths[:,None]
        boundary = self.boundary(torch.cat((before,after,context,position[...,None]),dim=-1))
        unary = self.unary(boundary)
        return unary[...,0],unary[...,1],self.start_query(boundary)/np.sqrt(32),self.end_key(boundary)


def boundary_masks(rows, width, device):
    start = np.zeros((len(rows),width),dtype=bool)
    end = np.zeros_like(start)
    for i,row in enumerate(rows):
        visible = [not char.isspace() for char in row['text']]
        length = len(visible)
        start[i,:length] = visible
        end[i,1:length+1] = visible
    return torch.from_numpy(start).to(device),torch.from_numpy(end).to(device)


def title_alternatives(text, baseline, scores, actions=TITLE_ACTIONS):
    titles = [(i,f) for i,f in enumerate(baseline['fields']) if f['label']=='TITLE']
    if len(titles) != 1:
        return {}
    title_index, title = titles[0]
    old_start, old_end = title['start'],title['end']
    other = [(i,f['start'],f['end']) for i,f in enumerate(baseline['fields']) if i!=title_index]
    people = [(p['start'],p['end']) for p in baseline['contributors']]
    left,right,query,key = scores
    starts = np.array([n for n in range(max(0,old_start-12),min(len(text),old_start+13))
                       if not text[n].isspace()],dtype=np.int64)
    ends = np.array([n for n in range(max(1,old_end-100),min(len(text),old_end+100)+1)
                     if not text[n-1].isspace()],dtype=np.int64)
    if not len(starts) or not len(ends):
        return {}
    values = (left[starts,None]+right[None,ends]+query[starts]@key[ends].T)
    valid = starts[:,None]<ends[None,:]
    # Only propose intervals that overlap the detected title and preserve every contributor.
    valid &= (starts[:,None]<old_end) & (ends[None,:]>old_start)
    for begin,finish in people:
        valid &= (starts[:,None]>=finish) | (ends[None,:]<=begin)
    absorbed = np.zeros_like(valid)
    for _,begin,finish in other:
        overlap = (starts[:,None]<finish) & (ends[None,:]>begin)
        contained = (starts[:,None]<=begin) & (ends[None,:]>=finish)
        valid &= (~overlap | contained)
        absorbed |= contained
    base = float(left[old_start]+right[old_end]+query[old_start]@key[old_end])
    valid &= (starts[:,None]!=old_start) | (ends[None,:]!=old_end)
    expanding = (starts[:,None]<old_start) | (ends[None,:]>old_end)
    variants = {}
    for action,mask in zip(TITLE_ACTIONS,(absorbed,~absorbed & expanding,~absorbed & ~expanding),strict=True):
        if action not in actions:
            continue
        allowed = valid & mask
        if not allowed.any():
            continue
        ranked = np.where(allowed,values,-np.inf)
        i,j = np.unravel_index(ranked.argmax(),ranked.shape)
        start,end = int(starts[i]),int(ends[j])
        dropped = {p for p,a,b in other if start<=a and b<=end}
        result = [f for p,f in enumerate(baseline['fields']) if p!=title_index and p not in dropped]
        result.append({'label':'TITLE','start':start,'end':end,'value':visible_value(text[start:end],'TITLE')})
        result.sort(key=lambda field:field['start'])
        variants[action] = (float(ranked[i,j])-base,{**baseline,'fields':result})
    return variants


@torch.inference_mode()
def title_comparisons(rows, predictions, hidden, pointer, actions=TITLE_ACTIONS):
    lengths = torch.tensor([len(row['text']) for row in rows],device=hidden.device)
    tensors = pointer(hidden.float(),lengths)
    scores = [tensor.float().cpu().numpy() for tensor in tensors]
    return [title_alternatives(row['text'],pred,tuple(array[i] for array in scores),actions)
            for i,(row,pred) in enumerate(zip(rows,predictions,strict=True))]


def apply_title_comparisons(predictions, alternatives, thresholds):
    adjusted = []
    for baseline,options in zip(predictions,alternatives,strict=True):
        eligible = [(score-thresholds[action],action,proposal) for action,(score,proposal) in options.items()
                    if action in thresholds and score>thresholds[action]]
        adjusted.append(max(eligible)[2] if eligible else baseline)
    return adjusted