-
Notifications
You must be signed in to change notification settings - Fork 25
/
compiler.py
965 lines (806 loc) · 34.3 KB
/
compiler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
import json
import logging
import os
import re
import shlex
import tempfile
import textwrap
import typing as t
from abc import ABC, abstractmethod
from dataclasses import asdict
from pathlib import Path
from urllib.parse import urlparse
import yaml
from fsspec.registry import known_implementations
from fondant.core.component_spec import ComponentSpec
from fondant.core.exceptions import InvalidPipelineDefinition
from fondant.core.manifest import Metadata
from fondant.core.schema import CloudCredentialsMount, DockerVolume
from fondant.pipeline import (
VALID_ACCELERATOR_TYPES,
VALID_VERTEX_ACCELERATOR_TYPES,
Image,
Pipeline,
)
logger = logging.getLogger(__name__)
# export DASK_DIAGNOSTICS_PORT="" to get a dynamic port assigned
DASK_DIAGNOSTICS_PORT = os.environ.get("DASK_DIAGNOSTICS_PORT", ":8787")
KubeflowCommandArguments = t.List[t.Union[str, t.Dict[str, str]]]
class Compiler(ABC):
"""Abstract base class for a compiler."""
@abstractmethod
def compile(self, *args, **kwargs) -> None:
"""Abstract method to invoke compilation."""
@abstractmethod
def _set_configuration(self, *args, **kwargs) -> None:
"""Abstract method to set pipeline configuration."""
def log_unused_configurations(self, **kwargs):
"""Log configurations that are set but will be unused."""
for config_name, config_value in kwargs.items():
if config_value is not None:
logger.warning(
f"Configuration `{config_name}` is set with `{config_value}` but has no effect"
f" for runner `{self.__class__.__name__}`.",
)
@staticmethod
def _build_entrypoint(image: Image) -> t.List[str]:
"""Build the entrypoint to execute the provided image."""
if not image.script:
# Not a lightweight python component
return ["fondant", "execute", "main"]
command = ""
if image.extra_requires:
requirements = "\n".join(image.extra_requires)
command += textwrap.dedent(
f"""\
printf {shlex.quote(requirements)} > 'requirements.txt'
python3 -m pip install -r requirements.txt
""",
)
command += textwrap.dedent(
f"""\
printf {shlex.quote(image.script)} > 'main.py'
fondant execute main "$@"
""",
)
return [
"sh",
"-ec",
command,
"--", # All arguments provided after this will be passed to `fondant execute main`
]
class DockerCompiler(Compiler):
"""Compiler that creates a docker-compose spec from a pipeline."""
def compile(
self,
pipeline: Pipeline,
*,
output_path: str = "docker-compose.yml",
extra_volumes: t.Union[t.Optional[list], t.Optional[str]] = None,
build_args: t.Optional[t.List[str]] = None,
auth_provider: t.Optional[CloudCredentialsMount] = None,
) -> None:
"""Compile a pipeline to docker-compose spec and save it to a specified output path.
Args:
pipeline: the pipeline to compile
output_path: the path where to save the docker-compose spec
extra_volumes: a list of extra volumes (using the Short syntax:
https://docs.docker.com/compose/compose-file/05-services/#short-syntax-5)
to mount in the docker-compose spec.
build_args: List of build arguments to pass to docker
auth_provider: The cloud provider to use for authentication. Default is None.
"""
if extra_volumes is None:
extra_volumes = []
if isinstance(extra_volumes, str):
extra_volumes = [extra_volumes]
if auth_provider:
extra_volumes.append(auth_provider.get_path())
logger.info(f"Compiling {pipeline.name} to {output_path}")
spec = self._generate_spec(
pipeline,
extra_volumes=extra_volumes,
build_args=build_args or [],
)
class NoAliasDumper(yaml.SafeDumper):
def ignore_aliases(self, data):
return True
with open(output_path, "w") as outfile:
yaml.dump(spec, outfile, Dumper=NoAliasDumper, default_flow_style=False)
logger.info(f"Successfully compiled to {output_path}")
@staticmethod
def _patch_path(base_path: str) -> t.Tuple[str, t.Optional[DockerVolume]]:
"""Helper that checks if the base_path is local or remote,
if local it patches the base_path and prepares a bind mount
Returns a tuple containing the path and volume.
"""
def is_remote_path(path: Path) -> bool:
"""Check if the path is remote."""
scheme = urlparse(str(path)).scheme
fsspec_schemes = set(known_implementations.keys()) - {"local", "file"}
return scheme in fsspec_schemes
def resolve_local_base_path(base_path: Path) -> Path:
"""Resolve local base path and create base directory if it no exist."""
p_base_path = base_path.resolve()
try:
if p_base_path.exists():
logger.info(
f"Base path found on local system, setting up {base_path} as mount volume",
)
else:
p_base_path.mkdir(parents=True, exist_ok=True)
logger.info(
f"Base path not found on local system, created base path and setting up "
f"{base_path} as mount volume",
)
except Exception as e:
msg = f"Unable to create and mount local base path. {e}"
raise ValueError(msg)
return p_base_path
p_base_path = Path(base_path)
if is_remote_path(p_base_path):
logger.info(f"Base path {base_path} is remote")
return base_path, None
p_base_path = resolve_local_base_path(p_base_path)
volume = DockerVolume(
type="bind",
source=str(p_base_path),
target=f"/{p_base_path.stem}",
)
path = f"/{p_base_path.stem}"
return path, volume
def _generate_spec(
self,
pipeline: Pipeline,
*,
extra_volumes: t.List[str],
build_args: t.List[str],
) -> dict:
"""Generate a docker-compose spec as a python dictionary,
loops over the pipeline graph to create services and their dependencies.
"""
path, volume = self._patch_path(base_path=pipeline.base_path)
run_id = pipeline.get_run_id()
services = {}
pipeline.validate(run_id=run_id)
component_cache_key = None
for component_id, component in pipeline._graph.items():
component_op = component["operation"]
component_cache_key = component_op.get_component_cache_key(
previous_component_cache=component_cache_key,
)
metadata = Metadata(
pipeline_name=pipeline.name,
run_id=run_id,
base_path=path,
component_id=component_id,
cache_key=component_cache_key,
)
logger.info(f"Compiling service for {component_id}")
entrypoint = self._build_entrypoint(component_op.image)
# add metadata argument to command
command = ["--metadata", metadata.to_json()]
# add in and out manifest paths to command
command.extend(
[
"--output_manifest_path",
f"{path}/{metadata.pipeline_name}/{metadata.run_id}/"
f"{component_id}/manifest.json",
],
)
# add arguments if any to command
for key, value in component_op.arguments.items():
if isinstance(value, (dict, list)):
command.extend([f"--{key}", json.dumps(value)])
else:
command.extend([f"--{key}", f"{value}"])
# resolve dependencies
depends_on = {}
if component["dependencies"]:
for dependency in component["dependencies"]:
depends_on[dependency] = {
"condition": "service_completed_successfully",
}
# there is only an input manifest if the component has dependencies
command.extend(
[
"--input_manifest_path",
f"{path}/{metadata.pipeline_name}/{metadata.run_id}/"
f"{dependency}/manifest.json",
],
)
volumes: t.List[t.Union[str, dict]] = []
if volume:
volumes.append(asdict(volume))
if extra_volumes:
volumes.extend(extra_volumes)
ports = [f"8787{DASK_DIAGNOSTICS_PORT}"]
services[component_id] = {
"entrypoint": entrypoint,
"command": command,
"depends_on": depends_on,
"volumes": volumes,
"ports": ports,
"labels": {
"pipeline_description": pipeline.description,
},
}
self._set_configuration(services, component_op, component_id)
if component_op.dockerfile_path is not None:
logger.info(
f"Found Dockerfile for {component_id}, adding build step.",
)
services[component_id]["build"] = {
"context": str(component_op.component_dir.absolute()),
"args": build_args,
}
else:
services[component_id]["image"] = component_op.component_spec.image
return {
"name": pipeline.name,
"version": "3.8",
"services": services,
}
def _set_configuration(self, services, fondant_component_operation, component_id):
resources_dict = fondant_component_operation.resources.to_dict()
accelerator_name = resources_dict.pop("accelerator_name")
accelerator_number = resources_dict.pop("accelerator_number")
# Unused configurations
self.log_unused_configurations(**resources_dict)
if accelerator_name is not None:
if accelerator_name not in VALID_ACCELERATOR_TYPES:
msg = (
f"Configured accelerator `{accelerator_name}`"
f" is not a valid accelerator type for Docker Compose compiler."
f" Available options: {VALID_VERTEX_ACCELERATOR_TYPES}"
)
raise InvalidPipelineDefinition(msg)
if accelerator_name == "GPU":
services[component_id]["deploy"] = {
"resources": {
"reservations": {
"devices": [
{
"driver": "nvidia",
"count": accelerator_number,
"capabilities": ["gpu"],
},
],
},
},
}
elif accelerator_name == "TPU":
msg = "TPU configuration is not yet implemented for Docker Compose "
raise NotImplementedError(msg)
return services
class KubeflowComponentSpec:
"""
Class representing a Kubeflow component specification.
Args:
specification: The kubeflow component specification as a Python dict
"""
def __init__(self, specification: t.Dict[str, t.Any]) -> None:
self._specification = specification
@staticmethod
def convert_arguments(fondant_component: ComponentSpec):
args = {}
for arg in fondant_component.args.values():
arg_type_dict = {}
# Enable isOptional attribute in spec if arg is Optional and defaults to None
if arg.optional and arg.default is None:
arg_type_dict["isOptional"] = True
if arg.default is not None:
arg_type_dict["defaultValue"] = arg.default
args[arg.name] = {
"parameterType": arg.kubeflow_type,
"description": arg.description,
**arg_type_dict, # type: ignore
}
return args
@classmethod
def from_fondant_component_spec(
cls,
fondant_component: ComponentSpec,
command: t.List[str],
image_uri: str,
):
"""Generate a Kubeflow component spec from a Fondant component spec."""
input_definitions = {
"parameters": {
**cls.convert_arguments(fondant_component),
},
}
kfp_safe_name = (
re.sub(
"-+",
"-",
re.sub("[^-0-9a-z]+", "-", fondant_component.safe_name.lower()),
)
.lstrip("-")
.rstrip("-")
)
specification = {
"components": {
"comp-"
+ kfp_safe_name: {
"executorLabel": "exec-" + kfp_safe_name,
"inputDefinitions": input_definitions,
},
},
"deploymentSpec": {
"executors": {
"exec-"
+ kfp_safe_name: {
"container": {
"command": command,
"image": image_uri,
},
},
},
},
"pipelineInfo": {"name": kfp_safe_name},
"root": {
"dag": {
"tasks": {
kfp_safe_name: {
"cachingOptions": {"enableCache": True},
"componentRef": {"name": "comp-" + kfp_safe_name},
"inputs": {
"parameters": {
param: {"componentInputParameter": param}
for param in input_definitions["parameters"]
},
},
"taskInfo": {"name": kfp_safe_name},
},
},
},
"inputDefinitions": input_definitions,
},
"schemaVersion": "2.1.0",
"sdkVersion": "kfp-2.6.0",
}
return cls(specification)
def to_file(self, path: t.Union[str, Path]) -> None:
"""Dump the component specification to the file specified by the provided path."""
with open(path, "w", encoding="utf-8") as file_:
yaml.dump(
self._specification,
file_,
indent=4,
default_flow_style=False,
sort_keys=False,
)
def to_string(self) -> str:
"""Return the component specification as a string."""
return json.dumps(self._specification)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self._specification!r})"
class KubeFlowCompiler(Compiler):
"""Compiler that creates a Kubeflow pipeline spec from a pipeline."""
def __init__(self):
self._resolve_imports()
def _resolve_imports(self):
"""Resolve imports for the Kubeflow compiler."""
try:
import kfp
import kfp.kubernetes as kfp_kubernetes
self.kfp = kfp
self.kfp_kubernetes = kfp_kubernetes
except ImportError:
msg = """You need to install kfp to use the Kubeflow compiler,\n
you can install it with `pip install fondant[kfp]`"""
raise ImportError(
msg,
)
def compile(
self,
pipeline: Pipeline,
output_path: str,
) -> None:
"""Compile a pipeline to Kubeflow 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}")
def set_component_exec_args(
*,
component_op,
component_args: t.List[str],
input_manifest_path: bool,
):
"""Dump Fondant specification arguments to kfp command executor arguments."""
dumped_args: KubeflowCommandArguments = []
component_args.extend(["output_manifest_path", "metadata"])
if input_manifest_path:
component_args.append("input_manifest_path")
for arg in component_args:
arg_name = arg.strip().replace(" ", "_")
arg_name_cmd = f"--{arg_name}"
dumped_args.append(arg_name_cmd)
dumped_args.append("{{$.inputs.parameters['" + f"{arg_name}" + "']}}")
component_op.component_spec.implementation.container.args = dumped_args
return component_op
@self.kfp.dsl.pipeline(name=pipeline.name, description=pipeline.description)
def kfp_pipeline():
previous_component_task = None
component_cache_key = None
for component_name, component in pipeline._graph.items():
logger.info(f"Compiling service for {component_name}")
component_op = component["operation"]
# convert ComponentOp to Kubeflow component
command = self._build_entrypoint(component_op.image)
image_uri = component_op.image.base_image
kubeflow_spec = KubeflowComponentSpec.from_fondant_component_spec(
component_op.component_spec,
command=command,
image_uri=image_uri,
)
kubeflow_component_op = self.kfp.components.load_component_from_text(
text=kubeflow_spec.to_string(),
)
# Remove None values from arguments
component_args = {
k: v for k, v in component_op.arguments.items() if v is not None
}
component_cache_key = component_op.get_component_cache_key(
previous_component_cache=component_cache_key,
)
metadata = Metadata(
pipeline_name=pipeline.name,
run_id=run_id,
base_path=pipeline.base_path,
component_id=component_name,
cache_key=component_cache_key,
)
output_manifest_path = (
f"{pipeline.base_path}/{pipeline.name}/"
f"{run_id}/{component_name}/manifest.json"
)
# Set the execution order of the component task to be after the previous
# component task.
if component["dependencies"]:
for dependency in component["dependencies"]:
input_manifest_path = (
f"{pipeline.base_path}/{pipeline.name}/"
f"{run_id}/{dependency}/manifest.json"
)
kubeflow_component_op = set_component_exec_args(
component_op=kubeflow_component_op,
component_args=list(component_args.keys()),
input_manifest_path=True,
)
component_task = kubeflow_component_op(
input_manifest_path=input_manifest_path,
output_manifest_path=output_manifest_path,
metadata=metadata.to_json(),
**component_args,
)
component_task.after(previous_component_task)
else:
kubeflow_component_op = set_component_exec_args(
component_op=kubeflow_component_op,
component_args=list(component_args.keys()),
input_manifest_path=False,
)
component_task = kubeflow_component_op(
metadata=metadata.to_json(),
output_manifest_path=output_manifest_path,
**component_args,
)
# Set optional configuration
component_task = self._set_configuration(
component_task,
component_op,
)
# Disable caching
component_task.set_caching_options(enable_caching=False)
previous_component_task = component_task
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")
def _set_configuration(self, task, fondant_component_operation):
# Used configurations
resources_dict = fondant_component_operation.resources.to_dict()
accelerator_number = resources_dict.pop("accelerator_number")
accelerator_name = resources_dict.pop("accelerator_name")
node_pool_label = resources_dict.pop("node_pool_label")
node_pool_name = resources_dict.pop("node_pool_name")
cpu_request = resources_dict.pop("cpu_request")
cpu_limit = resources_dict.pop("cpu_limit")
memory_request = resources_dict.pop("memory_request")
memory_limit = resources_dict.pop("memory_limit")
# Unused configurations
self.log_unused_configurations(**resources_dict)
# Assign optional specification
if cpu_request is not None:
task.set_memory_request(cpu_request)
if cpu_limit is not None:
task.set_memory_limit(cpu_limit)
if memory_request is not None:
task.set_memory_request(memory_request)
if memory_limit is not None:
task.set_memory_limit(memory_limit)
if accelerator_name is not None:
if accelerator_name not in VALID_ACCELERATOR_TYPES:
msg = (
f"Configured accelerator `{accelerator_name}` is not a valid accelerator type"
f"for Kubeflow compiler. Available options: {VALID_ACCELERATOR_TYPES}"
)
raise InvalidPipelineDefinition(msg)
task.set_accelerator_limit(accelerator_number)
if accelerator_name == "GPU":
task.set_accelerator_type("nvidia.com/gpu")
elif accelerator_name == "TPU":
task.set_accelerator_type("cloud-tpus.google.com/v3")
if node_pool_name is not None and node_pool_label is not None:
task = self.kfp_kubernetes.add_node_selector(
task,
node_pool_label,
node_pool_name,
)
return task
class VertexCompiler(KubeFlowCompiler):
def __init__(self):
super().__init__()
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 _set_configuration(self, task, fondant_component_operation):
# Used configurations
resources_dict = fondant_component_operation.resources.to_dict()
cpu_limit = resources_dict.pop("cpu_limit")
memory_limit = resources_dict.pop("memory_limit")
accelerator_number = resources_dict.pop("accelerator_number")
accelerator_name = resources_dict.pop("accelerator_name")
# Unused configurations
self.log_unused_configurations(**resources_dict)
# Assign optional specification
if cpu_limit is not None:
task.set_cpu_limit(cpu_limit)
if memory_limit is not None:
task.set_memory_limit(memory_limit)
if accelerator_number is not None:
task.set_accelerator_limit(accelerator_number)
if accelerator_name not in VALID_VERTEX_ACCELERATOR_TYPES:
msg = (
f"Configured accelerator `{accelerator_name}` is not a valid accelerator type"
f"for Vertex compiler. Available options: {VALID_VERTEX_ACCELERATOR_TYPES}"
)
raise InvalidPipelineDefinition(msg)
task.set_accelerator_type(accelerator_name)
return task
class SagemakerCompiler(Compiler): # pragma: no cover
def __init__(self):
self.ecr_namespace = "fndnt-mirror"
self._resolve_imports()
def _resolve_imports(self):
try:
import boto3
import sagemaker
import sagemaker.processing
import sagemaker.workflow.pipeline
import sagemaker.workflow.steps
self.boto3 = boto3
self.sagemaker = sagemaker
except ImportError:
msg = """You need to install the sagemaker extras to use the sagemaker compiler,\n
you can install it with `pip install fondant[sagemaker]`"""
raise ImportError(
msg,
)
def _build_command(
self,
metadata: Metadata,
arguments: t.Dict[str, t.Any],
dependencies: t.List[str] = [],
) -> t.List[str]:
# add metadata argument to command
command = ["--metadata", f"'{metadata.to_json()}'"]
# add in and out manifest paths to command
command.extend(
[
"--output_manifest_path",
f"{metadata.base_path}/{metadata.pipeline_name}/{metadata.run_id}/"
f"{metadata.component_id}/manifest.json",
],
)
# add arguments if any to command
for key, value in arguments.items():
if isinstance(value, (dict, list)):
command.extend([f"--{key}", f"'{json.dumps(value)}'"])
else:
command.extend([f"--{key}", f"'{value}'"])
# resolve dependencies
if dependencies:
for dependency in dependencies:
# there is only an input manifest if the component has dependencies
command.extend(
[
"--input_manifest_path",
f"{metadata.base_path}/{metadata.pipeline_name}/{metadata.run_id}/"
f"{dependency}/manifest.json",
],
)
return command
def _check_ecr_pull_through_rule(self) -> None:
logging.info(
f"Checking existing pull through cache rules for '{self.ecr_namespace}'",
)
try:
self.ecr_client.describe_pull_through_cache_rules(
ecrRepositoryPrefixes=[self.ecr_namespace],
)
except self.ecr_client.exceptions._code_to_exception[
"PullThroughCacheRuleNotFoundException"
]:
logging.info(
f"""Pull through cache rule for '{self.ecr_namespace}' not found..
creating pull through cache rule for '{self.ecr_namespace}'""",
)
self.ecr_client.create_pull_through_cache_rule(
ecrRepositoryPrefix=self.ecr_namespace,
upstreamRegistryUrl="public.ecr.aws",
)
logging.info(
f"Pull through cache rule for '{self.ecr_namespace}' created successfully",
)
def _patch_uri(self, og_uri: str) -> str:
full_ref, tag = og_uri.split(":")
ref, *repo = full_ref.split("/")
def pull_through(repository_name):
_ = self.ecr_client.batch_get_image(
repositoryName=repository_name,
imageIds=[{"imageTag": tag}],
)
repo_response = self.ecr_client.describe_repositories(
repositoryNames=[repository_name],
)
return repo_response["repositories"][0]["repositoryUri"] + ":" + tag
if ref == "fndnt":
logging.info("Reusable component detected, patching URI")
uri = pull_through(f"{self.ecr_namespace}/{full_ref}")
elif ref == "public.ecr.aws":
logging.info("Public AWS ECR component detected, patching URI")
uri = pull_through(f"{self.ecr_namespace}/{'/'.join(repo)}")
else:
logging.info("Custom component detected")
# the uri does not need patching
uri = og_uri
return uri
def validate_base_path(self, base_path: str) -> None:
if not base_path.startswith("s3://"):
msg = "base_path must be a valid s3 path, starting with s3://"
raise ValueError(msg)
if base_path.endswith("/"):
msg = "base_path must not end with a '/'"
raise ValueError(msg)
def compile(
self,
pipeline: Pipeline,
output_path: str,
*,
role_arn: t.Optional[str] = None,
) -> None:
"""Compile a fondant pipeline to sagemaker pipeline spec and save it
to a specified output path.
Args:
pipeline: the pipeline to compile
output_path: the path where to save the sagemaker pipeline spec.
role_arn: the Amazon Resource Name role to use for the processing steps,
if none provided the `sagemaker.get_execution_role()` role will be used.
"""
self.ecr_client = self.boto3.client("ecr")
self.validate_base_path(pipeline.base_path)
self._check_ecr_pull_through_rule()
run_id = pipeline.get_run_id()
path = pipeline.base_path
pipeline.validate(run_id=run_id)
component_cache_key = None
steps: t.List[t.Any] = []
with tempfile.TemporaryDirectory(dir=os.getcwd()) as tmpdirname:
for component_name, component in pipeline._graph.items():
component_op = component["operation"]
component_cache_key = component_op.get_component_cache_key(
previous_component_cache=component_cache_key,
)
metadata = Metadata(
pipeline_name=pipeline.name,
run_id=run_id,
base_path=path,
component_id=component_name,
cache_key=component_cache_key,
)
logger.info(f"Compiling service for {component_name}")
command = self._build_command(
metadata,
component_op.arguments,
component["dependencies"],
)
depends_on = [steps[-1]] if component["dependencies"] else []
image = component_op.image
entrypoint = self._build_entrypoint(image)
script_path = self.generate_component_script(
entrypoint=entrypoint,
command=command,
component_name=component_name,
directory=tmpdirname,
)
if not role_arn:
# if no role is provided use the default sagemaker execution role
# https://docs.aws.amazon.com/sagemaker/latest/dg/automatic-model-tuning-ex-role.html
role_arn = self.sagemaker.get_execution_role()
resources_dict = self._set_configuration(component_op)
processor = self.sagemaker.processing.ScriptProcessor(
image_uri=self._patch_uri(image.base_image),
command=["bash"],
instance_count=1,
base_job_name=component_name,
role=role_arn,
**resources_dict,
)
step = self.sagemaker.workflow.steps.ProcessingStep(
name=component_name,
processor=processor,
depends_on=depends_on,
code=script_path,
)
steps.append(step)
sagemaker_pipeline = self.sagemaker.workflow.pipeline.Pipeline(
name=pipeline.name,
steps=steps,
)
with open(output_path, "w") as outfile:
json.dump(
json.loads(sagemaker_pipeline.definition()),
outfile,
indent=4,
)
def _set_configuration(
self,
fondant_component_operation,
*args,
**kwargs,
):
# Used configurations
resources_dict = fondant_component_operation.resources.to_dict()
instance_type = resources_dict.pop("instance_type")
if not instance_type:
logger.warning(
"""No instance type provided, using default `ml.t3.medium`. See:
https://docs.aws.amazon.com/sagemaker/latest/dg/notebooks-available-instance-types.html
for options""",
)
instance_type = "ml.t3.medium"
# Unused configurations
self.log_unused_configurations(**resources_dict)
return {"instance_type": instance_type}
@staticmethod
def generate_component_script(
*,
entrypoint: t.List[str],
command: t.List[str],
component_name: str,
directory: str,
) -> str:
"""Generate a bash script for a component to be used as input in a
sagemaker pipeline step. Returns the path to the script.
"""
# use shlex.quote to escape special bash chars
command_string = [arg.replace("'", "") for arg in command]
cleaned_script = shlex.join([*entrypoint, *command_string])
with open(f"{directory}/{component_name}.sh", "w") as f:
f.write(cleaned_script)
return f"{directory}/{component_name}.sh"