Skip to content

Sync with Microsoft ONNX Runtime - 14082026 - #1258

Merged
hdharpure9922 merged 25 commits into
ovep-developfrom
sync_msft_14082026
Aug 14, 2026
Merged

Sync with Microsoft ONNX Runtime - 14082026#1258
hdharpure9922 merged 25 commits into
ovep-developfrom
sync_msft_14082026

Conversation

@ai-fw-intg

Copy link
Copy Markdown

Automated daily backmerge from ORT main to ovep-develop. No conflicts detected. Do NOT squash or rebase - use merge commit only.

qjia7 and others added 25 commits August 13, 2026 08:38
### Description

- Adds the WebGPU enableRobustness provider option to control Dawn
robust buffer access for ORT-created devices.
- Exposes the boolean option through the shared JavaScript type surface
for the native Node.js WebGPU binding.
- Keeps robustness independent from validationMode and preserves
build-specific defaults: enabled in Debug, disabled in Release and
RelWithDebInfo.
- Handles the process-global WebGPU device lifecycle: the first created
device establishes the value; later conflicts and requests for external
devices are ignored with a warning.
- Does not forward the option through browser/WASM WebGPU, where
standard WebGPU APIs cannot configure Dawn toggle chains.
- Adds focused native coverage for explicit values, defaults, invalid
input, validation independence, conflicts, compile-only contexts, and
external devices.

### Motivation and Context

Dawn buffer robustness is a device-creation setting that is separate
from validation. ONNX Runtime previously enabled Dawn's
disable_robustness toggle unconditionally, so native users could not
request robust buffer access or configure the safety/performance
tradeoff independently.

This change exposes that choice for native WebGPU device creation while
making the shared-device behavior explicit. It keeps the
performance-oriented non-Debug default and enables the safer Debug
default.

### Testing

- build\Windows\Release\Release\onnxruntime_provider_test.exe
--gtest_filter=WebGpuContextTest.* (8 tests passed)
- npm run prebuild --prefix js/web
- npx prettier --check common/lib/inference-session.ts
web/lib/wasm/session-options.ts web/test/test-types.ts
…oft#31703)

### Description

Broadcast each lane's A vecs across the subgroup via `subgroupShuffle`
instead of having every lane re-read the full A tile from workgroup
memory. Each lane loads its own row once and shuffles it to the other
rows that need it. The workgroup tile is stored as
`[KAVecSizeForBlock32/2][kTileM][2]` pairs so the two vecs consumed per
dequantized weight column are read in one contiguous access.

`tile_m` now adapts to the element size (16 for f32, 32 for f16, since
f16 packs twice as many elements per register), and the compute-stage
reduction strategy is chosen from the adapter's guaranteed minimum
subgroup size:

- `subgroup_min_size >= tile_m`: single full-tile `subgroupShuffle` band
- `tile_m % subgroup_min_size == 0` (subgroup_min_size > 0): chunked
`subgroupShuffle` bands
- otherwise (including when Subgroups isn't supported): direct
reduction, no shuffle

The actual runtime subgroup size can exceed the adapter's guaranteed
minimum, so a raw `sg_id` can index `a_data_tile` out of bounds in both
shuffle branches. Cap it via `capped_sg_id = min(sg_id, kTileM - 1u)`
before the main loop and use it in place of `sg_id` in both branches.

Reduces redundant workgroup-memory reads for the A operand in the
MatMulNBits wide-tile prefill kernel, and lets it run correctly with the
best available strategy across adapters with varying or no subgroup
support.

| max_length = 8K | Prefill Length | Default Prefill TPS | Opt TileM-16
Prefill TPS | Opt TileM-32 Prefill TPS | Improvement |
| :----------- | -------------: | ------------------: |
-----------------------: | -----------------------: | ----------: |
| Panther Lake | 128 | 221.01 | 242.49 | 243.02 | 110% |
| Panther Lake | 1024 | 594.20 | 644.74 | 696.70 | 117% |
| Panther Lake | 4096 | 596.47 | 667.17 | 727.84 | 122% |
| Alder Lake | 128 | 46.76 | 86.13 | 113.09 | 242% |
| Alder Lake | 1024 | 44.72 | 94.43 | 129.06 | 289% |

[1] https://huggingface.co/onnx-community/Phi-4-mini-instruct-ONNX

### Motivation and Context
See above.
## Description

Adds official Go bindings for the ONNX Runtime C API using CGO,
following
the structure of the existing language bindings.

Closes microsoft#9786.

## Motivation

Go is widely used for backend services that perform ML inference,
including
embeddings, classification, and NLP workloads. This PR provides a
supported
Go interface without requiring third-party runtime dependencies.

## Highlights

- **Sessions**: create sessions from files or byte buffers and inspect
  inputs and outputs, including dynamic dimensions
- **Tensor I/O**: generic numeric tensors, byte-backed tensors, string
  tensors, scalar tensors, and zero-length dimensions
- **Session options**: thread counts, graph optimization, execution
mode,
  memory arena, profiling, and free-dimension overrides
- **Execution Providers**: CUDA V2, TensorRT V2, and a generic key-value
  configuration API for other EPs
- **I/O binding**: bind inputs and outputs to devices for accelerator
  workflows
- **Metadata and value inspection**: model metadata, sequences, and maps
- **Run options**: logging, tags, configuration entries, and
cancellation
  through `context.Context`
- **Concurrency**: concurrent `Session.Run` calls supported by the
wrapper

## Design

The bindings access the ONNX Runtime C API vtable through C shim
functions
because CGO cannot directly call C function pointers.

The ONNX Runtime shared library is loaded at runtime with `dlopen` on
Linux/macOS and `LoadDLL` on Windows. A single `OrtEnv` is shared within
the process.

Numeric input tensors use `runtime.Pinner` to avoid copying their
backing
storage. Output tensors expose ORT-allocated memory that remains valid
until
the value is closed.

Filesystem paths use `ORTCHAR_T`: UTF-16 on Windows and UTF-8 elsewhere.
Paths containing NUL bytes are rejected instead of being silently
truncated.

- Module: `github.com/microsoft/onnxruntime/go`
- Package: `onnxruntime`
- Minimum Go version: 1.26
- Dependencies: Go standard library and CGO only

## Correctness and platform hardening

The implementation includes safeguards for several lifecycle and
C-boundary
conditions:

- Cancellation watchers are stopped before their run options are
released,
  preventing stale cancellation from affecting later inference calls.
- Cancellation works with both internally created and caller-provided
  `RunOptions`.
- I/O binding keeps its associated session alive and locked across C
calls.
- Nil and closed values are rejected before their handles reach the C
API.
- Windows model paths are passed to ORT as `ORTCHAR_T`.
- Tensor element-count and byte-size calculations are checked for
overflow.
- Library handles are released when initialization fails.
- Initialization and shutdown state is synchronized.
- Negative intra-op and inter-op thread counts are rejected, with
boundary
  tests for `-1`, `0`, and `1`.

## Linux CI coverage

The existing Linux x64 Release job now tests the Go bindings against the
`libonnxruntime.so` built from the same checkout.

The job:

- Verifies that the shared library exists
- Loads that exact library through `ORT_LIB_PATH`
- Runs `go test -race -count=1 ./onnxruntime`
- Fails if the library is missing, cannot be loaded, or the tests fail

`-count=1` prevents a cached Go test result from hiding a problem with
the
newly built library.

## Tests

Local validation covers:

- Session lifecycle and inference correctness
- Dynamic shapes and zero-length dimensions
- Numeric, Boolean, string, byte-backed, and scalar tensors
- Type and shape mismatches
- Nil, closed, and use-after-close handling
- Concurrent inference on a shared session
- Context cancellation and run options
- I/O binding and model metadata
- Non-ASCII model paths and NUL-path rejection
- Sequence and map values
- Qwen3-Embedding-0.6B ONNX INT8 integration

The complete binding suite passes under the race detector against:

- ONNX Runtime 1.29 built locally with GCC 14
- ONNX Runtime 1.27 for compatibility coverage

Additional validation completed:

- `GOEXPERIMENT=cgocheck2`
- `go vet`
- `golangci-lint`
- repository `lintrunner`
- `actionlint`
- MinGW Windows cross-build with incompatible-pointer checks enabled
- Regenerated test models verified byte-for-byte unchanged

`BenchmarkSessionRun` was added to track successful inference
allocations.
Avoiding eager validation-message formatting reduced the local benchmark
from 504 B and 17 allocations to 440 B and 13 allocations per run.
## Description

- Route npm lockfile downloads and C# package restores through
authenticated package feeds.
- Configure package feeds before restore and build steps.
- Skip redundant dependency scans in artifact-only packaging jobs.
- Remove unnecessary package bootstrap steps that can contact public
registries.

## Motivation and Context

These changes keep packaging workflows compatible with network-isolated
build agents and prevent package tools from bypassing the configured
authenticated sources.

## Validation

- Parsed the modified Azure Pipelines YAML files.
- Checked the changes for whitespace errors.

---------

Copilot-Session: 4d78d7b1-43f9-4fe3-972f-3a70b59db4f6
Copilot-Session: 3434ce2e-83c3-4560-a518-f8c54dec8e5f
…crosoft#31478)

## Description

Decode-time projections in hybrid / MoE LLMs are GEMVs with an `N` that
is too small to fill a cuBLAS tile kernel. On the Qwen3.6-35B-A3B NVFP4
decode loop, router, linear-attention, and shared-expert gate
projections repeatedly launch with `M <= 8`, `N <= 1024`, and `K >=
128`.

This PR adds an experimental split-K GEMV kernel for that corner of the
shape space. The path remains off by default because real-model
measurements are currently slower than cuBLAS.

## Summary of Changes

### Kernel and dispatch

- Adds a row-major FP16 split-K GEMV for `M <= 8`, `N <= 1024`, and `K
>= 128`.
- Gates dispatch with `ORT_ENABLE_SMALL_N_GEMV=1`; all ineligible
layouts, shapes, transpose modes, and alpha values fall through to
cuBLAS.
- Reads the opt-in setting once when each MatMul kernel instance is
created, keeping environment parsing out of `Compute()`.
- Compares leading dimensions in `int64_t` so large shape values are not
narrowed before eligibility checks.

### Cross-block reduction

- Uses a grid of `(ceil(N / 32), split_k)` so K slices can run across
multiple SMs.
- Publishes FP32 partials through volatile global workspace accesses
before `__threadfence()` and the completion atomic, following CUDA's
last-block reduction visibility pattern.
- Reduces partials in fixed slice order for deterministic output.
- Uses per-invocation scratch for both workspace and completion
counters. The caller clears counters before every launch; the kernel
does not reset them.

### Tests

- Direct-kernel tests cover every `M` specialization, `N = 1`, `N = 32`,
`N = 1024`, uneven K splits, and repeated launches with explicit counter
initialization.
- Inputs vary along K and results are checked against an FP32 CPU
reference.
- Operator-level MatMul tests enable the feature and cover eligible `M =
8, N = 1` and `M = 8, N = 1024` dispatches plus an ineligible `K = 127`
cuBLAS fallback.

## Current Status

The path is default-off because it is currently slower than cuBLAS in
the real model. On H200 (SM90) for the target decode shapes:

| | us / call | us / decode step |
|---|---:|---:|
| cuBLAS | 5.0 | 671 |
| this kernel | 10.6 | 1491 |

A standalone microbenchmark had suggested approximately 2.1 us/call
after subtracting the back-to-back launch floor. That result kept the
small weight matrix resident in H200's L2, while the model reads cold
weights. Keeping the implementation behind an opt-in flag preserves it
for follow-up work that addresses that cold-read cost.

## Validation

- Compiled `matmul.cc`, `matmul_small_n_gemv.cu`, and both small-N GEMV
test translation units with the CUDA 13.0 Release build.
- Ran scoped `lintrunner` checks for all changed source and test files.
- Full `onnxruntime_test_all` linking is currently blocked by an
unrelated `-Werror=unused-variable` in
`contrib_ops/cuda/bert/paged_attention.cc` on the rebased main branch.

## Checklist

- [x] No behavior change by default
- [x] Cache-safe cross-block workspace publication
- [x] Deterministic fixed-order reduction
- [x] Operator-level enabled dispatch and fallback coverage
- [ ] Enabled by default; blocked on closing the real-model performance
gap against cuBLAS

---------

Co-authored-by: GitHub Copilot <copilot@example.com>
This pull request improves the `ExpandBuffer` function in the generation
device helper to better support tensors with rank greater than four, and
adds a new test to verify this behavior. The main changes are as
follows:

**Support for high-rank tensors in `ExpandBuffer`:**
* Refactored the `ExpandBuffer` implementation in
`generation_device_helper.cc` to use a dynamic `TensorShapeVector`
instead of a fixed-size array, allowing support for input tensors with
more than four dimensions.

**Testing enhancements:**
* Added an explicit test, `ExpandBufferSupportsRankGreaterThanFour`, to
`beam_search_test.cc` to ensure that `ExpandBuffer` correctly handles
tensors with rank greater than four.
* Included the necessary header import for `generation_device_helper.h`
in the test file.
This pull request adds handling and tests for cases where the
`LpNormalization` operator receives input tensors with zero elements
along the normalization axis. The main changes include an early return
in the implementation to avoid unnecessary computation and a new test to
verify correct behavior for zero-extent axes.

**LpNormalization operator improvements:**

* Added an early return in `LpNorm<T>::Compute` to immediately return
success when the input tensor has zero elements, preventing unnecessary
computation for empty inputs.

**Testing enhancements:**

* Introduced the `LpNormalizationZeroExtentAxis` test, which checks that
the operator correctly handles input tensors with a zero-extent axis for
both `p=1` and `p=2`, and for both `float` and `double` types.
…#31997)

RandomKernelImpl computed grid_size from CeilDiv(N, block_size *
UNROLL), which is zero when the output tensor has no elements. The
counter_offset expression then divided by block_size * grid_size *
UNROLL, a host-side division by zero that traps before the kernel is
launched. Zero-sized tensors are legal in ONNX, so return early when
there is nothing to fill.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The bias size check was skipped whenever bias_ was empty, but the
compute loop unconditionally reads one bias entry per channel. A model
carrying a present-but-empty bias attribute therefore passed validation
and then indexed an empty vector. GetAttrs returns OK for a present
attribute regardless of element count, so the empty case was reachable
from a model file.

An empty bias is not a usable state in either kernel: the constructor
already fails outright when the attribute is absent, so requiring the
size to equal the channel count keeps the existing contract and closes
the gap. The CUDA kernel had the same check and the same per-channel
read, so both are updated.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The kernel built tensor offsets as b * in_strides.x + s * in_strides.z +
n * in_strides.y with every operand a 32-bit int. Tensors may hold more
than INT32_MAX elements, so these products could wrap for large batch
sizes even though each individual stride fits in an int, yielding
negative offsets into the input, output, and cos/sin cache pointers.

Compute the offsets, b_s_index, and cache_offset in 64-bit, and verify
the packed stride products fit in int32 before launching. Applies the
same change to the contrib variant, which shares the pattern.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This pull request improves the safety and robustness of memory
allocation in the `BFCArena` allocator by adding overflow protection and
corresponding tests. The most important changes are:

**Overflow Protection and Safety:**

* Updated the `BFCArena::RoundedBytes` function in `bfc_arena.cc` to use
`SafeInt<size_t>` for arithmetic operations, preventing integer
overflows during memory rounding calculations.
* Included the `safeint.h` header to enable safe integer operations in
`bfc_arena.cc`.

**Testing:**

* Added a new test, `RoundedBytesOverflowThrows`, in `bfc_arena_test.cc`
to verify that an overflow during allocation throws an
`OnnxRuntimeException`.
* Included the `<limits>` header in `bfc_arena_test.cc` to support
boundary value tests.
This pull request updates several for-loop variable types in the
`Compress::Compute` method to use `int64_t` instead of `int`. This
improves consistency and prevents potential issues when handling large
tensors with dimensions exceeding the range of `int`.

Type consistency and correctness:

* Changed loop variable types from `int` to `int64_t` in all relevant
for-loops within `Compress::Compute` to ensure proper handling of large
tensor sizes and avoid integer overflow.
[[1]](diffhunk://#diff-ae917f21ae9567368ac33d8efca9eecaeed59676b74ef1d0a219041c4eb79ce8L47-R47)
[[2]](diffhunk://#diff-ae917f21ae9567368ac33d8efca9eecaeed59676b74ef1d0a219041c4eb79ce8L76-R76)
[[3]](diffhunk://#diff-ae917f21ae9567368ac33d8efca9eecaeed59676b74ef1d0a219041c4eb79ce8L91-R97)
[[4]](diffhunk://#diff-ae917f21ae9567368ac33d8efca9eecaeed59676b74ef1d0a219041c4eb79ce8L109-R109)
This pull request enhances the `NodeAttrHelper` utility by adding
overloads for retrieving string attributes with different types of
default values, improving safety and flexibility when handling attribute
lookups. It also updates usages to leverage these new overloads and
clarifies ownership semantics for returned strings.

**Node attribute retrieval improvements:**

* Added two new overloads to `NodeAttrHelper::Get` for string
attributes: one that accepts an rvalue (`std::string&&`) and one that
accepts a C-style string (`const char*`). These overloads return owned
strings, ensuring safe return of temporaries or literals. The
documentation was updated to clarify that lvalue defaults may be
returned by reference, while temporaries and literals are returned as
owned strings. (`onnxruntime/core/providers/qnn/ort_api.h`
[[1]](diffhunk://#diff-125d3340e34fbe06f276d3161fd4e0c4d95d9350ebfc828126c46d5444f30fa6R153-R156)
`onnxruntime/core/providers/shared/utils/utils.h`
[[2]](diffhunk://#diff-a5c52f37e0543b71a1adac09c86efa27c3e2220dc471e976cc6d2f302e0cc215R53-R56)
* Implemented the new overloads in both QNN and shared utility
implementations of `NodeAttrHelper`.
(`onnxruntime/core/providers/qnn/ort_api.cc`
[[1]](diffhunk://#diff-248fa3f7ee4456683dd0c16f459f2af7464410682d158604ed221e70d9bb1623R105-R120)
`onnxruntime/core/providers/shared/utils/utils.cc`
[[2]](diffhunk://#diff-610114cf2945da83d16585d09457c3220b11bfbf494d961d6dd60c38360d59c9R151-R166)
* Included `<utility>` header for `std::move` usage.
(`onnxruntime/core/providers/shared/utils/utils.cc`
[onnxruntime/core/providers/shared/utils/utils.ccR7-R8](diffhunk://#diff-610114cf2945da83d16585d09457c3220b11bfbf494d961d6dd60c38360d59c9R7-R8))

**Usage updates and bug fixes:**

* Updated code to use the new overloads, such as switching from a
reference to a temporary `std::string` for the default value in context
binary retrieval, preventing potential dangling reference issues.
(`onnxruntime/core/providers/qnn/builder/onnx_ctx_model_helper.cc`
[onnxruntime/core/providers/qnn/builder/onnx_ctx_model_helper.ccL94-R95](diffhunk://#diff-5704c965da458a23d9f15a196058ac9ca6afebde41b3c68956fb8f32a049aaffL94-R95))
* Fixed a usage in the VSINPU provider to use an owned string when
retrieving the `coordinate_transoformation_mode` attribute, aligning
with the new overloads and preventing reference issues.
(`onnxruntime/core/providers/vsinpu/builders/impl/resize_op_builder.h`
[onnxruntime/core/providers/vsinpu/builders/impl/resize_op_builder.hL72-R72](diffhunk://#diff-5d9e3ac9e2001465c159c39ef2b9a3dd1d69f05663b390d0c128c576615add53L72-R72))
…icrosoft#31999)

InitializeKernel copied the trailing spatialDimensionCount elements of
kernel_shape and the leading spatialDimensionCount elements of
output_padding without checking either vector was that long, so a
shorter attribute made the iterator arithmetic run off the ends of the
buffers. The output_padding check required only two elements, which is
short for 3D. The adjacent strides, dilations and pads attributes
already validate their lengths the same way; extend that to these two,
and to the filter tensor shape used when kernel_shape is absent.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…icrosoft#32000)

InferenceSession's constructor and destructor held
active_sessions_mutex_ while calling into the telemetry and ETW sink
callback registries, each of which takes its own registry lock. The ETW
dispatch path acquires those locks in the opposite order: a
capture-state notification runs the registered callback under the
registry lock, and that callback reaches LogAllSessions, which takes
active_sessions_mutex_. Two threads hitting these paths concurrently can
form a lock cycle and hang the process.

Register and unregister the callbacks outside the active_sessions_mutex_
critical section so the two locks are never held at once. The registries
are independently synchronized and do not require
active_sessions_mutex_.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This pull request improves the robustness and error handling of the CUDA
NonMaxSuppression (NMS) implementation in ONNX Runtime. The changes
ensure that large input sizes which could exceed the range of supported
integer indices are properly checked and handled, preventing potential
overflows or crashes. Additionally, a new CUDA-specific test has been
added to verify this behavior.

Error handling and robustness improvements:

* Updated the calculation of `max_nms_mask_size` in
`non_max_suppression_impl.cu` to use `SafeInt<size_t>` for safer
arithmetic and added a check to ensure the mask size does not exceed the
maximum value of an `int`. If the limit is exceeded, an error is
returned.
(`onnxruntime/core/providers/cuda/object_detection/non_max_suppression_impl.cu`)
* Included necessary headers for safe integer operations and numeric
limits (`<limits>`, `core/common/safeint.h>`) to support the above
changes.
(`onnxruntime/core/providers/cuda/object_detection/non_max_suppression_impl.cu`)

Testing enhancements:

* Added a CUDA-specific unit test that verifies the operator correctly
rejects inputs that would require a mask size outside the supported
integer range, ensuring the new error handling works as intended.
(`onnxruntime/test/providers/cpu/object_detection/non_max_suppression_test.cc`)
* Included conditional compilation and necessary CUDA test utilities to
enable the new test only when CUDA is available.
(`onnxruntime/test/providers/cpu/object_detection/non_max_suppression_test.cc`)
This pull request improves the robustness of the `Trilu` operator
implementation and its test coverage, particularly for edge cases
involving extreme diagonal values. The main changes include handling
out-of-bounds diagonal indices in the implementation and adding new
tests to ensure correct behavior for these cases.

**Implementation robustness:**

* Updated `TriluImpl` in `trilu.cc` to handle extreme values of `k_val`
(diagonal index), ensuring the output is correctly set or skipped when
`k_val` is out of bounds for both upper and lower triangular cases.

**Test coverage improvements:**

* Added new tests in `trilu_op_test.cc` to verify behavior when `k` is
set to the minimum and maximum possible `int64_t` values, ensuring the
operator's correctness for these edge cases.
* Included `<limits>` header to facilitate the use of extreme integer
values in tests.
This pull request introduces important safety checks to the quantization
code in ONNX Runtime to ensure that blockwise quantization shapes do not
exceed the valid `int32_t` index range, preventing potential overflows
and runtime errors. The main changes include the addition of a
validation function, integration of this check into quantization and
transpose routines, and new unit tests to verify the behavior.

**Shape validation and enforcement:**

* Added `MlasQDQBlockwiseShapeIsValid` function in `mlas_q4.h` to
validate that quantization shape parameters fit within the `int32_t`
index domain, guarding against arithmetic overflows.
* Updated `MlasQDQQuantizeBlockwise` and
`MlasQDQTransposeBlockwiseQuantized` in `q4_dq.cpp` to enforce this
validation using `ORT_ENFORCE`, throwing an exception if the shape is
invalid.
[[1]](diffhunk://#diff-c0645466d0d24ab96ae69729371fba6cc1b2ff971b7e92971d32d647d0925171R2281-R2282)
[[2]](diffhunk://#diff-c0645466d0d24ab96ae69729371fba6cc1b2ff971b7e92971d32d647d0925171R2368-R2369)
* Added a similar check in `TransposeDQWeightsForMatMulNBits` to return
an error if the shape is invalid.

**Testing and code hygiene:**

* Added a new unit test `RejectsShapesOutsideInt32IndexDomain` in
`test_blockq4.cpp` to verify that invalid shapes are correctly rejected
and exceptions are thrown as expected.
* Included missing headers `<cstdint>` and `<limits>` in `mlas_q4.h` to
support the new validation logic.
…icrosoft#31678)

This pull request introduces additional validation and safety checks for
the `block_size` attribute in quantization-related code to prevent
unsafe values from causing runtime errors. The main changes ensure that
`block_size` is always within the safe range supported by the underlying
MLAS kernel, avoiding potential divide-by-zero errors.

Validation and safety improvements for `block_size`:

* Added a maximum block size guard (`kMaxBlockSize = 256`) in
`GetEffectiveBlockSize` to cap user-supplied values and prevent unsafe
block sizes from reaching the MLAS kernel, mirroring existing checks
elsewhere.
* Updated the validation logic in `ValidateDQForMatMulNBits` to require
that `block_size` is a power of two in the range [16, 256], explicitly
rejecting values above 256 to avoid integer overflow and possible
divide-by-zero errors.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Keep the sequence-length scratch allocation alive through asynchronous
attention launches, populate it with correctly typed total lengths, and
seed non-aliased shared-cache outputs before in-place append.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The getTokenOffset kernel used each sequence_token_count element
directly as a loop bound while writing into token_offset, which holds
exactly batch_size * sequence_length entries. A value above
sequence_length, or a negative one in the padding loop, walked past the
end of that buffer and also produced an inconsistent total token count
used to size the output.

Clamp each per-row count to [0, sequence_length] in the kernel and
verify the sequence_token_count shape is (batch_size) before launching.
Adds tests for out-of-range and negative counts.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…osoft#31996)

The scratch allocation sizes in SparseAttention::ComputeInternal and the
Q/K/V and rotary offsets in QkvToContext were evaluated as products of
int shape fields and only widened afterwards, so a large batch_size or
sequence_length could wrap the product before it reached
GetScratchBuffer or the pointer arithmetic. Use SafeInt<size_t> so the
products are computed at full width and overflow throws instead of
wrapping.

The Triton kernel parameter structs take 32-bit strides, so also bound
the corresponding element counts in CheckInputs to keep those strides
representable.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

@hdharpure9922 hdharpure9922 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hdharpure9922
hdharpure9922 merged commit 3cdc696 into ovep-develop Aug 14, 2026
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants