-
Notifications
You must be signed in to change notification settings - Fork 70
Implement aten::feature_dropout #2404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: justinchuby <[email protected]>
random_vals = op.RandomUniformLike( | ||
op.ConstantOfShape(mask_shape, value=0.0), | ||
dtype=1, # float32 | ||
low=0.0, | ||
high=1.0 | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use op.Dropout for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion! I've refactored the implementation to use op.Dropout
instead of manual random generation. The new approach creates a dummy tensor with the appropriate feature dropout shape [N, C, 1, 1, ...]
, applies op.Dropout
to it to get the scaled mask, then multiplies with the input. This is much cleaner and leverages the existing ONNX Dropout operation for all the logic (training mode, scaling, random generation). Commit: 29f1968
|
||
# Use ONNX operations to handle control flow | ||
# In inference mode or when p=0, return input unchanged | ||
should_dropout = op.And(train, p > 0.0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do
if p == 0 or not train:
return input
# Do this other thing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added early return conditions as requested. The function now returns input directly when p == 0 or not train, avoiding unnecessary computations. Commit: af8d622
…ation Co-authored-by: justinchuby <[email protected]>
Co-authored-by: justinchuby <[email protected]>
batch_size = op.Gather(input_shape, [0]) | ||
channel_size = op.Gather(input_shape, [1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
batch_size = op.Gather(input_shape, [0]) | |
channel_size = op.Gather(input_shape, [1]) | |
batch_size = op.Shape(input, start=0, end=0) | |
batch_size = op.Shape(input, start=1, end=1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to use op.Shape
with start/end parameters for more efficient shape extraction. Fixed the typo in the suggestion where both lines assigned to batch_size
. Commit: 96f78cd
…stead of op.Gather Co-authored-by: justinchuby <[email protected]>
This PR implements the missing
aten::feature_dropout
operation inonnxscript/function_libs/torch_lib/ops/core.py
.What is feature dropout?
Feature dropout (also known as channel dropout or spatial dropout) is a regularization technique that differs from regular dropout by dropping entire feature maps/channels rather than individual elements:
p
p
This is particularly useful in convolutional neural networks as it encourages the network not to rely too heavily on specific feature detectors while maintaining spatial correlations within each feature map.
Implementation Details
The implementation:
@torch_op("aten::feature_dropout", trace_only=True)
[N, C]
: creates mask of shape[N, C]
[N, C, H, W, ...]
: creates mask of shape[N, C, 1, 1, ...]
for broadcastingtrain=False
): returns input unchangedp=0
): returns input unchanged1/(1-p)
Key ONNX Operations Used
RandomUniformLike
for generating random valuesWhere
for conditional logicConstantOfShape
for creating appropriately shaped tensorsShape
,Size
,Gather
,Concat
for dynamic shape handlingValidation
torch.ops.aten.feature_dropout
reference implementationFixes #2403.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.