-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified #29632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…:tensor(empty braced-init-list) when dtype is not specified
CircleCI build failures summaryAs of commit 16137e2:
Here are the reasons each build failed:
This comment was automatically generated by Dr. CI. Please report bugs/suggestions on the GitHub issue tracker. This comment has been revised 30 time(s). |
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
| return count; | ||
| } | ||
|
|
||
| // A RAII, thread local (!) guard that changes default dtype upon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checking, torch::set_default_dtype changes the default dtype locally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems global: https://github.com/pytorch/pytorch/blob/master/c10/core/DefaultDtype.cpp. Orthogonal to this PR, does this need synchronization?
EDIT: Do people design global guards like this in general? I'm only really familiar with pytorch's use of thread-local guards.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I totally did not realize this was in test code. This is fine in testing code, I was wondering if it was OK as an API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the catch - I think this AutoDefaultDtypeMode guard in its original form was not safe to use if we ever run C++ tests in parallel in multiple threads, since a test in thread A might not have finished when another test in thread B changes the default dtype using torch::set_default_dtype. To address this problem, I made the following two changes:
- Add
static std::mutex default_dtype_mutextoAutoDefaultDtypeMode, so that usage ofAutoDefaultDtypeModeis synchronized and thus always thread-safe. - Change other C++ tests that have
set_default_dtype(...)to always useAutoDefaultDtypeModeto guard the whole scope of the test, so that access toset_default_dtype(...)is effectively synchronized within the C++ test suite. (It won't protect us against running a C++ test while changing default dtype through Python within the same process, but such usage seems more intentional than accidental, and the author of that test mechanism should be responsible for making sure such usage is sane.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the AutoDefaultDtypeMode guard is pretty ad-hoc - for now it's only used for the C++ tests, and its RAII mechanism should work :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does gtest run tests in parallel / is it able to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think gtest by itself can run tests in parallel (https://cuhkszlib-xiaoxing.readthedocs.io/en/latest/external/gtest/googletest/docs/FAQ.html#does-google-test-support-running-tests-in-parallel), but there are some test runners that can make it work: https://github.com/google/gtest-parallel. Whether to make them run in parallel is likely out of scope for this PR though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the design of the tests (they only run single-threaded), it might be better to simplify the data structure by removing the mutex and writing a quick comment that this isn't thread-safe and that it doesn't matter because we only use one thread
|
Does it make sense to use the default dtype when the user does |
On second thought, python floats also have double-precision and we use the default dtype for them. |
| // When `scalar_type == at::kDouble`, we know that the user is passing in | ||
| // a floating-point literal without specifying its type (e.g. `1.0` instead of `1.0f`). | ||
| // In Python, the dtype of `torch.tensor(1.0)` depends on the value of | ||
| // `torch.get_default_dtype()`, and we should do the same for C++ `torch::tensor(1.0)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be nice to have a parity test for this behavior. i.e., if we ever change the default tensor type behavior then we know to change it in both APIs. I don't see this changing anytime soon though so this is more of a nit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed - I will add it in a follow-up PR to improve the parity test mechanism.
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
… and torch::tensor(empty braced-init-list) when dtype is not specified" Use default dtype for torch::tensor(floating_point_values) and torch::tensor(empty braced-init-list) when dtype is not specified gh-metadata: pytorch pytorch 29632 gh/yf225/66/head
zou3519
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. I think it would be nice to remove the mutex to simplify the logic (since our tests are single-threaded anyways and probably won't change anytime soon) but let me know your thoughts
| return count; | ||
| } | ||
|
|
||
| // A RAII, thread local (!) guard that changes default dtype upon |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the design of the tests (they only run single-threaded), it might be better to simplify the data structure by removing the mutex and writing a quick comment that this isn't thread-safe and that it doesn't matter because we only use one thread
Thanks! I feel that it's probably worth keeping the mutex because changing the global state back and forth in the tests does seems a bit worrying to me, and it might cause hard-to-debug flaky tests if we ever run them in parallel without the mutex. |
That's reasonable, let's keep it then. |
Stack from ghstack:
This PR is BC-breaking in the following way:
Previously, C++
torch::tensorwith an integer type (e.g.torch::tensor(1)) or a (nested) braced-init-list of integer types (e.g.torch::tensor({{1, 2}})) produces a tensor with the same dtype. Now it always produces a tensor of dtypetorch::kLong(aka. int64_t). matching Pythontorch.tensorbehavior.Differential Revision: D18465819