diff options
author | Janusz Kudelka <janusz@fb.com> | 2018-03-26 11:05:32 -0700 |
---|---|---|
committer | Lu Fang <30275821+houseroad@users.noreply.github.com> | 2018-03-27 18:10:39 -0700 |
commit | a0a136117c35cc46d442a83a4662bfaabaac5aea (patch) | |
tree | d1644b759bc648a4d5f3326becb9abb7e7854c59 /caffe2/operators | |
parent | 16312e81238d2e4fbba00ed42d8af4a2b7e5f605 (diff) | |
download | pytorch-a0a136117c35cc46d442a83a4662bfaabaac5aea.tar.gz pytorch-a0a136117c35cc46d442a83a4662bfaabaac5aea.tar.bz2 pytorch-a0a136117c35cc46d442a83a4662bfaabaac5aea.zip |
Faster positive modulo in IndexHashOp
Change the positive modulo computation to use less modulo. This should
run ~2x faster (just the modulo part). In addition, we should later switch to
compute reciprocal modulo.
Diffstat (limited to 'caffe2/operators')
-rw-r--r-- | caffe2/operators/index_hash_ops.h | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/caffe2/operators/index_hash_ops.h b/caffe2/operators/index_hash_ops.h index a43fadf5e2..e77fdb4de2 100644 --- a/caffe2/operators/index_hash_ops.h +++ b/caffe2/operators/index_hash_ops.h @@ -53,8 +53,10 @@ class IndexHashOp : public Operator<Context> { for (int i = 0; i < sizeof(T) / sizeof(int8_t); i++) { hashed = hashed * 65537 + bytes[i]; } - hashed = static_cast<T>((modulo_ + hashed % modulo_) % modulo_); - return hashed; + // We want the result of the modulo to be positive. This works under the + // assumption that modulo_ > 0 which is enforced in the constructor. + auto modHashed = hashed % modulo_; + return modHashed >= 0 ? modHashed : modHashed + modulo_; } private: |