Skip to content

Commit ec9b1b9

Browse files
CM/CMX V3.4.2: new reusable functions (#1353)
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
2 parents 342cc44 + b2cc8da commit ec9b1b9

File tree

4 files changed

+122
-12
lines changed

4 files changed

+122
-12
lines changed

Diff for: README.md

+11-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ CK consists of several sub-projects:
3434
* [CM4ABTF](https://github.com/mlcommons/cm4abtf) - a unified CM interface and automation recipes
3535
to run automotive benchmark across different models, data sets, software and hardware from different vendors.
3636

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

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

6060
### Maintainers
6161

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

6566
### Citing our project
6667

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

9091
### Acknowledgments
9192

92-
The open-source Collective Knowledge project (CK)
93-
was founded by [Grigori Fursin](https://cKnowledge.org/gfursin),
94-
sponsored by cTuning.org, HiPEAC and OctoML, and donated to MLCommons
95-
to serve the wider community. This open-source initiative includes
96-
CM, CM4MLOps/CM4MLPerf, CM4ABTF, and CMX automation technologies.
97-
Its development is a collaborative community effort,
98-
made possible by our dedicated [volunteers, collaborators,
99-
and contributors](https://github.com/mlcommons/ck/blob/master/CONTRIBUTING.md)!
93+
The open-source Collective Knowledge project (CK, CM, CM4MLOps/CM4MLPerf,
94+
CM4Research and CMX) was created by [Grigori Fursin](https://cKnowledge.org/gfursin)
95+
and sponsored by cTuning.org, OctoAI and HiPEAC.
96+
Grigori donated CK to MLCommons to benefit the community
97+
and to advance its development as a collaborative, community-driven effort.
98+
We thank MLCommons and FlexAI for supporting this project,
99+
as well as our dedicated [volunteers and collaborators](https://github.com/mlcommons/ck/blob/master/CONTRIBUTING.md)
100+
for their feedback and contributions!

Diff for: cm/CHANGES.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
## V3.4.1.1
1+
## V3.4.2
22
- added utils.flatten_dict
3+
- added utils.safe_int
4+
- added utils.safe_float
5+
- added utils.get_set
6+
- added utils.digits
37

48
## V3.4.1
59
- reduced Python min version in pyproject.toml to 3.7 for backwards compatibility

Diff for: cm/cmind/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# Written by Grigori Fursin
44

5-
__version__ = "3.4.1.1"
5+
__version__ = "3.4.2"
66

77
from cmind.core import access
88
from cmind.core import x

Diff for: cm/cmind/utils.py

+105
Original file line numberDiff line numberDiff line change
@@ -2047,3 +2047,108 @@ def flatten_dict(d, fd = {}, prefix = ''):
20472047
fd[prefix + k] = str(v)
20482048

20492049
return
2050+
2051+
##############################################################################
2052+
def safe_int(i, d):
2053+
"""
2054+
Get safe int (useful for sorting function)
2055+
2056+
Args:
2057+
i (any): variable with any type
2058+
d (int): default value
2059+
2060+
Returns:
2061+
(int): returns i if it can be converted to int, or d otherwise
2062+
"""
2063+
2064+
r = d
2065+
try:
2066+
r = int(i)
2067+
except Exception as e:
2068+
pass
2069+
2070+
return r
2071+
2072+
##############################################################################
2073+
def safe_float(i, d):
2074+
"""
2075+
Get safe float (useful for sorting function)
2076+
2077+
Args:
2078+
i (any): variable with any type
2079+
d (float): default value
2080+
2081+
Returns:
2082+
(float): returns i if it can be converted to float or d otherwise
2083+
2084+
"""
2085+
2086+
r = d
2087+
try:
2088+
r = float(i)
2089+
except Exception as e:
2090+
pass
2091+
2092+
return r
2093+
2094+
##############################################################################
2095+
def get_set(meta, key, state, keys):
2096+
"""
2097+
Get value from dict and update in another dict
2098+
2099+
Args:
2100+
meta (dict): original dict
2101+
key (str): key to get value from original dict
2102+
state (dict): target dict
2103+
keys (list): list of keys to set value in target dict
2104+
2105+
Returns:
2106+
(Python object): value from original dict or None
2107+
2108+
"""
2109+
2110+
v = meta.get(key, None)
2111+
if v != None:
2112+
cur = state
2113+
vv = None
2114+
for k in keys:
2115+
if k == keys[-1]:
2116+
vv = cur.get(k, None)
2117+
if vv == None:
2118+
cur[k] = v
2119+
else:
2120+
if k not in cur:
2121+
cur[k] = {}
2122+
cur = cur[k]
2123+
2124+
return v
2125+
2126+
##############################################################################
2127+
def digits(s, first = True):
2128+
"""
2129+
Get first digits and convert to int
2130+
2131+
Args:
2132+
s (str): string ("1.3+xyz")
2133+
first (bool): if True, choose only first digits, otherwise all
2134+
2135+
Returns:
2136+
(int): returns int from first digits or 0
2137+
2138+
"""
2139+
2140+
v = 0
2141+
2142+
digits = ''
2143+
for char in s:
2144+
if char.isdigit():
2145+
digits+=char
2146+
elif first:
2147+
break
2148+
2149+
try:
2150+
v = int(digits)
2151+
except Exception as e:
2152+
pass
2153+
2154+
return v

0 commit comments

Comments
 (0)