torchax 0.0.6__py3-none-any.whl → 0.0.10.dev20251116__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 torchax might be problematic. Click here for more details.
- torchax/CONTRIBUTING.md +10 -5
- torchax/__init__.py +92 -65
- torchax/amp.py +14 -0
- torchax/checkpoint.py +79 -0
- torchax/config.py +14 -0
- torchax/decompositions.py +14 -0
- torchax/device_module.py +14 -0
- torchax/export.py +14 -0
- torchax/flax.py +14 -0
- torchax/interop.py +44 -31
- torchax/mesh_util.py +14 -0
- torchax/ops/__init__.py +14 -0
- torchax/ops/jaten.py +3985 -3686
- torchax/ops/jax_reimplement.py +14 -0
- torchax/ops/jc10d.py +14 -0
- torchax/ops/jimage.py +14 -0
- torchax/ops/jlibrary.py +14 -0
- torchax/ops/jtorch.py +364 -309
- torchax/ops/jtorchvision_nms.py +14 -0
- torchax/ops/mappings.py +26 -4
- torchax/ops/op_base.py +14 -0
- torchax/ops/ops_registry.py +14 -0
- torchax/tensor.py +38 -13
- torchax/train.py +112 -97
- torchax/types.py +14 -0
- torchax/util.py +14 -0
- torchax/view.py +14 -0
- torchax-0.0.10.dev20251116.dist-info/METADATA +507 -0
- torchax-0.0.10.dev20251116.dist-info/RECORD +31 -0
- torchax-0.0.10.dev20251116.dist-info/licenses/LICENSE +201 -0
- torchax/configuration.py +0 -30
- torchax/environment.py +0 -1
- torchax/tf_integration.py +0 -119
- torchax-0.0.6.dist-info/METADATA +0 -307
- torchax-0.0.6.dist-info/RECORD +0 -33
- torchax-0.0.6.dist-info/licenses/LICENSE +0 -28
- {torchax-0.0.6.dist-info → torchax-0.0.10.dev20251116.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
torchax/configuration.py
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import dataclasses
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
@dataclasses.dataclass
|
|
5
|
-
class Configuration:
|
|
6
|
-
debug_print_each_op: bool = False
|
|
7
|
-
debug_accuracy_for_each_op: bool = False
|
|
8
|
-
debug_mixed_tensor: bool = False
|
|
9
|
-
debug_print_each_op_operands: bool = False
|
|
10
|
-
|
|
11
|
-
use_int32_for_index: bool = False
|
|
12
|
-
|
|
13
|
-
# normally, math between CPU torch.Tensor with torchax.Tensor is not
|
|
14
|
-
# allowed. However, if that torch.Tensor happens to be scalar, then we
|
|
15
|
-
# can use scalar * tensor math to handle it
|
|
16
|
-
allow_mixed_math_with_scalar_tensor: bool = True
|
|
17
|
-
|
|
18
|
-
# If true, we will convert Views into torchax.Tensors eagerly
|
|
19
|
-
force_materialize_views: bool = False
|
|
20
|
-
|
|
21
|
-
# Use DLPack for converting jax.Arrays <-> and torch.Tensor
|
|
22
|
-
use_dlpack_for_data_conversion: bool = False
|
|
23
|
-
|
|
24
|
-
# Flash attention
|
|
25
|
-
use_tpu_flash_attention: bool = False
|
|
26
|
-
shmap_flash_attention: bool = False
|
|
27
|
-
|
|
28
|
-
# device
|
|
29
|
-
treat_cuda_as_jax_device: bool = True
|
|
30
|
-
internal_respect_torch_return_dtypes: bool = False
|
torchax/environment.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
torchax/tf_integration.py
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
# pylint: disable
|
|
2
|
-
import os
|
|
3
|
-
from typing import Any, Tuple
|
|
4
|
-
|
|
5
|
-
from jax.experimental import jax2tf
|
|
6
|
-
import tensorflow as tf
|
|
7
|
-
import torch
|
|
8
|
-
from torchax import export
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
def exported_program_to_tf_function(ep, enable_xla=True):
|
|
12
|
-
weights, jax_program = export.exported_program_to_jax(ep)
|
|
13
|
-
wrapped = lambda *args: jax_program(weights, (args,))
|
|
14
|
-
avals = export.extract_avals(ep)
|
|
15
|
-
input_signature = [
|
|
16
|
-
tf.TensorSpec(shape=t.shape, dtype=t.dtype, name=f"args_{i}")
|
|
17
|
-
for i, t in enumerate(avals)
|
|
18
|
-
]
|
|
19
|
-
tf_f = tf.function(
|
|
20
|
-
jax2tf.convert(
|
|
21
|
-
wrapped,
|
|
22
|
-
with_gradient=False,
|
|
23
|
-
enable_xla=enable_xla,
|
|
24
|
-
),
|
|
25
|
-
autograph=False,
|
|
26
|
-
input_signature=input_signature,
|
|
27
|
-
)
|
|
28
|
-
return tf_f
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def exported_program_to_tf_module(ep: torch.export.ExportedProgram,
|
|
32
|
-
enable_xla=True) -> tf.Module:
|
|
33
|
-
tfm = tf.Module()
|
|
34
|
-
tfm.f = exported_program_to_tf_function(ep, enable_xla)
|
|
35
|
-
return tfm
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
def save_exported_program_as_tf_saved_model(
|
|
39
|
-
ep: torch.export.ExportedProgram,
|
|
40
|
-
saved_model_dir: os.PathLike,
|
|
41
|
-
serving_key: str = tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY,
|
|
42
|
-
function_alias: str = "",
|
|
43
|
-
enable_xla=True,
|
|
44
|
-
):
|
|
45
|
-
"""This function will export and save a pytorch ExportedProgram to tf.saved_model format.
|
|
46
|
-
|
|
47
|
-
The resulting tf.saved_model can be used inference using tf.serving model
|
|
48
|
-
server
|
|
49
|
-
or further convert to tflite flatbuffer for on-device serving.
|
|
50
|
-
|
|
51
|
-
Args:
|
|
52
|
-
torch_model: torch.nn.Module - model to export and save
|
|
53
|
-
args: Tuple[Any] - a set of args to trace the model with, i.e.
|
|
54
|
-
torch_model(*args) must run
|
|
55
|
-
saved_model_dir: os.PathLike - location to an empty directory to store the
|
|
56
|
-
saved_model
|
|
57
|
-
serving_key: str - serving key tag, this is used by tf.serving to know
|
|
58
|
-
which function to run.
|
|
59
|
-
function_alias: str - passed through saved_model.save, used to tag a
|
|
60
|
-
function for inference converter or other tools.
|
|
61
|
-
"""
|
|
62
|
-
tfm = exported_program_to_tf_module(ep, enable_xla=enable_xla)
|
|
63
|
-
signatures = {
|
|
64
|
-
serving_key: tfm.f.get_concrete_function(*tfm.f.input_signature)
|
|
65
|
-
}
|
|
66
|
-
save_options = tf.saved_model.SaveOptions(function_aliases={
|
|
67
|
-
function_alias: tfm.f,
|
|
68
|
-
})
|
|
69
|
-
tf.saved_model.save(
|
|
70
|
-
tfm,
|
|
71
|
-
saved_model_dir,
|
|
72
|
-
signatures=signatures,
|
|
73
|
-
options=save_options,
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
def save_torch_module_as_tf_saved_model(
|
|
78
|
-
torch_model: torch.nn.Module,
|
|
79
|
-
args: Tuple[Any],
|
|
80
|
-
saved_model_dir: os.PathLike,
|
|
81
|
-
serving_key: str = tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY,
|
|
82
|
-
function_alias: str = "",
|
|
83
|
-
enable_xla=True,
|
|
84
|
-
):
|
|
85
|
-
"""This function will export and save a pytorch nn.Module to tf.saved_model format.
|
|
86
|
-
|
|
87
|
-
The resulting tf.saved_model can be used inference using tf.serving model
|
|
88
|
-
server
|
|
89
|
-
or further convert to tflite flatbuffer for on-device serving.
|
|
90
|
-
|
|
91
|
-
Args:
|
|
92
|
-
torch_model: torch.nn.Module - model to export and save
|
|
93
|
-
args: Tuple[Any] - a set of args to trace the model with, i.e.
|
|
94
|
-
torch_model(*args) must run
|
|
95
|
-
saved_model_dir: os.PathLike - location to an empty directory to store the
|
|
96
|
-
saved_model
|
|
97
|
-
serving_key: str - serving key tag, this is used by tf.serving to know
|
|
98
|
-
which function to run.
|
|
99
|
-
function_alias: str - passed through saved_model.save, used to tag a
|
|
100
|
-
function for inference converter or other tools.
|
|
101
|
-
"""
|
|
102
|
-
ep = torch.export.export(torch_model, args)
|
|
103
|
-
save_exported_program_as_tf_saved_model(ep, saved_model_dir, serving_key,
|
|
104
|
-
function_alias, enable_xla)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
def exported_program_to_tflite_flatbuffer(ep: torch.export.ExportedProgram):
|
|
108
|
-
tfm = exported_program_to_tf_module(ep)
|
|
109
|
-
tf_concrete_func = tfm.f.get_concrete_function(*tfm.f.input_signature)
|
|
110
|
-
converter = tf.lite.TFLiteConverter.from_concrete_functions(
|
|
111
|
-
[tf_concrete_func], tfm)
|
|
112
|
-
tflite_model = converter.convert()
|
|
113
|
-
return tflite_model
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
def torch_module_to_tflite_flatbuffer(torch_model: torch.nn.Module,
|
|
117
|
-
args: Tuple[Any]):
|
|
118
|
-
ep = torch.export.export(torch_model, args)
|
|
119
|
-
return exported_program_to_tflite_flatbuffer(ep)
|
torchax-0.0.6.dist-info/METADATA
DELETED
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: torchax
|
|
3
|
-
Version: 0.0.6
|
|
4
|
-
Summary: torchax is a library for running Jax and PyTorch together
|
|
5
|
-
Project-URL: Homepage, https://github.com/pytorch/xla/tree/master/torchax
|
|
6
|
-
Author-email: Han Qi <qihan.dev@gmail.com>, Pytorch/XLA team <pytorchxla-dev@google.com>
|
|
7
|
-
License: BSD 3-Clause License
|
|
8
|
-
|
|
9
|
-
Copyright (c) 2023, pytorch-tpu
|
|
10
|
-
|
|
11
|
-
Redistribution and use in source and binary forms, with or without
|
|
12
|
-
modification, are permitted provided that the following conditions are met:
|
|
13
|
-
|
|
14
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
15
|
-
list of conditions and the following disclaimer.
|
|
16
|
-
|
|
17
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
18
|
-
this list of conditions and the following disclaimer in the documentation
|
|
19
|
-
and/or other materials provided with the distribution.
|
|
20
|
-
|
|
21
|
-
3. Neither the name of the copyright holder nor the names of its
|
|
22
|
-
contributors may be used to endorse or promote products derived from
|
|
23
|
-
this software without specific prior written permission.
|
|
24
|
-
|
|
25
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
26
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
27
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
28
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
29
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
30
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
31
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
32
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
33
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
34
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
35
|
-
License-File: LICENSE
|
|
36
|
-
Classifier: Development Status :: 3 - Alpha
|
|
37
|
-
Classifier: Intended Audience :: Developers
|
|
38
|
-
Classifier: Intended Audience :: Education
|
|
39
|
-
Classifier: Intended Audience :: Science/Research
|
|
40
|
-
Classifier: License :: OSI Approved :: BSD License
|
|
41
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
42
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
43
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
44
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
45
|
-
Classifier: Topic :: Scientific/Engineering
|
|
46
|
-
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
47
|
-
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
48
|
-
Classifier: Topic :: Software Development
|
|
49
|
-
Classifier: Topic :: Software Development :: Libraries
|
|
50
|
-
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
51
|
-
Requires-Python: >=3.10
|
|
52
|
-
Provides-Extra: cpu
|
|
53
|
-
Requires-Dist: jax[cpu]; extra == 'cpu'
|
|
54
|
-
Requires-Dist: jax[cpu]>=0.6.2; extra == 'cpu'
|
|
55
|
-
Provides-Extra: cuda
|
|
56
|
-
Requires-Dist: jax[cpu]>=0.6.2; extra == 'cuda'
|
|
57
|
-
Requires-Dist: jax[cuda12]; extra == 'cuda'
|
|
58
|
-
Provides-Extra: odml
|
|
59
|
-
Requires-Dist: jax[cpu]; extra == 'odml'
|
|
60
|
-
Requires-Dist: jax[cpu]>=0.6.2; extra == 'odml'
|
|
61
|
-
Provides-Extra: tpu
|
|
62
|
-
Requires-Dist: jax[cpu]>=0.6.2; extra == 'tpu'
|
|
63
|
-
Requires-Dist: jax[tpu]; extra == 'tpu'
|
|
64
|
-
Description-Content-Type: text/markdown
|
|
65
|
-
|
|
66
|
-
# torchax: Running PyTorch on TPU via JAX
|
|
67
|
-
|
|
68
|
-
**torchax** is a backend for PyTorch, allowing users to run
|
|
69
|
-
PyTorch on Google Cloud TPUs. **torchax** is also a library for providing
|
|
70
|
-
graph-level interoperability between PyTorch and JAX.
|
|
71
|
-
|
|
72
|
-
This means, with **torchax** you can:
|
|
73
|
-
* Run PyTorch code on TPUs with as little as 2 lines of code change.
|
|
74
|
-
* Call a JAX function from a PyTorch function, passing in `jax.Array`s.
|
|
75
|
-
* Call a PyTorch function from a JAX function, passing in a `torch.Tensor`s.
|
|
76
|
-
* Use JAX features such as `jax.grad`, `optax`, and `GSPMD` to train a PyTorch
|
|
77
|
-
model.
|
|
78
|
-
* Use a PyTorch model as feature extractor and use it with a JAX model.
|
|
79
|
-
etc etc.
|
|
80
|
-
|
|
81
|
-
## Install
|
|
82
|
-
|
|
83
|
-
First install torch CPU:
|
|
84
|
-
|
|
85
|
-
```bash
|
|
86
|
-
# On Linux.
|
|
87
|
-
pip install torch --index-url https://download.pytorch.org/whl/cpu
|
|
88
|
-
|
|
89
|
-
# Or on Mac.
|
|
90
|
-
pip install torch
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
Then install JAX for the accelerator you want to use:
|
|
94
|
-
|
|
95
|
-
```bash
|
|
96
|
-
# On Google Cloud TPU.
|
|
97
|
-
pip install -U jax[tpu]
|
|
98
|
-
|
|
99
|
-
# Or, on GPU machines.
|
|
100
|
-
pip install -U jax[cuda12]
|
|
101
|
-
|
|
102
|
-
# Or, on Linux CPU machines or Macs (see the note below).
|
|
103
|
-
pip install -U jax
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
NOTE: if you like metal support for Apple devices then install the
|
|
107
|
-
metal version of JAX: https://developer.apple.com/metal/jax/
|
|
108
|
-
|
|
109
|
-
Finally install torchax:
|
|
110
|
-
|
|
111
|
-
```bash
|
|
112
|
-
# Install pre-built torchax.
|
|
113
|
-
pip install torchax
|
|
114
|
-
|
|
115
|
-
# Or, install torchax from source.
|
|
116
|
-
pip install git+https://github.com/pytorch/xla.git#subdirectory=torchax
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
## Run a model
|
|
120
|
-
|
|
121
|
-
Now let's execute a model under torchax. We'll start with a simple 2-layer model.
|
|
122
|
-
In theory, we can use any instance of `torch.nn.Module`.
|
|
123
|
-
|
|
124
|
-
```python
|
|
125
|
-
import torch
|
|
126
|
-
import torch.nn as nn
|
|
127
|
-
import torch.nn.functional as F
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
class MyModel(nn.Module):
|
|
131
|
-
def __init__(self):
|
|
132
|
-
super().__init__()
|
|
133
|
-
self.fc1 = nn.Linear(28 * 28, 120)
|
|
134
|
-
self.fc2 = nn.Linear(120, 84)
|
|
135
|
-
self.fc3 = nn.Linear(84, 10)
|
|
136
|
-
|
|
137
|
-
def forward(self, x):
|
|
138
|
-
x = x.view(-1, 28 * 28)
|
|
139
|
-
x = F.relu(self.fc1(x))
|
|
140
|
-
x = F.relu(self.fc2(x))
|
|
141
|
-
x = self.fc3(x)
|
|
142
|
-
return x
|
|
143
|
-
|
|
144
|
-
m = MyModel()
|
|
145
|
-
|
|
146
|
-
# Execute this model using torch.
|
|
147
|
-
inputs = torch.randn(3, 3, 28, 28)
|
|
148
|
-
print(m(inputs))
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
To execute this model with `torchax`, we need to enable torchax to capture PyTorch ops:
|
|
152
|
-
|
|
153
|
-
```python
|
|
154
|
-
import torchax
|
|
155
|
-
torchax.enable_globally()
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
Then, we can use a `jax` device:
|
|
159
|
-
|
|
160
|
-
```python
|
|
161
|
-
inputs = torch.randn(3, 3, 28, 28, device='jax')
|
|
162
|
-
m = MyModel().to('jax')
|
|
163
|
-
res = m(inputs)
|
|
164
|
-
print(type(res)) # outputs torchax.tensor.Tensor
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
`torchax.tensor.Tensor` is a `torch.Tensor` subclass that holds
|
|
168
|
-
a `jax.Array`. You can inspect that JAX array with `res.jax()`.
|
|
169
|
-
|
|
170
|
-
## What is happening behind the scene
|
|
171
|
-
|
|
172
|
-
We took the approach detailed in the
|
|
173
|
-
[new device](https://github.com/albanD/subclass_zoo/blob/main/new_device.py)
|
|
174
|
-
recipe by Alban (@albanD), using `jax.Array` for `raw_data`.
|
|
175
|
-
|
|
176
|
-
In other words, when a torch op is executed inside an `env` context manager,
|
|
177
|
-
which is enabled by `torchax.enable_globally()`, we will swap out the
|
|
178
|
-
implementation of that op with JAX.
|
|
179
|
-
|
|
180
|
-
When a model's constructor runs, it will call some tensor constructor, such as
|
|
181
|
-
`torch.rand`, `torch.ones`, or `torch.zeros` to create its weights. When torchax
|
|
182
|
-
is enabled, these constructors will create a `torchax.tensor.Tensor`, which
|
|
183
|
-
contains a `jax.Array`.
|
|
184
|
-
|
|
185
|
-
Then, each subsequent op will extract the `jax.Array`, call the op's JAX
|
|
186
|
-
implementation, and wrap the result back into a `torchax.tensor.Tensor`,
|
|
187
|
-
|
|
188
|
-
See more at [how it works](docs/how_it_works.md) and\
|
|
189
|
-
[ops registry](docs/ops_registry.md).
|
|
190
|
-
|
|
191
|
-
### Executing with jax.jit
|
|
192
|
-
|
|
193
|
-
The above script will execute the model using eager mode JAX as the backend. This
|
|
194
|
-
does allow executing torch models on TPUs, but is often slower than what we can
|
|
195
|
-
achieve with `jax.jit`.
|
|
196
|
-
|
|
197
|
-
`jax.jit` is a function that takes a JAX function (i.e. a function that takes JAX arrays
|
|
198
|
-
and returns JAX arrays) into a compiled (thus faster) version of the same function.
|
|
199
|
-
|
|
200
|
-
We have made a `jax_jit` decorator that would accomplish the same with functions
|
|
201
|
-
that takes and returns `torch.Tensor`s. To use this, the first step is to create
|
|
202
|
-
a functional version of this model: this means the parameters should be passed in
|
|
203
|
-
as input instead of being attributes of the class:
|
|
204
|
-
|
|
205
|
-
```python
|
|
206
|
-
def model_func(param, inputs):
|
|
207
|
-
return torch.func.functional_call(m, param, inputs)
|
|
208
|
-
```
|
|
209
|
-
|
|
210
|
-
Here we use [torch.func.functional_call](https://pytorch.org/docs/stable/generated/torch.func.functional_call.html)
|
|
211
|
-
from PyTorch to replace the model weights with `param` and then call the
|
|
212
|
-
model. This is roughly equivalent to:
|
|
213
|
-
|
|
214
|
-
```python
|
|
215
|
-
def model_func(param, inputs):
|
|
216
|
-
m.load_state_dict(param)
|
|
217
|
-
return m(*inputs)
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
Now, we can apply `jax_jit` on `module_func`:
|
|
221
|
-
|
|
222
|
-
```python
|
|
223
|
-
from torchax.interop import jax_jit
|
|
224
|
-
|
|
225
|
-
model_func_jitted = jax_jit(model_func)
|
|
226
|
-
print(model_func_jitted(new_state_dict, inputs))
|
|
227
|
-
```
|
|
228
|
-
|
|
229
|
-
See more examples at [eager_mode.py](examples/eager_mode.py) and the
|
|
230
|
-
[examples folder](examples/).
|
|
231
|
-
|
|
232
|
-
To ease the idiom of creating functional model and calling it with parameters,
|
|
233
|
-
we also created the `JittableModule` helper class. It lets us rewrite the
|
|
234
|
-
above as:
|
|
235
|
-
|
|
236
|
-
```python
|
|
237
|
-
from torchax.interop import JittableModule
|
|
238
|
-
|
|
239
|
-
m_jitted = JittableModule(m)
|
|
240
|
-
res = m_jitted(...)
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
The first time `m_jitted` is called, it will trigger `jax.jit` to compile the
|
|
244
|
-
compile for the given input shapes. Subsequent calls with the same input shapes
|
|
245
|
-
will be fast as the compilation is cached.
|
|
246
|
-
|
|
247
|
-
## Citation
|
|
248
|
-
|
|
249
|
-
```
|
|
250
|
-
@software{torchax,
|
|
251
|
-
author = {Han Qi, Chun-nien Chan, Will Cromar, Manfei Bai, Kevin Gleanson},
|
|
252
|
-
title = {torchax: PyTorch on TPU and JAX interoperability},
|
|
253
|
-
url = {https://github.com/pytorch/xla/tree/master/torchax}
|
|
254
|
-
version = {0.0.4},
|
|
255
|
-
date = {2025-02-24},
|
|
256
|
-
}
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
# Maintainers & Contributors:
|
|
260
|
-
|
|
261
|
-
This library is created and maintained by the PyTorch/XLA team at Google Cloud.
|
|
262
|
-
|
|
263
|
-
It benefitted from many direct and indirect
|
|
264
|
-
contributions outside of the team. Many of them done by
|
|
265
|
-
fellow Googlers using [Google's 20% project policy](https://ebsedu.org/blog/google-tapping-workplace-actualization-20-time-rule).
|
|
266
|
-
Others by partner teams at Google and other companies.
|
|
267
|
-
|
|
268
|
-
Here is the list of contributors by 2025-02-25.
|
|
269
|
-
|
|
270
|
-
```
|
|
271
|
-
Han Qi (qihqi), PyTorch/XLA
|
|
272
|
-
Manfei Bai (manfeibai), PyTorch/XLA
|
|
273
|
-
Will Cromar (will-cromar), Meta
|
|
274
|
-
Milad Mohammadi (miladm), PyTorch/XLA
|
|
275
|
-
Siyuan Liu (lsy323), PyTorch/XLA
|
|
276
|
-
Bhavya Bahl (bhavya01), PyTorch/XLA
|
|
277
|
-
Pei Zhang (zpcore), PyTorch/XLA
|
|
278
|
-
Yifei Teng (tengyifei), PyTorch/XLA
|
|
279
|
-
Chunnien Chan (chunnienc), Google, ODML
|
|
280
|
-
Alban Desmaison (albanD), Meta, PyTorch
|
|
281
|
-
Simon Teo (simonteozw), Google (20%)
|
|
282
|
-
David Huang (dvhg), Google (20%)
|
|
283
|
-
Barni Seetharaman (barney-s), Google (20%)
|
|
284
|
-
Anish Karthik (anishfish2), Google (20%)
|
|
285
|
-
Yao Gu (guyao), Google (20%)
|
|
286
|
-
Yenkai Wang (yenkwang), Google (20%)
|
|
287
|
-
Greg Shikhman (commander), Google (20%)
|
|
288
|
-
Matin Akhlaghinia (matinehAkhlaghinia), Google (20%)
|
|
289
|
-
Tracy Chen (tracych477), Google (20%)
|
|
290
|
-
Matthias Guenther (mrguenther), Google (20%)
|
|
291
|
-
WenXin Dong (wenxindongwork), Google (20%)
|
|
292
|
-
Kevin Gleason (GleasonK), Google, StableHLO
|
|
293
|
-
Nupur Baghel (nupurbaghel), Google (20%)
|
|
294
|
-
Gwen Mittertreiner (gmittert), Google (20%)
|
|
295
|
-
Zeev Melumian (zmelumian), Lightricks
|
|
296
|
-
Vyom Sharma (vyom1611), Google (20%)
|
|
297
|
-
Shitong Wang (ShitongWang), Adobe
|
|
298
|
-
Rémi Doreau (ayshiff), Google (20%)
|
|
299
|
-
Lance Wang (wang2yn84), Google, CoreML
|
|
300
|
-
Hossein Sarshar (hosseinsarshar), Google (20%)
|
|
301
|
-
Daniel Vega-Myhre (danielvegamyhre), Google (20%)
|
|
302
|
-
Tianqi Fan (tqfan28), Google (20%)
|
|
303
|
-
Jim Lin (jimlinntu), Google (20%)
|
|
304
|
-
Fanhai Lu (FanhaiLu1), Google Cloud
|
|
305
|
-
DeWitt Clinton (dewitt), Google PyTorch
|
|
306
|
-
Aman Gupta (aman2930), Google (20%)
|
|
307
|
-
```
|