Skip to content

Commit 9e9aab6

Browse files
Merge pull request #4224 from neuropilot-spiderman:upstream_1017
LiteRT-PiperOrigin-RevId: 825744484
2 parents c1fdd5c + 4fd2a9e commit 9e9aab6

File tree

8 files changed

+285
-0
lines changed

8 files changed

+285
-0
lines changed

litert/vendors/mediatek/compiler/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ cc_library(
8787
"//litert/vendors/mediatek/compiler/legalizations:rms_norm_op_legalization",
8888
"//litert/vendors/mediatek/compiler/legalizations:rsqrt_op_legalization",
8989
"//litert/vendors/mediatek/compiler/legalizations:softmax_op_legalization",
90+
"//litert/vendors/mediatek/compiler/legalizations:split_op_legalization",
9091
"//litert/vendors/mediatek/compiler/legalizations:squared_difference_op_legalization",
92+
"//litert/vendors/mediatek/compiler/legalizations:strided_slice_op_legalization",
9193
"//litert/vendors/mediatek/compiler/legalizations:sub_op_legalization",
9294
"//litert/vendors/mediatek/compiler/legalizations:transpose_conv_op_legalization",
9395
"//litert/vendors/mediatek/compiler/legalizations:transpose_op_legalization",

litert/vendors/mediatek/compiler/compiler_plugin.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ constexpr LiteRtOpCode kSupportedOps[] = {
9898
kLiteRtOpCodeTflConcatenation,
9999
kLiteRtOpCodeTflQuantize,
100100
kLiteRtOpCodeTflSlice,
101+
kLiteRtOpCodeTflStridedSlice,
102+
kLiteRtOpCodeTflSplit,
101103
kLiteRtOpCodeTflSub,
102104
kLiteRtOpCodeTflTanh,
103105
kLiteRtOpCodeTflSoftmax,

litert/vendors/mediatek/compiler/create_model.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@
4545
#include "litert/vendors/mediatek/compiler/legalizations/rms_norm_op_legalization.h"
4646
#include "litert/vendors/mediatek/compiler/legalizations/rsqrt_op_legalization.h"
4747
#include "litert/vendors/mediatek/compiler/legalizations/softmax_op_legalization.h"
48+
#include "litert/vendors/mediatek/compiler/legalizations/split_op_legalization.h"
4849
#include "litert/vendors/mediatek/compiler/legalizations/squared_difference_op_legalization.h"
50+
#include "litert/vendors/mediatek/compiler/legalizations/strided_slice_op_legalization.h"
4951
#include "litert/vendors/mediatek/compiler/legalizations/sub_op_legalization.h"
5052
#include "litert/vendors/mediatek/compiler/legalizations/transpose_conv_op_legalization.h"
5153
#include "litert/vendors/mediatek/compiler/legalizations/transpose_op_legalization.h"
@@ -137,6 +139,13 @@ Expected<void> CreateModel(const NeuronAdapterApi& neuron_adapter_api,
137139
status = LegalizeCommonOp(neuron_adapter_api, model, *operand_map, op,
138140
NEURON_SLICE);
139141
break;
142+
case kLiteRtOpCodeTflStridedSlice:
143+
status =
144+
LegalizeStridedSliceOp(neuron_adapter_api, model, *operand_map, op);
145+
break;
146+
case kLiteRtOpCodeTflSplit:
147+
status = LegalizeSplitOp(neuron_adapter_api, model, *operand_map, op);
148+
break;
140149
case kLiteRtOpCodeTflTanh:
141150
status = LegalizeCommonOp(neuron_adapter_api, model, *operand_map, op,
142151
NEURON_TANH);

litert/vendors/mediatek/compiler/legalizations/BUILD

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,41 @@ cc_library(
454454
"//litert/vendors/mediatek:neuron_adapter_api",
455455
],
456456
)
457+
458+
cc_library(
459+
name = "strided_slice_op_legalization",
460+
srcs = ["strided_slice_op_legalization.cc"],
461+
hdrs = ["strided_slice_op_legalization.h"],
462+
tags = [
463+
# Don't build/test in OS until MediaTek SDK is available.
464+
"nobuilder",
465+
"notap",
466+
],
467+
deps = [
468+
":operand_map",
469+
"//litert/c:litert_runtime_c_api_shared_lib",
470+
"//litert/c/internal:litert_logging",
471+
"//litert/cc:litert_expected",
472+
"//litert/cc/dynamic_runtime:litert_model",
473+
"//litert/vendors/mediatek:neuron_adapter_api",
474+
],
475+
)
476+
477+
cc_library(
478+
name = "split_op_legalization",
479+
srcs = ["split_op_legalization.cc"],
480+
hdrs = ["split_op_legalization.h"],
481+
tags = [
482+
# Don't build/test in OS until MediaTek SDK is available.
483+
"nobuilder",
484+
"notap",
485+
],
486+
deps = [
487+
":operand_map",
488+
"//litert/c:litert_runtime_c_api_shared_lib",
489+
"//litert/c/internal:litert_logging",
490+
"//litert/cc:litert_expected",
491+
"//litert/cc/dynamic_runtime:litert_model",
492+
"//litert/vendors/mediatek:neuron_adapter_api",
493+
],
494+
)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright (c) 2025 MediaTek Inc.
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/vendors/mediatek/compiler/legalizations/split_op_legalization.h"
16+
17+
#include <cstdint>
18+
#include <vector>
19+
20+
#include "litert/c/internal/litert_logging.h"
21+
#include "litert/c/litert_common.h"
22+
#include "litert/c/litert_op_options.h"
23+
#include "litert/cc/litert_expected.h"
24+
#include "litert/cc/litert_model.h"
25+
#include "litert/vendors/mediatek/compiler/legalizations/operand_map.h"
26+
#include "litert/vendors/mediatek/neuron_adapter_api.h"
27+
28+
namespace litert::mediatek {
29+
30+
Expected<void> LegalizeSplitOp(const NeuronAdapterApi& neuron_adapter_api,
31+
NeuronModel* model, OperandMap& operand_map,
32+
const litert::Op& op) {
33+
std::vector<uint32_t> input_indices;
34+
35+
// op.Inputs()[1] is the input data
36+
LITERT_ASSIGN_OR_RETURN(auto input_tensor_id,
37+
operand_map.GetOperandIndex(op.Inputs()[1]));
38+
input_indices.push_back(input_tensor_id);
39+
40+
// op.Inputs()[0] is the axis
41+
LITERT_ASSIGN_OR_RETURN(auto axis_tensor_id,
42+
operand_map.GetOperandIndex(op.Inputs()[0]));
43+
input_indices.push_back(axis_tensor_id);
44+
45+
int32_t num_splits;
46+
if (auto status = LiteRtGetSplitNumSplitsOption(op.Get(), &num_splits);
47+
status != kLiteRtStatusOk) {
48+
return Error(status, "Failed to get LiteRtGetSplitNumSplitsOption");
49+
}
50+
auto num_splits_operand_index = operand_map.AddScalarInt32(num_splits);
51+
if (!num_splits_operand_index) {
52+
return num_splits_operand_index.Error();
53+
}
54+
input_indices.push_back(*num_splits_operand_index);
55+
56+
std::vector<uint32_t> output_indices;
57+
for (auto& output : op.Outputs()) {
58+
auto id = operand_map.GetOperandIndex(output);
59+
if (!id) {
60+
return id.Error();
61+
}
62+
output_indices.push_back(*id);
63+
}
64+
65+
if (ModelAddOperation(neuron_adapter_api, model, NEURON_SPLIT, input_indices,
66+
output_indices) != NEURON_NO_ERROR) {
67+
return Error(kLiteRtStatusErrorRuntimeFailure, "Failed to add operation");
68+
}
69+
70+
return {};
71+
}
72+
73+
} // namespace litert::mediatek
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2025 MediaTek Inc.
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 ODML_LITERT_LITERT_VENDORS_MEDIATEK_COMPILER_LEGALIZATIONS_SPLIT_OP_LEGALIZATION_H_
16+
#define ODML_LITERT_LITERT_VENDORS_MEDIATEK_COMPILER_LEGALIZATIONS_SPLIT_OP_LEGALIZATION_H_
17+
18+
#include "litert/c/litert_common.h"
19+
#include "litert/cc/litert_expected.h"
20+
#include "litert/cc/litert_model.h"
21+
#include "litert/vendors/mediatek/compiler/legalizations/operand_map.h"
22+
#include "litert/vendors/mediatek/neuron_adapter_api.h"
23+
24+
namespace litert::mediatek {
25+
26+
Expected<void> LegalizeSplitOp(const NeuronAdapterApi& neuron_adapter_api,
27+
NeuronModel* model, OperandMap& operand_map,
28+
const litert::Op& op);
29+
30+
} // namespace litert::mediatek
31+
32+
#endif // ODML_LITERT_LITERT_VENDORS_MEDIATEK_COMPILER_LEGALIZATIONS_SPLIT_OP_LEGALIZATION_H_
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2025 MediaTek Inc.
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/vendors/mediatek/compiler/legalizations/strided_slice_op_legalization.h"
16+
17+
#include <cstdint>
18+
#include <vector>
19+
20+
#include "litert/c/internal/litert_logging.h"
21+
#include "litert/c/litert_common.h"
22+
#include "litert/c/litert_op_options.h"
23+
#include "litert/cc/litert_expected.h"
24+
#include "litert/cc/litert_model.h"
25+
#include "litert/vendors/mediatek/compiler/legalizations/operand_map.h"
26+
#include "litert/vendors/mediatek/neuron_adapter_api.h"
27+
28+
namespace litert::mediatek {
29+
30+
Expected<void> LegalizeStridedSliceOp(
31+
const NeuronAdapterApi& neuron_adapter_api, NeuronModel* model,
32+
OperandMap& operand_map, const litert::Op& op) {
33+
std::vector<uint32_t> input_indices;
34+
for (auto& input : op.Inputs()) {
35+
auto id = operand_map.GetOperandIndex(input);
36+
if (!id) {
37+
return id.Error();
38+
}
39+
input_indices.push_back(*id);
40+
}
41+
42+
int32_t begin_mask;
43+
if (auto status = LiteRtGetStridedSliceBeginMaskOption(op.Get(), &begin_mask);
44+
status != kLiteRtStatusOk) {
45+
return Error(status, "Failed to get LiteRtGetStridedSliceBeginMaskOption");
46+
}
47+
auto begin_mask_operand_index = operand_map.AddScalarInt32(begin_mask);
48+
if (!begin_mask_operand_index) {
49+
return begin_mask_operand_index.Error();
50+
}
51+
input_indices.push_back(*begin_mask_operand_index);
52+
53+
int32_t end_mask;
54+
if (auto status = LiteRtGetStridedSliceEndMaskOption(op.Get(), &end_mask);
55+
status != kLiteRtStatusOk) {
56+
return Error(status, "Failed to get LiteRtGetStridedSliceEndMaskOption");
57+
}
58+
59+
auto end_mask_operand_index = operand_map.AddScalarInt32(end_mask);
60+
if (!end_mask_operand_index) {
61+
return end_mask_operand_index.Error();
62+
}
63+
input_indices.push_back(*end_mask_operand_index);
64+
65+
int32_t shrink_axis_mask;
66+
if (auto status = LiteRtGetStridedSliceShrinkAxisMaskOption(
67+
op.Get(), &shrink_axis_mask);
68+
status != kLiteRtStatusOk) {
69+
return Error(status,
70+
"Failed to get LiteRtGetStridedSliceShrinkAxisMaskOption");
71+
}
72+
73+
auto shrink_axis_mask_operand_index =
74+
operand_map.AddScalarInt32(shrink_axis_mask);
75+
if (!shrink_axis_mask_operand_index) {
76+
return shrink_axis_mask_operand_index.Error();
77+
}
78+
input_indices.push_back(*shrink_axis_mask_operand_index);
79+
80+
std::vector<uint32_t> output_indices;
81+
for (auto& output : op.Outputs()) {
82+
auto id = operand_map.GetOperandIndex(output);
83+
if (!id) {
84+
return id.Error();
85+
}
86+
output_indices.push_back(*id);
87+
}
88+
89+
if (ModelAddOperation(neuron_adapter_api, model, NEURON_STRIDED_SLICE,
90+
input_indices, output_indices) != NEURON_NO_ERROR) {
91+
return Error(kLiteRtStatusErrorRuntimeFailure, "Failed to add operation");
92+
}
93+
94+
return {};
95+
}
96+
97+
} // namespace litert::mediatek
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2025 MediaTek Inc.
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 ODML_LITERT_LITERT_VENDORS_MEDIATEK_COMPILER_LEGALIZATIONS_STRIDED_SLICE_OP_LEGALIZATION_H_
16+
#define ODML_LITERT_LITERT_VENDORS_MEDIATEK_COMPILER_LEGALIZATIONS_STRIDED_SLICE_OP_LEGALIZATION_H_
17+
18+
#include "litert/c/litert_common.h"
19+
#include "litert/cc/litert_expected.h"
20+
#include "litert/cc/litert_model.h"
21+
#include "litert/vendors/mediatek/compiler/legalizations/operand_map.h"
22+
#include "litert/vendors/mediatek/neuron_adapter_api.h"
23+
24+
namespace litert::mediatek {
25+
26+
Expected<void> LegalizeStridedSliceOp(
27+
const NeuronAdapterApi& neuron_adapter_api, NeuronModel* model,
28+
OperandMap& operand_map, const litert::Op& op);
29+
30+
} // namespace litert::mediatek
31+
32+
#endif // ODML_LITERT_LITERT_VENDORS_MEDIATEK_COMPILER_LEGALIZATIONS_STRIDED_SLICE_OP_LEGALIZATION_H_

0 commit comments

Comments
 (0)