Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-860] - Avoid implicit double conversions in math operations. #12361

Merged
merged 1 commit into from
Sep 18, 2018

Conversation

KellenSunderland
Copy link
Contributor

@KellenSunderland KellenSunderland commented Aug 26, 2018

Description

Avoid implicit double conversions in legacy math operations.

Checklist

Essentials

Please feel free to remove inapplicable items for your PR.

  • The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant JIRA issue created (except PRs with tiny changes)
  • Changes are complete (i.e. I finished coding on this PR)
  • All changes have test coverage:
  • Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
  • Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
  • Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
  • Code is well-documented:
  • For user-facing API changes, API doc string has been updated.
  • For new C++ functions in header files, their functionalities and arguments are documented.
  • For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
  • Check the API doc at http://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
  • To the my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change

Note: After investigating this warning I'm not sure it's always applicable, however using std::math_func does catch some cases where double promotions happen. It will also make sure we have uniform implementations across compilers that support the C++11 standard.

#include <cmath>

int main() {
    float input = 0.5f;
    return static_cast<int>(std::ceil(input));
}
main:
    push   rbp
    mov    rbp,rsp
    sub    rsp,0x20
    movss  xmm0,DWORD PTR [rip+0xbe]        # 400624 <_IO_stdin_used+0x4>
    movss  DWORD PTR [rbp-0x4],xmm0
    mov    eax,DWORD PTR [rbp-0x4]
    mov    DWORD PTR [rbp-0x14],eax
    movss  xmm0,DWORD PTR [rbp-0x14]
    call   400581 <std::ceil(float)>
    cvttss2si eax,xmm0
    leave
    ret

while

#include <stdlib.h>
#include <math.h>

int main() {
    float input = 0.5f;
    return static_cast<int>(ceil(input));
}

generates

main:
    push   rbp
    mov    rbp,rsp
    sub    rsp,0x10
    movss  xmm0,DWORD PTR [rip+0x9e]        # 400604 <_IO_stdin_used+0x4>
    movss  DWORD PTR [rbp-0x4],xmm0
    cvtss2sd xmm0,DWORD PTR [rbp-0x4]
    call   400440 <ceil@plt>
    cvttsd2si eax,xmm0
    leave
    ret

@KellenSunderland KellenSunderland changed the title WIP - Avoid implicit double conversions in legacy math operations. [WIP] [DO-NOT-MERGE] - Avoid implicit double conversions in legacy math operations. Aug 26, 2018
@KellenSunderland
Copy link
Contributor Author

I'm closing this one for the time being, since I don't actually see a perf improvement here.

@KellenSunderland KellenSunderland changed the title [WIP] [DO-NOT-MERGE] - Avoid implicit double conversions in legacy math operations. [WIP] [DO-NOT-MERGE] - Avoid implicit double conversions in math operations. Aug 26, 2018
@KellenSunderland
Copy link
Contributor Author

Ok I did a little more investigation and found some examples where we're indeed promoting to double.

@KellenSunderland KellenSunderland force-pushed the math_fn_cpp11 branch 4 times, most recently from 576e4ac to a53dca6 Compare September 5, 2018 09:35
@KellenSunderland KellenSunderland changed the title [WIP] [DO-NOT-MERGE] - Avoid implicit double conversions in math operations. [MXNET-860] - Avoid implicit double conversions in math operations. Sep 5, 2018
@KellenSunderland
Copy link
Contributor Author

@mxnet-label-bot can please add [pr-awaiting-review]

This avoids some implicit conversions to double and also should unify
behaviour between C++11 compliant compilers.
@KellenSunderland
Copy link
Contributor Author

KellenSunderland commented Sep 18, 2018 via email

@marcoabreu marcoabreu merged commit 3c42447 into apache:master Sep 18, 2018
anirudh2290 pushed a commit to anirudh2290/mxnet that referenced this pull request Sep 19, 2018
This avoids some implicit conversions to double and also should unify
behaviour between C++11 compliant compilers.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pr-awaiting-review PR is waiting for code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants