Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix memory test #2081

Merged
merged 2 commits into from
Jul 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions smarts/core/tests/test_smarts_memory_growth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import logging

import gymnasium as gym
import numpy as np
import pytest
from pympler import muppy, summary, tracker

Expand All @@ -31,7 +32,7 @@
from smarts.zoo.agent_spec import AgentSpec

SMARTS_MEMORY_GROWTH_LIMIT = 2e5
EPISODE_MEMORY_GROWTH_LIMIT = 2e5
EPISODE_MEMORY_GROWTH_LIMIT = 8e5
TIMESTEP_SEC = 0.1
# Disable logging because it causes memory growth
logging.disable(logging.WARNING)
Expand Down Expand Up @@ -63,15 +64,30 @@ def seed():
@pytest.fixture(
params=[
# ( episodes, action, agent_type )
(100, None, AgentType.Buddha),
(100, (30, 1, -1), AgentType.Full),
(100, (), AgentType.Buddha),
(100, np.array((1, 1, -1), dtype=np.float32), AgentType.Full),
# (10, (30, 1, -1), AgentType.Standard), # standard is just full but less
(100, "keep_lane", AgentType.Laner),
(100, 2, AgentType.Laner),
# (100, (30, 1, -1), AgentType.Loner),
# (100, (30, 1, -1), AgentType.Tagger),
# (100, (30, 1, -1), AgentType.StandardWithAbsoluteSteering),
# (100, (50, 0), AgentType.LanerWithSpeed),
(100, ([1, 2, 3], [1, 2, 3], [0.5, 1, 1.5], [20, 20, 20]), AgentType.Tracker),
(
100,
(
np.array(
[
1,
2,
]
* 10
),
np.array([1, 2] * 10),
np.array([0.5, 1] * 10),
np.array([20, 20] * 10),
),
AgentType.Tracker,
),
# ( 5, ([0,1,2], [0,1,2], [0,1,2]), AgentType.MPCTracker),
]
)
Expand All @@ -81,7 +97,7 @@ def agent_params(request):

@pytest.fixture
def agent_type():
return ((30, 1, -1), AgentType.Full)
return (np.array((1, 1, -1), dtype=np.float32), AgentType.Full)


def env_and_spec(
Expand Down Expand Up @@ -119,7 +135,7 @@ def _env_memory_buildup(
gc.collect()


def _every_nth_episode(agent_id, episode_count, env_and_spec, steps_per_yield):
def _every_nth_episode(agent_id, episode_count, env_and_spec, episodes_per_yield):
env, agent_spec = env_and_spec

for episode_index in range(episode_count):
Expand All @@ -142,7 +158,7 @@ def _every_nth_episode(agent_id, episode_count, env_and_spec, steps_per_yield):
None,
None,
)
if episode_index % steps_per_yield == 0:
if episode_index % episodes_per_yield == 0:
yield episode_index


Expand Down Expand Up @@ -201,7 +217,7 @@ def test_smarts_episode_memory_cleanup(
):
MAX_EPISODE_STEPS = 100
EPISODE_COUNT = 100
STEPS_PER_YIELD = 10
EPISODES_PER_CHECK = 10

_, action, agent_type = agent_params

Expand All @@ -212,21 +228,25 @@ def test_smarts_episode_memory_cleanup(
size = 0
last_size = 0
gc.collect()
from pympler import refbrowser

tr = tracker.SummaryTracker()
try:
for current_episode in _every_nth_episode(
agent_id, EPISODE_COUNT, env_and_agent_spec, steps_per_yield=STEPS_PER_YIELD
agent_id,
EPISODE_COUNT,
env_and_agent_spec,
episodes_per_yield=EPISODES_PER_CHECK,
):
gc.collect()
all_objects = muppy.get_objects()
size = muppy.get_size(all_objects)
tr.print_diff(summary.summarize(all_objects))
print(flush=True)
all_objects = None
if current_episode > STEPS_PER_YIELD:
assert (
size - last_size < EPISODE_MEMORY_GROWTH_LIMIT
), f"End size delta {size - last_size}"
assert (
size - last_size < EPISODE_MEMORY_GROWTH_LIMIT
), f"End size delta `{size - last_size=}` at `{current_episode=}`"
last_size = size
finally:
env_and_agent_spec[0].close()
Expand Down Expand Up @@ -285,7 +305,7 @@ def test_smarts_fast_reset_memory_cleanup(agent_id, seed, social_agent_scenarios
seed,
social_agent_scenarios,
1,
None,
(),
agent_type,
max_episode_steps=2,
)
Expand All @@ -305,7 +325,9 @@ def test_smarts_social_agent_scenario_memory_cleanup(
agent_id, seed, social_agent_scenarios, agent_type
):
# Run once to initialize globals and test to see if smarts is working
_memory_buildup(agent_id, seed, social_agent_scenarios, 1, *agent_type)
_memory_buildup(
agent_id, seed, social_agent_scenarios, 1, agent_type[0], agent_type[1]
)

gc.collect()
initial_size = muppy.get_size(muppy.get_objects())
Expand Down