Implementation:Microsoft Onnxruntime CPU SliceGrad
| Knowledge Sources | |
|---|---|
| Domains | Training, CPU_Kernels |
| Last Updated | 2026-02-10 04:00 GMT |
Overview
Concrete tool for computing Slice gradient on CPU in the ONNX Runtime training framework.
Description
This file implements the SliceGrad kernel, which computes the gradient of the ONNX Slice operation. It creates a zero-initialized output tensor of the original data shape and copies the upstream gradient values into the appropriate positions using a WritableSliceIterator. The kernel parses the starts, ends, axes, and steps inputs to determine the mapping, and uses PrepareForCompute from the base Slice implementation to compute the flattened dimension metadata. The iterator-based copy supports both contiguous (solitary inner step) and strided (non-solitary inner step) access patterns. Supports float and double types.
Usage
This kernel is invoked during the backward pass when a Slice operation was used in the forward pass. The gradient is scattered from the sliced positions back to the full input shape.
Code Reference
Source Location
- Repository: Microsoft_Onnxruntime
- File: orttraining/orttraining/training_ops/cpu/tensor/slice_grad.cc
- Lines: 1-103
Signature
Status SliceGrad::Compute(OpKernelContext* context) const;
template <typename T>
Status SliceGrad::ComputeImpl(OpKernelContext* ctx,
Tensor& output_grad_tensor,
const gsl::span<const int64_t>& output_dims,
TensorShapeVector* p_flattened_input_dims,
TensorShapeVector* p_flattened_output_dims,
const gsl::span<const int64_t>& starts,
const gsl::span<const int64_t>& steps) const;
Import
#include "orttraining/orttraining/training_ops/cpu/tensor/slice_grad.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| grad | Tensor(T) | Yes | Upstream gradient (same shape as Slice output) |
| shape | Tensor(int64) | Yes | Shape of the original data tensor |
| starts | Tensor(int64/int32) | Yes | Slice start positions |
| ends | Tensor(int64/int32) | Yes | Slice end positions |
| axes | Tensor(int64/int32) | No | Axes along which to slice |
| steps | Tensor(int64/int32) | No | Step sizes for slicing |
Outputs
| Name | Type | Description |
|---|---|---|
| output | Tensor(T) | Gradient w.r.t. original data (zero-filled except at sliced positions) |
Usage Examples
ONNX_OPERATOR_KERNEL_EX(
SliceGrad, kMSDomain, 1, kCpuExecutionProvider,
KernelDefBuilder()
.TypeConstraint("I", DataTypeImpl::GetTensorType<int64_t>())
.TypeConstraint("T", DataTypeImpl::AllTensorTypes())
.TypeConstraint("Tind", std::vector<MLDataType>{
DataTypeImpl::GetTensorType<int32_t>(),
DataTypeImpl::GetTensorType<int64_t>()}),
SliceGrad);