ai-edge-torch-nightly 0.3.0.dev20240828__py3-none-any.whl → 0.3.0.dev20240829__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of ai-edge-torch-nightly might be problematic. Click here for more details.
- ai_edge_torch/_convert/fx_passes/optimize_layout_transposes_pass/layout_rewrite.py +6 -1
- ai_edge_torch/_convert/test/test_convert.py +1 -1
- ai_edge_torch/_convert/test/test_convert_composites.py +1 -1
- ai_edge_torch/_convert/test/test_convert_multisig.py +1 -1
- ai_edge_torch/_convert/test/test_to_channel_last_io.py +1 -1
- ai_edge_torch/debug/test/test_culprit.py +1 -1
- ai_edge_torch/debug/test/test_search_model.py +1 -1
- ai_edge_torch/generative/test/test_experimental_ekv.py +1 -1
- ai_edge_torch/generative/test/test_loader.py +1 -1
- ai_edge_torch/generative/test/test_model_conversion.py +1 -1
- ai_edge_torch/generative/test/test_quantize.py +1 -1
- ai_edge_torch/hlfb/test/test_mark_pattern.py +1 -1
- ai_edge_torch/hlfb/test/test_stablehlo_composite_builder.py +1 -1
- ai_edge_torch/lowertools/odml_torch_utils.py +5 -1
- ai_edge_torch/lowertools/test_utils.py +1 -1
- ai_edge_torch/odml_torch/__init__.py +20 -0
- ai_edge_torch/odml_torch/_torch_future.py +61 -0
- ai_edge_torch/odml_torch/_torch_library.py +19 -0
- ai_edge_torch/odml_torch/composite/__init__.py +16 -0
- ai_edge_torch/odml_torch/composite/mark_tensor.py +120 -0
- ai_edge_torch/odml_torch/composite/stablehlo_composite_builder.py +106 -0
- ai_edge_torch/odml_torch/debuginfo/__init__.py +16 -0
- ai_edge_torch/odml_torch/debuginfo/_build.py +43 -0
- ai_edge_torch/odml_torch/debuginfo/_op_polyfill.py +55 -0
- ai_edge_torch/odml_torch/export.py +320 -0
- ai_edge_torch/odml_torch/export_utils.py +168 -0
- ai_edge_torch/odml_torch/jax_bridge/__init__.py +15 -0
- ai_edge_torch/odml_torch/jax_bridge/_wrap.py +152 -0
- ai_edge_torch/odml_torch/jax_bridge/utils.py +75 -0
- ai_edge_torch/odml_torch/lowerings/__init__.py +24 -0
- ai_edge_torch/odml_torch/lowerings/_basic.py +204 -0
- ai_edge_torch/odml_torch/lowerings/_batch_norm.py +65 -0
- ai_edge_torch/odml_torch/lowerings/_convolution.py +119 -0
- ai_edge_torch/odml_torch/lowerings/_jax_lowerings.py +255 -0
- ai_edge_torch/odml_torch/lowerings/context.py +42 -0
- ai_edge_torch/odml_torch/lowerings/registry.py +87 -0
- ai_edge_torch/odml_torch/lowerings/utils.py +185 -0
- ai_edge_torch/odml_torch/passes/__init__.py +38 -0
- ai_edge_torch/odml_torch/tf_integration.py +194 -0
- ai_edge_torch/version.py +1 -1
- {ai_edge_torch_nightly-0.3.0.dev20240828.dist-info → ai_edge_torch_nightly-0.3.0.dev20240829.dist-info}/METADATA +1 -1
- {ai_edge_torch_nightly-0.3.0.dev20240828.dist-info → ai_edge_torch_nightly-0.3.0.dev20240829.dist-info}/RECORD +45 -21
- {ai_edge_torch_nightly-0.3.0.dev20240828.dist-info → ai_edge_torch_nightly-0.3.0.dev20240829.dist-info}/LICENSE +0 -0
- {ai_edge_torch_nightly-0.3.0.dev20240828.dist-info → ai_edge_torch_nightly-0.3.0.dev20240829.dist-info}/WHEEL +0 -0
- {ai_edge_torch_nightly-0.3.0.dev20240828.dist-info → ai_edge_torch_nightly-0.3.0.dev20240829.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
import functools
|
|
16
|
+
import logging
|
|
17
|
+
|
|
18
|
+
from ai_edge_torch.odml_torch import jax_bridge
|
|
19
|
+
import torch
|
|
20
|
+
import torch_xla2.ops.jaten # Import to load torch_xla2 ops
|
|
21
|
+
import torch_xla2.ops.ops_registry # Import to load torch_xla2 ops
|
|
22
|
+
|
|
23
|
+
from . import registry
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@functools.cache
|
|
27
|
+
def _log_usage(op):
|
|
28
|
+
logging.warning("Use jax lowering: %s", str(op))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def lower_by_jax(op, ir_input_names=None):
|
|
32
|
+
def inner(lowering):
|
|
33
|
+
bridged = jax_bridge.wrap(lowering, ir_input_names)
|
|
34
|
+
|
|
35
|
+
@registry.lower(op)
|
|
36
|
+
def _jax_lowering(lctx, *args, **kwargs):
|
|
37
|
+
_log_usage(op)
|
|
38
|
+
return bridged(lctx, *args, **kwargs)
|
|
39
|
+
|
|
40
|
+
return lowering
|
|
41
|
+
|
|
42
|
+
return inner
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
_TORCH_XLA2_IMPLS = {
|
|
46
|
+
key: val.func
|
|
47
|
+
for key, val in torch_xla2.ops.ops_registry.all_aten_ops.items()
|
|
48
|
+
if val.is_jax_function
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def lower_by_torch_xla2(op):
|
|
53
|
+
return lower_by_jax(op)(_TORCH_XLA2_IMPLS[op])
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
lower_by_torch_xla2(torch.ops.aten._adaptive_avg_pool2d)
|
|
57
|
+
lower_by_torch_xla2(torch.ops.aten._adaptive_avg_pool3d)
|
|
58
|
+
lower_by_torch_xla2(torch.ops.aten._cdist_forward)
|
|
59
|
+
lower_by_torch_xla2(torch.ops.aten._local_scalar_dense)
|
|
60
|
+
lower_by_torch_xla2(torch.ops.aten._local_scalar_dense)
|
|
61
|
+
lower_by_torch_xla2(torch.ops.aten._log_softmax)
|
|
62
|
+
lower_by_torch_xla2(torch.ops.aten._native_batch_norm_legit)
|
|
63
|
+
lower_by_torch_xla2(torch.ops.aten._native_batch_norm_legit_no_training)
|
|
64
|
+
lower_by_torch_xla2(torch.ops.aten._pdist_forward)
|
|
65
|
+
lower_by_torch_xla2(torch.ops.aten._softmax)
|
|
66
|
+
lower_by_torch_xla2(torch.ops.aten._to_copy)
|
|
67
|
+
lower_by_torch_xla2(torch.ops.aten._unsafe_index)
|
|
68
|
+
lower_by_torch_xla2(torch.ops.aten._unsafe_view)
|
|
69
|
+
lower_by_torch_xla2(torch.ops.aten.abs)
|
|
70
|
+
lower_by_torch_xla2(torch.ops.aten.acos)
|
|
71
|
+
lower_by_torch_xla2(torch.ops.aten.acosh)
|
|
72
|
+
lower_by_torch_xla2(torch.ops.aten.add.Scalar)
|
|
73
|
+
lower_by_torch_xla2(torch.ops.aten.add.Tensor)
|
|
74
|
+
lower_by_torch_xla2(torch.ops.aten.addbmm.default)
|
|
75
|
+
lower_by_torch_xla2(torch.ops.aten.addmm)
|
|
76
|
+
lower_by_torch_xla2(torch.ops.aten.addmv)
|
|
77
|
+
lower_by_torch_xla2(torch.ops.aten.alias)
|
|
78
|
+
lower_by_torch_xla2(torch.ops.aten.allclose)
|
|
79
|
+
lower_by_torch_xla2(torch.ops.aten.amax)
|
|
80
|
+
lower_by_torch_xla2(torch.ops.aten.amin)
|
|
81
|
+
lower_by_torch_xla2(torch.ops.aten.any)
|
|
82
|
+
lower_by_torch_xla2(torch.ops.aten.arange.default)
|
|
83
|
+
lower_by_torch_xla2(torch.ops.aten.arange.start)
|
|
84
|
+
lower_by_torch_xla2(torch.ops.aten.arange.start_step)
|
|
85
|
+
lower_by_torch_xla2(torch.ops.aten.argmax)
|
|
86
|
+
lower_by_torch_xla2(torch.ops.aten.argmin)
|
|
87
|
+
lower_by_torch_xla2(torch.ops.aten.as_strided)
|
|
88
|
+
lower_by_torch_xla2(torch.ops.aten.as_strided_copy)
|
|
89
|
+
lower_by_torch_xla2(torch.ops.aten.asin)
|
|
90
|
+
lower_by_torch_xla2(torch.ops.aten.asinh)
|
|
91
|
+
lower_by_torch_xla2(torch.ops.aten.atan)
|
|
92
|
+
lower_by_torch_xla2(torch.ops.aten.atan2)
|
|
93
|
+
lower_by_torch_xla2(torch.ops.aten.atanh)
|
|
94
|
+
lower_by_torch_xla2(torch.ops.aten.avg_pool2d)
|
|
95
|
+
lower_by_torch_xla2(torch.ops.aten.avg_pool3d)
|
|
96
|
+
lower_by_torch_xla2(torch.ops.aten.bitwise_and)
|
|
97
|
+
lower_by_torch_xla2(torch.ops.aten.bitwise_not)
|
|
98
|
+
lower_by_torch_xla2(torch.ops.aten.bitwise_or)
|
|
99
|
+
lower_by_torch_xla2(torch.ops.aten.bitwise_xor)
|
|
100
|
+
lower_by_torch_xla2(torch.ops.aten.bmm)
|
|
101
|
+
lower_by_torch_xla2(torch.ops.aten.cat)
|
|
102
|
+
lower_by_torch_xla2(torch.ops.aten.ceil)
|
|
103
|
+
lower_by_torch_xla2(torch.ops.aten.clamp.Tensor)
|
|
104
|
+
lower_by_torch_xla2(torch.ops.aten.clamp.default)
|
|
105
|
+
lower_by_torch_xla2(torch.ops.aten.clone)
|
|
106
|
+
lower_by_torch_xla2(torch.ops.aten.clone.default)
|
|
107
|
+
lower_by_torch_xla2(torch.ops.aten.constant_pad_nd)
|
|
108
|
+
lower_by_torch_xla2(torch.ops.aten.convolution)
|
|
109
|
+
lower_by_torch_xla2(torch.ops.aten.cos)
|
|
110
|
+
lower_by_torch_xla2(torch.ops.aten.cosh)
|
|
111
|
+
lower_by_torch_xla2(torch.ops.aten.cumsum)
|
|
112
|
+
lower_by_torch_xla2(torch.ops.aten.detach)
|
|
113
|
+
lower_by_torch_xla2(torch.ops.aten.diagonal)
|
|
114
|
+
lower_by_torch_xla2(torch.ops.aten.div)
|
|
115
|
+
lower_by_torch_xla2(torch.ops.aten.dot)
|
|
116
|
+
lower_by_torch_xla2(torch.ops.aten.embedding)
|
|
117
|
+
lower_by_torch_xla2(torch.ops.aten.empty)
|
|
118
|
+
lower_by_torch_xla2(torch.ops.aten.eq)
|
|
119
|
+
lower_by_torch_xla2(torch.ops.aten.erf)
|
|
120
|
+
lower_by_torch_xla2(torch.ops.aten.exp)
|
|
121
|
+
lower_by_torch_xla2(torch.ops.aten.expand)
|
|
122
|
+
lower_by_torch_xla2(torch.ops.aten.expand_copy)
|
|
123
|
+
lower_by_torch_xla2(torch.ops.aten.expm1)
|
|
124
|
+
lower_by_torch_xla2(torch.ops.aten.fill)
|
|
125
|
+
lower_by_torch_xla2(torch.ops.aten.flip)
|
|
126
|
+
lower_by_torch_xla2(torch.ops.aten.floor)
|
|
127
|
+
lower_by_torch_xla2(torch.ops.aten.fmod)
|
|
128
|
+
lower_by_torch_xla2(torch.ops.aten.full)
|
|
129
|
+
lower_by_torch_xla2(torch.ops.aten.full_like)
|
|
130
|
+
lower_by_torch_xla2(torch.ops.aten.gather)
|
|
131
|
+
lower_by_torch_xla2(torch.ops.aten.ge)
|
|
132
|
+
lower_by_torch_xla2(torch.ops.aten.gelu)
|
|
133
|
+
lower_by_torch_xla2(torch.ops.aten.glu)
|
|
134
|
+
lower_by_torch_xla2(torch.ops.aten.glu.default)
|
|
135
|
+
lower_by_torch_xla2(torch.ops.aten.gt)
|
|
136
|
+
lower_by_torch_xla2(torch.ops.aten.hardtanh)
|
|
137
|
+
lower_by_torch_xla2(torch.ops.aten.index)
|
|
138
|
+
lower_by_torch_xla2(torch.ops.aten.index.Tensor)
|
|
139
|
+
lower_by_torch_xla2(torch.ops.aten.index_copy)
|
|
140
|
+
lower_by_torch_xla2(torch.ops.aten.index_put)
|
|
141
|
+
lower_by_torch_xla2(torch.ops.aten.index_select)
|
|
142
|
+
lower_by_torch_xla2(torch.ops.aten.isinf)
|
|
143
|
+
lower_by_torch_xla2(torch.ops.aten.isnan)
|
|
144
|
+
lower_by_torch_xla2(torch.ops.aten.le)
|
|
145
|
+
lower_by_torch_xla2(torch.ops.aten.leaky_relu)
|
|
146
|
+
lower_by_torch_xla2(torch.ops.aten.lift_fresh_copy)
|
|
147
|
+
lower_by_torch_xla2(torch.ops.aten.linalg_vector_norm)
|
|
148
|
+
lower_by_torch_xla2(torch.ops.aten.log)
|
|
149
|
+
lower_by_torch_xla2(torch.ops.aten.log10)
|
|
150
|
+
lower_by_torch_xla2(torch.ops.aten.log1p)
|
|
151
|
+
lower_by_torch_xla2(torch.ops.aten.log2)
|
|
152
|
+
lower_by_torch_xla2(torch.ops.aten.logical_and)
|
|
153
|
+
lower_by_torch_xla2(torch.ops.aten.logical_not)
|
|
154
|
+
lower_by_torch_xla2(torch.ops.aten.logical_or)
|
|
155
|
+
lower_by_torch_xla2(torch.ops.aten.logical_xor)
|
|
156
|
+
lower_by_torch_xla2(torch.ops.aten.lt)
|
|
157
|
+
lower_by_torch_xla2(torch.ops.aten.max)
|
|
158
|
+
lower_by_torch_xla2(torch.ops.aten.max_pool2d_with_indices)
|
|
159
|
+
lower_by_torch_xla2(torch.ops.aten.max_pool2d_with_indices_backward)
|
|
160
|
+
lower_by_torch_xla2(torch.ops.aten.max_pool2d_with_indices_backward)
|
|
161
|
+
lower_by_torch_xla2(torch.ops.aten.max_pool3d_with_indices)
|
|
162
|
+
lower_by_torch_xla2(torch.ops.aten.maximum)
|
|
163
|
+
lower_by_torch_xla2(torch.ops.aten.mean)
|
|
164
|
+
lower_by_torch_xla2(torch.ops.aten.min)
|
|
165
|
+
lower_by_torch_xla2(torch.ops.aten.minimum)
|
|
166
|
+
lower_by_torch_xla2(torch.ops.aten.mm)
|
|
167
|
+
lower_by_torch_xla2(torch.ops.aten.mul.Scalar)
|
|
168
|
+
lower_by_torch_xla2(torch.ops.aten.mul.Tensor)
|
|
169
|
+
lower_by_torch_xla2(torch.ops.aten.native_batch_norm)
|
|
170
|
+
lower_by_torch_xla2(torch.ops.aten.native_group_norm)
|
|
171
|
+
lower_by_torch_xla2(torch.ops.aten.native_layer_norm)
|
|
172
|
+
lower_by_torch_xla2(torch.ops.aten.native_layer_norm_backward)
|
|
173
|
+
lower_by_torch_xla2(torch.ops.aten.ne)
|
|
174
|
+
lower_by_torch_xla2(torch.ops.aten.neg)
|
|
175
|
+
lower_by_torch_xla2(torch.ops.aten.nonzero)
|
|
176
|
+
lower_by_torch_xla2(torch.ops.aten.outer)
|
|
177
|
+
lower_by_torch_xla2(torch.ops.aten.permute)
|
|
178
|
+
lower_by_torch_xla2(torch.ops.aten.permute_copy)
|
|
179
|
+
lower_by_torch_xla2(torch.ops.aten.pixel_shuffle)
|
|
180
|
+
lower_by_torch_xla2(torch.ops.aten.pow)
|
|
181
|
+
lower_by_torch_xla2(torch.ops.aten.prod)
|
|
182
|
+
lower_by_torch_xla2(torch.ops.aten.rand)
|
|
183
|
+
lower_by_torch_xla2(torch.ops.aten.randn)
|
|
184
|
+
lower_by_torch_xla2(torch.ops.aten.reciprocal)
|
|
185
|
+
lower_by_torch_xla2(torch.ops.aten.reflection_pad1d)
|
|
186
|
+
lower_by_torch_xla2(torch.ops.aten.relu)
|
|
187
|
+
lower_by_torch_xla2(torch.ops.aten.remainder)
|
|
188
|
+
lower_by_torch_xla2(torch.ops.aten.repeat)
|
|
189
|
+
lower_by_torch_xla2(torch.ops.aten.reshape)
|
|
190
|
+
lower_by_torch_xla2(torch.ops.aten.roll)
|
|
191
|
+
lower_by_torch_xla2(torch.ops.aten.round)
|
|
192
|
+
lower_by_torch_xla2(torch.ops.aten.rsqrt)
|
|
193
|
+
lower_by_torch_xla2(torch.ops.aten.scalar_tensor)
|
|
194
|
+
lower_by_torch_xla2(torch.ops.aten.scatter.src)
|
|
195
|
+
lower_by_torch_xla2(torch.ops.aten.scatter.value)
|
|
196
|
+
lower_by_torch_xla2(torch.ops.aten.scatter_add)
|
|
197
|
+
lower_by_torch_xla2(torch.ops.aten.scatter_reduce)
|
|
198
|
+
lower_by_torch_xla2(torch.ops.aten.select)
|
|
199
|
+
lower_by_torch_xla2(torch.ops.aten.select_copy)
|
|
200
|
+
lower_by_torch_xla2(torch.ops.aten.select_scatter)
|
|
201
|
+
lower_by_torch_xla2(torch.ops.aten.sigmoid)
|
|
202
|
+
lower_by_torch_xla2(torch.ops.aten.sign)
|
|
203
|
+
lower_by_torch_xla2(torch.ops.aten.silu)
|
|
204
|
+
lower_by_torch_xla2(torch.ops.aten.sin)
|
|
205
|
+
lower_by_torch_xla2(torch.ops.aten.sinh)
|
|
206
|
+
lower_by_torch_xla2(torch.ops.aten.slice)
|
|
207
|
+
lower_by_torch_xla2(torch.ops.aten.slice_copy)
|
|
208
|
+
lower_by_torch_xla2(torch.ops.aten.slice_scatter)
|
|
209
|
+
lower_by_torch_xla2(torch.ops.aten.sort)
|
|
210
|
+
lower_by_torch_xla2(torch.ops.aten.split)
|
|
211
|
+
lower_by_torch_xla2(torch.ops.aten.split_copy)
|
|
212
|
+
lower_by_torch_xla2(torch.ops.aten.split_with_sizes)
|
|
213
|
+
lower_by_torch_xla2(torch.ops.aten.sqrt)
|
|
214
|
+
lower_by_torch_xla2(torch.ops.aten.squeeze)
|
|
215
|
+
lower_by_torch_xla2(torch.ops.aten.squeeze_copy)
|
|
216
|
+
lower_by_torch_xla2(torch.ops.aten.stack)
|
|
217
|
+
lower_by_torch_xla2(torch.ops.aten.sub.Scalar)
|
|
218
|
+
lower_by_torch_xla2(torch.ops.aten.sub.Tensor)
|
|
219
|
+
lower_by_torch_xla2(torch.ops.aten.sum)
|
|
220
|
+
lower_by_torch_xla2(torch.ops.aten.sym_size)
|
|
221
|
+
lower_by_torch_xla2(torch.ops.aten.t)
|
|
222
|
+
lower_by_torch_xla2(torch.ops.aten.tan)
|
|
223
|
+
lower_by_torch_xla2(torch.ops.aten.tanh)
|
|
224
|
+
lower_by_torch_xla2(torch.ops.aten.tensor_split.sections)
|
|
225
|
+
lower_by_torch_xla2(torch.ops.aten.tensor_split.sections)
|
|
226
|
+
lower_by_torch_xla2(torch.ops.aten.to.device)
|
|
227
|
+
lower_by_torch_xla2(torch.ops.aten.to.device)
|
|
228
|
+
lower_by_torch_xla2(torch.ops.aten.to.dtype)
|
|
229
|
+
lower_by_torch_xla2(torch.ops.aten.topk)
|
|
230
|
+
lower_by_torch_xla2(torch.ops.aten.transpose)
|
|
231
|
+
lower_by_torch_xla2(torch.ops.aten.transpose_copy)
|
|
232
|
+
lower_by_torch_xla2(torch.ops.aten.triu)
|
|
233
|
+
lower_by_torch_xla2(torch.ops.aten.true_divide)
|
|
234
|
+
lower_by_torch_xla2(torch.ops.aten.trunc)
|
|
235
|
+
lower_by_torch_xla2(torch.ops.aten.unbind)
|
|
236
|
+
lower_by_torch_xla2(torch.ops.aten.unbind_copy)
|
|
237
|
+
lower_by_torch_xla2(torch.ops.aten.unsqueeze)
|
|
238
|
+
lower_by_torch_xla2(torch.ops.aten.unsqueeze.default)
|
|
239
|
+
lower_by_torch_xla2(torch.ops.aten.unsqueeze_copy)
|
|
240
|
+
lower_by_torch_xla2(torch.ops.aten.var.correction)
|
|
241
|
+
lower_by_torch_xla2(torch.ops.aten.var_mean.correction)
|
|
242
|
+
lower_by_torch_xla2(torch.ops.aten.view)
|
|
243
|
+
lower_by_torch_xla2(torch.ops.aten.view_as_complex)
|
|
244
|
+
lower_by_torch_xla2(torch.ops.aten.view_as_real)
|
|
245
|
+
lower_by_torch_xla2(torch.ops.aten.view_copy)
|
|
246
|
+
lower_by_torch_xla2(torch.ops.aten.where.ScalarOther)
|
|
247
|
+
lower_by_torch_xla2(torch.ops.aten.where.ScalarSelf)
|
|
248
|
+
lower_by_torch_xla2(torch.ops.aten.where.self)
|
|
249
|
+
lower_by_torch_xla2(torch.ops.prims.broadcast_in_dim)
|
|
250
|
+
lower_by_torch_xla2(torch.ops.prims.var)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
@lower_by_jax(torch.ops.aten.copy, ir_input_names=["src"])
|
|
254
|
+
def _aten_copy(self, src, **kwargs):
|
|
255
|
+
return _TORCH_XLA2_IMPLS[torch.ops.aten.copy](self, src)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
"""Define context object for export and MLIR lowerings."""
|
|
16
|
+
|
|
17
|
+
import dataclasses
|
|
18
|
+
from jax._src.lib.mlir import ir
|
|
19
|
+
import torch
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclasses.dataclass
|
|
23
|
+
class LoweringContext:
|
|
24
|
+
"""The context object used in export interpreter and MLIR lowerings."""
|
|
25
|
+
|
|
26
|
+
ir_context: ir.Context
|
|
27
|
+
ir_module: ir.Module
|
|
28
|
+
ir_location: ir.Location = None
|
|
29
|
+
node: torch.fx.Node = None
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def ctx(self):
|
|
33
|
+
"""Shortcut for ir_context."""
|
|
34
|
+
return self.ir_context
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def loc(self):
|
|
38
|
+
"""Shortcut for ir_location."""
|
|
39
|
+
return self.ir_location
|
|
40
|
+
|
|
41
|
+
def replace(self, **kwargs):
|
|
42
|
+
return dataclasses.replace(self, **kwargs)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
"""Torch op decompositions and MLIR lowerings registry."""
|
|
16
|
+
|
|
17
|
+
from typing import Any, Callable
|
|
18
|
+
|
|
19
|
+
import torch
|
|
20
|
+
|
|
21
|
+
from . import context
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class LoweringRegistry:
|
|
25
|
+
"""Registry object for torch op decompositions and to-MLIR lowerings."""
|
|
26
|
+
|
|
27
|
+
def __init__(self):
|
|
28
|
+
self.registered_ops = {}
|
|
29
|
+
self.decompositions = {}
|
|
30
|
+
|
|
31
|
+
def lookup(self, op_or_name):
|
|
32
|
+
candidate = self._get_lowering(op_or_name)
|
|
33
|
+
if candidate is None:
|
|
34
|
+
if isinstance(op_or_name, torch._ops.OpOverloadPacket):
|
|
35
|
+
candidate = self._get_lowering(op_or_name.default)
|
|
36
|
+
if isinstance(op_or_name, torch._ops.OpOverload):
|
|
37
|
+
candidate = self._get_lowering(op_or_name.overloadpacket)
|
|
38
|
+
return candidate
|
|
39
|
+
|
|
40
|
+
def _get_lowering(self, op):
|
|
41
|
+
candidate = self.registered_ops.get(op)
|
|
42
|
+
return candidate
|
|
43
|
+
|
|
44
|
+
def register(self, op, lowering):
|
|
45
|
+
if isinstance(op, torch._ops.OpOverloadPacket):
|
|
46
|
+
ops = [getattr(op, overload) for overload in op.overloads()]
|
|
47
|
+
else:
|
|
48
|
+
ops = [op]
|
|
49
|
+
|
|
50
|
+
for op in ops:
|
|
51
|
+
self.registered_ops[op] = lowering
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
global_registry = LoweringRegistry()
|
|
55
|
+
global_registry.decompositions.update(
|
|
56
|
+
torch._decomp.get_decompositions([
|
|
57
|
+
torch.ops.aten.upsample_nearest2d,
|
|
58
|
+
torch.ops.aten._native_batch_norm_legit.no_stats,
|
|
59
|
+
torch.ops.aten._adaptive_avg_pool2d,
|
|
60
|
+
torch.ops.aten._adaptive_avg_pool3d,
|
|
61
|
+
torch.ops.aten.grid_sampler_2d,
|
|
62
|
+
torch.ops.aten.native_dropout,
|
|
63
|
+
torch.ops.aten.reflection_pad1d,
|
|
64
|
+
torch.ops.aten.reflection_pad2d,
|
|
65
|
+
torch.ops.aten.reflection_pad3d,
|
|
66
|
+
torch.ops.aten.replication_pad1d,
|
|
67
|
+
torch.ops.aten.replication_pad2d,
|
|
68
|
+
torch.ops.aten.replication_pad3d,
|
|
69
|
+
torch.ops.aten.addmm,
|
|
70
|
+
])
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def lookup(op):
|
|
75
|
+
return global_registry.lookup(op)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def lower(op):
|
|
79
|
+
def inner(lowering: Callable[[context.LoweringContext, ...], Any]):
|
|
80
|
+
global_registry.register(op, lowering)
|
|
81
|
+
return lowering
|
|
82
|
+
|
|
83
|
+
return inner
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def decompositions():
|
|
87
|
+
return global_registry.decompositions
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
"""Utilities for building MLIR lowerings."""
|
|
16
|
+
|
|
17
|
+
import numbers
|
|
18
|
+
from typing import Any
|
|
19
|
+
from typing import Optional
|
|
20
|
+
|
|
21
|
+
from jax._src.lib.mlir import ir
|
|
22
|
+
from jax._src.lib.mlir.dialects import hlo as stablehlo
|
|
23
|
+
import numpy as np
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def splat(val, ty, shape=tuple(), *, loc: Optional[Any] = None):
|
|
27
|
+
if isinstance(ty, ir.IntegerType):
|
|
28
|
+
if ty.width == 1:
|
|
29
|
+
attr = ir.BoolAttr.get(bool(val))
|
|
30
|
+
else:
|
|
31
|
+
attr = ir.IntegerAttr.get(ty, int(val))
|
|
32
|
+
elif isinstance(ty, ir.FloatType):
|
|
33
|
+
attr = ir.FloatAttr.get(ty, val)
|
|
34
|
+
else:
|
|
35
|
+
raise ValueError("Unsupported type: %s" % str(ty))
|
|
36
|
+
|
|
37
|
+
return stablehlo.constant(
|
|
38
|
+
ir.DenseElementsAttr.get_splat(
|
|
39
|
+
ir.RankedTensorType.get(shape, ty),
|
|
40
|
+
attr,
|
|
41
|
+
),
|
|
42
|
+
loc=loc,
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def get_common_broadcast_shape(
|
|
47
|
+
shape_1: list[int], shape_2: list[int]
|
|
48
|
+
) -> Optional[list[int]]:
|
|
49
|
+
if not shape_1 and not shape_2:
|
|
50
|
+
return None
|
|
51
|
+
|
|
52
|
+
shape_1 = shape_1 if shape_1 else [1]
|
|
53
|
+
shape_2 = shape_2 if shape_2 else [1]
|
|
54
|
+
|
|
55
|
+
length_diff = abs(len(shape_1) - len(shape_2))
|
|
56
|
+
if len(shape_1) < len(shape_2):
|
|
57
|
+
shape_1 = [1] * length_diff + shape_1
|
|
58
|
+
elif len(shape_1) > len(shape_2):
|
|
59
|
+
shape_2 = [1] * length_diff + shape_2
|
|
60
|
+
|
|
61
|
+
common_broadcast_shape = []
|
|
62
|
+
for idx in reversed(range(len(shape_1))):
|
|
63
|
+
dim_size1 = shape_1[idx]
|
|
64
|
+
dim_size2 = shape_2[idx]
|
|
65
|
+
|
|
66
|
+
if dim_size1 == dim_size2:
|
|
67
|
+
common_broadcast_shape.insert(0, dim_size1)
|
|
68
|
+
elif dim_size1 == 1 or dim_size2 == 1:
|
|
69
|
+
common_broadcast_shape.insert(0, max(dim_size1, dim_size2))
|
|
70
|
+
else:
|
|
71
|
+
return None
|
|
72
|
+
|
|
73
|
+
return common_broadcast_shape
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def get_broadcast_dimensions(
|
|
77
|
+
shape_from: list[int], shape_to: list[int]
|
|
78
|
+
) -> list[int]:
|
|
79
|
+
assert get_common_broadcast_shape(shape_from, shape_to) == shape_to
|
|
80
|
+
|
|
81
|
+
ret = []
|
|
82
|
+
for val in range(len(shape_to) - len(shape_from), len(shape_to)):
|
|
83
|
+
ret.append(val)
|
|
84
|
+
|
|
85
|
+
return ir.DenseI64ArrayAttr.get(np.asarray(ret, np.int64))
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def broadcast_args_if_needed(
|
|
89
|
+
val_1: ir.Value, val_2: ir.Value
|
|
90
|
+
) -> tuple[Optional[ir.Value], Optional[ir.Value]]:
|
|
91
|
+
broadcast_shape = get_common_broadcast_shape(
|
|
92
|
+
val_1.type.shape, val_2.type.shape
|
|
93
|
+
)
|
|
94
|
+
if broadcast_shape is None:
|
|
95
|
+
return None, None
|
|
96
|
+
|
|
97
|
+
new_val_1, new_val_2 = val_1, val_2
|
|
98
|
+
|
|
99
|
+
if val_1.type.shape != broadcast_shape:
|
|
100
|
+
new_val_1 = stablehlo.broadcast_in_dim(
|
|
101
|
+
result=ir.RankedTensorType.get(
|
|
102
|
+
broadcast_shape, val_1.type.element_type
|
|
103
|
+
),
|
|
104
|
+
operand=val_1,
|
|
105
|
+
broadcast_dimensions=get_broadcast_dimensions(
|
|
106
|
+
val_1.type.shape, broadcast_shape
|
|
107
|
+
),
|
|
108
|
+
)
|
|
109
|
+
if val_2.type.shape != broadcast_shape:
|
|
110
|
+
new_val_2 = stablehlo.broadcast_in_dim(
|
|
111
|
+
result=ir.RankedTensorType.get(
|
|
112
|
+
broadcast_shape, val_2.type.element_type
|
|
113
|
+
),
|
|
114
|
+
operand=val_2,
|
|
115
|
+
broadcast_dimensions=get_broadcast_dimensions(
|
|
116
|
+
val_2.type.shape, broadcast_shape
|
|
117
|
+
),
|
|
118
|
+
)
|
|
119
|
+
return new_val_1, new_val_2
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def upcast_to_same_type(*vals: ir.Value):
|
|
123
|
+
if not vals:
|
|
124
|
+
return None
|
|
125
|
+
if len(vals) == 1:
|
|
126
|
+
return vals[0]
|
|
127
|
+
|
|
128
|
+
def get_priority(ty: ir.Type):
|
|
129
|
+
priorities = [
|
|
130
|
+
ir.IntegerType.get_signless(1),
|
|
131
|
+
ir.IntegerType.get_signless(16),
|
|
132
|
+
ir.IntegerType.get_signless(32),
|
|
133
|
+
ir.IntegerType.get_signless(64),
|
|
134
|
+
ir.F16Type,
|
|
135
|
+
ir.F32Type,
|
|
136
|
+
ir.F64Type,
|
|
137
|
+
]
|
|
138
|
+
for i, tycls in enumerate(priorities):
|
|
139
|
+
if tycls.isinstance(ty):
|
|
140
|
+
return i
|
|
141
|
+
raise ValueError("Unsupported type: %s" % str(ty))
|
|
142
|
+
|
|
143
|
+
cast_tycls = type(max([v.type.element_type for v in vals], key=get_priority))
|
|
144
|
+
new_vals = []
|
|
145
|
+
for val in vals:
|
|
146
|
+
if not cast_tycls.isinstance(val.type.element_type):
|
|
147
|
+
val = stablehlo.convert(
|
|
148
|
+
ir.RankedTensorType.get(val.type.shape, cast_tycls.get()), val
|
|
149
|
+
)
|
|
150
|
+
new_vals.append(val)
|
|
151
|
+
return tuple(new_vals)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def minmax(ty: ir.Type) -> tuple[numbers.Number, numbers.Number]:
|
|
155
|
+
if isinstance(ty, ir.IntegerType):
|
|
156
|
+
if ty.is_unsigned:
|
|
157
|
+
return (0, 1 << ty.width)
|
|
158
|
+
else:
|
|
159
|
+
return (-(1 << (ty.width - 1)), (1 << (ty.width - 1)) - 1)
|
|
160
|
+
elif isinstance(ty, ir.F16Type):
|
|
161
|
+
return (np.finfo(np.float16).min, np.finfo(np.float16).max)
|
|
162
|
+
elif isinstance(ty, ir.F32Type):
|
|
163
|
+
return (np.finfo(np.float32).min, np.finfo(np.float32).max)
|
|
164
|
+
elif isinstance(ty, ir.F64Type):
|
|
165
|
+
return (np.finfo(np.float64).min, np.finfo(np.float64).max)
|
|
166
|
+
else:
|
|
167
|
+
raise ValueError("Unsupported type: %s" % ty)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def convert_int_to_float(t: ir.Value) -> ir.Value:
|
|
171
|
+
"""Converts an input with type ir.IntegerType to an ir.FloatType of equivalent width."""
|
|
172
|
+
elty = t.type.element_type
|
|
173
|
+
if not isinstance(elty, ir.IntegerType):
|
|
174
|
+
raise ValueError(
|
|
175
|
+
"Expected input with integer type, received %s" % type(elty)
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
if elty.width == 32:
|
|
179
|
+
return stablehlo.convert(
|
|
180
|
+
ir.RankedTensorType.get(t.type.shape, ir.F32Type.get()), t
|
|
181
|
+
)
|
|
182
|
+
elif elty.width == 64:
|
|
183
|
+
return stablehlo.convert(
|
|
184
|
+
ir.RankedTensorType.get(t.type.shape, ir.F64Type.get()), t
|
|
185
|
+
)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Copyright 2024 The AI Edge Torch Authors.
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
# ==============================================================================
|
|
15
|
+
from jax._src.lib.mlir import ir
|
|
16
|
+
from jax._src.lib.mlir import passmanager
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def run_pass(pipeline, module: ir.Module):
|
|
20
|
+
pm = passmanager.PassManager.parse(pipeline)
|
|
21
|
+
pm.run(module.operation)
|
|
22
|
+
return module
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def canonicalize(module: ir.Module):
|
|
26
|
+
return run_pass("builtin.module(canonicalize)", module)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def cse(module: ir.Module):
|
|
30
|
+
return run_pass("builtin.module(cse)", module)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def inline(module: ir.Module):
|
|
34
|
+
return run_pass("builtin.module(inline)", module)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def strip_debuginfo(module: ir.Module):
|
|
38
|
+
return run_pass("builtin.module(strip-debuginfo)", module)
|