Skip to content

docre

knowledge_graph.news.docre

Load ATLOP/DREEAM checkpoints and predict relations from DocRED inputs.

build_model(implementation: str, tokenizer, classes: int, *, checkpoint: Path | None = None)

Initialize from RoBERTa for training, or restore a complete relation checkpoint on CPU.

Source code in src/knowledge_graph/news/docre.py
22
23
24
25
26
27
28
29
30
31
32
33
def build_model(implementation: str, tokenizer, classes: int, *, checkpoint: Path | None = None):
    """Initialize from RoBERTa for training, or restore a complete relation checkpoint on CPU."""
    config = AutoConfig.from_pretrained(ENCODER, num_labels=classes)
    config.transformer_type = "roberta"
    config.cls_token_id = tokenizer.cls_token_id
    config.sep_token_id = tokenizer.sep_token_id
    if checkpoint is None:
        encoder = AutoModel.from_pretrained(ENCODER, config=config, attn_implementation="eager", torch_dtype=torch.float32)
    else:
        encoder = AutoModel.from_config(config, attn_implementation="eager", torch_dtype=torch.float32)
    model = IMPLEMENTATIONS[implementation].DocREModel(config, encoder, num_labels=LABELS_PER_PAIR)
    return load_checkpoint(model, checkpoint, torch.device("cpu")) if checkpoint is not None else model

load_checkpoint(model, checkpoint: Path, device: torch.device)

Load FP32 weights; inference uses the same BF16 autocast as validation.

Source code in src/knowledge_graph/news/docre.py
36
37
38
39
40
def load_checkpoint(model, checkpoint: Path, device: torch.device):
    """Load FP32 weights; inference uses the same BF16 autocast as validation."""
    model.to(device=device, dtype=torch.float32)
    model.load_state_dict(torch.load(checkpoint, map_location=device, weights_only=True))
    return model

model_inputs(batch, device: torch.device) -> dict

Move encoder tensors to the model device and retain entity and pair offsets.

Source code in src/knowledge_graph/news/docre.py
43
44
45
46
47
48
49
50
def model_inputs(batch, device: torch.device) -> dict:
    """Move encoder tensors to the model device and retain entity and pair offsets."""
    return {
        "input_ids": batch["input_ids"].to(device),
        "attention_mask": batch["attention_mask"].to(device),
        "entity_pos": batch["entity_pos"],
        "hts": batch["hts"],
    }

infer(model, features, batch_size: int, *, long_input_encoder=None) -> np.ndarray

Source code in src/knowledge_graph/news/docre.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def infer(model, features, batch_size: int, *, long_input_encoder=None) -> np.ndarray:
    device = next(model.parameters()).device
    loader = DataLoader(features, batch_size=batch_size, shuffle=False, collate_fn=collate_fn)
    predictions = []
    model.eval()
    with torch.no_grad():
        for batch in loader:
            with torch.autocast(device_type=device.type, dtype=torch.bfloat16):
                outputs = model(
                    **model_inputs(batch, device),
                    long_input_encoder=long_input_encoder,
                )
            scores = outputs["rel_pred"]
            predictions.append(scores.float().cpu().numpy())
    return np.concatenate(predictions, axis=0)

predicted_triples(predictions: np.ndarray, features: list[dict], id2rel: dict[int, str]) -> set[tuple]

Source code in src/knowledge_graph/news/docre.py
70
71
72
73
74
75
76
77
78
def predicted_triples(predictions: np.ndarray, features: list[dict], id2rel: dict[int, str]) -> set[tuple]:
    output, row = set(), 0
    for feature in features:
        for head, tail in feature["hts"]:
            for relation_id in np.flatnonzero(predictions[row]):
                if relation_id:
                    output.add((feature["title"], head, id2rel[int(relation_id)], tail))
            row += 1
    return output

DocREPredictor

Predict relations from DocRED records with a trained ATLOP or DREEAM checkpoint.

Source code in src/knowledge_graph/news/docre.py
 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
class DocREPredictor:
    """Predict relations from DocRED records with a trained ATLOP or DREEAM checkpoint."""

    def __init__(self, implementation: str, rel2id_path: Path, checkpoint: Path):
        rel2id = load_json(rel2id_path)
        self.tokenizer = AutoTokenizer.from_pretrained(ENCODER)
        self.device = torch.device("cuda")
        self.model = build_model(implementation, self.tokenizer, len(rel2id), checkpoint=checkpoint).to(self.device)
        self._id2rel = {value: key for key, value in rel2id.items()}

    def predict(self, inputs: list[dict]) -> set[tuple]:
        """Return (title, head, relation, tail) tuples from inputs capped at 1,024 subwords."""
        return self._predict(inputs, MAX_LENGTH)

    def predict_with_windows(self, inputs: list[dict]) -> set[tuple]:
        """Predict on whole documents capped at 2,048 subwords using overlapping encoder windows."""
        return self._predict(inputs, 2048, encode_with_windows)

    def _predict(self, inputs: list[dict], max_length: int, encoder=None) -> set[tuple]:
        inputs = [document for document in inputs if len(document["vertexSet"]) >= 2]
        if not inputs:
            return set()
        features = prepare_features(inputs, self.tokenizer, max_length)
        array = infer(
            self.model, features, 1,
            long_input_encoder=encoder,
        )
        return predicted_triples(array, features, self._id2rel)

tokenizer = AutoTokenizer.from_pretrained(ENCODER) instance-attribute

device = torch.device('cuda') instance-attribute

model = build_model(implementation, self.tokenizer, len(rel2id), checkpoint=checkpoint).to(self.device) instance-attribute

predict(inputs: list[dict]) -> set[tuple]

Return (title, head, relation, tail) tuples from inputs capped at 1,024 subwords.

Source code in src/knowledge_graph/news/docre.py
91
92
93
def predict(self, inputs: list[dict]) -> set[tuple]:
    """Return (title, head, relation, tail) tuples from inputs capped at 1,024 subwords."""
    return self._predict(inputs, MAX_LENGTH)

predict_with_windows(inputs: list[dict]) -> set[tuple]

Predict on whole documents capped at 2,048 subwords using overlapping encoder windows.

Source code in src/knowledge_graph/news/docre.py
95
96
97
def predict_with_windows(self, inputs: list[dict]) -> set[tuple]:
    """Predict on whole documents capped at 2,048 subwords using overlapping encoder windows."""
    return self._predict(inputs, 2048, encode_with_windows)