Skip to content
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

RuntimeError: dictionary changed size during iteration? #45

Open
animitta opened this issue Jan 29, 2024 · 2 comments
Open

RuntimeError: dictionary changed size during iteration? #45

animitta opened this issue Jan 29, 2024 · 2 comments

Comments

@animitta
Copy link

animitta commented Jan 29, 2024

pytorch ddp mode, dataloader wrapper batch data by EasyDict.
line 120, in init for k in self.class.dict.keys():

        # Class attributes
        for k in self.__class__.__dict__.keys():
            if not (k.startswith('__') and k.endswith('__')) and not k in ('update', 'pop'):
                setattr(self, k, getattr(self, k))
```python
@boray-tw
Copy link

boray-tw commented Mar 24, 2025

Please provide a minimal reproduceable code with PyTorch and easydict package versions, or people cannot understand your problem, nor do answer.

I suspect this issue relates to the Python 3 implementation of dict.keys().

In Python 3, dict.keys() returns a view object, while in Python 2 it returns a copy of keys. A view object is similar to a real-time dynamic view of the original object (dictionary in this case). It is an undefined behavior to iterate over a view object (iterator) modified in the flight, and thus causes an RuntimeError.

To enforce the Python 2 behavior, you need to either convert the view object to a list list(dict.keys()) or modify a copy of the original dictionary like this (see the last code block).

Suppose that your attached code is the EasyDict initializer. Could you modify the line 143 from for k in self.__class__.__dict__.keys(): to for k in list(self.__class__.__dict__.keys()):, and see if the error persists?

If no error, could you open a pull request concerning this improvement, as indicated in this PyTorch EasyDict support issue?

@animitta
Copy link
Author

OK, I will testing.

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

No branches or pull requests

2 participants