Skip to content

Commit 516bee8

Browse files
authored
Merge pull request #52 from Project-MONAI/mem_resource
Allow setting memory resource with string
2 parents 86e23b1 + 5200643 commit 516bee8

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

monai/deploy/core/application.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from monai.deploy.core.models import ModelFactory
2121
from monai.deploy.exceptions import IOMappingError
2222
from monai.deploy.utils.importutil import get_class_file_path, get_docstring, is_subclass
23+
from monai.deploy.utils.sizeutil import convert_bytes
2324

2425
from .app_context import AppContext
2526
from .datastores import DatastoreFactory
@@ -303,7 +304,7 @@ def get_package_info(self, model_path: Union[str, Path] = "") -> Dict:
303304
"resource": {
304305
"cpu": resource.cpu,
305306
"gpu": resource.gpu,
306-
"memory": resource.memory,
307+
"memory": convert_bytes(resource.memory),
307308
},
308309
"models": model_list,
309310
"pip-packages": pip_requirement_list,

monai/deploy/core/operator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def decorator(cls):
182182
else:
183183
raise UnknownTypeError("Use @input decorator only for a subclass of Operator!")
184184

185-
def new_builder(self):
185+
def new_builder(self: Operator):
186186
if builder:
187187
builder(self) # execute the original builder
188188
self.add_input(label, data_type, storage_type)
@@ -212,7 +212,7 @@ def decorator(cls):
212212
else:
213213
raise UnknownTypeError("Use @output decorator only for a subclass of Operator!")
214214

215-
def new_builder(self):
215+
def new_builder(self: Operator):
216216
if builder:
217217
builder(self) # execute the original builder
218218
self.add_output(label, data_type, storage_type)

monai/deploy/core/resource.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
from typing import Optional, Union
1313

14-
from monai.deploy.exceptions import ItemAlreadyExistsError, UnknownTypeError, UnsupportedOperationError
14+
from monai.deploy.exceptions import ItemAlreadyExistsError, UnknownTypeError, WrongValueError
15+
from monai.deploy.utils.sizeutil import get_bytes
1516

1617

1718
class Resource:
@@ -75,8 +76,10 @@ def set_resource_limits(
7576
)
7677

7778
if type(memory_limit) == str:
78-
# TODO(gigony): convert str to int
79-
raise UnsupportedOperationError("Memory limit must be an integer.")
79+
try:
80+
self._memory = get_bytes(memory_limit)
81+
except Exception as e:
82+
raise WrongValueError(f"Memory size specified in the application (via @resource) is not valid: {e.args[0]}")
8083
else:
8184
if memory_limit is not None:
8285
if self._memory is None:
@@ -116,7 +119,7 @@ def decorator(cls):
116119
else:
117120
raise UnknownTypeError("Use @resource decorator only for a subclass of Application!")
118121

119-
def new_builder(self):
122+
def new_builder(self: Application):
120123
if builder:
121124
builder(self) # execute the original builder
122125

monai/deploy/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class UnknownTypeError(MONAIAppSdkError):
3939

4040
pass
4141

42+
class WrongValueError(MONAIAppSdkError):
43+
"""Raises when wrong value is specified."""
44+
45+
pass
46+
4247
class UnsupportedOperationError(MONAIAppSdkError):
4348
"""Raises when unsupported operation is requested."""
4449

0 commit comments

Comments
 (0)