pytorch/torch/csrc/utils/six.h
Aaron Gokaslan 0cd18ba1ca
Some checks failed
weekly / update-commit-hash (push) Has been cancelled
weekly / update-slow-tests (push) Has been cancelled
Delete old branches / delete (push) Has been cancelled
docker-builds / get-label-type (push) Has been cancelled
inductor-nightly / get-default-label-prefix (push) Has been cancelled
inductor-micro-benchmark / get-default-label-prefix (push) Has been cancelled
inductor-micro-benchmark-x86 / linux-jammy-cpu-py3.9-gcc11-inductor (push) Has been cancelled
Nightly Upload to s3 / upload-stats-to-s3 (push) Has been cancelled
trunk / before-test (push) Has been cancelled
trunk / get-label-type (push) Has been cancelled
trunk / macos-py3-arm64 (push) Has been cancelled
slow / before-test (push) Has been cancelled
slow / get-label-type (push) Has been cancelled
s390x-periodic / before-test (push) Has been cancelled
s390x-periodic / linux-manylinux-2_28-py3-cpu-s390x (push) Has been cancelled
rocm / before-test (push) Has been cancelled
rocm / linux-jammy-rocm-py3.10 (push) Has been cancelled
rocm-mi300 / before-test (push) Has been cancelled
rocm-mi300 / get-label-type (push) Has been cancelled
pull / before-test (push) Has been cancelled
pull / get-label-type (push) Has been cancelled
inductor-unittest / get-label-type (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-focal-cuda11.8-cudnn9-py3-gcc9, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-focal-cuda12.6-cudnn9-py3-gcc11, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-focal-linter, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-focal-py3-clang10-onnx, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-focal-py3.11-clang10, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-focal-py3.12-clang10, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-focal-py3.13-clang10, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-focal-py3.9-clang10, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-aarch64-py3.10-gcc11, linux.arm64.m7g.4xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-aarch64-py3.10-gcc11-inductor-benchmarks, linux.arm64.m7g.4xlarge, 600) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-cuda11.8-cudnn9-py3.9-clang12, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-cuda11.8-cudnn9-py3.9-linter, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-cuda12.6-cudnn9-py3-gcc9-inductor-benchmarks, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-cuda12.6-cudnn9-py3.12-gcc9-inductor-benchmarks, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-cuda12.6-cudnn9-py3.13-gcc9-inductor-benchmarks, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-cuda12.8-cudnn9-py3-gcc9-inductor-benchmarks, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-py3-clang12-executorch, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-py3-clang15-asan, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-py3-clang18-asan, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-py3.12-halide, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-py3.12-triton-cpu, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-py3.9-gcc11, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-py3.9-gcc11-inductor-benchmarks, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-rocm-n-1-py3, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-rocm-n-py3, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-xpu-2025.0-py3, linux.12xlarge) (push) Has been cancelled
docker-builds / docker-build (pytorch-linux-jammy-xpu-2025.1-py3, linux.12xlarge) (push) Has been cancelled
inductor-nightly / linux-jammy-cpu-py3.9-gcc11-nightly-dynamo-benchmarks (push) Has been cancelled
inductor-micro-benchmark / cuda12.8-py3.10-gcc9-sm80 (push) Has been cancelled
trunk / libtorch-linux-focal-cuda12.6-py3.10-gcc11-debug (push) Has been cancelled
trunk / linux-focal-cuda12.6-py3.10-gcc11-no-ops (push) Has been cancelled
trunk / win-vs2022-cpu-py3 (push) Has been cancelled
trunk / win-vs2022-cuda12.6-py3 (push) Has been cancelled
trunk / linux-jammy-rocm-py3.10 (push) Has been cancelled
trunk / cuda12.8-py3.10-gcc9-sm80 (push) Has been cancelled
trunk / verify-cachebench-cpu-build (push) Has been cancelled
trunk / verify-cachebench-cpu-test (push) Has been cancelled
slow / linux-focal-cuda12.6-py3.10-gcc11-sm86 (push) Has been cancelled
slow / linux-focal-py3.9-clang10 (push) Has been cancelled
slow / linux-jammy-rocm-py3.10 (push) Has been cancelled
slow / linux-jammy-py3.10-clang15-asan (push) Has been cancelled
rocm-mi300 / linux-jammy-rocm-py3.10-mi300 (push) Has been cancelled
pull / linux-jammy-py3.9-gcc11 (push) Has been cancelled
pull / linux-docs (push) Has been cancelled
pull / linux-jammy-py3.9-gcc11-no-ops (push) Has been cancelled
pull / linux-jammy-py3.9-gcc11-pch (push) Has been cancelled
pull / linux-jammy-py3.10-clang15-asan (push) Has been cancelled
pull / linux-focal-py3.9-clang10-onnx (push) Has been cancelled
pull / linux-focal-py3.9-clang10 (push) Has been cancelled
pull / linux-focal-py3.13-clang10 (push) Has been cancelled
pull / linux-focal-cuda12.6-py3.10-gcc11-build-distributed (push) Has been cancelled
pull / linux-focal-cuda12.6-py3.10-gcc11-test (push) Has been cancelled
pull / linux-focal-cuda12.6-py3.10-gcc11 (push) Has been cancelled
pull / linux-jammy-py3-clang12-mobile-build (push) Has been cancelled
pull / linux-jammy-cuda11.8-cudnn9-py3.9-clang12 (push) Has been cancelled
pull / linux-focal-py3_9-clang9-xla (push) Has been cancelled
pull / linux-focal-cpu-py3.10-gcc11-bazel-test (push) Has been cancelled
pull / linux-jammy-py3.9-gcc11-mobile-lightweight-dispatch-build (push) Has been cancelled
pull / linux-jammy-rocm-py3.10 (push) Has been cancelled
pull / linux-focal-cuda12.6-py3.10-gcc11-sm89 (push) Has been cancelled
pull / unstable-linux-focal-cuda12.6-py3.10-gcc11-sm89-xfail (push) Has been cancelled
pull / linux-jammy-py3-clang12-executorch (push) Has been cancelled
pull / cuda12.8-py3.10-gcc9-sm75 (push) Has been cancelled
pull / linux-jammy-xpu-2025.1-py3.9 (push) Has been cancelled
inductor-unittest / cuda12.6-py3.10-gcc9-sm86 (push) Has been cancelled
inductor-unittest / cuda12.6-py3.12-gcc9-sm86 (push) Has been cancelled
inductor-unittest / linux-jammy-cpu-py3.12-gcc11-inductor-halide (push) Has been cancelled
inductor-unittest / linux-jammy-cpu-py3.12-gcc11-inductor-triton-cpu (push) Has been cancelled
inductor-unittest / linux-jammy-cpu-py3.9-gcc11-inductor (push) Has been cancelled
inductor-unittest / cuda12.6-py3.13-gcc9-sm86 (push) Has been cancelled
ossf-scorecard / Scorecards analysis (push) Has been cancelled
Close nonexistent disable issues / close-nonexistent-disable-issues (push) Has been cancelled
[BE][Ez] Update deprecated pybind11 functions (#154798)
* getType() is deprecated, replace it with new/proper static method. These are backwards compatible with old pybind11 versions we support. So break this off before we upgrade to pybind11 3.0 where these methods are dropped in #154115

Pull Request resolved: https://github.com/pytorch/pytorch/pull/154798
Approved by: https://github.com/jansel, https://github.com/cyyever
2025-06-01 06:17:50 +00:00

53 lines
1.5 KiB
C++

#pragma once
#include <pybind11/pybind11.h>
#include <torch/csrc/utils/object_ptr.h>
#include <torch/csrc/utils/pybind.h>
#include <torch/csrc/utils/structseq.h>
namespace six {
// Usually instances of PyStructSequence is also an instance of tuple
// but in some py2 environment it is not, so we have to manually check
// the name of the type to determine if it is a namedtupled returned
// by a pytorch operator.
inline bool isStructSeq(pybind11::handle input) {
return pybind11::cast<std::string>(pybind11::type::handle_of(input).attr(
"__module__")) == "torch.return_types";
}
inline bool isStructSeq(PyObject* obj) {
return isStructSeq(pybind11::handle(obj));
}
inline bool isTuple(pybind11::handle input) {
if (PyTuple_Check(input.ptr())) {
return true;
}
return false;
}
inline bool isTuple(PyObject* obj) {
return isTuple(pybind11::handle(obj));
}
// maybeAsTuple: if the input is a structseq, then convert it to a tuple
//
// On Python 3, structseq is a subtype of tuple, so these APIs could be used
// directly. But on Python 2, structseq is not a subtype of tuple, so we need to
// manually create a new tuple object from structseq.
inline THPObjectPtr maybeAsTuple(PyStructSequence* obj) {
Py_INCREF(obj);
return THPObjectPtr((PyObject*)obj);
}
inline THPObjectPtr maybeAsTuple(PyObject* obj) {
if (isStructSeq(obj))
return maybeAsTuple((PyStructSequence*)obj);
Py_INCREF(obj);
return THPObjectPtr(obj);
}
} // namespace six