Skip to content

Commit

Permalink
Making Features as a singleton for improved caching (apache#15835)
Browse files Browse the repository at this point in the history
  • Loading branch information
access2rohit authored and Ubuntu committed Aug 20, 2019
1 parent f1259f7 commit a5bece9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions python/mxnet/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ class Features(collections.OrderedDict):
"""
OrderedDict of name to Feature
"""
instance = None
def __new__(cls):
if cls.instance is None:
cls.instance = super(Features, cls).__new__(cls)
return cls.instance

def __init__(self):
super(Features, self).__init__([(f.name, f) for f in feature_list()])

Expand Down
9 changes: 9 additions & 0 deletions tests/python/unittest/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@
from mxnet.base import MXNetError
from nose.tools import *


def test_features():
features = Features()
print(features)
ok_('CUDA' in features)
ok_(len(features) >= 30)


def test_is_singleton():
x = Features()
y = Features()
assert x is y


def test_is_enabled():
features = Features()
for f in features:
Expand All @@ -35,6 +43,7 @@ def test_is_enabled():
else:
ok_(not features.is_enabled(f))


@raises(RuntimeError)
def test_is_enabled_not_existing():
features = Features()
Expand Down

0 comments on commit a5bece9

Please sign in to comment.