Skip to content
This repository was archived by the owner on Jul 1, 2025. It is now read-only.

[New Operator] Implement Mod operator#2345

Merged
jackm321 merged 5 commits intopytorch:masterfrom
jackm321:implement_mod
Feb 5, 2019
Merged

[New Operator] Implement Mod operator#2345
jackm321 merged 5 commits intopytorch:masterfrom
jackm321:implement_mod

Conversation

@jackm321
Copy link
Copy Markdown
Contributor

@jackm321 jackm321 commented Feb 5, 2019

Description:
Implements caffe2 Mod operator.
I chose to call the IR node/instruction Modulo instead of Mod to avoid confusion with Modules which are referred to throughout the codebase as mod.

Testing:
ninja test

Documentation:
doxygen

Fixes #2330

@jackm321 jackm321 force-pushed the implement_mod branch 2 times, most recently from 5b1912d to 8eb384f Compare February 5, 2019 02:13
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.

Let's simulate Caffe2 behavior. This implementation diverges at least when res == 0.
https://github.com/pytorch/pytorch/blob/1409a2afc8d81d911dbcfc2d0b210cf62658237b/caffe2/operators/mod_op.cc#L20

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@artemrakhov this implementation is equivalent to the code you linked and skips the unnecessary divisor > 0 check.

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.

I don't agree that it is equivalent. They never touch 0. But this code adds divisor to 0, making result of 0 % 3 to be 3. And the unit test #2 that you've added confirms this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah you're right about the output_ptr[i] check, I'll add that to this too

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.

from the doc: Input tensor with int32 or int64 data.
This will fail in debug mode if tensor of int32 data.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. Do we have an existing solution for this? Looks like dispatchFloatingPointImpl, dispatchQuantizedImpl, and dispatchQuantizedWithAccumulationImpl don't quite fit this.

Copy link
Copy Markdown
Contributor

@rdzhabarov rdzhabarov Feb 5, 2019

Choose a reason for hiding this comment

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

Yeah, Int32ITy and Int64ITy are indexed types, not quantized types. So, you'd need to add something similar.

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.

I don't agree that it is equivalent. They never touch 0. But this code adds divisor to 0, making result of 0 % 3 to be 3. And the unit test #2 that you've added confirms this.

@nadavrot
Copy link
Copy Markdown
Contributor

nadavrot commented Feb 5, 2019

@jackm321 @artemrakhov @artemrakhov Is there a reason why we are not templating all of the binary operators? Is there a difference between Mul, Sub, Add except to the one character that specifies the op?

@artemrakhov-glow
Copy link
Copy Markdown
Contributor

@nadavrot: This op is different, because of signFollowDivisor option. @jackm321 also suggests that it's not a binary op, because divisor is constant.

We do have a lot of templates around Add, Mul, Sub, Div. Quantization formulas are different, that's why it's not one character difference. I admit we could use more templates for these.

@jackm321
Copy link
Copy Markdown
Contributor Author

jackm321 commented Feb 5, 2019

@rdzhabarov is there any reason not to merge dispatchFloatingPointImpl, dispatchIndexTypeImpl, and dispatchQuantizedImpl? If not I think we should do that. I can create task for it.

@rdzhabarov
Copy link
Copy Markdown
Contributor

is there any reason not to merge dispatchFloatingPointImpl, dispatchIndexTypeImpl, and dispatchQuantizedImpl? If not I think we should do that. I can create task for it.

For many cases (ops) there are no quantized impl for a certain op. I think it makes sense to keep them separately.

Copy link
Copy Markdown
Contributor

@rdzhabarov rdzhabarov left a comment

Choose a reason for hiding this comment

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

Looks good from my side.

int64_t divisor;
ASSIGN_VALUE_OR_RETURN_ERR(divisor, loadInt(dict["divisor"]));

RETURN_ERR_IF_NOT(divisor >= 1, "Divisor must be positive.");
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.

not less than 1?

@jackm321
Copy link
Copy Markdown
Contributor Author

jackm321 commented Feb 5, 2019

@artemrakhov good to go?

loadInt(dict["sign_follow_divisor"]));
}

auto *node = G_.createModulo(opName, in, divisor, signFollowDivisor);
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.

At this point divisor is silently converted from 64-bit to 32-bit. Is it intentional? Caffe2 uses int64_t

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

Copy link
Copy Markdown
Contributor

@artemrakhov-glow artemrakhov-glow left a comment

Choose a reason for hiding this comment

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

LGTM

@jackm321
Copy link
Copy Markdown
Contributor Author

jackm321 commented Feb 5, 2019

Thanks @rdzhabarov and @artemrakhov for reviewing!

@jackm321 jackm321 merged commit 53eb17b into pytorch:master Feb 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants