Skip to content

Add frames_in_cpu and responses_in_cpu to project_stimulus_batched#60

Merged
candytaco merged 7 commits into
mainfrom
copilot/update-batched-stimulus-projection-path
Jun 11, 2026
Merged

Add frames_in_cpu and responses_in_cpu to project_stimulus_batched#60
candytaco merged 7 commits into
mainfrom
copilot/update-batched-stimulus-projection-path

Conversation

Copilot AI commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Under GPU backends (torch_cuda/torch_mps), project_stimulus_batched moved the entire stimulus to VRAM and allocated the full (nimages, nfilters) response matrix on GPU — defeating the VRAM-saving purpose of batching.

Changes

moten/core.py

  • Added frames_in_cpu=False: when True, keeps the full stimulus in CPU RAM; only the per-temporal-batch slice is transferred to the device via backend.asarray(stim_chunk) inside the loop.
  • Added responses_in_cpu=False: when True, allocates the output with np.zeros(...) on CPU and copies each batch result back via backend.to_numpy(channel_response). Return value is always a NumPy ndarray.
  • Both default to False — no behavior change for existing callers.

moten/pyramids.py

  • MotionEnergyPyramid.project_stimulus_batched accepts and forwards both new kwargs.

moten/tests/test_batched.py

  • New TestCpuMemoryFlags class (12 tests) covering: numpy/torch backends, flags individually and combined, with stimulus_batch_size, and passing a NumPy stimulus with frames_in_cpu=True under torch.

Usage

# Keep both stimulus and output off GPU; only batch slices touch VRAM
responses = pyramid.project_stimulus_batched(
    stimulus,
    stimulus_batch_size=32,
    frames_in_cpu=True,
    responses_in_cpu=True,
)
# responses is a NumPy ndarray regardless of active backend

Copilot AI changed the title [WIP] Update batched stimulus projection path to reduce GPU VRAM usage Add frames_in_cpu and responses_in_cpu to project_stimulus_batched Jun 11, 2026
Copilot AI requested a review from candytaco June 11, 2026 21:17
@candytaco candytaco marked this pull request as ready for review June 11, 2026 21:18
Copilot AI review requested due to automatic review settings June 11, 2026 21:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 adds two optional flags to project_stimulus_batched to reduce GPU VRAM usage on torch GPU backends by (a) keeping the full stimulus in CPU RAM and transferring only per-temporal-batch chunks to the device, and/or (b) keeping the full (nimages, nfilters) response matrix on CPU and copying each batch result back.

Changes:

  • Add frames_in_cpu and responses_in_cpu kwargs to moten.core.project_stimulus_batched.
  • Expose and forward the same kwargs via MotionEnergyPyramid.project_stimulus_batched.
  • Add a new TestCpuMemoryFlags test class covering numpy/torch backends and flag combinations.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
moten/core.py Implements the new CPU-memory flags in the batched projection core loop and updates the docstring.
moten/pyramids.py Extends MotionEnergyPyramid.project_stimulus_batched signature/docs and forwards the new kwargs to core.
moten/tests/test_batched.py Adds coverage for frames_in_cpu / responses_in_cpu behavior across numpy and torch backends.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread moten/core.py
@candytaco candytaco merged commit a641c8a into main Jun 11, 2026
7 checks passed
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.

3 participants