Dont know a smooth plug and play implementation, but if you had to do this yourself , you can put the particular code in a try except block, and if the error is of the particular type that cuda gives when crashing, you could reduce the batch size. although this way you will not know if ram has been freed up and you can increase the batch size again. maybe you ought to try and increase the batch size every few iterations, and if there is a seg fault revert it back again.