Skip to content

fix(activations): key stored activations on number_of_trials - #2629

Open
jig-mistry wants to merge 1 commit into
brain-score:masterfrom
jig-mistry:jm/number-of-trials-cache-key
Open

jig-mistry wants to merge 1 commit into
brain-score:masterfrom
jig-mistry:jm/number-of-trials-cache-key

Conversation

@jig-mistry

Copy link
Copy Markdown
Contributor

from_paths doesn't pass number_of_trials to _from_paths_stored, so every stored activations file is named ...,number_of_trials=1,require_variance=..., whatever the trial count actually was. With require_variance=True, a request with a different trial count than the cached one gets the cached result:

  • With a list of paths, it silently returns the wrong number of presentations. Asking for 5 trials of one image after a cached 2-trial run gave 2 presentations instead of 5.
  • With a StimulusSet, it fails in attach_stimulus_set_meta, e.g. for 4 stimuli cached at 2 trials and requested at 5:
    ValueError: operands could not be broadcast together with shapes (8,) (20,)
    
    It's a ValueError, not an AssertionError, so brainscore_vision.score doesn't add its stale-cache hint either.

This PR passes the trial count into the key when require_variance is set.

Effect on existing caches:

  • Calls without require_variance still get number_of_trials=1, so their file names don't change. They are identical to master for require_variance=False (with any number_of_trials), require_variance=None, and require_variance=True with 1 trial.
  • require_variance=True entries with more than one trial get a new name. Today that's only Malania2007: 2 trials for the fitting sets and 10 for the test sets. Its activations will be computed once more under the new names, and the old number_of_trials=1,require_variance=True entries become unused, both locally and on S3. Only a one-trial require_variance=True request would still read them, and nothing makes one, but they can be deleted.
  • Those old entries weren't wrong. Each Malania stimulus set is always requested with the same trial count, so I don't think any existing score is affected. This matters once a trial count changes, or for users who pick their own.

Some history:

  • Early in the microsaccades PR (Add microsaccades to Brain-Score core #492), from_paths passed the trial count into this key.
  • It dropped out when from_paths stopped taking number_of_trials (e13391a).
  • A later commit (4887728, "assume number_of_trials=1 and require_variance=False when getting stored activations") gave the parameter a default of 1. require_variance stayed in the key.

This PR puts the trial count back. If leaving it out was intentional, let me know and I'll close this.

Tests (test_number_of_trials_cache_key.py):

  • Two check, with _from_paths_stored mocked, the value that reaches it: the trial count with require_variance=True, and 1 without it.
  • One runs two fresh models with 2 and 5 trials against a real cache in tmp_path. On master the 5-trial run gets 2 presentations back; here it gets 5.

The file runs in a few seconds.

This is independent of #2628: the two don't conflict in either order, and either can go first.

`from_paths` didn't pass `number_of_trials` to `_from_paths_stored`, so
every stored activations file was keyed `number_of_trials=1`. With
`require_variance=True`, a request with a different trial count than the
cached one was served the cached result: a list of paths silently came
back with the wrong number of presentations, and a StimulusSet failed in
`attach_stimulus_set_meta`.

Pass the trial count into the key when `require_variance` is set.
Without it the key stays `number_of_trials=1`, so caches written without
`require_variance` keep their names.
KartikP pushed a commit that referenced this pull request Nov 5, 2026
Closes #376.

With an empty list of paths or an empty `StimulusSet`, nothing complains
until packaging. `_get_activations_batched` never enters its batch loop,
so `layer_activations` stays `None`, and `_package` fails with an error
that says nothing about the input:

```
  File ".../brainscore_vision/model_helpers/activations/core.py", line 369, in _package
    shapes = [a.shape for a in layer_activations.values()]
AttributeError: 'NoneType' object has no attribute 'values'
```

With stored activations it can be worse. Their key ignores the paths, so
an empty request under a stimuli identifier that is already stored gets
the stored activations back. With a list of paths that is a full
assembly for zero stimuli. With a `StimulusSet` it fails later, in
`attach_stimulus_set_meta`.

This adds a check at the start of `from_paths`, before the stored
activations are looked up:

```python
if len(stimuli_paths) == 0:
    raise ValueError("No stimuli passed to retrieve activations from")
```

`__call__` and `from_stimulus_set` both go through `from_paths`, so all
entry points are covered, with or without `require_variance`. The
message matches the existing "No layers passed to retrieve activations
from" check in `_from_paths`.

Test: `test_no_stimuli` passes an empty list and an empty `StimulusSet`
(filtered down to zero rows) through `__call__`, with `require_variance`
off and on. It also replaces `_from_paths_stored` and `_from_paths` with
stubs that fail if called, so the check is shown to come before both
computing and loading activations. It fails on `master` and passes here.

This doesn't overlap with #2628 or #2629. They touch other parts of the
same file and merge cleanly with this in either order.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant