Skip to content
Back to skills

Stateful Chunk Inference

ASecurity

Processes long sequences in fixed-size chunks while carrying RNN hidden state across chunks for memory-efficient inference.

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
datapython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill stateful-chunk-inference --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Stateful Chunk Inference?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Stateful Chunk Inference
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-stateful-chunk-inference/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-stateful-chunk-inference)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: timeseries-stateful-chunk-inference
description: >
  Processes long sequences in fixed-size chunks while carrying RNN hidden state across chunks for memory-efficient inference.
---
# Stateful Chunk Inference

## Overview

Long time series (days of sensor data at 5-sec intervals = 100k+ timesteps) don't fit in GPU memory as a single sequence. Split into fixed-size chunks and process sequentially, passing the hidden state from one chunk to the next. This gives the same result as processing the full sequence but with constant memory usage.

## Quick Start

```python
import torch
import numpy as np

def stateful_inference(model, features, chunk_size=4096, device='cuda'):
    """Run inference on long sequence in chunks, preserving hidden state.

    Args:
        model: RNN/GRU model that returns (predictions, hidden_state)
        features: numpy array of shape (seq_len, n_features)
        chunk_size: max timesteps per forward pass
    """
    seq_len = len(features)
    predictions = np.zeros((seq_len, model.n_classes))
    hidden = None

    model.eval()
    with torch.no_grad():
        for start in range(0, seq_len, chunk_size):
            end = min(start + chunk_size, seq_len)
            chunk = torch.tensor(features[start:end]).float().unsqueeze(0).to(device)

            preds, hidden = model(chunk, hidden)
            # Detach hidden state to prevent backprop across chunks
            hidden = [h.detach() for h in hidden]

            predictions[start:end] = preds.squeeze(0).cpu().numpy()

    return predictions
```

## Workflow

1. Split input sequence into chunks of fixed size (e.g., 4096 timesteps)
2. Process first chunk with hidden=None (zero-initialized)
3. Pass hidden state from each chunk to the next
4. Detach hidden state tensors to prevent memory accumulation
5. Concatenate chunk predictions into full-length output

## Key Decisions

- **Chunk size**: 4096-8192 is typical; larger = more context per step but more memory
- **Detach hidden**: Essential — without detaching, PyTorch builds a computation graph spanning all chunks
- **Overlap**: No overlap needed for RNNs (hidden state carries context); for CNNs, overlap by receptive field size
- **Batch of series**: Process multiple series in parallel if they fit in memory

## References

- Child Mind Institute - Detect Sleep States (Kaggle)
- Source: [sleep-critical-point-infer](https://www.kaggle.com/code/werus23/sleep-critical-point-infer)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.