-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code import to train/eval scripts (#1002)
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2024 MosaicML LLM Foundry authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import importlib.util | ||
import os | ||
from pathlib import Path | ||
from types import ModuleType | ||
from typing import Union | ||
|
||
__all__ = ['import_file'] | ||
|
||
|
||
def import_file(loc: Union[str, Path]) -> ModuleType: | ||
"""Import module from a file. Used to run arbitrary python code. | ||
Args: | ||
name (str): Name of module to load. | ||
loc (str / Path): Path to the file. | ||
Returns: | ||
ModuleType: The module object. | ||
""" | ||
if not os.path.exists(loc): | ||
raise FileNotFoundError(f'File {loc} does not exist.') | ||
|
||
spec = importlib.util.spec_from_file_location('python_code', str(loc)) | ||
|
||
assert spec is not None | ||
assert spec.loader is not None | ||
|
||
module = importlib.util.module_from_spec(spec) | ||
|
||
try: | ||
spec.loader.exec_module(module) | ||
except Exception as e: | ||
raise RuntimeError(f'Error executing {loc}') from e | ||
return module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2024 MosaicML LLM Foundry authors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import pathlib | ||
|
||
import pytest | ||
|
||
from llmfoundry.registry import import_file | ||
|
||
|
||
def test_registry_init_code(tmp_path: pathlib.Path): | ||
register_code = """ | ||
import os | ||
os.environ['TEST_ENVIRON_REGISTRY_KEY'] = 'test' | ||
""" | ||
|
||
with open(tmp_path / 'init_code.py', 'w') as _f: | ||
_f.write(register_code) | ||
|
||
import_file(tmp_path / 'init_code.py') | ||
|
||
assert os.environ['TEST_ENVIRON_REGISTRY_KEY'] == 'test' | ||
|
||
del os.environ['TEST_ENVIRON_REGISTRY_KEY'] | ||
|
||
|
||
def test_registry_init_code_fails(tmp_path: pathlib.Path): | ||
register_code = """ | ||
import os | ||
os.environ['TEST_ENVIRON_REGISTRY_KEY'] = 'test' | ||
asdf | ||
""" | ||
|
||
with open(tmp_path / 'init_code.py', 'w') as _f: | ||
_f.write(register_code) | ||
|
||
with pytest.raises(RuntimeError, match='Error executing .*init_code.py'): | ||
import_file(tmp_path / 'init_code.py') | ||
|
||
|
||
def test_registry_init_code_dne(tmp_path: pathlib.Path): | ||
with pytest.raises(FileNotFoundError, match='File .* does not exist'): | ||
import_file(tmp_path / 'init_code.py') |