Commit 271a6f8
authored
[ENH] Add gradient computation control and improve memory management (#31)
### TL;DR
Added memory optimization features to improve performance when evaluating large models.
### What changed?
- Added a `grads` parameter to the backend tensor configuration to control PyTorch gradient computation
- Implemented proper context management for PyTorch's `no_grad()` mode
- Added garbage collection calls during chunked evaluation to prevent memory buildup
- Optimized memory usage in the evaluation kernel by immediately deleting tensors after use
- Improved error handling in the evaluation function
### How to test?
1. Test with large models that previously caused memory issues
2. Compare memory usage before and after these changes
3. Verify that model evaluation still produces correct results
4. Test with both gradient computation enabled and disabled
### Why make this change?
These optimizations address memory leaks and excessive memory usage during model evaluation, particularly for large models. By properly managing PyTorch's gradient computation and implementing strategic garbage collection, we can significantly reduce memory footprint without sacrificing performance. The immediate cleanup of tensors after use prevents memory buildup during evaluation of large datasets.File tree
2 files changed
+36
-7
lines changed- gempy_engine
- core
- modules/evaluator
2 files changed
+36
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
50 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | | - | |
| 55 | + | |
| 56 | + | |
54 | 57 | | |
55 | 58 | | |
56 | 59 | | |
| |||
99 | 102 | | |
100 | 103 | | |
101 | 104 | | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
102 | 120 | | |
103 | 121 | | |
104 | 122 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
60 | 65 | | |
61 | 66 | | |
62 | 67 | | |
| |||
75 | 80 | | |
76 | 81 | | |
77 | 82 | | |
78 | | - | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
79 | 86 | | |
80 | 87 | | |
81 | 88 | | |
| |||
85 | 92 | | |
86 | 93 | | |
87 | 94 | | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
88 | 98 | | |
89 | 99 | | |
90 | 100 | | |
91 | | - | |
92 | | - | |
| 101 | + | |
| 102 | + | |
93 | 103 | | |
94 | 104 | | |
95 | 105 | | |
96 | 106 | | |
97 | 107 | | |
98 | | - | |
| 108 | + | |
| 109 | + | |
99 | 110 | | |
100 | 111 | | |
101 | 112 | | |
| |||
0 commit comments