jax-tpu-embedding 0.1.0__tar.gz

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.
Files changed (58) hide show
  1. jax_tpu_embedding-0.1.0/LICENSE +202 -0
  2. jax_tpu_embedding-0.1.0/PKG-INFO +71 -0
  3. jax_tpu_embedding-0.1.0/README.md +44 -0
  4. jax_tpu_embedding-0.1.0/jax_tpu_embedding/__init__.py +0 -0
  5. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/__init__.py +18 -0
  6. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/__init__.py +14 -0
  7. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/auto_pipelining/__init__.py +0 -0
  8. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/auto_pipelining/auto_pipelining.py +273 -0
  9. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/auto_pipelining/decompose.py +323 -0
  10. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/auto_pipelining/preprocess.py +143 -0
  11. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/auto_pipelining/utils.py +132 -0
  12. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/__init__.py +14 -0
  13. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/constants.py +17 -0
  14. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/input_preprocessing.py +345 -0
  15. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/libinput_preprocessing_core.so +0 -0
  16. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/libinput_preprocessing_threads.so +0 -0
  17. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/libinput_preprocessing_util.so +0 -0
  18. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/__init__.py +14 -0
  19. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/optimizers_computation.py +148 -0
  20. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_activation_unstack.py +103 -0
  21. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_csr.py +198 -0
  22. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_grad_with_adagrad.py +282 -0
  23. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_grad_with_adagrad_momentum.py +376 -0
  24. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_grad_with_adam.py +420 -0
  25. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_grad_with_f2a.py +374 -0
  26. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_grad_with_ftrl.py +405 -0
  27. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_grad_with_laprop.py +420 -0
  28. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_grad_with_sgd.py +251 -0
  29. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_gradient_stack.py +91 -0
  30. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/sparse_dense_matmul_optimizer_grad.py +288 -0
  31. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/primitives/utils.py +149 -0
  32. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/core/pybind_input_preprocessing.so +0 -0
  33. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/fdo/__init__.py +14 -0
  34. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/fdo/csv_file_fdo_client.py +213 -0
  35. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/fdo/fdo_client.py +83 -0
  36. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/fdo/file_fdo_client.py +205 -0
  37. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/flax/__init__.py +14 -0
  38. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/flax/linen/__init__.py +14 -0
  39. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/flax/linen/embed.py +276 -0
  40. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/flax/linen/embed_optimizer.py +97 -0
  41. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/flax/nnx/__init__.py +14 -0
  42. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/flax/nnx/embed.py +299 -0
  43. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/nn/__init__.py +14 -0
  44. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/nn/embedding.py +1810 -0
  45. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/nn/embedding_spec.py +1012 -0
  46. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/nn/table_stacking.py +1287 -0
  47. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/proto/__init__.py +14 -0
  48. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/lib/proto/embedding_spec_pb2.py +43 -0
  49. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/utils/__init__.py +14 -0
  50. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/utils/utils.py +132 -0
  51. jax_tpu_embedding-0.1.0/jax_tpu_embedding/sparsecore/version.py +18 -0
  52. jax_tpu_embedding-0.1.0/jax_tpu_embedding.egg-info/PKG-INFO +71 -0
  53. jax_tpu_embedding-0.1.0/jax_tpu_embedding.egg-info/SOURCES.txt +56 -0
  54. jax_tpu_embedding-0.1.0/jax_tpu_embedding.egg-info/dependency_links.txt +1 -0
  55. jax_tpu_embedding-0.1.0/jax_tpu_embedding.egg-info/requires.txt +6 -0
  56. jax_tpu_embedding-0.1.0/jax_tpu_embedding.egg-info/top_level.txt +1 -0
  57. jax_tpu_embedding-0.1.0/pyproject.toml +46 -0
  58. jax_tpu_embedding-0.1.0/setup.cfg +4 -0
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,71 @@
1
+ Metadata-Version: 2.4
2
+ Name: jax_tpu_embedding
3
+ Version: 0.1.0
4
+ Summary: JAX SparseCore API
5
+ Author-email: JAX SC Authors <jax-sc-dev@google.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: homepage, https://github.com/jax-ml/jax-tpu-embedding
8
+ Project-URL: repository, https://github.com/jax-ml/jax-tpu-embedding
9
+ Project-URL: issue_tracker, https://github.com/jax-ml/jax-tpu-embedding/issues
10
+ Keywords: jax,tpu,embedding,sparsecore,machine learning,sparse tensor,distributed computing
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Programming Language :: Python :: 3.14
17
+ Requires-Python: >=3.10
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: absl-py
21
+ Requires-Dist: einops
22
+ Requires-Dist: flax
23
+ Requires-Dist: jax
24
+ Requires-Dist: numpy
25
+ Requires-Dist: portpicker
26
+ Dynamic: license-file
27
+
28
+ # JAX TPU Embedding (JAX SparseCore)
29
+
30
+ [![Build and test](https://github.com/jax-ml/jax-tpu-embedding/actions/workflows/build_and_test.yml/badge.svg)](https://github.com/jax-ml/jax-tpu-embedding/actions/workflows/build_and_test.yml)
31
+ [![PyPI version](https://img.shields.io/pypi/v/jax-tpu-embedding)](https://pypi.org/project/jax-tpu-embedding/)
32
+ [![Documentation Status](https://readthedocs.org/projects/jax-tpu-embedding/badge/?version=latest)](https://jax-tpu-embedding.readthedocs.io/en/latest/?badge=latest)
33
+
34
+ [**Quick Overview**](https://jax-tpu-embedding.readthedocs.io/en/latest/overview.html)
35
+ | [**Install guide**](#installation)
36
+ | [**Reference docs**](https://jax-tpu-embedding.readthedocs.io/en/latest/)
37
+
38
+ ## What is JAX TPU Embedding?
39
+
40
+ JAX SparseCore provides support for leveraging the SparseCore accelerators
41
+ present in TPU generations starting with TPU v5.
42
+
43
+ SparseCores are specialized tiled processors engineered for high-performance
44
+ acceleration of workloads that involve irregular, sparse memory access and
45
+ computation. While they excel at tasks like embedding lookups (common in deep
46
+ learning recommendation models), their capabilities extend to accelerating a
47
+ variety of other dynamic and sparse workloads on large datasets stored in High
48
+ Bandwidth Memory (HBM).
49
+
50
+ This is a research project, not an official Google product. Expect sharp edges.
51
+ Please help by trying it out, reporting bugs, and letting us know what you
52
+ think!
53
+
54
+ ## Installation
55
+
56
+ You can install JAX TPU Embedding from PyPI:
57
+
58
+ ```bash
59
+ pip install jax-tpu-embedding
60
+ ```
61
+
62
+ *Note: To use TPU acceleration, you must run in an environment with access to TPU v5+ hardware and have the appropriate `jax` and `jaxlib` TPU releases installed (see the [JAX installation guide](https://github.com/google/jax#installation)).*
63
+
64
+ ## Documentation
65
+
66
+ For detailed guides, specifications, and tutorials, see the [JAX TPU Embedding Documentation](https://jax-tpu-embedding.readthedocs.io/en/latest/).
67
+
68
+ * [Quick Overview for Users](https://jax-tpu-embedding.readthedocs.io/en/latest/overview.html)
69
+ * [Embedding Specification](https://jax-tpu-embedding.readthedocs.io/en/latest/embedding.html)
70
+ * [Input Processing](https://jax-tpu-embedding.readthedocs.io/en/latest/input_processing.html)
71
+ * [Tutorials (Flax & Primitives)](https://jax-tpu-embedding.readthedocs.io/en/latest/#tutorials)
@@ -0,0 +1,44 @@
1
+ # JAX TPU Embedding (JAX SparseCore)
2
+
3
+ [![Build and test](https://github.com/jax-ml/jax-tpu-embedding/actions/workflows/build_and_test.yml/badge.svg)](https://github.com/jax-ml/jax-tpu-embedding/actions/workflows/build_and_test.yml)
4
+ [![PyPI version](https://img.shields.io/pypi/v/jax-tpu-embedding)](https://pypi.org/project/jax-tpu-embedding/)
5
+ [![Documentation Status](https://readthedocs.org/projects/jax-tpu-embedding/badge/?version=latest)](https://jax-tpu-embedding.readthedocs.io/en/latest/?badge=latest)
6
+
7
+ [**Quick Overview**](https://jax-tpu-embedding.readthedocs.io/en/latest/overview.html)
8
+ | [**Install guide**](#installation)
9
+ | [**Reference docs**](https://jax-tpu-embedding.readthedocs.io/en/latest/)
10
+
11
+ ## What is JAX TPU Embedding?
12
+
13
+ JAX SparseCore provides support for leveraging the SparseCore accelerators
14
+ present in TPU generations starting with TPU v5.
15
+
16
+ SparseCores are specialized tiled processors engineered for high-performance
17
+ acceleration of workloads that involve irregular, sparse memory access and
18
+ computation. While they excel at tasks like embedding lookups (common in deep
19
+ learning recommendation models), their capabilities extend to accelerating a
20
+ variety of other dynamic and sparse workloads on large datasets stored in High
21
+ Bandwidth Memory (HBM).
22
+
23
+ This is a research project, not an official Google product. Expect sharp edges.
24
+ Please help by trying it out, reporting bugs, and letting us know what you
25
+ think!
26
+
27
+ ## Installation
28
+
29
+ You can install JAX TPU Embedding from PyPI:
30
+
31
+ ```bash
32
+ pip install jax-tpu-embedding
33
+ ```
34
+
35
+ *Note: To use TPU acceleration, you must run in an environment with access to TPU v5+ hardware and have the appropriate `jax` and `jaxlib` TPU releases installed (see the [JAX installation guide](https://github.com/google/jax#installation)).*
36
+
37
+ ## Documentation
38
+
39
+ For detailed guides, specifications, and tutorials, see the [JAX TPU Embedding Documentation](https://jax-tpu-embedding.readthedocs.io/en/latest/).
40
+
41
+ * [Quick Overview for Users](https://jax-tpu-embedding.readthedocs.io/en/latest/overview.html)
42
+ * [Embedding Specification](https://jax-tpu-embedding.readthedocs.io/en/latest/embedding.html)
43
+ * [Input Processing](https://jax-tpu-embedding.readthedocs.io/en/latest/input_processing.html)
44
+ * [Tutorials (Flax & Primitives)](https://jax-tpu-embedding.readthedocs.io/en/latest/#tutorials)
File without changes
@@ -0,0 +1,18 @@
1
+ # Copyright 2024 The JAX SC 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
+ """JAX SparseCore library."""
15
+
16
+ from jax_tpu_embedding.sparsecore import version
17
+
18
+ __version__ = version.__version__
@@ -0,0 +1,14 @@
1
+ # Copyright 2024 The JAX SC 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
+ # Empty file needed by setuptools.find_packages to recognize this as a package.
@@ -0,0 +1,273 @@
1
+ # Copyright 2024 The JAX SC 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
+ """Autopipelining decorator.
15
+
16
+ This decorator is used to automatically transform a train step function to use
17
+ SparseCore pipelining.
18
+
19
+ Example:
20
+
21
+ ```python
22
+ @auto_pipelining.auto_pipelining
23
+ def train_step(carry: Carry, ...) -> tuple[Carry, ...]:
24
+ ...
25
+ return new_carry, ...
26
+ ```
27
+
28
+ With the decorator, the `train_step` function will be transformed to take
29
+ pipelining state as the first argument, before the carry. It also returns a new
30
+ pipelining state as the first return value.
31
+
32
+ ```python
33
+ def train_step(state: EmbeddingPipeliningState, carry: Carry, ...) ->
34
+ tuple[EmbeddingPipeliningState, Carry, ...]:
35
+ ...
36
+ return new_pipelining_state, new_carry, ...
37
+ ```
38
+
39
+ **Notice** that we expect that the train step function returns multiple values,
40
+ with carry as the first value. We don't support the train_step that only returns
41
+ the carry. This is because the carry may be defined as a NamedTuple. We can not
42
+ diferentiate a single carry result, vs multiple results.
43
+
44
+
45
+ # Carry
46
+
47
+ The training function should take carry as the first argument, and return a new
48
+ carry as the first return value.
49
+
50
+ The carry should include all the training states. Specifically, embedding tables
51
+ should be part of carry.
52
+
53
+ Usually, the carry should be donated to the train step function in each step.
54
+ And the function should return a new updated carry with exactly the same
55
+ structure. There's usually only one carry instance at one time.
56
+
57
+ On the other hand, args should be the "data" fed into each train step. It
58
+ should be ok for the pipelining system to keep them for up to three steps.
59
+
60
+ A typical training loop looks like this:
61
+
62
+ ```
63
+ carry = init_carry()
64
+ for (data1, data2) in dataset:
65
+ carry, loss, metrics = train_step(carry, data1, data2)
66
+ ```
67
+
68
+ With pipelining, the training loop should be modified to pass pipelining state:
69
+
70
+ ```
71
+ carry = init_carry()
72
+ state = auto_pipelining.init_state()
73
+ for (data1, data2) in dataset:
74
+ state, (carry, loss, metrics) = train_step(state, carry, data1, data2)
75
+ ```
76
+
77
+ This carry assumption may be avoidable, but it greatly simplifies the
78
+ implementation and use of auto-pipelining system:
79
+
80
+ 1. Training data and training state are naturally separated. We know what part
81
+ of the input should be reused across steps, and what part is step specific.
82
+
83
+ 2. For all training state arrays, we can easily map them from inputs to the
84
+ outputs. This helps create a "no-op" training step. This is useful for the first
85
+ and last few steps, where we only run part of the SparseCore forward, dense,
86
+ or the SparseCore backward.
87
+
88
+
89
+ # Limitations on embedding lookups and updates
90
+
91
+ 1. There should be only one embedding lookup and embedding update for each
92
+ embedding table.
93
+
94
+ 2. The embedding lookups and updates should not be wrapped in any ops, e.g.
95
+ `jax.lax.cond`, or nested `jax.jit`.
96
+
97
+ 3. Each embedding lookup and update shard_map should not contain other
98
+ operations.
99
+
100
+ 4. Embedding table should be the last argument to lookup and update shard_map.
101
+
102
+ The easiest way to comply to these limitations is to use our Flax API.
103
+ """
104
+
105
+ from __future__ import annotations
106
+
107
+ import dataclasses
108
+ import functools
109
+ import logging
110
+ import os
111
+ from typing import Any, Callable, Concatenate, NamedTuple, ParamSpec, TypeVar
112
+
113
+ import jax
114
+ import jax.extend as jex
115
+ from jax_tpu_embedding.sparsecore.lib.auto_pipelining import decompose
116
+
117
+
118
+ Carry = decompose.Carry
119
+
120
+
121
+ def dump_jaxpr(jaxpr: jex.core.ClosedJaxpr | jex.core.Jaxpr, name: str) -> None:
122
+ """Dumps a Jaxpr to a file, if the TEST_UNDECLARED_OUTPUTS_DIR is set."""
123
+ dirname = os.environ.get('TEST_UNDECLARED_OUTPUTS_DIR')
124
+ if not dirname:
125
+ return
126
+ dump_path = os.path.join(dirname, f'jaxpr_{name}.txt')
127
+
128
+ logging.info('Writing %s Jaxpr to %s', name, dump_path)
129
+
130
+ with open(dump_path, 'w') as f:
131
+ f.write(jaxpr.pretty_print(source_info=True))
132
+
133
+
134
+ class EmbeddingPipeliningState(NamedTuple):
135
+ activations_1: list[jax.Array] | None = None
136
+ args_1: Any = None
137
+ updates_0: list[jax.Array] | None = None
138
+
139
+
140
+ @dataclasses.dataclass
141
+ class PipeliningFunction:
142
+ train_step_func: Callable[..., Any]
143
+ finalize: Callable[..., Any]
144
+
145
+ def __call__(self, state: EmbeddingPipeliningState, *args_2):
146
+ return self.train_step_func(state, *args_2)
147
+
148
+
149
+ def init_state() -> EmbeddingPipeliningState:
150
+ """Initializes the EmbeddingPipeliningState."""
151
+ return EmbeddingPipeliningState()
152
+
153
+
154
+ def _run(
155
+ runner: decompose.FunctionRunner,
156
+ state: EmbeddingPipeliningState,
157
+ carry: Carry,
158
+ args_2,
159
+ ):
160
+ """Runs one step of the pipelined training loop."""
161
+ if args_2 is not None:
162
+ if state.updates_0 is not None:
163
+ # Run combined update and lookup (SparseCore backward then forward).
164
+ activations_2, carry = runner.embedding_update_lookup(
165
+ state.updates_0, carry, *args_2
166
+ )
167
+ else:
168
+ # Run lookup only (SparseCore forward).
169
+ activations_2 = runner.embedding_lookup(carry, *args_2)
170
+ else:
171
+ activations_2 = None
172
+ if state.updates_0 is not None:
173
+ # Run update only (SparseCore backward).
174
+ carry = runner.embedding_update(state.updates_0, carry)
175
+
176
+ if state.args_1 is not None and state.activations_1 is not None:
177
+ # Run dense layer.
178
+ updates_1, carry, *func_results = runner.dense(
179
+ state.activations_1, carry, *state.args_1
180
+ )
181
+ else:
182
+ # No dense computation needed, create empty results.
183
+ updates_1 = None
184
+ _, *func_results = runner.none_result()
185
+
186
+ # Prepare data for the next step.
187
+ next_state = EmbeddingPipeliningState(
188
+ activations_1=activations_2,
189
+ # During finalization, args_2 is None. Keep the last args_1 for tracing
190
+ # This can be removed if we move to one program setting.
191
+ args_1=args_2 or state.args_1,
192
+ updates_0=updates_1,
193
+ )
194
+
195
+ return next_state, (
196
+ carry,
197
+ *func_results,
198
+ )
199
+
200
+
201
+ def finalize_pipelining(
202
+ fn: Callable[..., Any], state: EmbeddingPipeliningState, carry: Carry
203
+ ):
204
+ """Finalizes the pipelining run."""
205
+ assert isinstance(
206
+ fn, PipeliningFunction
207
+ ), 'The function is not auto-pipelining wrapped.'
208
+ state, (carry, *res) = fn.finalize(state, carry)
209
+ state, (carry, *_) = fn.finalize(state, carry)
210
+ return state, (carry, *res)
211
+
212
+
213
+ FunctionArgs = ParamSpec('FunctionArgs')
214
+ FunctionResults = TypeVar('FunctionResults')
215
+
216
+
217
+ def auto_pipelining(
218
+ fn: Callable[FunctionArgs, FunctionResults],
219
+ out_shardings: tuple[Any, ...],
220
+ static_argnums: tuple[int, ...] = (),
221
+ ) -> Callable[
222
+ Concatenate[EmbeddingPipeliningState, FunctionArgs],
223
+ tuple[EmbeddingPipeliningState, FunctionResults],
224
+ ]:
225
+ """A decorator to transform a function for SparseCore pipelining."""
226
+
227
+ def _build_runner(carry: Carry, args_2):
228
+ """Compile the function if it's not compiled yet."""
229
+ closed_jaxpr, res_shape = jax.make_jaxpr(
230
+ fn, static_argnums=static_argnums, return_shape=True
231
+ )(carry, *args_2)
232
+ dump_jaxpr(closed_jaxpr, 'original')
233
+ assert isinstance(res_shape, tuple), 'train_step should return a tuple.'
234
+ assert jax.tree.structure(carry) == jax.tree.structure(res_shape[0]), (
235
+ 'The structure of the first argument (carry) should be the same as'
236
+ ' the first return value.'
237
+ )
238
+
239
+ res_structure = jax.tree.structure(res_shape)
240
+ carry_structure = jax.tree.structure(carry)
241
+ runner = decompose.decompose(closed_jaxpr, res_structure, carry_structure)
242
+
243
+ dump_jaxpr(runner.lookup_jaxpr, 'lookup')
244
+ dump_jaxpr(runner.dense_jaxpr, 'dense')
245
+ dump_jaxpr(runner.update_jaxpr, 'update')
246
+ dump_jaxpr(runner.update_lookup_jaxpr, 'update_lookup')
247
+
248
+ return runner
249
+
250
+ @functools.wraps(fn)
251
+ def train_pipeline(state: EmbeddingPipeliningState, carry: Carry, *args_2):
252
+ runner = _build_runner(carry, args_2)
253
+ return _run(runner, state, carry, args_2)
254
+
255
+ def finalize_pipeline(state: EmbeddingPipeliningState, carry: Carry):
256
+ assert state.args_1, (
257
+ 'Args_1 should not be None. This is likely caused by a bug in the'
258
+ ' auto-pipelining system.'
259
+ )
260
+
261
+ runner = _build_runner(carry, state.args_1)
262
+ return _run(runner, state, carry, None)
263
+
264
+ train_pipeline = jax.jit(
265
+ train_pipeline,
266
+ out_shardings=out_shardings,
267
+ donate_argnames=('carry',),
268
+ )
269
+ finalize_pipeline = jax.jit(
270
+ finalize_pipeline, out_shardings=out_shardings, donate_argnames=('carry',)
271
+ )
272
+
273
+ return PipeliningFunction(train_pipeline, finalize_pipeline)