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

Feature/vertex compiler #411

Merged
merged 9 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ classifiers = [
]

[tool.poetry.dependencies]
python = ">= 3.8"
python = ">= 3.8 < 3.12"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kfp(v2) only supports up to python 3.12

dask = {extras = ["dataframe"], version = ">= 2023.4.1"}
importlib-resources = { version = ">= 1.3", python = "<3.9" }
jsonschema = ">= 4.18"
Expand All @@ -51,14 +51,15 @@ fsspec = { version = ">= 2023.4.0", optional = true}
gcsfs = { version = ">= 2023.4.0", optional = true }
s3fs = { version = ">= 2023.4.0", optional = true }
adlfs = { version = ">= 2023.4.0", optional = true }
kfp = { version = ">= 1.8.19, < 2", optional = true }
kfp = { version = "2.0.1", optional = true }
pandas = { version = ">= 1.3.5", optional = true }

[tool.poetry.extras]
aws = ["fsspec", "s3fs"]
azure = ["fsspec", "adlfs"]
gcp = ["fsspec", "gcsfs"]
kfp = ["kfp"]
vertex = ["kfp"]

[tool.poetry.group.test.dependencies]
pre-commit = "^3.1.1"
Expand Down
118 changes: 101 additions & 17 deletions src/fondant/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def compile(
output_path: the path where to save the Kubeflow pipeline spec
"""
run_id = pipeline.get_run_id()
pipeline.validate(run_id=run_id)

@self.kfp.dsl.pipeline(name=pipeline.name, description=pipeline.description)
def kfp_pipeline():
Expand All @@ -263,28 +264,32 @@ def kfp_pipeline():
logger.info(f"Compiling service for {component_name}")

# convert ComponentOp to Kubeflow component
kubeflow_component_op = self.kfp.components.load_component(
kubeflow_component_op = self.kfp.components.load_component_from_text(
text=component_op.component_spec.kubeflow_specification.to_string(),
)

# # Set image pull policy to always
# Execute the Kubeflow component and pass in the output manifest path from
# the previous component.
component_args = component_op.arguments

component_task = kubeflow_component_op(
input_manifest_path=manifest_path,
metadata=metadata.to_json(),
**component_args,
)
# Set optional configurations
component_task = self._set_configuration(
component_task,
component_op,
)

# Set image pull policy to always
component_task.container.set_image_pull_policy("Always")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be nice to keep the image pull policy (unless it's another function in v2 in which case we can tackle it separately or create a task for it)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added as a task: #393

if previous_component_task is not None:
component_task = kubeflow_component_op(
input_manifest_path=manifest_path,
metadata=metadata.to_json(),
**component_args,
)
component_task.after(previous_component_task)

else:
component_task = kubeflow_component_op(
metadata=metadata.to_json(),
**component_args,
)
component_task
# Set optional configurations
# component_task,
# component_op,
# Set the execution order of the component task to be after the previous
# component task.
if previous_component_task is not None:
Expand All @@ -295,9 +300,7 @@ def kfp_pipeline():

previous_component_task = component_task

self.pipeline = pipeline
self.pipeline.validate(run_id=run_id)
logger.info(f"Compiling {self.pipeline.name} to {output_path}")
logger.info(f"Compiling {pipeline.name} to {output_path}")

self.kfp.compiler.Compiler().compile(kfp_pipeline, output_path) # type: ignore
logger.info("Pipeline compiled successfully")
Expand All @@ -315,3 +318,84 @@ def _set_configuration(self, task, fondant_component_operation):
task.add_node_selector_constraint(node_pool_label, node_pool_name)

return task


class VertexCompiler(Compiler):
def __init__(self):
self.resolve_imports()

def resolve_imports(self):
"""Resolve imports for the Vertex compiler."""
try:
import kfp

self.kfp = kfp

except ImportError:
msg = """You need to install kfp to use the Vertex compiler,\n
you can install it with `pip install fondant[vertex]`"""
raise ImportError(
msg,
)

def compile(
self,
pipeline: Pipeline,
output_path: str = "vertex_pipeline.yml",
) -> None:
"""Compile a pipeline to vertex pipeline spec and save it to a specified output path.

Args:
pipeline: the pipeline to compile
output_path: the path where to save the Kubeflow pipeline spec
"""
run_id = pipeline.get_run_id()
pipeline.validate(run_id=run_id)
logger.info(f"Compiling {pipeline.name} to {output_path}")

@self.kfp.dsl.pipeline(name=pipeline.name, description=pipeline.description)
def kfp_pipeline():
previous_component_task = None
manifest_path = None
for component_name, component in pipeline._graph.items():
logger.info(f"Compiling service for {component_name}")

component_op = component["fondant_component_op"]
# convert ComponentOp to Kubeflow component
kubeflow_component_op = self.kfp.components.load_component_from_text(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this is the only difference between the two compiler (the SDK changed for this function). Can we perhaps reuse the same function instead of defining it twice. Seems like there is quite a bit of overlap

text=component_op.component_spec.kubeflow_specification.to_string(),
)

# Execute the Kubeflow component and pass in the output manifest path from
# the previous component.

component_args = component_op.arguments
metadata = Metadata(
pipeline_name=pipeline.name,
run_id=run_id,
base_path=pipeline.base_path,
component_id=component_name,
cache_key=component_op.get_component_cache_key(),
)
# Set the execution order of the component task to be after the previous
# component task.
if previous_component_task is not None:
component_task = kubeflow_component_op(
input_manifest_path=manifest_path,
metadata=metadata.to_json(),
**component_args,
)
component_task.after(previous_component_task)

else:
component_task = kubeflow_component_op(
metadata=metadata.to_json(),
**component_args,
)
# Update the manifest path to be the output path of the current component task.
manifest_path = component_task.outputs["output_manifest_path"]

previous_component_task = component_task

self.kfp.compiler.Compiler().compile(kfp_pipeline, output_path) # type: ignore
logger.info("Pipeline compiled successfully")
Loading