-
Notifications
You must be signed in to change notification settings - Fork 66
SCRF-1630-Modeler validation and sim_dict performance improvements #2980
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
Conversation
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.
1 file reviewed, no comments
|
Thanks for the efforts! Two main questions:
|
|
@momchil-flex mind sneaking in |
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.
Pull Request Overview
This PR optimizes TerminalComponentModeler performance by deferring validation and deep copying until necessary during simulation construction. The changes skip expensive operations during intermediate steps and validate only the final base_sim object. Grid size validation is moved from checking every simulation in sim_dict to a single check of base_sim.
- Adds
validate=Falseanddeep=Falseflags to intermediateupdated_copy()calls - Moves grid size validation from per-simulation to a single check on
base_sim - Replaces deprecated
.copy(update=...)with.updated_copy(**...)
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tidy3d/plugins/smatrix/component_modelers/terminal.py | Performance optimizations: skip validation/deep copying in intermediate simulation construction, validate only final base_sim; move grid size checks to run once |
| tests/test_plugins/test_array_factor.py | Update test to return modeler instead of sim_unit, with corresponding usage changes; comment out override_structures assertion |
| .github/workflows/tidy3d-python-client-tests.yml | Add "SCRF" to allowed JIRA project prefixes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
eddbe04 to
28a6bb5
Compare
So a bunch of the skipped validators are in copies that are done before we finally update the grid with the
I guess that's a good point. Normally we try for @dmarek-flex @weiliangjin2021 you should help me reason about this? Note that I had to fix one test which was checking for override structures in derived simulations. But now, such derived simulations have CustomGrid, and the original grid_spec is only in the modeler (and that one doesn't contain the override structures that were created due to ports, etc.) I could see how this could be problematic. For example, if I wanted to create a new modeler starting from one of these simulations, I have lost track of the original auto grid, and if I e.g. do @dmarek-flex brought up pydantic v2's Practically speaking though, for the current update, it is not clear to me whether we can proceed as proposed here + maybe adding an extra field for the grid spec from which the simulations were derived... or adding it to the attrs, but both seems like not ideal solutions. |
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
|
depending on how important this is we can also do this now, i don't think it's necessarily pydantic v2 related. we have similar cache invalidation mechanisms in place for the autograd traced fields what about having this in |
Yeah all things considered maybe this is worth a try? What about serializing it too? Do you think it's a relatively straightforward addition? |
it's possible.. for serialization we'd have to add some custom handling in maybe do we have to do this in the basemodel or can we keep this change a bit more localized? i'd probably prefer it if we could keep this mostly contained within the component modeler, but not sure how feasible, i'm not too familiar with the issue |
Maybe even the grid could be a cached_property of the |
So the original grid_spec is always stored in But what if you do I don't know how much we want to worry about this? Also this reminds me about something else we discussed yesterday: I think |
Ok, well I am not worried about it. Your second point is a good one though. I would say we should allow |
Haha ok then I won't worry about it either. Will you work on the modification for the second point? To @weiliangjin2021's original question
Is it sufficient that this remains in |
This seems to be caused by
This is a good idea, and it can be useful in many cases if the cached_property can be uploaded so that it doesn't need to be re-computed on the server. But one issue is that if one modifies their tidy3d code locally and generates wrong cached_property, the solver can be dealing with odd simulations. |
|
Cached properties are cleared if the model is updated. Well, our implementation specifically which will remain in v2 I assume @yaugenst-flex ? |
For now before pydantic v2 is integrated, maybe we can add a private field |
A private field would not be serialized. And in any case adding it as a simple field (rather than something like a cached property) definitely runs the risk that if the user updates the simulation without updating that field, it will be stale (which I guess is the same problem like if we directly set |
I started working on this but quickly found that it basically already works. The GridSpec can hold snapping points, mesh overrides even if the grid types along each dimension are Uniform. They are simply ignored. |
|
I think the test that I commented out could be pointing to a problematic use case?
Similar comment to @dmarek-flex 's comment above: yes override structures are allowed in UnifromGrid and CustomGrid, so things will not error e.g. when refining around ports, but they will also not actually get refined. Isn't this bad? |
My confusion was that
It's probably bad, but I don't want to completely disallow it. I will return to this, and add validator to check if the user has chosen refinement options for ports and whether the grid_spec contains any type of auto grid. Basically if the user really wants custom/uniform grid they need to be extremely explicit.
Array calculator is just duplicating simulations in an array, so I think we just need to change this test to either expect 0 overrides or use the base simulation to ensure mesh overrides are duplicated when present @dbochkov-flexcompute |
Yeah I don't really understand those assertions I had to comment out. I guess if we think this is safe for now though, what I could do is maybe just add the original grid_spec to the |
Sounds good to me |
28a6bb5 to
59d54ff
Compare
|
So I just added this one line, does it look good now @weiliangjin2021 @dmarek-flex ? |
Yeah, actually when implementing the array calculator I didn't take into account the possibility of CustomGrid. I think it's ok to comment out/remove those lines for now as we are planning to do a major refactor of array calculator anyway in the future |
weiliangjin2021
left a comment
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.
All look good!
|
Closing as changes are incorporated in #2978 |
Separated these updates in a standalone PR.
Greptile Overview
Updated On: 2025-11-10 18:14:34 UTC
Greptile Summary
This PR optimizes
TerminalComponentModelerperformance by deferring validation until necessary. The changes skip validation (validate=False) and deep copying (deep=False) during intermediate simulation construction steps, performing full validation only on the finalbase_simobject at line 540. Additionally, grid size validation was moved from checking every generated simulation insim_dictto a single check ofbase_simbefore simulation generation, reducing redundant checks.Key changes:
_sim_with_sourcesdocstring from "troubleshooting" to "plotting" for clarity.copy(update=...)method with.updated_copy(**...)validate=Falseanddeep=Falseflags to intermediateupdated_copy()callssim_dict()to run once onbase_siminstead of per-simulationGridSpec.from_grid()call inbase_simproperty before final validationConfidence Score: 4/5
Important Files Changed
File Analysis
base_sim. Grid size validation moved earlier to check once instead of per-simulation.Sequence Diagram
sequenceDiagram participant User participant TCM as TerminalComponentModeler participant BaseSim as _base_sim_no_radiation_monitors participant FinalSim as base_sim participant SimDict as sim_dict User->>TCM: Create modeler TCM->>BaseSim: Build intermediate simulation Note over BaseSim: updated_copy(validate=False, deep=False) BaseSim-->>TCM: Simulation without validation TCM->>FinalSim: Add radiation monitors Note over FinalSim: GridSpec.from_grid() + validate=True FinalSim-->>TCM: Fully validated base_sim User->>TCM: Access sim_dict property TCM->>FinalSim: Check grid size (once) Note over FinalSim: _check_grid_size_at_ports()<br/>_check_grid_size_at_wave_ports() loop For each network_index TCM->>SimDict: Create simulation with source Note over SimDict: updated_copy(validate=False, deep=False) SimDict-->>TCM: Simulation copy (no validation) end TCM-->>User: SimulationMap with all simulations