From 963e410b57ce55f6381413ad2774ea6e75cc9cee Mon Sep 17 00:00:00 2001 From: Adam Paszke Date: Tue, 5 Feb 2019 09:31:21 -0800 Subject: [PATCH] Make tuple checks faster (#16657) Summary: As the comment indicates, the issue is only present in some versions of Python 2, so we should be able to use heavily optimized PyTuple_Check in most cases, and skip allocation of the strings, and unnecessary lookups on object's type. cc ezyang zasdfgbnm Pull Request resolved: https://github.com/pytorch/pytorch/pull/16657 Differential Revision: D13957854 Pulled By: ezyang fbshipit-source-id: be32eb473ad77a0805e8247d8d583d673d4bdf25 --- torch/csrc/utils/six.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/torch/csrc/utils/six.h b/torch/csrc/utils/six.h index 0dd2e2f..54899d4 100644 --- a/torch/csrc/utils/six.h +++ b/torch/csrc/utils/six.h @@ -10,8 +10,14 @@ namespace six { // by a pytorch operator. inline bool isTuple(pybind11::handle input) { - std::string m = pybind11::str(input.get_type().attr("__module__")); - return pybind11::isinstance(input) || m == "torch.return_types"; + if (PyTuple_Check(input.ptr())) { + return true; + } +#if PY_MAJOR_VERSION == 2 + return pybind11::cast(input.get_type().attr("__module__")) == "torch.return_types"; +#else + return false; +#endif } inline bool isTuple(PyObject* obj) { -- 2.7.4