# AOT ID: ['0_forward']
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_6/inductor_cache/wx/cwxwvlntewdrqi2r4caciy5ht4jdvafnhtiqncr4lo4aegcb4imz.py
# Topologically Sorted Source Nodes: [c], Original ATen: [aten._softmax]
# Source node to ATen node mapping:
#   c => amax, exp, sub
# Graph fragment:
#   %amax : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%primals_1, [-1], True), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%primals_1, %amax), kwargs = {})
#   %exp : [num_users=2] = call_function[target=torch.ops.aten.exp.default](args = (%sub,), kwargs = {})
triton_poi_fused__softmax_0 = async_compile.triton('triton_poi_fused__softmax_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=[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__softmax_0', '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_0(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
    x2 = xindex
    x1 = (xindex // 4)
    tmp0 = tl.load(in_ptr0 + (x2), xmask)
    tmp1 = tl.load(in_ptr0 + (4*x1), xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + (x2), tmp9, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/fj/cfjl47pvhwbpfbvh6rfehwy5ijxc5p3zgkld2lwf3mw5bl6pbkak.py
# Topologically Sorted Source Nodes: [c], Original ATen: [aten._softmax]
# Source node to ATen node mapping:
#   c => div, sum_1
# Graph fragment:
#   %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 = {})
triton_poi_fused__softmax_1 = async_compile.triton('triton_poi_fused__softmax_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=[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__softmax_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_1(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
    x2 = xindex
    x1 = (xindex // 4)
    tmp0 = tl.load(in_ptr0 + (x2), xmask)
    tmp1 = tl.load(in_ptr0 + (4*x1), xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + (x2), tmp8, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/ar/car7r3pnorqrwgaam3xr4uiwbn55pqv4gq3r75m6tl73vlvgej7w.py
# Topologically Sorted Source Nodes: [output_vectors, output_vectors_1], Original ATen: [aten.mul, aten.sum]
# Source node to ATen node mapping:
#   output_vectors => mul
#   output_vectors_1 => sum_2
# Graph fragment:
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%unsqueeze, %primals_2), kwargs = {})
#   %sum_2 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [-2]), kwargs = {})
triton_poi_fused_mul_sum_2 = async_compile.triton('triton_poi_fused_mul_sum_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: '*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_mul_sum_2', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 8, '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_mul_sum_2(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 // 4)
    x0 = xindex % 4
    x1 = (xindex // 4) % 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (4*x3), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + (16*x1)), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + (4*x3)), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + x0 + (16*x1)), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + (4*x3)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (8 + x0 + (16*x1)), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + (4*x3)), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (12 + x0 + (16*x1)), 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
    tl.store(out_ptr0 + (x4), tmp14, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/l6/cl6e4u6jmca2pavy723x6yygpqvfedtsl67jzxuyvfehwoi6qhom.py
# Topologically Sorted Source Nodes: [unit_vector, output_vectors_2], Original ATen: [aten.div, aten.mul]
# Source node to ATen node mapping:
#   output_vectors_2 => mul_2
#   unit_vector => div_2
# Graph fragment:
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_2, %unsqueeze_1), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%unsqueeze_2, %div_2), kwargs = {})
triton_poi_fused_div_mul_3 = async_compile.triton('triton_poi_fused_div_mul_3', '''
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_div_mul_3', '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_div_mul_3(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
    x1 = (xindex // 4)
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4*x1), xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + (x2), xmask)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp11 = 1.0
    tmp12 = tmp10 + tmp11
    tmp13 = tmp10 / tmp12
    tmp15 = 1e-07
    tmp16 = tmp10 + tmp15
    tmp17 = libdevice.sqrt(tmp16)
    tmp18 = tmp14 / tmp17
    tmp19 = tmp13 * tmp18
    tl.store(out_ptr0 + (x2), tmp19, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/6e/c6eb73qummnohroi4grkmffb7gv6d5dvrcqvghuuvcc7mrhkysle.py
# Topologically Sorted Source Nodes: [mul_3, sum_3, b_batch], Original ATen: [aten.mul, aten.sum, aten.add]
# Source node to ATen node mapping:
#   b_batch => add_2
#   mul_3 => mul_3
#   sum_3 => sum_4
# Graph fragment:
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%primals_2, %unsqueeze_3), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_3, [-1]), kwargs = {})
#   %add_2 : [num_users=3] = call_function[target=torch.ops.aten.add.Tensor](args = (%primals_1, %sum_4), kwargs = {})
triton_poi_fused_add_mul_sum_4 = async_compile.triton('triton_poi_fused_add_mul_sum_4', '''
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: '*fp32', 3: '*fp32', 4: '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, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_add_mul_sum_4', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 9, '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_add_mul_sum_4(in_ptr0, in_ptr1, in_ptr2, 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
    x4 = xindex % 64
    x5 = (xindex // 4)
    tmp0 = tl.load(in_ptr0 + (x3), xmask)
    tmp1 = tl.load(in_ptr1 + (4*x4), xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + (4*x5), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + (4*x4)), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr2 + (1 + (4*x5)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + (4*x4)), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr2 + (2 + (4*x5)), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (3 + (4*x4)), xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr2 + (3 + (4*x5)), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 * tmp2
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 + tmp6
    tmp10 = tmp8 * tmp9
    tmp11 = tmp7 + tmp10
    tmp14 = tmp12 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = tmp0 + tmp15
    tl.store(out_ptr0 + (x3), tmp16, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/ln/clne7te5dack722wr3zrnzu7eyu5vdcw2xa6bfplx5ucj4niru7p.py
# Topologically Sorted Source Nodes: [output_vectors_3, output_vectors_4], Original ATen: [aten.mul, aten.sum]
# Source node to ATen node mapping:
#   output_vectors_3 => mul_4
#   output_vectors_4 => sum_6
# Graph fragment:
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%primals_2, %unsqueeze_4), kwargs = {})
#   %sum_6 : [num_users=2] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_4, [-2]), kwargs = {})
triton_poi_fused_mul_sum_5 = async_compile.triton('triton_poi_fused_mul_sum_5', '''
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: '*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_mul_sum_5', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 8, '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_mul_sum_5(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
    x0 = xindex % 4
    x1 = (xindex // 4) % 16
    x3 = (xindex // 4)
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + (16*x1)), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (4*x3), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + x0 + (16*x1)), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + (4*x3)), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (8 + x0 + (16*x1)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + (4*x3)), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (12 + x0 + (16*x1)), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (3 + (4*x3)), 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
    tl.store(out_ptr0 + (x4), tmp14, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/jw/cjwvviqhqhepef2hpphmrbkikxtlpkawwi63bgdz4jxtzjclxcul.py
# Topologically Sorted Source Nodes: [mul_7, sum_6, b_batch_1], Original ATen: [aten.mul, aten.sum, aten.add]
# Source node to ATen node mapping:
#   b_batch_1 => add_5
#   mul_7 => mul_7
#   sum_6 => sum_8
# Graph fragment:
#   %mul_7 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%primals_2, %unsqueeze_7), kwargs = {})
#   %sum_8 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_7, [-1]), kwargs = {})
#   %add_5 : [num_users=3] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_2, %sum_8), kwargs = {})
triton_poi_fused_add_mul_sum_6 = async_compile.triton('triton_poi_fused_add_mul_sum_6', '''
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: '*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_add_mul_sum_6', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 9, '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_add_mul_sum_6(in_out_ptr0, in_ptr0, in_ptr1, xnumel, XBLOCK : tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x4 = xindex % 64
    x5 = (xindex // 4)
    tmp0 = tl.load(in_out_ptr0 + (x3), xmask)
    tmp1 = tl.load(in_ptr0 + (4*x4), xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + (4*x5), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (1 + (4*x4)), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + (1 + (4*x5)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + (4*x4)), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (2 + (4*x5)), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (3 + (4*x4)), xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + (3 + (4*x5)), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 * tmp2
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 + tmp6
    tmp10 = tmp8 * tmp9
    tmp11 = tmp7 + tmp10
    tmp14 = tmp12 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = tmp0 + tmp15
    tl.store(in_out_ptr0 + (x3), tmp16, xmask)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [c], Original ATen: [aten._softmax]
        stream0 = get_raw_stream(0)
        triton_poi_fused__softmax_0.run(primals_1, buf0, 256, grid=grid(256), stream=stream0)
        buf1 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [c], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_1.run(buf0, buf1, 256, grid=grid(256), stream=stream0)
        buf2 = buf0; del buf0  # reuse
        # Topologically Sorted Source Nodes: [output_vectors, output_vectors_1], Original ATen: [aten.mul, aten.sum]
        triton_poi_fused_mul_sum_2.run(buf1, primals_2, buf2, 256, grid=grid(256), stream=stream0)
        buf3 = buf1; del buf1  # reuse
        # Topologically Sorted Source Nodes: [unit_vector, output_vectors_2], Original ATen: [aten.div, aten.mul]
        triton_poi_fused_div_mul_3.run(buf2, buf3, 256, grid=grid(256), stream=stream0)
        buf4 = buf2; del buf2  # reuse
        # Topologically Sorted Source Nodes: [mul_3, sum_3, b_batch], Original ATen: [aten.mul, aten.sum, aten.add]
        triton_poi_fused_add_mul_sum_4.run(primals_1, primals_2, buf3, buf4, 256, grid=grid(256), stream=stream0)
        buf5 = buf3; del buf3  # reuse
        # Topologically Sorted Source Nodes: [c_1], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_0.run(buf4, buf5, 256, grid=grid(256), stream=stream0)
        buf6 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [c_1], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_1.run(buf5, buf6, 256, grid=grid(256), stream=stream0)
        buf7 = buf5; del buf5  # reuse
        # Topologically Sorted Source Nodes: [output_vectors_3, output_vectors_4], Original ATen: [aten.mul, aten.sum]
        triton_poi_fused_mul_sum_5.run(primals_2, buf6, buf7, 256, grid=grid(256), stream=stream0)
        buf8 = buf6; del buf6  # reuse
        # Topologically Sorted Source Nodes: [unit_vector_1, output_vectors_5], Original ATen: [aten.div, aten.mul]
        triton_poi_fused_div_mul_3.run(buf7, buf8, 256, grid=grid(256), stream=stream0)
        buf9 = buf4; del buf4  # reuse
        # Topologically Sorted Source Nodes: [mul_7, sum_6, b_batch_1], Original ATen: [aten.mul, aten.sum, aten.add]
        triton_poi_fused_add_mul_sum_6.run(buf9, primals_2, buf8, 256, grid=grid(256), stream=stream0)
        buf10 = buf8; del buf8  # reuse
        # Topologically Sorted Source Nodes: [c_2], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_0.run(buf9, buf10, 256, grid=grid(256), stream=stream0)
        buf11 = buf7; del buf7  # reuse
        # Topologically Sorted Source Nodes: [c_2], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_1.run(buf10, buf11, 256, grid=grid(256), stream=stream0)
        buf12 = buf10; del buf10  # reuse
        # Topologically Sorted Source Nodes: [output_vectors_6, output_vectors_7], Original ATen: [aten.mul, aten.sum]
        triton_poi_fused_mul_sum_5.run(primals_2, buf11, buf12, 256, grid=grid(256), stream=stream0)
        buf13 = buf11; del buf11  # reuse
        # Topologically Sorted Source Nodes: [unit_vector_2, output_vectors_8], Original ATen: [aten.div, aten.mul]
        triton_poi_fused_div_mul_3.run(buf12, buf13, 256, grid=grid(256), stream=stream0)
        buf14 = buf9; del buf9  # reuse
        # Topologically Sorted Source Nodes: [mul_11, sum_9, b_batch_2], Original ATen: [aten.mul, aten.sum, aten.add]
        triton_poi_fused_add_mul_sum_6.run(buf14, primals_2, buf13, 256, grid=grid(256), stream=stream0)
        buf15 = buf13; del buf13  # reuse
        # Topologically Sorted Source Nodes: [c_3], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_0.run(buf14, buf15, 256, grid=grid(256), stream=stream0)
        buf16 = buf12; del buf12  # reuse
        # Topologically Sorted Source Nodes: [c_3], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_1.run(buf15, buf16, 256, grid=grid(256), stream=stream0)
        buf17 = buf15; del buf15  # reuse
        # Topologically Sorted Source Nodes: [output_vectors_9, output_vectors_10], Original ATen: [aten.mul, aten.sum]
        triton_poi_fused_mul_sum_5.run(primals_2, buf16, buf17, 256, grid=grid(256), stream=stream0)
        buf18 = buf16; del buf16  # reuse
        # Topologically Sorted Source Nodes: [unit_vector_3, output_vectors_11], Original ATen: [aten.div, aten.mul]
        triton_poi_fused_div_mul_3.run(buf17, buf18, 256, grid=grid(256), stream=stream0)
        buf19 = buf14; del buf14  # reuse
        # Topologically Sorted Source Nodes: [mul_15, sum_12, b_batch_3], Original ATen: [aten.mul, aten.sum, aten.add]
        triton_poi_fused_add_mul_sum_6.run(buf19, primals_2, buf18, 256, grid=grid(256), stream=stream0)
        buf20 = buf18; del buf18  # reuse
        # Topologically Sorted Source Nodes: [c_4], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_0.run(buf19, buf20, 256, grid=grid(256), stream=stream0)
        buf21 = buf19; del buf19  # reuse
        # Topologically Sorted Source Nodes: [c_4], Original ATen: [aten._softmax]
        triton_poi_fused__softmax_1.run(buf20, buf21, 256, grid=grid(256), stream=stream0)
        buf22 = buf20; del buf20  # reuse
        # Topologically Sorted Source Nodes: [output_vectors_12, output_vectors_13], Original ATen: [aten.mul, aten.sum]
        triton_poi_fused_mul_sum_5.run(primals_2, buf21, buf22, 256, grid=grid(256), stream=stream0)
        buf23 = buf17; del buf17  # reuse
        # Topologically Sorted Source Nodes: [unit_vector_4, output_vectors_14], Original ATen: [aten.div, aten.mul]
        triton_poi_fused_div_mul_3.run(buf22, buf23, 256, grid=grid(256), stream=stream0)
        del buf22
    return (buf23, buf21, primals_1, primals_2, buf21, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((1, 4, 4, 4, 4), (256, 64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2])
    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


def squash(s, axis=-1, epsilon=1e-07):
    squared_norm = torch.sum(s * s, dim=axis)
    safe_norm = torch.sqrt(squared_norm + epsilon)
    squash_factor = squared_norm / (1.0 + squared_norm)
    unit_vector = torch.div(s, safe_norm.unsqueeze(-1))
    return torch.mul(squash_factor.unsqueeze(-1), unit_vector)


def safe_norm(s, axis=-1, epsilon=1e-07):
    squared_norm = torch.mul(s, s).sum(dim=axis)
    return torch.sqrt(squared_norm + epsilon)


class Agreement_Routing_Down(torch.nn.Module):
    """This is the localised agreement routing algorithm. It takes in the total
    prediction vectors from a layer l and computes the routing weights for 
    those predictions. It then squashes the prediction vectors using the 
    custom squash function."""

    def __init__(self, bias, input_caps_maps, input_caps_dim,
        output_caps_maps, output_caps_dim, new_hl, new_wl, num_iterations):
        super(Agreement_Routing_Down, self).__init__()
        self.input_caps_maps = input_caps_maps
        self.input_caps_dim = input_caps_dim
        self.output_caps_maps = output_caps_maps
        self.output_caps_dim = output_caps_dim
        self.new_hl = int(new_hl)
        self.new_wl = int(new_wl)
        self.num_iterations = num_iterations
        self.softmax = torch.nn.Softmax(dim=-1)
        self.b = torch.nn.Parameter(torch.zeros((1, self.output_caps_maps,
            self.new_hl, self.new_wl, self.input_caps_maps)))

    def forward(self, tensor_of_prediction_vector):
        c = self.softmax(self.b)
        output_vectors = torch.mul(c.unsqueeze(-1), tensor_of_prediction_vector
            )
        output_vectors = output_vectors.sum(dim=-2)
        output_vectors = squash(output_vectors, axis=-1)
        b_batch = self.b
        for d in range(self.num_iterations):
            b_batch = b_batch + torch.mul(tensor_of_prediction_vector,
                output_vectors.unsqueeze(-2)).sum(dim=-1)
            """
            distances = torch.mul(tensor_of_prediction_vector,
                                output_vectors.unsqueeze(-2)).sum(dim = -1)
            
            self.b = torch.add(self.b, distances)
            """
            c = self.softmax(b_batch)
            output_vectors = torch.mul(tensor_of_prediction_vector, c.
                unsqueeze(-1))
            output_vectors = output_vectors.sum(-2)
            output_vectors = squash(output_vectors, axis=-1)
        self.c = c
        return output_vectors


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


def get_init_inputs():
    return [[], {'bias': 4, 'input_caps_maps': 4, 'input_caps_dim': 4,
        'output_caps_maps': 4, 'output_caps_dim': 4, 'new_hl': 4, 'new_wl':
        4, 'num_iterations': 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 import triton_helpers
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
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__softmax_0(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_1(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
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_mul_sum_2(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 // 4
    x0 = xindex % 4
    x1 = xindex // 4 % 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (8 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (12 + x0 + 16 * x1), 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
    tl.store(out_ptr0 + x4, tmp14, xmask)


@triton.jit
def triton_poi_fused_div_mul_3(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
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tmp0 * tmp0
    tmp3 = tmp2 * tmp2
    tmp4 = tmp1 + tmp3
    tmp6 = tmp5 * tmp5
    tmp7 = tmp4 + tmp6
    tmp9 = tmp8 * tmp8
    tmp10 = tmp7 + tmp9
    tmp11 = 1.0
    tmp12 = tmp10 + tmp11
    tmp13 = tmp10 / tmp12
    tmp15 = 1e-07
    tmp16 = tmp10 + tmp15
    tmp17 = libdevice.sqrt(tmp16)
    tmp18 = tmp14 / tmp17
    tmp19 = tmp13 * tmp18
    tl.store(out_ptr0 + x2, tmp19, xmask)


@triton.jit
def triton_poi_fused_add_mul_sum_4(in_ptr0, in_ptr1, in_ptr2, 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
    x4 = xindex % 64
    x5 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + 4 * x4, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + 4 * x5, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr2 + (1 + 4 * x5), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr2 + (2 + 4 * x5), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr2 + (3 + 4 * x5), xmask, eviction_policy='evict_last'
        )
    tmp3 = tmp1 * tmp2
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 + tmp6
    tmp10 = tmp8 * tmp9
    tmp11 = tmp7 + tmp10
    tmp14 = tmp12 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = tmp0 + tmp15
    tl.store(out_ptr0 + x3, tmp16, xmask)


@triton.jit
def triton_poi_fused_mul_sum_5(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
    x0 = xindex % 4
    x1 = xindex // 4 % 16
    x3 = xindex // 4
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (4 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (8 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (12 + x0 + 16 * x1), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x3), 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
    tl.store(out_ptr0 + x4, tmp14, xmask)


@triton.jit
def triton_poi_fused_add_mul_sum_6(in_out_ptr0, in_ptr0, in_ptr1, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x4 = xindex % 64
    x5 = xindex // 4
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x4, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + 4 * x5, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (1 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr1 + (1 + 4 * x5), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x4), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (2 + 4 * x5), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (3 + 4 * x4), xmask, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr1 + (3 + 4 * x5), xmask, eviction_policy='evict_last'
        )
    tmp3 = tmp1 * tmp2
    tmp6 = tmp4 * tmp5
    tmp7 = tmp3 + tmp6
    tmp10 = tmp8 * tmp9
    tmp11 = tmp7 + tmp10
    tmp14 = tmp12 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = tmp0 + tmp15
    tl.store(in_out_ptr0 + x3, tmp16, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (1, 4, 4, 4, 4), (256, 64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_0[grid(256)](primals_1, buf0, 256, XBLOCK
            =128, num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused__softmax_1[grid(256)](buf0, buf1, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf2 = buf0
        del buf0
        triton_poi_fused_mul_sum_2[grid(256)](buf1, primals_2, buf2, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf3 = buf1
        del buf1
        triton_poi_fused_div_mul_3[grid(256)](buf2, buf3, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf4 = buf2
        del buf2
        triton_poi_fused_add_mul_sum_4[grid(256)](primals_1, primals_2,
            buf3, buf4, 256, XBLOCK=128, num_warps=4, num_stages=1)
        buf5 = buf3
        del buf3
        triton_poi_fused__softmax_0[grid(256)](buf4, buf5, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf6 = empty_strided_cuda((1, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused__softmax_1[grid(256)](buf5, buf6, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf7 = buf5
        del buf5
        triton_poi_fused_mul_sum_5[grid(256)](primals_2, buf6, buf7, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf8 = buf6
        del buf6
        triton_poi_fused_div_mul_3[grid(256)](buf7, buf8, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf9 = buf4
        del buf4
        triton_poi_fused_add_mul_sum_6[grid(256)](buf9, primals_2, buf8, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        buf10 = buf8
        del buf8
        triton_poi_fused__softmax_0[grid(256)](buf9, buf10, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf11 = buf7
        del buf7
        triton_poi_fused__softmax_1[grid(256)](buf10, buf11, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf12 = buf10
        del buf10
        triton_poi_fused_mul_sum_5[grid(256)](primals_2, buf11, buf12, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf13 = buf11
        del buf11
        triton_poi_fused_div_mul_3[grid(256)](buf12, buf13, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf14 = buf9
        del buf9
        triton_poi_fused_add_mul_sum_6[grid(256)](buf14, primals_2, buf13, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        buf15 = buf13
        del buf13
        triton_poi_fused__softmax_0[grid(256)](buf14, buf15, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        buf16 = buf12
        del buf12
        triton_poi_fused__softmax_1[grid(256)](buf15, buf16, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf17 = buf15
        del buf15
        triton_poi_fused_mul_sum_5[grid(256)](primals_2, buf16, buf17, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf18 = buf16
        del buf16
        triton_poi_fused_div_mul_3[grid(256)](buf17, buf18, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf19 = buf14
        del buf14
        triton_poi_fused_add_mul_sum_6[grid(256)](buf19, primals_2, buf18, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
        buf20 = buf18
        del buf18
        triton_poi_fused__softmax_0[grid(256)](buf19, buf20, 256, XBLOCK=
            128, num_warps=4, num_stages=1)
        buf21 = buf19
        del buf19
        triton_poi_fused__softmax_1[grid(256)](buf20, buf21, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf22 = buf20
        del buf20
        triton_poi_fused_mul_sum_5[grid(256)](primals_2, buf21, buf22, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf23 = buf17
        del buf17
        triton_poi_fused_div_mul_3[grid(256)](buf22, buf23, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        del buf22
    return buf23, buf21, primals_1, primals_2, buf21


def squash(s, axis=-1, epsilon=1e-07):
    squared_norm = torch.sum(s * s, dim=axis)
    safe_norm = torch.sqrt(squared_norm + epsilon)
    squash_factor = squared_norm / (1.0 + squared_norm)
    unit_vector = torch.div(s, safe_norm.unsqueeze(-1))
    return torch.mul(squash_factor.unsqueeze(-1), unit_vector)


def safe_norm(s, axis=-1, epsilon=1e-07):
    squared_norm = torch.mul(s, s).sum(dim=axis)
    return torch.sqrt(squared_norm + epsilon)


class Agreement_Routing_DownNew(torch.nn.Module):
    """This is the localised agreement routing algorithm. It takes in the total
    prediction vectors from a layer l and computes the routing weights for 
    those predictions. It then squashes the prediction vectors using the 
    custom squash function."""

    def __init__(self, bias, input_caps_maps, input_caps_dim,
        output_caps_maps, output_caps_dim, new_hl, new_wl, num_iterations):
        super(Agreement_Routing_DownNew, self).__init__()
        self.input_caps_maps = input_caps_maps
        self.input_caps_dim = input_caps_dim
        self.output_caps_maps = output_caps_maps
        self.output_caps_dim = output_caps_dim
        self.new_hl = int(new_hl)
        self.new_wl = int(new_wl)
        self.num_iterations = num_iterations
        self.softmax = torch.nn.Softmax(dim=-1)
        self.b = torch.nn.Parameter(torch.zeros((1, self.output_caps_maps,
            self.new_hl, self.new_wl, self.input_caps_maps)))

    def forward(self, input_0):
        primals_1 = self.b
        primals_2 = input_0
        output = call([primals_1, primals_2])
        return output[0]
