Skip to content

Lazy importing of agents - #247

Merged
jonbinney merged 4 commits into
mainfrom
jdb/lazy-agent-import
Jul 8, 2025
Merged

Lazy importing of agents#247
jonbinney merged 4 commits into
mainfrom
jdb/lazy-agent-import

Conversation

@jonbinney

Copy link
Copy Markdown
Owner

Instead of importing and registering the class for each agent in agents/__init__.py, we now just register the name of the class and the name of the module it is in. The AgentRegistry dynamically imports the module for each agent as needed when they are created in AgentRegisry.create_from_encoded_name(...). On my computer if I just want to train alphazero, this reduces the startup/import time from ~4 seconds to ~2 seconds.

I tested by running our benchmarks and tests, and running train.py with just alphazero. Let me know if I missed any scripts that I might have broken and should test.

There are some fancier approaches to this that would involve using python's lazy import mechanism and some wrapper, but that felt like more magic than we needed.

if value == "player_0":
value = 0
else:
value = 1

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I fixed this just so I could run our benchmark



# Register the agent with the registry
AgentRegistry.register(Agent._friendly_name(SB3PPOAgent.__name__), SB3PPOAgent)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

I don't think this was ever needed, since the agent is registered in agents.__init__

@adamantivm

Copy link
Copy Markdown
Collaborator

LGTM

@alejandromarcu alejandromarcu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, I don't think we need anything fancier, this is a nice approach

Comment thread deep_quoridor/src/agents/core/agent.py Outdated
Comment thread deep_quoridor/src/agents/core/agent.py
@jonbinney
jonbinney merged commit 8a3be86 into main Jul 8, 2025
1 check passed
@jonbinney
jonbinney deleted the jdb/lazy-agent-import branch February 3, 2026 14:52
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.

3 participants