Skip to content

Commit

Permalink
Fix 2.4.1 warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinfriede committed Sep 19, 2024
1 parent 2c798a7 commit 7519f57
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
18 changes: 12 additions & 6 deletions test/test_a_memory_leak/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from __future__ import annotations

import gc
import warnings

import torch

Expand All @@ -41,12 +42,17 @@ def garbage_collect() -> None:

def _tensors_from_gc() -> Generator[Tensor, None, None]:
# return [obj for obj in gc.get_objects() if isinstance(obj, Tensor)]
for obj in gc.get_objects():
try:
if isinstance(obj, Tensor):
yield obj
except Exception: # nosec B112 pylint: disable=broad-exception-caught
continue
with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)

for obj in gc.get_objects():
try:
if isinstance(obj, Tensor):
yield obj
except (
Exception
): # nosec B112 pylint: disable=broad-exception-caught
continue


@overload
Expand Down
13 changes: 9 additions & 4 deletions test/test_hamiltonian/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import pytest
import torch
from tad_mctc._version import __tversion__

from dxtb import GFN1_XTB as par
from dxtb import IndexHelper
Expand All @@ -51,12 +52,16 @@ def test_write_to_pt() -> None:
h = GFN1Hamiltonian(numbers, par, ihelp)
h._matrix = torch.tensor([[1.0, 2.0], [3.0, 4.0]])

kwargs: dict = {"map_location": torch.device("cpu")}
if __tversion__ > (1, 12, 1):
kwargs["weights_only"] = True

with td.TemporaryDirectory() as tmpdir:
p_write = Path(tmpdir) / "test.pt"
h.to_pt(p_write)

read_mat = torch.load(p_write)
assert pytest.approx(h._matrix.cpu()) == read_mat.cpu()
read_mat = torch.load(p_write, **kwargs)
assert pytest.approx(h._matrix.cpu()) == read_mat

with td.TemporaryDirectory() as tmpdir:
p_write = Path(tmpdir) / f"{h.label.casefold()}"
Expand All @@ -65,5 +70,5 @@ def test_write_to_pt() -> None:
h.label = str(p_write)
h.to_pt()

read_mat = torch.load(f"{p_write}.pt")
assert pytest.approx(h._matrix.cpu()) == read_mat.cpu()
read_mat = torch.load(f"{p_write}.pt", **kwargs)
assert pytest.approx(h._matrix.cpu()) == read_mat

0 comments on commit 7519f57

Please sign in to comment.