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

fix custom op fork test #14753

Merged
merged 2 commits into from
Apr 22, 2019
Merged

fix custom op fork test #14753

merged 2 commits into from
Apr 22, 2019

Conversation

arcadiaphy
Copy link
Member

@arcadiaphy arcadiaphy commented Apr 21, 2019

Description

The custom op fork test introduced in #14451 will cause error when running with gpu. The common situation is:

test_operator_gpu.test_custom_op ... terminate called after throwing an instance of 'dmlc::Error'

  what():  [04:22:53] /work/mxnet/3rdparty/mshadow/mshadow/./tensor_gpu-inl.h:35: Check failed: e == cudaSuccess: CUDA: initialization error

Stack trace:

  [bt] (0) /work/mxnet/python/mxnet/../../lib/libmxnet.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x35) [0x7f8ca52e2bd5]

  [bt] (1) /work/mxnet/python/mxnet/../../lib/libmxnet.so(void mshadow::SetDevice<mshadow::gpu>(int)+0x11a) [0x7f8ca8775a2a]

  [bt] (2) /work/mxnet/python/mxnet/../../lib/libmxnet.so(void mxnet::engine::ThreadedEnginePerDevice::GPUWorker<(dmlc::ConcurrentQueueType)1>(mxnet::Context, bool, mxnet::engine::ThreadedEnginePerDevice::ThreadWorkerBlock<(dmlc::ConcurrentQueueType)1>*, std::shared_ptr<dmlc::ManualEvent> const&)+0xa2) [0x7f8ca87a3d92]

  [bt] (3) /work/mxnet/python/mxnet/../../lib/libmxnet.so(std::_Function_handler<void (std::shared_ptr<dmlc::ManualEvent>), mxnet::engine::ThreadedEnginePerDevice::PushToExecute(mxnet::engine::OprBlock*, bool)::{lambda()#2}::operator()() const::{lambda(std::shared_ptr<dmlc::ManualEvent>)#1}>::_M_invoke(std::_Any_data const&, std::shared_ptr<dmlc::ManualEvent>)+0x4e) [0x7f8ca87a43de]

  [bt] (4) /work/mxnet/python/mxnet/../../lib/libmxnet.so(std::thread::_Impl<std::_Bind_simple<std::function<void (std::shared_ptr<dmlc::ManualEvent>)> (std::shared_ptr<dmlc::ManualEvent>)> >::_M_run()+0x5c) [0x7f8ca878626c]

  [bt] (5) /usr/lib64/libstdc++.so.6(+0xb5070) [0x7f8d0b256070]

  [bt] (6) /usr/lib64/libpthread.so.0(+0x7dd5) [0x7f8d19461dd5]

  [bt] (7) /usr/lib64/libc.so.6(clone+0x6d) [0x7f8d18a81ead]

ok (0.6177s)

When CUDA context is created in main process, the forking process tries to access the same context, causing initialization error.

This PR adds checking on exitcode of forking process and removes this test in gpu tests.

BTW, right now the correct way to fork mxnet is to do it when the CUDA context is not created, otherwise CUDA error is very likely to happen, maybe we should add some warning in docs?

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

Changes

  • Feature1, tests, (and when applicable, API doc)
  • Feature2, tests, (and when applicable, API doc)

Comments

  • If this change is a backward incompatible change, why must this change be made.
  • Interesting edge cases to note here

@arcadiaphy arcadiaphy assigned arcadiaphy and unassigned arcadiaphy Apr 22, 2019
@arcadiaphy arcadiaphy requested a review from wkcn April 22, 2019 06:06
@wkcn
Copy link
Member

wkcn commented Apr 22, 2019

When CUDA context is created in main process, the forking process tries to access the same context, causing initialization error.

Is the reason that the CUDA context Is not shared across different processes?

Copy link
Member

@wkcn wkcn left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you for the fix!

Copy link
Member

@anirudh2290 anirudh2290 left a comment

Choose a reason for hiding this comment

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

Yes cuda doesn't work well with fork. @arcadiaphy this PR looks good to me . Feel free to add another PR to improve docs for forking. Thank you !

@anirudh2290 anirudh2290 merged commit 68efc15 into apache:master Apr 22, 2019
@arcadiaphy arcadiaphy deleted the pr_custom branch April 23, 2019 02:41
@arcadiaphy
Copy link
Member Author

@wkcn Naively sharing CUDA context across processes by forking will not work, I'm not sure if it's possible at all.

@wkcn
Copy link
Member

wkcn commented Apr 23, 2019

I found an answer about CUDA initialization error after folk
https://stackoverflow.com/questions/22950047/cuda-initialization-error-after-fork

@arcadiaphy
Copy link
Member Author

arcadiaphy commented Apr 23, 2019

@wkcn I've read this answer, it's possible to use separate device in forked process.

But it still doesn't work for different device in mxnet if the main process has created the context, perhaps some cleanup needs to be done in pthread_at_fork.

haohuanw pushed a commit to haohuanw/incubator-mxnet that referenced this pull request Jun 23, 2019
* fix custom op fork test

* trigger CI
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants