Skip to content

Commit e17915a

Browse files
kushvinthalejandro-isaza
authored andcommitted
Add --min-deployment-target argument for configurable iOS/macOS deployment targets
- Added --min-deployment-target argument with default value macOS13 for backward compatibility - Supports macOS13, macOS14, macOS15, iOS16, iOS17, iOS18 targets - Graceful fallback for newer targets not available in older coremltools versions - Enables targeting iOS 18 for advanced quantization features while maintaining compatibility - Includes comprehensive help text explaining usage and compatibility requirements
1 parent e5d960c commit e17915a

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

python_coreml_stable_diffusion/torch2coreml.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,11 @@ def _convert_to_coreml(submodule_name, torchscript_module, sample_inputs,
126126
coreml_model.compute_unit = compute_unit
127127
else:
128128
logger.info(f"Converting {submodule_name} to CoreML..")
129+
deployment_target = _get_deployment_target(args.min_deployment_target)
129130
coreml_model = ct.convert(
130131
torchscript_module,
131132
convert_to="mlprogram",
132-
minimum_deployment_target=ct.target.macOS13,
133+
minimum_deployment_target=deployment_target,
133134
inputs=_get_coreml_inputs(sample_inputs, args),
134135
outputs=[ct.TensorType(name=name, dtype=np.float32) for name in output_names],
135136
compute_units=compute_unit,
@@ -143,6 +144,41 @@ def _convert_to_coreml(submodule_name, torchscript_module, sample_inputs,
143144
return coreml_model, out_path
144145

145146

147+
def _get_deployment_target(target_string):
148+
"""
149+
Convert deployment target string to coremltools target object.
150+
151+
Args:
152+
target_string (str): Target deployment string (e.g., "macOS13", "iOS18")
153+
154+
Returns:
155+
coremltools target object
156+
"""
157+
target_map = {
158+
"macOS13": ct.target.macOS13,
159+
"macOS14": ct.target.macOS14,
160+
"iOS16": ct.target.iOS16,
161+
"iOS17": ct.target.iOS17,
162+
}
163+
164+
# Handle newer targets that might not be available in older coremltools versions
165+
try:
166+
if target_string == "macOS15":
167+
return ct.target.macOS15
168+
elif target_string == "iOS18":
169+
return ct.target.iOS18
170+
except AttributeError:
171+
logger.warning(f"Deployment target {target_string} not available in this coremltools version. "
172+
f"Using macOS14 as fallback.")
173+
return ct.target.macOS14
174+
175+
if target_string in target_map:
176+
return target_map[target_string]
177+
else:
178+
logger.warning(f"Unknown deployment target {target_string}. Using macOS13 as fallback.")
179+
return ct.target.macOS13
180+
181+
146182
def quantize_weights(args):
147183
""" Quantize weights to args.quantize_nbits using a palette (look-up table)
148184
"""
@@ -1718,6 +1754,16 @@ def parser_spec():
17181754
default=
17191755
"https://huggingface.co/google-t5/t5-small/resolve/main/tokenizer.json",
17201756
help="The URL to the merged pairs used in by the text tokenizer.")
1757+
parser.add_argument(
1758+
"--min-deployment-target",
1759+
default="macOS13",
1760+
help=(
1761+
"Minimum deployment target for Core ML models. "
1762+
"Valid options include macOS13, macOS14, macOS15, iOS16, iOS17, iOS18. "
1763+
"For iOS 18 compatibility with advanced quantization features, use iOS18. "
1764+
"Default is macOS13 for backwards compatibility."
1765+
)
1766+
)
17211767
parser.add_argument(
17221768
"--xl-version",
17231769
action="store_true",

0 commit comments

Comments
 (0)