# Is it possible to capture all operators errors?

**URL:** <https://dev-discuss.pytorch.org/t/is-it-possible-to-capture-all-operators-errors/155>\
**Category:** Uncategorized\
**Created:** [March 1, 2021, 5:18pm UTC](https://dev-discuss.pytorch.org/t/is-it-possible-to-capture-all-operators-errors/155 "2021-03-01T17:18:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarisaKirisame](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/marisakirisame/32/106_2.png) [@MarisaKirisame](https://dev-discuss.pytorch.org/u/MarisaKirisame)\
**Post date:** [March 1, 2021, 5:18pm UTC](https://dev-discuss.pytorch.org/t/is-it-possible-to-capture-all-operators-errors/155/1 "2021-03-01T17:18:22Z")

</div>

Hi, I am developing a memory saving algorithm for pytorch.  
It basically goes like this:  
while (not\_enough\_memory()) { free\_memory(); }

however, this do not really work in extreme case:  
suppose an operator use to much memory, pytorch will oom without ever calling free\_memory().  
I had added hooks in cuda caching allocator to free\_memory(), but it turn out to be not enough, as cublas also try to malloc memory, and return CUBLAS\_STATUS\_NOT\_INITIALIZED for failing malloc.

Do I have some ways to catch this (and potentially other error) with exception in C++? I could then free up some memory and retry the offending memory-allocating computation.

---

<div class="post-metadata">

**Author:** ![ezyang](https://yyz2.discourse-cdn.com/flex036/user_avatar/dev-discuss.pytorch.org/ezyang/32/12_2.png) [@ezyang](https://dev-discuss.pytorch.org/u/ezyang)\
**Post date:** [March 2, 2021, 10:33pm UTC](https://dev-discuss.pytorch.org/t/is-it-possible-to-capture-all-operators-errors/155/2 "2021-03-02T22:33:16Z")

</div>

Marisa and I discussed this in Twitter DMs and Marisa decided to go the route of catching c10::Error and then groveling in the string to find out if it’s a cublas error or not. Not great, but it works!
