Skip to content

Commit

Permalink
graph: backend: passes: verbose log enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
rongzha1 committed Jan 8, 2025
1 parent 1ccda14 commit 3ae1fb9
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 198 deletions.
28 changes: 15 additions & 13 deletions src/graph/backend/dnnl/passes/compile_ops.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2021-2024 Intel Corporation
* Copyright 2021-2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,10 @@

#include "oneapi/dnnl/dnnl.hpp"

#define VCHECK_COMPILE_OPS(cond, status, msg, ...) \
VCONDCHECK(graph, create, check, compile_ops, (cond), status, msg, \
##__VA_ARGS__);

namespace dnnl {
namespace impl {
namespace graph {
Expand All @@ -43,26 +47,24 @@ status_t compile_ops(std::shared_ptr<subgraph_t> &sg) {
return topo_order_visit(sg->get_output_ops(), [&](op_t *op) {
const op_schema_t *opm
= op_schema_registry_t::get_op_schema(op->get_kind());
if (!opm) {
assertm(false, "no schema for current op");
return status::invalid_graph_op;
}

if (!opm->has_additional_item("executable_creator")) {
assertm(false, "no executable creator in this op schema");
return status::invalid_graph_op;
}
VCHECK_COMPILE_OPS(opm != nullptr, status::invalid_graph_op,
"no schema for current op %s", op->get_name().c_str());

VCHECK_COMPILE_OPS(opm->has_additional_item("executable_creator"),
status::invalid_graph_op,
"no executable creator in schema of op %s",
op->get_name().c_str());

auto cur_op = op->shared_from_this();
auto creator = opm->get_additional_item<executable_creator_func>(
"executable_creator");
std::shared_ptr<op_executable_t> exec
= creator(cur_op, p_engine, mgr, pd_cache);

if (!exec) {
assertm(false, "unimplemented op, can't compile it");
return status::unimplemented;
}
VCHECK_COMPILE_OPS(exec != nullptr, status::invalid_graph_op,
"unimplemented op, can't compile op %s",
op->get_name().c_str());

sg->execs_.emplace_back(exec);
sg->is_constant_.push_back(op->has_attr(op_attr::is_constant)
Expand Down
52 changes: 35 additions & 17 deletions src/graph/backend/dnnl/passes/insert_ops.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2021-2024 Intel Corporation
* Copyright 2021-2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,10 @@
#include "graph/backend/dnnl/passes/insert_ops.hpp"
#include "graph/backend/dnnl/passes/utils.hpp"

#define VCHECK_INSERT_OPS(cond, status, msg, ...) \
VCONDCHECK(graph, create, check, insert_ops, (cond), status, msg, \
##__VA_ARGS__);

namespace dnnl {
namespace impl {
namespace graph {
Expand Down Expand Up @@ -332,23 +336,30 @@ status_t insert_to_group_for_reorder(std::shared_ptr<subgraph_t> &sg) {
// reorder's input has blocked format with group
// while output has plain format, perhaps for
// backward path. No such case for now, disable
return status::unimplemented;
VCHECK_INSERT_OPS(false, status::unimplemented,
"unsupported i/o dimentions to insert to_group for "
"reorder, input ndims: %d, output ndims: %d",
in_md.get_ndims(), out_md.get_ndims());
} else if (in_md.get_ndims() + 1 == out_md.get_ndims()) {
// reorder's input has plain format while output
// has blocked format with group, typically for
// weight prepacking
auto group = out_md.get_dims()[0];
if (group * out_md.get_dims()[1] != in_md.get_dims()[0])
return status::invalid_shape;

VCHECK_INSERT_OPS(
group * out_md.get_dims()[1] == in_md.get_dims()[0],
status::invalid_shape,
"unmatched shape to insert to_group for reorder, group: %d,"
"output dims[1]: %d, input dims[0], %d",
group, out_md.get_dims()[1], in_md.get_dims()[0]);
// insert to_group op
op_ptr to_group_op = std::make_shared<op_t>(op_kind::dnnl_to_group);
to_group_op->set_attr<int64_t>(op_attr::groups, group);

rewriter.insert_op_before(to_group_op, cur_op, 0);
} else {
// illegal shape
return status::invalid_shape;
VCHECK_INSERT_OPS(false, status::invalid_shape,
"invalid shape to insert to_group for reorder");
}
}

Expand Down Expand Up @@ -573,7 +584,11 @@ status_t insert_unsqueeze_and_squeeze_for_matmul(

int32_t src_ndims = op->get_input_value(0)->get_logical_tensor().ndims;
int32_t wei_ndims = op->get_input_value(1)->get_logical_tensor().ndims;
assertm(src_ndims >= 1 && wei_ndims >= 1, "invalid dims");
VCHECK_INSERT_OPS(src_ndims >= 1 && wei_ndims >= 1,
status::invalid_shape,
"src_ndims and wei_ndims should >= 1, src_ndims: %d, "
"wei_ndims: %d",
src_ndims, wei_ndims);

int32_t unsqueezed_dst_ndims
= std::max(std::max(src_ndims, wei_ndims), 2);
Expand Down Expand Up @@ -690,8 +705,9 @@ impl::status_t insert_runtime_u8_to_s8_for_matmul(
// add a binary add here.
}
} else {
assertm(cur_op->num_inputs() == index,
"only support insert input at the end of inputs");
VCHECK_INSERT_OPS(cur_op->num_inputs() == index,
status::unimplemented,
"only support insert input for wei at the end of inputs");
std::vector<int64_t> zp {-128};
auto zps_op = std::make_shared<op_t>(op_kind::dnnl_add_zps);
zps_op->set_attr<std::string>(op_attr::qtype, "per_tensor");
Expand Down Expand Up @@ -833,10 +849,11 @@ status_t insert_unsqueeze_for_prelu(std::shared_ptr<subgraph_t> &sg) {
const bool per_channel_broadcast
= cur_op->get_attr<bool>(op_attr::per_channel_broadcast);

if (!prelu_doable(ltw(src_lt).vdims(), ltw(wei_lt).vdims(), data_format,
per_channel_broadcast)) {
return status::invalid_shape;
}
const bool prelu_doable_status = prelu_doable(ltw(src_lt).vdims(),
ltw(wei_lt).vdims(), data_format, per_channel_broadcast);
VCHECK_INSERT_OPS(prelu_doable_status, status::invalid_shape,
"invalid shape to insert unsqueeze for prelu");

// insert unsqueeze op
int32_t src_ndims = src_lt.ndims;
int32_t wei_ndims = wei_lt.ndims;
Expand Down Expand Up @@ -886,10 +903,11 @@ status_t insert_unsqueeze_and_squeeze_for_prelu_bwd(
const bool per_channel_broadcast
= wei_vdims.size() == 1 && wei_vdims[0] != 1;

if (!prelu_doable(ltw(src_lt).vdims(), wei_vdims, data_format,
per_channel_broadcast)) {
return status::invalid_shape;
}
const bool prelu_doable_status = prelu_doable(ltw(src_lt).vdims(),
wei_vdims, data_format, per_channel_broadcast);
VCHECK_INSERT_OPS(prelu_doable_status, status::invalid_shape,
"invalid shape to insert unsqueeze for prelu");

// insert unsqueeze op
int32_t src_ndims = src_lt.ndims;
int32_t wei_ndims = wei_lt.ndims;
Expand Down
39 changes: 20 additions & 19 deletions src/graph/backend/dnnl/passes/layout_propagation.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2021-2024 Intel Corporation
* Copyright 2021-2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,10 @@
#include "graph/backend/dnnl/common.hpp"
#include "graph/backend/dnnl/layout_propagator.hpp"

#define VCHECK_LAYOUT_PROPAGATION(cond, status, msg, ...) \
VCONDCHECK(graph, create, check, layout_propagation, (cond), status, msg, \
##__VA_ARGS__);

namespace dnnl {
namespace impl {
namespace graph {
Expand Down Expand Up @@ -117,15 +121,14 @@ status_t layout_propagation(std::shared_ptr<subgraph_t> &sg) {

const op_schema_t *opm
= op_schema_registry_t::get_op_schema(op->get_kind());
if (!opm) {
assertm(false, "no schema for current op");
return status::invalid_graph_op;
}
VCHECK_LAYOUT_PROPAGATION(opm != nullptr, status::invalid_graph_op,
"no schema for current op: %s", op->get_name().c_str());

if (!opm->has_additional_item("layout_propagator")) {
assertm(false, "no layout propagator in this op schema");
return status::invalid_graph_op;
}
VCHECK_LAYOUT_PROPAGATION(
opm->has_additional_item("layout_propagator"),
status::invalid_graph_op,
"no layout propagator in the schema of op: %s",
op->get_name().c_str());

auto cur_op = op->shared_from_this();
auto propagator = opm->get_additional_item<layout_propagator_func>(
Expand All @@ -137,14 +140,14 @@ status_t layout_propagation(std::shared_ptr<subgraph_t> &sg) {
return status;
});

if (ret != status::success) return ret;
VCHECK_LAYOUT_PROPAGATION(
ret == status::success, ret, "layout propagation failed");
rewriter.run();
propagation_number++;
if (propagation_number >= LAYOUT_PROPAGATION_NUMBER) {
assertm(false,
"expect layout propagation number to be less than 10");
return status::invalid_arguments;
}
VCHECK_LAYOUT_PROPAGATION(
propagation_number < LAYOUT_PROPAGATION_NUMBER,
status::invalid_arguments,
"expect layout propagation number to be less than 10");
} while (need_prop_once_more(sg));

// Add check for the layout type of partition outputs to make partition
Expand All @@ -160,8 +163,7 @@ status_t layout_propagation(std::shared_ptr<subgraph_t> &sg) {
auto lt = in_val->get_logical_tensor();
if (lt.id == sg->ins_[i].id) {
auto md = make_dnnl_memory_desc(lt);
auto status = fill_layout_info(&(sg->ins_[i]), md);
if (status != status::success) return status;
CHECK(fill_layout_info(&(sg->ins_[i]), md));
}
}
}
Expand All @@ -172,8 +174,7 @@ status_t layout_propagation(std::shared_ptr<subgraph_t> &sg) {
auto lt = out_val->get_logical_tensor();
if (lt.id == sg->outs_[i].id) {
auto md = make_dnnl_memory_desc(lt);
auto status = fill_layout_info(&(sg->outs_[i]), md);
if (status != status::success) return status;
CHECK(fill_layout_info(&(sg->outs_[i]), md));
}
}
}
Expand Down
53 changes: 29 additions & 24 deletions src/graph/backend/dnnl/passes/lower.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright 2022-2024 Intel Corporation
* Copyright 2022-2025 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -375,18 +375,17 @@ static status_t static_quant_handler(

auto in_vals = op->get_input_values();
auto out_vals = op->get_output_values();
assertm(in_vals.size() == 1 && out_vals.size() == 1,
"static quantize/dequantize should only have one input and "
"output");

VCHECK_INVALID_ARGUMENT(in_vals.size() == 1 && out_vals.size() == 1,
"static quantize/dequantize should only have one input and output"
" but got %zu input and %zu output",
in_vals.size(), out_vals.size());
VCHECK_INVALID_ARGUMENT(std::all_of(scales.begin(), scales.end(),
[](float i) { return i != 0.f; }),
"scales can't be zero");
// int8 = f32 / scales + zps
op_ptr mul_scales_op = std::make_shared<op_t>(op_kind::dnnl_mul_scales);
op_ptr add_zps_op = std::make_shared<op_t>(op_kind::dnnl_add_zps);

assertm(std::all_of(scales.begin(), scales.end(),
[](float i) { return i != 0.f; }),
"scales can't be zero");

std::vector<float> inv_scales
= dnnl_impl::utils::fmap(scales, [](float s) { return 1.f / s; });
mul_scales_op->set_attr<std::vector<float>>(op_attr::scales, inv_scales);
Expand Down Expand Up @@ -433,8 +432,10 @@ static status_t static_dequant_handler(

auto in_vals = cur_op->get_input_values();
auto out_vals = cur_op->get_output_values();
assertm(in_vals.size() == 1 && out_vals.size() == 1,
"static dequantize should only have one input and output");
VCHECK_INVALID_ARGUMENT(in_vals.size() == 1 && out_vals.size() == 1,
"static dequantize should only have one input and output but "
"got %zu input and %zu output",
in_vals.size(), out_vals.size());

// f32 = scales * (int8 - zps)
op_ptr sub_zps_op = std::make_shared<op_t>(op_kind::dnnl_sub_zps);
Expand Down Expand Up @@ -478,9 +479,11 @@ static status_t dynamic_quant_handler(

auto &in_vals = cur_op->get_input_values();
auto &out_vals = cur_op->get_output_values();
assertm((in_vals.size() == 3 || in_vals.size() == 2)
VCHECK_INVALID_ARGUMENT((in_vals.size() == 3 || in_vals.size() == 2)
&& out_vals.size() == 1,
"dynamic quantize must have 2 or 3 inputs and 1 output");
"dynamic quantize must have 2 or 3 inputs and 1 output, but "
"got %zu input and %zu output",
in_vals.size(), out_vals.size());

// DynamicQuantize has optional zps
bool has_zps = in_vals.size() == 3;
Expand Down Expand Up @@ -537,9 +540,11 @@ static status_t dynamic_dequant_handler(

auto &in_vals = cur_op->get_input_values();
auto &out_vals = cur_op->get_output_values();
assertm((in_vals.size() == 3 || in_vals.size() == 2)
VCHECK_INVALID_ARGUMENT((in_vals.size() == 3 || in_vals.size() == 2)
&& out_vals.size() == 1,
"dynamic dequantize must have 2 or 3 inputs and 1 output");
"dynamic dequantize must have 2 or 3 inputs and 1 output, but "
"got %zu input and %zu output",
in_vals.size(), out_vals.size());

// DynamicDequantize has optional zps
bool has_zps = in_vals.size() == 3;
Expand Down Expand Up @@ -653,8 +658,10 @@ static status_t select_handler(

auto in_vals = op->get_input_values();
auto out_vals = op->get_output_values();
assertm(in_vals.size() == 3 && out_vals.size() == 1,
"select should have three inputs and a output");
VCHECK_INVALID_ARGUMENT(in_vals.size() == 3 && out_vals.size() == 1,
"select should have three input and one output but "
"got %zu input and %zu output",
in_vals.size(), out_vals.size());
auto cond = in_vals[0];
auto src0 = in_vals[1];
auto src1 = in_vals[2];
Expand Down Expand Up @@ -893,13 +900,11 @@ status_t lower_down(std::shared_ptr<subgraph_t> &sg) {

for (auto &cur_op : sg->get_ops()) {
auto kind = cur_op->get_kind();
if (!handler_table.count(kind)) {
assertm(false,
"All spec ops should be lowered to internal ops, except "
"for some utility ops like End, Wildcard");
return status::invalid_graph_op;
}

VCHECK_INVALID_ARGUMENT(handler_table.count(kind),
"All spec ops should be lowered to internal ops, except "
"for some utility ops like End, Wildcard. Current op name is "
"%s",
cur_op->get_name().c_str());
// lower this spec op to dnnl backend internal op
const auto &handler = handler_table.at(kind);
auto status = handler(cur_op, rewriter);
Expand Down
Loading

0 comments on commit 3ae1fb9

Please sign in to comment.