Skip to content

Commit e920588

Browse files
committed
Address review comments
Signed-off-by: Jonathan Clohessy <Jonathan.Clohessy@arm.com>
1 parent ed627f2 commit e920588

6 files changed

Lines changed: 28 additions & 49 deletions

File tree

onnxruntime/core/mlas/inc/mlas.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,10 @@ struct MLAS_CONV_PARAMETERS {
893893
size_t BatchCount;
894894
size_t GroupCount;
895895
size_t InputChannels;
896+
/**
897+
* Existing Conv layout flag from MlasConvPrepare; for HalfConv this matches
898+
* InputOutputChannelsLast.
899+
*/
896900
bool ChannelsLast;
897901
size_t InputShape[3];
898902
size_t KernelShape[3];
@@ -911,6 +915,11 @@ struct MLAS_CONV_PARAMETERS {
911915
const void* PackedFilter = nullptr;
912916
size_t PackedFilterGroupStride = 0;
913917
bool FilterIsPacked = false;
918+
/**
919+
* HalfConv-specific: true means caller-provided input and output tensors
920+
* are channels-last (NHWC); false means NCHW public tensors with any NHWC
921+
* staging handled internally.
922+
*/
914923
bool InputOutputChannelsLast = false;
915924
union {
916925
struct {
@@ -1952,6 +1961,11 @@ struct MLAS_HALF_GEMM_DATA_PARAMS {
19521961
* Backend-native packed B is a constrained direct-consumption layout
19531962
* and does not support runtime Bias or OutputProcessor.
19541963
*
1964+
* Uses MLAS_THROW_EX(std::runtime_error, ...) for contract violations that
1965+
* cannot be safely ignored: non-empty work with null DataParams, malformed
1966+
* backend-native packed-B parameters on the K == 0 path, or backend-native
1967+
* packed B reaching the generic MLAS kernels when no backend override consumes it.
1968+
*
19551969
* Note: We only support uniform batching, so shapes and types of the
19561970
* input must be same across all parameter blocks.
19571971
*
@@ -1996,6 +2010,9 @@ MlasHalfGemmPackBSize(
19962010
* @brief For half precision GEMM, pack the right hand
19972011
* side matrix B
19982012
*
2013+
* @pre For non-zero N and K, B and PackedB must be non-null and ldb must be at
2014+
* least N.
2015+
*
19992016
* @param[in] N Number of columns
20002017
* @param[in] K Number of rows
20012018
* @param[in] B Address of matrix B

onnxruntime/core/mlas/lib/halfgemm.cpp

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ Module Name:
1919
#include "mlas_float16.h"
2020

2121
#include "halfgemm.h"
22-
#if defined(USE_KLEIDIAI)
23-
#include "kleidiai/mlasi_kleidiai.h"
24-
#endif
2522

2623
#include <exception>
2724

@@ -63,8 +60,7 @@ MlasFp16AccelerationSupported()
6360
#endif
6461
}
6562

66-
#if defined(USE_KLEIDIAI)
67-
bool
63+
static bool
6864
TryGetHalfGemmBackendSelectorConfig(
6965
size_t BatchN,
7066
const MLAS_HALF_GEMM_DATA_PARAMS* DataParams,
@@ -90,7 +86,6 @@ TryGetHalfGemmBackendSelectorConfig(
9086

9187
return true;
9288
}
93-
#endif
9489

9590
void
9691
MLASCALL
@@ -126,15 +121,13 @@ MlasHalfGemmBatch(
126121
return;
127122
}
128123

129-
#if defined(USE_KLEIDIAI)
130124
const MLAS_BACKEND_KERNEL_SELECTOR_CONFIG* BackendKernelSelectorConfig = nullptr;
131125
if (TryGetHalfGemmBackendSelectorConfig(BatchN, DataParams, BackendKernelSelectorConfig) &&
132126
GetMlasPlatform().MlasHalfGemmBatchOverride != nullptr &&
133127
GetMlasPlatform().MlasHalfGemmBatchOverride(
134128
M, N, K, BatchN, DataParams, ThreadPool, BackendKernelSelectorConfig)) {
135129
return;
136130
}
137-
#endif
138131

139132
// BIsPacked denotes the generic MLAS halfgemm packed-B layout and can be
140133
// consumed here. Backend-native packed layouts are separate and must not
@@ -251,6 +244,7 @@ MlasHalfGemmPackB(
251244
)
252245
{
253246
const auto* dispatch = MlasHalfGemmGetDispatch();
247+
assert(N == 0 || K == 0 || (B != nullptr && PackedB != nullptr && ldb >= N));
254248
if (B == nullptr || PackedB == nullptr || ldb < N ||
255249
dispatch->CopyPackBRoutine == nullptr || MlasHalfGemmPackBSize(N, K, false) == 0) {
256250
return;
@@ -269,18 +263,11 @@ MlasHalfGemmNativePackBSize(
269263
const MLAS_BACKEND_KERNEL_SELECTOR_CONFIG* BackendKernelSelectorConfig
270264
)
271265
{
272-
#if defined(USE_KLEIDIAI)
273266
if ((!BackendKernelSelectorConfig || BackendKernelSelectorConfig->use_kleidiai) &&
274267
GetMlasPlatform().MlasHalfGemmPackBSizeOverride != nullptr) {
275268
return GetMlasPlatform().MlasHalfGemmPackBSizeOverride(TransA, TransB, N, K);
276269
}
277-
#else
278-
MLAS_UNREFERENCED_PARAMETER(TransA);
279-
MLAS_UNREFERENCED_PARAMETER(TransB);
280-
MLAS_UNREFERENCED_PARAMETER(N);
281-
MLAS_UNREFERENCED_PARAMETER(K);
282-
MLAS_UNREFERENCED_PARAMETER(BackendKernelSelectorConfig);
283-
#endif
270+
284271
return 0;
285272
}
286273

@@ -297,21 +284,11 @@ MlasHalfGemmNativePackB(
297284
const MLAS_BACKEND_KERNEL_SELECTOR_CONFIG* BackendKernelSelectorConfig
298285
)
299286
{
300-
#if defined(USE_KLEIDIAI)
301287
if ((!BackendKernelSelectorConfig || BackendKernelSelectorConfig->use_kleidiai) &&
302288
GetMlasPlatform().MlasHalfGemmPackBOverride != nullptr) {
303289
return GetMlasPlatform().MlasHalfGemmPackBOverride(TransA, TransB, N, K, B, ldb, PackedB);
304290
}
305-
#else
306-
MLAS_UNREFERENCED_PARAMETER(TransA);
307-
MLAS_UNREFERENCED_PARAMETER(TransB);
308-
MLAS_UNREFERENCED_PARAMETER(N);
309-
MLAS_UNREFERENCED_PARAMETER(K);
310-
MLAS_UNREFERENCED_PARAMETER(B);
311-
MLAS_UNREFERENCED_PARAMETER(ldb);
312-
MLAS_UNREFERENCED_PARAMETER(PackedB);
313-
MLAS_UNREFERENCED_PARAMETER(BackendKernelSelectorConfig);
314-
#endif
291+
315292
return false;
316293
}
317294

onnxruntime/core/mlas/lib/kleidiai/convolve_kleidiai.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ static std::shared_ptr<const void*[]> GetOrCreateLhsPtrTableSme(const size_t ci,
460460
};
461461

462462
// Cache of computed LHS pointer offsets. thread_local to prevent interference from parallel sessions.
463-
// Entries include pointers to the pad buffer for out-of-bounds pixels, so group by pad buffer identity and
464-
// invalidate the old group if the grow-only pad buffer is reallocated.
463+
// Entries include pointers to the pad buffer for out-of-bounds pixels; if the grow-only pad buffer
464+
// reallocates, erase the old group so stale pointer tables cannot reference the previous allocation.
465465
using LhsPtrsCache = std::unordered_map<LhsCacheKey, std::shared_ptr<const void*[]>>;
466466
thread_local std::unordered_map<const float*, LhsPtrsCache> lhs_ptrs_cache_by_pad;
467467
thread_local const float* last_pad_ptr = nullptr;

onnxruntime/core/mlas/lib/kleidiai/halfgemm_kleidiai.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ ArmKleidiAI::MlasHalfGemmBatch(
177177
return false;
178178
}
179179

180+
// TODO: Plumb MLAS_ACTIVATION through this call site if MLAS_HALF_GEMM_DATA_PARAMS
181+
// grows fused activation support.
180182
const float clamp_min = -std::numeric_limits<float>::infinity();
181183
const float clamp_max = std::numeric_limits<float>::infinity();
182184

onnxruntime/core/mlas/lib/mlasi.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,6 @@ Module Name:
101101
#define MLAS_FORCEINLINE __attribute__ ((always_inline)) inline
102102
#endif
103103

104-
MLAS_FORCEINLINE
105-
bool
106-
MlasMultiplyOverflowsSizeT(
107-
size_t a,
108-
size_t b,
109-
size_t* out
110-
)
111-
{
112-
#if defined(__has_builtin)
113-
#if __has_builtin(__builtin_mul_overflow)
114-
return __builtin_mul_overflow(a, b, out);
115-
#endif
116-
#endif
117-
if (b != 0 && a > (std::numeric_limits<size_t>::max)() / b) {
118-
return true;
119-
}
120-
*out = a * b;
121-
return false;
122-
}
123-
124104
MLAS_FORCEINLINE
125105
bool
126106
MlasAddOverflowsSizeT(

onnxruntime/test/contrib_ops/matmul_4bits_test.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,10 @@ void TestMatMulNBitsTyped(std::optional<float> abs_error = std::nullopt,
307307
} else if (base_opts.accuracy_level == 4) {
308308
base_opts.output_abs_error = 0.1f;
309309
} else if constexpr (std::is_same<AType, MLFloat16>::value) {
310+
// The fp16 provider paths compare against a float reference while native kernels may accumulate
311+
// in fp16 (for example native HGEMM on SME; see PR #28786), so allow slightly wider drift.
310312
#if defined(USE_WEBGPU)
313+
// WebGPU's fp16 path has additional provider-specific rounding drift for these quantized matmul cases.
311314
base_opts.output_abs_error = 0.1f;
312315
#else
313316
base_opts.output_abs_error = 0.065f;

0 commit comments

Comments
 (0)