-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fuse Cast + SoftmaxCrossEntropyLossInternal (#20334)
### Description Fuse Cast + SoftmaxCrossEntropyLossInternal to SoftmaxCrossEntropyLossInternal.
- Loading branch information
1 parent
923b0ef
commit 3e4db2c
Showing
10 changed files
with
132 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
orttraining/orttraining/core/optimizer/cast_sce_loss_fusion.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include "orttraining/core/optimizer/cast_sce_loss_fusion.h" | ||
|
||
#include "core/graph/graph_utils.h" | ||
#include "core/optimizer/initializer.h" | ||
#include "core/optimizer/utils.h" | ||
|
||
namespace onnxruntime { | ||
|
||
Status CastSceLossFusion::ApplyImpl(Graph& graph, bool& modified, int graph_level, | ||
const logging::Logger& logger) const { | ||
GraphViewer graph_viewer(graph); | ||
const auto& node_topology_list = graph_viewer.GetNodesInTopologicalOrder(); | ||
|
||
for (auto node_index : node_topology_list) { | ||
auto* node_ptr = graph.GetNode(node_index); | ||
if (!node_ptr) continue; // Node was removed. | ||
|
||
auto& node = *node_ptr; | ||
ORT_RETURN_IF_ERROR(Recurse(node, modified, graph_level, logger)); | ||
|
||
bool is_internal_sce = graph_utils::IsSupportedOptypeVersionAndDomain(node, "SoftmaxCrossEntropyLossInternal", {1}, | ||
kMSDomain); | ||
|
||
if (!is_internal_sce || !graph_utils::IsSupportedProvider(node, GetCompatibleExecutionProviders())) { | ||
continue; | ||
} | ||
|
||
Node* input_node = graph.GetMutableProducerNode(node.MutableInputDefs()[0]->Name()); | ||
|
||
if (!(graph_utils::IsSupportedOptypeVersionAndDomain(*input_node, "Cast", {9, 13, 19}))) { | ||
continue; | ||
} | ||
|
||
if (input_node->GetOutputEdgesCount() != 1 || graph.IsOutput(input_node->OutputDefs()[0])) { | ||
continue; | ||
} | ||
|
||
if (input_node->MutableInputDefs()[0]->TypeAsProto()->tensor_type().elem_type() == onnx::TensorProto_DataType_FLOAT16 && | ||
input_node->MutableOutputDefs()[0]->TypeAsProto()->tensor_type().elem_type() == onnx::TensorProto_DataType_FLOAT) { | ||
std::vector<graph_utils::GraphEdge> input_edges = graph_utils::GraphEdge::GetNodeInputEdges(node, 0); | ||
graph_utils::GraphEdge::RemoveGraphEdges(graph, input_edges); | ||
node.MutableInputDefs()[0] = input_node->MutableInputDefs()[0]; | ||
graph_utils::MoveAllNodeInputEdges(graph, *input_node, node); | ||
graph.RemoveNode(input_node->Index()); | ||
|
||
if (node.GetAttributes().count("output_type") == 0) { | ||
node.AddAttribute("output_type", static_cast<int64_t>(onnx::TensorProto_DataType_FLOAT)); | ||
} | ||
modified = true; | ||
} | ||
} | ||
|
||
return Status::OK(); | ||
} | ||
|
||
} // namespace onnxruntime |
23 changes: 23 additions & 0 deletions
23
orttraining/orttraining/core/optimizer/cast_sce_loss_fusion.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#pragma once | ||
|
||
#include "core/optimizer/graph_transformer.h" | ||
|
||
namespace onnxruntime { | ||
|
||
/** | ||
@Class CastSceLossFusion | ||
Fuse Cast + SoftmaxCrossEntropyLossInternal to SoftmaxCrossEntropyLossInternal. | ||
*/ | ||
class CastSceLossFusion : public GraphTransformer { | ||
public: | ||
explicit CastSceLossFusion(const InlinedHashSet<std::string_view>& compatible_execution_providers = {}) noexcept | ||
: GraphTransformer("CastSceLossFusion", compatible_execution_providers) { | ||
} | ||
|
||
Status ApplyImpl(Graph& graph, bool& modified, int graph_level, const logging::Logger& logger) const override; | ||
}; | ||
|
||
} // namespace onnxruntime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters