Skip to content

Add better device idx parse checks#37376

Closed
ssnl wants to merge 9 commits intopytorch:masterfrom
ssnl:device_parse_error
Closed

Add better device idx parse checks#37376
ssnl wants to merge 9 commits intopytorch:masterfrom
ssnl:device_parse_error

Conversation

@ssnl
Copy link
Copy Markdown
Collaborator

@ssnl ssnl commented Apr 27, 2020

Fixes #32079

@dr-ci
Copy link
Copy Markdown

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
Copy Markdown
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
Comment thread c10/core/Device.cpp Outdated
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

Comment thread c10/core/Device.cpp Outdated
Comment thread c10/core/Device.cpp Outdated
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Collaborator Author

ssnl commented May 12, 2020 via email

Copy link
Copy Markdown
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.

Comment thread c10/util/string_utils.h
// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor

@zou3519 merged this pull request in ae392a7.

@ssnl ssnl deleted the device_parse_error branch May 14, 2020 19:22
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
Summary:
Fixes pytorch#32079
Pull Request resolved: pytorch#37376

Differential Revision: D21476036

Pulled By: zou3519

fbshipit-source-id: 86907083c23cbaf165b645307fb340f2656b814e
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