Skip to content

Commit

Permalink
Make TensorColumn shape and dtype properties lazy but memoized (#257)
Browse files Browse the repository at this point in the history
This improves performance in cases where a lot of `TensorColumns` get created but the shapes and dtypes aren't used (like in the dataloaders.)
  • Loading branch information
karlhigley authored Mar 28, 2023
1 parent b7cf860 commit 3455ec3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
1 change: 1 addition & 0 deletions merlin/dtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from merlin.dtypes.aliases import *
from merlin.dtypes.base import DType
from merlin.dtypes.registry import _dtype_registry
from merlin.dtypes.shape import Dimension, Shape

# Convenience alias for registering dtypes
register = _dtype_registry.register
Expand Down
14 changes: 8 additions & 6 deletions merlin/table/tensor_column.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import merlin.dtypes as md
from merlin.dispatch.lazy import lazy_singledispatch
from merlin.dtypes.shape import Shape
from merlin.dtypes import DType, Shape


class Device(Enum):
Expand Down Expand Up @@ -67,12 +67,10 @@ def __init__(self, values: Any, offsets: Any = None, dtype=None, _ref=None, _dev

self._values = values
self._offsets = offsets

shape = self._construct_shape(values, offsets)
self._dtype = md.dtype(dtype or values.dtype).with_shape(shape)

self._dtype = dtype or values.dtype
self._ref = _ref
self._device = _device
self._shape = None

def cpu(self):
"""
Expand Down Expand Up @@ -104,7 +102,9 @@ def gpu(self):

@property
def shape(self) -> Shape:
return self._dtype.shape
if not self._shape:
self._shape = self._construct_shape(self.values, self.offsets)
return self._shape

@property
def is_list(self) -> Shape:
Expand All @@ -128,6 +128,8 @@ def offsets(self):

@property
def dtype(self):
if not isinstance(self._dtype, DType):
self._dtype = md.dtype(self._dtype).with_shape(self.shape)
return self._dtype

def __len__(self):
Expand Down

0 comments on commit 3455ec3

Please sign in to comment.