目录

推理

TorchRec 提供了易于使用的 API,用于转换编写的 TorchRec 模型 通过 EAGER 模块交换转换为分布式推理的优化推理模型。

这会将 TorchRec 模块(如模型中的模块)转换为 可以使用 torch.fx 和 TorchScript 编译的量化分片版本 用于 C++ 环境中的推理。EmbeddingBagCollection

预期用途是调用 model 后跟 。quantize_inference_modelshard_quant_model

torchrec.inference.modules 中。quantize_inference_modelmodel modulequantization_mapping optional[dict[str type[module]]] = per_table_weight_dtype:可选[Dict[str dtype]] = fp_weight_dtypedtype = torch.int8,quantization_dtypedtype = torch.int8output_dtype: dtype = torch.float32 模块

量化模型,将 TorchRec 系列模块与其 量化的对应项(例如 EmbeddingBagCollection -> QuantEmbeddingBagCollection)。

参数
  • modeltorch.nn.Module) – 要量化的模型

  • quantization_mappingOptional[Dict[strType[torch.nn.Module]]] – 从 将 Original Module type 转换为 quantized Module 类型。如果未提供,将使用默认映射: (EmbeddingBagCollection -> QuantEmbeddingBagCollection、EmbeddingCollection -> QuantEmbeddingCollection)。

  • per_table_weight_dtypeOptional[Dict[strtorch.dtype]]) – 从表名到权重 dtype 的映射。 如果未提供,将使用默认的量化 dtype (int8)。

  • fp_weight_dtypetorch.dtype) – 特征处理器权重所需的量化 dtype FeatureProcessedEmbeddingBagCollection(如果使用)。默认值为 int8。

结果

量化模型

返回类型

torch.nn.Module

例:

ebc = EmbeddingBagCollection(tables=eb_configs, device=torch.device("meta"))

module = DLRMPredictModule(
    embedding_bag_collection=ebc,
    dense_in_features=self.model_config.dense_in_features,
    dense_arch_layer_sizes=self.model_config.dense_arch_layer_sizes,
    over_arch_layer_sizes=self.model_config.over_arch_layer_sizes,
    id_list_features_keys=self.model_config.id_list_features_keys,
    dense_device=device,
)

quant_model = quantize_inference_model(module)
torchrec.inference.modules 中。shard_quant_modelmodel Moduleworld_size int = 1compute_device str = 'cuda'sharding_device str = 'meta'分片器: 可选[List[ModuleSharder[模块]]] = device_memory_size:可选[int] = 约束 可选[Dict[str ParameterConstraints]] = ddr_cap: 可选[int] = None Tuple[Module ShardingPlan]

分片一个量化的 TorchRec 模型,用于生成最佳的推理模型和 分布式推理所必需的。

参数
  • modeltorch.nn.Module) – 要分片的量化模型

  • world_sizeint) – 要对模型进行分片的设备数,默认为 1

  • compute_devicestr) – 运行模型的设备,默认为 “cuda”

  • sharding_devicestr) – 运行分片的设备,默认为 “meta”

  • shardersOptional[List[ModuleSharder[torch.nn.Module]]] – 用于分片的分片 量化模型,默认为 QuantEmbeddingBagCollectionSharder、QuantEmbeddingCollectionSharder、 QuantFeatureProcessedEmbeddingBagCollectionSharder 的 QuantFeatureProcessedBeddingBagCollectionSharder 中。

  • device_memory_sizeOptional[int]) – cuda 设备的内存限制,默认为 None

  • constraintsOptional[Dict[strParameterConstraints]]) – 用于分片的约束,默认为 None 然后,它将实现默认约束,并将 QuantEmbeddingBagCollection 分片为 TableWise

结果

分片模型和分片计划

返回类型

元组[torch.nn.Module, ShardingPlan]]

例::

ebc = EmbeddingBagCollection(tables=eb_configs, device=torch.device(“meta”))

模块 = DLRMPredictModule(

embedding_bag_collection=EBC, dense_in_features=self.model_config.dense_in_features 中, dense_arch_layer_sizes=self.model_config.dense_arch_layer_sizes, over_arch_layer_sizes=self.model_config.over_arch_layer_sizes, id_list_features_keys=self.model_config.id_list_features_keys 中, dense_device=设备,

)

quant_model = quantize_inference_model(模块) sharded_model, _ = shard_quant_model(quant_model)

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

获取面向初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并解答您的问题

查看资源