Skip to content

Conversation

@Schopenhauer-loves-Hegel
Copy link
Contributor

hello, I discovered that the road traffic scenario throws an error when running on CUDA due to inconsistent device usage in road_traffic.py, so I modified the file to fix this bug.

The bug can be reproduced with the following code:

import vmas  
import time  

def run_env_steps(device="cuda", num_steps=100):  
    env = vmas.make_env(  
        scenario="road_traffic",  
        num_envs=4,   
        device=device,  
        continuous_actions=True,  
        wrapper=None,  
        max_steps=None,  
        seed=None,  
        dict_spaces=False,  
        grad_enabled=False,  
        terminated_truncated=False,  
    )  

    start_time = time.time()  

    obs = env.reset()  
    for i in range(num_steps):  
        random_actions = env.get_random_actions()  
        obs, rewards, dones, info = env.step(random_actions)  

        if dones.any():  
            obs = env.reset()  

    end_time = time.time()  
    total_time = end_time - start_time  

    print(f"\nRunning {num_steps} steps on {device}:")  
    print(f"Total time: {total_time:.4f} seconds")  
    print(f"Average time per step: {(total_time/num_steps)*1000:.4f} ms")  

    return total_time  

# Test CPU vs. GPU performance  
print("Starting performance test...")  
cpu_time = run_env_steps(device="cpu", num_steps=100)  
gpu_time = run_env_steps(device="cuda", num_steps=100)  

print("\nPerformance comparison:")  
print(f"CPU total time: {cpu_time:.4f} seconds")  
print(f"GPU total time: {gpu_time:.4f} seconds")  
print(f"GPU speedup over CPU: {cpu_time/gpu_time:.2f}x") 

Copy link
Member

@matteobettini matteobettini left a comment

Choose a reason for hiding this comment

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

Thanks a mil for this!

@matteobettini
Copy link
Member

cc @Jianye-Xu

@matteobettini matteobettini merged commit 5faccc4 into proroklab:main Apr 24, 2025
4 checks passed
@Schopenhauer-loves-Hegel Schopenhauer-loves-Hegel deleted the fix_bug_rf_cuda branch April 24, 2025 08:59
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.

2 participants