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

Introduce benchmark utilities #165

Merged
merged 7 commits into from
Apr 23, 2022
Merged

Introduce benchmark utilities #165

merged 7 commits into from
Apr 23, 2022

Conversation

vwxyzjn
Copy link
Owner

@vwxyzjn vwxyzjn commented Apr 18, 2022

Description

This PR introduces utilities that help us to run the benchmark experiments more smoothly.

Previously, we relied on a very simple mechanism for conducting benchmark experiments such as

for env_id in "HalfCheetah-v2" "Walker2d-v2" "Hopper-v2"; do
for seed in 1 2 3; do
poetry run python cleanrl/sac_continuous_action.py --track --capture-video --wandb-project-name cleanrl --wandb-entity openrlbenchmark --env-id $env_id --seed $seed
done
done

Such bash script usage is pretty straightforward but lacks flexibility and configurability. This PR introduce a command such as

OMP_NUM_THREADS=1 python -m cleanrl_utils.benchmark \
    --env-ids CartPole-v1 Acrobot-v1 MountainCar-v0 \
    --command "poetry run python cleanrl/ppo.py --track --cuda False" \
    --num-seeds 3 \
    --workers 3

which will automatically run experiments withworkers=3 subprocesses. A full example can be seen here:

# export WANDB_ENTITY=openrlbenchmark
# export WANDB_PROJECT=cleanrl

OMP_NUM_THREADS=1 python -m cleanrl_utils.benchmark \
    --env-ids CartPole-v1 Acrobot-v1 MountainCar-v0 \
    --command "poetry run python cleanrl/ppo.py --track --cuda False" \
    --num-seeds 3 \
    --workers 3

poetry install -E atari
python -m cleanrl_utils.benchmark \
    --env-ids PongNoFrameskip-v4 BeamRiderNoFrameskip-v4 BreakoutNoFrameskip-v4 \
    --command "poetry run python cleanrl/ppo_atari.py --track" \
    --num-seeds 3 \
    --workers 1

python -m cleanrl_utils.benchmark \
    --env-ids PongNoFrameskip-v4 BeamRiderNoFrameskip-v4 BreakoutNoFrameskip-v4 \
    --command "poetry run python cleanrl/ppo_atari_lstm.py --track" \
    --num-seeds 3 \
    --workers 1

python -m cleanrl_utils.benchmark \
    --env-ids PongNoFrameskip-v4 BeamRiderNoFrameskip-v4 BreakoutNoFrameskip-v4 \
    --command "poetry run python cleanrl/ppo_atari_envpool.py --track" \
    --num-seeds 3 \
    --workers 1

poetry install -E "mujoco pybullet"
python -c "import mujoco_py"
OMP_NUM_THREADS=1 python -m cleanrl_utils.benchmark \
    --env-ids HalfCheetah-v2 Walker2d-v2 Hopper-v2 \
    --command "poetry run python cleanrl/ppo_continuous_action.py --track --cuda False" \
    --num-seeds 3 \
    --workers 3

poetry install -E procgen
python -m cleanrl_utils.benchmark \
    --env-ids starpilot bossfight bigfish \
    --command "poetry run python cleanrl/ppo_procgen.py --track --track" \
    --num-seeds 3 \
    --workers 1

Types of changes

  • Bug fix
  • New feature
  • New algorithm
  • Documentation

Checklist:

  • I've read the CONTRIBUTION guide (required).
  • I have ensured pre-commit run --all-files passes (required).
  • I have updated the documentation and previewed the changes via mkdocs serve.

@vercel
Copy link

vercel bot commented Apr 18, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/vwxyzjn/cleanrl/9tPNxQNMcQfs1X6PgsoMTTb7YCW8
✅ Preview: https://cleanrl-git-benchmark-utilities-vwxyzjn.vercel.app

@gitpod-io
Copy link

gitpod-io bot commented Apr 18, 2022

Copy link
Collaborator

@dosssman dosssman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite useful addition. Will try to properly use it for the next benchmarks.

@vwxyzjn
Copy link
Owner Author

vwxyzjn commented Apr 20, 2022

Added more documentation. Going to merge this soon.

Comment on lines +1 to +45
import argparse
import shlex
import subprocess


def parse_args():
# fmt: off
parser = argparse.ArgumentParser()
parser.add_argument("--env-ids", nargs="+", default=["CartPole-v1", "Acrobot-v1", "MountainCar-v0"],
help="the ids of the environment to benchmark")
parser.add_argument("--command", type=str, default="poetry run python cleanrl/ppo.py",
help="the command to run")
parser.add_argument("--num-seeds", type=int, default=3,
help="the number of random seeds")
parser.add_argument('--workers', type=int, default=0,
help='the number of eval workers to run benchmark experimenets (skips evaluation when set to 0)')
args = parser.parse_args()
# fmt: on
return args


def run_experiment(command: str):
command_list = shlex.split(command)
print(f"running {command}")
fd = subprocess.Popen(command_list)
return_code = fd.wait()
assert return_code == 0


if __name__ == "__main__":
args = parse_args()
commands = []
for seed in range(1, args.num_seeds + 1):
for env_id in args.env_ids:
commands += [" ".join([args.command, "--env-id", env_id, "--seed", str(seed)])]

print(commands)

if args.workers > 0:
from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(max_workers=args.workers, thread_name_prefix="cleanrl-benchmark-worker-")
for command in commands:
executor.submit(run_experiment, command)
executor.shutdown(wait=True, cancel_futures=False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it simply spawns multiple processes, why don't we instead write a bash script?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m open to using a bash script. But the main benefit here is pythons code is a bit easier to read and also allows me to set a maximum number of workers. I don’t know if I can Set a maximum number of workers with bash.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think u can use poetry run python cleanrl/ppo.py & to spawn a new process and run it in the background.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. The issue is setting a maximum amount of workers. Imagine I have 60 commands to run, poetry run python cleanrl/ppo.py & is just gonna overflow my CPU.

docs/get-started/benchmark-utility.md Show resolved Hide resolved
@vwxyzjn
Copy link
Owner Author

vwxyzjn commented Apr 23, 2022

Merging now.

@vwxyzjn vwxyzjn merged commit 5184afc into master Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants