According to https://pytorch.org/docs/stable/torch.html#torch.where 55, there r 2 ways to use torch.where. Here, it seems that u r using the non-differential one.  torch.where(condition) is equivalent to torch.nonzero(condition, as_tuple=True), which returns indices, and thus broke the graph.