Skip to content

MAINT: work around arg{min,max} not implemented for booleans #63

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

Merged
merged 1 commit into from
Feb 23, 2023

Conversation

ev-br
Copy link
Collaborator

@ev-br ev-br commented Feb 22, 2023

No description provided.

@ev-br ev-br requested a review from lezcano February 22, 2023 17:46

if tensor.dtype == torch.bool:
# RuntimeError: "argmax_cpu" not implemented for 'Bool'
tensor = tensor.view(torch.int8)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is the equivalence guaranteed in pytorch?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I would cast it to uint8 to be 100% sure, but yeah, I believe it is guaranteed by some work @peterbell10 did some time ago.

Copy link
Contributor

Choose a reason for hiding this comment

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

A cast is guaranteed to be 0 or 1, but a view could contain any byte value.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh and of course the work I did doesn't apply to the torch.compile stack.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case, if viewing with uint8 we would get something that's non zero, so argmax would then work, but sure, let's do to, as the compiler should be able to optimise out the copy really

Copy link
Contributor

Choose a reason for hiding this comment

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

IIRC argmax always returns the lowest index that's equal to the maximum, so the distinction does matter.

Copy link
Collaborator

@lezcano lezcano left a comment

Choose a reason for hiding this comment

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

Approving to unblock, but I believe it'd be better to cast it to uint8 to be 100% sure.

@ev-br
Copy link
Collaborator Author

ev-br commented Feb 22, 2023

By cast you mean tensor.to or tensor.view?

@lezcano
Copy link
Collaborator

lezcano commented Feb 22, 2023

view is fine.

@ev-br
Copy link
Collaborator Author

ev-br commented Feb 22, 2023

Based on #63 (comment) and #63 (comment) let's copy to uint8 to be on the safe side.

@ev-br ev-br merged commit 478df33 into main Feb 23, 2023
@ev-br ev-br deleted the argminmax_bool branch February 23, 2023 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants