Skip to content

Commit fd9e47d

Browse files
LukeBoyercopybara-github
authored andcommitted
Add printer for list of ops.
LiteRT-PiperOrigin-RevId: 775408981
1 parent 76e10d1 commit fd9e47d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

litert/core/model/model.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,19 @@ void AbslStringify(Sink& sink, const LiteRtOpT* op) {
11131113
absl::Format(&sink, "null");
11141114
}
11151115

1116+
namespace absl {
1117+
1118+
template <class Sink>
1119+
void AbslStringify(Sink& sink, const absl::Span<LiteRtOp>& ops) {
1120+
for (auto it = ops.begin(); it < ops.end() - 1; ++it) {
1121+
sink.Append(absl::StrFormat("%v", **it));
1122+
sink.Append("/");
1123+
}
1124+
sink.Append(absl::StrFormat("%v", *ops.back()));
1125+
}
1126+
1127+
} // namespace absl
1128+
11161129
// OPTIONS PRINTING
11171130

11181131
namespace litert::internal {

litert/core/model/model_test.cc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,5 +660,65 @@ TEST(PrintingTest, TflAddOptionsPointer) {
660660
EXPECT_EQ(absl::StrFormat("%v", &add_opts), "{fa=RELU6,pot=true}");
661661
}
662662

663+
TEST(PrintingTest, Subgraph) {
664+
LiteRtSubgraphT subgraph;
665+
666+
{
667+
auto& op = subgraph.EmplaceOp();
668+
669+
op.SetOpCode(kLiteRtOpCodeTflAdd);
670+
671+
::tflite::AddOptionsT add_opts;
672+
add_opts.fused_activation_function = ::tflite::ActivationFunctionType_RELU;
673+
add_opts.pot_scale_int16 = false;
674+
TflOptions opts;
675+
opts.type = ::tflite::BuiltinOptions_AddOptions;
676+
opts.Set(std::move(add_opts));
677+
litert::internal::SetTflOptions(op, std::move(opts));
678+
679+
auto& tensor = subgraph.EmplaceTensor();
680+
tensor.SetType(MakeRankedTensorType(kLiteRtElementTypeInt32, {2, 2, 2}));
681+
op.Inputs().push_back(&tensor);
682+
683+
auto& tensor2 = subgraph.EmplaceTensor();
684+
tensor2.SetType(MakeRankedTensorType(kLiteRtElementTypeInt32, {2}));
685+
op.Inputs().push_back(&tensor2);
686+
687+
auto& tensor3 = subgraph.EmplaceTensor();
688+
tensor3.SetType(MakeRankedTensorType(kLiteRtElementTypeInt32, {2, 2, 2}));
689+
op.Outputs().push_back(&tensor3);
690+
}
691+
692+
{
693+
auto& op = subgraph.EmplaceOp();
694+
695+
op.SetOpCode(kLiteRtOpCodeTflAdd);
696+
697+
::tflite::AddOptionsT add_opts;
698+
add_opts.fused_activation_function = ::tflite::ActivationFunctionType_RELU;
699+
add_opts.pot_scale_int16 = false;
700+
TflOptions opts;
701+
opts.type = ::tflite::BuiltinOptions_AddOptions;
702+
opts.Set(std::move(add_opts));
703+
litert::internal::SetTflOptions(op, std::move(opts));
704+
705+
auto& tensor = subgraph.EmplaceTensor();
706+
tensor.SetType(MakeRankedTensorType(kLiteRtElementTypeInt32, {2, 2, 2}));
707+
op.Inputs().push_back(&tensor);
708+
709+
auto& tensor2 = subgraph.EmplaceTensor();
710+
tensor2.SetType(MakeRankedTensorType(kLiteRtElementTypeInt32, {2}));
711+
op.Inputs().push_back(&tensor2);
712+
713+
auto& tensor3 = subgraph.EmplaceTensor();
714+
tensor3.SetType(MakeRankedTensorType(kLiteRtElementTypeInt32, {2, 2, 2}));
715+
op.Outputs().push_back(&tensor3);
716+
}
717+
718+
EXPECT_EQ(absl::StrFormat("%v", subgraph.Ops()),
719+
"tfl.add{fa=RELU}(3d_i32<2x2x2>,1d_i32<2>)->(3d_i32<2x2x2>)/"
720+
"tfl.add{fa=RELU}(3d_i32<2x2x2>,1d_i32<2>)->(3d_i32<2x2x2>)");
721+
}
722+
663723
} // namespace
664724
} // namespace litert::internal

0 commit comments

Comments
 (0)