Skip to content

Commit 99515aa

Browse files
jflan-ddaaronsky
andauthored
Fix missing origin for mlmodelc files (#2799)
Bundling a .mlmodel file into a resource bundle results in the following error: ``` File "/private/var/tmp/_bazel_johnflanagan/a29065da0d532a50315a8974596a3ed4/external/rules_apple+/apple/internal/testing/ios_rules.bzl", line 77, column 60, in _ios_unit_test_bundle_impl return apple_test_bundle_support.apple_test_bundle_impl( File "/private/var/tmp/_bazel_johnflanagan/a29065da0d532a50315a8974596a3ed4/external/rules_apple+/apple/internal/testing/apple_test_bundle_support.bzl", line 545, column 41, in _apple_test_bundle_impl processor_result = processor.process( File "/private/var/tmp/_bazel_johnflanagan/a29065da0d532a50315a8974596a3ed4/external/rules_apple+/apple/internal/processor.bzl", line 748, column 36, in _process partial_outputs = [partial.call(p) for p in partials] File "/private/var/tmp/_bazel_johnflanagan/a29065da0d532a50315a8974596a3ed4/external/bazel_skylib+/lib/partial.bzl", line 43, column 28, in _call return partial.function(*function_args, **function_kwargs) File "/private/var/tmp/_bazel_johnflanagan/a29065da0d532a50315a8974596a3ed4/external/rules_apple+/apple/internal/partials/resources.bzl", line 299, column 26, in _resources_partial_impl resources.deduplicate( File "/private/var/tmp/_bazel_johnflanagan/a29065da0d532a50315a8974596a3ed4/external/rules_apple+/apple/internal/resources.bzl", line 997, column 42, in _deduplicate deduplicated = _deduplicate_field( File "/private/var/tmp/_bazel_johnflanagan/a29065da0d532a50315a8974596a3ed4/external/rules_apple+/apple/internal/resources.bzl", line 942, column 49, in _deduplicate_field path_origins = processed_origins[short_path] Error: key "example.resource_bundle-intermediates/Example_Resources.bundle/FastViTT8F16.mlmodelc" not found in dictionary ``` I made an example repo to demonstrate the issue here: https://github.com/jflan-dd/coreml-FastViT-T8 This fix adds the `.mlmodel` to `.mlmodelc` mapping to `processed_origins` --------- Co-authored-by: Aaron Sky <aaronsky@skyaaron.com>
1 parent 34db795 commit 99515aa

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

apple/internal/partials/support/resources_support.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,8 @@ def _mlmodels(
471471
output_discriminator = output_discriminator,
472472
dir_name = paths.join(parent_dir or "", paths.replace_extension(basename, ".mlmodelc")),
473473
)
474+
processed_origins[output_bundle.short_path] = [file.short_path]
475+
474476
output_plist = intermediates.file(
475477
actions = actions,
476478
target_name = rule_label.name,

test/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ apple_shell_test(
7373
],
7474
)
7575

76+
apple_shell_test(
77+
name = "apple_core_ml_resource_test",
78+
size = "medium",
79+
src = "apple_core_ml_resource_test.sh",
80+
data = [
81+
"//test/testdata/resources:mlmodel_srcs",
82+
],
83+
)
84+
7685
apple_shell_test(
7786
name = "apple_intent_library_test",
7887
size = "medium",
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/bin/bash
2+
3+
# Copyright 2019 The Bazel Authors. All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -eu
18+
19+
# Integration tests for bundling CoreML models into resource bundles.
20+
21+
function set_up() {
22+
mkdir -p app
23+
}
24+
25+
function tear_down() {
26+
rm -rf app
27+
}
28+
29+
# Creates common source, targets, and basic plist for iOS applications.
30+
function create_common_files() {
31+
cat > app/BUILD <<EOF
32+
load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application")
33+
load(
34+
"@build_bazel_rules_apple//apple:resources.bzl",
35+
"apple_precompiled_resource_bundle",
36+
)
37+
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
38+
39+
apple_precompiled_resource_bundle(
40+
name = "app.resources",
41+
bundle_name = "App_Resources",
42+
resources = [
43+
"@build_bazel_rules_apple//test/testdata/resources:sample.mlpackage",
44+
],
45+
)
46+
47+
swift_library(
48+
name = "app_lib",
49+
data = ["app.resources"],
50+
srcs = ["AppDelegate.swift"],
51+
)
52+
53+
ios_application(
54+
name = "app",
55+
bundle_id = "my.bundle.id",
56+
families = ["iphone", "ipad"],
57+
infoplists = ["Info.plist"],
58+
minimum_os_version = "${MIN_OS_IOS}",
59+
deps = [":app_lib"],
60+
)
61+
EOF
62+
63+
cat > app/AppDelegate.swift <<EOF
64+
import UIKit
65+
66+
@UIApplicationMain
67+
class AppDelegate: UIResponder, UIApplicationDelegate {
68+
var window: UIWindow?
69+
}
70+
EOF
71+
72+
cat > app/Info.plist <<EOF
73+
{
74+
CFBundleIdentifier = "\${PRODUCT_BUNDLE_IDENTIFIER}";
75+
CFBundleName = "\${PRODUCT_NAME}";
76+
CFBundlePackageType = "APPL";
77+
CFBundleShortVersionString = "1.0";
78+
CFBundleVersion = "1.0";
79+
}
80+
EOF
81+
}
82+
83+
# Tests that the mlpackage is compiled correctly into a resource bundle
84+
function test_mlmodel_resource_bundle_builds() {
85+
create_common_files
86+
87+
do_build ios //app:app || fail "Should build"
88+
89+
assert_zip_contains "test-bin/app/app.ipa" "Payload/app.app/App_Resources.bundle/sample.mlmodelc/"
90+
}
91+
92+
run_suite "coreml resource bundle tests"

0 commit comments

Comments
 (0)