From 705a757ceb1839e7e09e5236c0a45e9b633e500e Mon Sep 17 00:00:00 2001 From: Behzad Abghari Date: Wed, 10 Feb 2021 12:48:58 -0500 Subject: [PATCH] Sort variables by name in custom gradient for determinism --- tensorflow/python/ops/custom_gradient.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tensorflow/python/ops/custom_gradient.py b/tensorflow/python/ops/custom_gradient.py index 33f4dac2f9d..45cf5e6a27e 100644 --- a/tensorflow/python/ops/custom_gradient.py +++ b/tensorflow/python/ops/custom_gradient.py @@ -373,8 +373,10 @@ def _graph_mode_decorator(f, args, kwargs): v.ref() for v in _get_dependent_variables( input_ops=filtered_input_tensors, output_ops=flat_result) ]) - variables = list( - [v.deref() for v in variables_in_subgraph.union(variables_in_tape)]) + variables = sorted( + [v.deref() for v in variables_in_subgraph.union(variables_in_tape)], + key=lambda v: v.name + ) grad_argspec = tf_inspect.getfullargspec(grad_fn) variables_in_signature = ("variables" in grad_argspec.args or