# AOT ID: ['0_inference']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_4/inductor_cache/62/c62wpgljd4ecvegggaftspafsyogtulrfnj2kngm2aaelqiv3yee.py
# Topologically Sorted Source Nodes: [br, cr, mul, sr, pow_1, sum_2], Original ATen: [aten.zeros, aten._softmax, aten.mul, aten.sum, aten.pow]
# Source node to ATen node mapping:
#   br => full_default
#   cr => amax, div, exp, sub, sum_1
#   mul => mul
#   pow_1 => pow_1
#   sr => sum_2
#   sum_2 => sum_3
# Graph fragment:
#   %full_default : [num_users=2] = call_function[target=torch.ops.aten.full.default](args = ([4, 4, 4, 1], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %amax : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%full_default, [1], True), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%full_default, %amax), kwargs = {})
#   %exp : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%sub,), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [1], True), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp, %sum_1), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div, %arg0_1), kwargs = {})
#   %sum_2 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [1]), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_2, 2), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%pow_1, [-1], True), kwargs = {})
triton_poi_fused__softmax_mul_pow_sum_zeros_0 = async_compile.triton('triton_poi_fused__softmax_mul_pow_sum_zeros_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[16], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused__softmax_mul_pow_sum_zeros_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 16, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/wi/cwiqra3t4q3pgl7y55y4ha74wnsr3trnbcy5uzfmy3vk6esvxdrg.py
# Topologically Sorted Source Nodes: [br, cr, mul, sr, normal, truediv, sr_normal2, add, truediv_1, v], Original ATen: [aten.zeros, aten._softmax, aten.mul, aten.sum, aten.pow, aten.div, aten.add]
# Source node to ATen node mapping:
#   add => add
#   br => full_default
#   cr => amax, div, exp, sub, sum_1
#   mul => mul
#   normal => pow_2
#   sr => sum_2
#   sr_normal2 => pow_3
#   truediv => div_1
#   truediv_1 => div_2
#   v => mul_1
# Graph fragment:
#   %full_default : [num_users=2] = call_function[target=torch.ops.aten.full.default](args = ([4, 4, 4, 1], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %amax : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%full_default, [1], True), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%full_default, %amax), kwargs = {})
#   %exp : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%sub,), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [1], True), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%exp, %sum_1), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div, %arg0_1), kwargs = {})
#   %sum_2 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [1]), kwargs = {})
#   %pow_2 : [num_users=2] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_3, 0.5), kwargs = {})
#   %div_1 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_2, %pow_2), kwargs = {})
#   %pow_3 : [num_users=2] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%pow_2, 2), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%pow_3, 1), kwargs = {})
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%pow_3, %add), kwargs = {})
#   %mul_1 : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, %div_2), kwargs = {})
triton_poi_fused__softmax_add_div_mul_pow_sum_zeros_1 = async_compile.triton('triton_poi_fused__softmax_add_div_mul_pow_sum_zeros_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused__softmax_add_div_mul_pow_sum_zeros_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 5, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/n3/cn3h43fi4m7oq2vwlktxfhxi3dzck4gnc765fyme47rufsuxazkg.py
# Topologically Sorted Source Nodes: [einsum], Original ATen: [aten.clone]
# Source node to ATen node mapping:
#   einsum => clone
# Graph fragment:
#   %clone : [num_users=1] = call_function[target=torch.ops.aten.clone.default](args = (%permute_2,), kwargs = {memory_format: torch.contiguous_format})
triton_poi_fused_clone_2 = async_compile.triton('triton_poi_fused_clone_2', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[256], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_clone_2', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 1, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@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)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

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)
        # Topologically Sorted Source Nodes: [br, cr, mul, sr, pow_1, sum_2], Original ATen: [aten.zeros, aten._softmax, aten.mul, aten.sum, aten.pow]
        stream0 = get_raw_stream(0)
        triton_poi_fused__softmax_mul_pow_sum_zeros_0.run(arg0_1, buf0, 16, grid=grid(16), stream=stream0)
        buf1 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [br, cr, mul, sr, normal, truediv, sr_normal2, add, truediv_1, v], Original ATen: [aten.zeros, aten._softmax, aten.mul, aten.sum, aten.pow, aten.div, aten.add]
        triton_poi_fused__softmax_add_div_mul_pow_sum_zeros_1.run(arg0_1, buf0, buf1, 64, grid=grid(64), stream=stream0)
        del buf0
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [einsum], Original ATen: [aten.clone]
        triton_poi_fused_clone_2.run(arg0_1, buf2, 256, grid=grid(256), stream=stream0)
        del arg0_1
        buf3 = empty_strided_cuda((16, 4, 1), (4, 1, 1), torch.float32)
        # Topologically Sorted Source Nodes: [einsum], Original ATen: [aten.bmm]
        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 benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import torch
from torch.nn import functional as F
import torch.nn as nn


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 RoutingBase(nn.Module):

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

    def forward(self, inx):
        """
        inx: (b, num_size, num_classes, dim)
        :return:
        """
        v_h = []
        b_h = []
        inx_device = inx.device
        br = torch.zeros(size=(*inx.size()[:-1], 1), requires_grad=False,
            device=inx_device)
        for i in range(self.num_routing_iterations):
            br, vr = dynamic_routing(inx, br)
            v_h.append(vr.unsqueeze(dim=3))
            b_h.append(br)
        return torch.cat(b_h, dim=-1), torch.cat(v_h, dim=-1)


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


def get_init_inputs():
    return [[], {}]

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]
