astropy / astropy/astropy

Compound models use excessive memory

Open
#16,701 16 comments 1 reaction 1 assignee Claimed by @WilliamJamieson View on GitHub
Bug modeling Performance
Dominant language
Python
Stars
5.3k
Forks
2.2k
Avg merge
1d 18h
Merged PRs (30d)
74

Description

### Description

Creating a large compound model will result in using huge amounts of memory and/or reach the maximum recursion depth. The amount of memory used by very small models is very large.

### Expected behavior

The amount of memory used by compound models should be linear with the number of included models

### How to Reproduce

Run the following example:

```python
from astropy.modeling.models import Gaussian2D

dao_psf_model = Gaussian2D()

print()
# try copying models a bunch
models = []
x = 1000000
from tqdm.auto import tqdm
for i in tqdm(range(x)):
models.append(dao_psf_model.copy())
if i == 0:
psf_model = dao_psf_model
else:
psf_model += dao_psf_model
print(f"Finished making {x} model copies")
```

I ran tests with 4, 8, 16, 32 GB memory:

```
0%| | 1299/1000000 [00:26<8:53:21, 31.21it/s]
0%| | 1836/1000000 [00:51<12:42:30, 21.82it/s]
0%| | 2519/1000000 [03:06<35:56:14, 7.71it/s]
0%| | 2987/1000000 [03:19<18:28:11, 14.99it/s]
```
The first 3 fail by running into memory limits. The last ends with
```python
Traceback (most recent call last):
File "/blue/adamginsburg/adamginsburg/repos/astropy/astropy/modeling/core.py", line 4200, in make_subtree_dict
make_subtree_dict(tree.left, nodepath + "l", tdict, leaflist)
File "/blue/adamginsburg/adamginsburg/repos/astropy/astropy/modeling/core.py", line 4200, in make_subtree_dict
make_subtree_dict(tree.left, nodepath + "l", tdict, leaflist)
File "/blue/adamginsburg/adamginsburg/repos/astropy/astropy/modeling/core.py", line 4200, in make_subtree_dict
make_subtree_dict(tree.left, nodepath + "l", tdict, leaflist)
[Previous line repeated 996 more times]
File "/blue/adamginsburg/adamginsburg/repos/astropy/astropy/modeling/core.py", line 4196, in make_subtree_dict
if not hasattr(tree, "isleaf"):
RecursionError: maximum recursion depth exceeded
```

Note that performance degrades rapidly before the memory runs out.

### Versions
Tests were run on:
```
Linux-4.18.0-513.24.1.el8_9.x86_64-x86_64-with-glibc2.28
Python 3.10.8 (main, Nov 24 2022, 14:13:03) [GCC 11.2.0]
astropy 6.1.dev108+gaa142496ef
Numpy 1.23.5
pyerfa 2.0.0
Scipy 1.9.3
Matplotlib 3.6.2
```

cc @larrybradley as this affects large groups in photutils.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.