import torch
from torch._inductor.select_algorithm import extern_kernels
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime.triton_helpers import math as tl_math
from torch import nn
import torch.utils.data.distributed
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_div_mul_sigmoid_0(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tl_math.abs(tmp1)
    tmp4 = tl_math.abs(tmp3)
    tmp5 = tmp2 + tmp4
    tmp7 = tl_math.abs(tmp6)
    tmp8 = tmp5 + tmp7
    tmp10 = tl_math.abs(tmp9)
    tmp11 = tmp8 + tmp10
    tmp12 = tmp0 / tmp11
    tmp14 = tl.sigmoid(tmp13)
    tmp15 = tmp12 * tmp14
    tmp16 = 0.97
    tmp17 = tmp15 * tmp16
    tl.store(out_ptr0 + x2, tmp17, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 1), (1, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_div_mul_sigmoid_0[grid(16)](primals_1, primals_2,
            buf0, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_3, reinterpret_tensor(primals_4, (64, 
            4), (4, 1), 0), reinterpret_tensor(buf0, (4, 4), (1, 4), 0),
            alpha=1, beta=1, out=buf1)
        del buf0
        del primals_3
    return reinterpret_tensor(buf1, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), primals_1, primals_2, reinterpret_tensor(primals_4, (64, 4), (4,
        1), 0)


def _max_except_dim(input, dim):
    maxed = input
    for axis in range(input.ndimension() - 1, dim, -1):
        maxed, _ = maxed.max(axis, keepdim=True)
    for axis in range(dim - 1, -1, -1):
        maxed, _ = maxed.max(axis, keepdim=True)
    return maxed


def _norm_except_dim(w, norm_type, dim):
    if norm_type == 1 or norm_type == 2:
        return torch.norm_except_dim(w, norm_type, dim)
    elif norm_type == float('inf'):
        return _max_except_dim(w, dim)


def operator_norm_settings(domain, codomain):
    if domain == 1 and codomain == 1:
        max_across_input_dims = True
        norm_type = 1
    elif domain == 1 and codomain == 2:
        max_across_input_dims = True
        norm_type = 2
    elif domain == 1 and codomain == float('inf'):
        max_across_input_dims = True
        norm_type = float('inf')
    elif domain == 2 and codomain == float('inf'):
        max_across_input_dims = False
        norm_type = 2
    elif domain == float('inf') and codomain == float('inf'):
        max_across_input_dims = False
        norm_type = 1
    else:
        raise ValueError('Unknown combination of domain "{}" and codomain "{}"'
            .format(domain, codomain))
    return max_across_input_dims, norm_type


def _logit(p):
    p = torch.max(torch.ones(1) * 0.1, torch.min(torch.ones(1) * 0.9, p))
    return torch.log(p + 1e-10) + torch.log(1 - p + 1e-10)


class LipNormLinearNew(nn.Linear):
    """Lipschitz constant defined using operator norms."""

    def __init__(self, in_features, out_features, bias=True, coeff=0.97,
        domain=float('inf'), codomain=float('inf'), local_constraint=True,
        **unused_kwargs):
        del unused_kwargs
        super(LipNormLinearNew, self).__init__(in_features, out_features, bias)
        self.coeff = coeff
        self.domain = domain
        self.codomain = codomain
        self.local_constraint = local_constraint
        max_across_input_dims, self.norm_type = operator_norm_settings(self
            .domain, self.codomain)
        self.max_across_dim = 1 if max_across_input_dims else 0
        with torch.no_grad():
            w_scale = _norm_except_dim(self.weight, self.norm_type, dim=
                self.max_across_dim)
            if not self.local_constraint:
                w_scale = w_scale.max()
            self.scale = nn.Parameter(_logit(w_scale / self.coeff))

    def compute_weight(self):
        w_scale = _norm_except_dim(self.weight, self.norm_type, dim=self.
            max_across_dim)
        if not self.local_constraint:
            w_scale = w_scale.max()
        return self.weight / w_scale * torch.sigmoid(self.scale) * self.coeff

    def extra_repr(self):
        s = super(LipNormLinearNew, self).extra_repr()
        return s + ', coeff={}, domain={}, codomain={}, local={}'.format(self
            .coeff, self.domain, self.codomain, self.local_constraint)

    def forward(self, input_0):
        primals_1 = self.weight
        primals_3 = self.bias
        primals_2 = self.scale
        primals_4 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.014108