mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Self defined Operator

Open feiyulv opened this issue 10 years ago • 7 comments

I implement a self defined loss operator. Some kernel functions can't be implemented using mshadow, so I use self defined kernel functions in .cu file. The problem is the result is not deterministic. However when I change the MXNET_ENGINE_TYPE to NaiveEngine, the results of different times are the same. I guess it is some synchoronize problem of my kernel. So I add cudaThreadSynchronize before the kernel and cudaStreamSynchronize after the kenrel, as the following presudo code shows . But the result is still not deterministic.

Stream *s = ctx.get_stream(); cudaDeviceSynchronize(); ker_xxx<<<grid, block, 0, s->stream>>>(); cudaStreamSynchronize(s->stream);

feiyulv avatar Jun 07 '16 01:06 feiyulv

non deterministic as in random error or slight difference between runs?

piiswrong avatar Jun 07 '16 01:06 piiswrong

@piiswrong slight difference between runs. Using NaiveEngine won't have this problem, so it may be the problem of synchronization

feiyulv avatar Jun 07 '16 02:06 feiyulv

Are you sure the diff is from this layer? Is it's input deterministic?

piiswrong avatar Jun 07 '16 02:06 piiswrong

@piiswrong Yes, I replaced the self define loss layer with softmaxoutput, the results between runs are the same. What't more, if the input is non deterministic, I won't get the same value with NaiveEngine

feiyulv avatar Jun 07 '16 02:06 feiyulv

can you verify that the input to your loss layer is the same by binding it as a additional output? If it's indeed the same. Try a simpler kernel to see which operation in your kernel is causing this.

piiswrong avatar Jun 07 '16 02:06 piiswrong

The input to the loss layer is definitely the same. I extract the self-defined kernels codes, and do a unit-test, they have no problem. I find two ways to eliminate this no deterministic phenomenon. The First is as I mentioned above, using the naive engine. Another way is setting the MXNET_GPU_WORKER_NTHREADS to 1. I still can't figure out why it happens. Is there any thing should be noticed when writing a self defined kernel?

feiyulv avatar Jun 08 '16 09:06 feiyulv

This issue is closed due to lack of activity in the last 90 days. Feel free to reopen if this is still an active issue. Thanks!

phunterlau avatar Sep 28 '17 05:09 phunterlau