这是indexloc提供的服务,不要输入任何密码
Skip to content

Pickle compatible for replay buffer and improve buffer.get #182

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

Merged
merged 20 commits into from
Aug 16, 2020
Merged

Pickle compatible for replay buffer and improve buffer.get #182

merged 20 commits into from
Aug 16, 2020

Conversation

Trinkle23897
Copy link
Collaborator

@Trinkle23897 Trinkle23897 commented Aug 12, 2020

fix #84 and make buffer more efficient

@Trinkle23897 Trinkle23897 linked an issue Aug 12, 2020 that may be closed by this pull request
Co-authored-by: youkaichao <youkaichao@gmail.com>
youkaichao
youkaichao previously approved these changes Aug 12, 2020
@youkaichao
Copy link
Collaborator

Are you sure it is not working out-of-the-box ?

Do you guys have any tutorial on what exactly do pickle.dump and pickle.load do? I mean, in what order, call which function. I googled but found nothing satisfying. @Trinkle23897 @duburcqa

@duburcqa
Copy link
Collaborator

Hum strange, I found the online Python documentation about Pickle quite well written.

@youkaichao
Copy link
Collaborator

Hum strange, I found the online Python documentation about Pickle quite well written.

Oh yes, I didn't notice this section. After reading it, everything is crystal clear.

@Trinkle23897
Copy link
Collaborator Author

Trinkle23897 commented Aug 14, 2020

Okay, so far there are 2 solutions to resolve this issue. The main issue is: pickle.loads will not initialize the self._meta first. When calling setstate = getattr(inst, "__setstate__", None) in pickle.loads, it will recursively call buffer.__getattr__ since self._meta always fails to fetch.

The first is to write __getstate__ and __setstate__ because it will not call __getattr__ which will cause RecursionError;

The second is to add a if before self._meta[key]:

def __getattr__(self, key):
    if '_meta' not in self.__dict__:
        raise AttributeError
    try:
        return self._meta[key]
    except KeyError:
        return AttributeError

But this will cause another overhead, though it doesn't need to rewrite the set/getattr. What's your opinion @duburcqa @youkaichao

@youkaichao
Copy link
Collaborator

The first is to write getstate and setstate

I vote for this, the pythonic way to do it. Pickling buffer does not work out of box because the customized behavior of buffer.__getattr__.

@youkaichao
Copy link
Collaborator

I think this is actually a stupid behavior of python. Why the hell does it invoke __getattr__ during pickle and unpickle? Just provide a default __setstate__ and __getstate__ makes the day much easier 👎

@duburcqa
Copy link
Collaborator

I vote for this, the pythonic way to do it.

Why would it be more pythonic ?

I also think it is more logical to override the pickle interface, or at least part of it, since it more directly solve the problème your are facing.

@youkaichao
Copy link
Collaborator

By "pythonic", I mean, do what python wants us to do, since python leaves these interfaces to achieve pickle.

@duburcqa
Copy link
Collaborator

duburcqa commented Aug 14, 2020

By "pythonic", I mean, do what python wants us to do

I don't agree. Those interfaces have been designed to allow implementing much more complex behavior than one liners with default behavior except for a single attribute. So it is not clear to me if it is more appropriate to adapt the code to make the default work, or define custom interface, making pickle behavior less predictable for the user without reading the code.

Modifying the behavior of standard methods is almost always at last resort for me.

@youkaichao
Copy link
Collaborator

Okay, I withdraw the word "pythonic". Just because pickling buffer does not work out of box(buffer.__getattr__ is customized) so we need to use implement __setstate__ and __getstate__.

@youkaichao
Copy link
Collaborator

Btw, take a look at #180 when you are available 😄 @duburcqa

youkaichao
youkaichao previously approved these changes Aug 14, 2020
@Trinkle23897 Trinkle23897 changed the title Pickle compatible for replay buffer Pickle compatible for replay buffer and improve buffer.get Aug 15, 2020
youkaichao
youkaichao previously approved these changes Aug 15, 2020
@codecov-commenter
Copy link

codecov-commenter commented Aug 15, 2020

Codecov Report

Merging #182 into dev will increase coverage by 0.09%.
The diff coverage is 94.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #182      +/-   ##
==========================================
+ Coverage   89.16%   89.25%   +0.09%     
==========================================
  Files          39       39              
  Lines        2391     2402      +11     
==========================================
+ Hits         2132     2144      +12     
+ Misses        259      258       -1     
Flag Coverage Δ
#unittests 89.25% <94.87%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
tianshou/data/buffer.py 95.89% <94.73%> (+0.78%) ⬆️
tianshou/policy/base.py 95.94% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7f3b817...b289baf. Read the comment docs.

duburcqa
duburcqa previously approved these changes Aug 16, 2020
@youkaichao
Copy link
Collaborator

Nice to see you again! @duburcqa We are discussing about releasing version 0.3.0 after merging #182 and #179 :) It seems like a celebration for Tianshou having 2k stars (soon)!

@Trinkle23897 Trinkle23897 merged commit 311a2be into thu-ml:dev Aug 16, 2020
@Trinkle23897 Trinkle23897 deleted the buffer-save-load branch August 16, 2020 08:26
@duburcqa
Copy link
Collaborator

duburcqa commented Aug 16, 2020

Maybe it would be safe to wait one week or so before releasing it, just in case there is still a few bugs around. But I think the features are there.

BFAnas pushed a commit to BFAnas/tianshou that referenced this pull request May 5, 2024
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.

save/load data in replay buffer
4 participants