diff --git a/tensorflow/python/distribute/values.py b/tensorflow/python/distribute/values.py index effe194f945..0089db635a3 100644 --- a/tensorflow/python/distribute/values.py +++ b/tensorflow/python/distribute/values.py @@ -435,7 +435,7 @@ class DistributedVarOp(object): self.traceback == o.traceback and self.type == o.type) def __hash__(self): - return hash((self.name, self.graph, self.traceback, self.type)) + return hash((self.name, self.graph, tuple(self.traceback), self.type)) class DistributedVariable(DistributedDelegate, variables_lib.Variable, diff --git a/tensorflow/python/util/tf_stack.cc b/tensorflow/python/util/tf_stack.cc index aa9be6305ce..7f5ff7ff8ae 100644 --- a/tensorflow/python/util/tf_stack.cc +++ b/tensorflow/python/util/tf_stack.cc @@ -127,6 +127,11 @@ PYBIND11_MODULE(_tf_stack, m) { // For compatibility with the traceback module. .def("__eq__", &FrameSummary::operator==) .def("__ne__", &FrameSummary::operator!=) + .def("__hash__", + [](const FrameSummary& self) { + return py::hash( + py::make_tuple(self.filename, self.lineno, self.name)); + }) .def("__getitem__", [](const FrameSummary& self, const py::object& index) -> py::object { return py::make_tuple(self.filename, self.lineno, self.name, diff --git a/tensorflow/python/util/tf_stack_test.py b/tensorflow/python/util/tf_stack_test.py index dc5a2a2baa0..07dc2d3f930 100644 --- a/tensorflow/python/util/tf_stack_test.py +++ b/tensorflow/python/util/tf_stack_test.py @@ -52,6 +52,17 @@ class TFStackTest(test.TestCase): another_frame0, _ = tf_stack.extract_stack(limit=2) self.assertEqual(frame0, another_frame0) + def testFrameSummaryEqualityAndHash(self): + # Both defined on the same line to produce identical stacks. + frame1, frame2 = tf_stack.extract_stack(), tf_stack.extract_stack() + self.assertEqual(len(frame1), len(frame2)) + for f1, f2 in zip(frame1, frame2): + self.assertEqual(f1, f2) + self.assertEqual(hash(f1), hash(f1)) + self.assertEqual(hash(f1), hash(f2)) + self.assertEqual(frame1, frame2) + self.assertEqual(hash(tuple(frame1)), hash(tuple(frame2))) + def extract_stack(limit=None): # Both defined on the same line to produce identical stacks.