Skip to content

Commit

Permalink
CM/CMX V3.4.2: new reusable functions (#1353)
Browse files Browse the repository at this point in the history
Added reusable functions  from MLPerf automations:
  - added utils.flatten_dict
  - added utils.safe_int
  - added utils.safe_float
  - added utils.get_set
  - added utils.digits
  • Loading branch information
ctuning-admin authored Nov 22, 2024
2 parents 342cc44 + b2cc8da commit ec9b1b9
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 12 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ CK consists of several sub-projects:
* [CM4ABTF](https://github.com/mlcommons/cm4abtf) - a unified CM interface and automation recipes
to run automotive benchmark across different models, data sets, software and hardware from different vendors.

* [CMX (the next generation of CM)](cm/docs/cmx) - we are developing the next generation of CM
* [CMX (the next generation of CM and CM4MLOps)](cm/docs/cmx) - we are developing the next generation of CM
to make it simpler and more flexible based on user feedback. Please follow
this project [here]( https://github.com/orgs/mlcommons/projects/46 ).

Expand All @@ -59,8 +59,9 @@ CK consists of several sub-projects:

### Maintainers

* CM/CMX/CM4Research: [Grigori Fursin](https://cKnowledge.org/gfursin)
* CM/CM4Research: [Grigori Fursin](https://cKnowledge.org/gfursin)
* CM4MLOps: [Arjun Suresh](https://github.com/arjunsuresh) and [Anandhu Sooraj](https://github.com/anandhu-eng)
* CMX (the next generation of CM) [Grigori Fursin](https://cKnowledge.org/gfursin)

### Citing our project

Expand Down Expand Up @@ -89,11 +90,11 @@ To learn more about the motivation behind CK and CM technology, please explore t

### Acknowledgments

The open-source Collective Knowledge project (CK)
was founded by [Grigori Fursin](https://cKnowledge.org/gfursin),
sponsored by cTuning.org, HiPEAC and OctoML, and donated to MLCommons
to serve the wider community. This open-source initiative includes
CM, CM4MLOps/CM4MLPerf, CM4ABTF, and CMX automation technologies.
Its development is a collaborative community effort,
made possible by our dedicated [volunteers, collaborators,
and contributors](https://github.com/mlcommons/ck/blob/master/CONTRIBUTING.md)!
The open-source Collective Knowledge project (CK, CM, CM4MLOps/CM4MLPerf,
CM4Research and CMX) was created by [Grigori Fursin](https://cKnowledge.org/gfursin)
and sponsored by cTuning.org, OctoAI and HiPEAC.
Grigori donated CK to MLCommons to benefit the community
and to advance its development as a collaborative, community-driven effort.
We thank MLCommons and FlexAI for supporting this project,
as well as our dedicated [volunteers and collaborators](https://github.com/mlcommons/ck/blob/master/CONTRIBUTING.md)
for their feedback and contributions!
6 changes: 5 additions & 1 deletion cm/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## V3.4.1.1
## V3.4.2
- added utils.flatten_dict
- added utils.safe_int
- added utils.safe_float
- added utils.get_set
- added utils.digits

## V3.4.1
- reduced Python min version in pyproject.toml to 3.7 for backwards compatibility
Expand Down
2 changes: 1 addition & 1 deletion cm/cmind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Written by Grigori Fursin

__version__ = "3.4.1.1"
__version__ = "3.4.2"

from cmind.core import access
from cmind.core import x
Expand Down
105 changes: 105 additions & 0 deletions cm/cmind/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2047,3 +2047,108 @@ def flatten_dict(d, fd = {}, prefix = ''):
fd[prefix + k] = str(v)

return

##############################################################################
def safe_int(i, d):
"""
Get safe int (useful for sorting function)
Args:
i (any): variable with any type
d (int): default value
Returns:
(int): returns i if it can be converted to int, or d otherwise
"""

r = d
try:
r = int(i)
except Exception as e:
pass

return r

##############################################################################
def safe_float(i, d):
"""
Get safe float (useful for sorting function)
Args:
i (any): variable with any type
d (float): default value
Returns:
(float): returns i if it can be converted to float or d otherwise
"""

r = d
try:
r = float(i)
except Exception as e:
pass

return r

##############################################################################
def get_set(meta, key, state, keys):
"""
Get value from dict and update in another dict
Args:
meta (dict): original dict
key (str): key to get value from original dict
state (dict): target dict
keys (list): list of keys to set value in target dict
Returns:
(Python object): value from original dict or None
"""

v = meta.get(key, None)
if v != None:
cur = state
vv = None
for k in keys:
if k == keys[-1]:
vv = cur.get(k, None)
if vv == None:
cur[k] = v
else:
if k not in cur:
cur[k] = {}
cur = cur[k]

return v

##############################################################################
def digits(s, first = True):
"""
Get first digits and convert to int
Args:
s (str): string ("1.3+xyz")
first (bool): if True, choose only first digits, otherwise all
Returns:
(int): returns int from first digits or 0
"""

v = 0

digits = ''
for char in s:
if char.isdigit():
digits+=char
elif first:
break

try:
v = int(digits)
except Exception as e:
pass

return v

0 comments on commit ec9b1b9

Please sign in to comment.