forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factor out
AssignIdsToCustomAggregatorOps
.
Implements `AssignIdsToCustomAggregatorOps` in cpp, replicating the python implementation of `assign_ids_to_custom_aggregator_ops`. Accordingly, this change removes `assign_ids_to_custom_aggregator_ops` from `py_function_lib`. PiperOrigin-RevId: 589349801
- Loading branch information
1 parent
6812199
commit 4a617a0
Showing
17 changed files
with
212 additions
and
114 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
43 changes: 43 additions & 0 deletions
43
tensorflow/compiler/mlir/quantization/stablehlo/cc/calibration/assign_ids.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,43 @@ | ||
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
#include "tensorflow/compiler/mlir/quantization/stablehlo/cc/calibration/assign_ids.h" | ||
|
||
#include <cstdint> | ||
|
||
#include "absl/strings/str_cat.h" | ||
#include "tensorflow/compiler/mlir/quantization/stablehlo/cc/graph_def.h" | ||
#include "tensorflow/compiler/mlir/quantization/tensorflow/calibrator/calibrator_singleton.h" | ||
#include "tensorflow/core/framework/attr_value.pb.h" | ||
#include "tensorflow/core/framework/graph.pb.h" | ||
|
||
namespace stablehlo::quantization { | ||
namespace { | ||
|
||
using ::tensorflow::GraphDef; | ||
using ::tensorflow::NodeDef; | ||
using ::tensorflow::calibrator::CalibratorSingleton; | ||
|
||
} // namespace | ||
|
||
void AssignIdsToCustomAggregatorOps(GraphDef& graph_def) { | ||
MutateNodeDefs(graph_def, [](NodeDef& node_def) { | ||
if (node_def.op() == "CustomAggregator") { | ||
const int64_t new_id = CalibratorSingleton::IssueNewId(); | ||
(*node_def.mutable_attr())["id"].set_s(absl::StrCat(new_id)); | ||
} | ||
}); | ||
} | ||
|
||
} // namespace stablehlo::quantization |
30 changes: 30 additions & 0 deletions
30
tensorflow/compiler/mlir/quantization/stablehlo/cc/calibration/assign_ids.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,30 @@ | ||
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
#ifndef TENSORFLOW_COMPILER_MLIR_QUANTIZATION_STABLEHLO_CC_CALIBRATION_ASSIGN_IDS_H_ | ||
#define TENSORFLOW_COMPILER_MLIR_QUANTIZATION_STABLEHLO_CC_CALIBRATION_ASSIGN_IDS_H_ | ||
|
||
#include "tensorflow/core/framework/graph.pb.h" | ||
|
||
namespace stablehlo::quantization { | ||
|
||
// Assigns unique ids to each CustomAggregator op found in `graph_def`. The | ||
// ids are set to the `id` attribute. The ids are used during the calibration | ||
// step to identify the collected quantization statistics for each | ||
// CustsomAggregator op. | ||
void AssignIdsToCustomAggregatorOps(tensorflow::GraphDef& graph_def); | ||
|
||
} // namespace stablehlo::quantization | ||
|
||
#endif // TENSORFLOW_COMPILER_MLIR_QUANTIZATION_STABLEHLO_CC_CALIBRATION_ASSIGN_IDS_H_ |
63 changes: 63 additions & 0 deletions
63
tensorflow/compiler/mlir/quantization/stablehlo/cc/calibration/assign_ids_test.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,63 @@ | ||
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
#include "tensorflow/compiler/mlir/quantization/stablehlo/cc/calibration/assign_ids.h" | ||
|
||
#include <gmock/gmock.h> | ||
#include <gtest/gtest.h> | ||
#include "tensorflow/core/framework/attr_value.pb.h" | ||
#include "tensorflow/core/framework/graph.pb.h" | ||
#include "tensorflow/core/framework/node_def.pb.h" | ||
#include "tsl/platform/protobuf.h" // IWYU pragma: keep for tsl::protobuf | ||
|
||
namespace stablehlo::quantization { | ||
namespace { | ||
|
||
using ::tensorflow::GraphDef; | ||
using ::testing::IsEmpty; | ||
using ::testing::Not; | ||
using ::testing::SizeIs; | ||
using ::tsl::protobuf::TextFormat; | ||
|
||
TEST(AssignIdsTest, IdsAddedToCustomAggregatorOps) { | ||
GraphDef graph_def; | ||
ASSERT_TRUE(TextFormat::ParseFromString( | ||
R"pb( | ||
node { op: "CustomAggregator" name: "foo" } | ||
)pb", | ||
&graph_def)); | ||
|
||
AssignIdsToCustomAggregatorOps(graph_def); | ||
|
||
ASSERT_THAT(graph_def.node(), SizeIs(1)); | ||
EXPECT_TRUE(graph_def.node()[0].attr().contains("id")); | ||
EXPECT_THAT(graph_def.node()[0].attr().at("id").s(), Not(IsEmpty())); | ||
} | ||
|
||
TEST(AssignIdsTest, IdsNotAddedForNonCustomAggregatorOps) { | ||
GraphDef graph_def; | ||
ASSERT_TRUE(TextFormat::ParseFromString( | ||
R"pb( | ||
node { op: "NotCustomAggregator" name: "bar" } | ||
)pb", | ||
&graph_def)); | ||
|
||
AssignIdsToCustomAggregatorOps(graph_def); | ||
|
||
ASSERT_THAT(graph_def.node(), SizeIs(1)); | ||
EXPECT_FALSE(graph_def.node()[0].attr().contains("id")); | ||
} | ||
|
||
} // namespace | ||
} // namespace stablehlo::quantization |
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
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
Oops, something went wrong.