Skip to content

Commit

Permalink
fix: remove autograd temporary simulation files
Browse files Browse the repository at this point in the history
  • Loading branch information
yaugenst-flex committed Oct 23, 2024
1 parent 29bafce commit 9f6f5ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- Resolved an issue where temporary files for autograd simulations with `local_gradient=False` were not being deleted properly.

## [2.7.5] - 2024-10-16

Expand Down
33 changes: 20 additions & 13 deletions tidy3d/web/api/autograd/autograd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# autograd wrapper for web functions

import os
import tempfile
import typing
from collections import defaultdict
Expand Down Expand Up @@ -529,26 +530,32 @@ def postprocess_fwd(

def upload_sim_fields_keys(sim_fields_keys: list[tuple], task_id: str, verbose: bool = False):
"""Function to grab the VJP result for the simulation fields from the adjoint task ID."""
data_file = tempfile.NamedTemporaryFile(suffix=".hdf5")
data_file.close()
TracerKeys(keys=sim_fields_keys).to_file(data_file.name)
upload_file(
task_id,
data_file.name,
SIM_FIELDS_KEYS_FILE,
verbose=verbose,
)
handle, fname = tempfile.mkstemp(suffix=".hdf5")
os.close(handle)
try:
TracerKeys(keys=sim_fields_keys).to_file(fname)
upload_file(
task_id,
fname,
SIM_FIELDS_KEYS_FILE,
verbose=verbose,
)
finally:
os.unlink(fname)


""" VJP maker for ADJ pass."""


def get_vjp_traced_fields(task_id_adj: str, verbose: bool) -> AutogradFieldMap:
"""Function to grab the VJP result for the simulation fields from the adjoint task ID."""
data_file = tempfile.NamedTemporaryFile(suffix=".hdf5")
data_file.close()
download_file(task_id_adj, SIM_VJP_FILE, to_file=data_file.name, verbose=verbose)
field_map = FieldMap.from_file(data_file.name)
handle, fname = tempfile.mkstemp(suffix=".hdf5")
os.close(handle)
try:
download_file(task_id_adj, SIM_VJP_FILE, to_file=fname, verbose=verbose)
field_map = FieldMap.from_file(fname)
finally:
os.unlink(fname)
return field_map.to_autograd_field_map


Expand Down

0 comments on commit 9f6f5ae

Please sign in to comment.