Download annotations/annotation_36/test.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 7.49 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_36/test.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_36/test.py
-
curl -L -o test.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_36/test.py
7.49 kB
| import unittest | |
| import torch | |
| import random | |
| import numpy as np | |
| from model.pytorch.wukong import LinearCompressBlock, FactorizationMachineBlock, ResidualProjection | |
| # Import canonical and generated modules/classes | |
| from model.pytorch.canonical import Wukong as CanonicalWukong, WukongLayer as CanonicalWukongLayer | |
| from model.pytorch.wukong import Wukong as GeneratedWukong, WukongLayer as GeneratedWukongLayer | |
| def set_seeds(seed=42): | |
| random.seed(seed) | |
| np.random.seed(seed) | |
| torch.manual_seed(seed) | |
| class TestWukongFunctionalEquivalence(unittest.TestCase): | |
| def setUpClass(cls): | |
| set_seeds(123) | |
| # Minimal hyperparameters for fast/efficient testing | |
| cls.batch_size = 2 | |
| cls.num_sparse_emb = 10 | |
| cls.num_emb_in = 4 | |
| cls.num_emb_lcb = 2 | |
| cls.num_emb_fmb = 2 | |
| cls.rank_fmb = 2 | |
| cls.dim_emb = 3 | |
| cls.num_hidden = 1 | |
| cls.dim_hidden = 4 | |
| cls.dropout = 0.0 | |
| # For Wukong model | |
| cls.num_layers = 2 | |
| cls.dim_input_sparse = 2 | |
| cls.dim_input_dense = 2 | |
| cls.num_hidden_head = 1 | |
| cls.dim_hidden_head = 4 | |
| cls.dim_output = 1 | |
| # Toy input | |
| cls.sparse_inputs = torch.randint(0, cls.num_sparse_emb, (cls.batch_size, cls.dim_input_sparse)) | |
| cls.dense_inputs = torch.randn(cls.batch_size, cls.dim_input_dense) | |
| cls.layer_inputs = torch.randn(cls.batch_size, cls.num_emb_in, cls.dim_emb) | |
| def test_wukonglayer_init_equivalence(self): | |
| # Test that the class names are the same (not strict Python type equality) | |
| can_layer = CanonicalWukongLayer( | |
| num_emb_in=self.num_emb_in, | |
| dim_emb=self.dim_emb, | |
| num_emb_lcb=self.num_emb_lcb, | |
| num_emb_fmb=self.num_emb_fmb, | |
| rank_fmb=self.rank_fmb, | |
| num_hidden=self.num_hidden, | |
| dim_hidden=self.dim_hidden, | |
| dropout=self.dropout | |
| ) | |
| gen_layer = GeneratedWukongLayer( | |
| num_emb_in=self.num_emb_in, | |
| dim_emb=self.dim_emb, | |
| num_emb_lcb=self.num_emb_lcb, | |
| num_emb_fmb=self.num_emb_fmb, | |
| rank_fmb=self.rank_fmb, | |
| num_hidden=self.num_hidden, | |
| dim_hidden=self.dim_hidden, | |
| dropout=self.dropout | |
| ) | |
| self.assertEqual( | |
| gen_layer.__class__.__name__, can_layer.__class__.__name__, | |
| "Generated and canonical layer have different class names." | |
| ) | |
| def test_wukonglayer_forward_equivalence(self): | |
| set_seeds(321) | |
| can_layer = CanonicalWukongLayer( | |
| num_emb_in=self.num_emb_in, | |
| dim_emb=self.dim_emb, | |
| num_emb_lcb=self.num_emb_lcb, | |
| num_emb_fmb=self.num_emb_fmb, | |
| rank_fmb=self.rank_fmb, | |
| num_hidden=self.num_hidden, | |
| dim_hidden=self.dim_hidden, | |
| dropout=self.dropout | |
| ) | |
| gen_layer = GeneratedWukongLayer( | |
| num_emb_in=self.num_emb_in, | |
| dim_emb=self.dim_emb, | |
| num_emb_lcb=self.num_emb_lcb, | |
| num_emb_fmb=self.num_emb_fmb, | |
| rank_fmb=self.rank_fmb, | |
| num_hidden=self.num_hidden, | |
| dim_hidden=self.dim_hidden, | |
| dropout=self.dropout | |
| ) | |
| can_out = can_layer(self.layer_inputs) | |
| gen_out = gen_layer(self.layer_inputs) | |
| # Check type, shape, and value (tolerant, since weights init may differ) | |
| self.assertIsInstance(gen_out, type(can_out)) | |
| self.assertEqual(can_out.shape, gen_out.shape) | |
| self.assertTrue( | |
| torch.allclose(can_out, gen_out, rtol=1e-4, atol=1e-6) or | |
| (not torch.any(torch.isnan(can_out)) and not torch.any(torch.isnan(gen_out))), | |
| "Outputs are not sufficiently close or valid" | |
| ) | |
| def test_wukong_init_equivalence(self): | |
| can_model = CanonicalWukong( | |
| num_layers=self.num_layers, | |
| num_sparse_emb=self.num_sparse_emb, | |
| dim_emb=self.dim_emb, | |
| dim_input_sparse=self.dim_input_sparse, | |
| dim_input_dense=self.dim_input_dense, | |
| num_emb_lcb=self.num_emb_lcb, | |
| num_emb_fmb=self.num_emb_fmb, | |
| rank_fmb=self.rank_fmb, | |
| num_hidden_wukong=self.num_hidden, | |
| dim_hidden_wukong=self.dim_hidden, | |
| num_hidden_head=self.num_hidden_head, | |
| dim_hidden_head=self.dim_hidden_head, | |
| dim_output=self.dim_output, | |
| dropout=self.dropout | |
| ) | |
| gen_model = GeneratedWukong( | |
| num_layers=self.num_layers, | |
| num_sparse_emb=self.num_sparse_emb, | |
| dim_emb=self.dim_emb, | |
| dim_input_sparse=self.dim_input_sparse, | |
| dim_input_dense=self.dim_input_dense, | |
| num_emb_lcb=self.num_emb_lcb, | |
| num_emb_fmb=self.num_emb_fmb, | |
| rank_fmb=self.rank_fmb, | |
| num_hidden_wukong=self.num_hidden, | |
| dim_hidden_wukong=self.dim_hidden, | |
| num_hidden_head=self.num_hidden_head, | |
| dim_hidden_head=self.dim_hidden_head, | |
| dim_output=self.dim_output, | |
| dropout=self.dropout | |
| ) | |
| self.assertEqual( | |
| gen_model.__class__.__name__, can_model.__class__.__name__, | |
| "Generated and canonical model have different class names." | |
| ) | |
| def test_wukong_forward_equivalence(self): | |
| set_seeds(456) | |
| can_model = CanonicalWukong( | |
| num_layers=self.num_layers, | |
| num_sparse_emb=self.num_sparse_emb, | |
| dim_emb=self.dim_emb, | |
| dim_input_sparse=self.dim_input_sparse, | |
| dim_input_dense=self.dim_input_dense, | |
| num_emb_lcb=self.num_emb_lcb, | |
| num_emb_fmb=self.num_emb_fmb, | |
| rank_fmb=self.rank_fmb, | |
| num_hidden_wukong=self.num_hidden, | |
| dim_hidden_wukong=self.dim_hidden, | |
| num_hidden_head=self.num_hidden_head, | |
| dim_hidden_head=self.dim_hidden_head, | |
| dim_output=self.dim_output, | |
| dropout=self.dropout | |
| ) | |
| gen_model = GeneratedWukong( | |
| num_layers=self.num_layers, | |
| num_sparse_emb=self.num_sparse_emb, | |
| dim_emb=self.dim_emb, | |
| dim_input_sparse=self.dim_input_sparse, | |
| dim_input_dense=self.dim_input_dense, | |
| num_emb_lcb=self.num_emb_lcb, | |
| num_emb_fmb=self.num_emb_fmb, | |
| rank_fmb=self.rank_fmb, | |
| num_hidden_wukong=self.num_hidden, | |
| dim_hidden_wukong=self.dim_hidden, | |
| num_hidden_head=self.num_hidden_head, | |
| dim_hidden_head=self.dim_hidden_head, | |
| dim_output=self.dim_output, | |
| dropout=self.dropout | |
| ) | |
| can_out = can_model(self.sparse_inputs, self.dense_inputs) | |
| gen_out = gen_model(self.sparse_inputs, self.dense_inputs) | |
| self.assertIsInstance(gen_out, type(can_out)) | |
| self.assertEqual(can_out.shape, gen_out.shape) | |
| self.assertTrue( | |
| torch.allclose(can_out, gen_out, rtol=1e-4, atol=1e-6) or | |
| (not torch.any(torch.isnan(can_out)) and not torch.any(torch.isnan(gen_out))), | |
| "Outputs are not sufficiently close or valid" | |
| ) | |
| if __name__ == '__main__': | |
| unittest.main() | |