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

Common settings for fastmath, sharrow_skip, and other compute controls across components #824

Merged
merged 25 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
caf7d23
prevent crash when sharrow-test fails in interaction-simulate
jpn-- Feb 29, 2024
a5aeeac
Merge branch 'main' into fix-escort-sharrow
jpn-- Mar 18, 2024
1dc8959
sharrow_fastmath
jpn-- Mar 18, 2024
b69fae9
activate sharrow without fastmath in example
jpn-- Mar 18, 2024
4e80c04
Merge branch 'ActivitySim:main' into fix-escort-sharrow
jpn-- Mar 27, 2024
28c15a4
change fastmath default to F for escort
jpn-- Mar 27, 2024
95f404e
Merge commit 'f15486c8794dbac64f3915bde3fe9a9a49cd5ac9' into fix-esco…
jpn-- Mar 29, 2024
6f4b25f
skip and fastmath in sharrow_settings
jpn-- Mar 31, 2024
c0f5111
fix deprecation handling
jpn-- Mar 31, 2024
6a2aaf4
fix fastmath setting on escort configs
jpn-- Apr 1, 2024
be88bcf
pin pandera on other envs
jpn-- Apr 1, 2024
f659965
remove unused LOGSUM_CHOOSER_COLUMNS
jpn-- Apr 1, 2024
4183e61
Merge branch 'main' into fix-escort-sharrow
jpn-- Apr 1, 2024
62d80f2
Merge commit '5111c79c1ce8b9f9b05b36ddabc30bb764c3e47c' into fix-esco…
jpn-- Apr 3, 2024
38fb23a
Merge commit 'bb437b345a9270ac050a23c1e53048b01ec7e5a2' into fix-esco…
jpn-- Apr 3, 2024
396e618
fix sharrow skip in simple simulate
jpn-- Apr 4, 2024
35810f9
fix regressions
jpn-- Apr 4, 2024
61bf29d
Merge commit '739c2e428856eb401b42e95cedf9fd88bfcb02c6' into fix-esco…
jpn-- Apr 4, 2024
e314fa1
sharrow_settings in cdap
jpn-- Apr 5, 2024
d301f2f
add missing sharrow_settings
jpn-- Apr 15, 2024
a9679e6
pandas compute options
jpn-- Apr 17, 2024
4a13781
sharrow_settings -> compute_settings
jpn-- Apr 17, 2024
59bb675
fix transit-pass-subsidy model for compute_settings
jpn-- Apr 18, 2024
2dd1420
strip outdated code
jpn-- Apr 18, 2024
13f950b
change class name to PydanticCompute for clarity
jpn-- Apr 18, 2024
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
1 change: 1 addition & 0 deletions activitysim/abm/models/atwork_subtour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def atwork_subtour_frequency(
trace_label=trace_label,
trace_choice_name="atwork_subtour_frequency",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

# convert indexes to alternative names
Expand Down
4 changes: 3 additions & 1 deletion activitysim/abm/models/auto_ownership.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@

from activitysim.core import (
config,
expressions,
estimation,
expressions,
simulate,
tracing,
workflow,
)
from activitysim.core.configuration.base import PreprocessorSettings, PydanticReadable
from activitysim.core.configuration.logit import LogitComponentSettings

from .util import annotate

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -99,6 +100,7 @@ def auto_ownership_simulate(
trace_choice_name="auto_ownership",
log_alt_losers=log_alt_losers,
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

if estimator:
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/free_parking.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def free_parking(
trace_label=trace_label,
trace_choice_name="free_parking_at_work",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

free_parking_alt = model_settings.FREE_PARKING_ALT
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/joint_tour_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def joint_tour_composition(
trace_label=trace_label,
trace_choice_name="composition",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

# convert indexes to alternative names
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/joint_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def joint_tour_frequency(
trace_label=trace_label,
trace_choice_name="joint_tour_frequency",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

# convert indexes to alternative names
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/joint_tour_frequency_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def joint_tour_frequency_composition(
trace_choice_name=trace_label,
estimator=estimator,
explicit_chunk_size=0,
fastmath=model_settings.sharrow_fastmath,
)

if estimator:
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/joint_tour_participation.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ def joint_tour_participation(
trace_choice_name="participation",
custom_chooser=participants_chooser,
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

# choice is boolean (participate or not)
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ def mandatory_tour_frequency(
trace_label=trace_label,
trace_choice_name="mandatory_tour_frequency",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

# convert indexes to alternative names
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/non_mandatory_tour_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ def non_mandatory_tour_frequency(
trace_choice_name="non_mandatory_tour_frequency",
estimator=estimator,
explicit_chunk_size=model_settings.explicit_chunk,
fastmath=model_settings.sharrow_fastmath,
)

if estimator:
Expand Down
17 changes: 17 additions & 0 deletions activitysim/abm/models/school_escorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,22 @@ class SchoolEscortSettings(BaseLogitComponentSettings):
```
"""

sharrow_fastmath: bool = False
"""Use fastmath when evaluating this component with sharrow.

The fastmath option can be used to speed up the evaluation of expressions in
this component's spec files, but it does so by making some simplifying
assumptions about the math, e.g. that neither inputs nor outputs of any
computations are NaN or Inf. This can lead to errors when the assumptions
are violated. If running in sharrow test mode generates errors, try turning
this setting off.

The default for most components is True, but due to the prevalence of NaN
values in the school escorting model, the default is False for this
component. It can be turned on if desired, but it is recommended to test
carefully that results are as expected.
"""

SIMULATE_CHOOSER_COLUMNS: list[str] | None = None

SPEC: None = None
Expand Down Expand Up @@ -550,6 +566,7 @@ def school_escorting(
trace_choice_name="school_escorting_" + stage,
estimator=estimator,
explicit_chunk_size=model_settings.explicit_chunk,
fastmath=model_settings.sharrow_fastmath,
)

if estimator:
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/stop_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def stop_frequency(
trace_label=tracing.extend_trace_label(trace_label, segment_name),
trace_choice_name="stops",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

# convert indexes to alternative names
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/telecommute_frequency.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def telecommute_frequency(
trace_label=trace_label,
trace_choice_name="telecommute_frequency",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

choices = pd.Series(model_spec.columns[choices.values], index=choices.index)
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/transit_pass_ownership.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def transit_pass_ownership(
trace_label=trace_label,
trace_choice_name="transit_pass_ownership",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

if estimator:
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/trip_mode_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ def trip_mode_choice(
trace_label=segment_trace_label,
trace_choice_name="trip_mode_choice",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

if state.settings.trace_hh_id:
Expand Down
4 changes: 4 additions & 0 deletions activitysim/abm/models/util/mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def mode_choice_simulate(
trace_choice_name,
trace_column_names=None,
estimator: Optional[Estimator] = None,
fastmath: bool = True,
):
"""
common method for both tour_mode_choice and trip_mode_choice
Expand All @@ -51,6 +52,7 @@ def mode_choice_simulate(
trace_label
trace_choice_name
estimator
fastmath

Returns
-------
Expand All @@ -70,6 +72,7 @@ def mode_choice_simulate(
trace_choice_name=trace_choice_name,
estimator=estimator,
trace_column_names=trace_column_names,
fastmath=fastmath,
)

# for consistency, always return dataframe, whether or not logsums were requested
Expand Down Expand Up @@ -170,6 +173,7 @@ def run_tour_mode_choice_simulate(
trace_choice_name=trace_choice_name,
trace_column_names=trace_column_names,
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

return choices
1 change: 1 addition & 0 deletions activitysim/abm/models/vehicle_allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def vehicle_allocation(
trace_label=trace_label,
trace_choice_name="vehicle_allocation",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

# matching alt names to choices
Expand Down
2 changes: 2 additions & 0 deletions activitysim/abm/models/vehicle_type_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,7 @@ def iterate_vehicle_type_choice(
trace_choice_name="vehicle_type",
estimator=estimator,
explicit_chunk_size=model_settings.explicit_chunk,
fastmath=model_settings.sharrow_fastmath,
)

# otherwise, "simple simulation" should suffice, with a model spec that enumerates
Expand All @@ -500,6 +501,7 @@ def iterate_vehicle_type_choice(
trace_label=trace_label,
trace_choice_name="vehicle_type",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)
else:
raise NotImplementedError(simulation_type)
Expand Down
1 change: 1 addition & 0 deletions activitysim/abm/models/work_from_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def work_from_home(
trace_label=trace_label,
trace_choice_name="work_from_home",
estimator=estimator,
fastmath=model_settings.sharrow_fastmath,
)

if iterations_target_percent is not None:
Expand Down
11 changes: 11 additions & 0 deletions activitysim/core/configuration/logit.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ class BaseLogitComponentSettings(PydanticReadable):
sharrow_skip: bool = False
"""Skip sharrow when evaluating this component."""

sharrow_fastmath: bool = True
jpn-- marked this conversation as resolved.
Show resolved Hide resolved
"""Use fastmath when evaluating this component with sharrow.

The fastmath option can be used to speed up the evaluation of expressions in
this component's spec files, but it does so by making some simplifying
assumptions about the math, e.g. that neither inputs nor outputs of any
computations are NaN or Inf. This can lead to errors when the assumptions
are violated. If running in sharrow test mode generates errors, try turning
this setting off.
"""


class LogitComponentSettings(BaseLogitComponentSettings):
"""
Expand Down
16 changes: 16 additions & 0 deletions activitysim/core/flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ def get_flow(
choosers=None,
interacts=None,
zone_layer=None,
fastmath=True,
):
extra_vars = only_simple(local_d)
orig_col_name = local_d.get("orig_col_name", None)
Expand Down Expand Up @@ -184,6 +185,7 @@ def get_flow(
zone_layer=zone_layer,
aux_vars=aux_vars,
primary_origin_col_name=primary_origin_col_name,
fastmath=fastmath,
)
flow.tree.aux_vars = aux_vars
return flow
Expand Down Expand Up @@ -465,6 +467,7 @@ def new_flow(
zone_layer=None,
aux_vars=None,
primary_origin_col_name=None,
fastmath=True,
):
"""
Setup a new sharrow flow.
Expand Down Expand Up @@ -516,6 +519,11 @@ def new_flow(
aux_vars : Mapping
Extra values that are available to expressions and which are written
only by reference into compiled code (and thus can be changed later).
fastmath : bool, default True
Whether to use fastmath in the numba compiled code. Leaving this option
set to "True" is generally a good idea to maximize performance, but it
can be turned off if some expressions are not compatible with fastmath,
i.e. when they include "NaN" or "Inf" values.

Returns
-------
Expand Down Expand Up @@ -700,6 +708,7 @@ def _apply_filter(_dataset, renames: list):
extra_hash_data=extra_hash_data,
hashing_level=0,
boundscheck=False,
fastmath=fastmath,
)


Expand Down Expand Up @@ -750,6 +759,7 @@ def apply_flow(
required=False,
interacts=None,
zone_layer=None,
fastmath=True,
):
"""
Apply a sharrow flow.
Expand Down Expand Up @@ -779,6 +789,11 @@ def apply_flow(
Specify which zone layer of the skims is to be used. You cannot use the
'maz' zone layer in a one-zone model, but you can use the 'taz' layer in
a two- or three-zone model (e.g. for destination pre-sampling).
fastmath : bool, default True
Whether to use fastmath in the numba compiled code. Leaving this option
set to "True" is generally a good idea to maximize performance, but it
can be turned off if some expressions are not compatible with fastmath,
i.e. when they include "NaN" or "Inf" values.

Returns
-------
Expand Down Expand Up @@ -807,6 +822,7 @@ def apply_flow(
choosers=choosers,
interacts=interacts,
zone_layer=zone_layer,
fastmath=fastmath,
)
except ValueError as err:
if "unable to rewrite" in str(err):
Expand Down
8 changes: 8 additions & 0 deletions activitysim/core/interaction_simulate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def eval_interaction_utilities(
log_alt_losers=False,
extra_data=None,
zone_layer=None,
fastmath=True,
):
"""
Compute the utilities for a single-alternative spec evaluated in the context of df
Expand Down Expand Up @@ -184,6 +185,7 @@ def replace_in_index_level(mi, level, *repls):
trace_label,
interacts=extra_data,
zone_layer=zone_layer,
fastmath=fastmath,
)
if sh_util is not None:
chunk_sizer.log_df(trace_label, "sh_util", sh_util)
Expand Down Expand Up @@ -566,6 +568,7 @@ def to_series(x):
retrace_eval_parts_ = pd.concat(retrace_eval_parts, axis=1)

re_sh_flow_load = sh_flow.load(
sh_tree,
dtype=np.float32,
)
re_sh_flow_load_ = re_sh_flow_load[re_trace]
Expand Down Expand Up @@ -606,6 +609,7 @@ def _interaction_simulate(
log_alt_losers=False,
estimator=None,
chunk_sizer=None,
fastmath: bool = True,
):
"""
Run a MNL simulation in the situation in which alternatives must
Expand Down Expand Up @@ -721,6 +725,7 @@ def _interaction_simulate(
estimator=estimator,
log_alt_losers=log_alt_losers,
extra_data=alternatives,
fastmath=fastmath,
)

# set this index here as this is how later code extracts the chosen alt id's
Expand Down Expand Up @@ -785,6 +790,7 @@ def _interaction_simulate(
trace_rows,
estimator=estimator,
log_alt_losers=log_alt_losers,
fastmath=fastmath,
)
chunk_sizer.log_df(trace_label, "interaction_utilities", interaction_utilities)
# mem.trace_memory_info(f"{trace_label}.init interaction_utilities", force_garbage_collect=True)
Expand Down Expand Up @@ -892,6 +898,7 @@ def interaction_simulate(
trace_choice_name=None,
estimator=None,
explicit_chunk_size=0,
fastmath: bool = True,
):
"""
Run a simulation in the situation in which alternatives must
Expand Down Expand Up @@ -969,6 +976,7 @@ def interaction_simulate(
log_alt_losers=log_alt_losers,
estimator=estimator,
chunk_sizer=chunk_sizer,
fastmath=fastmath,
)

result_list.append(choices)
Expand Down
Loading
Loading