-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit implements `relax.op.view` (`R.view` in TVMScript) to produce a view into an existing array. This returned view shares the same backing allocation as the existing array. Because `R.view` comes with potential trade-offs; such as increased memory footprint, performance cost to apply a non-zero `DLTensor::byte_offset`, and potential misalignment for vector operators; this PR does not use `R.view` apart from unit tests. Applications of `R.view`, either for specific compute kernels or in optimization passes, is instead kept for follow-up PRs.
- Loading branch information
1 parent
c0385c7
commit ed4fd50
Showing
10 changed files
with
1,287 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,7 @@ | |
tan, | ||
tanh, | ||
) | ||
from .view import view | ||
|
||
|
||
def _register_op_make(): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""Operations that act on the DLTensor container """ | ||
from typing import Optional, Sequence, Union | ||
|
||
from tvm.ir.expr import PrimExpr | ||
|
||
from . import _ffi_api | ||
from ..expr import Expr, PrimValue, ShapeExpr, DataTypeImm | ||
|
||
PrimExprLike = Union[int, PrimExpr] | ||
|
||
|
||
def view( | ||
data: Expr, | ||
shape: Optional[Union[Sequence[PrimExprLike], Expr]] = None, | ||
dtype: Optional[Expr] = None, | ||
relative_byte_offset: Optional[Expr] = None, | ||
) -> Expr: | ||
"""Broadcasts a tensor to a specified shape. | ||
Parameters | ||
---------- | ||
data : relax.Expr | ||
The input data to the operator. | ||
shape : Optional[Union[Sequence[PrimExprLike], Expr]] | ||
The target shape. Should be a `relax.ShapeExpr`, or a | ||
collection that can be converted to a `relax.ShapeExpr`. | ||
dtype : Optional[Expr] | ||
The target datatype. Should be a `relax.ShapeExpr`, or a | ||
collection that can be converted to a `relax.ShapeExpr`. | ||
relative_byte_offset: Optional[Expr] | ||
The offset of the output NDArray, relative to the byte offset | ||
of `data`. If `None`, the offset of the view is the same as | ||
the offset of `data`. | ||
Returns | ||
------- | ||
result : relax.Expr | ||
The tensor view | ||
""" | ||
|
||
def _normalize(expr, relax_cls): | ||
if expr is None or isinstance(expr, Expr): | ||
return expr | ||
else: | ||
return relax_cls(expr) | ||
|
||
shape = _normalize(shape, ShapeExpr) | ||
dtype = _normalize(dtype, DataTypeImm) | ||
relative_byte_offset = _normalize(relative_byte_offset, PrimValue) | ||
|
||
return _ffi_api.view(data, shape, dtype, relative_byte_offset) # type: ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.