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
Why custom ringbuffer used instead of deque? #165
Comments
Was this benchmark done using Python 3 or Python 2.7? I remember that I had trouble with deque and Python 2.7 in the past, but I may be wrong. |
It was with python 2.7, with python 3 I get this: Here the deque is a bit slower but still much faster then the ring buffer. |
Could you do a benchmark with truly random access? It seems like you go through the buffer in sequential order. Instead, try something where you randomly draw indices. If this test turns out to show the same trend, feel free to remove the custom |
with this code:
I get this for python2.7: and for python 3: Indeed the random access in Python2.7 is about half the speed of sequential access but still faster than the ring buffer. |
Cool, feel free to update the code to use |
@matthiasplappert, @kirkscheper Please let me know if anyone is working on this. I will be glad to finish this as soon as possible. |
@Luffy1996 Feel free to work on this, I don't think anyone it tackling this issue at the moment! |
@RaphaelMeudec Give me a week. My semester exams are going on. I would love to work on this from next week. |
No pressure, good luck on your exams! |
hey @kirkscheper ! I'm interested in the deque implementation, do you have any progress from above? |
All my changes are here: https://github.com/kirkscheper/keras-rl/tree/recurrent-ddpg I have been playing around with lots of different things that are probably not directly ready for a pull request. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
the comments above, with performance testing are correct, deque is faster, for appends. but the slowdown comes with random.sample(dq,batch_size), when you are sampling a large buffer. the implementation given for RingBuffer (via SequentialMemory), in both python 2.7 and 3+ is about 10x faster for Sampling. my performance runs for sampling( size 1,000,000) were: so... use the RingBuffer(SequentialMemory class), it samples way faster, and is on about the same magnitude for append(). |
Ah yes, I remember that this was the reason for creating this custom implementation since fast random sampling is very important. Thanks for looking into it. |
@sperazza I did an experiment as you said, but I got an opposite result.
And I got this for RingBuffer:
I got this: |
@UPUPGOO if you read the Lines 45 to 83 in e6efb0d
and the SequentialMemory classLines 158 to 195 in e6efb0d
Note that the RingBuffer has been updated using a deque, and SequentialMemory is using RingBuffer, so which means both are using deques.
Now if you read the code from this function - Lines 14 to 42 in e6efb0d
You will notice that now the code has been updated to always sampling from a list, so you were not doing the same deque/list sampling comparison as @sperazza did up there. |
https://github.com/matthiasplappert/keras-rl/blob/d9e3b64a20f056030c02bfe217085b7e54098e48/rl/memory.py#L42 states:
Do not use deque to implement the memory. This data structure may seem convenient but it is way too slow on random access. Instead, we use our own ring buffer implementation.
Not sure why this is stated. I ran this quick test:
`
ring = RingBuffer(maxlen = 1000)
and got this result:
--- 0.0608789920807 seconds ---
--- 0.00712609291077 seconds ---
--- 0.0430929660797 seconds ---
--- 0.00617289543152 seconds ---
`
You can see that the custom ringbuffer is significantly slower than the deque at adding or removing. Can someone explain to me what the comment is referring to exactly and why the ringbuffer is used?
The text was updated successfully, but these errors were encountered: