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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice
import torch.cuda
from torch import nn
import torch.distributed
import torch.utils.data
import torch.optim
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_add_clamp_div_mul_pow_sqrt_sub_sum_0(in_out_ptr0,
    in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 64
    x0 = xindex % 16
    x2 = xindex // 64
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (4 * x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x0 + 64 * x2), xmask, eviction_policy
        ='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x0 + 64 * x2), xmask, eviction_policy
        ='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x0 + 64 * x2), xmask,
        eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp15 = tmp1 + tmp4
    tmp16 = tmp15 + tmp8
    tmp17 = tmp16 + tmp12
    tmp18 = 1.0
    tmp19 = triton_helpers.maximum(tmp17, tmp18)
    tmp20 = 100000.0
    tmp21 = triton_helpers.minimum(tmp19, tmp20)
    tmp22 = tmp14 / tmp21
    tmp23 = tmp0 - tmp22
    tmp24 = tmp23 * tmp23
    tmp25 = tmp24 * tmp1
    tmp26 = tmp3 - tmp22
    tmp27 = tmp26 * tmp26
    tmp28 = tmp27 * tmp4
    tmp29 = tmp25 + tmp28
    tmp30 = tmp7 - tmp22
    tmp31 = tmp30 * tmp30
    tmp32 = tmp31 * tmp8
    tmp33 = tmp29 + tmp32
    tmp34 = tmp11 - tmp22
    tmp35 = tmp34 * tmp34
    tmp36 = tmp35 * tmp12
    tmp37 = tmp33 + tmp36
    tmp38 = tmp17 - tmp18
    tmp39 = triton_helpers.maximum(tmp38, tmp18)
    tmp40 = triton_helpers.minimum(tmp39, tmp20)
    tmp41 = tmp37 / tmp40
    tmp42 = 1e-08
    tmp43 = tmp41 + tmp42
    tmp44 = libdevice.sqrt(tmp43)
    tl.store(out_ptr0 + x4, tmp22, xmask)
    tl.store(in_out_ptr0 + x4, tmp44, xmask)


@triton.jit
def triton_poi_fused_add_clamp_div_sqrt_sub_sum_1(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 256
    x4 = xindex // 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 - tmp1
    tmp4 = tmp2 / tmp3
    tl.store(out_ptr0 + x5, tmp4, xmask)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (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, 4, 1), (64, 16, 4, 1, 256),
            torch.float32)
        buf1 = empty_strided_cuda((4, 4, 4, 4, 1), (64, 16, 4, 1, 256),
            torch.float32)
        buf2 = buf1
        del buf1
        get_raw_stream(0)
        triton_poi_fused_add_clamp_div_mul_pow_sqrt_sub_sum_0[grid(256)](buf2,
            arg1_1, arg0_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
        buf3 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused_add_clamp_div_sqrt_sub_sum_1[grid(1024)](arg1_1,
            buf0, buf2, buf3, 1024, XBLOCK=128, num_warps=4, num_stages=1)
        del arg1_1
        del buf0
        del buf2
    return buf3,


class MaskedInstanceNorm1dNew(nn.Module):
    """Instance norm + masking."""
    MAX_CNT = 100000.0

    def __init__(self, d_channel: 'int', unbiased: 'bool'=True, affine:
        'bool'=False):
        super().__init__()
        self.d_channel = d_channel
        self.unbiased = unbiased
        self.affine = affine
        if self.affine:
            gamma = torch.ones(d_channel, dtype=torch.float)
            beta = torch.zeros_like(gamma)
            self.register_parameter('gamma', nn.Parameter(gamma))
            self.register_parameter('beta', nn.Parameter(beta))

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0]


Measured latency_ms: 0.030156