-
Notifications
You must be signed in to change notification settings - Fork 50
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
Fix simulation file overwriting caused by same file name of forward and adjoint simulations #1989
Conversation
Hi @txdai, good catch and thanks for submitting a fix, this is great! I guess you were referring to #1965? |
tidy3d/web/api/autograd/autograd.py
Outdated
path_parts = path.rsplit('.', 1) | ||
path = path_parts[0] + "_adjoint." + path_parts[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.
For clarity I'd prefer using os.path.splitext
here, i.e.:
import os
...
name, ext = os.path.splitext(path)
path = name + "_adjoint" + ext
Functionally identical, but a bit easier to tell what's going on I think.
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.
Sure this would also work, I was just reluctant to import another library
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.
Python stdlib is fine :)
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.
Thanks @txdai . We just had another discussion about this and to be very pedantic (although throwing away readability...) we reached this conclusion:
from os.path import basename, dirname, join
...
if task_name.endswith("_adjoint"):
path_parts = basename(path).split('.')
path = join(dirname(path), path_parts[0] + "_adjoint." + ".".join(path_parts[1:]))
This is because we might have extensions like .hdf5.gz
in principle. Not currently happening but to make this a bit more future-proof.
Do you want to modify your PR with this so we can merge it?
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.
oh and could you please make sure to run ruff
on the PR once before committing, the tests are currently failing due to a lint error. if you use poetry, you can just poetry run pre-commit install
on the repo, it'll run the linter automatically before the commit
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 reformatted the code and changed to the suggested implementation. Thanks!
Yes this is the expected behavior. If running local_gradient=True, the server doesn't need to do anything special to handle fwd and adj simulations, they are just wrapped and handled by the front end. However with local_gradient=False, we need to know whether this was a forward or adjoint task to cache fields and do the gradient calculation, respectively |
Thanks - looks good now! I just need to ask one more thing, sorry - could you squash your two commits into 1? E.g. |
Following the bug fix in 2.7.4 for autograd, whenever a adjoint simulation is ran, it would have the same file name as the forward simulation file, causing overwrite. In more detail, currently when this function is called, the if statement in line 886 is never called because for the forward and adjoint simulation, its types are always "'simulation_type': ‘tidy3d_autograd’’, and the only way to differentiate between them is by using the “_adjoint” postfix in task_name. I propose this fix here to add the postfix to the file name as well
Following the bug fix in 2.7.4 for autograd, whenever a adjoint simulation is ran, it would have the same file name as the forward simulation file, causing overwrite. This is because both the forward and adjoint simulation calls this function with the same arguments
In more detail, currently when this function is called, the if statement in line 886 is never called because for the forward and adjoint simulation, its types are always "'simulation_type': ‘tidy3d_autograd’’, and the only way to differentiate between them is by using the “_adjoint” postfix in task_name.
I propose this fix here to add the postfix to the file name as well, to avoid the overwriting while being as less invasive as possible.