﻿# hipCUB DeviceSelect: output capacity is not the selected result length

Consume hipCUB selection through its device-produced count and valid output prefix, preserving flag semantics and completion before reading results on the host.

Author: Retrivium editorial team
Updated: 2026-11-04
Canonical URL: https://retrivium.ai/journal/fg2-hipcub-selected-count-valid-output-prefix/
Language: en

## Separate capacity from logical result size

A token-filtering extension reserves enough output storage for every candidate and then treats the entire array as selected. The [versioned DeviceSelect interface](https://rocm.docs.amd.com/projects/hipCUB/en/docs-5.3.3/.doxygen/docBin/html/classhipcub_1_1DeviceSelect.html) exposes a separate selected-count output. Use it to define the logical result length. Unwritten tail capacity is not a list of rejected elements, guaranteed zeros or valid results. The buffer may retain values from an earlier request without violating the selection contract.

## Follow the installed backend's contract

The [current hipCUB documentation](https://rocm.docs.amd.com/projects/hipCUB/en/latest/index.html) identifies the project and its wrapper relationship to rocPRIM. The older interface reference is a historical signature example, not a claim that every current overload is identical. Match the installed headers and backend before using an overload. Preserve the operation variant, count type and output location so a framework binding does not mistake a device-written count for an immediately available host scalar.

## Interpret flags and counts explicitly

The [rocPRIM select reference](https://rocm.docs.amd.com/projects/rocPRIM/en/latest/device_ops/select.html) describes selection flags and the selected-count output for the underlying operation. A flag is a selection decision under that API, not automatically a calibrated probability or class label. If scores need thresholding, define that conversion before selection. Keep output capacity sufficient for the permitted maximum selection while using the actual count for downstream work. These responsibilities solve different problems and should not be merged into one allocation-size variable.

## Protect the count's generation

A count buffer belongs to the same invocation as its corresponding output prefix. Reading a previous count with a newly produced array can silently select the wrong length. Establish stream ordering for device consumers and a valid completed transfer before host code inspects a device-produced count. Preserve count and data ownership together when recycling buffers. Merely checking the host return status does not make an asynchronous device count immediately ready for an ordinary CPU dereference.

## Exercise sparse and empty selections

Test no selected items, a subset and all permitted items, checking both count and valid prefix against a simple reference. Do not require unspecified tail values to satisfy invented expectations. Related guides explain [stream ordering](/journal/learn-stream-ordering-dependencies/) and [software reproducibility](/journal/software-stack-reproducibility/). The acceptance result is correct logical output consumption on the tested interface, not a guarantee that arbitrary aliasing, every iterator type or every backend permits the same in-place behavior.
