Skip to content

Commit 8beabff

Browse files
pavithranraofacebook-github-bot
authored andcommitted
[PyTorchEdge] Make aten function common to aten and torch_common (#66663)
Summary: Pull Request resolved: #66663 fb: TensorCompare.cpp is in per-app, a target higher than torch_mobile Please read this doc to know about [Per-app ATen/native and Template Selective Build]( https://docs.google.com/document/d/1O5--mOAi_gGh2GkE-REo3qJRRQ_Lks69IfgszcB8ThI/edit) Create a filed called "prim_native_functions.cpp" in ATen, add it to aten_cpu, and cut-paste native::is_nonzero() to prim_native_functions.cpp. By doing this we move the function to lower layer which is more visible to all targets depending on it. Instruction count comparison new vs old https://www.internalfb.com/phabricator/paste/view/P463272302?view=diff Test Plan: fb: ``` (base) [pavithran@devvm1803.vll0 /data/users/pavithran/fbsource] buck build //xplat/caffe2:aten_cpu Building: finished in 0.4 sec (100%) 1/202 jobs, 0/202 updated Total time: 0.4 sec More details at https://www.internalfb.com/intern/buck/build/ea35300b-55be-4b9f-bc74-80cdd869c16a BUILD SUCCEEDED (base) [pavithran@devvm1803.vll0 /data/users/pavithran/fbsource] buck build //xplat/caffe2:aten_native_cpu Building: finished in 0.7 sec (100%) 1/1 jobs, 0/1 updated Total time: 0.8 sec More details at https://www.internalfb.com/intern/buck/build/ccd97d43-c59d-4f29-9418-485cd24575e2 BUILD SUCCEEDED ``` Reviewed By: iseeyuan Differential Revision: D31669536 fbshipit-source-id: d35f069f975db6dce0b678c5b5ddd74bd690f599
1 parent f8f04d5 commit 8beabff

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

aten/src/ATen/native/TensorCompare.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -228,24 +228,6 @@ Tensor isfinite(const Tensor& self) {
228228
});
229229
}
230230

231-
bool is_nonzero(const Tensor& self) {
232-
auto n = self.numel();
233-
TORCH_CHECK(n != 0, "Boolean value of Tensor with no values is ambiguous");
234-
TORCH_CHECK(n < 2, "Boolean value of Tensor with more than one value is ambiguous");
235-
236-
Scalar localScalar = self.item();
237-
if (localScalar.isFloatingPoint()) {
238-
return localScalar.to<double>() != 0;
239-
} else if (localScalar.isComplex()) {
240-
return localScalar.to<c10::complex<double>>() != c10::complex<double>(0.0, 0.0);
241-
} else if (localScalar.isIntegral(false)){
242-
return localScalar.to<int64_t>() != 0;
243-
} else if (localScalar.isBoolean()) {
244-
return localScalar.to<bool>();
245-
}
246-
TORCH_INTERNAL_ASSERT(false, "Expected non-Tensor backend scalar");
247-
}
248-
249231
void _assert_async_cpu(const Tensor& self) {
250232
TORCH_CHECK(native::is_nonzero(self), "Expected Tensor with single nonzero value, but got zero");
251233
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <ATen/ATen.h>
2+
3+
namespace at {
4+
namespace native {
5+
6+
bool is_nonzero(const Tensor& self) {
7+
auto n = self.numel();
8+
TORCH_CHECK(n != 0, "Boolean value of Tensor with no values is ambiguous");
9+
TORCH_CHECK(
10+
n < 2, "Boolean value of Tensor with more than one value is ambiguous");
11+
12+
Scalar localScalar = self.item();
13+
if (localScalar.isFloatingPoint()) {
14+
return localScalar.to<double>() != 0;
15+
} else if (localScalar.isComplex()) {
16+
return localScalar.to<c10::complex<double>>() !=
17+
c10::complex<double>(0.0, 0.0);
18+
} else if (localScalar.isIntegral(false)) {
19+
return localScalar.to<int64_t>() != 0;
20+
} else if (localScalar.isBoolean()) {
21+
return localScalar.to<bool>();
22+
}
23+
TORCH_INTERNAL_ASSERT(false, "Expected non-Tensor backend scalar");
24+
}
25+
} // namespace meta
26+
} // namespace at

tools/build_variables.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ aten_cpu_source_non_codegen_list = [
938938
"aten/src/ATen/nnapi/nnapi_bind.cpp",
939939
"aten/src/ATen/nnapi/nnapi_wrapper.cpp",
940940
"aten/src/ATen/nnapi/nnapi_model_loader.cpp",
941+
"aten/src/ATen/native/prim_native_functions.cpp",
941942
]
942943

943944
aten_cpu_source_codegen_list = [

0 commit comments

Comments
 (0)