Model training time increase more than 10x in some case

import torch
para =torch.Tensor(initial_para)
x,y  = torch.tensor(x),torch.relu(torch.tensor(y))
para.requires_grad = True
loss = torch.nn.MSELoss()
optimizer = torch.optim.Adam([para], lr=lr)
for i in `range(max_iter):`
    y_fit = multi_gaussian(x,para)
    l = loss(y_fit,y)
    try:
        if torch.isnan(l) or torch.isinf(l) :
            print("l is nan or inf")
            break
        l.backward()
        optimizer.step()
        optimizer.zero_grad()
    except:
        break
def multi_gaussian(x, params):
    y = torch.zeros_like(x)
    for i in range(0, params.shape[0], 3):
        g = gaussian(x, params[i], params[i + 1], params[i + 2])
        if torch.any(torch.isnan(g)) or torch.any(torch.isinf(g)):
            continue
        y = y + g
    return y
def gaussian(x,center,amp,variance, offset = None):
    return amp*torch.exp(-(x-center)**2/(variance ))

Hi, im using pytorch to fit gaussian mixture model. It works perfect via directly run main.py. My data had length of 1024, and it only took less than 0.1 s to fit the model. However, a strange issue occurred when I using PythonNet package in CSharp. After the python environment was initialized, the main.py was also successfully runned. But the time for model fit increased to nearly 1.8 s, 18 times slower than previous condition.
While i used another fit methed named scipy.optimize.curve_fit method, it tooks almost the same time (0.03 s) as run main.py directly or by CSharp code call. I was so confused by the difference, and I want to know if there is way to solve the problem.

(By the way, im coding my program in net4.8 framework, i cant using torchsharp in Csharp which need net6.0).

Thank a lot!