This document is relevant for: Trn2, Trn3

nki.isa.local_gather#

nki.isa.local_gather(dst: NkiTensor, src_buffer: NkiTensor, index: NkiTensor, num_elem_per_idx=1, num_valid_indices=None, name=None)[source]#

Gather SBUF data in src_buffer using index on GpSimd Engine.

Each of the eight GpSimd cores in GpSimd Engine connects to 16 contiguous SBUF partitions (e.g., core[0] connected to partition[0:16]) and performs gather from the connected 16 SBUF partitions independently in parallel. The indices used for gather on each core should also come from the same 16 connected SBUF partitions. If you only need to gather elements within a partition, consider using nisa.nc_n_gather instead, which supports gathering more indices.

During execution of the instruction, each GpSimd core reads a 16-partition slice from index, flattens all indices into a 1D array indices_1d (along the partition dimension first). Each core consumes the first num_valid_indices entries of indices_1d.

Next, each GpSimd core uses the flattened indices_1d indices as partition offsets to gather from the connected 16-partition slice of src_buffer. Optionally, this API also allows gathering of multiple contiguous elements starting at each index to improve gather throughput, as indicated by num_elem_per_idx. Behavior of out-of-bound index access is undefined.

In the constraints below, index_free_size and dst_free_size are the per-partition element counts of index and dst (the product of their free dimensions).

num_valid_indices must satisfy num_valid_indices <= index_free_size * 16, num_valid_indices * num_elem_per_idx == dst_free_size, and num_valid_indices <= 4096 (hardware limit). Exceeding 4096 raises an error. Violating either of the other two produces undefined results; it emits a FutureWarning in 0.8.0 and raises an error in 0.9.0.

Even though all eight GpSimd cores can gather with completely different indices, a common use case for this API is to make all cores gather with the same set of indices (i.e., partition offsets). In this case, users can generate indices into 16 partitions, replicate them eight times to 128 partitions and then feed them into local_gather.

As an example, if src_buffer is (128, 512) in shape and index is (128, 4) in shape, where the partition dimension size is 128, local_gather effectively performs the following operation:

num_gpsimd_cores = 8
num_partitions_per_core = 16

src_buffer = np.random.random_sample([128, 512, 4]).astype(np.float32) * 100
index_per_core = np.random.randint(low=0, high=512, size=(16, 4), dtype=np.uint16)
# replicate 8 times for 8 GpSimd cores
index = np.tile(index_per_core, (num_gpsimd_cores, 1))
num_elem_per_idx = 4
index_hw = index * num_elem_per_idx
num_valid_indices = 64
output_shape = (128, 4, 16, 4)

output_np = np.ndarray(shape=(128, num_valid_indices, num_elem_per_idx),
                       dtype=src_buffer.dtype)

for i_core in range(num_gpsimd_cores):
  start_par = i_core * num_partitions_per_core
  end_par = (i_core + 1) * num_partitions_per_core
  indices_1d = index[start_par:end_par].flatten(order='F')[0: num_valid_indices]

  output_np[start_par:end_par, :, :] = np.take(
    src_buffer[start_par:end_par],
    indices_1d, axis=1)

output_np = output_np.reshape(output_shape)

local_gather preserves the input data types from src_buffer in the gather output. Therefore, no data type casting is allowed in this API. The indices in index tile must be uint16 types.

This API has three tile size constraints [subject to future relaxation]:

  1. The partition axis size of src_buffer must match that of index and must be a multiple of 16. In other words, src_buffer.shape[0] == index.shape[0] and src_buffer.shape[0] % 16 == 0.

  2. The number of contiguous elements to gather per index per partition num_elem_per_idx must be one of the following values: [1, 2, 4, 8, 16, 32].

  3. The number of indices for gather per core must be less than or equal to 4096.

  4. src_buffer and dst can have at most four free dimensions (maximum rank 5, including the partition dimension).

Deprecated since version 0.8.0: Omitting num_valid_indices yields undefined behavior and emits a FutureWarning. In 0.9.0 the default becomes dst_free_size / num_elem_per_idx. Pass num_valid_indices explicitly to get well-defined behavior and silence the warning.

Parameters:
  • dst – an output tile of the gathered data

  • src_buffer – an input tile for gathering.

  • index – an input tile with indices used for gathering.

  • num_elem_per_idx – an optional integer value to read multiple contiguous elements per index per partition; default is 1.

  • num_valid_indices – an optional integer value to specify the number of valid indices per GpSimd core. Must equal dst_free_size / num_elem_per_idx and must not exceed index_free_size * 16 or 4096 (see the constraints above). When omitted, a FutureWarning is emitted and the result is undefined; the default becomes dst_free_size / num_elem_per_idx in 0.9.0 (see the deprecation note above).

Click here to download the full NKI code example with equivalent numpy implementation.

This document is relevant for: Trn2, Trn3