File size: 8,892 Bytes
897fe06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8db8077
 
 
 
 
 
 
 
 
 
 
d1ea2f2
897fe06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c2ae95
 
897fe06
5a3962c
 
897fe06
 
 
 
94752a5
 
897fe06
 
 
 
f550456
 
 
3f792e3
f550456
897fe06
 
 
 
 
 
 
 
 
 
 
d1ea2f2
 
0c2ae95
 
d1ea2f2
 
 
 
 
 
 
5a3962c
 
d1ea2f2
 
 
 
 
 
 
 
 
 
 
8b94669
d1ea2f2
8b94669
 
8db8077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b94669
8db8077
 
 
8b94669
8db8077
 
 
8b94669
897fe06
8db8077
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
897fe06
 
 
 
 
 
 
 
 
 
 
 
f550456
 
 
 
 
 
 
 
 
 
 
 
897fe06
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from typing import Any, Dict, Tuple

import torch
import torch.nn as nn
import torch.nn.functional as F
from lightning import LightningModule
from torchmetrics import MaxMetric, MeanMetric
from torchmetrics.classification.accuracy import Accuracy

from transformers import BertModel


class MiniAgentModule(LightningModule):
    def __init__(
        self,
        bert_model: str,
        inst_proj_model: nn.Module,
        tool_proj_model: nn.Module,
        pred_model: nn.Module,
        lr: float,
    ) -> None:
        super().__init__()

        self.save_hyperparameters(
            logger=False, ignore=["inst_proj_model", "tool_proj_model", "pred_model"]
        )

        self.bert_model = BertModel.from_pretrained(bert_model)

        self.inst_proj_model = inst_proj_model
        self.tool_proj_model = tool_proj_model
        self.pred_model = pred_model

        self.val_1_acc = Accuracy(task="binary")
        self.val_1_precision = MeanMetric()
        self.val_1_recall = MeanMetric()

        self.val_2_acc = Accuracy(task="binary")
        self.val_2_precision = MeanMetric()
        self.val_2_recall = MeanMetric()

        self.val_other_acc = Accuracy(task="binary")
        self.val_other_precision = MeanMetric()
        self.val_other_recall = MeanMetric()

        self.lr = lr

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        pass

    def on_train_start(self) -> None:
        pass

    def training_step(
        self, batch: Tuple[torch.Tensor, torch.Tensor], batch_idx: int
    ) -> torch.Tensor:
        B = batch["inst_ids"].shape[0]

        inst_ids = batch["inst_ids"]
        inst_mask = batch["inst_mask"]
        tool_ids = batch["tool_ids"]
        tool_mask = batch["tool_mask"]

        inst_z = self.bert_model(inst_ids, inst_mask, return_dict=False)[0]
        tool_z = self.bert_model(tool_ids, tool_mask, return_dict=False)[0]

        inst_emb = self.inst_proj_model(inst_z)
        tool_emb = self.tool_proj_model(tool_z)

        inst_emb_r = inst_emb.unsqueeze(1).repeat(1, B, 1).view(B * B, -1)
        tool_emb_r = tool_emb.unsqueeze(0).repeat(B, 1, 1).view(B * B, -1)

        pred = self.pred_model(inst_emb_r, tool_emb_r)  # [BxB, 1]
        pred = pred.view(B, B)  # [B, B]

        target = torch.eye(B, device=pred.device).float()

        pos_weight = torch.tensor([B - 1], device=pred.device)
        # pos_weight = torch.tensor([1], device=pred.device)
        loss = F.binary_cross_entropy_with_logits(pred, target, pos_weight=pos_weight)

        self.log("train/loss", loss, on_step=True, sync_dist=True, prog_bar=True)

        return loss

    def on_train_epoch_end(self) -> None:
        pass

    def validation_step(
        self, batch: Tuple[torch.Tensor, torch.Tensor], batch_idx: int
    ) -> None:
        inst_ids = batch["inst_ids"]
        inst_mask = batch["inst_mask"]
        tool_ids = batch["tool_ids"]
        tool_mask = batch["tool_mask"]
        correct_tool_mask = batch["correct_tool_mask"]

        B = inst_ids.shape[0]  # batch size
        C = correct_tool_mask.shape[1]  # tool capacity
        tool_ids = tool_ids.view(-1, tool_ids.shape[-1])  # [B*C, L]
        tool_mask = tool_mask.view(-1, tool_mask.shape[-1])  # [B*C, L]

        inst_z = self.bert_model(inst_ids, inst_mask, return_dict=False)[0]
        tool_z = self.bert_model(tool_ids, tool_mask, return_dict=False)[0]

        inst_emb = self.inst_proj_model(inst_z)  # [B, D]
        tool_emb = self.tool_proj_model(tool_z)  # [B*C, D]

        inst_emb_r = inst_emb.unsqueeze(1).repeat(1, C, 1).view(B * C, -1)
        tool_emb_r = tool_emb.view(B * C, -1)

        pred = self.pred_model(inst_emb_r, tool_emb_r)  # [B*C, 1]
        pred = pred.view(B, C)
        pred = torch.sigmoid(pred)

        pred_tool_mask = pred > 0.5

        true_pos_mask = pred_tool_mask & correct_tool_mask

        one_tool_mask = correct_tool_mask.sum(dim=1) == 1
        two_tool_mask = correct_tool_mask.sum(dim=1) == 2
        other_mask = ~(one_tool_mask | two_tool_mask)

        # one tool
        one_tool_pos_sample = (
            (pred_tool_mask[one_tool_mask] == correct_tool_mask[one_tool_mask])
            .all(dim=1)
            .long()
        )

        one_tool_precision = true_pos_mask[one_tool_mask].sum(dim=1) / torch.clamp(
            pred_tool_mask[one_tool_mask].sum(dim=1), min=1
        )

        one_tool_recall = true_pos_mask[one_tool_mask].sum(dim=1) / torch.clamp(
            correct_tool_mask[one_tool_mask].sum(dim=1), min=1
        )

        # two tool
        two_tool_pos_sample = (
            (pred_tool_mask[two_tool_mask] == correct_tool_mask[two_tool_mask])
            .all(dim=1)
            .long()
        )

        two_tool_precision = true_pos_mask[two_tool_mask].sum(dim=1) / torch.clamp(
            pred_tool_mask[two_tool_mask].sum(dim=1), min=1
        )

        two_tool_recall = true_pos_mask[two_tool_mask].sum(dim=1) / torch.clamp(
            correct_tool_mask[two_tool_mask].sum(dim=1), min=1
        )

        # other
        other_pos_sample = (
            (pred_tool_mask[other_mask] == correct_tool_mask[other_mask])
            .all(dim=1)
            .long()
        )

        other_precision = true_pos_mask[other_mask].sum(dim=1) / torch.clamp(
            pred_tool_mask[other_mask].sum(dim=1), min=1
        )

        other_recall = true_pos_mask[other_mask].sum(dim=1) / torch.clamp(
            correct_tool_mask[other_mask].sum(dim=1), min=1
        )

        if one_tool_pos_sample.sum().item() > 0:
            self.val_1_acc.update(
                one_tool_pos_sample, torch.ones_like(one_tool_pos_sample)
            )
            self.val_1_precision.update(one_tool_precision)
            self.val_1_recall.update(one_tool_recall)

            self.log(
                "val/1_acc",
                self.val_1_acc,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

            self.log(
                "val/1_precision",
                self.val_1_precision,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

            self.log(
                "val/1_recall",
                self.val_1_recall,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

        if two_tool_pos_sample.sum().item() > 0:
            self.val_2_acc.update(
                two_tool_pos_sample, torch.ones_like(two_tool_pos_sample)
            )
            self.val_2_precision.update(two_tool_precision)
            self.val_2_recall.update(two_tool_recall)

            self.log(
                "val/2_acc",
                self.val_2_acc,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

            self.log(
                "val/2_precision",
                self.val_2_precision,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

            self.log(
                "val/2_recall",
                self.val_2_recall,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

        if other_pos_sample.sum().item() > 0:
            self.val_other_acc.update(
                other_pos_sample, torch.ones_like(other_pos_sample)
            )
            self.val_other_precision.update(other_precision)
            self.val_other_recall.update(other_recall)

            self.log(
                "val/other_acc",
                self.val_other_acc,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

            self.log(
                "val/other_precision",
                self.val_other_precision,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

            self.log(
                "val/other_recall",
                self.val_other_recall,
                on_epoch=True,
                sync_dist=True,
                prog_bar=True,
            )

    def on_validation_epoch_end(self) -> None:
        pass

    def test_step(
        self, batch: Tuple[torch.Tensor, torch.Tensor], batch_idx: int
    ) -> None:
        pass

    def on_test_epoch_end(self) -> None:
        pass

    def configure_optimizers(self):
        opt = torch.optim.AdamW(
            [
                {"params": self.bert_model.parameters(), "lr": 1e-5},
                {
                    "params": list(self.inst_proj_model.parameters())
                    + list(self.tool_proj_model.parameters())
                    + list(self.pred_model.parameters()),
                    "lr": self.lr,
                },
            ],
            weight_decay=1e-4,
        )
        return opt