MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / xla_gather

Function xla_gather

imperative/python/megengine/xla/rules/tensor.py:345–515  ·  view source on GitHub ↗

This is xla Gather op wrapper. The biggest difference between xla Gather and mge Gather is that xla Gather gather blocks each time from source. We will use some examples to explain how it works. For more information, you can read the hlo doc: https://github.com/openxla/stableh

(
    src: HLOTensor,
    index: HLOTensor,
    slice_sizes: Sequence[int],
    offset_dims: Sequence[int],
    collapsed_slice_dims: Sequence[int],
    start_index_map: Sequence[int],
    indices_are_sorted: bool = False,
)

Source from the content-addressed store, hash-verified

343
344
345def xla_gather(
346 src: HLOTensor,
347 index: HLOTensor,
348 slice_sizes: Sequence[int],
349 offset_dims: Sequence[int],
350 collapsed_slice_dims: Sequence[int],
351 start_index_map: Sequence[int],
352 indices_are_sorted: bool = False,
353):
354 """
355 This is xla Gather op wrapper. The biggest difference between xla Gather and mge
356 Gather is that xla Gather gather blocks each time from source.
357
358 We will use some examples to explain how it works. For more information, you can
359 read the hlo doc: https://github.com/openxla/stablehlo/blob/main/docs/spec.md.
360
361 ## Case1: What does `src`, `index`, `slice_sizes` mean?
362 We have `src` Tensor with shape (4, 5) as below:
363 src = [[ 0 1 2 3 4]
364 [ 5 6 7 8 9]
365 [10 11 12 13 14]
366 [15 16 17 18 19]]
367 We have `index` Tensor with shape (2,) as below:
368 index = [1, 2]
369 We set the `slice_sizes` in arglist as (2, 3):
370 That means we want to gather a block with shape (2, 3) at `src[1, 2]`, it returns:
371 out = [[ 7 8 9]
372 [12 13 14]]
373 The `out.shape` is (2, 3). The last two dimensions of `out` are same as
374 `slice_sizes`.
375
376 The last dimension of the `index` will be interpreted as coordinate to indicate
377 where we start to gather block. The length of the last dimension of `index` is 2
378 because the `src` is 2-d tensor. In this case, we start gather at
379 `src[index[0], index[1]]`.
380
381 Here the `slice_sizes` determine the size of the block to gather. Because the `src`
382 is 2-d tensor, the length of `slice_sizes` is also 2. Each element of `slice_sizes`
383 corresponds to a dimension of `src`. In this case, we gather block of size (2, 3).
384
385 Factually, the gather operation above is equal to:
386 out = src[index[0]:index[0]+slice_sizes[0], index[1]:index[1]+slice_sizes[1]].
387
388 ## Case2: How to do batch gather?
389 The source Tensor is the same as above with shape (4, 5).
390 The index Tensor with shape (3, 2) as below:
391 index = [[0 1]
392 [2 1]
393 [1 2]]
394 The `slice_sizes` is also set as (2, 3).
395 That means we want to gather 3 block with shape (2, 3) from `src[0, 1]`,
396 `src[2, 1]`, `src[1, 2]`it returns:
397 out = [[[ 1 2 3]
398 [ 6 7 8]]
399 [[11 12 13]
400 [16 17 18]]
401 [[ 7 8 9]
402 [12 13 14]]].

Callers 3

resize_nearestFunction · 0.85
resize_linearFunction · 0.85
vec_indexing_lowerFunction · 0.85

Calls 3

listFunction · 0.85
HLOTensorClass · 0.85
getMethod · 0.45

Tested by

no test coverage detected