-
Notifications
You must be signed in to change notification settings - Fork 23.5k
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
Add better device idx parse checks #37376
Conversation
💊 CI failures summary and remediationsAs of commit 696a87c (more details on the Dr. CI page):
ci.pytorch.org: 1 failedThis 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. This comment has been revised 64 times. |
f20a7d2
to
8aa8382
Compare
fa6365c
to
12acd19
Compare
@zou3519 Maybe you could take a look when you have time? :) Thanks! |
c10/core/Device.cpp
Outdated
@@ -73,12 +73,17 @@ Device::Device(const std::string& device_string) : Device(Type::CPU) { | |||
type_ = parse_type(s); | |||
|
|||
std::string device_index = device_string.substr(index + 1); | |||
std::size_t pos = 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.
@ssnl would it be more elegant to do this with std::regex?
Lines 41 to 63 in 5ec87a3
// `std::regex` is still in a very incomplete state in GCC 4.8.x, | |
// so we have to do our own parsing, like peasants. | |
// https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions | |
// | |
// Replace with the following code once we shed our GCC skin: | |
// | |
// static const std::regex regex( | |
// "(cuda|cpu)|(cuda|cpu):([0-9]+)|([0-9]+)", | |
// std::regex_constants::basic); | |
// std::smatch match; | |
// const bool ok = std::regex_match(device_string, match, regex); | |
// TORCH_CHECK(ok, "Invalid device string: '", device_string, "'"); | |
// if (match[1].matched) { | |
// type_ = parse_type_from_string(match[1].str()); | |
// } else { | |
// if (match[2].matched) { | |
// type_ = parse_type_from_string(match[1].str()); | |
// } else { | |
// type_ = Type::CUDA; | |
// } | |
// AT_ASSERT(match[3].matched); | |
// index_ = std::stoi(match[3].str()); | |
// } |
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.
To make regex work, we would have to account for future/other device types (ROCM?, etc.). I wasn't sure how to do that properly... So I kept stoi
.
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.
You could do something with regex groups, like the following so that we don't hardcode cpu & cuda:
#include <iostream>
#include <regex>
int main()
{
static const std::regex with_idx_regex("([a-zA-Z_]+):([0-9]+)");
static const std::regex without_idx_regex("([a-zA-Z_]+)");
std::smatch match;
const std::string device_string = "cuda:1";
const bool ok = std::regex_match(device_string, match, with_idx_regex);
if (ok) {
std::cout << "device: " << match[1].str() << std::endl;
std::cout << "idx: " << match[2].str() << std::endl;
}
}
The regex-based approach will probably have different error messages from the current parsing based approach, but it's a lot nicer than having to maintain our own parser for this
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.
using regex now!
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.
friendly ping :) @zou3519
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! I'll take a look by eod tomorrow
779d023
to
abb6b71
Compare
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.
lgtm. Could you add an extra test for some two digit device numbers?
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.
@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
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.
@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
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.
@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
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.
@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
@ssnl do you mind if I rebase this? There seems to be an internal merge conflict |
Done!
…On Tue, May 12, 2020 at 13:27 Richard Zou ***@***.***> wrote:
@ssnl <https://github.com/SsnL> do you mind if I rebase this? There seems
to be an internal merge conflict
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#37376 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABLJMZPI3MUZ6XB2ON4A7V3RRGBI7ANCNFSM4MSJ43FA>
.
|
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.
@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
// To mimic `std::stoi` and to avoid including `Exception.h`, throw | ||
// `std::invalid_argument`. | ||
// We can't easily detect out-of-range, so we don't use `std::out_of_range`. | ||
throw std::invalid_argument("Not an integer"); |
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'm getting "error: no member named 'invalid_argument' in namespace 'std'
throw std::invalid_argument("Not an integer");" on some of the internal builds. As far as I can tell we don't need to throw nice errors here, so do you think it would be fine to remove these?
Alternatively we could try to include <stdexcept>
but I'm not really sure what the downstream ramifications of that are
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.
Let's try including stdexcept
! Without this check stoi(empty_string)
would succeed.
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.
@zou3519 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
Fixes #32079