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 import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
from collections import OrderedDict
from typing import Tuple
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_relu_0(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 128
    xnumel = 16
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y3 = yindex
    y1 = yindex // 4
    y0 = yindex % 4
    tmp0 = tl.load(in_ptr0 + (x2 + 16 * y3), xmask & ymask, eviction_policy
        ='evict_last')
    tmp1 = tl.load(in_ptr1 + y1, ymask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1, 1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(out_ptr0 + (y0 + 4 * x2 + 64 * y1), tmp4, xmask & ymask)


@triton.jit
def triton_poi_fused_max_pool3d_with_indices_1(in_ptr0, out_ptr0, ynumel,
    xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 64
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y3 = yindex
    y0 = yindex % 2
    y1 = yindex // 2
    tmp0 = tl.load(in_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy=
        'evict_last')
    tl.store(out_ptr0 + (y0 + 2 * x2 + 8 * y1), tmp0, xmask & ymask)


@triton.jit
def triton_poi_fused_relu_2(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 128
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y3 = yindex
    y1 = yindex // 2
    y0 = yindex % 2
    tmp0 = tl.load(in_ptr0 + (x2 + 4 * y3), xmask & ymask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + y1, ymask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1, 1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(out_ptr0 + (y0 + 2 * x2 + 8 * y1), tmp4, xmask & ymask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_3(in_out_ptr0, in_ptr0,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask)
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(in_out_ptr0 + x0, tmp4, xmask)
    tl.store(out_ptr0 + x0, tmp6, xmask)


@triton.jit
def triton_per_fused__log_softmax_4(in_ptr0, out_ptr2, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 2
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = triton_helpers.max2(tmp1, 1)[:, None]
    tmp4 = tmp0 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp9 = tl_math.log(tmp8)
    tmp10 = tmp4 - tmp9
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp10, None)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11) = args
    args.clear()
    assert_size_stride(primals_1, (32, 4, 3, 3, 3), (108, 27, 9, 3, 1))
    assert_size_stride(primals_2, (32,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (64, 32, 3, 3, 3), (864, 27, 9, 3, 1))
    assert_size_stride(primals_5, (64,), (1,))
    assert_size_stride(primals_6, (128, 64, 3, 3, 3), (1728, 27, 9, 3, 1))
    assert_size_stride(primals_7, (128,), (1,))
    assert_size_stride(primals_8, (2, 128), (128, 1))
    assert_size_stride(primals_9, (2,), (1,))
    assert_size_stride(primals_10, (1, 128), (128, 1))
    assert_size_stride(primals_11, (1,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(reinterpret_tensor(primals_3, (1,
            4, 4, 4, 4), (256, 64, 16, 4, 1), 0), primals_1, stride=(1, 1, 
            1), padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (1, 32, 4, 4, 4), (2048, 64, 16, 4, 1))
        buf1 = empty_strided_cuda((32, 4, 4, 4), (64, 1, 16, 4), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_relu_0[grid(128, 16)](buf0, primals_2, buf1, 128, 
            16, XBLOCK=16, YBLOCK=16, num_warps=4, num_stages=1)
        del buf0
        del primals_2
        buf2 = torch.ops.aten.max_pool3d_with_indices.default(buf1, [2, 2, 
            2], [2, 2, 2])
        buf3 = buf2[0]
        buf4 = buf2[1]
        del buf2
        buf5 = empty_strided_cuda((32, 2, 2, 2), (8, 1, 4, 2), torch.int64)
        triton_poi_fused_max_pool3d_with_indices_1[grid(64, 4)](buf4, buf5,
            64, 4, XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1)
        del buf4
        buf6 = extern_kernels.convolution(reinterpret_tensor(buf3, (1, 32, 
            2, 2, 2), (256, 8, 4, 2, 1), 0), primals_4, stride=(1, 1, 1),
            padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf6, (1, 64, 2, 2, 2), (512, 8, 4, 2, 1))
        buf7 = empty_strided_cuda((64, 2, 2, 2), (8, 1, 4, 2), torch.float32)
        triton_poi_fused_relu_2[grid(128, 4)](buf6, primals_5, buf7, 128, 4,
            XBLOCK=4, YBLOCK=32, num_warps=4, num_stages=1)
        del buf6
        del primals_5
        buf8 = torch.ops.aten.max_pool3d_with_indices.default(buf7, [2, 2, 
            2], [2, 2, 2])
        buf9 = buf8[0]
        buf10 = buf8[1]
        del buf8
        buf11 = extern_kernels.convolution(reinterpret_tensor(buf9, (1, 64,
            1, 1, 1), (64, 1, 1, 1, 1), 0), primals_6, stride=(1, 1, 1),
            padding=(1, 1, 1), dilation=(1, 1, 1), transposed=False,
            output_padding=(0, 0, 0), groups=1, bias=None)
        assert_size_stride(buf11, (1, 128, 1, 1, 1), (128, 1, 1, 1, 1))
        buf12 = reinterpret_tensor(buf11, (128, 1, 1, 1), (1, 128, 128, 128), 0
            )
        del buf11
        buf19 = empty_strided_cuda((128, 1, 1, 1), (1, 1, 1, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_3[grid(128)](buf12,
            primals_7, buf19, 128, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_7
        buf13 = empty_strided_cuda((1, 2), (2, 1), torch.float32)
        extern_kernels.addmm(primals_9, reinterpret_tensor(buf12, (1, 128),
            (0, 1), 0), reinterpret_tensor(primals_8, (128, 2), (1, 128), 0
            ), alpha=1, beta=1, out=buf13)
        del primals_9
        buf16 = empty_strided_cuda((1, 2), (2, 1), torch.float32)
        triton_per_fused__log_softmax_4[grid(1)](buf13, buf16, 1, 2, XBLOCK
            =1, num_warps=2, num_stages=1)
        del buf13
        buf18 = empty_strided_cuda((1, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_11, reinterpret_tensor(buf12, (1, 128),
            (0, 1), 0), reinterpret_tensor(primals_10, (128, 1), (1, 128), 
            0), alpha=1, beta=1, out=buf18)
        del primals_11
    return buf16, reinterpret_tensor(buf18, (1,), (1,), 0
        ), primals_1, primals_4, primals_6, reinterpret_tensor(primals_3, (
        1, 4, 4, 4, 4), (256, 64, 16, 4, 1), 0
        ), buf1, buf5, reinterpret_tensor(buf3, (1, 32, 2, 2, 2), (256, 8, 
        4, 2, 1), 0), buf7, buf10, reinterpret_tensor(buf9, (1, 64, 1, 1, 1
        ), (64, 1, 1, 1, 1), 0), reinterpret_tensor(buf12, (1, 128), (128, 
        1), 0), buf16, primals_10, primals_8, buf19


class HiResPoseNew(nn.Module):
    """
    GNINA HiResPose model architecture.

    Parameters
    ----------
    input_dims: tuple
        Model input dimensions (channels, depth, height, width)

    Notes
    -----
    This architecture was translated from the following Caffe model:

        https://github.com/gnina/models/blob/master/crossdocked_paper/hires_pose.model

    The main difference is that the PyTorch implementation resurns the log softmax.

    This model is implemented only for multi-task pose and affinity prediction.
    """

    def __init__(self, input_dims: 'Tuple'):
        super().__init__()
        self.input_dims = input_dims
        self.features = nn.Sequential(OrderedDict([('unit1_conv', nn.Conv3d
            (in_channels=input_dims[0], out_channels=32, kernel_size=3,
            stride=1, padding=1)), ('unit1_func', nn.ReLU()), ('unit2_pool',
            nn.MaxPool3d(kernel_size=2, stride=2)), ('unit2_conv', nn.
            Conv3d(in_channels=32, out_channels=64, kernel_size=3, stride=1,
            padding=1)), ('unit2_func', nn.ReLU()), ('unit3_pool', nn.
            MaxPool3d(kernel_size=2, stride=2)), ('unit3_conv', nn.Conv3d(
            in_channels=64, out_channels=128, kernel_size=3, stride=1,
            padding=1)), ('unit3_func', nn.ReLU())]))
        self.features_out_size = input_dims[1] // 4 * input_dims[2
            ] // 4 * input_dims[3] // 4 * 128
        self.pose = nn.Sequential(OrderedDict([('pose_output', nn.Linear(
            in_features=self.features_out_size, out_features=2))]))
        self.affinity = nn.Sequential(OrderedDict([('affinity_output', nn.
            Linear(in_features=self.features_out_size, out_features=1))]))

    def forward(self, input_0):
        primals_1 = self.features.unit1_conv.weight
        primals_2 = self.features.unit1_conv.bias
        primals_4 = self.features.unit2_conv.weight
        primals_5 = self.features.unit2_conv.bias
        primals_6 = self.features.unit3_conv.weight
        primals_7 = self.features.unit3_conv.bias
        primals_8 = self.pose.pose_output.weight
        primals_9 = self.pose.pose_output.bias
        primals_10 = self.affinity.affinity_output.weight
        primals_11 = self.affinity.affinity_output.bias
        primals_3 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11])
        return output[0], output[1]


Measured latency_ms: 0.015072