Skip to content

Commit 55e6099

Browse files
terryheocopybara-github
authored andcommitted
CompiledModel: Hide Annotation API
- Hide the API from CompiledModel until it's needed for public use. - Introduced CompiledModelNext to support the feature. LiteRT-PiperOrigin-RevId: 825813579
1 parent cf52fe3 commit 55e6099

File tree

8 files changed

+612
-338
lines changed

8 files changed

+612
-338
lines changed

litert/cc/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,9 +653,11 @@ cc_test(
653653
"//litert/test:tflite_test_data",
654654
],
655655
deps = [
656+
":litert_common",
656657
":litert_compiled_model",
657658
":litert_element_type",
658659
":litert_environment",
660+
":litert_expected",
659661
":litert_layout",
660662
":litert_macros",
661663
":litert_model",
@@ -665,8 +667,10 @@ cc_test(
665667
":litert_tensor_buffer",
666668
":litert_tensor_buffer_types",
667669
"//litert/c:litert_common",
670+
"//litert/c:litert_compiled_model",
668671
"//litert/c:litert_tensor_buffer",
669672
"//litert/c:litert_tensor_buffer_types",
673+
"//litert/cc/internal:litert_handle",
670674
"//litert/cc/options:litert_runtime_options",
671675
"//litert/test:common",
672676
"//litert/test:matchers",

litert/cc/dynamic_runtime/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,7 @@ cc_test(
598598
"//litert/c:litert_tensor_buffer",
599599
"//litert/c:litert_tensor_buffer_types",
600600
"//litert/cc:litert_element_type",
601+
"//litert/cc:litert_expected",
601602
"//litert/cc:litert_layout",
602603
"//litert/cc:litert_macros",
603604
"//litert/cc:litert_ranked_tensor_type",

litert/cc/internal/BUILD

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,3 +475,65 @@ exports_files(glob([
475475
"*.h",
476476
"*.cc",
477477
]))
478+
479+
# -----------------------------------------------------------------------------
480+
481+
# Advanced CompiledModel with new / experimental features.
482+
cc_library(
483+
name = "litert_compiled_model_next",
484+
srcs = ["litert_compiled_model_next.cc"],
485+
hdrs = ["litert_compiled_model_next.h"],
486+
visibility = [
487+
# copybara:uncomment_begin(oss litert_lm)
488+
# "//platforms/darwinn:__subpackages__",
489+
# "//litert:__subpackages__",
490+
# copybara:uncomment_end_and_comment_begin
491+
"//visibility:public",
492+
# copybara:comment_end
493+
],
494+
deps = [
495+
":litert_handle",
496+
"//litert/c:litert_common",
497+
"//litert/c:litert_compiled_model",
498+
"//litert/c:litert_metrics",
499+
"//litert/cc:litert_common",
500+
"//litert/cc:litert_compiled_model",
501+
"//litert/cc:litert_environment",
502+
"//litert/cc:litert_expected",
503+
"//litert/cc:litert_macros",
504+
"//litert/cc:litert_model",
505+
"//litert/cc:litert_options",
506+
"@com_google_absl//absl/strings:string_view",
507+
],
508+
)
509+
510+
cc_test(
511+
name = "litert_compiled_model_next_test",
512+
srcs = ["litert_compiled_model_next_test.cc"],
513+
data = [
514+
"//litert/test:mlir_test_data",
515+
"//litert/test:tflite_test_data",
516+
],
517+
deps = [
518+
":litert_compiled_model_next",
519+
"//litert/c:litert_compiled_model",
520+
"//litert/c:litert_tensor_buffer",
521+
"//litert/cc:litert_common",
522+
"//litert/cc:litert_environment",
523+
"//litert/cc:litert_model",
524+
"//litert/cc:litert_options",
525+
"//litert/cc:litert_tensor_buffer",
526+
"//litert/cc:litert_tensor_buffer_types",
527+
"//litert/test:common",
528+
"//litert/test:matchers",
529+
"//litert/test:simple_model",
530+
"//tflite:framework",
531+
"//tflite/c:c_api_opaque",
532+
"//tflite/c:common",
533+
"//tflite/kernels:builtin_ops",
534+
"@com_google_absl//absl/log:absl_log",
535+
"@com_google_absl//absl/strings:string_view",
536+
"@com_google_absl//absl/types:span",
537+
"@com_google_googletest//:gtest_main",
538+
],
539+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2025 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "litert/cc/internal/litert_compiled_model_next.h"
16+
17+
#include "litert/c/litert_common.h"
18+
#include "litert/c/litert_compiled_model.h"
19+
#include "litert/cc/internal/litert_handle.h"
20+
#include "litert/cc/litert_common.h"
21+
#include "litert/cc/litert_environment.h"
22+
#include "litert/cc/litert_expected.h"
23+
#include "litert/cc/litert_macros.h"
24+
#include "litert/cc/litert_model.h"
25+
#include "litert/cc/litert_options.h"
26+
27+
namespace litert {
28+
29+
Expected<CompiledModelNext> CompiledModelNext::Create(
30+
litert::Environment& env, const litert::Model& model,
31+
litert::HwAccelerators hardware_accelerators) {
32+
LITERT_ASSIGN_OR_RETURN(auto compilation_options, Options::Create());
33+
compilation_options.SetHardwareAccelerators(hardware_accelerators);
34+
LiteRtModel litert_model = model.Get();
35+
LiteRtCompiledModel compiled_model;
36+
LITERT_RETURN_IF_ERROR(LiteRtCreateCompiledModel(
37+
env.Get(), litert_model, compilation_options.Get(), &compiled_model));
38+
return CompiledModelNext(litert_model, compiled_model, OwnHandle::kYes);
39+
}
40+
41+
} // namespace litert
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
// Copyright 2025 Google LLC.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef THIRD_PARTY_ODML_LITERT_LITERT_CC_INTERNAL_LITERT_COMPILED_MODEL_NEXT_H_
16+
#define THIRD_PARTY_ODML_LITERT_LITERT_CC_INTERNAL_LITERT_COMPILED_MODEL_NEXT_H_
17+
18+
#include <cstddef>
19+
#include <optional>
20+
#include <string>
21+
22+
#include "absl/strings/string_view.h" // from @com_google_absl
23+
#include "litert/c/litert_common.h"
24+
#include "litert/c/litert_compiled_model.h"
25+
#include "litert/cc/internal/litert_handle.h"
26+
#include "litert/cc/litert_common.h"
27+
#include "litert/cc/litert_compiled_model.h"
28+
#include "litert/cc/litert_environment.h"
29+
#include "litert/cc/litert_expected.h"
30+
#include "litert/cc/litert_macros.h"
31+
#include "litert/cc/litert_model.h"
32+
33+
namespace litert {
34+
35+
// Advanced CompiledModel with new / experimental features.
36+
class CompiledModelNext : public CompiledModel {
37+
public:
38+
static Expected<CompiledModelNext> Create(
39+
litert::Environment& env, const litert::Model& model,
40+
litert::HwAccelerators hardware_accelerators);
41+
42+
// Sets a dispatch annotation on the compiled model. These annotations will be
43+
// propagated to dispatch graphs when they are created during model execution.
44+
// The annotations provide runtime hints and metadata that can be used by
45+
// hardware accelerators for optimization.
46+
//
47+
// Parameters:
48+
// - signature_index: the index of the signature (zero-based).
49+
// - key: the annotation key.
50+
// - value: the annotation value.
51+
//
52+
// Example annotations:
53+
// - "priority": "high|medium|low" - execution priority hints
54+
// - "memory_type": "shared|dedicated" - memory allocation preferences
55+
// - "accelerator": "npu|gpu|dsp" - preferred hardware accelerator
56+
// - "precision": "fp32|fp16|int8" - computation precision requirements
57+
Expected<void> SetDispatchAnnotation(size_t signature_index,
58+
absl::string_view key,
59+
absl::string_view value) {
60+
LITERT_RETURN_IF_ERROR(LiteRtCompiledModelSetDispatchAnnotation(
61+
Get(), signature_index, key.data(), value.data()));
62+
return {};
63+
}
64+
65+
// Gets a dispatch annotation from the compiled model.
66+
//
67+
// Parameters:
68+
// - signature_index: the index of the signature (zero-based).
69+
// - key: the annotation key to look up.
70+
//
71+
// Returns:
72+
// - The annotation value if found, or nullopt if the key doesn't exist.
73+
Expected<std::optional<std::string>> GetDispatchAnnotation(
74+
size_t signature_index, absl::string_view key) {
75+
const char* value = nullptr;
76+
LITERT_RETURN_IF_ERROR(LiteRtCompiledModelGetDispatchAnnotation(
77+
Get(), signature_index, key.data(), &value));
78+
if (value == nullptr) {
79+
return Expected<std::optional<std::string>>(std::nullopt);
80+
}
81+
return Expected<std::optional<std::string>>(std::string(value));
82+
}
83+
84+
// Removes a dispatch annotation from the compiled model.
85+
//
86+
// Parameters:
87+
// - signature_index: the index of the signature (zero-based).
88+
// - key: the annotation key to remove.
89+
//
90+
// Note: This function succeeds even if the key doesn't exist.
91+
Expected<void> RemoveDispatchAnnotation(size_t signature_index,
92+
absl::string_view key) {
93+
LITERT_RETURN_IF_ERROR(LiteRtCompiledModelRemoveDispatchAnnotation(
94+
Get(), signature_index, key.data()));
95+
return {};
96+
}
97+
98+
// Overloaded version for the default signature (index 0).
99+
Expected<void> SetDispatchAnnotation(absl::string_view key,
100+
absl::string_view value) {
101+
return SetDispatchAnnotation(0, key, value);
102+
}
103+
104+
// Overloaded version for the default signature (index 0).
105+
Expected<std::optional<std::string>> GetDispatchAnnotation(
106+
absl::string_view key) {
107+
return GetDispatchAnnotation(0, key);
108+
}
109+
110+
// Overloaded version for the default signature (index 0).
111+
Expected<void> RemoveDispatchAnnotation(absl::string_view key) {
112+
return RemoveDispatchAnnotation(0, key);
113+
}
114+
115+
// Overloaded version that takes a signature name instead of index.
116+
Expected<void> SetDispatchAnnotation(absl::string_view signature_name,
117+
absl::string_view key,
118+
absl::string_view value) {
119+
LITERT_ASSIGN_OR_RETURN(size_t signature_index,
120+
model_.GetSignatureIndex(signature_name));
121+
return SetDispatchAnnotation(signature_index, key, value);
122+
}
123+
124+
// Overloaded version that takes a signature name instead of index.
125+
Expected<std::optional<std::string>> GetDispatchAnnotation(
126+
absl::string_view signature_name, absl::string_view key) {
127+
LITERT_ASSIGN_OR_RETURN(size_t signature_index,
128+
model_.GetSignatureIndex(signature_name));
129+
return GetDispatchAnnotation(signature_index, key);
130+
}
131+
132+
// Overloaded version that takes a signature name instead of index.
133+
Expected<void> RemoveDispatchAnnotation(absl::string_view signature_name,
134+
absl::string_view key) {
135+
LITERT_ASSIGN_OR_RETURN(size_t signature_index,
136+
model_.GetSignatureIndex(signature_name));
137+
return RemoveDispatchAnnotation(signature_index, key);
138+
}
139+
140+
private:
141+
explicit CompiledModelNext(LiteRtModel litert_model,
142+
LiteRtCompiledModel compiled_model,
143+
OwnHandle owned)
144+
: CompiledModel(litert_model, compiled_model, owned) {}
145+
};
146+
147+
} // namespace litert
148+
149+
#endif // THIRD_PARTY_ODML_LITERT_LITERT_CC_INTERNAL_LITERT_COMPILED_MODEL_NEXT_H_

0 commit comments

Comments
 (0)