Skip to content

Commit 23d9a8b

Browse files
author
Mike Ruberry
committed
Merge branch 'master' of https://github.com/pytorch/pytorch into warn_integer_div
2 parents 2330e90 + eef17ed commit 23d9a8b

66 files changed

Lines changed: 1131 additions & 697 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
run: |
106106
set -eux
107107
pip install flake8
108-
rm -rf .circleci
108+
rm -rf .circleci tools/clang_format_new.py
109109
flake8 --exit-zero > ${GITHUB_WORKSPACE}/flake8-output.txt
110110
cat ${GITHUB_WORKSPACE}/flake8-output.txt
111111
- name: Add annotations

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,6 @@ TAGS
252252

253253
# clang-format storage location used by apply_clang_format.py
254254
.clang-format-bin
255+
256+
# clangd background index
257+
.clangd/

android/pytorch_android/src/main/cpp/pytorch_jni_jit.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ namespace {
2626
struct JITCallGuard {
2727
// AutoGrad is disabled for mobile by default.
2828
torch::autograd::AutoGradMode no_autograd_guard{false};
29+
// VariableType dispatch is not included in default mobile build. We need set
30+
// this guard globally to avoid dispatch error (only for dynamic dispatch).
31+
// Thanks to the unification of Variable class and Tensor class it's no longer
32+
// required to toggle the NonVariableTypeMode per op - so it doesn't hurt to
33+
// always set NonVariableTypeMode for inference only use case.
34+
torch::AutoNonVariableTypeMode non_var_guard{true};
2935
// Disable graph optimizer to ensure list of unused ops are not changed for
3036
// custom mobile build.
3137
torch::jit::GraphOptimizerEnabledGuard no_optimizer_guard{false};
@@ -111,11 +117,11 @@ class PytorchJni : public facebook::jni::HybridClass<PytorchJni> {
111117
/* need_inputs */ false,
112118
/* sampled */ false);
113119
#endif
114-
JITCallGuard guard;
115120
}
116121

117122
PytorchJni(facebook::jni::alias_ref<jstring> modelPath) {
118123
preModuleLoadSetup();
124+
JITCallGuard guard;
119125
module_ = torch::jit::load(std::move(modelPath->toStdString()));
120126
module_.eval();
121127
}
@@ -147,6 +153,7 @@ class PytorchJni : public facebook::jni::HybridClass<PytorchJni> {
147153
"Could not get buffer for asset '%s'",
148154
assetName->toStdString().c_str());
149155
}
156+
JITCallGuard guard;
150157
module_ = torch::jit::load(torch::make_unique<MemoryReadAdapter>(
151158
assetBuffer, AAsset_getLength(asset)));
152159
AAsset_close(asset);

android/pytorch_android/src/main/cpp/pytorch_jni_lite.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,23 @@
1212

1313
#include "pytorch_jni_common.h"
1414

15-
using namespace pytorch_jni;
16-
1715
namespace pytorch_jni {
1816

17+
namespace {
18+
19+
struct LiteJITCallGuard {
20+
// VariableType dispatch is not included in default mobile build. We need set
21+
// this guard globally to avoid dispatch error (only for dynamic dispatch).
22+
// Thanks to the unification of Variable class and Tensor class it's no longer
23+
// required to toggle the NonVariableTypeMode per op - so it doesn't hurt to
24+
// always set NonVariableTypeMode for inference only use case.
25+
// TODO: avoid having to set this guard for custom mobile build with mobile
26+
// interpreter.
27+
torch::AutoNonVariableTypeMode non_var_guard{true};
28+
};
29+
30+
} // namespace
31+
1932
class PytorchJni : public facebook::jni::HybridClass<PytorchJni> {
2033
private:
2134
friend HybridBase;
@@ -31,6 +44,7 @@ class PytorchJni : public facebook::jni::HybridClass<PytorchJni> {
3144
}
3245

3346
PytorchJni(facebook::jni::alias_ref<jstring> modelPath) {
47+
LiteJITCallGuard guard;
3448
module_ = torch::jit::_load_for_mobile(std::move(modelPath->toStdString()));
3549
}
3650

@@ -55,8 +69,7 @@ class PytorchJni : public facebook::jni::HybridClass<PytorchJni> {
5569
}
5670

5771
auto output = [&]() {
58-
torch::autograd::AutoGradMode guard(false);
59-
at::AutoNonVariableTypeMode non_var_type_mode(true);
72+
LiteJITCallGuard guard;
6073
return module_.forward(inputs);
6174
}();
6275
return JIValue::newJIValueFromAtIValue(output);
@@ -78,7 +91,7 @@ class PytorchJni : public facebook::jni::HybridClass<PytorchJni> {
7891
}
7992
if (auto method = module_.find_method(methodName)) {
8093
auto output = [&]() {
81-
at::AutoNonVariableTypeMode non_var_type_mode(true);
94+
LiteJITCallGuard guard;
8295
return module_.run_method(methodName, inputs);
8396
}();
8497
return JIValue::newJIValueFromAtIValue(output);

aten/src/ATen/native/BinaryOps.cpp

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,6 @@ Tensor& remainder_(Tensor& self, const Tensor& other) {
108108
return native::remainder_out(self, self, other);
109109
}
110110

111-
Tensor truncate(const Tensor& tensor) {
112-
if (tensor.is_floating_point()) {
113-
return tensor.trunc();
114-
}
115-
return tensor;
116-
}
117-
118111
Tensor& true_divide_out(Tensor& result, const Tensor& self, const Tensor& divisor) {
119112
TORCH_CHECK(!isIntegralType(result.scalar_type(), /*includeBool=*/ true),
120113
"True division requires a floating output type, but got ",
@@ -145,14 +138,34 @@ Tensor true_divide(const Tensor& self, const Tensor& divisor) {
145138
return iter.output();
146139
}
147140

148-
Tensor floor_divide(const Tensor& input, const Tensor& other) {
149-
Tensor out = input / other;
150-
return truncate(out);
141+
Tensor& floor_divide_out(Tensor& result, const Tensor& self, const Tensor& other) {
142+
auto iter = TensorIterator::binary_op(result, self, other,
143+
/*check_mem_overlap=*/true);
144+
div_stub(iter.device_type(), iter);
145+
146+
if (result.is_floating_point()) {
147+
result.trunc_();
148+
}
149+
150+
return result;
151+
}
152+
153+
Tensor floor_divide(const Tensor& self, const Tensor& other) {
154+
Tensor result;
155+
auto iter = TensorIterator::binary_op(result, self, other);
156+
157+
div_stub(iter.device_type(), iter);
158+
159+
auto out = iter.output();
160+
if (out.is_floating_point()) {
161+
out.trunc_();
162+
}
163+
164+
return out;
151165
}
152166

153-
Tensor floor_divide(const Tensor& input, Scalar other) {
154-
Tensor out = input / other;
155-
return truncate(out);
167+
Tensor& floor_divide_(Tensor& self, const Tensor& other) {
168+
return native::floor_divide_out(self, self, other);
156169
}
157170

158171
Tensor& mul_out(Tensor& result, const Tensor& self, const Tensor& other) {
@@ -675,6 +688,14 @@ Tensor min(const Tensor& self, const Tensor& other) {
675688

676689
Tensor& min_(Tensor& self, const Tensor& other) { return at::min_out(self, self, other); }
677690

691+
Tensor floor_divide(const Tensor& self, Scalar other) {
692+
return at::floor_divide(self, wrapped_scalar_tensor(other));
693+
}
694+
695+
Tensor& floor_divide_(Tensor& self, Scalar other) {
696+
return at::floor_divide_out(self, self, wrapped_scalar_tensor(other));
697+
}
698+
678699
Tensor& fmod_out(Tensor & result, const Tensor& self, const Tensor& other) {
679700
auto iter = TensorIterator::binary_op(result, self, other,
680701
/*check_mem_overlap=*/true);

aten/src/ATen/native/native_functions.yaml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,10 +1306,38 @@
13061306
CPU: floor_out
13071307
CUDA: floor_out
13081308

1309-
- func: floor_divide(Tensor input, Tensor other) -> Tensor
1309+
- func: floor_divide(Tensor self, Tensor other) -> Tensor
1310+
variants: function, method
1311+
dispatch:
1312+
CPU: floor_divide
1313+
CUDA: floor_divide
1314+
SparseCPU: floor_divide_sparse
1315+
SparseCUDA: floor_divide_sparse
13101316
supports_named_tensor: True
13111317

1312-
- func: floor_divide.Scalar(Tensor input, Scalar other) -> Tensor
1318+
- func: floor_divide_.Tensor(Tensor(a!) self, Tensor other) -> Tensor(a!)
1319+
variants: method
1320+
dispatch:
1321+
CPU: floor_divide_
1322+
CUDA: floor_divide_
1323+
SparseCPU: floor_divide_sparse_
1324+
SparseCUDA: floor_divide_sparse_
1325+
supports_named_tensor: True
1326+
1327+
- func: floor_divide.out(Tensor self, Tensor other, *, Tensor(a!) out) -> Tensor(a!)
1328+
dispatch:
1329+
CPU: floor_divide_out
1330+
CUDA: floor_divide_out
1331+
SparseCPU: floor_divide_out_sparse_zerodim
1332+
SparseCUDA: floor_divide_out_sparse_zerodim
1333+
supports_named_tensor: True
1334+
1335+
- func: floor_divide.Scalar(Tensor self, Scalar other) -> Tensor
1336+
variants: function, method
1337+
supports_named_tensor: True
1338+
1339+
- func: floor_divide_.Scalar(Tensor(a!) self, Scalar other) -> Tensor(a!)
1340+
variants: method
13131341
supports_named_tensor: True
13141342

13151343
- func: frac(Tensor self) -> Tensor

0 commit comments

Comments
 (0)