﻿# CuPy RawKernel views: a transposed array does not automatically change pointer indexing

Pass CuPy array views to RawKernel with explicit stride-aware indexing or a deliberate contiguous representation, and validate types and output layout.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cupy-rawkernel-view-stride-contract/
Language: en

## Locate the abstraction that disappeared

A custom kernel works on a matrix but returns unexpected results when given its transpose. The [RawKernel reference](https://docs.cupy.dev/en/stable/reference/generated/cupy.RawKernel.html) warns that it does not automatically interpret array views or strides. The Python object still describes a transposed logical matrix, but pointer arithmetic in the CUDA function follows the indexing you wrote. Passing a different view does not rewrite that function's memory addressing.

## Choose a layout contract for the kernel

The [user-defined kernel guide](https://docs.cupy.dev/en/stable/user_guide/kernel.html) contrasts raw kernels with abstractions that handle array indexing. Decide whether the raw implementation supports arbitrary strides or accepts only a particular contiguous layout. For stride-aware code, pass the required shape and stride information and interpret its units consistently. For a restricted implementation, validate that restriction at the wrapper boundary before launch rather than relying on callers to remember an undocumented assumption.

## Make a contiguous representation deliberately

The [ascontiguousarray API](https://docs.cupy.dev/en/stable/reference/generated/cupy.ascontiguousarray.html) returns a C-contiguous array, copying when required. This can simplify a kernel's indexing contract, but it is an explicit representation choice with its own data movement and lifetime. Include that preparation in the application design. A benchmark of the kernel alone does not explain the cost of accepting a transposed or sliced input when every call first needs a compatible copy.

## Check arguments and output interpretation together

Review array dtypes against the CUDA parameter types and ensure the launch covers the intended element domain. Keep bounds handling inside the kernel's defined logic. Also define the output layout expected by the next component: contiguous storage does not by itself establish which logical axis is a feature or an example. A wrapper that corrects input layout but returns a misinterpreted output can hide a second error behind the first successful fix.

## Use views that expose incorrect assumptions

Test a base array, a transpose and a representative slice with distinguishable values. Compare against a high-level reference for the intended logical operation. The guides on [tensor memory descriptors](/journal/learn-tensor-memory-descriptor/) and [integer address arithmetic](/journal/guide-perf-integer-address/) explain related low-level contracts. Raw pointer access gives control over indexing, and correctness requires deliberately preserving the relationship between that indexing and the logical CuPy array presented by the caller.
