|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Indexing operators.""" |
| 18 | +from typing import List, Optional, Union |
| 19 | + |
| 20 | +from tvm.ir.expr import PrimExpr |
| 21 | + |
| 22 | +from . import _ffi_api |
| 23 | +from ..expr import Expr |
| 24 | + |
| 25 | +PrimExprLike = Union[int, PrimExpr] |
| 26 | + |
| 27 | + |
| 28 | +def take(x: Expr, indices: Expr, axis: Optional[int] = None) -> Expr: |
| 29 | + """Take elements from a tensor along an axis. |
| 30 | +
|
| 31 | + Parameters |
| 32 | + ---------- |
| 33 | + x : relax.Expr |
| 34 | + The source tensor. |
| 35 | +
|
| 36 | + indices : relax.Expr |
| 37 | + The indices of the values to extract. |
| 38 | + It is required to be a one-dimensional tensor which has integer dtype. |
| 39 | +
|
| 40 | + axis : Optional[int] |
| 41 | + The axis over which to select values. |
| 42 | + If it is none, the input tensor is required to be one-dimensional. |
| 43 | +
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + ret : relax.Expr |
| 47 | + The taken result. |
| 48 | + """ |
| 49 | + return _ffi_api.take(x, indices, axis) # type: ignore |
| 50 | + |
| 51 | + |
| 52 | +def strided_slice( |
| 53 | + x: Expr, |
| 54 | + axes: List[int], |
| 55 | + begin: List[PrimExprLike], |
| 56 | + end: List[PrimExprLike], |
| 57 | + strides: Optional[List[PrimExprLike]] = None, |
| 58 | +) -> Expr: |
| 59 | + """Strided slice of a tensor. |
| 60 | +
|
| 61 | + Parameters |
| 62 | + ---------- |
| 63 | + x : relax.Expr |
| 64 | + The source tensor to be sliced. |
| 65 | +
|
| 66 | + axes : List[int] |
| 67 | + Axes along which slicing is applied. |
| 68 | +
|
| 69 | + begin : List[PrimExprLike] |
| 70 | + The indices to begin with in the slicing, inclusive. |
| 71 | +
|
| 72 | + end : List[PrimExprLike] |
| 73 | + The indices indicating end of the slice, exclusive. |
| 74 | +
|
| 75 | + strides : Optional[List[PrimExprLike]] |
| 76 | + Specifies the stride values, it can be negative in that case, |
| 77 | + the input tensor will be reversed in that particular axis. |
| 78 | + If not specified, it by default is an list of ones of the same length as `axes`. |
| 79 | +
|
| 80 | + Returns |
| 81 | + ------- |
| 82 | + ret : relax.Expr |
| 83 | + The sliced result. |
| 84 | +
|
| 85 | + Note |
| 86 | + ---- |
| 87 | + strided_slice require the input `begin`, `end` and `strides` to have the |
| 88 | + same length as `axes`. |
| 89 | + """ |
| 90 | + return _ffi_api.strided_slice(x, axes, begin, end, strides) # type: ignore |
0 commit comments