Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add M1 MacOS runner to github actions jobs #370

Conversation

GenevieveBuckley
Copy link
Collaborator

Closes #369

https://github.blog/changelog/2024-01-30-github-actions-introducing-the-new-m1-macos-runner-available-to-open-source/

GitHub Actions: Introducing the new M1 macOS runner available to open source!

January 30, 2024

Today, GitHub is excited to announce the launch of a new M1 macOS runner! This runner is available for all plans, free in public repositories, and eligible to consume included free plan minutes in private repositories. The new runner executes Actions workflows with a 3 vCPU, 7 GB RAM, and 14 GB of storage VM, which provides the latest Mac hardware Actions has to offer. The new runner operates exclusively on macOS 14 and to use it, simply update the runs-on key in your YAML workflow file to macos-14.

@GenevieveBuckley
Copy link
Collaborator Author

Question: do we want to remove macos-latest, and keep only macos-14? Or keep both, until macos-latest finally does catch up to using macos-14 by default (probably some time away). For now, I've kept both.

It seems that currently, macos-latest gives you macos-12 (see the official table here, and the most recent announcement post I could find about it on the github blog.

@constantinpape
Copy link
Contributor

Thanks for working on this @GenevieveBuckley! Let's keep macos-latest around until it catches up to macos-12.
I have identified the reason for the test failure #371 and I will look into fixing it in the evening.

@constantinpape
Copy link
Contributor

I fixed #373 @GenevieveBuckley. Once you merge dev here the test failure should be gone.

Copy link

codecov bot commented Jan 31, 2024

Codecov Report

Attention: 690 lines in your changes are missing coverage. Please review.

Comparison is base (71181d4) 39.72% compared to head (0dffe15) 47.40%.
Report is 116 commits behind head on dev.

Files Patch % Lines
micro_sam/sam_annotator/_widgets.py 23.92% 159 Missing ⚠️
micro_sam/evaluation/instance_segmentation.py 17.03% 112 Missing ⚠️
micro_sam/training/joint_sam_trainer.py 16.53% 106 Missing ⚠️
micro_sam/instance_segmentation.py 14.91% 97 Missing ⚠️
micro_sam/sam_annotator/util.py 13.86% 87 Missing ⚠️
micro_sam/evaluation/inference.py 23.33% 23 Missing ⚠️
micro_sam/evaluation/livecell.py 0.00% 23 Missing ⚠️
micro_sam/training/util.py 58.18% 23 Missing ⚠️
micro_sam/sample_data.py 17.64% 14 Missing ⚠️
micro_sam/sam_annotator/image_series_annotator.py 66.66% 13 Missing ⚠️
... and 10 more
Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #370      +/-   ##
==========================================
+ Coverage   39.72%   47.40%   +7.68%     
==========================================
  Files          33       35       +2     
  Lines        4179     3955     -224     
==========================================
+ Hits         1660     1875     +215     
+ Misses       2519     2080     -439     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@GenevieveBuckley
Copy link
Collaborator Author

Oh no, memory problems

FAILED test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box - RuntimeError: MPS backend out of memory (MPS allocated: 0 bytes, other allocations: 0 bytes, max allowed: 7.93 GB). Tried to allocate 256 bytes on private pool. Use PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable upper limit for memory allocations (may cause system failure).

The macos-14 runner has 3 M1 cpus, 7 GB of RAM, and 14 GB SSD storage.

We can either mark some of the tests as memory hungry, and skip them on the macos-14 runner,
OR try to make the tests smaller (depending on what they are, this could be difficult).

I also saw that according to this page:

  • there are large and extra large versions for the new M1 mac runners, but these are only for paid users (Github Teams and Enterprise)
  • they are hoping to upgrade macos-latest to use macos-14 sometime around April to June 2024

@constantinpape
Copy link
Contributor

Oh no, memory problems

It is a bit strange that this test runs out of memory. The test data for this is tiny, a 10x10 array with a single binary mask:

https://github.com/computational-cell-analytics/micro-sam/blob/master/test/test_vendored.py#L11-L15

Maybe something in https://github.com/computational-cell-analytics/micro-sam/blob/master/micro_sam/_vendored.py#L29 is still very memory inefficient on MPS?!

If you have time you could maybe memory profile it on MPS again? Otherwise we have to skip this test in CI.

@GenevieveBuckley
Copy link
Collaborator Author

Possibly it is not that specific test that causes a lot of memory use, but maybe memory is not being cleared properly from previous tests? I'm only guessing right now.

These are the results I got from pytest-memray:

pytest --memray (click to expand)

(micro-sam-dev) genevieb@Admins-MacBook-Pro micro-sam % pytest --memray
============================= test session starts ==============================
platform darwin -- Python 3.11.7, pytest-8.0.0, pluggy-1.4.0 -- /Users/genevieb/mambaforge/envs/micro-sam-dev/bin/python3.11
cachedir: .pytest_cache
PyQt5 5.15.10 -- Qt runtime 5.15.12 -- Qt compiled 5.15.11
rootdir: /Users/genevieb/Documents/GitHub/micro-sam
configfile: pyproject.toml
plugins: cov-4.1.0, napari-plugin-engine-0.2.0, memray-1.5.0, qt-4.3.1, napari-0.4.19, npe2-0.7.3
collected 34 items

test/test_instance_segmentation.py::TestInstanceSegmentation::test_automatic_mask_generator PASSED [  2%]
test/test_instance_segmentation.py::TestInstanceSegmentation::test_tiled_automatic_mask_generator PASSED [  5%]
test/test_multi_dimensional_segmentation.py::TestMultiDimensionalSegmentation::test_merge_instance_segmentation_3d PASSED [  8%]
test/test_prompt_based_segmentation.py::TestPromptBasedSegmentation::test_segment_from_box PASSED [ 11%]
test/test_prompt_based_segmentation.py::TestPromptBasedSegmentation::test_segment_from_box_and_points PASSED [ 14%]
test/test_prompt_based_segmentation.py::TestPromptBasedSegmentation::test_segment_from_mask PASSED [ 17%]
test/test_prompt_based_segmentation.py::TestPromptBasedSegmentation::test_segment_from_mask_non_square PASSED [ 20%]
test/test_prompt_based_segmentation.py::TestPromptBasedSegmentation::test_segment_from_points PASSED [ 23%]
test/test_prompt_generators.py::TestPromptGenerators::test_box_prompt_generator PASSED [ 26%]
test/test_prompt_generators.py::TestPromptGenerators::test_iterative_prompt_generator PASSED [ 29%]
test/test_prompt_generators.py::TestPromptGenerators::test_point_prompt_generator PASSED [ 32%]
test/test_sam_annotator/test_annotator_2d.py::test_annotator_2d PASSED   [ 35%]
test/test_sam_annotator/test_annotator_3d.py::test_annotator_3d PASSED   [ 38%]
test/test_sam_annotator/test_annotator_tracking.py::test_annotator_tracking PASSED [ 41%]
test/test_sam_annotator/test_cli.py::TestCLI::test_annotator_2d PASSED   [ 44%]
test/test_sam_annotator/test_cli.py::TestCLI::test_annotator_3d PASSED   [ 47%]
test/test_sam_annotator/test_cli.py::TestCLI::test_annotator_tracking PASSED [ 50%]
test/test_sam_annotator/test_cli.py::TestCLI::test_image_series_annotator PASSED [ 52%]
test/test_sam_annotator/test_cli.py::TestCLI::test_precompute_embeddings PASSED [ 55%]
test/test_sam_annotator/test_image_series_annotator.py::test_image_series_annotator PASSED [ 58%]
test/test_sam_annotator/test_image_series_annotator.py::test_image_folder_annotator PASSED [ 61%]
test/test_sam_annotator/test_state.py::TestState::test_state_for_interactive_segmentation PASSED [ 64%]
test/test_sam_annotator/test_state.py::TestState::test_state_for_tracking PASSED [ 67%]
test/test_sam_annotator/test_widgets.py::test_embedding_widget PASSED    [ 70%]
test/test_training.py::TestTraining::test_training PASSED                [ 73%]
test/test_util.py::TestUtil::test_compute_iou PASSED                     [ 76%]
test/test_util.py::TestUtil::test_get_device PASSED                      [ 79%]
test/test_util.py::TestUtil::test_get_sam_model PASSED                   [ 82%]
test/test_util.py::TestUtil::test_segmentation_to_one_hot PASSED         [ 85%]
test/test_util.py::TestUtil::test_tiled_prediction PASSED                [ 88%]
test/test_vendored.py::TestVendored::test_cpu_batched_mask_to_box PASSED [ 91%]
test/test_vendored.py::TestVendored::test_cuda_batched_mask_to_box SKIPPED [ 94%]
test/test_vendored.py::TestVendored::test_mask_to_rle_pytorch PASSED     [ 97%]
test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box PASSED [100%]

=============================== warnings summary ===============================
../../../mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/timm/models/layers/__init__.py:49
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/timm/models/layers/__init__.py:49: DeprecationWarning: Importing from timm.models.layers is deprecated, please import via timm.layers
    warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.layers", DeprecationWarning)

../../../mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/timm/models/registry.py:4
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/timm/models/registry.py:4: DeprecationWarning: Importing from timm.models.registry is deprecated, please import via timm.models
    warnings.warn(f"Importing from {__name__} is deprecated, please import via timm.models", DeprecationWarning)

../../../mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656: UserWarning: Overwriting tiny_vit_5m_224 in registry with mobile_sam.modeling.tiny_vit_sam.tiny_vit_5m_224. This is because the name being registered conflicts with an existing name. Please check if this is not expected.
    return register_model(fn_wrapper)

../../../mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656: UserWarning: Overwriting tiny_vit_11m_224 in registry with mobile_sam.modeling.tiny_vit_sam.tiny_vit_11m_224. This is because the name being registered conflicts with an existing name. Please check if this is not expected.
    return register_model(fn_wrapper)

../../../mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656: UserWarning: Overwriting tiny_vit_21m_224 in registry with mobile_sam.modeling.tiny_vit_sam.tiny_vit_21m_224. This is because the name being registered conflicts with an existing name. Please check if this is not expected.
    return register_model(fn_wrapper)

../../../mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656: UserWarning: Overwriting tiny_vit_21m_384 in registry with mobile_sam.modeling.tiny_vit_sam.tiny_vit_21m_384. This is because the name being registered conflicts with an existing name. Please check if this is not expected.
    return register_model(fn_wrapper)

../../../mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:656: UserWarning: Overwriting tiny_vit_21m_512 in registry with mobile_sam.modeling.tiny_vit_sam.tiny_vit_21m_512. This is because the name being registered conflicts with an existing name. Please check if this is not expected.
    return register_model(fn_wrapper)

../../../mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/napari/layers/utils/layer_utils.py:10
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/napari/layers/utils/layer_utils.py:10: DeprecationWarning:
  Pyarrow will become a required dependency of pandas in the next major release of pandas (pandas 3.0),
  (to allow more performant data types, such as the Arrow string type, and better interoperability with other libraries)
  but was not found to be installed on your system.
  If this would cause problems for you,
  please provide us feedback at https://github.com/pandas-dev/pandas/issues/54466

    import pandas as pd

test/test_training.py::TestTraining::test_training
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch_em/util/util.py:169: UserWarning: Constructor arguments for <class 'micro_sam.training.trainable_sam.TrainableSAM'> cannot be deduced.For this object, empty constructor arguments will be used.Hence, the trainer can probably not be correctly deserialized via 'DefaultTrainer.from_checkpoint'.
    warnings.warn(

test/test_training.py::TestTraining::test_training
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch_em/util/util.py:169: UserWarning: Constructor arguments for <class 'micro_sam.training.util.ConvertToSamInputs'> cannot be deduced.For this object, empty constructor arguments will be used.Hence, the trainer can probably not be correctly deserialized via 'DefaultTrainer.from_checkpoint'.
    warnings.warn(

test/test_training.py::TestTraining::test_training
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch_em/util/util.py:169: UserWarning: Constructor arguments for <class 'torch.nn.modules.loss.MSELoss'> cannot be deduced.For this object, empty constructor arguments will be used.Hence, the trainer can probably not be correctly deserialized via 'DefaultTrainer.from_checkpoint'.
    warnings.warn(

test/test_training.py::TestTraining::test_training
  /Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch_em/util/util.py:169: UserWarning: Constructor arguments for <class 'micro_sam.prompt_generators.IterativePromptGenerator'> cannot be deduced.For this object, empty constructor arguments will be used.Hence, the trainer can probably not be correctly deserialized via 'DefaultTrainer.from_checkpoint'.
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

================================ MEMRAY REPORT =================================
Allocation results for test/test_sam_annotator/test_widgets.py::test_embedding_widget at the high watermark

	 📦 Total memory allocated: 306.6MiB
	 📏 Total allocations: 778579
	 📊 Histogram of allocation sizes: |▂ █ ▂    |
	 🥇 Biggest allocating functions:
		- batch_norm:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/functional.py:2478 -> 64.0MiB
		- forward:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/activation.py:682 -> 64.0MiB
		- _conv_forward:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/conv.py:456 -> 64.0MiB
		- forward:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/activation.py:682 -> 16.0MiB
		- batch_norm:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/functional.py:2478 -> 16.0MiB


Allocation results for test/test_sam_annotator/test_annotator_2d.py::test_annotator_2d at the high watermark

	 📦 Total memory allocated: 135.9MiB
	 📏 Total allocations: 1713510
	 📊 Histogram of allocation sizes: |▁ ▄ █    |
	 🥇 Biggest allocating functions:
		- load_tensor:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/serialization.py:1357 -> 38.7MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 6.0MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 6.0MiB
		- get_app:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/napari/_qt/qt_event_loop.py:184 -> 5.8MiB
		- train:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:257 -> 4.7MiB


Allocation results for test/test_sam_annotator/test_annotator_3d.py::test_annotator_3d at the high watermark

	 📦 Total memory allocated: 103.6MiB
	 📏 Total allocations: 2164506
	 📊 Histogram of allocation sizes: |▃ █ ▁    |
	 🥇 Biggest allocating functions:
		- load_tensor:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/serialization.py:1357 -> 38.7MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 6.0MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 6.0MiB
		- train:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:257 -> 4.7MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 4.5MiB


Allocation results for test/test_sam_annotator/test_image_series_annotator.py::test_image_folder_annotator at the high watermark

	 📦 Total memory allocated: 103.6MiB
	 📏 Total allocations: 1339806
	 📊 Histogram of allocation sizes: |▃ █ ▁    |
	 🥇 Biggest allocating functions:
		- load_tensor:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/serialization.py:1357 -> 38.7MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 6.0MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 6.0MiB
		- train:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:257 -> 4.7MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 4.5MiB


Allocation results for test/test_sam_annotator/test_annotator_tracking.py::test_annotator_tracking at the high watermark

	 📦 Total memory allocated: 102.6MiB
	 📏 Total allocations: 2174561
	 📊 Histogram of allocation sizes: |▃ █ ▁    |
	 🥇 Biggest allocating functions:
		- load_tensor:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/serialization.py:1357 -> 38.7MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 6.0MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 6.0MiB
		- train:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/mobile_sam/modeling/tiny_vit_sam.py:257 -> 4.7MiB
		- __init__:/Users/genevieb/mambaforge/envs/micro-sam-dev/lib/python3.11/site-packages/torch/nn/modules/linear.py:96 -> 4.5MiB



---------- coverage: platform darwin, python 3.11.7-final-0 ----------
Name                                                Stmts   Miss  Cover   Missing
---------------------------------------------------------------------------------
micro_sam/__init__.py                                   3      0   100%
micro_sam/__version__.py                                1      0   100%
micro_sam/_test_util.py                                11      0   100%
micro_sam/_vendored.py                                 85     28    67%   17-21, 43, 86-97, 101-107, 129, 136-142
micro_sam/evaluation/__init__.py                        4      0   100%
micro_sam/evaluation/evaluation.py                     32      3    91%   61, 72-73
micro_sam/evaluation/experiments.py                    28     21    25%   29-49, 61-67, 78-83
micro_sam/evaluation/inference.py                     226     97    57%   39-44, 53-64, 120, 165, 190-194, 198-210, 227-257, 263-277, 287-290, 330, 333, 350, 373, 375, 383-384, 386-387, 416-418, 454, 459, 491, 505, 538-563, 579-603
micro_sam/evaluation/instance_segmentation.py         135    112    17%   23-28, 50-54, 81-95, 112-132, 136-143, 191-235, 258-285, 306-321, 326-339, 371-386
micro_sam/evaluation/livecell.py                      172    172     0%   5-415
micro_sam/evaluation/model_comparison.py              236    236     0%   4-435
micro_sam/inference.py                                 60      6    90%   55, 58, 66, 69, 75, 82
micro_sam/instance_segmentation.py                    383    154    60%   31-32, 44, 47-48, 81, 83, 180-181, 187-225, 230, 238, 242, 259-260, 300, 363-366, 374, 404, 424, 458, 462, 503, 536-541, 545, 553, 678-680, 696-709, 712-733, 751-800, 825-833, 839, 857-879, 882-919, 946-962
micro_sam/multi_dimensional_segmentation.py           102     74    27%   50-141, 184-217
micro_sam/precompute_state.py                          72     56    22%   42-79, 85-91, 97-100, 136-147, 156-174, 182
micro_sam/prompt_based_segmentation.py                200     85    58%   57-60, 90-92, 124-125, 133-134, 142-169, 173-188, 192-202, 210-219, 223-224, 233-237, 298, 300, 349-360, 366-368, 383, 394, 397, 437, 439, 474-480, 495, 497
micro_sam/prompt_generators.py                        129      8    94%   52, 101, 175-182, 302
micro_sam/sam_annotator/__init__.py                     4      0   100%
micro_sam/sam_annotator/_annotator.py                  70      7    90%   11, 80, 84, 88, 92, 97, 150
micro_sam/sam_annotator/_state.py                      69     11    84%   82, 91-102, 114-125
micro_sam/sam_annotator/_widgets.py                   237    172    27%   25, 33-44, 50, 56-57, 63-76, 81-101, 106-109, 120, 125-128, 155-156, 167-186, 202-203, 222-246, 251-275, 296-319, 326-343, 348-377, 389-422, 444-465, 480-516
micro_sam/sam_annotator/annotator_2d.py                37     12    68%   77, 91, 96-109
micro_sam/sam_annotator/annotator_3d.py                57     21    63%   24-34, 102, 116, 121-133
micro_sam/sam_annotator/annotator_tracking.py         105     23    78%   33, 38-39, 47-50, 64-67, 76-79, 232, 246, 251-275
micro_sam/sam_annotator/gui_utils.py                   41     41     0%   1-75
micro_sam/sam_annotator/image_series_annotator.py      88     43    51%   27-35, 87, 103-106, 113-141, 147, 151, 185-227
micro_sam/sam_annotator/util.py                       266    237    11%   26-32, 37-84, 96-97, 122-149, 171-225, 240-253, 271-292, 304-362, 370-422, 427-437, 441-443, 453-543
micro_sam/sample_data.py                              145    110    24%   41-57, 71-77, 91-102, 112-116, 130-140, 150-154, 167-178, 188-192, 206-221, 231-238, 256-271, 281-288, 301-317, 327-334, 345, 347-348, 367-378
micro_sam/training/__init__.py                          3      0   100%
micro_sam/training/joint_sam_trainer.py               127    106    17%   23-26, 29-36, 39-58, 61-67, 70-114, 117-155, 161-167, 170-177, 182-189, 194-200
micro_sam/training/sam_trainer.py                     221     18    92%   84-107, 316
micro_sam/training/trainable_sam.py                    44      0   100%
micro_sam/training/util.py                            107     31    71%   57-66, 115, 192-194, 197-211, 216-218, 221-236
micro_sam/util.py                                     363    101    72%   28-30, 35-36, 40-41, 152, 155, 163, 188, 192, 266, 273, 275, 297-299, 349, 381-383, 401-404, 441-485, 562-606, 664, 686, 709, 711, 717, 795-802, 820
micro_sam/visualization.py                             92     92     0%   4-169
---------------------------------------------------------------------------------
TOTAL                                                3955   2077    47%
Coverage XML written to file coverage.xml

============================= slowest 10 durations =============================
75.87s call     test/test_training.py::TestTraining::test_training
18.82s call     test/test_instance_segmentation.py::TestInstanceSegmentation::test_automatic_mask_generator
16.62s call     test/test_instance_segmentation.py::TestInstanceSegmentation::test_tiled_automatic_mask_generator
6.52s setup    test/test_instance_segmentation.py::TestInstanceSegmentation::test_automatic_mask_generator
6.38s call     test/test_sam_annotator/test_annotator_2d.py::test_annotator_2d
3.61s call     test/test_prompt_based_segmentation.py::TestPromptBasedSegmentation::test_segment_from_mask
3.09s call     test/test_sam_annotator/test_annotator_3d.py::test_annotator_3d
2.97s call     test/test_sam_annotator/test_annotator_tracking.py::test_annotator_tracking
2.35s call     test/test_sam_annotator/test_image_series_annotator.py::test_image_series_annotator
2.24s call     test/test_util.py::TestUtil::test_tiled_prediction
============ 33 passed, 1 skipped, 12 warnings in 165.13s (0:02:45) ============
(micro-sam-dev) genevieb@Admins-MacBook-Pro micro-sam %

@constantinpape
Copy link
Contributor

@GenevieveBuckley I only see memory profiles for some of the tests:

  • Allocation results for test/test_sam_annotator/test_widgets.py::test_embedding_widget at the high watermark
  • Allocation results for test/test_sam_annotator/test_annotator_2d.py::test_annotator_2d at the high watermark
  • Allocation results for test/test_sam_annotator/test_annotator_3d.py::test_annotator_3d
  • Allocation results for test/test_sam_annotator/test_image_series_annotator.py::test_image_folder_annotator
  • Allocation results for test/test_sam_annotator/test_annotator_tracking.py::test_annotator_tracking

Could you maybe rerun it only for TestVendored::test_mps_batched_mask_to_box and post the memory benchmark? I think that will help better to see if there is a memory spike.

@GenevieveBuckley
Copy link
Collaborator Author

I only see memory profiles for some of the tests

Yes, that's what I see too. I can't make it produce any more information than that.

  • I tried asking pytest for more verbose output (pytest -vv --memray), but the memray report section did not show more information.
  • I tried adjusting the --most-allocations pytest-memray configuration option. I used both --most-allocations=0 (which should show everything) and --most-allocations=34 (the number of tests collected by pytest)

Could you maybe rerun it only for TestVendored::test_mps_batched_mask_to_box and post the memory benchmark? I think that will help better to see if there is a memory spike.

I'm sorry, when I tried this earlier there was no output. The "MEMRAY REPORT" section heading still appears in the pytest output, but no contents below it and the next lines of text are the test coverage information. Here's a copy of those (lack of) results:

Details (click to expand)
pytest -vv --memray test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box
======================================== test session starts =========================================
platform darwin -- Python 3.11.7, pytest-8.0.0, pluggy-1.4.0 -- /Users/genevieb/mambaforge/envs/micro-sam-dev/bin/python3.11
cachedir: .pytest_cache
PyQt5 5.15.10 -- Qt runtime 5.15.12 -- Qt compiled 5.15.11
rootdir: /Users/genevieb/Documents/GitHub/micro-sam
configfile: pyproject.toml
plugins: cov-4.1.0, napari-plugin-engine-0.2.0, qt-4.3.1, napari-0.4.19, memray-1.5.1.dev12+g1549d62, npe2-0.7.3
collected 1 item

test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box PASSED                       [100%]


=========================================== MEMRAY REPORT ============================================

---------- coverage: platform darwin, python 3.11.7-final-0 ----------
Name                                                Stmts   Miss  Cover   Missing
---------------------------------------------------------------------------------
micro_sam/__init__.py                                   3      0   100%
micro_sam/__version__.py                                1      0   100%
micro_sam/_test_util.py                                11     11     0%   1-22
micro_sam/_vendored.py                                 85     46    46%   17-21, 43, 49, 77, 86-97, 101-107, 122-150
micro_sam/evaluation/__init__.py                        4      4     0%   4-17
micro_sam/evaluation/evaluation.py                     32     32     0%   5-75
micro_sam/evaluation/experiments.py                    28     28     0%   4-83
micro_sam/evaluation/inference.py                     226    226     0%   4-603
micro_sam/evaluation/instance_segmentation.py         135    135     0%   4-386
micro_sam/evaluation/livecell.py                      172    172     0%   5-415
micro_sam/evaluation/model_comparison.py              236    236     0%   4-435
micro_sam/inference.py                                 60     60     0%   1-148
micro_sam/instance_segmentation.py                    383    383     0%   7-962
micro_sam/multi_dimensional_segmentation.py           102    102     0%   4-259
micro_sam/precompute_state.py                          72     72     0%   4-182
micro_sam/prompt_based_segmentation.py                200    200     0%   5-499
micro_sam/prompt_generators.py                        129    129     0%   6-350
micro_sam/sam_annotator/__init__.py                     4      4     0%   4-7
micro_sam/sam_annotator/_annotator.py                  70     70     0%   1-160
micro_sam/sam_annotator/_state.py                      69     69     0%   6-140
micro_sam/sam_annotator/_widgets.py                   237    237     0%   4-516
micro_sam/sam_annotator/annotator_2d.py                37     37     0%   1-109
micro_sam/sam_annotator/annotator_3d.py                57     57     0%   1-133
micro_sam/sam_annotator/annotator_tracking.py         105    105     0%   1-275
micro_sam/sam_annotator/gui_utils.py                   41     41     0%   1-75
micro_sam/sam_annotator/image_series_annotator.py      88     88     0%   1-227
micro_sam/sam_annotator/util.py                       266    266     0%   1-543
micro_sam/sample_data.py                              145    145     0%   15-378
micro_sam/training/__init__.py                          3      3     0%   4-6
micro_sam/training/joint_sam_trainer.py               127    127     0%   1-200
micro_sam/training/sam_trainer.py                     221    221     0%   1-447
micro_sam/training/trainable_sam.py                    44     44     0%   1-128
micro_sam/training/util.py                            107    107     0%   1-236
micro_sam/util.py                                     363    363     0%   5-841
micro_sam/visualization.py                             92     92     0%   4-169
---------------------------------------------------------------------------------
TOTAL                                                3955   3912     1%
Coverage XML written to file coverage.xml

======================================== slowest 10 durations ========================================
0.16s call     test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box
0.05s setup    test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box
0.00s teardown test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box
========================================= 1 passed in 2.09s =========================================

I don't know if the reason for this is that the test actually doesn't use much memory at all (like you expect). Maybe there are memory leaks elsewhere, and this test just happens to be when the memory is full, but not the cause?

I tried to test this by adding the pytest-memray marker to limit leaks @pytest.mark.limit_leaks("1 MB") to all our test functions. Surprisingly, there was no error/failure/crash/warning when I ran the tests again. But I am still not sure if this sufficiently tests the hypothesis - there are a lot of setup and tear down actions not covered by the markers & I don't know if they could be leaking.

Other things we could try:

  1. Try adding the limit leak marker to the setup and teardown functions, and/or maybe the test classes as well. I don't know if this will work, or break things, but it seems easy enough to try.
  2. Convert the test case into a plain python script, and run it using memray directly (not pytest-memray). Maybe then we will be able to get detailed output about test_mps_batched_mask_to_box
  3. We could also make a new branch, with another github actions script including a workflow_dispatch manual trigger, that exists just so we can try running pytest on smaller sections or different combinations of the tests. That would be annoying, but perhaps we could narrow down where the problem comes from?

@GenevieveBuckley
Copy link
Collaborator Author

Getting closer. I made a mistake with my earlier memory leak tests, and forgot to decorate the files test/test_sam_annotator. The memory leaks are happening in all the places where we run the annotator (which sets up a model, the AnnotatorState, etc.). Files affected are:

  • test_annotator_2d.py
  • test_annotator_3d.py
  • test_annotator_tracking.py
  • test_image_series.py

I'm trying to reduce the problem by deleting variables, running the garbage collector, and emptying the pytorch cache. That seems to help a bit, but it's not great yet.

@GenevieveBuckley
Copy link
Collaborator Author

GenevieveBuckley commented Feb 5, 2024

Here's a test, we'll see if this helps the macos-14 CI memory problems: GenevieveBuckley#5

EDIT: the changes don't seem to have made any difference 😢

@constantinpape
Copy link
Contributor

The memory leaks are happening in all the places where we run the annotator (which sets up a model, the AnnotatorState, etc.). Files affected are:

Ok, that's good to know! This might point to a problem with the napari test infrastructure though?!

EDIT: the changes don't seem to have made any difference 😢

That's too bad :(.

I still find it curious that it's always test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box that fails...

For now, I suggest we skip this test and go ahead with the PR.

@GenevieveBuckley
Copy link
Collaborator Author

GenevieveBuckley commented Feb 5, 2024

This might point to a problem with the napari test infrastructure though?!

I don't think so? I think that because:

  1. The make_napari_viewer_proxy test fixture was created to avoid those potential problems (see here).
  2. Also, the test_widgets.py tests do not report memory leaks using the pytest-memray decorator. This test just opens napari and adds the plugin dock widget, but doesn't do any processing/computation with micro-sam. That's the other reason I suspect something created by micro-sam might still be hogging memory somewhere (like the model, annotator state, torch cache, ...something else?) EDIT: sorry, it looks like there is a small memory leak, somewhere around 5-6MB? I think the leaks from the other annotator tests were much larger though

I still find it curious that it's always test/test_vendored.py::TestVendored::test_mps_batched_mask_to_box that fails...

I don't think this is curious. The tests are always collected and run in the same order
We could try:

  • Renaming test_vendored.py to test_aaa_vendored.py to see if I can get that test to run first, not last, and see if it still fails, or
  • Looking for a pytest plugin that randomizes the order of tests. I'm sure one must exist (I've just found pytest-randomly and pytest-random-order, looks like there are quite a few options to choose from). Then we can see if the same test fails or not.

@GenevieveBuckley
Copy link
Collaborator Author

GenevieveBuckley commented Feb 6, 2024

You're right - it doesn't matter which order the tests run in (thanks pytest-randomly), TestVendored::test_mps_batched_mask_to_box always fails on the macos-14 CI.

That is very weird.

@GenevieveBuckley
Copy link
Collaborator Author

I give up. I do not know why that test fails. I've made an issue here: #380 and have skipped the test if it is running on github actions.

@constantinpape
Copy link
Contributor

My best guess is the following:

  • We do have some memory leaks in the widget tests. Not sure if they are due to napari, or pytorch. (This might also be due to pytorch MPS, I do have the feeling that this is still quite experimental...)
  • In addition the test_mps_batched... takes a lot of memory, because there is some memory inefficiency in MPS.

In any case, thanks for looking into this!! At least we now know that these issues are there.

@constantinpape constantinpape merged commit b64a692 into computational-cell-analytics:dev Feb 6, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants