Download annotations/annotation_20/test.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_20/test.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_20/test.py
-
curl -L -o test.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_20/test.py
3.81 kB
| 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() | |