Skip to content

Commit df40f68

Browse files
committed
Backport fixes in #11075
Introduced back the functions to convert k8s size values to float, but moved to kfp.dsl.utils Signed-off-by: Ricardo M. Oliveira <rmartine@redhat.com>
1 parent c57e897 commit df40f68

File tree

3 files changed

+86
-9
lines changed

3 files changed

+86
-9
lines changed

sdk/python/kfp/compiler/pipeline_spec_builder.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -645,19 +645,19 @@ def convert_to_placeholder(input_value: str) -> str:
645645

646646
if task.container_spec.resources is not None:
647647
if task.container_spec.resources.cpu_request is not None:
648-
container_spec.resources.resource_cpu_request = (
649-
convert_to_placeholder(
648+
container_spec.resources.cpu_request = utils.validate_cpu_request_limit_to_float(
649+
cpu = convert_to_placeholder(
650650
task.container_spec.resources.cpu_request))
651651
if task.container_spec.resources.cpu_limit is not None:
652-
container_spec.resources.resource_cpu_limit = (
653-
convert_to_placeholder(task.container_spec.resources.cpu_limit))
652+
container_spec.resources.cpu_limit = utils.validate_cpu_request_limit_to_float(
653+
cpu = convert_to_placeholder(task.container_spec.resources.cpu_limit))
654654
if task.container_spec.resources.memory_request is not None:
655-
container_spec.resources.resource_memory_request = (
656-
convert_to_placeholder(
655+
container_spec.resources.memory_request = utils.validate_memory_request_limit_to_float(
656+
memory = convert_to_placeholder(
657657
task.container_spec.resources.memory_request))
658658
if task.container_spec.resources.memory_limit is not None:
659-
container_spec.resources.resource_memory_limit = (
660-
convert_to_placeholder(
659+
container_spec.resources.memory_limit = utils.validate_memory_request_limit_to_float(
660+
memory = convert_to_placeholder(
661661
task.container_spec.resources.memory_limit))
662662
if task.container_spec.resources.accelerator_count is not None:
663663
container_spec.resources.accelerator.CopyFrom(

sdk/python/kfp/dsl/pipeline_task.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ def _validate_cpu_request_limit(self, cpu: str) -> str:
346346
'Invalid cpu string. Should be float or integer, or integer'
347347
' followed by "m".')
348348
return cpu
349-
349+
350350
@block_if_final()
351351
def set_cpu_request(
352352
self,

sdk/python/kfp/dsl/utils.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import types
2121
from typing import List
2222

23+
from kfp.dsl import constants
24+
2325
COMPONENT_NAME_PREFIX = 'comp-'
2426
_EXECUTOR_LABEL_PREFIX = 'exec-'
2527

@@ -126,3 +128,78 @@ def validate_pipeline_name(name: str) -> None:
126128
'Please specify a pipeline name that matches the regular '
127129
'expression "^[a-z0-9][a-z0-9-]{0,127}$" using '
128130
'`dsl.pipeline(name=...)` decorator.' % name)
131+
132+
def validate_cpu_request_limit_to_float(cpu: str) -> float:
133+
"""Validates cpu request/limit string and converts to its numeric
134+
float value.
135+
136+
Args:
137+
cpu: CPU requests or limits. This string should be a number or a
138+
number followed by an "m" to indicate millicores (1/1000). For
139+
more information, see `Specify a CPU Request and a CPU Limit
140+
<https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/#specify-a-cpu-request-and-a-cpu-limit>`_.
141+
142+
Raises:
143+
ValueError if the cpu request/limit string value is invalid.
144+
145+
Returns:
146+
The numeric value (float) of the cpu request/limit.
147+
"""
148+
if re.match(r'([0-9]*[.])?[0-9]+m?$', cpu) is None:
149+
raise ValueError(
150+
'Invalid cpu string. Should be float or integer, or integer'
151+
' followed by "m".')
152+
153+
return float(cpu[:-1]) / 1000 if cpu.endswith('m') else float(cpu)
154+
155+
def validate_memory_request_limit_to_float(memory: str) -> float:
156+
"""Validates memory request/limit string and converts to its numeric
157+
value.
158+
159+
Args:
160+
memory: Memory requests or limits. This string should be a number or
161+
a number followed by one of "E", "Ei", "P", "Pi", "T", "Ti", "G",
162+
"Gi", "M", "Mi", "K", or "Ki".
163+
164+
Raises:
165+
ValueError if the memory request/limit string value is invalid.
166+
167+
Returns:
168+
The numeric value (float) of the memory request/limit.
169+
"""
170+
if re.match(r'^[0-9]+(E|Ei|P|Pi|T|Ti|G|Gi|M|Mi|K|Ki){0,1}$',
171+
memory) is None:
172+
raise ValueError(
173+
'Invalid memory string. Should be a number or a number '
174+
'followed by one of "E", "Ei", "P", "Pi", "T", "Ti", "G", '
175+
'"Gi", "M", "Mi", "K", "Ki".')
176+
177+
if memory.endswith('E'):
178+
memory = float(memory[:-1]) * constants._E / constants._G
179+
elif memory.endswith('Ei'):
180+
memory = float(memory[:-2]) * constants._EI / constants._G
181+
elif memory.endswith('P'):
182+
memory = float(memory[:-1]) * constants._P / constants._G
183+
elif memory.endswith('Pi'):
184+
memory = float(memory[:-2]) * constants._PI / constants._G
185+
elif memory.endswith('T'):
186+
memory = float(memory[:-1]) * constants._T / constants._G
187+
elif memory.endswith('Ti'):
188+
memory = float(memory[:-2]) * constants._TI / constants._G
189+
elif memory.endswith('G'):
190+
memory = float(memory[:-1])
191+
elif memory.endswith('Gi'):
192+
memory = float(memory[:-2]) * constants._GI / constants._G
193+
elif memory.endswith('M'):
194+
memory = float(memory[:-1]) * constants._M / constants._G
195+
elif memory.endswith('Mi'):
196+
memory = float(memory[:-2]) * constants._MI / constants._G
197+
elif memory.endswith('K'):
198+
memory = float(memory[:-1]) * constants._K / constants._G
199+
elif memory.endswith('Ki'):
200+
memory = float(memory[:-2]) * constants._KI / constants._G
201+
else:
202+
# By default interpret as a plain integer, in the unit of Bytes.
203+
memory = float(memory) / constants._G
204+
205+
return memory

0 commit comments

Comments
 (0)