Skip to content

Commit 24c2bf5

Browse files
committed
Update on "add quantized layer norm implementation"
Summary: Adds a quantized implementation of LayerNorm for server. Relevant PRs: * #20345 (floating point LN) * #33080 (quantized BN) A future PR will add the Python wrapper. Test Plan: numerics match the floating point implementation TODO: benchmarks Reviewers: Subscribers: Tasks: Tags: [ghstack-poisoned]
1 parent bce59e9 commit 24c2bf5

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

benchmarks/operator_benchmark/benchmark_all_quantized_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
qcomparators_test,
1212
qconv_test,
1313
qinterpolate_test,
14+
qlayernorm_test,
1415
qlinear_test,
1516
qobserver_test,
1617
qpool_test,
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from __future__ import absolute_import
2+
from __future__ import division
3+
from __future__ import print_function
4+
from __future__ import unicode_literals
5+
6+
7+
import operator_benchmark as op_bench
8+
import torch
9+
10+
11+
"""Microbenchmarks for quantized layernorm operator."""
12+
13+
layernorm_configs_long = op_bench.cross_product_configs(
14+
dims=(
15+
(64, 224, 224),
16+
(128, 112, 112),
17+
(256, 56, 56),
18+
),
19+
dtype=(torch.qint8,),
20+
tags=["short"],
21+
)
22+
23+
24+
class LayerNormBenchmark(op_bench.TorchBenchmarkBase):
25+
def init(self, dims, dtype):
26+
print(dims, dtype)
27+
28+
X = (torch.rand(*dims) - 0.5) * 256
29+
scale = 1.0
30+
zero_point = 0
31+
self.qX = torch.quantize_per_tensor(
32+
X, scale=scale, zero_point=zero_point, dtype=dtype)
33+
self.weight = torch.rand(*self.qX.size()[1:], dtype=torch.float)
34+
self.bias = torch.rand(*self.qX.size()[1:], dtype=torch.float)
35+
self.eps = 1e-5
36+
self.Y_scale = 0.1
37+
self.Y_zero_point = 0
38+
39+
def forward(self):
40+
return torch.ops.quantized.layer_norm(
41+
self.qX, self.qX.size()[1:], weight=self.weight, bias=self.bias,
42+
eps=self.eps, output_scale=self.Y_scale,
43+
output_zero_point=self.Y_zero_point)
44+
45+
46+
op_bench.generate_pt_test(layernorm_configs_long, LayerNormBenchmark)
47+
48+
49+
if __name__ == "__main__":
50+
op_bench.benchmark_runner.main()

0 commit comments

Comments
 (0)