Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting inference seed is nowhere to be found in the doc #4272

Closed
londumas opened this issue Dec 5, 2024 · 4 comments
Closed

Setting inference seed is nowhere to be found in the doc #4272

londumas opened this issue Dec 5, 2024 · 4 comments
Labels
Enhancement New feature or request triaged Issue has been triaged by maintainers

Comments

@londumas
Copy link

londumas commented Dec 5, 2024

I have exported a SegNext-t to .engine, and I infer images with it. The issue is that this model is not deterministic since it uses matrix decomposition through random numbers. One simple way to do so in onnxruntime is trhough ort.set_seed(seed). However I can not find anywhere how to do the same for tensorrt in python. I do not find anything in the documentation about reproductibility.

@lix19937
Copy link

lix19937 commented Dec 6, 2024

Try to use follow

import torch 
import numpy as np
import random 

def fix_seed(identical_seed : int = 1024):
  torch.manual_seed(identical_seed)
  torch.cuda.manual_seed(identical_seed)
  torch.cuda.manual_seed_all(identical_seed)
  np.random.seed(identical_seed)
  random.seed(identical_seed)
  torch.backends.cudnn.deterministic = True
  torch.backends.cudnn.benchmark = False

@londumas
Copy link
Author

londumas commented Dec 6, 2024

@lix19937 , thank you for your help. However this does not work, I tested it today.
However, it makes sense, since the code you send me, fixes seeds of packages that have nothing to do with tensorrt or pycuda.
I found a way to set the seed of a generator in pycuda, however this generator is not the same as the one used by tensorrt, so it does not help us here:

import pycuda.autoinit
from pycuda import curandom
import numpy as np
import pycuda

def seed_getter(N):
    array = np.array([42]*N).astype(np.int32)
    array = pycuda.gpuarray.to_gpu(array)
    return array

sampler = curandom.XORWOWRandomNumberGenerator(seed_getter=seed_getter)
print(sampler.gen_uniform(1000, np.float32).get()[0])

sampler = curandom.XORWOWRandomNumberGenerator(seed_getter=seed_getter)
print(sampler.gen_uniform(1000, np.float32).get()[0])

@lix19937
Copy link

lix19937 commented Dec 7, 2024

@londumas Why do you not use the fixed inputs ?

@asfiyab-nvidia asfiyab-nvidia added Enhancement New feature or request triaged Issue has been triaged by maintainers labels Dec 16, 2024
@asfiyab-nvidia
Copy link
Collaborator

@londumas if your model contains random ops that require a seed input, TensorRT doesn't support specifying this input at the moment. We may add support in the future.

However, in the event that the input to your model is a randomly generated tensor, you can set the seed during input tensor initialization in PyTorch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request triaged Issue has been triaged by maintainers
Projects
None yet
Development

No branches or pull requests

3 participants