rec_12.md 8.3 KB
Newer Older
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
1 2
# torchrec.quant

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
3
> [`pytorch.org/torchrec/torchrec.quant.html`](https://pytorch.org/torchrec/torchrec.quant.html)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
4

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
5
Torchrec 量化
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
6

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
7
Torchrec 为推断提供了 EmbeddingBagCollection 的量化版本。它依赖于 fbgemm 量化操作。这减少了模型权重的大小并加快了模型执行速度。
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

示例

```py
>>> import torch.quantization as quant
>>> import torchrec.quant as trec_quant
>>> import torchrec as trec
>>> qconfig = quant.QConfig(
>>>     activation=quant.PlaceholderObserver,
>>>     weight=quant.PlaceholderObserver.with_args(dtype=torch.qint8),
>>> )
>>> quantized = quant.quantize_dynamic(
>>>     module,
>>>     qconfig_spec={
>>>         trec.EmbeddingBagCollection: qconfig,
>>>     },
>>>     mapping={
>>>         trec.EmbeddingBagCollection: trec_quant.EmbeddingBagCollection,
>>>     },
>>>     inplace=inplace,
>>> ) 
```

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
31
## torchrec.quant.embedding_modules
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
32 33

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
34
class torchrec.quant.embedding_modules.EmbeddingBagCollection(tables: List[EmbeddingBagConfig], is_weighted: bool, device: device, output_dtype: dtype = torch.float32, table_name_to_quantized_weights: Optional[Dict[str, Tuple[Tensor, Tensor]]] = None, register_tbes: bool = False, quant_state_dict_split_scale_bias: bool = False, row_alignment: int = 16)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
35 36
```

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
37
基础:`EmbeddingBagCollectionInterface`, `ModuleNoCopyMixin`
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
38

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
39
EmbeddingBagCollection 表示池化嵌入(EmbeddingBags)的集合。这个 EmbeddingBagCollection 被量化为较低的精度。它依赖于 fbgemm 量化操作并提供表批处理。
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
40

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
41
它处理形式为 [F X B X L] 的 KeyedJaggedTensor 的稀疏数据 F: 特征(键) B: 批量大小 L: 稀疏特征的长度(不规则)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
42

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
43
并输出形式为 [B * (F * D)] 的 KeyedTensor,其中 F: 特征(键) D: 每个特征(键)的嵌入维度 B: 批量大小
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
44 45 46 47 48

参数:

+   **table_name_to_quantized_weights***字典**[**str**,* *元组**[**张量**,* *张量**]**]*)- 表到量化权重的映射

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
49
+   **embedding_configs***列表***[*EmbeddingBagConfig**]*)- 嵌入表的列表
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
50

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
51
+   **is_weighted** - (布尔值):输入的 KeyedJaggedTensor 是否加权
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

+   **设备** - (可选[torch.device]):默认计算设备

调用参数:

特征:KeyedJaggedTensor,

返回:

KeyedTensor

示例:

```py
table_0 = EmbeddingBagConfig(
    name="t1", embedding_dim=3, num_embeddings=10, feature_names=["f1"]
)
table_1 = EmbeddingBagConfig(
    name="t2", embedding_dim=4, num_embeddings=10, feature_names=["f2"]
)
ebc = EmbeddingBagCollection(tables=[eb1_config, eb2_config])

#        0       1        2  <-- batch
# "f1"   [0,1] None    [2]
# "f2"   [3]    [4]    [5,6,7]
#  ^
# feature
features = KeyedJaggedTensor(
    keys=["f1", "f2"],
    values=torch.tensor([0, 1, 2, 3, 4, 5, 6, 7]),
    offsets=torch.tensor([0, 2, 2, 3, 4, 5, 8]),
)

ebc.qconfig = torch.quantization.QConfig(
    activation=torch.quantization.PlaceholderObserver.with_args(
        dtype=torch.qint8
    ),
    weight=torch.quantization.PlaceholderObserver.with_args(dtype=torch.qint8),
)

qebc = QuantEmbeddingBagCollection.from_float(ebc)
quantized_embeddings = qebc(features) 
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
97
property device: device
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
98 99 100
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
101
embedding_bag_configs()  List[EmbeddingBagConfig]
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
102 103 104
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
105
forward(features: KeyedJaggedTensor)  KeyedTensor
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
106 107 108 109
```

参数:

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
110
**特征***KeyedJaggedTensor* → EmbeddingBagCollection
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
111 112 113
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
114
is_weighted() → bool
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
115 116 117
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
118
output_dtype() → dtype
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
119 120 121
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
122
training: bool
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
123 124 125
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
126
class torchrec.quant.embedding_modules.EmbeddingCollection(tables: List[EmbeddingConfig], device: device, need_indices: bool = False, output_dtype: dtype = torch.float32, table_name_to_quantized_weights: Optional[Dict[str, Tuple[Tensor, Tensor]]] = None, register_tbes: bool = False, quant_state_dict_split_scale_bias: bool = False, row_alignment: int = 16)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
127 128
```

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
129
基础:`EmbeddingCollectionInterface`, `ModuleNoCopyMixin`
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
130

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
131
EmbeddingCollection 表示非池化嵌入的集合。
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
132

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
133
它处理形式为 [F X B X L] 的 KeyedJaggedTensor 的稀疏数据,其中:
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
134 135 136 137 138 139 140

+   F: 特征(键)

+   B: 批量大小

+   L: 稀疏特征的长度(可变)

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
141
并输出 Dict[特征(键),JaggedTensor]。每个 JaggedTensor 包含形式为 (B * L) X D 的值,其中:
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
142 143 144 145 146 147 148 149 150

+   B: 批量大小

+   L: 稀疏特征的长度(不规则)

+   D: 每个特征(键)的嵌入维度和长度的形式为 L

参数:

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
151
+   **tables**(*列表***[*EmbeddingConfig**]*)- 嵌入表的列表。
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

+   **设备**(*可选**[**torch.device**]*)- 默认计算设备。

+   **need_indices**(*布尔值*)- 如果我们需要将索引传递给最终查找结果字典

示例:

```py
e1_config = EmbeddingConfig(
    name="t1", embedding_dim=3, num_embeddings=10, feature_names=["f1"]
)
e2_config = EmbeddingConfig(
    name="t2", embedding_dim=3, num_embeddings=10, feature_names=["f2"]
)

ec = EmbeddingCollection(tables=[e1_config, e2_config])

#     0       1        2  <-- batch
# 0   [0,1] None    [2]
# 1   [3]    [4]    [5,6,7]
# ^
# feature

features = KeyedJaggedTensor.from_offsets_sync(
    keys=["f1", "f2"],
    values=torch.tensor([0, 1, 2, 3, 4, 5, 6, 7]),
    offsets=torch.tensor([0, 2, 2, 3, 4, 5, 8]),
)
feature_embeddings = ec(features)
print(feature_embeddings['f2'].values())
tensor([[-0.2050,  0.5478,  0.6054],
[ 0.7352,  0.3210, -3.0399],
[ 0.1279, -0.1756, -0.4130],
[ 0.7519, -0.4341, -0.0499],
[ 0.9329, -1.0697, -0.8095]], grad_fn=<EmbeddingBackward>) 
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
190
property device: device
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
191 192 193
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
194
embedding_configs() → List[EmbeddingConfig]
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
195 196 197
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
198
embedding_dim() → int
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
199 200 201
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
202
embedding_names_by_table() → List[List[str]]
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
203 204 205
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
206
forward(features: KeyedJaggedTensor) → Dict[str, JaggedTensor]
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
207 208 209 210
```

参数:

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
211
**特征**(*KeyedJaggedTensor* → EmbeddingCollection
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
212 213 214
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
215
need_indices()  bool
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
216 217 218
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
219
output_dtype()  dtype
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
220 221 222
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
223
training: bool
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
224 225 226
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
227
class torchrec.quant.embedding_modules.FeatureProcessedEmbeddingBagCollection(tables: List[EmbeddingBagConfig], is_weighted: bool, device: device, output_dtype: dtype = torch.float32, table_name_to_quantized_weights: Optional[Dict[str, Tuple[Tensor, Tensor]]] = None, register_tbes: bool = False, quant_state_dict_split_scale_bias: bool = False, row_alignment: int = 16, feature_processor: Optional[FeatureProcessorsCollection] = None)
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
228 229
```

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
230
基础:`EmbeddingBagCollection`
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
231 232

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
233
embedding_bags: nn.ModuleDict
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
234 235 236
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
237
forward(features: KeyedJaggedTensor)  KeyedTensor
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
238 239 240 241
```

参数:

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
242
**特征***KeyedJaggedTensor* → FeatureProcessedEmbeddingBagCollection
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
243 244 245
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
246
tbes: torch.nn.ModuleList
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
247 248 249
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
250
training: bool
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
251 252 253
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
254
torchrec.quant.embedding_modules.for_each_module_of_type_do(module: Module, module_types: List[Type[Module]], op: Callable[[Module], None]) → None
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
255 256 257
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
258
torchrec.quant.embedding_modules.pruned_num_embeddings(pruning_indices_mapping: Tensor) → int
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
259 260 261
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
262
torchrec.quant.embedding_modules.quant_prep_customize_row_alignment(module: Module, module_types: List[Type[Module]], row_alignment: int) → None
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
263 264 265
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
266
torchrec.quant.embedding_modules.quant_prep_enable_quant_state_dict_split_scale_bias(module: Module) → None
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
267 268 269
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
270
torchrec.quant.embedding_modules.quant_prep_enable_quant_state_dict_split_scale_bias_for_types(module: Module, module_types: List[Type[Module]]) → None
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
271 272 273
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
274
torchrec.quant.embedding_modules.quant_prep_enable_register_tbes(module: Module, module_types: List[Type[Module]]) → None
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
275 276 277
```

```py
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
278 279
torchrec.quant.embedding_modules.quantize_state_dict(module: Module, table_name_to_quantized_weights: Dict[str, Tuple[Tensor, Tensor]], table_name_to_data_type: Dict[str, DataType], table_name_to_pruning_indices_mapping: Optional[Dict[str, Tensor]] = None) → device
```  ## 模块内容
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
280

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
281
Torchrec 量化
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
282

绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
283
Torchrec 为推断提供了 EmbeddingBagCollection 的量化版本。它依赖于 fbgemm 量化操作。这减少了模型权重的大小并加快了模型执行速度。
绝不原创的飞龙's avatar
绝不原创的飞龙 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

示例

```py
>>> import torch.quantization as quant
>>> import torchrec.quant as trec_quant
>>> import torchrec as trec
>>> qconfig = quant.QConfig(
>>>     activation=quant.PlaceholderObserver,
>>>     weight=quant.PlaceholderObserver.with_args(dtype=torch.qint8),
>>> )
>>> quantized = quant.quantize_dynamic(
>>>     module,
>>>     qconfig_spec={
>>>         trec.EmbeddingBagCollection: qconfig,
>>>     },
>>>     mapping={
>>>         trec.EmbeddingBagCollection: trec_quant.EmbeddingBagCollection,
>>>     },
>>>     inplace=inplace,
>>> ) 
```