import torch
import torch.nn as nn


class PaddedInstanceNorm1d(nn.Module):

    def __init__(self, num_features, eps=1e-05, momentum=0.1, affine=False,
        track_running_stats=False):
        super().__init__()
        self.num_features = num_features
        self.eps = eps
        self.momentum = momentum
        if affine is True:
            raise NotImplementedError
        if track_running_stats is True:
            raise NotImplementedError

    def forward(self, x, lengths):
        lengths = lengths.view(-1, 1, 1).float()
        sum_ = torch.sum(x, dim=2, keepdim=True)
        mean = sum_ / lengths
        sqsum = torch.sum(torch.pow(x, 2.0), dim=2, keepdim=True)
        sqmean = sqsum / lengths
        var = sqmean - torch.pow(mean, 2.0)
        return (x - mean) / torch.pow(var + self.eps, 0.5)


def get_inputs():
    return [torch.rand([4, 16384, 4, 4]), torch.rand([4, 256, 4, 4])]


def get_init_inputs():
    return [[], {'num_features': 4}]

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
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_div_pow_sub_sum_0(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex % 4
    x3 = xindex // 4
    x1 = xindex // 4 % 16384
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x3), None)
    tmp1 = tl.load(in_ptr0 + (4 + x0 + 16 * x3), None)
    tmp3 = tl.load(in_ptr0 + (8 + x0 + 16 * x3), None)
    tmp5 = tl.load(in_ptr0 + (12 + x0 + 16 * x3), None)
    tmp7 = tl.load(in_ptr1 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp8 = tmp6 / tmp7
    tmp9 = tmp0 * tmp0
    tmp10 = tmp1 * tmp1
    tmp11 = tmp9 + tmp10
    tmp12 = tmp3 * tmp3
    tmp13 = tmp11 + tmp12
    tmp14 = tmp5 * tmp5
    tmp15 = tmp13 + tmp14
    tmp16 = tmp15 / tmp7
    tmp17 = tmp8 * tmp8
    tmp18 = tmp16 - tmp17
    tmp19 = 1e-05
    tmp20 = tmp18 + tmp19
    tmp21 = libdevice.sqrt(tmp20)
    tl.store(out_ptr0 + x4, tmp8, None)
    tl.store(out_ptr1 + x4, tmp21, None)


@triton.jit
def triton_poi_fused_add_div_pow_sub_sum_1(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x0 = xindex % 4
    x2 = xindex // 16
    tmp0 = tl.load(in_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr1 + (x0 + 4 * x2), None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (x0 + 4 * x2), None, eviction_policy='evict_last')
    tmp2 = tmp0 - tmp1
    tmp4 = tmp2 / tmp3
    tl.store(out_ptr0 + x3, tmp4, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 256, 4, 4), (4096, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 16384, 4, 4), (262144, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 16384, 1, 4), (65536, 4, 262144, 1),
            torch.float32)
        buf1 = empty_strided_cuda((4, 16384, 1, 4), (65536, 4, 262144, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_div_pow_sub_sum_0[grid(262144)](arg1_1, arg0_1,
            buf0, buf1, 262144, XBLOCK=512, num_warps=8, num_stages=1)
        del arg0_1
        buf2 = empty_strided_cuda((4, 16384, 4, 4), (262144, 16, 4, 1),
            torch.float32)
        triton_poi_fused_add_div_pow_sub_sum_1[grid(1048576)](arg1_1, buf0,
            buf1, buf2, 1048576, XBLOCK=1024, num_warps=4, num_stages=1)
        del arg1_1
        del buf0
        del buf1
    return buf2,


class PaddedInstanceNorm1dNew(nn.Module):

    def __init__(self, num_features, eps=1e-05, momentum=0.1, affine=False,
        track_running_stats=False):
        super().__init__()
        self.num_features = num_features
        self.eps = eps
        self.momentum = momentum
        if affine is True:
            raise NotImplementedError
        if track_running_stats is True:
            raise NotImplementedError

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