Conversation
…6_bit_precision (apple#7) Add ignored_ops parameter to cast_fp32_to_fp16 and cast_to_16_bit_precision, allowing callers to exclude operations with high-dynamic-range activations (such as exp, softplus, logsumexp, or mish) from FP16 casting to prevent overflow to infinity. - Add is_ignored_op helper in _utils.casting_utils supporting OpOverload, OpOverloadPacket, callables, string op names, and FX node names. - Route ignored ops in _FP16Casting directly to handle_overflow_op, keeping them in FP32 with automatic input upcasts and output downcasts. - Automatic cast cleanup collapses redundant intermediate casts between consecutive ignored ops. - Add unit tests in test_casting_utils.py and end-to-end tests in test_casting.py. - Add documentation in docs/src/utils/casting.md and Towncrier fragment 7.added.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds an
ignored_opsparameter tocast_fp32_to_fp16andcast_to_16_bit_precision, allowing users to specify operations that should be preserved intorch.float32during 16-bit casting. This resolves #7 by providing a deterministic mechanism to prevent intermediate activation-level overflow in operations such asexp,logsumexp,softplus, ormish.Motivation & Background
As discussed in #7, IEEE 754 half-precision float ($\text{Softplus}(x) = \ln(1 + \exp(x))$ , $\text{LogSumExp}$ , or $\text{Mish}$ ) exceed this limit whenever intermediate activations $x > 11.0$ (e.g. $\exp(15.0) \approx 3.27 \times 10^6$ ), causing the operation to evaluate to $+\infty$ . On hardware backends like Apple Silicon / ANE, this overflow causes numerical collapse downstream into
FP16) has a maximum finite limit of65,504. Neural network activation functions involving exponentials (such as0.0orNaN.Because
cast_fp32_to_fp16performs graph transformations prior to runtime without inspecting dynamic activation magnitudes, it eagerly converts these compute ops totorch.float16. Following maintainer guidance from @crowbat in #7 (Direction 2: "allowing the user to specify specific ops or op types to ignore being casted"), this PR enables users to selectively skip casting for sensitive operations.Changes
Selective Op Identification (
is_ignored_op):is_ignored_opincoreai_opt._utils.casting_utils.torch._ops.OpOverload(e.g.torch.ops.aten.exp.default)torch._ops.OpOverloadPacket(e.g.torch.ops.aten.exp)torch.exp)"exp","aten.exp","aten::exp")"exp_1", targeting specific graph nodes)Casting Engine Integration (
_FP16Casting):_FP16Casting.iterate_nodes_and_insert_casts, checksis_ignored_op(node, self._ignored_ops)before creation and general compute ops.handle_overflow_op(node), which preserves the op intorch.float32, insertsFP16 -> FP32upcasts on any FP16 inputs, and insertsFP32 -> FP16downcasts on outputs for downstream consumers.explog1p) are automatically collapsed by the existing_cleanup_castsengine into a direct FP32 chain with zero overhead.Public API Updates:
ignored_ops: Sequence[Callable | str] | None = Nonetocast_fp32_to_fp16andcast_to_16_bit_precision.None.Testing & Documentation:
TestIsIgnoredOpunit tests intests/casting/test_casting_utils.pycovering all matching styles and edge cases.TestSelectiveOpSkippingend-to-end tests intests/casting/test_casting.pyverifying activation overflow prevention atcast_to_16_bit_precisionforwarding.docs/src/utils/casting.md.changelog.d/7.added.Verification
tests/casting/):Closes #7