_meta_registrations.py 7.04 KB
Newer Older
1
2
import functools

Edward Z. Yang's avatar
Edward Z. Yang committed
3
import torch
4
import torch._custom_ops
Edward Z. Yang's avatar
Edward Z. Yang committed
5
6
7
8
9
10
import torch.library

# Ensure that torch.ops.torchvision is visible
import torchvision.extension  # noqa: F401


11
12
13
@functools.lru_cache(None)
def get_meta_lib():
    return torch.library.Library("torchvision", "IMPL", "Meta")
Edward Z. Yang's avatar
Edward Z. Yang committed
14
15


16
def register_meta(op_name, overload_name="default"):
Edward Z. Yang's avatar
Edward Z. Yang committed
17
    def wrapper(fn):
18
19
        if torchvision.extension._has_ops():
            get_meta_lib().impl(getattr(getattr(torch.ops.torchvision, op_name), overload_name), fn)
Edward Z. Yang's avatar
Edward Z. Yang committed
20
21
22
23
24
        return fn

    return wrapper


25
@register_meta("roi_align")
Edward Z. Yang's avatar
Edward Z. Yang committed
26
def meta_roi_align(input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio, aligned):
27
28
    torch._check(rois.size(1) == 5, lambda: "rois must have shape as Tensor[K, 5]")
    torch._check(
Edward Z. Yang's avatar
Edward Z. Yang committed
29
30
31
32
33
34
35
        input.dtype == rois.dtype,
        lambda: (
            "Expected tensor for input to have the same type as tensor for rois; "
            f"but type {input.dtype} does not equal {rois.dtype}"
        ),
    )
    num_rois = rois.size(0)
36
    channels = input.size(1)
Edward Z. Yang's avatar
Edward Z. Yang committed
37
38
39
    return input.new_empty((num_rois, channels, pooled_height, pooled_width))


40
@register_meta("_roi_align_backward")
Edward Z. Yang's avatar
Edward Z. Yang committed
41
42
def meta_roi_align_backward(
    grad, rois, spatial_scale, pooled_height, pooled_width, batch_size, channels, height, width, sampling_ratio, aligned
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
):
    torch._check(
        grad.dtype == rois.dtype,
        lambda: (
            "Expected tensor for grad to have the same type as tensor for rois; "
            f"but type {grad.dtype} does not equal {rois.dtype}"
        ),
    )
    return grad.new_empty((batch_size, channels, height, width))


@register_meta("ps_roi_align")
def meta_ps_roi_align(input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio):
    torch._check(rois.size(1) == 5, lambda: "rois must have shape as Tensor[K, 5]")
    torch._check(
        input.dtype == rois.dtype,
        lambda: (
            "Expected tensor for input to have the same type as tensor for rois; "
            f"but type {input.dtype} does not equal {rois.dtype}"
        ),
    )
    channels = input.size(1)
    torch._check(
        channels % (pooled_height * pooled_width) == 0,
        "input channels must be a multiple of pooling height * pooling width",
    )

    num_rois = rois.size(0)
    out_size = (num_rois, channels // (pooled_height * pooled_width), pooled_height, pooled_width)
    return input.new_empty(out_size), torch.empty(out_size, dtype=torch.int32, device="meta")


@register_meta("_ps_roi_align_backward")
def meta_ps_roi_align_backward(
    grad,
    rois,
    channel_mapping,
    spatial_scale,
    pooled_height,
    pooled_width,
    sampling_ratio,
    batch_size,
    channels,
    height,
    width,
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
):
    torch._check(
        grad.dtype == rois.dtype,
        lambda: (
            "Expected tensor for grad to have the same type as tensor for rois; "
            f"but type {grad.dtype} does not equal {rois.dtype}"
        ),
    )
    return grad.new_empty((batch_size, channels, height, width))


@register_meta("roi_pool")
def meta_roi_pool(input, rois, spatial_scale, pooled_height, pooled_width):
    torch._check(rois.size(1) == 5, lambda: "rois must have shape as Tensor[K, 5]")
    torch._check(
        input.dtype == rois.dtype,
        lambda: (
            "Expected tensor for input to have the same type as tensor for rois; "
            f"but type {input.dtype} does not equal {rois.dtype}"
        ),
    )
    num_rois = rois.size(0)
    channels = input.size(1)
    out_size = (num_rois, channels, pooled_height, pooled_width)
    return input.new_empty(out_size), torch.empty(out_size, device="meta", dtype=torch.int32)


@register_meta("_roi_pool_backward")
def meta_roi_pool_backward(
    grad, rois, argmax, spatial_scale, pooled_height, pooled_width, batch_size, channels, height, width
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
):
    torch._check(
        grad.dtype == rois.dtype,
        lambda: (
            "Expected tensor for grad to have the same type as tensor for rois; "
            f"but type {grad.dtype} does not equal {rois.dtype}"
        ),
    )
    return grad.new_empty((batch_size, channels, height, width))


@register_meta("ps_roi_pool")
def meta_ps_roi_pool(input, rois, spatial_scale, pooled_height, pooled_width):
    torch._check(rois.size(1) == 5, lambda: "rois must have shape as Tensor[K, 5]")
    torch._check(
        input.dtype == rois.dtype,
        lambda: (
            "Expected tensor for input to have the same type as tensor for rois; "
            f"but type {input.dtype} does not equal {rois.dtype}"
        ),
    )
    channels = input.size(1)
    torch._check(
        channels % (pooled_height * pooled_width) == 0,
        "input channels must be a multiple of pooling height * pooling width",
    )
    num_rois = rois.size(0)
    out_size = (num_rois, channels // (pooled_height * pooled_width), pooled_height, pooled_width)
    return input.new_empty(out_size), torch.empty(out_size, device="meta", dtype=torch.int32)


@register_meta("_ps_roi_pool_backward")
def meta_ps_roi_pool_backward(
    grad, rois, channel_mapping, spatial_scale, pooled_height, pooled_width, batch_size, channels, height, width
Edward Z. Yang's avatar
Edward Z. Yang committed
152
):
153
    torch._check(
Edward Z. Yang's avatar
Edward Z. Yang committed
154
155
156
157
158
159
160
        grad.dtype == rois.dtype,
        lambda: (
            "Expected tensor for grad to have the same type as tensor for rois; "
            f"but type {grad.dtype} does not equal {rois.dtype}"
        ),
    )
    return grad.new_empty((batch_size, channels, height, width))
161
162
163
164
165
166
167
168
169
170
171
172
173
174


@torch._custom_ops.impl_abstract("torchvision::nms")
def meta_nms(dets, scores, iou_threshold):
    torch._check(dets.dim() == 2, lambda: f"boxes should be a 2d tensor, got {dets.dim()}D")
    torch._check(dets.size(1) == 4, lambda: f"boxes should have 4 elements in dimension 1, got {dets.size(1)}")
    torch._check(scores.dim() == 1, lambda: f"scores should be a 1d tensor, got {scores.dim()}")
    torch._check(
        dets.size(0) == scores.size(0),
        lambda: f"boxes and scores should have same number of elements in dimension 0, got {dets.size(0)} and {scores.size(0)}",
    )
    ctx = torch._custom_ops.get_ctx()
    num_to_keep = ctx.create_unbacked_symint()
    return dets.new_empty(num_to_keep, dtype=torch.long)
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225


@register_meta("deform_conv2d")
def meta_deform_conv2d(
    input,
    weight,
    offset,
    mask,
    bias,
    stride_h,
    stride_w,
    pad_h,
    pad_w,
    dil_h,
    dil_w,
    n_weight_grps,
    n_offset_grps,
    use_mask,
):

    out_height, out_width = offset.shape[-2:]
    out_channels = weight.shape[0]
    batch_size = input.shape[0]
    return input.new_empty((batch_size, out_channels, out_height, out_width))


@register_meta("_deform_conv2d_backward")
def meta_deform_conv2d_backward(
    grad,
    input,
    weight,
    offset,
    mask,
    bias,
    stride_h,
    stride_w,
    pad_h,
    pad_w,
    dilation_h,
    dilation_w,
    groups,
    offset_groups,
    use_mask,
):

    grad_input = input.new_empty(input.shape)
    grad_weight = weight.new_empty(weight.shape)
    grad_offset = offset.new_empty(offset.shape)
    grad_mask = mask.new_empty(mask.shape)
    grad_bias = bias.new_empty(bias.shape)
    return grad_input, grad_weight, grad_offset, grad_mask, grad_bias