From 8a6ad22e85810c7a87dca31ae28ab51732026194 Mon Sep 17 00:00:00 2001 From: Vivek Panyam Date: Wed, 27 May 2020 03:21:25 -0400 Subject: [PATCH] [Torch] Seeds / Determinism --- .../backends/torchscript/torch_backend.cc | 9 +++++++++ source/neuropod/options.hh | 17 +++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/source/neuropod/backends/torchscript/torch_backend.cc b/source/neuropod/backends/torchscript/torch_backend.cc index da19d735..6dfcbf09 100644 --- a/source/neuropod/backends/torchscript/torch_backend.cc +++ b/source/neuropod/backends/torchscript/torch_backend.cc @@ -233,6 +233,9 @@ TorchNeuropodBackend::TorchNeuropodBackend(const std::string &neuropod_path, con void TorchNeuropodBackend::load_model_internal() { + at::globalContext().setDeterministicCuDNN(options_.torch_cudnn_deterministic); + 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"); @@ -297,6 +300,12 @@ std::unique_ptr TorchNeuropodBackend::infer_internal(const Neu { torch::NoGradGuard guard; + // Seed if we need to + if (options_.seed >= 0) + { + torch::manual_seed(options_.seed); + } + // Get inference schema const auto &method = model_->get_method("forward"); const auto &schema = SCHEMA(method); diff --git a/source/neuropod/options.hh b/source/neuropod/options.hh index 319bda07..53049381 100644 --- a/source/neuropod/options.hh +++ b/source/neuropod/options.hh @@ -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; + + // 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