Skip to content

graph: backend: dnnl: backend refactor and sdpa v1 kernel support quantize SDPA #3423

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
52 changes: 18 additions & 34 deletions src/graph/backend/dnnl/dnnl_op_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_convolution, 1,
// Attributes inherited from Convolution.
.SET_CONV_COMMON_ATTRS
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::with_bias, false, attribute_kind::b, false)
.set_attr(
op_attr::canonicalized, false, attribute_kind::b, false)
Expand All @@ -340,8 +339,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_convtranspose, 1,
std::vector<int64_t>(DNNL_MAX_NDIMS, 0))
.SET_DNNL_CONVTRANSPOSE_COMMON_ATTRS
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::with_bias, false, attribute_kind::b, false)
.set_attr(
op_attr::canonicalized, false, attribute_kind::b, false)
Expand Down Expand Up @@ -427,8 +425,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_pool, 1,
.set_attr(op_attr::auto_pad, false, attribute_kind::s, "None",
{"None", "SAME_UPPER", "SAME_LOWER", "VALID"})
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::kind, true, attribute_kind::s)
.set_attr(
op_attr::canonicalized, false, attribute_kind::b, false)
Expand Down Expand Up @@ -623,8 +620,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_batchnorm, 1,
.set_attr(op_attr::data_format, false, attribute_kind::s, "NXC",
{"NXC", "NCX"})
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::is_training, false, attribute_kind::b)
.set_attr(op_attr::fuse_relu, false, attribute_kind::b)
.set_attr(
Expand Down Expand Up @@ -653,8 +649,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_batchnorm_bwd, 1,
.set_output(2, "beta_delta")
.set_output(3, "scratchpad")
.set_attr(op_attr::epsilon, true, attribute_kind::f)
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::data_format, false, attribute_kind::s, "NXC",
{"NXC", "NCX"})
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
Expand Down Expand Up @@ -684,8 +679,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_resampling_bwd, 1,
.set_attr(op_attr::scales, false, attribute_kind::fs)
.set_attr(op_attr::data_format, false, attribute_kind::s, "NXC",
{"NXC", "NCX"})
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
.set_shape_inference_function(infer_identity_output_shape)
.SET_LAYOUT_PROPAGATOR(layout_propagator_for_resampling_bwd)
Expand Down Expand Up @@ -728,8 +722,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_binary, 1,
{"NXC", "NCX"})
// New added attributes
.set_attr(op_attr::is_bias_add, false, attribute_kind::b, false)
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::alg_kind, true, attribute_kind::i)
.set_attr(
op_attr::canonicalized, false, attribute_kind::b, false)
Expand All @@ -754,8 +747,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_eltwise, 1,
.set_attr(op_attr::alpha, false, attribute_kind::f, 0.f)
.set_attr(op_attr::beta, false, attribute_kind::f, 0.f)
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::alg_kind, true, attribute_kind::i)
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
// Analysis rules
Expand All @@ -777,8 +769,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_eltwise_bwd, 1,
.set_attr(op_attr::beta, false, attribute_kind::f, 0.f)
.set_attr(op_attr::use_dst, false, attribute_kind::b, false)
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::alg_kind, true, attribute_kind::i)
.set_attr(op_attr::fwd_alg_kind, true, attribute_kind::i)
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
Expand Down Expand Up @@ -836,8 +827,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_reduction, 1,
// Attributes inherited from front reduction ops
.SET_REDUCE_COMMON_ATTRS
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::alg_kind, true, attribute_kind::i)
.set_attr(op_attr::p, false, attribute_kind::f, 0.0f)
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
Expand Down Expand Up @@ -906,8 +896,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_resampling, 1,
.set_attr(op_attr::data_format, false, attribute_kind::s, "NXC",
{"NXC", "NCX"})
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(
op_attr::canonicalized, false, attribute_kind::b, false)
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
Expand Down Expand Up @@ -956,8 +945,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_layernorm_bwd, 1,
.set_attr(op_attr::begin_norm_axis, false, attribute_kind::i,
int64_t(-1))
.set_attr(op_attr::epsilon, false, attribute_kind::f, 1e-5f)
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
.set_shape_inference_function(infer_norm_bprop_output_shape)
.SET_LAYOUT_PROPAGATOR(layout_propagator_for_layernorm_bwd)
Expand All @@ -978,8 +966,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_matmul, 1,
// Attributes inherited from MatMul.
.SET_MATMUL_COMMON_ATTRS
// New added attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::with_bias, false, attribute_kind::b, false)
.set_attr(
op_attr::canonicalized, false, attribute_kind::b, false)
Expand All @@ -1006,8 +993,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_softmax, 1,
{"none", "inf_as_zero"})
// New added attributes
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
// Analysis rules
.set_shape_inference_function(infer_identity_output_shape)
.SET_LAYOUT_PROPAGATOR(layout_propagator_for_softmax)
Expand Down Expand Up @@ -1052,8 +1038,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_layernorm, 1,
int64_t(-1))
.set_attr(op_attr::use_affine, false, attribute_kind::b, true)
.set_attr(op_attr::epsilon, false, attribute_kind::f, 1e-5f)
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
// New added attributes
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
// Analysis rules
Expand All @@ -1077,8 +1062,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_reorder, 1,
.set_attr(
op_attr::qtype, false, attribute_kind::s, "per_tensor")
// Attributes
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(
op_attr::change_layout, false, attribute_kind::b, false)
.set_attr(op_attr::scales, false, attribute_kind::fs)
Expand Down Expand Up @@ -1122,8 +1106,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_groupnorm, 1,
.set_attr(op_attr::epsilon, false, attribute_kind::f, 1e-5f)
.set_attr(op_attr::data_format, false, attribute_kind::s, "NXC",
{"NCX", "NXC"})
.set_attr(op_attr::fusion_info_key, false, attribute_kind::i,
(int64_t)-1)
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
// New added attributes
.SET_ATTR_IS_CONSTANT // used for constant prop and cache
// Analysis rules
Expand Down Expand Up @@ -1171,6 +1154,7 @@ DNNL_GRAPH_OP_SCHEMA(dnnl_sdpa, 1,
.set_input(4, "mask") // optional
.set_output(0, "output")
.set_output(1, "scratchpad")
.set_attr(op_attr::fusion_info, false, attribute_kind::c)
.set_attr(op_attr::with_scale, true, attribute_kind::b)
.set_attr(op_attr::is_invert_scale, false, attribute_kind::b,
false)
Expand Down
137 changes: 137 additions & 0 deletions src/graph/backend/dnnl/fusion_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,143 @@ dnnl::primitive_attr make_dnnl_primitive_attr(
return attr;
}

dnnl::primitive_attr make_dnnl_sdpa_primitive_attr(
const std::shared_ptr<op_t> &op, const fusion_info_t &fusion_info,
const attr_type_t attr_type) {
dnnl::primitive_attr attr;
std::vector<int64_t> default_groups;

const static std::unordered_map<size_t, size_t> arg_map = {
{DNNL_ARG_QUERIES, DNNL_ARG_SRC},
{DNNL_ARG_KEYS, DNNL_ARG_WEIGHTS},
{DNNL_ARG_VALUES, DNNL_ARG_WEIGHTS},
};

// convert input scales
if (!fusion_info.input_scales_.empty()) {

for (const auto &in_scales : fusion_info.input_scales_) {
size_t in_scales_indices = in_scales.first;
if (attr_type == attr_type_t::QK) {
if (in_scales_indices != DNNL_ARG_QUERIES
&& in_scales_indices != DNNL_ARG_KEYS) {
continue;
}
} else if (attr_type == attr_type_t::VS) {
if (in_scales_indices != DNNL_ARG_VALUES) { continue; }
}
const op_t *in_scales_op = in_scales.second->get_op();
VCHECK_FUSION_INFO(
fusion_info.with_runtime_scales(true, in_scales_indices),
attr,
"failed to set scales for %s since primitive only supports "
"runtime src scales",
op->get_name().c_str());
int mask = 0;
if (in_scales_op->has_attr(op_attr::qtype)) {
std::string qtype
= in_scales_op->get_attr<std::string>(op_attr::qtype);
const auto scales_data_type
= in_scales_op->has_attr(op_attr::data_type)
? in_scales_op->get_attr<int64_t>(op_attr::data_type)
: dnnl_f32;
if (qtype == "per_tensor") {
mask = 0;
attr.set_scales(
static_cast<int>(arg_map.at(in_scales_indices)),
mask, default_groups,
static_cast<dnnl::memory::data_type>(
scales_data_type));
} else if (qtype == "per_channel") { // per-channel quantization
int64_t axis = in_scales_op->has_attr(op_attr::axis)
? in_scales_op->get_attr<int64_t>(op_attr::axis)
: 1;
mask = 1 << axis;
attr.set_scales(
static_cast<int>(arg_map.at(in_scales_indices)),
mask, default_groups,
static_cast<dnnl::memory::data_type>(
scales_data_type));
} else { // per-group quantization
// oneDNN only supports weights-decompressed matmul
if (arg_map.at(in_scales_indices) != DNNL_ARG_WEIGHTS)
continue;
const auto &group_shape
= in_scales_op->get_attr<std::vector<int64_t>>(
op_attr::group_shape);

// Currently oneDNN only supports grouped scales and zps on
// last two dimensions.
std::vector<int64_t> groups(
group_shape.end() - 2, group_shape.end());
int mask = (1 << group_shape.size()) - 1;

attr.set_scales(DNNL_ARG_WEIGHTS, mask, groups,
static_cast<dnnl::memory::data_type>(
scales_data_type));
}
}
}
}

// convert input zps
if (!fusion_info.input_zps_.empty()) {
for (const auto &in_zps : fusion_info.input_zps_) {
size_t in_zps_indices = in_zps.first;
if (attr_type == attr_type_t::QK) {
if (in_zps_indices != DNNL_ARG_QUERIES
&& in_zps_indices != DNNL_ARG_KEYS) {
continue;
}
} else if (attr_type == attr_type_t::VS) {
if (in_zps_indices != DNNL_ARG_VALUES) { continue; }
}
const op_t *in_zps_op = in_zps.second->get_op();
VCHECK_FUSION_INFO(
fusion_info.with_runtime_zero_points(true, in_zps_indices),
attr,
"failed to set zero points for %s since primitive only "
"supports runtime src zero points",
op->get_name().c_str());

if (in_zps_op->has_attr(op_attr::qtype)) {
std::string qtype
= in_zps_op->get_attr<std::string>(op_attr::qtype);
const auto zps_data_type
= in_zps_op->has_attr(op_attr::data_type)
? in_zps_op->get_attr<int64_t>(op_attr::data_type)
: dnnl_s32;
if (qtype == "per_group") {
// oneDNN only supports weights-decompressed matmul
if (arg_map.at(in_zps_indices) != DNNL_ARG_WEIGHTS) break;
const auto &group_shape
= in_zps_op->get_attr<std::vector<int64_t>>(
op_attr::group_shape);

// Currently oneDNN only supports grouped scales and zps on
// last two dimensions.
std::vector<int64_t> groups(
group_shape.end() - 2, group_shape.end());
int mask = (1 << group_shape.size()) - 1;

// Currently oneDNN only supports grouped zps on last two dimensions.
attr.set_zero_points(DNNL_ARG_WEIGHTS, mask, groups,
static_cast<dnnl::memory::data_type>(
zps_data_type));

} else {
int mask = 0;
attr.set_zero_points(arg_map.at(in_zps_indices), mask,
default_groups,
static_cast<dnnl::memory::data_type>(
zps_data_type));
}
}
}
}
return attr;
}

} // namespace dnnl_impl
} // namespace graph
} // namespace impl
Expand Down
Loading
Loading