Skip to content

Commit

Permalink
Optimize array stacking in the output by using NumPy rather than JAX.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 701895415
  • Loading branch information
sbodenstein authored and Torax team committed Dec 20, 2024
1 parent f6af816 commit 74e1940
Showing 1 changed file with 9 additions and 3 deletions.
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

0 comments on commit 74e1940

Please sign in to comment.