Skip to content

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions onnxscript/function_libs/torch_lib/ops/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3538,10 +3538,51 @@ def aten_feature_alpha_dropout(input: TensorType, p: float, train: bool) -> Tens
raise NotImplementedError()


def aten_feature_dropout(input: TensorType, p: float, train: bool) -> TensorType:
@torch_op("aten::feature_dropout", trace_only=True)
def aten_feature_dropout(input: TFloat, p: FLOAT, train: BOOL) -> TFloat:
"""feature_dropout(Tensor input, float p, bool train) -> Tensor"""

raise NotImplementedError()
# Feature dropout applies dropout to entire feature maps/channels
# rather than individual elements

if p == 0 or not train:
return input

# Get input dimensions
ndim = op.Size(op.Shape(input))

# Create mask shape for feature dropout
# For 2D tensors [N, C]: mask shape is [N, C]
# For higher dim tensors [N, C, ...]: mask shape is [N, C, 1, 1, ...]
batch_size = op.Shape(input, start=0, end=1)
channel_size = op.Shape(input, start=1, end=2)

# Create the appropriate mask shape based on tensor dimensions
is_2d = op.Equal(ndim, 2)

# For 2D case, mask_shape = [N, C]
mask_shape_2d = op.Concat(batch_size, channel_size, axis=0)

# For higher dimensions, mask_shape = [N, C, 1, 1, ...]
spatial_dims_count = op.Sub(ndim, 2)
ones_for_spatial = op.ConstantOfShape(
op.Reshape(spatial_dims_count, [1]),
value=1
)
mask_shape_nd = op.Concat(batch_size, channel_size, ones_for_spatial, axis=0)

# Select appropriate mask shape
mask_shape = op.Where(is_2d, mask_shape_2d, mask_shape_nd)

# Create a dummy tensor of ones with the mask shape and apply dropout to it
# This leverages op.Dropout to handle training mode, scaling, and random generation
dummy_tensor = op.ConstantOfShape(mask_shape, value=1.0)
mask, _ = op.Dropout(dummy_tensor, p, train)

# Apply mask to input (broadcasting will handle different shapes)
result = op.Mul(input, mask)

return result


@torch_op(("aten::fill.Tensor", "aten::fill.Scalar"))
Expand Down