From d90c57660cb4a90c85169ac14e1fbe46a7ae7b31 Mon Sep 17 00:00:00 2001 From: invo-coder19 Date: Thu, 30 Apr 2026 09:59:15 +0530 Subject: [PATCH] fix(flow_de): remove dead code and fix stale NaN debug prints MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The logsumexp refactor (replacing the old torch.where clamp approach) was completed earlier but left behind two issues: 1. Dead commented-out block (old implementation, ~33 lines) The original numerically-unstable implementation that used orch.where(v2 < 207, v2, 207) and intermediate variables pt, ptsum, pt_sum was left as a triple-quoted string comment. This added noise and confusion for contributors. Removed entirely and replaced with a short comment explaining *why* the logsumexp approach was chosen. 2. Latent NameError in the NaN debug guard After the refactor, pt and pt_sum no longer exist in scope, but the NaN guard block still attempted to print them: print(pt) # -> NameError: name 'pt' is not defined print(pt_sum) # -> NameError: name 'pt_sum' is not defined If NaN was ever triggered, this would raise a NameError *before* the actual ValueError, masking the real bug entirely. Fixed to print the live intermediates that are in scope: 2, log_weights, and wt. No functional changes — behaviour is identical for all valid inputs. --- .../flow_de/flow_de.py | 51 +++---------------- 1 file changed, 8 insertions(+), 43 deletions(-) diff --git a/Physics_Informed_Neural_Network_Diffusion_Equation_Sijil_Jose/flow_de/flow_de.py b/Physics_Informed_Neural_Network_Diffusion_Equation_Sijil_Jose/flow_de/flow_de.py index f4abab4..0d2d5ff 100644 --- a/Physics_Informed_Neural_Network_Diffusion_Equation_Sijil_Jose/flow_de/flow_de.py +++ b/Physics_Informed_Neural_Network_Diffusion_Equation_Sijil_Jose/flow_de/flow_de.py @@ -93,45 +93,10 @@ def forward(self, t, xt): print(' qVectorField: vt.shape', vt.shape) print(' qVectorField: vt', vt) - # sum over arguments of exponential, that is, - # over the d-dimensions of each element in x0, - # so that we get the product of d normal densities. - - ''' - v2 = (vt*vt).sum(dim=-1) - vv = torch.where(v2 < 207, v2, 207) ## why not use the logsumexp for greater stability - if torch.isnan(vv).any(): - raise ValueError("vv contains at least one NAN") - - if debug: - print(' qVectorField: vv.shape', vv.shape) - - # compute unnormalized probability densities. - pt = torch.exp(-vv/2) - if torch.isnan(pt).any(): - raise ValueError("pt contains at least one NAN") - - # pt.shape: (N, M) - if debug: - print(' qVectorField: pt.shape', pt.shape) - - # sum over the M d-dimensional Gaussian densities - ptsum = pt.sum(dim=-1) - # ptsum.shape: (N, ) - if torch.isnan(ptsum).any(): - raise ValueError("ptsum contains at least one NAN") - - # protect sum against divide by zero - pt_sum = torch.where(ptsum < 1.e-44, 1, ptsum).unsqueeze(-1) - # pt_sum.shape: (N, 1) - if torch.isnan(ptsum).any(): - raise ValueError("ptsum contains at least one NAN") - - # compute weights - wt = pt / pt_sum - ''' - - + # Compute squared distances from each collocation point xt to every + # MC sample x0, summed over the d feature dimensions. + # Uses the log-sum-exp trick for numerical stability (avoids exp overflow + # that occurred in the old implementation which used torch.where clamping). v2 = (vt * vt).sum(dim=-1) log_weights = -0.5 * v2 log_weights = log_weights - torch.logsumexp(log_weights, dim=-1, keepdim=True) @@ -140,11 +105,11 @@ def forward(self, t, xt): # wt.shape: (N, M) if torch.isnan(wt).any(): - print('pt') - print(pt) + print('v2') + print(v2) print() - print('pt_sum') - print(pt_sum) + print('log_weights') + print(log_weights) print() print('wt') print(wt)