core.common.utils#

Copyright (c) Meta Platforms, Inc. and affiliates.

This source code is licensed under the MIT license found in the LICENSE file in the root directory of this source tree.

Attributes#

Classes#

UniqueKeyLoader

SeverityLevelBetween

Filter instances are used to perform arbitrary filtering of LogRecords.

Functions#

conditional_grad(dec)

Decorator to enable/disable grad depending on whether force/energy predictions are being made

_import_local_file(→ None)

Imports a Python file as a module

setup_experimental_imports(→ None)

Import selected directories of modules from the "experimental" subdirectory.

_get_project_root(→ pathlib.Path)

Gets the root folder of the project (the "ocp" folder)

setup_imports(→ None)

debug_log_entry_exit(func)

setup_logging(→ None)

setup_env_vars(→ None)

_resolve_scale_factor_submodule(model, name)

_report_incompat_keys(→ tuple[list[str], list[str]])

match_state_dict(→ dict)

load_state_dict(→ tuple[list[str], list[str]])

get_commit_hash(→ str)

get_commit_hash_for_repo(→ str | None)

load_model_and_weights_from_checkpoint(→ torch.nn.Module)

get_timestamp_uid(→ str)

tensor_stats(→ dict)

get_weight_table(→ tuple[list, list])

get_checkpoint_format(→ str)

get_deep(dictionary, keys[, default])

get_subdirectories_sorted_by_time(→ list)

Get all subdirectories in a directory sorted by their last modification time.

get_cluster_name(→ str)

Module Contents#

core.common.utils.DEFAULT_ENV_VARS#
class core.common.utils.UniqueKeyLoader(stream)#

Bases: yaml.SafeLoader

construct_mapping(node, deep=False)#
core.common.utils.conditional_grad(dec)#

Decorator to enable/disable grad depending on whether force/energy predictions are being made

core.common.utils._import_local_file(path: pathlib.Path, *, project_root: pathlib.Path) None#

Imports a Python file as a module

Parameters:
  • path (Path) – The path to the file to import

  • project_root (Path) – The root directory of the project (i.e., the “ocp” folder)

core.common.utils.setup_experimental_imports(project_root: pathlib.Path) None#

Import selected directories of modules from the “experimental” subdirectory.

If a file named “.include” is present in the “experimental” subdirectory, this will be read as a list of experimental subdirectories whose module (including in any subsubdirectories) should be imported.

Parameters:

project_root – The root directory of the project (i.e., the “ocp” folder)

core.common.utils._get_project_root() pathlib.Path#

Gets the root folder of the project (the “ocp” folder) :return: The absolute path to the project root.

core.common.utils.setup_imports(config: dict | None = None) None#
core.common.utils.debug_log_entry_exit(func)#
class core.common.utils.SeverityLevelBetween(min_level: int, max_level: int)#

Bases: logging.Filter

Filter instances are used to perform arbitrary filtering of LogRecords.

Loggers and Handlers can optionally use Filter instances to filter records as desired. The base filter class only allows events which are below a certain point in the logger hierarchy. For example, a filter initialized with “A.B” will allow events logged by loggers “A.B”, “A.B.C”, “A.B.C.D”, “A.B.D” etc. but not “A.BB”, “B.A.B” etc. If initialized with the empty string, all events are passed.

min_level#
max_level#
filter(record) bool#

Determine if the specified record is to be logged.

Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.

core.common.utils.setup_logging() None#
core.common.utils.setup_env_vars() None#
core.common.utils._resolve_scale_factor_submodule(model: torch.nn.Module, name: str)#
core.common.utils._report_incompat_keys(model: torch.nn.Module, keys: torch.nn.modules.module._IncompatibleKeys, strict: bool = False) tuple[list[str], list[str]]#
core.common.utils.match_state_dict(model_state_dict: collections.abc.Mapping[str, torch.Tensor], checkpoint_state_dict: collections.abc.Mapping[str, torch.Tensor]) dict#
core.common.utils.load_state_dict(module: torch.nn.Module, state_dict: collections.abc.Mapping[str, torch.Tensor], strict: bool = True) tuple[list[str], list[str]]#
core.common.utils.get_commit_hash() str#
core.common.utils.get_commit_hash_for_repo(git_repo_path: str) str | None#
core.common.utils.load_model_and_weights_from_checkpoint(checkpoint_path: str) torch.nn.Module#
core.common.utils.get_timestamp_uid() str#
core.common.utils.tensor_stats(name: str, x: torch.Tensor) dict#
core.common.utils.get_weight_table(model: torch.nn.Module) tuple[list, list]#
core.common.utils.get_checkpoint_format(config: dict) str#
core.common.utils.get_deep(dictionary: dict, keys: str, default: str | None = None)#
core.common.utils.get_subdirectories_sorted_by_time(directory: str) list#

Get all subdirectories in a directory sorted by their last modification time. :param directory: The path to the directory to search. :type directory: str

Returns:

A list of tuples containing the subdirectory path and its last modification time.

Return type:

list

core.common.utils.get_cluster_name() str#