|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +#!/usr/bin/env python3 |
| 9 | + |
| 10 | +from typing import Dict, List, Optional, Tuple |
| 11 | + |
| 12 | +import torch |
| 13 | +from torchrec.sparse.jagged_tensor import ( |
| 14 | + _all_keys_used_once, |
| 15 | + _desugar_keyed_tensors, |
| 16 | + _remap_to_groups, |
| 17 | + KeyedTensor, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +@torch.fx.wrap |
| 22 | +def _concat_values(kts: List[KeyedTensor], dim: int) -> torch.Tensor: |
| 23 | + return torch.cat([kt.values() for kt in kts], dim=dim) |
| 24 | + |
| 25 | + |
| 26 | +@torch.fx.wrap |
| 27 | +def _permuted_values( |
| 28 | + kts: List[KeyedTensor], remap: List[Tuple[int, str]], dim: int |
| 29 | +) -> torch.Tensor: |
| 30 | + embedding_dicts = [kt.to_dict() for kt in kts] |
| 31 | + values = [embedding_dicts[idx][key] for (idx, key) in remap] |
| 32 | + return torch.cat(values, dim=dim) |
| 33 | + |
| 34 | + |
| 35 | +@torch.fx.wrap |
| 36 | +def _build_dict( |
| 37 | + keys: List[str], values: torch.Tensor, splits: List[int], dim: int |
| 38 | +) -> Dict[str, torch.Tensor]: |
| 39 | + return { |
| 40 | + key: tensor for key, tensor in zip(keys, torch.split(values, splits, dim=dim)) |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +class KTRegroupAsDict(torch.nn.Module): |
| 45 | + """ |
| 46 | + KTRegroupAsDict is a nn.Module that mirrors beahvior of static method KeyedTensor.regroup_as_dict() |
| 47 | +
|
| 48 | + The advantage of using this module it caches the regrouping logic after first batch. |
| 49 | +
|
| 50 | + Args: |
| 51 | + groups (List[List[str]]): features per output group |
| 52 | + keys (List[str]): key of each output group |
| 53 | +
|
| 54 | + Example:: |
| 55 | +
|
| 56 | + keys = ['object', 'user'] |
| 57 | + groups = [['f1', 'f2'], ['f3']] |
| 58 | + regroup_module = KTRegroupAsDict(groups, keys) |
| 59 | +
|
| 60 | +
|
| 61 | + tensor_list = [torch.randn(2, 4), torch.randn(2, 8), torch.randn(2, 2)] |
| 62 | + kts = [KeyedTensor.from_tensor_list(['f1', 'f2', 'f3' ], tensor_list)] |
| 63 | + out = regroup_module(kts) |
| 64 | +
|
| 65 | + """ |
| 66 | + |
| 67 | + def __init__(self, groups: List[List[str]], keys: List[str]) -> None: |
| 68 | + super().__init__() |
| 69 | + torch._C._log_api_usage_once(f"torchrec.modules.{self.__class__.__name__}") |
| 70 | + assert len(groups) == len(keys), "Groups and keys should have same length" |
| 71 | + self._groups = groups |
| 72 | + self._keys = keys |
| 73 | + self._is_inited = False |
| 74 | + |
| 75 | + # cached values populated on first forward call |
| 76 | + self.device: Optional[torch.device] = None |
| 77 | + self._concat_dim: int = 1 |
| 78 | + self._use_fbgemm_regroup: bool = False |
| 79 | + self._splits: List[int] = [] |
| 80 | + self._idx_key_pairs: List[Tuple[int, str]] = [] |
| 81 | + self._permute_tensor: Optional[torch.Tensor] = None |
| 82 | + self._inv_permute_tensor: Optional[torch.Tensor] = None |
| 83 | + self._offsets_tensor: Optional[torch.Tensor] = None |
| 84 | + self._inv_offsets_tensor: Optional[torch.Tensor] = None |
| 85 | + |
| 86 | + def _init_fbgemm_regroup(self, kts: List[KeyedTensor]) -> None: |
| 87 | + self._use_fbgemm_regroup = True |
| 88 | + keys, lengths, values = _desugar_keyed_tensors(kts) |
| 89 | + permute, inv_permute, offsets, inv_offsets, splits = _remap_to_groups( |
| 90 | + keys, lengths, self._groups |
| 91 | + ) |
| 92 | + # no need to pin_memory() or to(..., non_blocking=True) since occurs only once |
| 93 | + self._permute_tensor = permute.to(self.device) |
| 94 | + self._inv_permute_tensor = inv_permute.to(self.device) |
| 95 | + self._offsets_tensor = offsets.to(self.device) |
| 96 | + self._inv_offsets_tensor = inv_offsets.to(self.device) |
| 97 | + self._splits = splits |
| 98 | + |
| 99 | + def _init_regroup(self, kts: List[KeyedTensor]) -> None: |
| 100 | + lengths = [kt.length_per_key() for kt in kts] |
| 101 | + indices = [kt._key_indices() for kt in kts] |
| 102 | + |
| 103 | + key_to_idx: dict[str, int] = {} |
| 104 | + for i, kt in enumerate(kts): |
| 105 | + for key in kt.keys(): |
| 106 | + if key in key_to_idx: |
| 107 | + raise RuntimeError( |
| 108 | + f"Duplicate key {key} found in KeyedTensors, undefined behavior" |
| 109 | + ) |
| 110 | + key_to_idx[key] = i |
| 111 | + |
| 112 | + splits: List[int] = [] |
| 113 | + idx_key_pairs: List[Tuple[int, str]] = [] |
| 114 | + for group in self._groups: |
| 115 | + group_length = 0 |
| 116 | + for name in group: |
| 117 | + idx_key_pairs.append((key_to_idx[name], name)) |
| 118 | + group_length += lengths[key_to_idx[name]][ |
| 119 | + indices[key_to_idx[name]][name] |
| 120 | + ] |
| 121 | + splits.append(group_length) |
| 122 | + |
| 123 | + self._splits = splits |
| 124 | + self._idx_key_pairs = idx_key_pairs |
| 125 | + |
| 126 | + def forward(self, keyed_tensors: List[KeyedTensor]) -> Dict[str, torch.Tensor]: |
| 127 | + if not self._is_inited: |
| 128 | + assert len(keyed_tensors) > 0, "Empty list provided" |
| 129 | + assert all( |
| 130 | + kt.device() == keyed_tensors[0].device() for kt in keyed_tensors |
| 131 | + ), "All inputs should be on the same device." |
| 132 | + self.device = keyed_tensors[0].device() |
| 133 | + assert all( |
| 134 | + kt.key_dim() == keyed_tensors[0].key_dim() for kt in keyed_tensors |
| 135 | + ), "All inputs should have the same key_dim" |
| 136 | + self._dim = keyed_tensors[0].key_dim() |
| 137 | + |
| 138 | + if _all_keys_used_once(keyed_tensors, self._groups) and self._dim == 1: |
| 139 | + self._init_fbgemm_regroup(keyed_tensors) |
| 140 | + else: |
| 141 | + self._init_regroup(keyed_tensors) |
| 142 | + self._is_inited = True |
| 143 | + |
| 144 | + if self._use_fbgemm_regroup: |
| 145 | + values = _concat_values(keyed_tensors, self._dim) |
| 146 | + permuted_values = torch.ops.fbgemm.permute_pooled_embs_auto_grad( |
| 147 | + values, |
| 148 | + self._offsets_tensor, |
| 149 | + self._permute_tensor, |
| 150 | + self._inv_offsets_tensor, |
| 151 | + self._inv_permute_tensor, |
| 152 | + ) |
| 153 | + else: |
| 154 | + permuted_values = _permuted_values( |
| 155 | + keyed_tensors, self._idx_key_pairs, self._dim |
| 156 | + ) |
| 157 | + |
| 158 | + return _build_dict(self._keys, permuted_values, self._splits, self._dim) |
0 commit comments