forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannels_last_ops.py
More file actions
153 lines (123 loc) · 5.24 KB
/
Copy pathchannels_last_ops.py
File metadata and controls
153 lines (123 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
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
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
152
153
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""The ``channels_last`` operator dialect.
Operators in this dialect interpret their activation input/output as channels-last
``(N, H, W, C)`` with contiguous strides and a fixed (identity) dim-order, as
opposed to the implicit dim-order handling used elsewhere. They let layout-handling
passes (see RFC #19299) make channels-last regions explicit in the graph.
Efficiency is a non-goal: kernels are implemented as ``permute -> aten op -> permute``.
Importing this module registers the dialect.
"""
import torch
from torch.library import Library, register_fake
lib = Library("channels_last", "DEF")
def _conv(
input, weight, bias, stride, padding, dilation, transposed, output_padding, groups
):
nchw = input.permute(0, 3, 1, 2)
out = torch.ops.aten.convolution(
nchw,
weight,
bias,
stride,
padding,
dilation,
transposed,
output_padding,
groups,
)
return out.permute(0, 2, 3, 1).contiguous()
def _avg_pool2d(
input, kernel_size, stride, padding, ceil_mode, count_include_pad, divisor_override
):
nchw = input.permute(0, 3, 1, 2)
out = torch.ops.aten.avg_pool2d(
nchw,
kernel_size,
stride,
padding,
ceil_mode,
count_include_pad,
divisor_override,
)
return out.permute(0, 2, 3, 1).contiguous()
def _adaptive_avg_pool2d(input, output_size):
nchw = input.permute(0, 3, 1, 2)
out = torch.ops.aten.adaptive_avg_pool2d(nchw, output_size)
return out.permute(0, 2, 3, 1).contiguous()
def _upsample_bilinear2d(input, output_size, align_corners, scale_factors):
nchw = input.permute(0, 3, 1, 2)
out = torch.ops.aten.upsample_bilinear2d.vec(
nchw, output_size, align_corners, scale_factors
)
return out.permute(0, 2, 3, 1).contiguous()
def _upsample_nearest2d(input, output_size, scale_factors):
nchw = input.permute(0, 3, 1, 2)
out = torch.ops.aten.upsample_nearest2d.vec(nchw, output_size, scale_factors)
return out.permute(0, 2, 3, 1).contiguous()
def _max_pool2d_with_indices(input, kernel_size, stride, padding, dilation, ceil_mode):
nchw = input.permute(0, 3, 1, 2)
values, indices = torch.ops.aten.max_pool2d_with_indices(
nchw, kernel_size, stride, padding, dilation, ceil_mode
)
values = values.permute(0, 2, 3, 1).contiguous()
indices = indices.permute(0, 2, 3, 1).contiguous()
return values, indices
def _grid_sampler_2d(input, grid, interpolation_mode, padding_mode, align_corners):
nchw = input.permute(0, 3, 1, 2)
out = torch.ops.aten.grid_sampler_2d(
nchw, grid, interpolation_mode, padding_mode, align_corners
)
return out.permute(0, 2, 3, 1).contiguous()
def _permute_copy(input, dims):
return torch.ops.aten.permute_copy(input, dims).contiguous()
lib.define(
"convolution(Tensor input, Tensor weight, Tensor? bias, int[] stride, "
"int[] padding, int[] dilation, bool transposed, int[] output_padding, "
"int groups) -> Tensor"
)
lib.impl("convolution", _conv, "CompositeExplicitAutograd")
register_fake("channels_last::convolution", _conv, lib=lib)
lib.define(
"avg_pool2d(Tensor input, int[2] kernel_size, int[2] stride, int[2] padding, "
"bool ceil_mode, bool count_include_pad, int? divisor_override) -> Tensor"
)
lib.impl("avg_pool2d", _avg_pool2d, "CompositeExplicitAutograd")
register_fake("channels_last::avg_pool2d", _avg_pool2d, lib=lib)
lib.define("adaptive_avg_pool2d(Tensor input, int[2] output_size) -> Tensor")
lib.impl("adaptive_avg_pool2d", _adaptive_avg_pool2d, "CompositeExplicitAutograd")
register_fake("channels_last::adaptive_avg_pool2d", _adaptive_avg_pool2d, lib=lib)
lib.define(
"upsample_bilinear2d(Tensor input, int[]? output_size, bool align_corners, "
"float[]? scale_factors) -> Tensor"
)
lib.impl("upsample_bilinear2d", _upsample_bilinear2d, "CompositeExplicitAutograd")
register_fake("channels_last::upsample_bilinear2d", _upsample_bilinear2d, lib=lib)
lib.define(
"upsample_nearest2d(Tensor input, int[]? output_size, float[]? scale_factors) "
"-> Tensor"
)
lib.impl("upsample_nearest2d", _upsample_nearest2d, "CompositeExplicitAutograd")
register_fake("channels_last::upsample_nearest2d", _upsample_nearest2d, lib=lib)
lib.define(
"max_pool2d_with_indices(Tensor input, int[2] kernel_size, int[2] stride, "
"int[2] padding, int[2] dilation, bool ceil_mode) -> (Tensor, Tensor)"
)
lib.impl(
"max_pool2d_with_indices", _max_pool2d_with_indices, "CompositeExplicitAutograd"
)
register_fake(
"channels_last::max_pool2d_with_indices", _max_pool2d_with_indices, lib=lib
)
lib.define(
"grid_sampler_2d(Tensor input, Tensor grid, int interpolation_mode, "
"int padding_mode, bool align_corners) -> Tensor"
)
lib.impl("grid_sampler_2d", _grid_sampler_2d, "CompositeExplicitAutograd")
register_fake("channels_last::grid_sampler_2d", _grid_sampler_2d, lib=lib)
lib.define("permute_copy(Tensor input, int[] dims) -> Tensor")
lib.impl("permute_copy", _permute_copy, "CompositeExplicitAutograd")
register_fake("channels_last::permute_copy", _permute_copy, lib=lib)