Skip to content

creating multiple LocalHFBackend in pytest caused a memory leak #630

Closed

Description

@ajbozarth

This is a follow up to #623 which attempted to solve this issue on the tests side before eventually just marking the tests as @pytest.mark.requires_heavy_ram to solve the issue in the short term.

Currently when a LocalHFBackend is created it loads the model into memory, but the model is not cleared from emory until the python process ends. This is not an issue if a python process is just creating one LocalHFBackend since most models can fit into memory or users are aware they are trying to load a model that is too big.

This does cause an issue in pytest though since pytest owns the memory not any one test that creates a LocalHFBackend. This leads to every test that initializes a LocalHFBackend adding another model to memory, which won't be cleared until all of pytest is finished. After @planetf1 work to add the @pytest.mark.requires_heavy_ram mark most of the tests that do this were flagged to skip on low RAM machines and thus this wasn't an issue. But one test in test/stdlib/test_spans.py still loaded a model. In #563 I added a new test (that runs twice) that uses LocalHFBackend which caused tests to hang or crash when both these files ran in one pytest command. #623 partially addressed this by having the tests within a module share an instance, but that still left two instances of loading a model into memory, which cause the tests to hang on the second test ran due to memory issues.

We need to identify why the models are not being cleared from memory and add functionality to either the backend itself or to the tests to do so.

Activity

  1. ajbozarth commented on Mar 12, 2026

    @ajbozarth
    MemberAuthor

    Also worth noting that adding torch.cuda.empty_cache() to the test fixtures had no affect on this, so that is not a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions