llmcompressor.pytorch.utils
PyTorch通用的工具和辅助代码
模块
-
helpers–实用/辅助函数
-
sparsification–与模型稀疏化相关的信息的辅助函数
-
sparsification_info–
类
-
ModuleSparsificationInfo–提供PyTorch模块参数相关信息的辅助类
函数
-
get_linear_layers–:param module: 需要获取所有线性层的模块
-
get_quantized_layers–:param module: 需要从中获取量化层的模块
-
set_deterministic_seeds–手动设置numpy, random, 和 torch 包的随机种子。
-
tensor_sparsity–:param tens: 需要计算稀疏度的张量
-
tensors_module_forward–用于调用模型进行前向执行的默认函数。
-
tensors_to_device–将张量或张量集合放到相应设备上的默认函数。
-
tensors_to_precision–:param tensors: 需要更改精度的张量
ModuleSparsificationInfo
提供与PyTorch模块参数和稀疏化应用量相关信息的辅助类。包括剪枝和量化信息
参数
-
(moduleModule) –需要分析的PyTorch模块
-
(state_dictOptional[Dict[str, Tensor]], 默认值:None) –可选的state_dict,用于分析代替PyTorch模型。当分析FSDP模型时使用,此时可能无法访问完整的权重
属性
-
params_quantized(int) –:return: 量化层中的参数数量
-
params_quantized_percent(float) –:return: 量化参数的百分比
-
params_sparse(int) –:return: 模型中稀疏(0)可训练参数的总数
-
params_sparse_percent(float) –:return: 模型中稀疏化参数的百分比
-
params_total(int) –:return: 模型中可训练参数的总数
Source code in llmcompressor/pytorch/utils/sparsification.py
get_linear_layers
参数
-
(moduleModule) –需要获取所有线性层的模块
返回
-
Dict[str, Module]–模块中的所有线性层列表
Source code in llmcompressor/pytorch/utils/helpers.py
get_quantized_layers
参数
-
(moduleModule) –需要从中获取量化层的模块
返回
-
List[Tuple[str, Module]]–包含量化层(Embedding、Linear、Conv2d、Conv3d)的名称和模块的列表
Source code in llmcompressor/pytorch/utils/helpers.py
set_deterministic_seeds
手动设置numpy, random, 和 torch 包的随机种子。还设置 torch.backends.cudnn.deterministic 为 True
参数
-
(seedint, 默认值:0) –使用的手动种子。默认为0
Source code in llmcompressor/pytorch/utils/helpers.py
tensor_sparsity
tensor_sparsity(
tens: Tensor,
dim: Union[
None, int, List[int], Tuple[int, ...]
] = None,
) -> Tensor
参数
-
(tensTensor) –需要计算稀疏度的张量
-
(dimUnion[None, int, List[int], Tuple[int, ...]], default:None) –用于分割计算的维度;例如,可以按批次、通道或组合进行分割
返回
-
Tensor–输入张量的稀疏度,即零的比例
Source code in llmcompressor/pytorch/utils/helpers.py
tensors_module_forward
tensors_module_forward(
tensors: Union[
Tensor, Iterable[Tensor], Mapping[Any, Tensor]
],
module: Module,
check_feat_lab_inp: bool = True,
) -> Any
用于调用模型进行前向执行的默认函数。返回模型结果。注意,如果是一个可迭代对象,则传递给模型的特征被认为是索引0,其他索引是标签。
支持用例:单个张量,第一个张量作为特征传递给模型的迭代器
参数
-
(tensorsUnion[Tensor, Iterable[Tensor], Mapping[Any, Tensor]]) –要传递给模型的数据,如果是一个可迭代对象,则传递给模型的特征被认为是索引0,其他索引是标签
-
(moduleModule) –要将数据传递到的模块
-
(check_feat_lab_inpbool, 默认值:True) –如果为True,则检查传入的张量是否看起来由特征和标签组成(例如,一个包含2个元素的元组或列表,通常来自数据加载器),并将只用第一个元素调用模型,假定它是特征。如果为False,则不进行检查。
返回
-
Any–调用模型进行前向传递的结果
Source code in llmcompressor/pytorch/utils/helpers.py
tensors_to_device
tensors_to_device(
tensors: Union[
Tensor, Iterable[Tensor], Dict[Any, Tensor]
],
device: str,
) -> Union[Tensor, Iterable[Tensor], Dict[Any, Tensor]]
将张量或张量集合放到相应设备上的默认函数。返回放置在相应设备上的张量引用。
支持用例: - 单个张量 - 单个张量字典 - 单个张量迭代器字典 - 张量字典的字典 - 单个张量迭代器 - 张量迭代器的迭代器 - 张量字典的迭代器
参数
-
(tensorsUnion[Tensor, Iterable[Tensor], Dict[Any, Tensor]]) –要放置到设备上的张量或张量集合
-
(devicestr) –表示要将张量放置到的设备的字符串,例如:'cpu','cuda','cuda:1'
返回
-
Union[Tensor, Iterable[Tensor], Dict[Any, Tensor]]–放置到设备上的张量或张量集合
Source code in llmcompressor/pytorch/utils/helpers.py
tensors_to_precision
tensors_to_precision(
tensors: Union[
Tensor, Iterable[Tensor], Dict[Any, Tensor]
],
full_precision: bool,
) -> Union[Tensor, Iterable[Tensor], Dict[Any, Tensor]]
参数
-
(tensorsUnion[Tensor, Iterable[Tensor], Dict[Any, Tensor]]) –需要更改精度的张量
-
(full_precisionbool) –True 表示全精度(float 32),False 表示半精度(float 16)
返回
-
Union[Tensor, Iterable[Tensor], Dict[Any, Tensor]]–转换为所需精度的张量