Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/relay/op/nn/nn.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ bool DenseRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
// validate the weight shape is proper if defined
// Assign weight type
Array<IndexExpr> wshape({param->units, dshape[dshape.size() - 1]});
reporter->Assign(types[1], TensorTypeNode::make(wshape, data->dtype));
// It is possible for weight to be nullptr in which case we will use
// data dtype as the weight dtype. However if weight dtype is explicitly
// present we will use that.
auto weight_dtype = (weight == nullptr ? data->dtype : weight->dtype);
reporter->Assign(types[1], TensorTypeNode::make(wshape, weight_dtype));
oshape.Set((oshape.size() - 1), param->units);
} else {
if (weight == nullptr) return false;
Expand Down
16 changes: 16 additions & 0 deletions tests/python/relay/test_op_level1.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,21 @@ def test_dense():
tvm.testing.assert_allclose(op_res2.asnumpy(), ref_res, rtol=1e-5)


def test_dense_dtype():
data_dtype = 'uint8'
weight_dtype = 'int8'
out_dtype = 'uint8'
n, c , h, w = tvm.var("n"), tvm.var("c"), tvm.var("h"), tvm.var("w")
x = relay.var("x", relay.TensorType((n, c, h, w), data_dtype))
w = relay.var("w", relay.TensorType((2, w), weight_dtype))
y = relay.nn.dense(x, w, units=2, out_dtype=out_dtype)
assert "units=2" in y.astext()
yy = run_infer_type(y)
assert yy.checked_type == relay.TensorType((n, c, h, 2), out_dtype)
assert run_infer_type(yy.args[0]).checked_type.dtype == 'uint8'
assert run_infer_type(yy.args[1]).checked_type.dtype == 'int8'


def test_bitserial_dense():
m, k = tvm.var("m"), tvm.var("k")
x = relay.var("x", relay.TensorType((m, k), "int16"))
Expand All @@ -405,3 +420,4 @@ def test_bitserial_dense():
test_batch_norm()
test_dense()
test_bitserial_dense()
test_dense_dtype()