Skip to content

Commit

Permalink
[FIX] Fix autoscheduler tuning on sparse matrices where there are mul…
Browse files Browse the repository at this point in the history
…tiple with the same shape (apache#7974)

* [FIX] Fix autoscheduler tuning on sparse matrices where there are multiple with the same shape

* formatting

* remove unreachable code
  • Loading branch information
tkonolige authored and Trevor Morris committed May 6, 2021
1 parent 342c613 commit f9328e4
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion python/tvm/auto_scheduler/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ def _timed_eval_func(
random_fill(empty_array)
args.append(empty_array)
if task_inputs_count != len(task_input_names):
logger.warning(
raise RuntimeError(
"task_inputs not fully matched, check if there's any unexpected error"
)
dev.sync()
Expand Down
11 changes: 4 additions & 7 deletions python/tvm/auto_scheduler/search_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,11 @@ def register_task_input_buffer(
tensor_from_file = _try_load_buffer_from_file(input_name)
if tensor_from_file:
input_table[input_name] = tensor_from_file

if input_name in input_table.keys():
logger.warning(
"Tensor %s exists in TASK_INPUT_BUFFER_TABLE, %s",
input_name,
"set overwrite to True or this Tensor will not be registered",
elif input_name in input_table.keys():
raise RuntimeError(
"Tensor %s exists in TASK_INPUT_BUFFER_TABLE, %s"
% (input_name, "set overwrite to True or this Tensor will not be registered")
)
return input_table[input_name]

input_table[input_name] = input_data
if save_to_file:
Expand Down
20 changes: 15 additions & 5 deletions python/tvm/relay/analysis/sparse_dense.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,31 @@ def process_params(expr, params, block_size, sparsity_threshold):
params[name + ".indices"] = tvm.nd.array(sparse_weight.indices)
params[name + ".indptr"] = tvm.nd.array(sparse_weight.indptr)

prefix = "sparse_dense_bsr_%d_%d_%d_%d_%.2f_" % (
prefix = "sparse_dense_bsr_%d_%d_%d_%d_%d_%d_" % (
w_np.shape[0],
w_np.shape[1],
block_size[0],
block_size[1],
1 - sparsity,
sparse_weight.indices.shape[0],
sparse_weight.indptr.shape[0],
)
register_task_input_buffer(
"default", prefix + "W_data", tvm.runtime.ndarray.array(sparse_weight.data)
"default",
prefix + "W_data",
tvm.runtime.ndarray.array(sparse_weight.data),
overwrite=True,
)
register_task_input_buffer(
"default", prefix + "W_indices", tvm.runtime.ndarray.array(sparse_weight.indices)
"default",
prefix + "W_indices",
tvm.runtime.ndarray.array(sparse_weight.indices),
overwrite=True,
)
register_task_input_buffer(
"default", prefix + "W_indptr", tvm.runtime.ndarray.array(sparse_weight.indptr)
"default",
prefix + "W_indptr",
tvm.runtime.ndarray.array(sparse_weight.indptr),
overwrite=True,
)
ret = SparseAnalysisResult(
weight_name=tvm.runtime.convert(memo.weight_name),
Expand Down
10 changes: 9 additions & 1 deletion python/tvm/topi/nn/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,15 @@ def _process_inputs(input_tensors, m, n, prefix_init):
density *= i
density /= k * n
density = density.value
sparse_prefix = "%s_%d_%d_%d_%d_%.2f_" % (prefix_init, n, k, bs_r, bs_c, density)
sparse_prefix = "%s_%d_%d_%d_%d_%d_%d_" % (
prefix_init,
n,
k,
bs_r,
bs_c,
sparse_indices.shape[0],
sparse_indptr.shape[0],
)

visited = set()

Expand Down
1 change: 0 additions & 1 deletion src/relay/transforms/memory_alloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ inline Expr AllocTensor(const Expr& storage, tvm::relay::Expr shape, DataType dt
return AllocTensor(storage, offset, shape, dtype, assert_shape);
}


// Check if the primitive function contains only reshape ops.
bool IsReshapeOnly(const Expr& expr) {
if (auto* func = expr.as<FunctionNode>()) {
Expand Down
10 changes: 9 additions & 1 deletion tutorials/auto_scheduler/tune_sparse_x86.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import numpy as np
import tvm
import tvm.testing
from tvm import te, auto_scheduler, runtime, topi
from tvm.auto_scheduler import _ffi_api
from tvm.topi.utils import get_const_tuple
Expand Down Expand Up @@ -108,7 +109,14 @@ def sparse_dense(M, N, K, w_data_shape, w_indices_shape, w_indptr_shape, dtype):
target = tvm.target.Target("llvm")

# Register the sparse data to task inputs
prefix = "sparse_dense_bsr_%d_%d_%d_%d_%.2f_" % (N, K, BS_R, BS_C, density)
prefix = "sparse_dense_bsr_%d_%d_%d_%d_%d_%d_" % (
N,
K,
BS_R,
BS_C,
W_sp_np.indices.shape[0],
W_sp_np.indptr.shape[0],
)
task = tvm.auto_scheduler.SearchTask(
func=sparse_dense,
args=(M, N, K, W_sp_np.data.shape, W_sp_np.indices.shape, W_sp_np.indptr.shape, "float32"),
Expand Down

0 comments on commit f9328e4

Please sign in to comment.