Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPIRV] Support Bool buffer argument #7591

Merged
merged 4 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/target/spirv/codegen_spirv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ std::vector<uint32_t> CodeGenSPIRV::BuildFunction(const PrimFunc& f, const std::
auto* prim = ptr->element_type.as<PrimTypeNode>();
ICHECK(prim);
DataType value_type = prim->dtype;
if (value_type == DataType::UInt(1)) {
masahi marked this conversation as resolved.
Show resolved Hide resolved
value_type = DataType::UInt(8);
}
spirv::Value arg_value =
builder_->BufferArgument(builder_->GetSType(value_type), 0, num_buffer);
storage_info_[arg.get()].UpdateContentType(value_type);
Expand Down Expand Up @@ -369,11 +372,17 @@ spirv::Value CodeGenSPIRV::VisitExpr_(const LoadNode* op) {
mask |= spv::MemoryAccessVolatileMask;
}
if (op->dtype.lanes() == 1) {
ICHECK_EQ(info.content_type, op->dtype)
<< "Vulkan only allow one type access to the same buffer";
spirv::Value index = MakeValue(op->index);
spirv::Value ptr = builder_->StructArrayAccess(ptr_type, buffer, index);
return builder_->MakeValue(spv::OpLoad, content_type, ptr, mask);
spirv::Value loaded = builder_->MakeValue(spv::OpLoad, content_type, ptr, mask);
if (op->dtype == DataType::UInt(1)) {
auto bool_ty = builder_->GetSType(DataType::UInt(1));
return builder_->Cast(bool_ty, loaded);
} else {
ICHECK_EQ(info.content_type, op->dtype)
<< "Vulkan only allow one type access to the same buffer";
return loaded;
}
} else {
if (op->dtype.element_of() == info.content_type) {
// because content type is element type, we can only do scalarize load.
Expand Down
78 changes: 78 additions & 0 deletions tests/python/unittest/test_target_codegen_spirv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# 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 tvm
import tvm.testing
from tvm import te
from tvm.topi.math import cast
import numpy as np


tx = te.thread_axis("threadIdx.x")
ty = te.thread_axis("threadIdx.y")
masahi marked this conversation as resolved.
Show resolved Hide resolved
bx = te.thread_axis("blockIdx.x")
by = te.thread_axis("blockIdx.y")


def test_bool_load():
def do_copy(A, B, n):
ib = tvm.tir.ir_builder.create()
A = ib.buffer_ptr(A)
B = ib.buffer_ptr(B)

max_threads = 32
ib.scope_attr(bx, "thread_extent", tvm.tir.indexdiv(n + max_threads - 1, max_threads))
ib.scope_attr(tx, "thread_extent", max_threads)
tid = bx * max_threads + tx

with ib.if_scope(tid < n):
B[tid] = cast(A[tid], "int32")

return ib.get()

n = 1024
A = te.placeholder((n,), name="A", dtype="bool")
B = te.placeholder((n,), name="B", dtype="int32")

target = "vulkan"

if not tvm.testing.device_enabled(target):
return

B = te.extern(
A.shape,
[A],
lambda ins, outs: do_copy(ins[0], outs[0], n),
name="bool_copy_ir",
dtype="int32",
)
s = te.create_schedule(B.op)

with tvm.transform.PassContext(opt_level=3):
func = tvm.build(s, [A, B], target)

ctx = tvm.context(target, 0)
a_np = np.random.uniform(size=n) > 0.5
b_np = np.zeros((n,), dtype="int32")
a = tvm.nd.array(a_np, ctx)
b = tvm.nd.array(b_np, ctx)
func(a, b)
ref = a_np.astype(np.int32)
tvm.testing.assert_allclose(b.asnumpy(), ref)


if __name__ == "__main__":
test_bool_load()