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 libdevice, math as tl_math
from torch.nn import functional as F
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused__softmax_mul_pow_sum_zeros_0(in_ptr0, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    x2 = xindex
    tmp6 = tl.load(in_ptr0 + (4 * x0 + 64 * x1), xmask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr0 + (16 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (32 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + (48 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp18 = tl.load(in_ptr0 + (1 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp20 = tl.load(in_ptr0 + (17 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (33 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr0 + (49 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr0 + (2 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr0 + (18 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp36 = tl.load(in_ptr0 + (34 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp39 = tl.load(in_ptr0 + (50 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp44 = tl.load(in_ptr0 + (3 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp46 = tl.load(in_ptr0 + (19 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp49 = tl.load(in_ptr0 + (35 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp52 = tl.load(in_ptr0 + (51 + 4 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp0 = 0.0
    tmp1 = tl_math.exp(tmp0)
    tmp2 = tmp1 + tmp1
    tmp3 = tmp2 + tmp1
    tmp4 = tmp3 + tmp1
    tmp5 = tmp1 / tmp4
    tmp7 = tmp5 * tmp6
    tmp9 = tmp5 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp5 * tmp11
    tmp13 = tmp10 + tmp12
    tmp15 = tmp5 * tmp14
    tmp16 = tmp13 + tmp15
    tmp17 = tmp16 * tmp16
    tmp19 = tmp5 * tmp18
    tmp21 = tmp5 * tmp20
    tmp22 = tmp19 + tmp21
    tmp24 = tmp5 * tmp23
    tmp25 = tmp22 + tmp24
    tmp27 = tmp5 * tmp26
    tmp28 = tmp25 + tmp27
    tmp29 = tmp28 * tmp28
    tmp30 = tmp17 + tmp29
    tmp32 = tmp5 * tmp31
    tmp34 = tmp5 * tmp33
    tmp35 = tmp32 + tmp34
    tmp37 = tmp5 * tmp36
    tmp38 = tmp35 + tmp37
    tmp40 = tmp5 * tmp39
    tmp41 = tmp38 + tmp40
    tmp42 = tmp41 * tmp41
    tmp43 = tmp30 + tmp42
    tmp45 = tmp5 * tmp44
    tmp47 = tmp5 * tmp46
    tmp48 = tmp45 + tmp47
    tmp50 = tmp5 * tmp49
    tmp51 = tmp48 + tmp50
    tmp53 = tmp5 * tmp52
    tmp54 = tmp51 + tmp53
    tmp55 = tmp54 * tmp54
    tmp56 = tmp43 + tmp55
    tl.store(out_ptr0 + x2, tmp56, xmask)


@triton.jit
def triton_poi_fused__softmax_add_div_mul_pow_sum_zeros_1(in_ptr0, in_ptr1,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex // 16
    x3 = xindex % 16
    x4 = xindex // 4
    x5 = xindex
    tmp6 = tl.load(in_ptr0 + (x3 + 64 * x2), xmask)
    tmp8 = tl.load(in_ptr0 + (16 + x3 + 64 * x2), xmask)
    tmp11 = tl.load(in_ptr0 + (32 + x3 + 64 * x2), xmask)
    tmp14 = tl.load(in_ptr0 + (48 + x3 + 64 * x2), xmask)
    tmp17 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp0 = 0.0
    tmp1 = tl_math.exp(tmp0)
    tmp2 = tmp1 + tmp1
    tmp3 = tmp2 + tmp1
    tmp4 = tmp3 + tmp1
    tmp5 = tmp1 / tmp4
    tmp7 = tmp5 * tmp6
    tmp9 = tmp5 * tmp8
    tmp10 = tmp7 + tmp9
    tmp12 = tmp5 * tmp11
    tmp13 = tmp10 + tmp12
    tmp15 = tmp5 * tmp14
    tmp16 = tmp13 + tmp15
    tmp18 = libdevice.sqrt(tmp17)
    tmp19 = tmp16 / tmp18
    tmp20 = tmp18 * tmp18
    tmp21 = 1.0
    tmp22 = tmp20 + tmp21
    tmp23 = tmp20 / tmp22
    tmp24 = tmp19 * tmp23
    tl.store(out_ptr0 + x5, tmp24, xmask)


@triton.jit
def triton_poi_fused_clone_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4 % 4
    x2 = xindex // 16 % 4
    x3 = xindex // 64
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask)
    tl.store(out_ptr0 + x4, tmp0, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_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, 1), (4, 1, 16), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_mul_pow_sum_zeros_0[grid(16)](arg0_1,
            buf0, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_add_div_mul_pow_sum_zeros_1[grid(64)](arg0_1,
            buf0, buf1, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf0
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_clone_2[grid(256)](arg0_1, buf2, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del arg0_1
        buf3 = empty_strided_cuda((16, 4, 1), (4, 1, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf1, (16, 4, 1), (4, 1, 1), 0), out=buf3)
        del buf2
    return reinterpret_tensor(buf3, (4, 4, 4, 1), (16, 1, 4, 1), 0
        ), reinterpret_tensor(buf1, (4, 4, 4, 1), (16, 4, 1, 1), 0)


def cal_normal(v, dim=-1, keepdim=False):
    """

    :return:
    """
    normal = torch.sum(v ** 2, dim=dim, keepdim=keepdim) ** 0.5
    return normal


def squash(sr, dim=1):
    """

    :param dim:
    :param sr:(bs, dim)
    :return:
    """
    sr_normal = cal_normal(sr, keepdim=True, dim=dim)
    sr_normal2 = sr_normal ** 2
    v = sr / sr_normal * (sr_normal2 / (1 + sr_normal2))
    return v


def dynamic_routing(u, br):
    """
    u: (b, num_size, num_classes, dim)
    br: (b, num_size, num_classes, 1)
    :return:
    """
    cr = F.softmax(br, dim=1)
    sr = torch.sum(cr * u, dim=1)
    vr = squash(sr, dim=-1)
    sm = torch.einsum('bncd,bcd->bnc', u, vr).unsqueeze(dim=3)
    br = br + sm
    return br, vr


class RoutingBaseNew(nn.Module):

    def __init__(self, num_routing_iterations=1, **kwargs):
        super(RoutingBaseNew, self).__init__()
        self.num_routing_iterations = num_routing_iterations

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


Measured latency_ms: 0.048677