-
Notifications
You must be signed in to change notification settings - Fork 682
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
one_embedding add doc string #7902
Changes from 5 commits
712025b
888472d
43fc23d
5ce3511
ea465c8
9b4a0a5
9c5b98d
5a1c96e
9af820b
f3be202
5fec2a5
e516292
7bd50ec
a337604
9234745
e69940c
c98545d
4cb08b6
1aa81e6
ba55ae2
8a93681
314a06e
3ef7de3
b240ea2
2ea5dec
a3f95c9
b141e64
7924a87
5b8cbb9
eeb8f6e
14ece2d
94a94b0
93d49a1
6187faf
fc5fff3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ OneFlow API Reference | |
utils | ||
env | ||
comm | ||
one_embedding | ||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
oneflow.one_embedding | ||
=================================== | ||
OneFlow one_embedding operations. | ||
---------------------------------- | ||
.. currentmodule:: oneflow.one_embedding | ||
.. automodule:: oneflow.one_embedding | ||
:members: MultiTableEmbedding, | ||
|
||
.. autofunction:: oneflow.one_embedding.make_device_mem_store_options | ||
.. autofunction:: oneflow.one_embedding.make_cached_ssd_store_options | ||
.. autofunction:: oneflow.one_embedding.make_cached_host_mem_store_options | ||
.. autofunction:: oneflow.one_embedding.make_uniform_initializer | ||
.. autofunction:: oneflow.one_embedding.make_normal_initializer | ||
.. autofunction:: oneflow.one_embedding.make_table |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,80 @@ def _check_cache(cache): | |
|
||
|
||
class MultiTableEmbedding(Module): | ||
r"""MultiTableEmbedding represent multi Embedding tables with same embedding_dim, dtype, and key_type. | ||
|
||
Args: | ||
name (str): The name of Embedding | ||
embedding_dim (int): the size of each embedding vector | ||
dtype (flow.dtype): the data type of embeddings | ||
key_type (flow.dtype): the data type of feature ids | ||
tables (list): list of table param which can be made by flow.one_embedding.make_table | ||
store_options (dict): store option of Embedding | ||
default_initializer (dict, optional): if tables param is None, use default_initializer to initialize table. Defaults to None. | ||
|
||
For example: | ||
|
||
.. code-block:: python | ||
|
||
>>> import oneflow as flow | ||
>>> import numpy as np | ||
>>> import oneflow.nn as nn | ||
>>> table_size_array = [39884407,39043,17289,7420,20263,3,7120,1543,63,38532952,2953546,403346,10,2208,11938,155,4,976,14,39979772,25641295,39664985,585935,12972,108,36] | ||
>>> vocab_size = sum(table_size_array) | ||
>>> num_tables = len(table_size_array) | ||
>>> embedding_size = 128 | ||
>>> scales = np.sqrt(1 / np.array(table_size_array)) | ||
>>> tables = [ | ||
>>> flow.one_embedding.make_table( | ||
>>> flow.one_embedding.make_uniform_initializer(low=-scale, high=scale) | ||
>>> ) | ||
>>> for scale in scales | ||
>>> ] | ||
>>> store_options = flow.one_embedding.make_cached_ssd_store_options( | ||
>>> cache_budget_mb=8192, persistent_path="/your_path_to_ssd", capacity=vocab_size, | ||
>>> ) | ||
>>> embedding = flow.one_embedding.MultiTableEmbedding( | ||
>>> name="my_embedding", | ||
>>> embedding_dim=embedding_size, | ||
>>> dtype=flow.float, | ||
>>> key_type=flow.int64, | ||
>>> tables=tables, | ||
>>> store_options=store_options, | ||
>>> ) | ||
>>> embedding.to("cuda") | ||
>>> mlp = flow.nn.FusedMLP( | ||
>>> in_features=embedding_size * num_tables, | ||
>>> hidden_features=[512, 256, 128], | ||
>>> out_features=1, | ||
>>> skip_final_activation=True, | ||
>>> ) | ||
>>> mlp.to("cuda") | ||
>>> | ||
>>> class TrainGraph(flow.nn.Graph): | ||
>>> def __init__(self,): | ||
>>> super().__init__() | ||
>>> self.embedding_lookup = embedding | ||
>>> self.mlp = mlp | ||
>>> self.add_optimizer( | ||
>>> flow.optim.SGD(self.embedding_lookup.parameters(), lr=0.1, momentum=0.0) | ||
>>> ) | ||
>>> self.add_optimizer( | ||
>>> flow.optim.SGD(self.mlp.parameters(), lr=0.1, momentum=0.0) | ||
>>> ) | ||
>>> def build(self, ids): | ||
>>> embedding = self.embedding_lookup(ids) | ||
>>> loss = self.mlp(flow.reshape(embedding, (-1, num_tables * embedding_size))) | ||
>>> loss = loss.sum() | ||
>>> loss.backward() | ||
>>> return loss | ||
>>> ids = np.random.randint(0, 1000, (100, num_tables), dtype=np.int64) | ||
>>> ids_tensor = flow.tensor(ids, requires_grad=False).to("cuda") | ||
>>> graph = TrainGraph() | ||
>>> loss = graph(ids_tensor) | ||
>>> print(loss) | ||
|
||
""" | ||
|
||
def __init__( | ||
self, | ||
name, | ||
|
@@ -194,9 +268,19 @@ def _load_from_state_dict( | |
) | ||
|
||
def save_snapshot(self, snapshot_name): | ||
"""save snapshot | ||
|
||
Args: | ||
snapshot_name (str): save snapshot to | ||
""" | ||
self.handler.SaveSnapshot(snapshot_name) | ||
|
||
def load_snapshot(self, snapshot_name): | ||
"""load snapshot | ||
|
||
Args: | ||
snapshot_name (str): load snapshot from | ||
""" | ||
self.handler.LoadSnapshot(snapshot_name) | ||
|
||
def forward(self, ids, table_ids=None): | ||
|
@@ -216,6 +300,17 @@ def forward(self, ids, table_ids=None): | |
def make_device_mem_store_options( | ||
persistent_path, capacity, size_factor=1, physical_block_size=512 | ||
): | ||
"""make GPU only store_options param of MultiTableEmbedding | ||
|
||
Args: | ||
persistent_path (str, list): persistent storage path of Embedding | ||
capacity (int): total capacity of Embedding | ||
size_factor (int, optional): store size factor of embedding_dim, if SGD update, and momentum = 0, should be 1, if momentum > 0, it should be 2. if Adam, should be 3. Defaults to 1. | ||
physical_block_size (int, optional): physical_block_size should be sector size. Defaults to 512. | ||
|
||
Returns: | ||
dict: GPU only store_options param of MultiTableEmbedding | ||
""" | ||
assert isinstance(persistent_path, (str, list, tuple)) | ||
assert capacity > 0 | ||
options = { | ||
|
@@ -245,6 +340,18 @@ def make_cached_ssd_store_options( | |
size_factor=1, | ||
physical_block_size=512, | ||
): | ||
"""make SSD use GPU as cache store_options param of MultiTableEmbedding | ||
|
||
Args: | ||
cache_budget_mb (int): the mb budget of per GPU as cache | ||
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. 这个参数的是设置 cache 大小,单位是 MB 吗? 如果是的话,是不是用 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. cache budget好像是个术语 |
||
persistent_path (str, list): persistent storage path of Embedding, must use fast SSD because of frequently random disk access during training | ||
capacity (int): total capacity of Embedding | ||
size_factor (int, optional): store size factor of embedding_dim, if SGD update, and momentum = 0, should be 1, if momentum > 0, it should be 2. if Adam, should be 3. Defaults to 1. | ||
physical_block_size (int, optional): physical_block_size should be sector size. Defaults to 512. | ||
|
||
Returns: | ||
dict: SSD use GPU as cache store_options param of MultiTableEmbedding | ||
""" | ||
assert isinstance(persistent_path, (str, list, tuple)) | ||
assert cache_budget_mb > 0 | ||
if capacity is not None: | ||
|
@@ -274,6 +381,18 @@ def make_cached_ssd_store_options( | |
def make_cached_host_mem_store_options( | ||
cache_budget_mb, persistent_path, capacity, size_factor=1, physical_block_size=512, | ||
): | ||
"""make host use GPU as cache store_options param of MultiTableEmbedding | ||
|
||
Args: | ||
cache_budget_mb (int): the mb budget of per GPU as cache | ||
persistent_path (str, list): persistent storage path of Embedding | ||
capacity (int): total capacity of Embedding | ||
size_factor (int, optional): store size factor of embedding_dim, if SGD update, and momentum = 0, should be 1, if momentum > 0, it should be 2. if Adam, should be 3. Defaults to 1. | ||
physical_block_size (int, optional): physical_block_size should be sector size. Defaults to 512. | ||
|
||
Returns: | ||
dict: host use GPU as cache store_options param of MultiTableEmbedding | ||
""" | ||
assert isinstance(persistent_path, (str, list, tuple)) | ||
assert cache_budget_mb > 0 | ||
assert capacity > 0 | ||
|
@@ -303,12 +422,38 @@ def make_cached_host_mem_store_options( | |
|
||
|
||
def make_uniform_initializer(low, high): | ||
"""make uniform initializer param of make_table | ||
|
||
Args: | ||
low (float): A python scalar. Lower bound of the range of random values to generate. | ||
high (float): A python scalar. Upper bound of the range of random values to generate. | ||
|
||
Returns: | ||
dict: initializer param of make_table | ||
""" | ||
return {"type": "uniform", "low": low, "high": high} | ||
|
||
|
||
def make_normal_initializer(mean, std): | ||
"""make normal initializer param of make_table | ||
|
||
Args: | ||
mean (float): A python scalar. Mean of the random values to generate. | ||
std (float): A python scalar. Standard deviation of the random values to generate. | ||
|
||
Returns: | ||
dict: initializer param of make_table | ||
""" | ||
return {"type": "normal", "mean": mean, "std": std} | ||
|
||
|
||
def make_table(initializer): | ||
"""make table param of MultiTableEmbedding tables | ||
|
||
Args: | ||
initializer (dict): initializer param, make by make_uniform_initializer or make_normal_initializer | ||
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. 我觉得这些方法,也得有 example。 然后上文的那些初始化方法,即使没有必要每个都有 example,那起码也可以给个交叉引用,写 不过这些都只是初步粗浅的看法。具体以 @Chenqll 体验后给出建议。 |
||
|
||
Returns: | ||
dict: table param of MultiTableEmbedding tables | ||
""" | ||
return {"initializer": initializer} |
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.
这个方法之前没注意。我觉得有必要补以下 docstring