forked from neo-ai/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[REFACTOR][PY] Establish tvm.arith (apache#4904)
- Loading branch information
Showing
14 changed files
with
322 additions
and
107 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 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. | ||
"""Integer bound analysis, simplification and pattern detection.""" | ||
|
||
from .int_set import IntSet, IntervalSet | ||
from .analyzer import ModularSet, ConstIntBound, Analyzer | ||
from .bound import deduce_bound | ||
from .pattern import detect_linear_equation, detect_clip_bound |
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,21 @@ | ||
# 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. | ||
"""FFI APIs for tvm.arith""" | ||
import tvm._ffi | ||
|
||
|
||
tvm._ffi._init_api("arith", __name__) |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 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. | ||
"""Bound deduction.""" | ||
from . import _ffi_api | ||
|
||
|
||
def deduce_bound(var, cond, hint_map, relax_map): | ||
"""Deduce the bound of the target variable in the cond. | ||
Parameters | ||
---------- | ||
var : Var | ||
The target variable to be deduced. | ||
cond : PrimExpr | ||
The condition | ||
hint_map : Map[Var, IntSet] | ||
Domain of variables used to help deduction. | ||
relax_map : Map[Var, IntSet] | ||
The fomain of the variables to be relaxed | ||
using the provided domain. | ||
""" | ||
return _ffi_api.DeduceBound(var, cond, hint_map, relax_map) |
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,80 @@ | ||
# 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. | ||
"""Integer set.""" | ||
import tvm._ffi | ||
from tvm.runtime import Object | ||
from . import _ffi_api | ||
|
||
|
||
class IntSet(Object): | ||
"""Represent a set of integer in one dimension.""" | ||
def is_nothing(self): | ||
"""Whether the set represent nothing""" | ||
return _ffi_api.IntSetIsNothing(self) | ||
|
||
def is_everything(self): | ||
"""Whether the set represent everything""" | ||
return _ffi_api.IntSetIsEverything(self) | ||
|
||
@staticmethod | ||
def vector(vec): | ||
"""Construct an integer set that covers the vector expr | ||
Parameters | ||
---------- | ||
vec : PrimExpr | ||
The vector expression. | ||
Returns | ||
------- | ||
rset : IntSet | ||
The result set. | ||
""" | ||
return _ffi_api.intset_vector(vec) | ||
|
||
@staticmethod | ||
def single_point(point): | ||
"""Construct a point set. | ||
Parameters | ||
---------- | ||
point : PrimExpr | ||
The vector expression. | ||
Returns | ||
------- | ||
rset : IntSet | ||
The result set. | ||
""" | ||
return _ffi_api.intset_single_point(point) | ||
|
||
|
||
@tvm._ffi.register_object("arith.IntervalSet") | ||
class IntervalSet(IntSet): | ||
"""Represent set of continuous interval [min_value, max_value] | ||
Parameters | ||
---------- | ||
min_value : PrimExpr | ||
The minimum value in the interval. | ||
max_value : PrimExpr | ||
The maximum value in the interval. | ||
""" | ||
def __init__(self, min_value, max_value): | ||
self.__init_handle_by_constructor__( | ||
_ffi_api.IntervalSet, min_value, max_value) |
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,60 @@ | ||
# 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. | ||
"""Detect common patterns.""" | ||
from . import _ffi_api | ||
|
||
|
||
def detect_linear_equation(expr, var_list): | ||
"""Match `expr = sum_{i=0}^{n-1} var[i] * coeff[i] + coeff[n]` | ||
Where coeff[i] and base are invariant of var[j] for all i and j. | ||
Parameters | ||
---------- | ||
expr : PrimExpr | ||
The expression to be matched. | ||
var_list : List[tvm.tir.Var] | ||
A list of variables. | ||
Returns | ||
------- | ||
coeff : List[PrimExpr] | ||
A list of co-efficients if the match is successful. | ||
An empty list if the match failed. | ||
""" | ||
return _ffi_api.DetectLinearEquation(expr, var_list) | ||
|
||
|
||
def detect_clip_bound(expr, var_list): | ||
""" Detect if expression corresponds to clip bound of the vars | ||
Parameters | ||
---------- | ||
expr : PrimExpr | ||
The expression to be matched. | ||
var_list : List[tvm.tir.Var] | ||
A list of variables. | ||
Returns | ||
------- | ||
coeff : List[PrimExpr] | ||
`concat([min_value[i], max_value[i]] for i, v in enumerate(var_list))` | ||
An empty list if the match failed. | ||
""" | ||
return _ffi_api.DetectClipBound(expr, var_list) |
Oops, something went wrong.