import torch
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 libdevice
import torch.nn as nn
import torch.optim
import torch.utils.data
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_per_fused_add_div_mean_mul_sqrt_sub_var_0(in_out_ptr0,
    in_out_ptr1, in_out_ptr2, in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr1,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    x2 = xindex % 4
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp26 = tl.load(in_ptr1 + (r1 + 16 * x0), xmask, other=0.0)
    tmp48 = tl.load(in_ptr2 + x2, xmask, eviction_policy='evict_last')
    tmp50 = tl.load(in_ptr3 + x2, xmask, eviction_policy='evict_last')
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp6 = tl.where(xmask, tmp4, 0)
    tmp7 = tl.sum(tmp6, 1)[:, None]
    tmp8 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp7 / tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.where(xmask, tmp13, 0)
    tmp16 = tl.sum(tmp15, 1)[:, None]
    tmp18 = tl.sum(tmp3, 1)[:, None]
    tmp19 = 15.0
    tmp20 = tmp16 / tmp19
    tmp21 = 1e-05
    tmp22 = tmp20 + tmp21
    tmp23 = libdevice.sqrt(tmp22)
    tmp24 = 16.0
    tmp25 = tmp18 / tmp24
    tmp27 = tl.broadcast_to(tmp26, [XBLOCK, RBLOCK])
    tmp29 = tl.where(xmask, tmp27, 0)
    tmp30 = tl.broadcast_to(tmp27, [XBLOCK, RBLOCK])
    tmp32 = tl.where(xmask, tmp30, 0)
    tmp33 = tl.sum(tmp32, 1)[:, None]
    tmp34 = tmp33 / tmp9
    tmp35 = tmp27 - tmp34
    tmp36 = tmp35 * tmp35
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp39 = tl.where(xmask, tmp37, 0)
    tmp40 = tl.sum(tmp39, 1)[:, None]
    tmp42 = tl.sum(tmp29, 1)[:, None]
    tmp43 = tmp40 / tmp19
    tmp44 = tmp43 + tmp21
    tmp45 = libdevice.sqrt(tmp44)
    tmp46 = tmp0 - tmp25
    tmp47 = tmp46 / tmp23
    tmp49 = tmp47 * tmp48
    tmp51 = tmp49 + tmp50
    tmp52 = tmp51 * tmp45
    tmp53 = tmp42 / tmp24
    tmp54 = tmp52 + tmp53
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp23, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + x0, tmp25, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr2 + x0, tmp45, xmask)
    tl.store(out_ptr1 + (r1 + 16 * x0), tmp54, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf8 = buf6
        del buf6
        buf10 = buf9
        del buf9
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf3 = buf1
        del buf1
        buf11 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_div_mean_mul_sqrt_sub_var_0[grid(16)](buf8,
            buf10, buf3, primals_1, primals_2, primals_3, primals_4, buf11,
            16, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del primals_2
        del primals_3
        del primals_4
    return buf11, primals_1, reinterpret_tensor(buf3, (4, 4, 1, 1), (4, 1, 
        1, 1), 0), reinterpret_tensor(buf8, (4, 4, 1, 1), (4, 1, 1, 1), 0
        ), reinterpret_tensor(buf10, (4, 4, 1, 1), (4, 1, 1, 1), 0)


def calc_mean_std(feat, eps=1e-05):
    size = feat.size()
    assert len(size) == 4
    N, C = size[:2]
    feat_var = feat.view(N, C, -1).var(dim=2) + eps
    feat_std = feat_var.sqrt().view(N, C, 1, 1)
    feat_mean = feat.view(N, C, -1).mean(dim=2).view(N, C, 1, 1)
    return feat_mean, feat_std


class SaAdaINNew(nn.Module):
    """
    Sandwich Adaptive-Instance-Normalization.
    """

    def __init__(self, num_features):
        super().__init__()
        self.num_features = num_features
        self.shared_weight = nn.Parameter(torch.Tensor(num_features),
            requires_grad=True)
        self.shared_bias = nn.Parameter(torch.Tensor(num_features),
            requires_grad=True)
        nn.init.ones_(self.shared_weight)
        nn.init.zeros_(self.shared_bias)

    def forward(self, input_0, input_1):
        primals_3 = self.shared_weight
        primals_4 = self.shared_bias
        primals_1 = input_0
        primals_2 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.015645