import torch
import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        gate = torch.clamp(x / 2.0 + 0.5, min=0.0, max=1.0)

        positive_mask = x >= 0

        output = torch.empty_like(x)

        # x >= 0: x * gate
        output_pos = x.masked_select(positive_mask) * gate.masked_select(positive_mask)
        output.masked_scatter_(positive_mask, output_pos)

        # x < 0: (exp(x) - 1) * gate
        x_neg = x.masked_select(~positive_mask)
        gate_neg = gate.masked_select(~positive_mask)
        output_neg = (torch.exp(x_neg) - 1.0) * gate_neg
        output.masked_scatter_(~positive_mask, output_neg)

        return output


batch_size = 512
feature_dim = 1024


def get_inputs():
    x = torch.randn(batch_size, feature_dim, dtype=torch.float32)
    return [x]


def get_init_inputs():
    return []