Skip to content

Commit c4f7cdf

Browse files
YunanAZcopybara-github
authored andcommitted
Add Op index in IR for internal debugging.
LiteRT-PiperOrigin-RevId: 828803023
1 parent d05c94f commit c4f7cdf

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

litert/core/model/model.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ bool IsConstant(const LiteRtTensorT& tensor) {
477477
<< "Constant tensors should not be defined by an op";
478478
return is_const;
479479
}
480+
// TODO: reuse this in cc api.
481+
bool IsSubgraphInput(const LiteRtTensorT& tensor) {
482+
return !IsConstant(tensor) && tensor.DefiningOp() == nullptr;
483+
}
480484

481485
void AttachInput(LiteRtTensor tensor, LiteRtOpT& op) {
482486
op.Inputs().push_back(tensor);
@@ -532,7 +536,7 @@ bool IsCompiledOp(const LiteRtModelT& graph, LiteRtOpT& op) {
532536
// If there hasn't been a round of serialization,
533537
// since dispatches were added, they won't be in the code
534538
// table.
535-
// TODO: Fix this once the code table is updateded
539+
// TODO: Fix this once the code table is updated
536540
// dynamically.
537541
return litert::internal::GetTflOpCodeInd(op) ==
538542
litert::internal::kDispatchOpCodeTflInd ||

litert/core/model/model.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ class LiteRtOpT {
498498
// Get any custom options attached to this op. Empty if there are none.
499499
litert::BufferRef<uint8_t> CustomOptions() const { return custom_options_; }
500500

501+
// Op index. (For internal debugging only)
502+
void SetOpIndex(uint32_t op_index) { op_index_ = op_index; }
503+
uint32_t OpIndex() const { return op_index_; }
504+
501505
// Attach custom opaque optins to this op.
502506
template <class... Args>
503507
void SetCustomOptions(Args&&... args) {
@@ -573,6 +577,8 @@ class LiteRtOpT {
573577

574578
std::string custom_code_;
575579

580+
uint32_t op_index_;
581+
576582
// TFLITE
577583
int32_t tfl_op_code_ind_ = litert::internal::kDispatchOpCodeTflInd;
578584
::litert::internal::TflOptions tfl_option_;
@@ -1215,6 +1221,9 @@ UseIndices FindUseInds(const LiteRtTensorT& tensor, const LiteRtOpT& op);
12151221
// Is this tensor a constant tensor?
12161222
bool IsConstant(const LiteRtTensorT& tensor);
12171223

1224+
// Is this tensor a subgraph input tensor?
1225+
bool IsSubgraphInput(const LiteRtTensorT& tensor);
1226+
12181227
// MUTATORS
12191228

12201229
// Attaches the pre-allocated tensor to be an input of given op.

litert/core/model/model_load.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "litert/core/model/model_load.h"
1616

1717
#include <algorithm>
18+
#include <cstddef>
1819
#include <cstdint>
1920
#include <memory>
2021
#include <string>
@@ -25,7 +26,6 @@
2526
#include "absl/strings/string_view.h" // from @com_google_absl
2627
#include "litert/c/internal/litert_logging.h"
2728
#include "litert/c/litert_common.h"
28-
#include "litert/c/litert_model.h"
2929
#include "litert/c/litert_op_code.h"
3030
#include "litert/cc/litert_buffer_ref.h"
3131
#include "litert/cc/litert_expected.h"
@@ -90,7 +90,8 @@ class FlatbufferContext {
9090
};
9191

9292
LiteRtStatus UnpackOp(FlatbufferContext& context, LiteRtSubgraphT& parent,
93-
const TflPackedOp& tfl_op, LiteRtOpT& litert_op) {
93+
const TflPackedOp& tfl_op, LiteRtOpT& litert_op,
94+
size_t op_index) {
9495
// I/O TENSORS
9596

9697
if (tfl_op.intermediates() && tfl_op.intermediates()->size() != 0) {
@@ -145,6 +146,7 @@ LiteRtStatus UnpackOp(FlatbufferContext& context, LiteRtSubgraphT& parent,
145146
// OP CODE
146147

147148
LITERT_RETURN_IF_ERROR(context.SetOpCode(litert_op, tfl_op.opcode_index()));
149+
litert_op.SetOpIndex(op_index);
148150

149151
return kLiteRtStatusOk;
150152
}
@@ -275,7 +277,7 @@ LiteRtStatus UnpackSubgraph(FlatbufferContext& context,
275277
for (auto i = 0; i < num_ops; ++i) {
276278
const auto* tfl_op = tfl_subgraph.operators()->Get(i);
277279
LITERT_RETURN_IF_ERROR(UnpackOp(context, litert_subgraph, *tfl_op,
278-
litert_subgraph.EmplaceOp()));
280+
litert_subgraph.EmplaceOp(), i));
279281
}
280282

281283
// Update subgraph I/O.

0 commit comments

Comments
 (0)