Preserve input dtype in population code helpers#434
Open
rlogger wants to merge 1 commit into
Open
Conversation
rlogger
force-pushed
the
fix-population-code-dtype
branch
from
July 14, 2026 21:16
026d79b to
fa6d080
Compare
rlogger
force-pushed
the
fix-population-code-dtype
branch
from
July 14, 2026 21:17
fa6d080 to
5aa6bf6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #429, and the
ce_count_lossfloat64 case from #428._population_codein bothsnntorch/functional/acc.pyandsnntorch/functional/loss.pyallocates its accumulator withtorch.zeros(...), which always produces float32 regardless of the input dtype. This causes two problems with float64 inputs: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).RuntimeErrorince_count_loss(population_code=True): the float32 population code is passed toNLLLossalongside float64 class weights, raisingexpected scalar type Float but found Double.The fix allocates the accumulator with
dtype=spk_out.dtype(and passesdevice=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 basicaccuracy_ratetests sinceacc.pyhad no coverage before.test_ce_count_loss_population_code_dtype: checks that population-codedce_count_lossworks 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