-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Refactor and cleaning up demonstration file finding logic + associated test #3149
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
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
421215a
Refactor and cleaning up demonstration file finding logic + associate…
f2e886c
Fixed code formatting errors
9c54b82
Revert "Fixed code formatting errors"
b1ee06d
Fix code formatting changes (real)
83d02f4
Got rid of unnecessary import
06cace2
Mainly documentation changes and if-elif-else change
7a980c8
Merge pull request #1 from Unity-Technologies/master
kevtan bc5a837
Refactor map/filter with list comprehension
b28dd2f
Get rid of magic number
9bfcb75
More comprehensive get_file_names tests
33f60e5
Merge pull request #2 from Unity-Technologies/master
kevtan a28bf26
Decrease BrainInfo.from_agent_proto calls by half
de6a54c
Black reformatting
495ed30
Merge branch 'master' of https://github.com/kevtan/ml-agents
9bb766c
Merge pull request #3 from Unity-Technologies/master
kevtan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,10 +1,17 @@ | ||
| import os | ||
| import pytest | ||
| import tempfile | ||
|
|
||
| from mlagents.trainers.demo_loader import load_demonstration, demo_to_buffer | ||
| from mlagents.trainers.demo_loader import ( | ||
| load_demonstration, | ||
| demo_to_buffer, | ||
| get_demo_files, | ||
| ) | ||
|
|
||
| path_prefix = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
|
|
||
| def test_load_demo(): | ||
| path_prefix = os.path.dirname(os.path.abspath(__file__)) | ||
| brain_parameters, pair_infos, total_expected = load_demonstration( | ||
| path_prefix + "/test.demo" | ||
| ) | ||
|
|
@@ -17,7 +24,6 @@ def test_load_demo(): | |
|
|
||
|
|
||
| def test_load_demo_dir(): | ||
| path_prefix = os.path.dirname(os.path.abspath(__file__)) | ||
| brain_parameters, pair_infos, total_expected = load_demonstration( | ||
| path_prefix + "/test_demo_dir" | ||
| ) | ||
|
|
@@ -27,3 +33,28 @@ def test_load_demo_dir(): | |
|
|
||
| _, demo_buffer = demo_to_buffer(path_prefix + "/test_demo_dir", 1) | ||
| assert len(demo_buffer["actions"]) == total_expected - 1 | ||
|
|
||
|
|
||
| def test_edge_cases(): | ||
| # nonexistent file and directory | ||
| with pytest.raises(FileNotFoundError): | ||
| get_demo_files(os.path.join(path_prefix, "nonexistent_file.demo")) | ||
| with pytest.raises(FileNotFoundError): | ||
| get_demo_files(os.path.join(path_prefix, "nonexistent_directory")) | ||
| with tempfile.TemporaryDirectory() as tmpdirname: | ||
| # empty directory | ||
| with pytest.raises(ValueError): | ||
| get_demo_files(tmpdirname) | ||
| # invalid file | ||
| invalid_fname = tmpdirname + "/mydemo.notademo" | ||
| with open(invalid_fname, "w") as f: | ||
| f.write("I'm not a demo") | ||
| with pytest.raises(ValueError): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should also confirm that |
||
| get_demo_files(invalid_fname) | ||
| # valid file | ||
| valid_fname = tmpdirname + "/mydemo.demo" | ||
kevtan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| with open(valid_fname, "w") as f: | ||
| f.write("I'm a demo file") | ||
| assert get_demo_files(valid_fname) == [valid_fname] | ||
| # valid directory | ||
| assert get_demo_files(tmpdirname) == [valid_fname] | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little worried that this will eat up a lot of memory for large demo files, since it will decompress everything at once (in the original issue, demos were 1GB and that includes compressed visual observations). I think a better approach would be something like (very rough sketch):
Does that sounds reasonable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, great suggestion! This way, the entire demonstration file doesn't need to be in memory at once. I want to write a test for the demonstration loading that verifies that the loading is still being done correctly. My idea is to take the old implementation, load the demo files in
test_demo_dirwithdemo_to_bufferand then pickle the returned objects and save them somewhere as the "correct answer." Then, intest_demo_loader.py, load in the pickled objects and compare them against what the current implementation ofdemo_to_bufferis returning.The problem is that the
AgentBufferandBrainParametersclasses don't have defined__eq__methods yet. Any ideas? Should I go through with the test or not? If so, what should my next step be?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way I proposed still has the protobuf objects loaded all at once (this is the current behavior) but it at least doesn't increase the peak. Adding things to the buffer as soon as they're read from the file is another option, but could probably wait until another PR.
Instead of saving pickled data, how about writing new demonstration files and comparing the values? We don't currently have a python implementation of this, but I think it would be useful to have anyway.
You can see how this is done in an old gist that I wrote: https://gist.github.com/chriselion/3714d05255eea2f9132b96a182fbdcaa#file-convert_demo-py-L101-L115
(the structures being written might have changed a bit, but the overall format is the same).
So the test would be something like
Adding
__eq__to BrainParameters sounds fine if you think it would help. I'm not sure it's necessary for AgentBuffer since that inherits from dict (whether or not it should is another discussion).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I quite get what you mean. Every iteration through the for loop (in your proposed implementation), don't we lose an object reference? In particular, the object that
current_brain_inforeferred to beforecurrent_brain_infoandnext_brain_infogot updated. After the reference to that object is lost, wouldn't Python garbage collect that data, and, so not all the demonstration file is in memory at once?And, just so I'm clear, you're proposing to reconvert the loaded demonstration file contents back into a demonstration file (in a temporary directory) and then directly compare the file contents? I was just thinking about if we wanted to have tests for different
sequence_lengths for themake_demo_bufferfunction. Wouldn't this approach only work forsequence_length= 1?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay.
By "still has the protobuf objects loaded all at once", I meant the
pair_infoslist. If we wanted to reduce the total memory, we could combineload_demonstrationandmake_demo_bufferso that themake_demo_buffer()loop logic happens right afteragent_info_action.ParseFromString(). But I don't think we should worry about this for now; I just don't want to increase the peak usage, but we don't need to try to decrease it.For a test, I'm suggesting something like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A logistical note - there are some more changes to this surrounding code in the next week or so. To minimize the number of merge conflicts you're going to have to deal with, let's try to get the get_demo_files() and load_demonstration() changed merged, and come back to the make_demo_buffer / proto conversion change after.
Can you either undo the changes to make_demo_buffer(), or move them to another PR and I'll approve that? (the former is probably easier)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chriselion Sorry for the delay. School just started for me and it took a while to get myself settled in! I'll undo the changes now so that you can merge the
get_demo_files()andload_demonstration()changes!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries. Let me know if you want to keep working on this, or want to try something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chriselion I really want to complete this first assignment, and I'll try to do so by the end of this week! But, that being said, I'd really appreciate it if you could start to onboard me to the next thing I could be working on at the same time. Much appreciated!