Skip to content

Preserve input dtype in population code helpers#434

Open
rlogger wants to merge 1 commit into
jeshraghian:masterfrom
rlogger:fix-population-code-dtype
Open

Preserve input dtype in population code helpers#434
rlogger wants to merge 1 commit into
jeshraghian:masterfrom
rlogger:fix-population-code-dtype

Conversation

@rlogger

@rlogger rlogger commented Jul 14, 2026

Copy link
Copy Markdown

Fixes #429, and the ce_count_loss float64 case from #428.

_population_code in both snntorch/functional/acc.py and snntorch/functional/loss.py allocates its accumulator with torch.zeros(...), which always produces float32 regardless of the input dtype. This causes two problems with float64 inputs:

  1. Silent precision loss in accuracy_rate: close float64 scores are downcast to float32, which can collapse them to equal values and flip the predicted class (the reproduction in snnTorch population-coded accuracy can mis-score close float64 predictions #429 reports 0.0 accuracy when the target class has the strictly larger score).
  2. RuntimeError in ce_count_loss(population_code=True): the float32 population code is passed to NLLLoss alongside float64 class weights, raising expected scalar type Float but found Double.

The fix allocates the accumulator with dtype=spk_out.dtype (and passes device= directly instead of the extra .to(device) copy) so the population code always matches the input.

Added regression tests for both cases:

  • test_accuracy_rate_population_code_dtype: checks that close float64 scores keep their ordering and the accuracy is scored correctly, plus a couple of basic accuracy_rate tests since acc.py had no coverage before.
  • test_ce_count_loss_population_code_dtype: checks that population-coded ce_count_loss works with float64 inputs/weights and returns a float64 loss.

Full test suite passes locally (150 passed + NIR tests), and the changed files are formatted with black. float32 behavior is unchanged.

Made with Cursor

@rlogger
rlogger force-pushed the fix-population-code-dtype branch from 026d79b to fa6d080 Compare July 14, 2026 21:16
@rlogger
rlogger force-pushed the fix-population-code-dtype branch from fa6d080 to 5aa6bf6 Compare July 14, 2026 21:17
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.

snnTorch population-coded accuracy can mis-score close float64 predictions

1 participant