TorchSharp icon indicating copy to clipboard operation
TorchSharp copied to clipboard

Multithreading safety

Open gevorgter opened this issue 1 year ago • 4 comments

Great library but please provide some information on multi threading safety. In production use, models are normally wrapped into REST service which is inherently multithreaded. The Torch itself is safe to use for inference in that matter. Not sure if your library keeping it this way. Major concern is introduction of scopes for memory management. Stack of scopes seems to me existing on global level which can be a problem. Not sure what else.

gevorgter avatar Feb 27 '23 17:02 gevorgter

Dispose scopes are thread local: https://github.com/dotnet/TorchSharp/blob/6bc4574cc94a244a318f22ee0244addd9d4d94f2/src/TorchSharp/DisposeScopeManager.cs#L17

The only thing to care about here is that thread local actually does not work with async.

For the latter, we might just need to switch to AsyncLocal<T>. I believe it does work with both threads and async flows.

lostmsu avatar Feb 27 '23 18:02 lostmsu

For the latter, we might just need to switch to AsyncLocal<T>. I believe it does work with both threads and async flows.

As always an excellent point, @lostmsu. The current solution accommodates thread parallelism, but not task-based concurrency.

NiklasGustafsson avatar Feb 28 '23 21:02 NiklasGustafsson

The Torch itself is safe to use for inference in that matter.

@gevorgter -- I have always wondered where, in all of the torch documentation, that safety is formalized into a guarantee. Do you have a reference?

NiklasGustafsson avatar Feb 28 '23 21:02 NiklasGustafsson

Sorry, no reference. But my hunch is that training might not be thread safe but inference is since there is no gradient to keep and it's just calculations.

Pytorch team appears to care about threading issues and fixing them. https://github.com/pytorch/pytorch/issues/16828

gevorgter avatar Mar 01 '23 04:03 gevorgter