# record记录文件

record文件，为基于protobuf协议的序列化数据结构文件，记录量化因子scale/offset等，通过该文件、压缩配置文件以及原始网络模型文件，生成压缩后的模型文件。

#### record原型定义

record文件对应的protobuf原型定义为（或查看AMCT安装目录/amct_onnx/proto/scale_offset_record_onnx.proto文件）：

```
syntax = "proto2";
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;
optional bool skip_fusion = 6 [default = true];
optional bool is_tensor_quantize = 10 [default = false];
repeated float tensor_balance_factor = 13;
optional string op_data_type = 15;
optional string act_type = 20 [default = 'INT8'];
optional string wts_type = 21 [default = 'INT8'];
}
message MapFiledEntry {
optional string key = 1;
optional SingleLayerRecord value = 2;
}
message ScaleOffsetRecord {
repeated MapFiledEntry record = 1;
}
```

参数说明如下：

| 消息 | 是否必填 | 类型 | 字段 | 说明 |
| --- | --- | --- | --- | --- |
| SingleLayerRecord | \- | \- | \- | 包含了量化层所需要的所有量化因子记录信息。 |
| SingleLayerRecord | optional | float | scale\_d | 数据量化scale因子，仅支持对数据进行统一量化。 |
| SingleLayerRecord | optional | int32 | offset\_d | 数据量化offset因子，仅支持对数据进行统一量化。 |
| SingleLayerRecord | repeated | float | scale\_w | 权重量化scale因子，支持标量（对当前层的权重进行统一量化），向量（对当前层的权重按channel\_wise方式进行量化）两种模式，仅支持Conv2d类型进行channel\_wise量化模式。 |
| SingleLayerRecord | repeated | int32 | offset\_w | 权重量化offset因子，同scale\_w一样支持标量和向量两种模式，且需要同scale\_w维度一致，当前不支持权重带offset量化模式，offset\_w仅支持0。 |
| SingleLayerRecord | repeated | uint32 | shift\_bit | 移位因子。只有训练后量化简易配置文件配置了joint\_quant参数，shift\_bit参数才会写入record文件。 |
| SingleLayerRecord | optional | bool | skip\_fusion | 配置当前层是否要跳过Conv+BN融合，默认为false，即当前层要做上述融合。 |
| SingleLayerRecord | optional | bool | is\_tensor\_quantize | 标识当前record文件中Tensor的量化记录，默认为false，表示该条record记录为非Tensor量化记录。 |
| SingleLayerRecord | repeated | float | tensor\_balance\_factor | 均衡量化因子。该字段仅量化数据均衡预处理场景使用。 |
| SingleLayerRecord | optional | string | op\_data\_type | 算子的输入数据类型。包括FLOAT16和FLOAT32两种类型。 |
| SingleLayerRecord | optional | string | act\_type | 激活量化数据类型，包括INT8和INT16两种量化类型。当前版本仅支持INT8量化。 |
| SingleLayerRecord | optional | string | wts\_type | 权重量化数据类型。 当前INT6、INT7量化后的量化因子仍保存为INT8类型。 |
| ScaleOffsetRecord | \- | \- | \- | map结构，为保证兼容性，采用离散的map结构。 |
| ScaleOffsetRecord | repeated | MapFiledEntry | record | 每个record对应一个量化层的量化因子记录；record包括两个成员： key为所记录量化层的layer name。 value对应SingleLayerRecord定义的具体量化因子。 |
| MapFiledEntry | optional | string | key | 层名。 |
| MapFiledEntry | optional | SingleLayerRecord | value | 量化因子配置。 |


对于optional字段，由于protobuf协议未对重复出现的值报错，而是采用覆盖处理，因此出现重复配置的optional字段内容时会默认保留最后一次配置的值，需要用户自己保证文件的正确性。


#### record记录文件

最终生成的record文件格式为record.txt，文件内容根据特性不同划分如下。

- 量化特性record文件
  对于一般量化层配置需要包含scale_d、offset_d、scale_w、offset_w、shift_bit参数，文件内容示例如下：

```
record {
key: "
conv
"
value {
shift_bit: 1              //只有
训练后量化简易配置文件
配置了joint_quant参数，record文件中才会记录shift_bit信息
scale_d: 0.0798481479
offset_d: 1
op_data_type: 'FLOAT32'
scale_w: 0.007364662
scale_w: 0.0069018262
offset_w: 0
offset_w: 0
skip_fusion: true
act_type: "INT8"
wts_type: "INT8"
}
}
record {
key: "
maxpool_ld_default:0
"
value {
scale_d: 0.00392156886
offset_d: -128
op_data_type: 'FLOAT32'
is_tensor_quantize: true
}
}
```


- 量化数据均衡预处理特性record文件，内容示例如下：

```
record {
key: "matmul_1"
value {
scale_d: 0.00784554612
offset_d: -1
op_data_type: 'FLOAT32'
scale_w: 0.00778095098
offset_w: 0
shift_bit: 2                   //只有
训练后量化简易配置文件
配置了joint_quant参数，record文件中才会记录shift_bit信息
tensor_balance_factor: 0.948409557
tensor_balance_factor: 0.984379828
}
}
record {
key: "conv_1"
value {
scale_d: 0.00759239076
offset_d: -4
op_data_type: 'FLOAT32'
scale_w: 0.0075149606
offset_w: 0
shift_bit: 1
tensor_balance_factor: 1.04744744
tensor_balance_factor: 1.44586647
}
}
```
