#include "graph/backend/dnnl/kernels/quantize.hpp"
#include "graph/backend/dnnl/patterns/fusions.hpp"
#include "graph/backend/dnnl/patterns/pattern_matcher_pass.hpp"
#include "graph/backend/dnnl/patterns/utils.hpp"
#include "graph/utils/pm/pbuilder.hpp"
namespace dnnl {
namespace impl {
namespace graph {
namespace dnnl_impl {
namespace pattern {
namespace pm = graph::utils::pm;
using in_edges_t = pm::in_edges_t;
using pb_graph_t = graph::utils::pm::pb_graph_t;
using FCreatePattern = graph::pass::FCreatePattern;
namespace {
bool check_inputs_all_bf16(op_t *op) {
for (size_t i = 0; i < op->num_inputs(); ++i) {
logical_tensor_t iport = op->get_input_logical_tensor(i);
VCHECK_PATTERN_UTILS(iport.data_type == graph::data_type::bf16, false,
"input data type for typecast-quantize fusion is not bf16");
}
return true;
}
}
DNNL_BACKEND_REGISTER_PATTERN_DEF_BEGIN(quantize_fusion)
DNNL_BACKEND_REGISTER_PATTERN_MATCHER_PASS(dnnl, typecast_quantize_fusion)
.set_priority(8.1f)
.set_kind(partition_kind_t::misc_quantized_post_ops)
.set_attr<FCreatePattern>("FCreatePattern",
[](const std::shared_ptr<pb_graph_t> &pgraph) -> void {
pm::pb_op_t *typecast
= pgraph->append_op(graph::op_kind::TypeCast);
typecast->append_decision_function(check_inputs_all_bf16);
pgraph->append_op(graph::op_kind::Quantize,
in_edges_t {in_edge(0, typecast, 0)});
})
.set_attr<FCreateKernel>("FCreateKernel", []() -> kernel_ptr {
return std::make_shared<quantize_dequantize_t>();
});
DNNL_BACKEND_REGISTER_PATTERN_DEF_END
} } } } }