autodl-projects/tests/test_super_mlp.py

127 lines
4.8 KiB
Python
Raw Normal View History

2021-03-18 13:15:50 +01:00
#####################################################
# Copyright (c) Xuanyi Dong [GitHub D-X-Y], 2021.03 #
#####################################################
# pytest ./tests/test_super_model.py -s #
#####################################################
2021-05-19 07:00:33 +02:00
import torch
2021-03-18 13:15:50 +01:00
import unittest
2021-05-19 07:00:33 +02:00
from xautodl.xlayers import super_core
from xautodl import spaces
2021-03-18 13:15:50 +01:00
class TestSuperLinear(unittest.TestCase):
"""Test the super linear."""
def test_super_linear(self):
out_features = spaces.Categorical(12, 24, 36)
bias = spaces.Categorical(True, False)
model = super_core.SuperLinear(10, out_features, bias=bias)
2021-03-19 08:17:49 +01:00
print("The simple super linear module is:\n{:}".format(model))
2021-03-20 08:56:37 +01:00
model.apply_verbose(True)
2021-03-19 08:17:49 +01:00
2021-03-18 13:44:22 +01:00
print(model.super_run_type)
2021-03-19 08:17:49 +01:00
self.assertTrue(model.bias)
2021-03-19 16:57:23 +01:00
inputs = torch.rand(20, 10)
2021-03-19 08:17:49 +01:00
print("Input shape: {:}".format(inputs.shape))
print("Weight shape: {:}".format(model._super_weight.shape))
print("Bias shape: {:}".format(model._super_bias.shape))
outputs = model(inputs)
2021-03-19 16:57:23 +01:00
self.assertEqual(tuple(outputs.shape), (20, 36))
2021-03-19 08:17:49 +01:00
abstract_space = model.abstract_search_space
2021-03-19 16:57:23 +01:00
abstract_space.clean_last()
2021-03-19 08:17:49 +01:00
abstract_child = abstract_space.random()
print("The abstract searc space:\n{:}".format(abstract_space))
print("The abstract child program:\n{:}".format(abstract_child))
model.set_super_run_type(super_core.SuperRunMode.Candidate)
2021-03-19 16:57:23 +01:00
model.apply_candidate(abstract_child)
2021-03-19 08:17:49 +01:00
2021-03-19 16:57:23 +01:00
output_shape = (20, abstract_child["_out_features"].value)
2021-03-19 08:17:49 +01:00
outputs = model(inputs)
self.assertEqual(tuple(outputs.shape), output_shape)
2021-03-19 11:22:58 +01:00
2021-03-20 15:28:23 +01:00
def test_super_mlp_v1(self):
2021-03-19 11:22:58 +01:00
hidden_features = spaces.Categorical(12, 24, 36)
2021-03-19 16:57:23 +01:00
out_features = spaces.Categorical(24, 36, 48)
2021-03-20 15:28:23 +01:00
mlp = super_core.SuperMLPv1(10, hidden_features, out_features)
2021-03-19 11:22:58 +01:00
print(mlp)
2021-03-21 13:52:22 +01:00
mlp.apply_verbose(False)
2021-03-19 11:22:58 +01:00
self.assertTrue(mlp.fc1._out_features, mlp.fc2._in_features)
2021-03-19 16:57:23 +01:00
inputs = torch.rand(4, 10)
outputs = mlp(inputs)
self.assertEqual(tuple(outputs.shape), (4, 48))
2021-03-19 11:22:58 +01:00
abstract_space = mlp.abstract_search_space
2021-03-20 15:28:23 +01:00
print(
"The abstract search space for SuperMLPv1 is:\n{:}".format(abstract_space)
)
2021-03-19 11:22:58 +01:00
self.assertEqual(
abstract_space["fc1"]["_out_features"],
abstract_space["fc2"]["_in_features"],
)
self.assertTrue(
abstract_space["fc1"]["_out_features"]
is abstract_space["fc2"]["_in_features"]
)
2021-03-19 16:57:23 +01:00
abstract_space.clean_last()
2021-03-19 11:22:58 +01:00
abstract_child = abstract_space.random(reuse_last=True)
print("The abstract child program is:\n{:}".format(abstract_child))
self.assertEqual(
abstract_child["fc1"]["_out_features"].value,
abstract_child["fc2"]["_in_features"].value,
)
2021-03-19 16:57:23 +01:00
mlp.set_super_run_type(super_core.SuperRunMode.Candidate)
mlp.apply_candidate(abstract_child)
outputs = mlp(inputs)
output_shape = (4, abstract_child["fc2"]["_out_features"].value)
self.assertEqual(tuple(outputs.shape), output_shape)
2021-03-20 08:56:37 +01:00
2021-03-20 15:28:23 +01:00
def test_super_mlp_v2(self):
hidden_multiplier = spaces.Categorical(1.0, 2.0, 3.0)
out_features = spaces.Categorical(24, 36, 48)
mlp = super_core.SuperMLPv2(10, hidden_multiplier, out_features)
print(mlp)
2021-03-21 13:52:22 +01:00
mlp.apply_verbose(False)
2021-03-20 08:56:37 +01:00
2021-03-20 15:28:23 +01:00
inputs = torch.rand(4, 10)
outputs = mlp(inputs)
self.assertEqual(tuple(outputs.shape), (4, 48))
2021-03-20 08:56:37 +01:00
2021-03-20 15:28:23 +01:00
abstract_space = mlp.abstract_search_space
2021-03-20 08:56:37 +01:00
print(
2021-03-20 15:28:23 +01:00
"The abstract search space for SuperMLPv2 is:\n{:}".format(abstract_space)
2021-03-20 08:56:37 +01:00
)
2021-03-20 15:28:23 +01:00
2021-03-20 08:56:37 +01:00
abstract_space.clean_last()
abstract_child = abstract_space.random(reuse_last=True)
print("The abstract child program is:\n{:}".format(abstract_child))
2021-03-20 15:28:23 +01:00
mlp.set_super_run_type(super_core.SuperRunMode.Candidate)
mlp.apply_candidate(abstract_child)
outputs = mlp(inputs)
output_shape = (4, abstract_child["_out_features"].value)
2021-03-20 08:56:37 +01:00
self.assertEqual(tuple(outputs.shape), output_shape)
2021-03-21 13:52:22 +01:00
def test_super_stem(self):
out_features = spaces.Categorical(24, 36, 48)
model = super_core.SuperAlphaEBDv1(6, out_features)
inputs = torch.rand(4, 360)
abstract_space = model.abstract_search_space
abstract_space.clean_last()
abstract_child = abstract_space.random(reuse_last=True)
print("The abstract searc space:\n{:}".format(abstract_space))
print("The abstract child program:\n{:}".format(abstract_child))
model.set_super_run_type(super_core.SuperRunMode.Candidate)
model.apply_candidate(abstract_child)
outputs = model(inputs)
output_shape = (4, 60, abstract_child["_embed_dim"].value)
self.assertEqual(tuple(outputs.shape), output_shape)