Skip to content
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

Fix layernorm and softmax axis after upstream #17255

Merged
merged 3 commits into from
Aug 25, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,27 @@ bool LayerNormalizationGatherActor::PreCheck(const Graph& /* graph */,
return true;
}

bool LayerNormalizationGatherActor::PostProcess(Graph& /*graph*/, Node& current_node,
const SliceInfo& info_without_node,
const logging::Logger& /*logger*/,
const std::unordered_map<int, int>& /*propagate_input_indices*/,
const std::unordered_map<int, std::vector<DimCompare>>&
/*all_input_cmp_rets*/,
const std::unordered_map<int, SliceInfo>& /*new_gather_infos*/) {
// Update LayerNormalization's axis attribute if it is scalar slice.
if (info_without_node.is_scalar_slice) {
askhade marked this conversation as resolved.
Show resolved Hide resolved
auto axis = static_cast<int64_t>(current_node.GetAttributes().at("axis").i());
auto original_ln_input_rank = info_without_node.input_rank;
axis = axis < 0 ? axis + original_ln_input_rank : axis;
auto new_axis = axis - 1;

auto& attributes = current_node.GetMutableAttributes();
attributes["axis"] = ONNX_NAMESPACE::MakeAttribute("axis", static_cast<int64_t>(new_axis));
}

return true;
}

bool SoftmaxGatherActor::PreCheck(const Graph& graph, const Node& current_node, const SliceInfo& info,
const logging::Logger& logger,
std::unordered_map<int, int>& propagate_input_indices,
Expand All @@ -479,6 +500,28 @@ bool SoftmaxGatherActor::PreCheck(const Graph& graph, const Node& current_node,
propagate_input_indices, all_input_cmp_rets, shape_update_func);
}

bool SoftmaxGatherActor::PostProcess(Graph& graph, Node& current_node, const SliceInfo& info_without_node,
const logging::Logger& logger,
const std::unordered_map<int, int>& propagate_input_indices,
const std::unordered_map<int, std::vector<DimCompare>>& all_input_cmp_rets,
const std::unordered_map<int, SliceInfo>& new_gather_infos) {
SimplePointwiseGatherActor<true>::PostProcess(graph, current_node, info_without_node, logger,
propagate_input_indices, all_input_cmp_rets, new_gather_infos);

// Update Softmax's axis attribute if it is scalar slice.
if (info_without_node.is_scalar_slice) {
auto axis = static_cast<int64_t>(current_node.GetAttributes().at("axis").i());
auto original_ln_input_rank = info_without_node.input_rank;
axis = axis < 0 ? axis + original_ln_input_rank : axis;
auto new_axis = axis - 1;

auto& attributes = current_node.GetMutableAttributes();
attributes["axis"] = ONNX_NAMESPACE::MakeAttribute("axis", static_cast<int64_t>(new_axis));
}

return true;
}

bool ReshapeGatherActor::PreCheck(const Graph& graph, const Node& current_node, const SliceInfo& info,
const logging::Logger& logger,
std::unordered_map<int, int>& propagate_input_indices,
Expand Down Expand Up @@ -566,6 +609,11 @@ bool ReshapeGatherActor::PreCheck(const Graph& graph, const Node& current_node,
return true;
}

LOG_DEBUG_INFO(logger, "Skip handle the Reshape, new_shape_const_values[info.non_negative_axis]:" +
std::to_string(new_shape_const_values[info.non_negative_axis]) +
", info.output_dim_on_axis.has_dim_value(): " +
std::to_string(info.output_dim_on_axis.has_dim_value()) + ".");

return false;
}

Expand Down Expand Up @@ -604,11 +652,12 @@ bool ReshapeGatherActor::PostProcess(
return true;
}

// If it selected shape is a dim value, we can update the shape tensor directory.
// If the selected shape is a dim value, we can update the shape tensor directory.
if (info_without_node.output_dim_on_axis.has_dim_value()) {
new_shape_const_values[slice_axis] = info_without_node.output_dim_on_axis.dim_value();
auto new_shape_arg =
CreateInitializerFromVector(graph, {static_cast<int64_t>(new_shape_const_values.size())}, new_shape_const_values,
CreateInitializerFromVector(graph, {static_cast<int64_t>(new_shape_const_values.size())},
new_shape_const_values,
graph.GenerateNodeArgName(current_node.MutableInputDefs()[1]->Name()));
graph_utils::ReplaceNodeInput(current_node, 1, *new_shape_arg);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class LayerNormalizationGatherActor : public UpStreamGatherOperatorActorBase {
const logging::Logger& /* logger */,
const std::unordered_map<int, int>& /* propagate_input_indices */,
const std::unordered_map<int, std::vector<DimCompare>>& /* all_input_cmp_rets */,
const std::unordered_map<int, SliceInfo>& /* new_gather_infos */) override { return true; }
const std::unordered_map<int, SliceInfo>& /* new_gather_infos */) override;
};

class SoftmaxGatherActor : public SimplePointwiseGatherActor<true> {
Expand All @@ -202,6 +202,12 @@ class SoftmaxGatherActor : public SimplePointwiseGatherActor<true> {
std::unordered_map<int, int>& propagate_input_indices,
std::unordered_map<int, std::vector<DimCompare>>& all_input_cmp_rets,
std::function<void(Node& node)>& shape_update_func) override;

bool PostProcess(Graph& /* graph */, Node& /* current_node */, const SliceInfo& /* info_without_node */,
const logging::Logger& /* logger */,
const std::unordered_map<int, int>& /* propagate_input_indices */,
const std::unordered_map<int, std::vector<DimCompare>>& /* all_input_cmp_rets */,
const std::unordered_map<int, SliceInfo>& /* new_gather_infos */) override;
};

class ReshapeGatherActor : public UpStreamGatherOperatorActorBase {
Expand Down
Loading