Skip to content

Commit 5c2c30e

Browse files
ai-edge-botcopybara-github
authored andcommitted
Support passing xnnpack weight cache by file descriptor in LiteRT's CpuOptions.
LiteRT-PiperOrigin-RevId: 818880466
1 parent 1bb6729 commit 5c2c30e

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed

litert/c/options/litert_cpu_options.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ LiteRtStatus LiteRtSetCpuOptionsXnnPackWeightCachePath(LiteRtCpuOptions options,
9191
const char* path) {
9292
LITERT_RETURN_IF_ERROR(options, litert::ErrorStatusBuilder::InvalidArgument())
9393
<< "options is null.";
94+
LITERT_RETURN_IF_ERROR(options->xnn.weight_cache_file_descriptor <= 0,
95+
litert::ErrorStatusBuilder::InvalidArgument())
96+
<< "weight cache file descriptor and path cannot both be set.";
9497
options->xnn.weight_cache_file_path = path;
9598
return kLiteRtStatusOk;
9699
}
@@ -104,3 +107,24 @@ LiteRtStatus LiteRtGetCpuOptionsXnnPackWeightCachePath(
104107
*path = options->xnn.weight_cache_file_path;
105108
return kLiteRtStatusOk;
106109
}
110+
111+
LiteRtStatus LiteRtSetCpuOptionsXnnPackWeightCacheFileDescriptor(
112+
LiteRtCpuOptions options, int fd) {
113+
LITERT_RETURN_IF_ERROR(options, litert::ErrorStatusBuilder::InvalidArgument())
114+
<< "options is null.";
115+
LITERT_RETURN_IF_ERROR(options->xnn.weight_cache_file_path == nullptr,
116+
litert::ErrorStatusBuilder::InvalidArgument())
117+
<< "weight cache file descriptor and path cannot both be set.";
118+
options->xnn.weight_cache_file_descriptor = fd;
119+
return kLiteRtStatusOk;
120+
}
121+
122+
LiteRtStatus LiteRtGetCpuOptionsXnnPackWeightCacheFileDescriptor(
123+
LiteRtCpuOptionsConst options, int* const fd) {
124+
LITERT_RETURN_IF_ERROR(options, litert::ErrorStatusBuilder::InvalidArgument())
125+
<< "options is null.";
126+
LITERT_RETURN_IF_ERROR(fd, litert::ErrorStatusBuilder::InvalidArgument())
127+
<< "fd is null.";
128+
*fd = options->xnn.weight_cache_file_descriptor;
129+
return kLiteRtStatusOk;
130+
}

litert/c/options/litert_cpu_options.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ LiteRtStatus LiteRtGetCpuOptionsXNNPackFlags(LiteRtCpuOptionsConst options,
5353
uint32_t* flags);
5454

5555
// Sets the XNNPack weight cache file path used by the CPU accelerator.
56+
// Weight cache file path and descriptor must not both be set.
5657
// The `path` string is owned by the caller and must outlive the `options`
5758
// object.
5859
LiteRtStatus LiteRtSetCpuOptionsXnnPackWeightCachePath(LiteRtCpuOptions options,
@@ -64,6 +65,16 @@ LiteRtStatus LiteRtSetCpuOptionsXnnPackWeightCachePath(LiteRtCpuOptions options,
6465
LiteRtStatus LiteRtGetCpuOptionsXnnPackWeightCachePath(
6566
LiteRtCpuOptionsConst options, const char** path);
6667

68+
// Sets the XNNPack weight cache file descriptor used by the CPU accelerator.
69+
// Weight cache file path and descriptor must not both be set.
70+
LiteRtStatus LiteRtSetCpuOptionsXnnPackWeightCacheFileDescriptor(
71+
LiteRtCpuOptions options, int fd);
72+
73+
// Gets the XNNPack weight cache file descriptor used by the CPU accelerator.
74+
LiteRtStatus LiteRtGetCpuOptionsXnnPackWeightCacheFileDescriptor(
75+
LiteRtCpuOptionsConst options, int* fd);
76+
77+
6778
#ifdef __cplusplus
6879
} // extern "C"
6980
#endif

litert/c/options/litert_cpu_options_test.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,4 +207,33 @@ TEST_F(LiteRtCpuOptionsFieldsTest,
207207
IsError(kLiteRtStatusErrorInvalidArgument));
208208
}
209209

210+
TEST_F(LiteRtCpuOptionsFieldsTest, SetAndGetXNNPackWeightCacheDescriptor) {
211+
const int expected_fd = 1234;
212+
int fd;
213+
214+
// Avoid a no-op test.
215+
LITERT_EXPECT_OK(
216+
LiteRtGetCpuOptionsXnnPackWeightCacheFileDescriptor(cpu_options_, &fd));
217+
ASSERT_NE(fd, expected_fd);
218+
219+
// Actual test.
220+
LITERT_EXPECT_OK(LiteRtSetCpuOptionsXnnPackWeightCacheFileDescriptor(
221+
cpu_options_, expected_fd));
222+
LITERT_EXPECT_OK(
223+
LiteRtGetCpuOptionsXnnPackWeightCacheFileDescriptor(cpu_options_, &fd));
224+
ASSERT_EQ(fd, expected_fd);
225+
}
226+
227+
TEST_F(LiteRtCpuOptionsFieldsTest,
228+
SetXNNPackWeightCacheFailsIfBothPathAndDescriptorAreSet) {
229+
const absl::string_view path = "a/path/to/the/cache";
230+
const int fd = 1234;
231+
232+
LITERT_EXPECT_OK(
233+
LiteRtSetCpuOptionsXnnPackWeightCachePath(cpu_options_, path.data()));
234+
EXPECT_THAT(
235+
LiteRtSetCpuOptionsXnnPackWeightCacheFileDescriptor(cpu_options_, fd),
236+
IsError(kLiteRtStatusErrorInvalidArgument));
237+
}
238+
210239
} // namespace

litert/cc/options/litert_cpu_options.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,21 @@ Expected<absl::string_view> CpuOptions::GetXNNPackWeightCachePath() const {
9696
return absl::NullSafeStringView(path);
9797
}
9898

99+
Expected<void> CpuOptions::SetXNNPackWeightCacheFileDescriptor(int fd) {
100+
LiteRtCpuOptions cpu_options;
101+
LITERT_RETURN_IF_ERROR(LiteRtFindCpuOptions(Get(), &cpu_options));
102+
LITERT_RETURN_IF_ERROR(
103+
LiteRtSetCpuOptionsXnnPackWeightCacheFileDescriptor(cpu_options, fd));
104+
return {};
105+
}
106+
107+
Expected<int> CpuOptions::GetXNNPackWeightCacheFileDescriptor() const {
108+
LiteRtCpuOptions cpu_options;
109+
LITERT_RETURN_IF_ERROR(LiteRtFindCpuOptions(Get(), &cpu_options));
110+
int fd;
111+
LITERT_RETURN_IF_ERROR(
112+
LiteRtGetCpuOptionsXnnPackWeightCacheFileDescriptor(cpu_options, &fd));
113+
return fd;
114+
}
115+
99116
} // namespace litert

litert/cc/options/litert_cpu_options.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ class CpuOptions : public OpaqueOptions {
4040

4141
Expected<void> SetXNNPackWeightCachePath(const char* path);
4242
Expected<absl::string_view> GetXNNPackWeightCachePath() const;
43+
44+
Expected<void> SetXNNPackWeightCacheFileDescriptor(int fd);
45+
Expected<int> GetXNNPackWeightCacheFileDescriptor() const;
4346
};
4447

4548
} // namespace litert

litert/cc/options/litert_cpu_options_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ TEST(CpuOptions, SetAndGetXNNPackWeighCachePathWorks) {
9797
IsOkAndHolds(StrEq("a/path")));
9898
}
9999

100+
TEST(CpuOptions, CheckXNNPackWeightCacheFileDescriptorDefaultValue) {
101+
LITERT_ASSERT_OK_AND_ASSIGN(CpuOptions options, CpuOptions::Create());
102+
EXPECT_THAT(options.GetXNNPackWeightCacheFileDescriptor(),
103+
IsOkAndHolds(-1));
104+
}
105+
106+
TEST(CpuOptions, SetAndGetXNNPackWeighCacheFileDescriptorWorks) {
107+
LITERT_ASSERT_OK_AND_ASSIGN(CpuOptions options, CpuOptions::Create());
108+
109+
LITERT_EXPECT_OK(options.SetXNNPackWeightCacheFileDescriptor(1234));
110+
EXPECT_THAT(options.GetXNNPackWeightCacheFileDescriptor(),
111+
IsOkAndHolds(1234));
112+
}
113+
100114
TEST(CpuOptions, CheckXNNPackFlagsDefaultValue) {
101115
LITERT_ASSERT_OK_AND_ASSIGN(CpuOptions options, CpuOptions::Create());
102116
// Note: we can't check the default value for this as XNNPack compile options

0 commit comments

Comments
 (0)