-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Data loader merlin graph transforms and embeddings #37
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
db89cab
lay down foundation of transform capability in dataloader.
jperez999 736c8e8
working transforms for greater that host memory and greater
jperez999 c1572fa
add in memory versions of indexing
jperez999 0eb8d81
added docstrings to operators and made change to tf embedding test
jperez999 6334d91
remove base loader logic for gpu only
jperez999 f978c57
Merge branch 'main' into dl-transforms
jperez999 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# | ||
# Copyright (c) 2021, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# | ||
# Copyright (c) 2021, NVIDIA CORPORATION. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
# flake8: noqa | ||
from merlin.loader.ops.embeddings.tf_embedding_op import ( | ||
Numpy_Mmap_TFEmbedding, | ||
Numpy_TFEmbeddingOperator, | ||
TFEmbeddingOperator, | ||
) | ||
from merlin.loader.ops.embeddings.torch_embedding_op import ( | ||
Numpy_Mmap_TorchEmbedding, | ||
Numpy_TorchEmbeddingOperator, | ||
TorchEmbeddingOperator, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
import numpy as np | ||
import tensorflow as tf | ||
|
||
from merlin.core.protocols import Transformable | ||
from merlin.dag import BaseOperator | ||
from merlin.dag.selector import ColumnSelector | ||
from merlin.schema import ColumnSchema, Schema, Tags | ||
|
||
|
||
class TFEmbeddingOperator(BaseOperator): | ||
"""Create an operator that will apply a tf embedding table to supplied indices. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of these are repeated with small tweaks, would be nice to be able to converge so we dont have three operators for the same thing just using different inputs. |
||
This operator allows the user to supply an id lookup table if the indices supplied | ||
via the id_lookup_table. Embedding table is stored in host memory. | ||
|
||
Parameters | ||
---------- | ||
embeddings : np.ndarray | ||
numpy ndarray representing embedding values | ||
lookup_key : str, optional | ||
the name of the column that will be used as indices, by default "id" | ||
embedding_name : str, optional | ||
name of new column of embeddings, added to output, by default "embeddings" | ||
id_lookup_table : np.array, optional | ||
numpy array of values that represent embedding indices, by default None | ||
""" | ||
|
||
def __init__( | ||
self, | ||
embeddings: np.ndarray, | ||
lookup_key: str = "id", | ||
embedding_name: str = "embeddings", | ||
id_lookup_table=None, | ||
): | ||
self.embeddings = ( | ||
embeddings if isinstance(embeddings, tf.Tensor) else tf.convert_to_tensor(embeddings) | ||
) | ||
self.lookup_key = lookup_key | ||
self.embedding_name = embedding_name | ||
self.id_lookup_table = id_lookup_table | ||
|
||
def transform( | ||
self, col_selector: ColumnSelector, transformable: Transformable | ||
) -> Transformable: | ||
indices = transformable[self.lookup_key] | ||
if self.id_lookup_table: | ||
indices = np.in1d(self.id_lookup_table, indices) | ||
embeddings = tf.nn.embedding_lookup(self.embeddings, indices) | ||
transformable[self.embedding_name] = embeddings | ||
return transformable | ||
|
||
def compute_output_schema( | ||
self, | ||
input_schema: Schema, | ||
col_selector: ColumnSelector, | ||
prev_output_schema: Schema = None, | ||
) -> Schema: | ||
"""Creates the output schema for this operator. | ||
|
||
Parameters | ||
---------- | ||
input_schema : Schema | ||
schema coming from ancestor nodes | ||
col_selector : ColumnSelector | ||
subselection of columns to apply to this operator | ||
prev_output_schema : Schema, optional | ||
the output schema of the previously executed operators, by default None | ||
|
||
Returns | ||
------- | ||
Schema | ||
Schema representing the correct output for this operator. | ||
""" | ||
col_schemas = [] | ||
for _, col_schema in input_schema.column_schemas.items(): | ||
col_schemas.append(col_schema) | ||
col_schemas.append( | ||
ColumnSchema( | ||
name=self.embedding_name, | ||
tags=[Tags.CONTINUOUS], | ||
dtype=self.embeddings.dtype.as_numpy_dtype, | ||
is_list=True, | ||
is_ragged=False, | ||
) | ||
) | ||
|
||
return Schema(col_schemas) | ||
|
||
|
||
class Numpy_TFEmbeddingOperator(BaseOperator): | ||
"""Create an embedding table from supplied embeddings to add embedding entry | ||
to records based on supplied indices. Support for indices lookup table is available. | ||
Embedding table is stored in host memory. | ||
|
||
Parameters | ||
---------- | ||
embeddings : np.ndarray | ||
numpy ndarray representing embedding values | ||
lookup_key : str, optional | ||
the name of the column that will be used as indices, by default "id" | ||
embedding_name : str, optional | ||
name of new column of embeddings, added to output, by default "embeddings" | ||
id_lookup_table : np.array, optional | ||
numpy array of values that represent embedding indices, by default None | ||
""" | ||
|
||
def __init__( | ||
self, | ||
embeddings: np.ndarray, | ||
lookup_key: str = "id", | ||
embedding_name: str = "embeddings", | ||
id_lookup_table=None, | ||
): | ||
self.embeddings = embeddings | ||
self.lookup_key = lookup_key | ||
self.embedding_name = embedding_name | ||
self.id_lookup_table = id_lookup_table | ||
|
||
def transform( | ||
self, col_selector: ColumnSelector, transformable: Transformable | ||
) -> Transformable: | ||
indices = transformable[self.lookup_key] | ||
if self.id_lookup_table: | ||
indices = np.in1d(self.id_lookup_table, indices) | ||
embeddings = self.embeddings[indices] | ||
transformable[self.embedding_name] = tf.convert_to_tensor(embeddings) | ||
return transformable | ||
|
||
def compute_output_schema( | ||
self, | ||
input_schema: Schema, | ||
col_selector: ColumnSelector, | ||
prev_output_schema: Schema = None, | ||
) -> Schema: | ||
"""Creates the output schema for this operator. | ||
|
||
Parameters | ||
---------- | ||
input_schema : Schema | ||
schema coming from ancestor nodes | ||
col_selector : ColumnSelector | ||
subselection of columns to apply to this operator | ||
prev_output_schema : Schema, optional | ||
the output schema of the previously executed operators, by default None | ||
|
||
Returns | ||
------- | ||
Schema | ||
Schema representing the correct output for this operator. | ||
""" | ||
col_schemas = [] | ||
for _, col_schema in input_schema.column_schemas.items(): | ||
col_schemas.append(col_schema) | ||
col_schemas.append( | ||
ColumnSchema( | ||
name=self.embedding_name, | ||
tags=[Tags.CONTINUOUS], | ||
dtype=self.embeddings.dtype, | ||
is_list=True, | ||
is_ragged=False, | ||
) | ||
) | ||
|
||
return Schema(col_schemas) | ||
|
||
|
||
class Numpy_Mmap_TFEmbedding(BaseOperator): | ||
"""Operator loads numpy embedding table from file using memory map to be used to create | ||
tensorflow embedding representations. This allows for larger than host memory embedding | ||
tables to be used for embedding lookups. The only limit to the size is what fits in | ||
storage, preferred storage device is SSD for faster lookups. | ||
|
||
Parameters | ||
---------- | ||
embedding_npz : numpy ndarray file | ||
file holding numpy ndarray representing embedding table | ||
ids_lookup_npz : numpy array file, optional | ||
file holding numpy array of values that represent embedding indices, by default None | ||
lookup_key : str, optional | ||
the name of the column that will be used as indices, by default "id" | ||
embedding_name : str, optional | ||
name of new column of embeddings, added to output, by default "embeddings" | ||
transform_function : _type_, optional | ||
function that will transform embedding from numpy to torch, by default None | ||
""" | ||
|
||
def __init__( | ||
self, | ||
embedding_npz, | ||
ids_lookup_npz=None, | ||
lookup_key="id", | ||
embedding_name="embeddings", | ||
transform_function=None, | ||
): | ||
self.embeddings = np.load(embedding_npz, mmap_mode="r") | ||
self.id_lookup = np.load(ids_lookup_npz) if ids_lookup_npz else None | ||
self.lookup_key = lookup_key | ||
self.embedding_name = embedding_name | ||
self.transform_function = tf.convert_to_tensor | ||
|
||
def transform( | ||
self, col_selector: ColumnSelector, transformable: Transformable | ||
) -> Transformable: | ||
ids_tensor = transformable[self.lookup_key] | ||
if self.id_lookup: | ||
ids_tensor = np.in1d(self.id_lookup[:, 0], ids_tensor) | ||
embeddings = self.embeddings[ids_tensor] | ||
if self.transform_function: | ||
transformable[self.embedding_name] = self.transform_function(embeddings) | ||
else: | ||
transformable[self.embedding_name] = embeddings | ||
return transformable | ||
|
||
def compute_output_schema( | ||
self, | ||
input_schema: Schema, | ||
col_selector: ColumnSelector, | ||
prev_output_schema: Schema = None, | ||
) -> Schema: | ||
"""Creates the output schema for this operator. | ||
|
||
Parameters | ||
---------- | ||
input_schema : Schema | ||
schema coming from ancestor nodes | ||
col_selector : ColumnSelector | ||
subselection of columns to apply to this operator | ||
prev_output_schema : Schema, optional | ||
the output schema of the previously executed operators, by default None | ||
|
||
Returns | ||
------- | ||
Schema | ||
Schema representing the correct output for this operator. | ||
""" | ||
col_schemas = [] | ||
for _, col_schema in input_schema.column_schemas.items(): | ||
col_schemas.append(col_schema) | ||
col_schemas.append( | ||
ColumnSchema( | ||
name=self.embedding_name, | ||
tags=[Tags.CONTINUOUS], | ||
dtype=self.embeddings.dtype, | ||
is_list=True, | ||
is_ragged=False, | ||
) | ||
) | ||
|
||
return Schema(col_schemas) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should think about creating a comprehensive "column" class that can be sub-classed to ScalarColumn and ListColumn. This will hide the tuple format behind a df series type interface that will be more friendly to the other parts of merlin, i.e. the graph. The use case is what if I want to do some after dataloader inbatch processing to a list column. It will be easier to abstract that tuple representation (values, nnz) and allow the user to not have to worry about keeping track of all that.