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

chore: use functools.cached_property for cached properties #4187

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions deepmd/tf/infer/deep_eval.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import json
from functools import (
cache,
cached_property,
)
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -263,8 +263,7 @@ def _init_attr(self):
else:
self.modifier_type = None

@property
@cache
@cached_property
def model_type(self) -> type["DeepEvalWrapper"]:
"""Get type of model.

Expand All @@ -288,8 +287,7 @@ def model_type(self) -> type["DeepEvalWrapper"]:
else:
raise RuntimeError(f"unknown model type {model_type}")

@property
@cache
@cached_property
def model_version(self) -> str:
"""Get version of model.

Expand All @@ -307,8 +305,7 @@ def model_version(self) -> str:
[mt] = run_sess(self.sess, [t_mt], feed_dict={})
return mt.decode("utf-8")

@property
@cache
@cached_property
def sess(self) -> tf.Session:
"""Get TF session."""
# start a tf session associated to the graph
Expand Down Expand Up @@ -1192,8 +1189,7 @@ def __init__(

self.neighbor_list = neighbor_list

@property
@cache
@cached_property
def model_type(self) -> str:
"""Get type of model.

Expand All @@ -1203,8 +1199,7 @@ def model_type(self) -> str:
[mt] = run_sess(self.sess, [t_mt], feed_dict={})
return mt.decode("utf-8")

@property
@cache
@cached_property
def model_version(self) -> str:
"""Get version of model.

Expand All @@ -1222,8 +1217,7 @@ def model_version(self) -> str:
[mt] = run_sess(self.sess, [t_mt], feed_dict={})
return mt.decode("utf-8")

@property
@cache
@cached_property
def sess(self) -> tf.Session:
"""Get TF session."""
# start a tf session associated to the graph
Expand Down
4 changes: 2 additions & 2 deletions deepmd/tf/utils/tabulate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import logging
from functools import (
cached_property,
lru_cache,
)
from typing import (
Expand Down Expand Up @@ -770,8 +771,7 @@ def _get_layer_size(self):
raise RuntimeError("Unsupported descriptor")
return layer_size

@property
@lru_cache
@cached_property
def _n_all_excluded(self) -> int:
"""Then number of types excluding all types."""
return sum(int(self._all_excluded(ii)) for ii in range(0, self.ntypes))
Expand Down
5 changes: 2 additions & 3 deletions deepmd/utils/data_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import warnings
from functools import (
cache,
cached_property,
)
from typing import (
Any,
Expand Down Expand Up @@ -238,8 +238,7 @@ def _load_test(self, ntests=-1):
for nn in test_system_data:
self.test_data[nn].append(test_system_data[nn])

@property
@cache
@cached_property
def default_mesh(self) -> list[np.ndarray]:
"""Mesh for each system."""
return [
Expand Down