File size: 3,808 Bytes
2d13eb2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import unittest
import torch
from copy import deepcopy
from canonical import (
    cal_grad_z as canonical_cal_grad_z,
    cal_grad_batch_z as canonical_cal_grad_batch_z,
    hvp as canonical_hvp,
    estimate_hv as canonical_estimate_hv,
    get_influence_score as canonical_get_influence_score
)
from influence_score import (
    cal_grad_z as generated_cal_grad_z,
    cal_grad_batch_z as generated_cal_grad_batch_z,
    hvp as generated_hvp,
    estimate_hv as generated_estimate_hv,
    get_influence_score as generated_get_influence_score
)
from torch.utils.data import DataLoader, Dataset
import torch
import random
import numpy as np
def set_seed(seed):
    torch.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)

class Args:
    hidden_size = 8
    batch_size = 2
    iteration = 1
    recursion_depth = 3
    device = "cpu"
    cuda_condition = False


class DummyModel(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.dummy_weight = torch.nn.Parameter(torch.randn(8, 5))
        # self.dummy_weight = torch.nn.Parameter(torch.randn(10, 8))

    def forward(self, x):
        # Project input to match weight size and use matmul to create dependency
        x_proj = torch.nn.functional.linear(x, self.dummy_weight)
        return x_proj

    def named_parameters(self):
        return [("item_encoder.layer.1.attention.dense.weight", self.dummy_weight)]


class DummyTrainer:
    def __init__(self):
        set_seed(42)
        self.args = Args()
        self.model = DummyModel()
        self.train_dataset = [torch.rand(5) for _ in range(4)]
        self.train_dataloader = [[torch.rand(5) for _ in range(3)] for _ in range(2)]

    def get_sample_loss(self, idx):
        x = self.train_dataset[idx]
        pred = self.model(x.unsqueeze(0))
        return (pred ** 2).sum()

    def get_batch_loss(self, batch):
        pred = self.model(torch.stack(batch))
        return (pred ** 2).sum()


class InfluenceTestSuite(unittest.TestCase):
    def setUp(self):
        self.trainer = DummyTrainer()
        self.args = self.trainer.args

    def test_cal_grad_z(self):
        out1 = canonical_cal_grad_z(0, self.trainer)
        out2 = generated_cal_grad_z(0, self.trainer)
        self.assertEqual(len(out1), len(out2))
        for t1, t2 in zip(out1, out2):
            torch.testing.assert_close(t1, t2)

    def test_cal_grad_batch_z(self):
        batch = self.trainer.train_dataloader[0]
        out1 = canonical_cal_grad_batch_z(batch, self.trainer)
        out2 = generated_cal_grad_batch_z(batch, self.trainer)
        self.assertEqual(len(out1), len(out2))
        for t1, t2 in zip(out1, out2):
            torch.testing.assert_close(t1, t2)

    def test_hvp(self):
        loss = self.trainer.get_sample_loss(0)
        params = [p for n, p in self.trainer.model.named_parameters()]
        v = [torch.ones_like(p) for p in params]
        out1 = canonical_hvp(loss, params, v)
        out2 = generated_hvp(loss, params, v)
        for t1, t2 in zip(out1, out2):
            torch.testing.assert_close(t1, t2)

    def test_estimate_hv(self):
        params = [p for n, p in self.trainer.model.named_parameters()]
        v = [torch.ones_like(p) for p in params]
        h_est1 = deepcopy(v)
        h_est2 = deepcopy(v)
        out1 = canonical_estimate_hv(self.trainer, self.args, h_est1, v)
        out2 = generated_estimate_hv(self.trainer, self.args, h_est2, v)
        for t1, t2 in zip(out1, out2):
            torch.testing.assert_close(t1, t2)

    def test_get_influence_score(self):
        out1 = canonical_get_influence_score(self.args, self.trainer)
        out2 = generated_get_influence_score(self.args, self.trainer)
        torch.testing.assert_close(out1, out2)


if __name__ == "__main__":
    unittest.main()