Skip to content
New issue

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

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

Already on GitHub? # to your account

[WIP][Torch] Seeds / Determinism #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions source/neuropod/backends/torchscript/torch_backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@

void TorchNeuropodBackend::load_model_internal()
{
at::globalContext().setDeterministicCuDNN(options_.torch_cudnn_deterministic);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we avoid making these calls all-together is a user opted out to reduce a chance of modifying the default behavior?

at::globalContext().setBenchmarkCuDNN(options_.torch_cudnn_benchmark);

// Get the model from the neuropod
auto graph_stream = loader_->get_istream_for_file("0/data/model.pt");

Expand Down Expand Up @@ -297,6 +300,12 @@
{
torch::NoGradGuard guard;

// Seed if we need to
if (options_.seed >= 0)
{
torch::manual_seed(options_.seed);

Check warning on line 306 in source/neuropod/backends/torchscript/torch_backend.cc

View check run for this annotation

Codecov / codecov/patch

source/neuropod/backends/torchscript/torch_backend.cc#L305-L306

Added lines #L305 - L306 were not covered by tests
}

// Get inference schema
const auto &method = model_->get_method("forward");
const auto &schema = SCHEMA(method);
Expand Down
17 changes: 17 additions & 0 deletions source/neuropod/options.hh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ struct RuntimeOptions

// Whether or not to disable shape and type checking when running inference
bool disable_shape_and_type_checking = false;

// EXPERIMENTAL
// A seed to use when running a graph
// Note: this currently only applies to TorchScript models
int64_t seed = -1;

// EXPERIMENTAL
// Whether or not to run in deterministic mode. See https://pytorch.org/docs/stable/notes/randomness.html#cudnn
// Note: this currently only applies to TorchScript models and affects all torchscript models in the process.
// Should only be used with OPE to avoid this issue.
bool torch_cudnn_deterministic = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal perferences:

Even we know that this is for Torch only for now, I would call it: backends_cudnn_deterministic and backends_cudnn_benchmark. And then comment specifies that currently this is Torch only. So, we are going to add more options probably in future and it is better to keep them generic and name accordingly (if only 1 backend supports it - fine, but it helps to see gaps/difference between backends and even can be a start point to add options to other framework.


// EXPERIMENTAL
// Whether or not to enable cudnn benchmark. See https://pytorch.org/docs/stable/notes/randomness.html#cudnn
// Note: this currently only applies to TorchScript models and affects all torchscript models in the process.
// Should only be used with OPE to avoid this issue.
bool torch_cudnn_benchmark = false;
};

} // namespace neuropod