From 94ad8eb47eb6a8a37f665fe2552b9c0c44d86511 Mon Sep 17 00:00:00 2001 From: Rohit Kumar Srivastava Date: Sat, 10 Aug 2019 01:49:49 -0700 Subject: [PATCH] Making Features as a singleton for improved caching (#15835) --- python/mxnet/runtime.py | 6 ++++++ tests/python/unittest/test_runtime.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/python/mxnet/runtime.py b/python/mxnet/runtime.py index e47cca93dace..edfa24525ab9 100644 --- a/python/mxnet/runtime.py +++ b/python/mxnet/runtime.py @@ -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()]) diff --git a/tests/python/unittest/test_runtime.py b/tests/python/unittest/test_runtime.py index 5b06d0c3c36c..82e2314532b8 100644 --- a/tests/python/unittest/test_runtime.py +++ b/tests/python/unittest/test_runtime.py @@ -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: @@ -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()