Skip to content

Commit

Permalink
Switched from parameter in can_cast to from_. (#126030)
Browse files Browse the repository at this point in the history
Fixes #126012.

`from` is a reserved keyword in Python, thus we can't make the C++ impl available with `from` as function parameter. This PR changes the name to `from_` and also adjusts the docs.

If we want to preserve backwards compatibility, we can leave the C++ name as-is and only fix the docs. However, `torch.can_cast(from_=torch.int, to=torch.int)` won't work then.

Pull Request resolved: #126030
Approved by: https://github.com/albanD
  • Loading branch information
tringwald authored and ZelboK committed May 19, 2024
1 parent db73a01 commit fed9d93
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions aten/src/ATen/native/TypeProperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ ScalarType result_type(const Scalar& scalar1, const Scalar& scalar2) {
return result_type(state);
}

bool can_cast(const at::ScalarType from, const at::ScalarType to) {
return at::canCast(from, to);
bool can_cast(const at::ScalarType from_, const at::ScalarType to) {
return at::canCast(from_, to);
}

ScalarType promote_types(ScalarType type1, ScalarType type2) {
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7702,7 +7702,7 @@

- func: result_type.Scalar_Scalar(Scalar scalar1, Scalar scalar2) -> ScalarType

- func: can_cast(ScalarType from, ScalarType to) -> bool
- func: can_cast(ScalarType from_, ScalarType to) -> bool
variants: function

- func: promote_types(ScalarType type1, ScalarType type2) -> ScalarType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
("aten::_aminmax.dim", datetime.date(2024, 12, 31)),
("aten::_aminmax.out", datetime.date(2024, 12, 31)),
("aten::_aminmax.dim_out", datetime.date(2024, 12, 31)),
# BC-breaking change in can_cast signature: 'from' -> 'from_'
("aten::can_cast", datetime.date(2024, 5, 31)),
]

ALLOW_LIST_COMPILED = [
Expand Down
4 changes: 2 additions & 2 deletions torch/_torch_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2195,13 +2195,13 @@ def merge_dicts(*dicts):
add_docstr(
torch.can_cast,
r"""
can_cast(from, to) -> bool
can_cast(from_, to) -> bool
Determines if a type conversion is allowed under PyTorch casting rules
described in the type promotion :ref:`documentation <type-promotion-doc>`.
Args:
from (dtype): The original :class:`torch.dtype`.
from\_ (dtype): The original :class:`torch.dtype`.
to (dtype): The target :class:`torch.dtype`.
Example::
Expand Down

0 comments on commit fed9d93

Please sign in to comment.