该文档描述了concrete.ml.torch.compile
模块,该模块提供了将 PyTorch 和 ONNX 模型编译为 FHE 等效形式的功能。
<!-- markdownlint-disable -->
<a href="../../../src/concrete/ml/torch/compile.py#L0"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
concrete.ml.torch.compile
torch 编译函数。
<a href="../../../src/concrete/ml/torch/compile.py#L36"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
has_any_qnn_layers
has_any_qnn_layers(torch_model: Module) → bool
检查一个 torch 模型是否包含 QNN 层。
这对于检查一个模型是否为 QAT 模型很有用。
Args:
torch_model
</b> (torch.nn.Module): 一个 torch 模型Returns:
bool
</b>: 该 torch 模型是否包含任何 QNN 层。<a href="../../../src/concrete/ml/torch/compile.py#L52"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
convert_torch_tensor_or_numpy_array_to_numpy_array
convert_torch_tensor_or_numpy_array_to_numpy_array(
torch_tensor_or_numpy_array: Union[Tensor, ndarray]
) → ndarray
将一个 torch 张量或 numpy 数组转换为 numpy 数组。
Args:
torch_tensor_or_numpy_array
</b> (Tensor): 值为 torch 张量或者 numpy 数组。Returns:
numpy.ndarray
</b>: 转换为 numpy 数组后的值。<a href="../../../src/concrete/ml/torch/compile.py#L71"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
build_quantized_module
build_quantized_module(
model: Union[Module, ModelProto],
torch_inputset: Union[Tensor, ndarray, Tuple[Union[Tensor, ndarray], ]],
import_qat: bool = False,
n_bits: Union[int, Dict[str, int]] = 8,
rounding_threshold_bits: Union[NoneType, int, Dict[str, Union[str, int]]] = None,
reduce_sum_copy=False
) → QuantizedModule
从 Torch 或 ONNX 模型构建一个量化模块。
接收 torch 或 ONNX 中的模型,将其转换为 numpy,量化其输入/权重/输出并检索相关的量化模块。
Args:
model
</b> (Union[torch.nn.Module, onnx.ModelProto]): 要量化的模型,可以是 torch 或者 ONNX 格式。torch_inputset
</b> (Dataset): 校准输入集,可以包含 torch 张量或 numpy.ndarrayimport_qat
</b> (bool): 标志,用于指示被导入的网络包含量化器在其计算图中,并且 Concrete ML 不应该重新量化它n_bits
</b>: 量化的位数rounding_threshold_bits
</b> (Union[None, int, Dict[str, Union[str, int]]]): 定义模型累加器的精度舍入。 接受 None、int 或 dict。 该 dict 可以指定 'method' (fhe.Exactness.EXACT 或 fhe.Exactness.APPROXIMATE) 和 'n_bits' ('auto' 或 int)reduce_sum_copy
</b> (bool): 如果应该复制 QuantizedReduceSum 的输入以避免位宽传播Returns:
QuantizedModule
</b>: 生成的 QuantizedModule。<a href="../../../src/concrete/ml/torch/compile.py#L237"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
compile_torch_model
compile_torch_model(
torch_model: Module,
torch_inputset: Union[Tensor, ndarray, Tuple[Union[Tensor, ndarray], ]],
import_qat: bool = False,
configuration: Optional[Configuration] = None,
artifacts: Optional[DebugArtifacts] = None,
show_mlir: bool = False,
n_bits: Union[int, Dict[str, int]] = 8,
rounding_threshold_bits: Union[NoneType, int, Dict[str, Union[str, int]]] = None,
p_error: Optional[float] = None,
global_p_error: Optional[float] = None,
verbose: bool = False,
inputs_encryption_status: Optional[Sequence[str]] = None,
reduce_sum_copy: bool = False
) → QuantizedModule
将一个 torch 模块编译成等效的 FHE。
采用 torch 中的模型,将其转换为 numpy,量化其输入/权重/输出,最后使用 Concrete 编译它
Args:
torch_model
</b> (torch.nn.Module): 要量化的模型torch_inputset
</b> (Dataset): 校准输入集,可以包含 torch 张量或 numpy.ndarray。import_qat
</b> (bool): 设置为 True 以导入包含量化器并使用量化感知训练训练的网络configuration
</b> (Configuration): 编译期间使用的配置对象artifacts
</b> (DebugArtifacts): 编译期间填充的 Artifacts 对象show_mlir
</b> (bool): 如果设置,转换器生成的 MLIR 将显示在屏幕上,该 MLIR 将被发送到编译器后端,例如,用于调试或演示n_bits
</b> (Union[int, Dict[str, int]]): 量化的位数,可以是一个单独的值或一个字典,其中包含以下键:
- “op_inputs” 和 “op_weights”(强制性)
- “model_inputs” 和 “model_outputs”(可选,默认为 5 位)。 当使用单个整数作为 n_bits 时,其值将分配给 “op_inputs” 和 “op_weights” 位。 默认为 8 位。rounding_threshold_bits
</b> (Union[None, int, Dict[str, Union[str, int]]]): 定义模型累加器的精度舍入。 接受 None、int 或 dict。 该 dict 可以指定 'method' (fhe.Exactness.EXACT 或 fhe.Exactness.APPROXIMATE) 和 'n_bits' ('auto' 或 int)p_error
</b> (Optional[float]): 单个 PBS 的错误概率global_p_error
</b> (Optional[float]): 完整电路的错误概率。 在 FHE 模拟中,global_p_error
设置为 0verbose
</b> (bool): 是否显示编译信息inputs_encryption_status
</b> (Optional[Sequence[str]]): 每个输入的加密状态(“clear”、“encrypted”)。 默认情况下,将加密所有参数。reduce_sum_copy
</b> (bool): 如果应该复制 QuantizedReduceSum 的输入以避免位宽传播Returns:
QuantizedModule
</b>: 生成的已编译 QuantizedModule。<a href="../../../src/concrete/ml/torch/compile.py#L321"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
compile_onnx_model
compile_onnx_model(
onnx_model: ModelProto,
torch_inputset: Union[Tensor, ndarray, Tuple[Union[Tensor, ndarray], ]],
import_qat: bool = False,
configuration: Optional[Configuration] = None,
artifacts: Optional[DebugArtifacts] = None,
show_mlir: bool = False,
n_bits: Union[int, Dict[str, int]] = 8,
rounding_threshold_bits: Union[NoneType, int, Dict[str, Union[str, int]]] = None,
p_error: Optional[float] = None,
global_p_error: Optional[float] = None,
verbose: bool = False,
inputs_encryption_status: Optional[Sequence[str]] = None,
reduce_sum_copy: bool = False
) → QuantizedModule
将一个 torch 模块编译成等效的 FHE。
采用 torch 中的模型,将其转换为 numpy,量化其输入/权重/输出,最后使用 Concrete-Python 编译它
Args:
onnx_model
</b> (onnx.ModelProto): 要量化的模型torch_inputset
</b> (Dataset): 校准输入集,可以包含 torch 张量或 numpy.ndarray。import_qat
</b> (bool): 标志,用于指示被导入的网络包含量化器在其计算图中,并且 Concrete ML 不应该重新量化它。configuration
</b> (Configuration): 编译期间使用的配置对象artifacts
</b> (DebugArtifacts): 编译期间填充的 Artifacts 对象show_mlir
</b> (bool): 如果设置,转换器生成的 MLIR 将显示在屏幕上,该 MLIR 将被发送到编译器后端,例如,用于调试或演示n_bits
</b> (Union[int, Dict[str, int]]): 量化的位数,可以是一个单独的值或一个字典,其中包含以下键:
- “op_inputs” 和 “op_weights”(强制性)
- “model_inputs” 和 “model_outputs”(可选,默认为 5 位)。 当使用单个整数作为 n_bits 时,其值将分配给 “op_inputs” 和 “op_weights” 位。 默认为 8 位。rounding_threshold_bits
</b> (Union[None, int, Dict[str, Union[str, int]]]): 定义模型累加器的精度舍入。 接受 None、int 或 dict。 该 dict 可以指定 'method' (fhe.Exactness.EXACT 或 fhe.Exactness.APPROXIMATE) 和 'n_bits' ('auto' 或 int)p_error
</b> (Optional[float]): 单个 PBS 的错误概率global_p_error
</b> (Optional[float]): 完整电路的错误概率。 在 FHE 模拟中,global_p_error
设置为 0verbose
</b> (bool): 是否显示编译信息inputs_encryption_status
</b> (Optional[Sequence[str]]): 每个输入的加密状态(“clear”、“encrypted”)。 默认情况下,将加密所有参数。reduce_sum_copy
</b> (bool): 如果应该复制 QuantizedReduceSum 的输入以避免位宽传播Returns:
QuantizedModule
</b>: 生成的已编译 QuantizedModule。<a href="../../../src/concrete/ml/torch/compile.py#L401"><img align="right" style="float:right;" src="https://img.shields.io/badge/-source-cccccc?style=flat-square"></a>
compile_brevitas_qat_model
compile_brevitas_qat_model(
torch_model: Module,
torch_inputset: Union[Tensor, ndarray, Tuple[Union[Tensor, ndarray], ]],
n_bits: Optional[int, Dict[str, int]] = None,
configuration: Optional[Configuration] = None,
artifacts: Optional[DebugArtifacts] = None,
show_mlir: bool = False,
rounding_threshold_bits: Union[NoneType, int, Dict[str, Union[str, int]]] = None,
p_error: Optional[float] = None,
global_p_error: Optional[float] = None,
output_onnx_file: Union[NoneType, Path, str] = None,
verbose: bool = False,
inputs_encryption_status: Optional[Sequence[str]] = None,
reduce_sum_copy: bool = False
) → QuantizedModule
编译 Brevitas 量化感知训练模型。
torch_model 参数是 torch.nn.Module 的子类,它使用 brevitas.qnn 中的量化操作。 在调用此函数之前,需要训练模型。 此函数将经过训练的模型编译为 FHE。
Args:
torch_model
</b> (torch.nn.Module): 要量化的模型torch_inputset
</b> (Dataset): 校准输入集,可以包含 torch 张量或 numpy.ndarray。n_bits
</b> (Optional[Union[int, dict]): 量化的位数。 默认情况下,对于大多数模型,应提供 None 值,这指示 Concrete ML 使用使用 Brevitas 量化选项配置的位宽。 对于某些网络,它们在输出端的输入上执行非线性操作,如果给定 None,则输入/输出量化使用 8 位的默认值。 对于此类模型,用户还可以指定带有 model_inputs/model_outputs 键的字典来覆盖 8 位默认值,或者为这两个值指定一个整数。configuration
</b> (Configuration): 编译期间使用的配置对象artifacts
</b> (DebugArtifacts): 编译期间填充的 Artifacts 对象show_mlir
</b> (bool): 如果设置,转换器生成的 MLIR 将显示在屏幕上,该 MLIR 将被发送到编译器后端,例如,用于调试或演示rounding_threshold_bits
</b> (Union[None, int, Dict[str, Union[str, int]]]): 定义模型累加器的精度舍入。 接受 None、int 或 dict。 该 dict 可以指定 'method' (fhe.Exactness.EXACT 或 fhe.Exactness.APPROXIMATE) 和 'n_bits' ('auto' 或 int)p_error
</b> (Optional[float]): 单个 PBS 的错误概率global_p_error
</b> (Optional[float]): 完整电路的错误概率。 在 FHE 模拟中,global_p_error
设置为 0output_onnx_file
</b> (str): 用于存储 ONNX 模型的临时文件。 如果为 None,则生成临时文件verbose
</b> (bool): 是否显示编译信息inputs_encryption_status
</b> (Optional[Sequence[str]]): 每个输入的加密状态(“clear”、“encrypted”)。 默认情况下,将加密所有参数。reduce_sum_copy
</b> (bool): 如果应该复制 QuantizedReduceSum 的输入以避免位宽传播Returns:
QuantizedModule
</b>: 生成的已编译 QuantizedModule。
- 原文链接: github.com/zama-ai/concr...
- 登链社区 AI 助手,为大家转译优秀英文文章,如有翻译不通的地方,还请包涵~
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!