Skip to content

Add type-safe, const-safe loops to c10#46414

Closed
r-barnes wants to merge 1 commit intopytorch:masterfrom
r-barnes:export-D24334732
Closed

Add type-safe, const-safe loops to c10#46414
r-barnes wants to merge 1 commit intopytorch:masterfrom
r-barnes:export-D24334732

Conversation

@r-barnes
Copy link
Copy Markdown
Contributor

@r-barnes r-barnes commented Oct 15, 2020

Pull Request resolved: #46414

For loops are often written with mismatched data types which causes silent type and sign coercion in the absence of integer conversion warnings. Getting around this in templated code requires convoluted patterns such as

for(auto i=decltype(var){0};i<var;i++)

(the above pattern appears ~31 times in the code base) with this diff we can instead write

for(const auto i = c10::ranges::indices(var))

Note that this loop is type-safe and const-safe. (Type-unsafe loops occur an unknown number of times do to the lack of conversion warnings on compilation.)

The functions introduced here (c10::ranges::ints and c10::ranges::indices) allow for type-safety and const-ness within for loops, which prevents the accidental truncation or modification of integers and other types, improving code safety. Further, they use the same naming convention as the ranges-v3 library and offer an easy migration path to C++20 functionality.

Test Plan:

buck run //caffe2/c10:c10_test_0

Differential Revision: D24334732

@facebook-github-bot
Copy link
Copy Markdown
Contributor

💊 CI failures summary and remediations

As of commit 332349eab5 (more details on the Dr. CI page):


Commit 332349eab5 was recently pushed. Waiting for builds...


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 1 times.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D24334732

@dr-ci
Copy link
Copy Markdown

dr-ci Bot commented Oct 15, 2020

💊 CI failures summary and remediations

As of commit e7aa64d (more details on the Dr. CI page):



❄️ 1 failure tentatively classified as flaky

but reruns have not yet been triggered to confirm:

See CircleCI build pytorch_linux_xenial_cuda10_2_cudnn7_py3_gcc7_test2 (1/1)

Step: "Run tests" (full log | diagnosis details | 🔁 rerun) ❄️

Jan 14 06:42:21 RuntimeError: CUDA error: an illegal memory access was encountered
Jan 14 06:42:21                        ~~~~ <--- HERE
Jan 14 06:42:21 RuntimeError: CUDA error: an illegal memory access was encountered
Jan 14 06:42:21 
Jan 14 06:42:21 
Jan 14 06:42:21 ======================================================================
Jan 14 06:42:21 ERROR [0.203s]: test_where_and_typing (__main__.TestTEFuser)
Jan 14 06:42:21 ----------------------------------------------------------------------
Jan 14 06:42:21 Traceback (most recent call last):
Jan 14 06:42:21   File "test_jit_fuser_te.py", line 1142, in test_where_and_typing
Jan 14 06:42:21     x = torch.randn(4, 4, dtype=torch.double, device=device)
Jan 14 06:42:21 RuntimeError: CUDA error: an illegal memory access was encountered
Jan 14 06:42:21 
Jan 14 06:42:21 ======================================================================
Jan 14 06:42:21 ERROR [0.176s]: test_zero_element_tensors_cuda (__main__.TestTEFuser)
Jan 14 06:42:21 ----------------------------------------------------------------------
Jan 14 06:42:21 Traceback (most recent call last):
Jan 14 06:42:21   File "/opt/conda/lib/python3.6/site-packages/torch/testing/_internal/common_utils.py", line 888, in wrapper
Jan 14 06:42:21     method(*args, **kwargs)
Jan 14 06:42:21   File "test_jit_fuser_te.py", line 178, in test_zero_element_tensors_cuda
Jan 14 06:42:21     self._test_zero_element_tensors(device="cuda")
Jan 14 06:42:21   File "test_jit_fuser_te.py", line 174, in _test_zero_element_tensors

ci.pytorch.org: 1 failed


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions to the (internal) Dr. CI Users group.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D24334732

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D24334732

@dzhulgakov dzhulgakov requested review from ezyang and smessmer October 19, 2020 21:39
@ezyang ezyang removed their request for review October 20, 2020 17:18
@ezyang
Copy link
Copy Markdown
Contributor

ezyang commented Oct 20, 2020

I'm going to let @smessmer make the call on this one. This seems to be a Boost-ism, not part of upcoming C++ standard library.

@r-barnes
Copy link
Copy Markdown
Contributor Author

That's correct, @ezyang, C++20 or prior doesn't, as far as I can tell, have a similar functionality. I'll edit the commit message to include:

irange allows for type-safety and const-ness within for loops, which prevents the accidental truncation of integers and other types, improving code safety.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D24334732

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D24334732

@r-barnes r-barnes changed the title Move irange to c10 Add type-safe, const-safe loops to c10 Dec 2, 2020
@r-barnes
Copy link
Copy Markdown
Contributor Author

r-barnes commented Dec 2, 2020

@ezyang or @smessmer : It seems like review on this has stalled, so I've simplified the diff. I've also expanded my comments on the rationale.

@ezyang ezyang requested a review from swolchok December 2, 2020 18:08
@r-barnes
Copy link
Copy Markdown
Contributor Author

#50484 , #50485 , #50482 , #50486 all fix compiler warnings and potential integer overflow/wrap issues that this PR would prevent.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D24334732

@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 13, 2021

Codecov Report

Merging #46414 (0389708) into master (05542f6) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master   #46414   +/-   ##
=======================================
  Coverage   80.64%   80.64%           
=======================================
  Files        1907     1907           
  Lines      206825   206828    +3     
=======================================
+ Hits       166791   166799    +8     
+ Misses      40034    40029    -5     

Copy link
Copy Markdown
Collaborator

@ngimel ngimel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how to make it performance-neutral compared to (also type-safe) loops that are written with decltype. TORCH_CHECK is known to prevent inlining, and probably it would have to be added to an upper-limit variant too.

Comment thread c10/util/irange.h Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is adding a TORCH_CHECK and might affect performance of hot paths

Comment thread c10/util/irange.h Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if end is negative? Can you add a test for this?

Summary:
Pull Request resolved: pytorch#46414

For loops are often written with mismatched data types which causes silent type and sign coercion in the absence of integer conversion warnings. Getting around this in templated code requires convoluted patterns such as
```
for(auto i=decltype(var){0};i<var;i++)
```
with this diff we can instead write
```
for(const auto i = c10::irange(var))
```
Note that this loop is type-safe and const-safe.

The function introduced here (`c10::irange`) allows for type-safety and const-ness within for loops, which prevents the accidental truncation or modification of integers and other types, improving code safety.

Test Plan:
```
buck test //caffe2/c10:c10_test_0
```

Differential Revision: D24334732

fbshipit-source-id: 595d03de995288da83d0e4325e269511a7f122f7
@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request was exported from Phabricator. Differential Revision: D24334732

@ngimel
Copy link
Copy Markdown
Collaborator

ngimel commented Jan 14, 2021

For the record, here's godbolt for irange compared to regular for loop, there's an extra cmovsq: https://godbolt.org/z/3o1968

@facebook-github-bot
Copy link
Copy Markdown
Contributor

This pull request has been merged in 8e74024.

@r-barnes r-barnes deleted the export-D24334732 branch March 18, 2021 20:06
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Pull Request resolved: pytorch#46414

For loops are often written with mismatched data types which causes silent type and sign coercion in the absence of integer conversion warnings. Getting around this in templated code requires convoluted patterns such as
```
for(auto i=decltype(var){0};i<var;i++)
```
with this diff we can instead write
```
for(const auto i = c10::irange(var))
```
Note that this loop is type-safe and const-safe.

The function introduced here (`c10::irange`) allows for type-safety and const-ness within for loops, which prevents the accidental truncation or modification of integers and other types, improving code safety.

Test Plan:
```
buck test //caffe2/c10:c10_test_0
```

Reviewed By: ngimel

Differential Revision: D24334732

fbshipit-source-id: fec5ebda3643ec5589f7ea3a8e7bbea4432ed771
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants