record记录文件
record文件,为基于protobuf协议的序列化数据结构文件,记录量化场景量化因子scale/offset,稀疏场景各稀疏层间的级联关系等,通过该文件、压缩配置文件以及原始网络模型文件,生成压缩后的模型文件。
record原型定义
record文件对应的protobuf原型定义为(或查看_AMCT安装目录_/amct_pytorch/proto/scale_offset_record_pytorch.proto文件):
syntax = "proto2";
import "amct_pytorch/proto/basic_info.proto";
message SingleLayerRecord {
optional float scale_d = 1;
optional int32 offset_d = 2;
repeated float scale_w = 3;
repeated int32 offset_w = 4;
repeated uint32 shift_bit = 5;
repeated float tensor_balance_factor = 6;
optional bool skip_fusion = 9 [default = true];
optional string dst_type = 10 [default = 'INT8'];
optional string act_type = 11 [default = 'INT8'];
optional string wts_type = 12 [default = 'INT8'];
}
message SingleLayerKVCacheRecord {
repeated float scale = 1;
repeated int32 offset = 2;
}
message MapFiledEntry {
optional string key = 1;
optional SingleLayerRecord value = 2;
optional SingleLayerKVCacheRecord kv_cache_value = 3;
}
message ScaleOffsetRecord {
repeated MapFiledEntry record = 1;
repeated PruneRecord prune_record = 2;
}
message PruneRecord {
repeated PruneNode producer = 1;
repeated PruneNode consumer = 2;
optional PruneNode selective_prune = 3;
}
message PruneNode {
required string name = 1;
repeated AMCTProto.AttrProto attr = 2;
}
参数说明如下:
对于optional字段,由于protobuf协议未对重复出现的值报错,而是采用覆盖处理,因此出现重复配置的optional字段内容时会默认保留最后一次配置的值,需要用户自己保证文件的正确性。
record记录文件
最终生成的record文件格式为_record.txt_,文件内容根据特性不同划分如下。
-
量化特性record文件
对于一般量化层配置需要包含scale_d、offset_d、scale_w、offset_w参数,文件内容示例如下:
record { key: "conv1" value { scale_d: 0.0798481479 offset_d: 1 scale_w: 0.00297622895 offset_w: 0 skip_fusion: true dst_type: "INT8" } } record { key: "layer1.0.conv1" value { scale_d: 0.00392156886 offset_d: -128 scale_w: 0.00106807391 scale_w: 0.00104224426 offset_w: 0 offset_w: 0 dst_type: "INT8" } } -
量化数据均衡预处理特性record文件,内容示例如下:
record { key: "linear_1" value { scale_d: 0.00784554612 offset_d: -1 scale_w: 0.00778095098 offset_w: 0 tensor_balance_factor: 0.948409557 tensor_balance_factor: 0.984379828 } } record { key: "conv_1" value { scale_d: 0.00759239076 offset_d: -4 scale_w: 0.0075149606 offset_w: 0 tensor_balance_factor: 1.04744744 tensor_balance_factor: 1.44586647 } } -
通道稀疏record文件记录各稀疏层间的级联关系,文件内容示例如下:
prune_record { producer { name: "conv1" attr { name: "type" type: STRING s: "Conv2d" } attr { name: "begin" type: INT i: 0 } attr { name: "end" type: INT i: 64 } } consumer { name: "BN_1" attr { name: "type" type: STRING s: "FusedBatchNormV3" } attr { name: "begin" type: INT i: 0 } attr { name: "end" type: INT i: 64 } } } -
结构化稀疏record文件内容示例如下:
prune_record { selective_prune { name: "conv1" attr { name: "mask_shape" type: INTS ints: 3 ints: 3 ints: 3 ints: 32 } } }