1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
| /*++
|
| Copyright (c) Microsoft Corporation. All rights reserved.
|
| Licensed under the MIT License.
|
| Module Name:
|
| layernorm.cpp
|
| Abstract:
|
| This module implements the dispatch for platform-optimized
| LayerNorm/RMSNorm kernels.
|
| --*/
|
| #include "mlasi.h"
|
| bool
| MLASCALL
| MlasLayerNormF32(
| const float* Input,
| const float* Scale,
| const float* Bias,
| float* Output,
| float* MeanOut,
| float* InvStdDevOut,
| size_t NormSize,
| float Epsilon,
| bool Simplified
| )
| {
| auto kernel = GetMlasPlatform().LayerNormF32Kernel;
| if (kernel == nullptr) {
| return false;
| }
|
| #if defined(MLAS_TARGET_AMD64) || defined(MLAS_TARGET_IX86)
| //
| // Skip the AVX2 kernel for short rows where it cannot win.
| //
| // LayerNorm performs vector work from 8 elements onward. RMSNorm needs
| // at least 16 elements to recover its additional setup costs.
| //
| // This threshold is x86-specific. Other platforms (e.g. RISC-V RVV)
| // use variable-length vectors and handle short rows natively, so they
| // must not be gated here.
| //
| // Keep in sync: test/mlas/unittest/test_layernorm.cpp kKernelDispatchThreshold.
| //
| //
| const size_t dispatch_threshold = Simplified ? 16 : 8;
| if (NormSize < dispatch_threshold) {
| return false;
| }
| #endif
|
| kernel(Input, Scale, Bias, Output, MeanOut, InvStdDevOut, NormSize, Epsilon, Simplified);
| return true;
| }
|
| bool
| MLASCALL
| MlasLayerNormF16(
| const uint16_t* Input,
| const float* Scale,
| const float* Bias,
| uint16_t* Output,
| float* MeanOut,
| float* InvStdDevOut,
| size_t NormSize,
| float Epsilon,
| bool Simplified
| )
| {
| auto kernel = GetMlasPlatform().LayerNormF16Kernel;
| if (kernel == nullptr || NormSize < 16) {
| return false;
| }
|
| kernel(Input, Scale, Bias, Output, MeanOut, InvStdDevOut, NormSize, Epsilon, Simplified);
| return true;
| }
|
|