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
12 changes: 8 additions & 4 deletions src/relay/transforms/to_mixed_precision.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ class MixedPrecisionPass : public MixedModeMutator {
*/
DataType cur_type = (attrs->out_dtype);
ObjectPtr<T> new_attrs = make_object<T>(*attrs);
if (cur_type.is_float() || cur_type.is_void()) new_attrs->out_dtype = accumulation_dtype;
if (cur_type.is_float() || cur_type.is_bfloat16() || cur_type.is_void()) {
new_attrs->out_dtype = accumulation_dtype;
}
return Attrs(new_attrs);
}

Expand All @@ -175,7 +177,9 @@ class MixedPrecisionPass : public MixedModeMutator {
*/
DataType cur_type = (attrs->dtype);
ObjectPtr<T> new_attrs = make_object<T>(*attrs);
if (cur_type.is_float() || cur_type.is_void()) new_attrs->dtype = accumulation_dtype;
if (cur_type.is_float() || cur_type.is_bfloat16() || cur_type.is_void()) {
new_attrs->dtype = accumulation_dtype;
}
return Attrs(new_attrs);
}

Expand Down Expand Up @@ -217,7 +221,7 @@ class MixedPrecisionPass : public MixedModeMutator {
/* Cast tensor to the wanted datatype, returning a cached version if it's already been done. */

// If this is not a floating point type, do not cast. E.g. it might be an integer
if (!expr_dtype.is_float()) {
if (!(expr_dtype.is_float() || expr_dtype.is_bfloat16())) {
return expr;
}

Expand Down Expand Up @@ -299,7 +303,7 @@ class MixedPrecisionPass : public MixedModeMutator {
original_dtype_.push_back((root_->checked_type_).as<TensorTypeNode>()->dtype);
}
}
if (!mixed_precision_type_.is_float() && !mixed_precision_type_.is_bfloat16()) {
if (!(mixed_precision_type_.is_float() || mixed_precision_type_.is_bfloat16())) {
LOG(FATAL) << "Only support IEEE floating point mixed precision types and bfloat16, but got "
<< mixed_precision_type_;
}
Expand Down
5 changes: 2 additions & 3 deletions tests/python/contrib/test_dnnl.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,8 @@ def assert_result_dict_holds(result_dict):
res1 = vmobj_to_list(result_dict[k1])
res2 = vmobj_to_list(result_dict[k2])
for r1, r2 in zip(res1, res2):
if "bf16" in k1 or "bf16" in k2:
np.testing.assert_array_almost_equal(r1, r2, decimal=1)
else:
# ignore the accuracy checking if only one bf16 result presents
if ("bf16" in k1) == ("bf16" in k2):
tvm.testing.assert_allclose(r1, r2, rtol=1e-3, atol=1e-3)


Expand Down
Loading