Skip to content

Commit

Permalink
Make tuple checks faster (pytorch#16657)
Browse files Browse the repository at this point in the history
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: pytorch#16657

Differential Revision: D13957854

Pulled By: ezyang

fbshipit-source-id: be32eb473ad77a0805e8247d8d583d673d4bdf25
  • Loading branch information
apaszke authored and facebook-github-bot committed Feb 5, 2019
1 parent 85ad011 commit 963e410
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions torch/csrc/utils/six.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<pybind11::tuple>(input) || m == "torch.return_types";
if (PyTuple_Check(input.ptr())) {
return true;
}
#if PY_MAJOR_VERSION == 2
return pybind11::cast<std::string>(input.get_type().attr("__module__")) == "torch.return_types";
#else
return false;
#endif
}

inline bool isTuple(PyObject* obj) {
Expand Down

0 comments on commit 963e410

Please sign in to comment.