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

Optimize array stacking in the output by using NumPy rather than JAX. #586

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions torax/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@
# limitations under the License.

"""Module containing functions for saving and loading simulation output."""

from __future__ import annotations

import dataclasses

from absl import logging
import chex
import jax
from jax import numpy as jnp
import numpy as np

from torax import state
from torax.config import runtime_params
from torax.geometry import geometry
Expand Down Expand Up @@ -200,7 +202,11 @@ def __init__(
post_processed_output = [
state.post_processed_outputs for state in sim_outputs.sim_history
]
stack = lambda *ys: jnp.stack(ys)

def stack(*x):
out = np.stack([np.asarray(i) for i in x])
return out

self.core_profiles: state.CoreProfiles = jax.tree_util.tree_map(
stack, *core_profiles
)
Expand All @@ -213,7 +219,7 @@ def __init__(
self.post_processed_outputs: state.PostProcessedOutputs = (
jax.tree_util.tree_map(stack, *post_processed_output)
)
self.times = jnp.array([state.t for state in sim_outputs.sim_history])
self.times = np.array([state.t for state in sim_outputs.sim_history])
chex.assert_rank(self.times, 1)
self.sim_error = sim_outputs.sim_error
self.source_models = source_models
Expand Down
3 changes: 2 additions & 1 deletion torax/simulation_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def run(_):
from typing import Callable, Final

from absl import logging
import chex
import jax
from torax import output
from torax import sim as sim_lib
Expand Down Expand Up @@ -141,7 +142,7 @@ def _log_single_state(
def log_simulation_output_to_stdout(
core_profile_history: state.CoreProfiles,
geo: geometry.Geometry,
t: jax.Array,
t: chex.Array,
) -> None:
del geo
_log_single_state(core_profile_history.index(0), t[0])
Expand Down
Loading