Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Making Features as a singleton for improved caching #15835

Merged
merged 1 commit into from
Aug 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!!

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