Skip to content

Commit

Permalink
[Util] Introduce ContentHashClass.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaomy3832 committed May 18, 2017
1 parent baffd2e commit ea5ff3a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ List of major changes and improvements

- Use a single global argument parser.

- Introduce ContentHashClass.


### Fixed

Expand Down
15 changes: 2 additions & 13 deletions nn_dataflow/Layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
program. If not, see <https://opensource.org/licenses/BSD-3-Clause>.
"""

from .Util import StringifyClass
from .Util import StringifyClass, ContentHashClass

class Layer(StringifyClass):
class Layer(StringifyClass, ContentHashClass):
'''
Base NN layer.
Expand Down Expand Up @@ -122,17 +122,6 @@ def total_ops(self, batch_size=1):
''' Get total number of operations. '''
return self.total_ofmap_size() * self.ops_per_neuron() * batch_size

def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return NotImplemented

def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self):
return hash(tuple(sorted(self.__dict__.items())))


class InputLayer(Layer):
'''
Expand Down
15 changes: 2 additions & 13 deletions nn_dataflow/PartitionScheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
from . import Util
from .Layer import ConvLayer, LocalRegionLayer
from .PhyDim2 import PhyDim2
from .Util import StringifyClass
from .Util import StringifyClass, ContentHashClass

class PartitionScheme(StringifyClass):
class PartitionScheme(StringifyClass, ContentHashClass):
'''
A parallel processing partitioning scheme.
'''
Expand Down Expand Up @@ -123,14 +123,3 @@ def part_layer(self, layer, batch_size):

return p_layer, p_batch_size, p_occ

def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return NotImplemented

def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self):
return hash(tuple(sorted(self.__dict__.items())))

20 changes: 20 additions & 0 deletions nn_dataflow/Util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ def __str__(self):
in sorted(self.__dict__.items())]))


class ContentHashClass(object):
'''
Class using the content instead of the object ID for hash.
Such class instance can be used as key in dictionary.
'''
# pylint: disable=too-few-public-methods

def __eq__(self, other):
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return NotImplemented

def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self):
return hash(tuple(sorted(self.__dict__.items())))


def idivc(valx, valy):
'''
Integer division and ceiling.
Expand Down

0 comments on commit ea5ff3a

Please sign in to comment.