-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flyin - interactive debugging (#2000)
* WIP Signed-off-by: troychiu <[email protected]> * finish functionality Signed-off-by: troychiu <[email protected]> * add hint Signed-off-by: troychiu <[email protected]> * add util test Signed-off-by: troychiu <[email protected]> * fix suggestions Signed-off-by: troychiu <[email protected]> * lint Signed-off-by: troychiu <[email protected]> * add launch.json Signed-off-by: troychiu <[email protected]> * fix comment Signed-off-by: troychiu <[email protected]> * change generated file name Signed-off-by: troychiu <[email protected]> * fix suggestions Signed-off-by: troychiu <[email protected]> --------- Signed-off-by: troychiu <[email protected]>
- Loading branch information
Showing
8 changed files
with
173 additions
and
7 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
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,56 @@ | ||
import importlib | ||
import os | ||
import sys | ||
|
||
from flytekit.core import utils | ||
from flytekit.core.context_manager import FlyteContextManager | ||
from flyteidl.core import literals_pb2 as _literals_pb2 | ||
from flytekit.core.type_engine import TypeEngine | ||
from flytekit.models import literals as _literal_models | ||
|
||
|
||
def load_module_from_path(module_name, path): | ||
""" | ||
Imports a Python module from a specified file path. | ||
Args: | ||
module_name (str): The name you want to assign to the imported module. | ||
path (str): The file system path to the Python file (.py) that contains the module you want to import. | ||
Returns: | ||
module: The imported module. | ||
Raises: | ||
ImportError: If the module cannot be loaded from the provided path, an ImportError is raised. | ||
""" | ||
spec = importlib.util.spec_from_file_location(module_name, path) | ||
if spec is not None: | ||
module = importlib.util.module_from_spec(spec) | ||
sys.modules[module_name] = module | ||
spec.loader.exec_module(module) | ||
return module | ||
else: | ||
raise ImportError(f"Module at {path} could not be loaded") | ||
|
||
|
||
def get_task_inputs(task_module_name, task_name, context_working_dir): | ||
""" | ||
Read task input data from inputs.pb for a specific task function and convert it into Python types and structures. | ||
Args: | ||
task_module_name (str): The name of the Python module containing the task function. | ||
task_name (str): The name of the task function within the module. | ||
context_working_dir (str): The directory path where the input file and module file are located. | ||
Returns: | ||
dict: A dictionary containing the task inputs, converted into Python types and structures. | ||
""" | ||
local_inputs_file = os.path.join(context_working_dir, "inputs.pb") | ||
input_proto = utils.load_proto_from_file(_literals_pb2.LiteralMap, local_inputs_file) | ||
idl_input_literals = _literal_models.LiteralMap.from_flyte_idl(input_proto) | ||
task_module = load_module_from_path(task_module_name, os.path.join(context_working_dir, f"{task_module_name}.py")) | ||
task_def = getattr(task_module, task_name) | ||
native_inputs = TypeEngine.literal_map_to_kwargs( | ||
FlyteContextManager(), idl_input_literals, task_def.python_interface.inputs | ||
) | ||
return native_inputs |
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
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,20 @@ | ||
import os | ||
|
||
from flytekitplugins.flyin import get_task_inputs | ||
from flytekitplugins.flyin.utils import load_module_from_path | ||
|
||
|
||
def test_load_module_from_path(): | ||
module_name = "task" | ||
module_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testdata", "task.py") | ||
task_name = "t1" | ||
task_module = load_module_from_path(module_name, module_path) | ||
assert hasattr(task_module, task_name) | ||
task_def = getattr(task_module, task_name) | ||
assert task_def(a=6, b=3) == 2 | ||
|
||
|
||
def test_get_task_inputs(): | ||
test_working_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testdata") | ||
native_inputs = get_task_inputs("task", "t1", test_working_dir) | ||
assert native_inputs == {"a": 30, "b": 0} |
Binary file not shown.
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,8 @@ | ||
from flytekitplugins.flyin import vscode | ||
from flytekit import task | ||
|
||
|
||
@task() | ||
@vscode(run_task_first=True) | ||
def t1(a: int, b: int) -> int: | ||
return a // b |