TorchSharp icon indicating copy to clipboard operation
TorchSharp copied to clipboard

[Windows / CPU] NYI crash in Categorical.log_prob → floordiv (SymInt) when calling gather

Open GGreenNine opened this issue 11 months ago • 1 comments

TorchSharp: 0.105.0 TorchSharp-cuda-windows 0.105.0 OS : Windows 11 .NET : 8.0.3 Hardware : intel 13th i9 - 13980HX, RTX 4090 laptop (not used – repro on CPU)

Repro case:

using TorchSharp;
using static TorchSharp.torch;

torch.random.manual_seed(0);

// any 1-D probability vector, CPU
using var probs = tensor(new float[] { 0.3f, 0.4f, 0.3f }, dtype: ScalarType.Float32);

var dist   = torch.distributions.Categorical(probs);
using var sample = dist.sample();            // OK
using var logP   = dist.log_prob(sample);    //  NYI -> floordiv

stack trace:

Unhandled exception. System.AggregateException: One or more errors occurred. (NYI
Exception raised from floordiv at C:\actions-runner\_work\pytorch\pytorch\builder\windows\pytorch\c10/core/SymNodeImpl.h:76 (most recent call first):
00007FFE284383C900007FFE28438320 c10.dll!c10::Error::Error [<unknown file> @ <unknown line number>]

Analysis (why it happens) Categorical.log_prob calls aten::gather under the hood.

On the Windows LibTorch build ≥ 2.1, the CPU branch of gather was migrated to symbolic shapes; it now performs SymInt::floordiv when checking overlaps.

That floordiv is marked NYI for Windows releases, so any CPU call that ultimately reaches gather(select()) with symbolic sizes crashes.

Proposed fix / directions

  • Short-term: Categorical.log_prob could detect 1-D input on CPU and compute log(probs[index]) directly (no gather), mirroring PyTorch’s distributions/categorical.py fallback.

  • Long-term: implement or disable SymInt::floordiv for CPU gather on Windows, or compile LibTorch Windows wheels with BUILD_FM_SUPPORT_SYMSHAPE=OFF until full support lands.

GGreenNine avatar May 12 '25 08:05 GGreenNine

Hi, i tested this on Libtorch 2.8.0 Cuda 12.8 (with tensors on CPU) and not have any exception. Soon i will try replicate them with old libtorch like 2.1+cpu

Environment tested:

  • Libtorch 2.8.0+cu128
  • Net Framework 4.7.2
  • Debug mode

haytham2597 avatar Sep 20 '25 17:09 haytham2597