-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(luyd): add model test code (#728)
* Fix test files * Add vac test and fix dt test * Add qtrain test and GTrXLDQN test * Fix ngu test * Add transformer_segment_wrapper test * Reformat
- Loading branch information
Showing
21 changed files
with
466 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import torch | ||
import pytest | ||
from itertools import product | ||
|
||
from ding.model.template import ACER | ||
from ding.torch_utils import is_differentiable | ||
|
||
B = 4 | ||
obs_shape = [4, (8, ), (4, 64, 64)] | ||
act_shape = [3, (6, )] | ||
args = list(product(*[obs_shape, act_shape])) | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestACER: | ||
|
||
@pytest.mark.parametrize('obs_shape, act_shape', args) | ||
def test_ACER(self, obs_shape, act_shape): | ||
if isinstance(obs_shape, int): | ||
inputs = torch.randn(B, obs_shape) | ||
else: | ||
inputs = torch.randn(B, *obs_shape) | ||
model = ACER(obs_shape, act_shape) | ||
|
||
outputs_c = model(inputs, mode='compute_critic') | ||
assert isinstance(outputs_c, dict) | ||
if isinstance(act_shape, int): | ||
assert outputs_c['q_value'].shape == (B, act_shape) | ||
elif len(act_shape) == 1: | ||
assert outputs_c['q_value'].shape == (B, *act_shape) | ||
|
||
outputs_a = model(inputs, mode='compute_actor') | ||
assert isinstance(outputs_a, dict) | ||
if isinstance(act_shape, int): | ||
assert outputs_a['logit'].shape == (B, act_shape) | ||
elif len(act_shape) == 1: | ||
assert outputs_a['logit'].shape == (B, *act_shape) | ||
|
||
outputs = {**outputs_a, **outputs_c} | ||
loss = sum([v.sum() for v in outputs.values()]) | ||
is_differentiable(loss, model) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import pytest | ||
from itertools import product | ||
import torch | ||
from ding.model.template import BCQ | ||
from ding.torch_utils import is_differentiable | ||
|
||
B = 4 | ||
obs_shape = [4, (8, )] | ||
act_shape = [3, (6, )] | ||
args = list(product(*[obs_shape, act_shape])) | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestBCQ: | ||
|
||
def output_check(self, model, outputs): | ||
if isinstance(outputs, torch.Tensor): | ||
loss = outputs.sum() | ||
elif isinstance(outputs, dict): | ||
loss = sum([v.sum() for v in outputs.values()]) | ||
is_differentiable(loss, model) | ||
|
||
@pytest.mark.parametrize('obs_shape, act_shape', args) | ||
def test_BCQ(self, obs_shape, act_shape): | ||
if isinstance(obs_shape, int): | ||
inputs_obs = torch.randn(B, obs_shape) | ||
else: | ||
inputs_obs = torch.randn(B, *obs_shape) | ||
if isinstance(act_shape, int): | ||
inputs_act = torch.randn(B, act_shape) | ||
else: | ||
inputs_act = torch.randn(B, *act_shape) | ||
inputs = {'obs': inputs_obs, 'action': inputs_act} | ||
model = BCQ(obs_shape, act_shape) | ||
|
||
outputs_c = model(inputs, mode='compute_critic') | ||
assert isinstance(outputs_c, dict) | ||
if isinstance(act_shape, int): | ||
assert torch.stack(outputs_c['q_value']).shape == (2, B) | ||
else: | ||
assert torch.stack(outputs_c['q_value']).shape == (2, B) | ||
self.output_check(model.critic, torch.stack(outputs_c['q_value'])) | ||
|
||
outputs_a = model(inputs, mode='compute_actor') | ||
assert isinstance(outputs_a, dict) | ||
if isinstance(act_shape, int): | ||
assert outputs_a['action'].shape == (B, act_shape) | ||
elif len(act_shape) == 1: | ||
assert outputs_a['action'].shape == (B, *act_shape) | ||
self.output_check(model.actor, outputs_a) | ||
|
||
outputs_vae = model(inputs, mode='compute_vae') | ||
assert isinstance(outputs_vae, dict) | ||
if isinstance(act_shape, int): | ||
assert outputs_vae['recons_action'].shape == (B, act_shape) | ||
assert outputs_vae['mu'].shape == (B, act_shape * 2) | ||
assert outputs_vae['log_var'].shape == (B, act_shape * 2) | ||
assert outputs_vae['z'].shape == (B, act_shape * 2) | ||
elif len(act_shape) == 1: | ||
assert outputs_vae['recons_action'].shape == (B, *act_shape) | ||
assert outputs_vae['mu'].shape == (B, act_shape[0] * 2) | ||
assert outputs_vae['log_var'].shape == (B, act_shape[0] * 2) | ||
assert outputs_vae['z'].shape == (B, act_shape[0] * 2) | ||
if isinstance(obs_shape, int): | ||
assert outputs_vae['prediction_residual'].shape == (B, obs_shape) | ||
else: | ||
assert outputs_vae['prediction_residual'].shape == (B, *obs_shape) | ||
|
||
outputs_eval = model(inputs, mode='compute_eval') | ||
assert isinstance(outputs_eval, dict) | ||
assert isinstance(outputs_eval, dict) | ||
if isinstance(act_shape, int): | ||
assert outputs_eval['action'].shape == (B, act_shape) | ||
elif len(act_shape) == 1: | ||
assert outputs_eval['action'].shape == (B, *act_shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import torch | ||
import pytest | ||
from itertools import product | ||
|
||
from ding.model.template import EDAC | ||
from ding.torch_utils import is_differentiable | ||
|
||
B = 4 | ||
obs_shape = [4, (8, )] | ||
act_shape = [3, (6, )] | ||
args = list(product(*[obs_shape, act_shape])) | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestEDAC: | ||
|
||
def output_check(self, model, outputs): | ||
if isinstance(outputs, torch.Tensor): | ||
loss = outputs.sum() | ||
elif isinstance(outputs, list): | ||
loss = sum([t.sum() for t in outputs]) | ||
elif isinstance(outputs, dict): | ||
loss = sum([v.sum() for v in outputs.values()]) | ||
is_differentiable(loss, model) | ||
|
||
@pytest.mark.parametrize('obs_shape, act_shape', args) | ||
def test_EDAC(self, obs_shape, act_shape): | ||
if isinstance(obs_shape, int): | ||
inputs_obs = torch.randn(B, obs_shape) | ||
else: | ||
inputs_obs = torch.randn(B, *obs_shape) | ||
if isinstance(act_shape, int): | ||
inputs_act = torch.randn(B, act_shape) | ||
else: | ||
inputs_act = torch.randn(B, *act_shape) | ||
inputs = {'obs': inputs_obs, 'action': inputs_act} | ||
model = EDAC(obs_shape, act_shape, ensemble_num=2) | ||
|
||
outputs_c = model(inputs, mode='compute_critic') | ||
assert isinstance(outputs_c, dict) | ||
assert outputs_c['q_value'].shape == (2, B) | ||
self.output_check(model.critic, outputs_c) | ||
|
||
if isinstance(obs_shape, int): | ||
inputs = torch.randn(B, obs_shape) | ||
else: | ||
inputs = torch.randn(B, *obs_shape) | ||
outputs_a = model(inputs, mode='compute_actor') | ||
assert isinstance(outputs_a, dict) | ||
if isinstance(act_shape, int): | ||
assert outputs_a['logit'][0].shape == (B, act_shape) | ||
assert outputs_a['logit'][1].shape == (B, act_shape) | ||
elif len(act_shape) == 1: | ||
assert outputs_a['logit'][0].shape == (B, *act_shape) | ||
assert outputs_a['logit'][1].shape == (B, *act_shape) | ||
outputs = {'mu': outputs_a['logit'][0], 'sigma': outputs_a['logit'][1]} | ||
self.output_check(model.actor, outputs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import pytest | ||
from itertools import product | ||
import torch | ||
from ding.model.template import NGU | ||
from ding.torch_utils import is_differentiable | ||
|
||
B = 4 | ||
H = 4 | ||
obs_shape = [4, (8, ), (4, 64, 64)] | ||
act_shape = [4, (4, )] | ||
args = list(product(*[obs_shape, act_shape])) | ||
|
||
|
||
@pytest.mark.unittest | ||
class TestNGU: | ||
|
||
def output_check(self, model, outputs): | ||
if isinstance(outputs, torch.Tensor): | ||
loss = outputs.sum() | ||
elif isinstance(outputs, list): | ||
loss = sum([t.sum() for t in outputs]) | ||
elif isinstance(outputs, dict): | ||
loss = sum([v.sum() for v in outputs.values()]) | ||
is_differentiable(loss, model) | ||
|
||
@pytest.mark.parametrize('obs_shape, act_shape', args) | ||
def test_ngu(self, obs_shape, act_shape): | ||
if isinstance(obs_shape, int): | ||
inputs_obs = torch.randn(B, H, obs_shape) | ||
else: | ||
inputs_obs = torch.randn(B, H, *obs_shape) | ||
if isinstance(act_shape, int): | ||
inputs_prev_action = torch.ones(B, act_shape).long() | ||
else: | ||
inputs_prev_action = torch.ones(B, *act_shape).long() | ||
inputs_prev_reward_extrinsic = torch.randn(B, H, 1) | ||
inputs_beta = 2 * torch.ones([4, 4], dtype=torch.long) | ||
inputs = { | ||
'obs': inputs_obs, | ||
'prev_state': None, | ||
'prev_action': inputs_prev_action, | ||
'prev_reward_extrinsic': inputs_prev_reward_extrinsic, | ||
'beta': inputs_beta | ||
} | ||
|
||
model = NGU(obs_shape, act_shape, collector_env_num=3) | ||
outputs = model(inputs) | ||
assert isinstance(outputs, dict) | ||
if isinstance(act_shape, int): | ||
assert outputs['logit'].shape == (B, act_shape, act_shape) | ||
elif len(act_shape) == 1: | ||
assert outputs['logit'].shape == (B, *act_shape, *act_shape) | ||
self.output_check(model, outputs['logit']) | ||
|
||
inputs = { | ||
'obs': inputs_obs, | ||
'prev_state': None, | ||
'action': inputs_prev_action, | ||
'reward': inputs_prev_reward_extrinsic, | ||
'prev_reward_extrinsic': inputs_prev_reward_extrinsic, | ||
'beta': inputs_beta | ||
} | ||
model = NGU(obs_shape, act_shape, collector_env_num=3) | ||
outputs = model(inputs) | ||
assert isinstance(outputs, dict) | ||
if isinstance(act_shape, int): | ||
assert outputs['logit'].shape == (B, act_shape, act_shape) | ||
elif len(act_shape) == 1: | ||
assert outputs['logit'].shape == (B, *act_shape, *act_shape) | ||
self.output_check(model, outputs['logit']) |
Oops, something went wrong.