clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name tensor_apply.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-output=html -analyzer-checker=python -analyzer-disable-checker=deadcode -analyzer-config prune-paths=true,suppress-c++-stdlib=true,suppress-inlined-defensive-checks=false,suppress-null-return-paths=false,crosscheck-with-z3=true,model-path=/opt/pyrefcon/lib/pyrefcon/models/models -analyzer-config experimental-enable-naive-ctu-analysis=true,ctu-dir=/tmp/pyrefcon/pytorch/csa-scan,ctu-index-name=/tmp/pyrefcon/pytorch/csa-scan/externalDefMap.txt,ctu-invocation-list=/tmp/pyrefcon/pytorch/csa-scan/invocations.yaml,display-ctu-progress=false -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -relaxed-aliasing -fno-rounding-math -ffp-exception-behavior=ignore -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/tmp/pyrefcon/pytorch/build -resource-dir /opt/pyrefcon/lib/clang/13.0.0 -isystem third_party/gloo -isystem ../cmake/../third_party/gloo -isystem ../cmake/../third_party/googletest/googlemock/include -isystem ../cmake/../third_party/googletest/googletest/include -isystem ../third_party/protobuf/src -isystem ../third_party/gemmlowp -isystem ../third_party/neon2sse -isystem ../third_party/XNNPACK/include -isystem ../third_party -isystem ../cmake/../third_party/eigen -isystem /opt/pyrefcon/lib/pyrefcon/models/python3.8 -isystem /usr/lib/python3/dist-packages/numpy/core/include -isystem ../cmake/../third_party/pybind11/include -isystem /usr/lib/x86_64-linux-gnu/openmpi/include/openmpi -isystem /usr/lib/x86_64-linux-gnu/openmpi/include -isystem ../third_party/ideep/mkl-dnn/include -isystem ../third_party/ideep/include -D BUILDING_TESTS -D FMT_HEADER_ONLY=1 -D HAVE_MALLOC_USABLE_SIZE=1 -D HAVE_MMAP=1 -D HAVE_SHM_OPEN=1 -D HAVE_SHM_UNLINK=1 -D MINIZ_DISABLE_ZIP_READER_CRC32_CHECKS -D ONNXIFI_ENABLE_EXT=1 -D ONNX_ML=1 -D ONNX_NAMESPACE=onnx_torch -D THP_BUILD_MAIN_LIB -D USE_C10D -D USE_C10D_GLOO -D USE_C10D_MPI -D USE_DISTRIBUTED -D USE_EXTERNAL_MZCRC -D USE_NUMPY -D USE_RPC -D USE_TENSORPIPE -D USE_VALGRIND -D _FILE_OFFSET_BITS=64 -D torch_python_EXPORTS -I aten/src -I ../aten/src -I . -I ../ -I ../cmake/../third_party/benchmark/include -I caffe2/contrib/aten -I ../third_party/onnx -I third_party/onnx -I ../third_party/foxi -I third_party/foxi -I ../torch/.. -I ../torch/../aten/src -I ../torch/../aten/src/TH -I caffe2/aten/src -I third_party -I ../torch/../third_party/valgrind-headers -I ../torch/../third_party/gloo -I ../torch/../third_party/onnx -I ../torch/csrc -I ../torch/csrc/api/include -I ../torch/lib -I ../torch/lib/libshm -I ../torch/csrc/distributed -I ../torch/csrc/api -I ../c10/.. -I third_party/ideep/mkl-dnn/include -I ../third_party/ideep/mkl-dnn/src/../include -I ../torch/lib/libshm/../../../torch/lib -I ../third_party/fmt/include -D USE_PTHREADPOOL -D NDEBUG -D USE_KINETO -D LIBKINETO_NOCUPTI -D USE_FBGEMM -D USE_QNNPACK -D USE_PYTORCH_QNNPACK -D USE_XNNPACK -D SYMBOLICATE_MOBILE_DEBUG_HANDLE -D HAVE_AVX_CPU_DEFINITION -D HAVE_AVX2_CPU_DEFINITION -D NDEBUG -D NDEBUG -D CAFFE2_USE_GLOO -D HAVE_GCC_GET_CPUID -D USE_AVX -D USE_AVX2 -D TH_HAVE_THREAD -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /opt/pyrefcon/lib/clang/13.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wno-narrowing -Wall -Wextra -Werror=return-type -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -Wno-unused-but-set-variable -Wno-maybe-uninitialized -Werror=format -Werror=cast-function-type -Wno-stringop-overflow -Wno-write-strings -Wno-strict-aliasing -w -std=gnu++14 -fdeprecated-macro -fdebug-compilation-dir=/tmp/pyrefcon/pytorch/build -ferror-limit 19 -fvisibility-inlines-hidden -fopenmp -fopenmp-cuda-parallel-target-regions -pthread -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -faligned-allocation -fcolor-diagnostics -vectorize-loops -vectorize-slp -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/pyrefcon/pytorch/csa-scan/reports -x c++ ../torch/csrc/utils/tensor_apply.cpp
1 | #include <torch/csrc/utils/tensor_apply.h> |
2 | |
3 | #include <ATen/TensorUtils.h> |
4 | #include <ATen/ExpandUtils.h> |
5 | #include <c10/util/irange.h> |
6 | |
7 | #include <torch/csrc/Exceptions.h> |
8 | #include <torch/csrc/utils/python_numbers.h> |
9 | #include <torch/csrc/utils/python_scalars.h> |
10 | |
11 | using namespace at; |
12 | |
13 | namespace torch { namespace utils { |
14 | |
15 | struct StridedData { |
16 | StridedData(const Tensor & tensor) |
17 | : data(tensor.data_ptr()) |
18 | , strides(tensor.strides()) |
19 | , elementSize(tensor.element_size()) {} |
20 | |
21 | void* data; |
22 | IntArrayRef strides; |
23 | int64_t elementSize; |
24 | |
25 | void step(int dim) { |
26 | data = (char*)data + (strides[dim] * elementSize); |
27 | } |
28 | }; |
29 | |
30 | template<size_t N> |
31 | static void recursive_apply(IntArrayRef sizes, ScalarType scalarType, int64_t dim, |
32 | PyObject* fn, std::array<StridedData, N> strided_data) { |
33 | int64_t ndim = sizes.size(); |
34 | if (dim == ndim) { |
| 1 | Assuming 'dim' is equal to 'ndim' | |
|
| |
35 | auto args = THPObjectPtr(PyTuple_New(N)); |
| |
| 5 | | Returning from 'PyTuple_New' | |
|
| 10 | | PyObject ownership leak with reference count of 1 |
|
36 | if (!args) throw python_error(); |
| 6 | | Assuming the condition is false | |
|
| |
37 | for(const auto i : c10::irange(N)) { |
38 | PyObject* arg = load_scalar(strided_data[i].data, scalarType); |
39 | if (!arg) throw python_error(); |
| |
| |
40 | PyTuple_SET_ITEM(args.get(), i, arg); |
41 | } |
42 | auto ret = THPObjectPtr(PyObject_CallObject(fn, args.get())); |
43 | if (!ret) throw python_error(); |
44 | store_scalar(strided_data[0].data, scalarType, ret.get()); |
45 | return; |
46 | } |
47 | |
48 | auto n = sizes[dim]; |
49 | for(const auto i : c10::irange(n)) { |
50 | (void)i; |
51 | recursive_apply(sizes, scalarType, dim + 1, fn, strided_data); |
52 | for (auto& td : strided_data) { |
53 | td.step(dim); |
54 | } |
55 | } |
56 | } |
57 | |
58 | const Tensor & apply_(const Tensor & self, PyObject* fn) { |
59 | if (self.is_meta()) { |
60 | return self; |
61 | } |
62 | if (!self.device().is_cpu()) { |
63 | throw TypeError("apply_ is only implemented on CPU tensors"); |
64 | } |
65 | auto scalarType = self.scalar_type(); |
66 | recursive_apply<1>(self.sizes(), scalarType, 0, fn, {{ self }}); |
67 | return self; |
68 | } |
69 | |
70 | const Tensor & map_(const Tensor & self, const Tensor & other_, PyObject* fn) { |
71 | if (!other_.options().type_equal(self.options())) { |
72 | throw TypeError("map_: expected %s for 'other' (got %s)", |
73 | self.toString().c_str(), other_.toString().c_str()); |
74 | } |
75 | if (self.is_meta()) { |
76 | return self; |
77 | } |
78 | if (!self.device().is_cpu()) { |
79 | throw TypeError("map_ is only implemented on CPU tensors"); |
80 | } |
81 | c10::MaybeOwned<Tensor> other = expand_inplace(self, other_, "map_"); |
82 | auto scalarType = self.scalar_type(); |
83 | recursive_apply<2>(self.sizes(), scalarType, 0, fn, {{ self, *other }}); |
84 | return self; |
85 | } |
86 | |
87 | const Tensor & map2_(const Tensor & self, const Tensor & x_, const Tensor & y_, PyObject* fn) { |
88 | if (!x_.options().type_equal(self.options())) { |
89 | throw TypeError("map2_: expected %s for argument 'x' (got %s)", |
90 | self.toString().c_str(), x_.toString().c_str()); |
91 | } |
92 | if (!y_.options().type_equal(self.options())) { |
93 | throw TypeError("map2_: expected %s for argument 'y' (got %s)", |
94 | self.toString().c_str(), y_.toString().c_str()); |
95 | } |
96 | if (self.is_meta()) { |
97 | return self; |
98 | } |
99 | if (!self.device().is_cpu() || !x_.device().is_cpu() || !y_.device().is_cpu()) { |
100 | throw TypeError("map2_ is only implemented on CPU tensors"); |
101 | } |
102 | auto others = expand_inplace(self, x_, y_, "map2_"); |
103 | auto scalarType = self.scalar_type(); |
104 | recursive_apply<3>(self.sizes(), scalarType, 0, fn, {{ self, *std::get<0>(others), *std::get<1>(others) }}); |
105 | return self; |
106 | } |
107 | |
108 | }} |