﻿# CuPy indexed writes: validate bounds and repeated destinations before porting NumPy code

Port integer-indexed NumPy updates to CuPy with explicit bounds and duplicate-destination semantics instead of assuming CPU assignment rules carry over.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-cupy-index-wrap-duplicate-stores/
Language: en

## Identify the intended update operation

A feature pipeline scatters event weights into per-item buckets. Several events can belong to the same item, so the algorithm needs accumulation rather than replacement. Before moving the calculation to GPU, describe what repeated destinations mean. The [CuPy differences guide](https://docs.cupy.dev/en/stable/user_guide/difference.html) documents that repeated destinations in integer-array assignment do not have NumPy's last-value behavior. A result looking plausible on a sample with unique indices does not exercise this boundary.

## Validate the index domain explicitly

The same guide explains that out-of-bounds integer-array indices wrap in CuPy instead of necessarily producing the NumPy error a caller expects. A corrupted item identifier can therefore update a valid but unintended location. Establish valid bounds from the indexed axis and the application's policy for negative indices. Validate that contract near the point where identifiers become offsets. A broad exception handler around the GPU operation is not a substitute for checking the intended domain.

## Choose accumulation when every contribution matters

The [scatter_add reference](https://docs.cupy.dev/en/stable/reference/generated/cupyx.scatter_add.html) describes including contributions from repeated indices. Ordinary indexed read-modify-write does not provide that same behavior. Select an operation whose documented semantics match the algorithm, and verify supported data types for the installed version. For floating-point accumulation, also distinguish inclusion of every contribution from a promise of identical reduction order or exact bitwise agreement across execution strategies.

## Make the selected axis unambiguous

The [take API](https://docs.cupy.dev/en/stable/reference/generated/cupy.take.html) documents explicit-axis extraction and flattening when no axis is selected. Carry this awareness into an indexed pipeline: an offset into flattened storage is different from a row identifier. Keep the table identity, axis and index mapping together when reshaping features. Otherwise even in-range indices can refer to the wrong logical examples, and bounds validation alone will not expose the error.

## Test adversarial identities alongside normal data

A useful migration pilot includes repeated destinations, boundary indices and distinct values that reveal accidental replacement. Compare against an explicitly defined CPU reference for the intended operation. The guides on [parallel reduction order](/journal/learn-parallel-reduction-tree/) and [array-of-structures layouts](/journal/learn-aos-soa-kernel-layout/) provide related background. Correct GPU updates require both a valid memory destination and the intended meaning of multiple contributions to that destination.
