目的与范围
用途与范围
本单元为 FP32 有限值转换、仿射量化到 S8 或 U8,以及反量化回 FP32 提供确定性的 PTO v0 参考实现。
PTO-ARCH-PROFILE-REFERENCE-QUANTIZATION下面直接显示完整的 ASL 所有者。
// PTO-UNIT: {"id":"PTO-ARCH-PROFILE-REFERENCE-QUANTIZATION","surface":"arch","classification":["profile","reference-quantization"],"depends_on":["PTO-ARCH-PROFILE-REFERENCE-PROFILE","PTO-TILE-MODEL-NUMERIC-FORMATS"]}pure func ReferencePowerOfTwo(exponent: integer {-149..127}) => realbegin var result: real = 1.0; if exponent > 0 then for step = 1 to exponent looplimit 127 do result = result * 2.0; end; elsif exponent < 0 then for step = 1 to -exponent looplimit 149 do result = result / 2.0; end; end; return result;end;
pure func ReferenceFP32FiniteValue(value: bits(32)) => realbegin let exponent = UInt(value[30:23]); let fraction = UInt(value[22:0]); assert exponent != 255; var magnitude: real = 0.0; if exponent == 0 then magnitude = Real(fraction) * ReferencePowerOfTwo(-149); else let significand = Real(0x800000 + fraction) / Real(0x800000); magnitude = significand * ReferencePowerOfTwo( (exponent - 127) as integer {-126..127}); end; if value[31] == '1' then return -magnitude; end; return magnitude;end;
pure func ReferenceIntegerValue(value: Word, data_type: TileDataType) => integerbegin let normalized = NormalizeTileInteger(value, data_type); if TileDataTypeIsSigned(data_type) then return SInt(normalized); end; return UInt(normalized);end;
func ReferenceFP32FiniteEncoding( value: real, rounding_mode: NumericRoundingMode) => (Word, bits(5))begin if value == 0.0 then return (Zeros{PTO_XLEN}, Zeros{5}); end; let negative = value < 0.0; var normalized = if negative then -value else value; var exponent: integer {-149..128} = 0; for step = 1 to 128 looplimit 128 do if normalized >= 2.0 && exponent < 128 then normalized = normalized / 2.0; exponent = (exponent + 1) as integer {-149..128}; end; end; for step = 1 to 149 looplimit 149 do if normalized < 1.0 && exponent > -149 then normalized = normalized * 2.0; exponent = (exponent - 1) as integer {-149..128}; end; end;
let sign = if negative then 0x80000000 else 0; if exponent > 127 then return ( Zeros{PTO_XLEN} + sign + 0x7f800000, Zeros{5} + 0x14); end;
if exponent < -126 then let scaled = value / ReferencePowerOfTwo(-149); let rounded = FloatingToInteger(scaled, rounding_mode); let magnitude = if rounded < 0 then -rounded else rounded; if magnitude == 0 then return (Zeros{PTO_XLEN} + sign, Zeros{5} + 0x18); end; return ( Zeros{PTO_XLEN} + sign + magnitude, if Real(rounded) == scaled then Zeros{5} else Zeros{5} + 0x18); end;
let scaled = if negative then -(normalized * Real(0x800000)) else normalized * Real(0x800000); let rounded = FloatingToInteger(scaled, rounding_mode); var magnitude = if rounded < 0 then -rounded else rounded; var encoded_exponent = exponent + 127; if magnitude == 0x1000000 then magnitude = 0x800000; encoded_exponent = (encoded_exponent + 1) as integer {-22..255}; end; if encoded_exponent >= 255 then return ( Zeros{PTO_XLEN} + sign + 0x7f800000, Zeros{5} + 0x14); end; let fraction = magnitude - 0x800000; return ( Zeros{PTO_XLEN} + sign + encoded_exponent * 0x800000 + fraction, if Real(rounded) == scaled then Zeros{5} else Zeros{5} + 0x10);end;
implementation func TileProfileQuantize(value: Word, scale: Word, zero_point: Word, source_type: TileDataType, destination_type: TileDataType, control: NumericExecutionControl) => (Word, bits(5))begin assert source_type == TileDataType_FP32; assert destination_type == TileDataType_S8 || destination_type == TileDataType_U8; let source_class = TileNumericValueClass(source_type, value); let scale_class = TileNumericValueClass(TileDataType_FP32, scale); assert !NumericValueClassIsNaN(scale_class); assert !NumericValueClassIsInfinity(scale_class); assert !NumericValueClassIsZero(scale_class); let minimum = ReferenceIntegerValue( TileIntegerMinimum(destination_type), destination_type); let maximum = ReferenceIntegerValue( TileIntegerMaximum(destination_type), destination_type); if NumericValueClassIsNaN(source_class) then return (Zeros{PTO_XLEN}, Zeros{5} + 1); elsif NumericValueClassIsInfinity(source_class) then let saturated = if value[31] == '1' then minimum else maximum; return ( NormalizeTileInteger( Zeros{PTO_XLEN} + saturated, destination_type), Zeros{5} + 0x14); end; let affine = ReferenceFP32FiniteValue(value[31:0]) * ReferenceFP32FiniteValue(scale[31:0]) + Real(ReferenceIntegerValue(zero_point, destination_type)); let rounded = FloatingToInteger(affine, control.rounding_mode); var selected = rounded; let flags = if Real(rounded) == affine then Zeros{5} else Zeros{5} + 0x10; if control.saturating then if selected < minimum then selected = minimum; elsif selected > maximum then selected = maximum; end; end; return ( NormalizeTileInteger(Zeros{PTO_XLEN} + selected, destination_type), flags);end;
implementation func TileProfileDequantize(value: Word, scale: Word, zero_point: Word, source_type: TileDataType, destination_type: TileDataType, control: NumericExecutionControl) => (Word, bits(5))begin assert source_type == TileDataType_S8 || source_type == TileDataType_U8; assert destination_type == TileDataType_FP32; let scale_class = TileNumericValueClass(TileDataType_FP32, scale); assert !NumericValueClassIsNaN(scale_class); assert !NumericValueClassIsInfinity(scale_class); assert !NumericValueClassIsZero(scale_class); let source_value = ReferenceIntegerValue(value, source_type); let zero_value = ReferenceIntegerValue(zero_point, source_type); let dequantized = Real(source_value - zero_value) * ReferenceFP32FiniteValue(scale[31:0]); return ReferenceFP32FiniteEncoding( dequantized, control.rounding_mode);end;
本单元为 FP32 有限值转换、仿射量化到 S8 或 U8,以及反量化回 FP32 提供确定性的 PTO v0 参考实现。
ReferencePowerOfTwo 构造指数 -149 到 127 对应的二次幂。ReferenceFP32FiniteValue 把有限 FP32 的符号、指数和小数部分解码为实数值。ReferenceIntegerValue 规范化 Tile 整数,并按源类型的有符号性解释该值。ReferenceFP32FiniteEncoding 执行带状态的反向有限值编码。TileProfileQuantize 接受 FP32 输入以及 S8 或 U8 输出。它检查 FP32 缩放值有限且非零,计算 source * scale + zero_point,按所选模式舍入,并可选择把结果截取到目标范围。
TileProfileDequantize 接受 S8 或 U8 输入以及 FP32 输出。它计算 (source - zero_point) * scale,再返回参考 FP32 编码。
量化 NaN 会产生零并报告 NV;无穷值选择对应符号的整数端点,并报告上溢和不精确状态。缩放值不能是 NaN、无穷或零。这些是 PTO v0 的 implementation 函数,因此其他具名配置档需要各自经过审阅的定义。
本示例块只用于帮助阅读:先应用上文规则,再到规范 ASL 所有者中确认结果。它不会增加任何架构契约。
正文来自 owning ASL。拖拽或按钮只临时改变当前页面显示顺序。
No NDF clause is attached to this unit.
10 matching entries
PTO-AVS-ARCH-PROFILE-REFERENCE-QUANTIZATION-STATIC-001tests/asl/arch/profile/reference-quantization/arch-static-reference-quantization-contract-001.asle5859faec80b723cccc8cd21c068ad1cd6ce6ba79c1ba1d9bf22e6365113487dPTO-AVS-ARCH-REFERENCE-QUANTIZATION-001tests/asl/arch/profile/reference-quantization/arch-exec-reference-quantization-001.asl77a1aeec8c6b63e08c22aca602ea197a22a51320e033721fa531bdfbb1046feaPTO-EVIDENCE-RELEASE-TRACEABILITYPTO-EVIDENCE-RELEASE-TRACEABILITYspec/evidence/release-traceability-readiness.jsonc7327021d39dc67ac5564bc55073b3870a397d79ac8d9648284d56e33bc14a3ePTO-EVIDENCE-INSTRUCTION-CONTRACT-CLOSUREPTO-EVIDENCE-INSTRUCTION-CONTRACT-CLOSUREspec/evidence/instruction-contract-closure.json3ef2bb62421c79dff8fa77a1c7983923b523244b8090812883ef81286ca8106aPTO-EVIDENCE-ARCHITECTURE-READINESSPTO-EVIDENCE-ARCHITECTURE-READINESSspec/evidence/architecture-readiness.json4b0b85199101251bea744e0f3591cc31906909dc80d5ab651c417a936036a004PTO-EVIDENCE-RELEASE-GATE-READINESSPTO-EVIDENCE-RELEASE-GATE-READINESSspec/evidence/release-gate-readiness.jsona0f4d2b6920c08981ea55fd8ef820708a40d4feb5402c5150e8e9ab532d84ce0PTO-EVIDENCE-RELEASE-MANIFESTPTO-EVIDENCE-RELEASE-MANIFESTspec/release-manifest.json1a64c109ed7a90351c41e2a418b3c0ebaf8ad975838986d2101385186b85c0d8Loading ADR-0005…
ADR-0005docs/status/decisions/0005-pto-v0-concrete-reference-profile.mda83528c2fc744cc120c2a0a32c82410059638ea88936fa65ac0eb36a0274d87cLoading ADR-0037…
ADR-0037docs/status/decisions/0037-numeric-profile-identity-and-variation-framework.mdb09efd36ccfc7258d1743c974f42b875ead42137c503f576a12f471f65b52e30Loading ADR-0042…
ADR-0042docs/status/decisions/0042-numeric-variation-point-ownership.mdf25df23306837e4199f6b7c03f70de853a693fdc6435c65ead57b2a174076081{
"classification": [
"profile",
"reference-quantization"
],
"documentation": "docs/arch/profile/reference-quantization.md",
"id": "PTO-ARCH-PROFILE-REFERENCE-QUANTIZATION",
"mnemonic": null,
"readiness_subjects": [
"ADR-0005",
"ADR-0037",
"ADR-0042"
],
"semantic_tests": [
"PTO-AVS-ARCH-REFERENCE-QUANTIZATION-001"
],
"source": "asl/arch/profile/reference-quantization.asl",
"surface": "arch",
"tests": [
"PTO-AVS-ARCH-PROFILE-REFERENCE-QUANTIZATION-STATIC-001",
"PTO-AVS-ARCH-REFERENCE-QUANTIZATION-001"
]
}0.58.5 · 候选发布7dc8b7e5b121d2b2499a2273bebff29e2cd86812e82820dc13f89142aa0448c4435f6cbd53d4eb0b85c05e63a887452c186d9aef5079808b9cb86d7eaa34576801b96182bdd5b47dfa6d74f4a37e7b1c21fa1efaasl/arch/profile/reference-quantization.asl