Skip to content

Commit

Permalink
added runningtime and hwinfo recorder (as universal params)
Browse files Browse the repository at this point in the history
  • Loading branch information
keighrim committed Jul 6, 2024
1 parent 02c43df commit 830a691
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions clams/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import warnings
from abc import ABC, abstractmethod
from contextlib import contextmanager
from datetime import datetime
from urllib import parse as urlparser

__all__ = ['ClamsApp']
Expand Down Expand Up @@ -44,6 +45,14 @@ class ClamsApp(ABC):
'name': 'pretty', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False,
'description': 'The JSON body of the HTTP response will be re-formatted with 2-space indentation',
},
{
'name': 'runningTime', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False,
'description': 'The running time of the app will be recorded in the view metadata',
},
{
'name': 'hwFetch', 'type': 'boolean', 'choices': None, 'default': False, 'multivalued': False,
'description': 'The hardware information (architecture, GPU and vRAM) will be recorded in the view metadata',
},
]

# this key is used to store users' raw input params in the parameter dict
Expand Down Expand Up @@ -136,6 +145,7 @@ def annotate(self, mmif: Union[str, dict, Mmif], **runtime_params: List[str]) ->
refined = self._refine_params(**runtime_params)
self.logger.debug(f"Refined parameters: {refined}")
pretty = refined.get('pretty', False)
t = datetime.now()
with warnings.catch_warnings(record=True) as ws:
annotated = self._annotate(mmif, **refined)
if ws:
Expand All @@ -144,6 +154,26 @@ def annotate(self, mmif: Union[str, dict, Mmif], **runtime_params: List[str]) ->
warnings_view = annotated.new_view()
self.sign_view(warnings_view, refined)
warnings_view.metadata.warnings = issued_warnings
td = datetime.now() - t
runningTime = refined.get('runningTime', False)
hwFetch = refined.get('hwFetch', False)
runtime_recs = {}
if runningTime:
runtime_recs['runningTime'] = str(td)
if hwFetch:
import platform, shutil, subprocess
runtime_recs['architecture'] = platform.machine()
# runtime_recs['processor'] = platform.processor() # this only works on Windows
runtime_recs['cuda'] = []
if shutil.which('nvidia-smi'):
for gpu in subprocess.run(['nvidia-smi', '--query-gpu=name,memory.total', '--format=csv,noheader'],
stdout=subprocess.PIPE).stdout.decode('utf-8').strip().split('\n'):
name, mem = gpu.split(', ')
runtime_recs['cuda'].append(f'{name} ({mem})')
if len(runtime_recs) > 0:
for annotated_view in annotated.views:
if annotated_view.metadata.app == self.metadata.identifier:
annotated_view.metadata.set_additional_property('runtime', runtime_recs)
return annotated.serialize(pretty=pretty, sanitize=True)

@abstractmethod
Expand Down

0 comments on commit 830a691

Please sign in to comment.