Skip to content
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

Closed
wants to merge 9 commits into from
Closed

Conversation

ssnl
Copy link
Collaborator

@ssnl ssnl commented Apr 27, 2020

Fixes #32079

@dr-ci
Copy link

dr-ci bot commented Apr 27, 2020

💊 CI failures summary and remediations

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


  • 1/1 failures possibly* introduced in this PR
    • 1/1 non-CircleCI failure(s)

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 on the GitHub issue tracker.

See how this bot performed.

This comment has been revised 64 times.

@ssnl ssnl force-pushed the device_parse_error branch from f20a7d2 to 8aa8382 Compare April 27, 2020 22:35
@ssnl ssnl force-pushed the device_parse_error branch 5 times, most recently from fa6365c to 12acd19 Compare April 28, 2020 05:04
@ssnl
Copy link
Collaborator Author

ssnl commented May 3, 2020

@zou3519 Maybe you could take a look when you have time? :) Thanks!

@zou3519 zou3519 self-requested a review May 4, 2020 13:56
@@ -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;
Copy link
Contributor

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?

// `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());
// }
. It looks like the minimum version of gcc we support is 5 so this should work now.

Copy link
Collaborator Author

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.

Copy link
Contributor

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

using regex now!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

friendly ping :) @zou3519

Copy link
Contributor

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

@ngimel ngimel added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label May 5, 2020
@ssnl ssnl force-pushed the device_parse_error branch 2 times, most recently from 779d023 to abb6b71 Compare May 5, 2020 18:44
Copy link
Contributor

@zou3519 zou3519 left a 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?

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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 ssnl force-pushed the device_parse_error branch from 01f09c0 to 4969d75 Compare May 11, 2020 13:42
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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 ssnl force-pushed the device_parse_error branch from 4969d75 to c13b8bd Compare May 11, 2020 13:46
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

@zou3519
Copy link
Contributor

zou3519 commented May 12, 2020

@ssnl do you mind if I rebase this? There seems to be an internal merge conflict

@ssnl ssnl force-pushed the device_parse_error branch from c13b8bd to cb72d55 Compare May 12, 2020 17:28
@ssnl
Copy link
Collaborator Author

ssnl commented May 12, 2020 via email

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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");
Copy link
Contributor

@zou3519 zou3519 May 13, 2020

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

Copy link
Collaborator Author

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.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a 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.

@facebook-github-bot
Copy link
Contributor

@zou3519 merged this pull request in ae392a7.

@ssnl ssnl deleted the device_parse_error branch May 14, 2020 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Merged open source triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging this pull request may close these issues.

torch.device parsing does not do enough checks
6 participants