forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Infra for tvm op runtime dispatch (apache#16100)
* infra for dispatch tvm op * fix ci and sanity error * disable shape with hint and fix coding style * rename to avoid conflict with original dot * update tvm and use soft link * config file moves to lib/ when using Makefile * add tvmop.conf to ci * fix rebase * fix rebase * use inspect to detect dispatchable func
- Loading branch information
1 parent
9d2d1a7
commit 0b1ec85
Showing
20 changed files
with
841 additions
and
25 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
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,57 @@ | ||
# 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. | ||
|
||
import time | ||
import mxnet as mx | ||
import numpy as _np | ||
from mxnet import np, npx | ||
|
||
def measure_cost(repeat, func_name, *args, **kwargs): | ||
"""Measure time cost of running a function | ||
""" | ||
mx.nd.waitall() | ||
start = time.time() | ||
for _ in range(repeat): | ||
func_name(*args, **kwargs) | ||
mx.nd.waitall() | ||
end = time.time() | ||
diff = end - start | ||
return diff / repeat | ||
|
||
|
||
def test_tvm_dot(): | ||
# benchmark | ||
for i in list(range(1000, 1100, 4)): | ||
m = i | ||
k = i | ||
n = i | ||
print("{} * {} X {} * {}".format(m, k, k, n)) | ||
a = mx.nd.random.uniform(shape=(m, k), dtype='float32') | ||
b = mx.nd.random.uniform(shape=(k, n), dtype='float32') | ||
cost = measure_cost(2, mx.nd.contrib.tvm_dot, a, b) | ||
print("dispatch cost: {} ms".format(cost * 1000)) | ||
a = mx.nd.random.uniform(shape=(m, k), dtype='float32') | ||
b = mx.nd.random.uniform(shape=(k, n), dtype='float32') | ||
cost = measure_cost(2, mx.nd.contrib.tvm_dot_fallback, a, b) | ||
print("fallback cost: {} ms".format(cost * 1000)) | ||
a = mx.nd.random.uniform(shape=(m, k), dtype='float32') | ||
b = mx.nd.random.uniform(shape=(k, n), dtype='float32') | ||
cost = measure_cost(2, mx.nd.dot, a, b) | ||
print("dot cost: {} ms".format(cost * 1000)) | ||
|
||
if __name__ == "__main__": | ||
test_tvm_dot() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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. | ||
|
||
# coding: utf-8 | ||
import tvm | ||
from tvm import autotvm | ||
from .. import defop, AllTypes | ||
from .. import assign_by_req, reduce_axes | ||
|
||
def compute_dot(A, B): | ||
M = A.shape[0] | ||
K = A.shape[1] | ||
N = B.shape[1] | ||
k = tvm.reduce_axis((0, K), 'k') | ||
C = tvm.compute((M, N), | ||
lambda x, y: tvm.sum(A[x, k] * B[k, y], axis=k), | ||
name='C') | ||
return C | ||
|
||
|
||
@defop(name="dot", target="cpu", dtype=AllTypes) | ||
def dot(dtype, fallback): | ||
cfg = autotvm.get_config() | ||
cfg.define_knob("bn", [64] if fallback else [64, 32]) | ||
cfg.define_knob("factor", [4] if fallback else [4]) | ||
M = tvm.var("M") | ||
K = tvm.var("K") | ||
N = tvm.var("N") | ||
A = tvm.placeholder((M, K), name='A', dtype=dtype) | ||
B = tvm.placeholder((K, N), name='B', dtype=dtype) | ||
C = compute_dot(A, B) | ||
s = tvm.create_schedule(C.op) | ||
# Blocking by loop tiling | ||
xo, yo, xi, yi = s[C].tile(C.op.axis[0], C.op.axis[1], cfg["bn"].val, cfg["bn"].val) | ||
k, = s[C].op.reduce_axis | ||
ko, ki = s[C].split(k, factor=cfg["factor"].val) | ||
# Hoist reduction domain outside the blocking loop | ||
s[C].reorder(xo, yo, ko, ki, xi, yi) | ||
return s, [A, B, C] |
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.