Skip to content

Commit

Permalink
Add _assert_scalar (pytorch#117378)
Browse files Browse the repository at this point in the history
Peeled off from pytorch#114148, because that PR is going to take a while to actually land.

Signed-off-by: Edward Z. Yang <[email protected]>
Pull Request resolved: pytorch#117378
Approved by: https://github.com/jansel
  • Loading branch information
ezyang authored and pytorchmergebot committed Jan 14, 2024
1 parent 1a85451 commit 003c900
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions aten/src/ATen/native/TensorCompare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <ATen/ops/_aminmax_native.h>
#include <ATen/ops/_assert_async_native.h>
#include <ATen/ops/_functional_assert_async_native.h>
#include <ATen/ops/_assert_scalar_native.h>
#include <ATen/ops/_functional_assert_scalar_native.h>
#include <ATen/ops/_make_per_tensor_quantized_tensor.h>
#include <ATen/ops/_unique.h>
#include <ATen/ops/allclose_native.h>
Expand Down Expand Up @@ -421,6 +423,15 @@ void _assert_async_msg_cpu(const Tensor& self, c10::string_view assert_msg) {
TORCH_CHECK(native::is_nonzero(self), assert_msg != "" ? assert_msg : "Assertion is failed");
}

void _assert_scalar(const Scalar& scalar, c10::string_view assert_msg) {
TORCH_SYM_CHECK(scalar.toSymBool(), assert_msg != "" ? assert_msg : "Assertion is failed");
}

Tensor _functional_assert_scalar(const Scalar& scalar, c10::string_view assert_msg, const Tensor& dep_token) {
_assert_scalar(scalar, assert_msg);
return dep_token.clone();
}

Tensor _functional_assert_async_msg_cpu(
const Tensor& self,
c10::string_view assert_msg,
Expand Down
8 changes: 8 additions & 0 deletions aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@
CPU: _assert_async_msg_cpu
CUDA: _assert_async_msg_cuda

- func: _assert_scalar(Scalar self, str assert_msg) -> ()
dispatch:
CompositeExplicitAutograd: _assert_scalar

- func: _functional_assert_scalar(Scalar self, str assert_msg, Tensor dep_token) -> Tensor
dispatch:
CompositeExplicitAutograd: _functional_assert_scalar

- func: _functional_assert_async.msg(Tensor self, str assert_msg, Tensor dep_token) -> Tensor
dispatch:
CPU: _functional_assert_async_msg_cpu
Expand Down
1 change: 1 addition & 0 deletions test/expect/HasDecompTest.test_has_decomposition.expect
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ aten::_foreach_zero
aten::_foreach_zero.out
aten::_foreach_zero_
aten::_functional_assert_async.msg
aten::_functional_assert_scalar
aten::_functional_sym_constrain_range
aten::_functional_sym_constrain_range_for_size
aten::_fused_adam
Expand Down
1 change: 1 addition & 0 deletions torch/fx/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
torch._assert,
torch._assert_async,
_ops.aten._assert_async.msg,
_ops.aten._assert_scalar.default,
_ops.aten.copy_.default,
_ops.aten.sym_constrain_range.default,
_ops.aten.sym_constrain_range_for_size.default,
Expand Down
1 change: 1 addition & 0 deletions torchgen/native_function_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"_assert_async", # no return
"_assert_async.msg", # no return
"_cslt_sparse_mm_search", # returns an int
"_assert_scalar", # no return
"_dimI", # returns an int
"_dimV", # returns an int
"_has_same_storage_numel", # returns a boolean
Expand Down

0 comments on commit 003c900

Please sign in to comment.