Skip to main content

Overview

cl1_neural_interface.py is a minimal hardware interface server that runs directly on the CL1 device. It handles all neural hardware operations while the training logic runs remotely. Responsibilities:
  • Receive stimulation commands via UDP from training server
  • Apply stimulation to biological neurons using CL SDK
  • Collect spike responses from neural hardware
  • Send spike counts back to training server via UDP
  • Record neural activity to disk
  • Log event metadata to CL DataStream
Architecture:
  • Zero computation: No PyTorch, no game logic, no policy inference
  • Pure I/O: UDP receive → hardware stimulation → spike collection → UDP send
  • Real-time: Hardware loop runs at configurable tick frequency (typically 10-240 Hz)
Location: source/cl1_neural_interface.py

Command-Line Arguments

Network Configuration

string
required
IP address of the remote training systemRequired parameter. The CL1 device sends spike data to this address.Example: --training-host 192.168.1.50
integer
default:"12345"
UDP port for receiving stimulation commands from training systemThe CL1 device listens on this port (bound to 0.0.0.0).Must match --cl1-stim-port on training server.
integer
default:"12346"
UDP port for sending spike data to training systemThe CL1 device sends spike packets to <training-host>:<spike-port>.Must match --cl1-spike-port on training server.
integer
default:"12347"
UDP port for receiving event metadata from training systemEvents include episode completions, checkpoints, and training completion signals.Must match --cl1-event-port on training server.
integer
default:"12348"
UDP port for receiving feedback stimulation commands from training systemFeedback includes reward signals and event-based stimulation.Must match --cl1-feedback-port on training server.

Hardware Configuration

integer
default:"10"
Frequency (Hz) to run the CL hardware loopControls the rate of:
  • Applying neural stimulation
  • Collecting spike responses
  • Sending spike data to training system
Typical values:
  • 10 Hz: Slow, stable, good for debugging
  • 30 Hz: Normal gameplay speed
  • 60 Hz: Fast gameplay
  • 120-240 Hz: Maximum performance (experimental)
Must match --tick_frequency_hz on training server.
string
default:"./recordings"
Directory path for saving CL1 neural recordingsRecordings contain raw neural data (spikes, stimulation, events) captured during training.Example: --recording-path /data/recordings/doom-neuron

Usage Examples

Basic Setup

Custom Ports

High-Frequency Loop

Custom Recording Location

Architecture

CL1Config Class

Minimal configuration matching the training system:

CL1NeuralInterface Class

Main interface class with methods:

setup_sockets()

Create and bind UDP sockets for communication.

apply_stimulation(neurons, frequencies, amplitudes)

Apply stimulation to neural hardware based on received commands. Parameters:
  • neurons: CL SDK neurons interface
  • frequencies: np.ndarray of shape (num_channel_sets,) with Hz values
  • amplitudes: np.ndarray of shape (num_channel_sets,) with μA values
Process:
  1. Interrupt ongoing stimulation on all channels
  2. For each encoding channel, create StimDesign and BurstDesign
  3. Apply stimulation via neurons.stim()
  4. Cache designs to avoid repeated object creation

collect_spikes(tick)

Collect and count spikes from CL SDK tick. Parameters:
  • tick: cl.LoopTick object from neurons.loop()
Returns:
  • spike_counts: np.ndarray of shape (num_channel_sets,) with spike counts per channel group
Process:
  1. Initialize zero array for spike counts
  2. Iterate through tick.analysis.spikes
  3. Map each spike’s channel to its group index
  4. Increment corresponding group counter

apply_feedback_command(neurons, feedback_type, channels, frequency, amplitude, pulses, unpredictable, event_name)

Apply feedback stimulation to neural hardware. Parameters:
  • neurons: CL SDK neurons interface
  • feedback_type: "interrupt", "event", or "reward"
  • channels: List of channel numbers
  • frequency: Stimulation frequency in Hz
  • amplitude: Stimulation amplitude in μA
  • pulses: Number of pulses/bursts
  • unpredictable: Whether this is unpredictable stimulation
  • event_name: Name of event (for logging)
Feedback Types:
  • interrupt: Stop ongoing stimulation on specified channels
  • event: Apply event-based feedback (kills, damage, pickups)
  • reward: Apply reward-based feedback (positive/negative)

run()

Main hardware loop:

Hardware Loop Details

Stimulation Design Caching

To avoid creating new StimDesign and BurstDesign objects every tick, designs are cached using an LRU cache:

Channel Mapping

Spikes are mapped to channel groups for counting:

Non-Blocking I/O

All UDP sockets use non-blocking mode to prevent loop stalling:
Missing packets are handled gracefully:
  • No stimulation command → continue with previous stimulation
  • No event → continue loop
  • No feedback → continue loop

Recording & Logging

CL Recording

Neural recordings are saved to disk automatically:
Recording Contents:
  • Raw spike data (all channels)
  • Stimulation commands
  • Timestamps
  • Metadata attributes

DataStream Events

Episode metadata is logged to a CL DataStream:

Statistics Logging

Statistics are printed every 10 seconds:
Metrics:
  • ticks: Total hardware loop iterations in this period
  • Recv: Stimulation packets received per second
  • Send: Spike packets sent per second
  • Events: Event metadata packets received
  • Feedback: Feedback command packets received
  • Avg spikes: Average spike count per tick across all channel groups

Graceful Shutdown

The interface handles shutdown gracefully:

Training Complete Event

When the training system sends a training_complete event:

Keyboard Interrupt

When interrupted with Ctrl+C:

Troubleshooting

CL SDK Connection Failed

Solutions:
  • Ensure CL1 device is powered on
  • Check USB connection
  • Verify CL SDK is installed: python -c "import cl"
  • Run with sudo if permission denied

No Stimulation Commands Received

Solutions:
  • Verify training system is running
  • Check network connectivity: ping <training-host>
  • Ensure firewall allows UDP on stim port
  • Verify port numbers match on both systems

High Latency

Solutions:
  • Use wired network instead of WiFi
  • Reduce network traffic on shared network
  • Check for CPU throttling on CL1 device
  • Reduce tick frequency temporarily

Recording Failed to Save

Solutions:
  • Ensure recording path exists: mkdir -p /data/recordings/doom-neuron
  • Check disk space: df -h
  • Verify write permissions: ls -ld /data/recordings

See Also