baretile 0.0.1__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.
baretile-0.0.1/LICENSE ADDED
@@ -0,0 +1,176 @@
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
@@ -0,0 +1,133 @@
1
+ Metadata-Version: 2.5
2
+ Name: baretile
3
+ Version: 0.0.1
4
+ Summary: A compiler and runtime for Tenstorrent Blackhole, written in Python
5
+ Project-URL: Source, https://github.com/baretile/baretile
6
+ Author: alkinun
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ Classifier: Operating System :: POSIX :: Linux
10
+ Requires-Python: >=3.12
11
+ Description-Content-Type: text/markdown
12
+
13
+ # baretile
14
+
15
+ baretile is a compiler and runtime for Tenstorrent Blackhole, written in Python.
16
+ You write tensor operations, and it compiles them into Tensix instructions. It
17
+ also handles the PCIe access needed to load and run those programs on the chip.
18
+
19
+ It runs on Tenstorrent's ttsim simulator. We haven't tested it on a card yet.
20
+
21
+ The code in `baretile/` uses only the Python standard library. The driver runs
22
+ in userspace, and baretile loads its own firmware onto the workers. It needs
23
+ Tenstorrent's board firmware for ARC and Ethernet. There's no dependency on
24
+ tt-metal, tt-umd or LLK.
25
+
26
+ ## Try it
27
+
28
+ You'll need Python 3, clang with a RISC-V target, lld, and a Blackhole build of
29
+ [ttsim](https://github.com/tenstorrent/ttsim). In a ttsim checkout, build the library:
30
+
31
+ ```sh
32
+ ./make.py src/_out/release_bh/libttsim.so
33
+ ```
34
+
35
+ Then, from the baretile checkout:
36
+
37
+ ```sh
38
+ export TTSIM=/absolute/path/to/ttsim/src/_out/release_bh/libttsim.so
39
+ python3 -m baretile.chip
40
+ python3 -m baretile.hello
41
+ ```
42
+
43
+ With 120 Tensix cores, `baretile.hello` prints:
44
+
45
+ ```text
46
+ 119 Tensix cores computed fib(30) = {832040}, and one more core dispatched the work
47
+ ```
48
+
49
+ Keep `TTSIM` set for the examples below. Without it, baretile tries to open a
50
+ Blackhole on the PCI bus. That needs root and takes the card away from its kernel
51
+ driver. See [card bring-up](NOTES.md#bring-up-on-a-p150) before running on a card.
52
+
53
+ ## Tensors
54
+
55
+ Run this in Python from the checkout:
56
+
57
+ ```python
58
+ from baretile import pci
59
+ from baretile.chip import Chip
60
+ from baretile.runtime import Runtime
61
+ from baretile.tensor import Tensor
62
+
63
+ rt = Runtime(Chip(pci.open_device()))
64
+ x = Tensor.of(rt, [[1.0, 2.0], [3.0, 4.0]])
65
+ w = Tensor.of(rt, [[2.0, 0.0], [0.0, 3.0]])
66
+ print((x @ w).relu().tolist())
67
+ # [[2.0, 6.0], [6.0, 12.0]]
68
+ ```
69
+
70
+ The matmul and ReLU run when you call `tolist()`. Until then, they're an
71
+ expression waiting to be compiled. They run in one kernel here, with the matmul's
72
+ result kept in local memory for the ReLU.
73
+
74
+ Tensors support broadcasting, reductions, views and autograd. Storage formats are
75
+ bf16, fp32, int32, bfp8 and bfp4. [NOTES.md](NOTES.md#tensors-and-kernels) explains
76
+ the layout and precision limits.
77
+
78
+ ## Models
79
+
80
+ Install NumPy for the model code and pytest for the tests:
81
+
82
+ ```sh
83
+ python3 -m pip install numpy pytest
84
+ python3 -m pytest -s test
85
+ ```
86
+
87
+ The tests include small GPT-2, Llama, mixture-of-experts and Gated DeltaNet models.
88
+ They compare results with NumPy. Some comparisons need PyTorch and transformers,
89
+ and pytest skips them if those packages aren't installed. Tests that need
90
+ checkpoint files are opt-in.
91
+
92
+ To generate text from a local SmolLM-135M checkpoint:
93
+
94
+ ```sh
95
+ python3 models/llama.py /path/to/SmolLM-135M "The meaning of life is" 6
96
+ ```
97
+
98
+ You'll need the model's config, tokenizer and safetensors weights in that folder.
99
+ The command runs baretile and NumPy side by side. It prints when they choose
100
+ different tokens, then continues with NumPy's choice.
101
+
102
+ The loader also handles Llama, Mistral, Qwen2, Qwen3, Mixtral, gpt-oss and the
103
+ Qwen3.5/3.6 text models. Most tests use small models. Full checkpoints need
104
+ separate checks. [Models and tests](NOTES.md#models-and-tests) has the commands.
105
+
106
+ To serve a model through an OpenAI-compatible HTTP API:
107
+
108
+ ```sh
109
+ python3 models/serve.py HuggingFaceTB/SmolLM-135M 8000
110
+ ```
111
+
112
+ This downloads the model if you don't already have it. You can also pass a local
113
+ folder. Requests decode together, and the server can reuse a cached prompt when
114
+ you send the next turn of a conversation. See [Serving](NOTES.md#serving) for an
115
+ example request.
116
+
117
+ ## Reading the code
118
+
119
+ Start with [hello.py](baretile/hello.py) to see a kernel launched and read back.
120
+ [pci.py](baretile/pci.py) opens the device and [chip.py](baretile/chip.py) reads and
121
+ writes its memory. [runtime.py](baretile/runtime.py) loads programs, allocates
122
+ memory and queues work.
123
+
124
+ [tensix.py](baretile/tensix.py) emits instructions.
125
+ [kernels.py](baretile/kernels.py) uses those instructions to build kernels, and
126
+ [tensor.py](baretile/tensor.py) compiles tensor expressions into launches.
127
+ Addresses and instruction fields live in [bh.py](baretile/bh.py).
128
+
129
+ [models/](models/) contains the model code and HTTP server.
130
+ [adapters/](adapters/) connects tinygrad to baretile. PyTorch uses tinygrad's torch
131
+ frontend. There are more details in [NOTES.md](NOTES.md).
132
+
133
+ Licensed under [Apache-2.0](LICENSE).
@@ -0,0 +1,121 @@
1
+ # baretile
2
+
3
+ baretile is a compiler and runtime for Tenstorrent Blackhole, written in Python.
4
+ You write tensor operations, and it compiles them into Tensix instructions. It
5
+ also handles the PCIe access needed to load and run those programs on the chip.
6
+
7
+ It runs on Tenstorrent's ttsim simulator. We haven't tested it on a card yet.
8
+
9
+ The code in `baretile/` uses only the Python standard library. The driver runs
10
+ in userspace, and baretile loads its own firmware onto the workers. It needs
11
+ Tenstorrent's board firmware for ARC and Ethernet. There's no dependency on
12
+ tt-metal, tt-umd or LLK.
13
+
14
+ ## Try it
15
+
16
+ You'll need Python 3, clang with a RISC-V target, lld, and a Blackhole build of
17
+ [ttsim](https://github.com/tenstorrent/ttsim). In a ttsim checkout, build the library:
18
+
19
+ ```sh
20
+ ./make.py src/_out/release_bh/libttsim.so
21
+ ```
22
+
23
+ Then, from the baretile checkout:
24
+
25
+ ```sh
26
+ export TTSIM=/absolute/path/to/ttsim/src/_out/release_bh/libttsim.so
27
+ python3 -m baretile.chip
28
+ python3 -m baretile.hello
29
+ ```
30
+
31
+ With 120 Tensix cores, `baretile.hello` prints:
32
+
33
+ ```text
34
+ 119 Tensix cores computed fib(30) = {832040}, and one more core dispatched the work
35
+ ```
36
+
37
+ Keep `TTSIM` set for the examples below. Without it, baretile tries to open a
38
+ Blackhole on the PCI bus. That needs root and takes the card away from its kernel
39
+ driver. See [card bring-up](NOTES.md#bring-up-on-a-p150) before running on a card.
40
+
41
+ ## Tensors
42
+
43
+ Run this in Python from the checkout:
44
+
45
+ ```python
46
+ from baretile import pci
47
+ from baretile.chip import Chip
48
+ from baretile.runtime import Runtime
49
+ from baretile.tensor import Tensor
50
+
51
+ rt = Runtime(Chip(pci.open_device()))
52
+ x = Tensor.of(rt, [[1.0, 2.0], [3.0, 4.0]])
53
+ w = Tensor.of(rt, [[2.0, 0.0], [0.0, 3.0]])
54
+ print((x @ w).relu().tolist())
55
+ # [[2.0, 6.0], [6.0, 12.0]]
56
+ ```
57
+
58
+ The matmul and ReLU run when you call `tolist()`. Until then, they're an
59
+ expression waiting to be compiled. They run in one kernel here, with the matmul's
60
+ result kept in local memory for the ReLU.
61
+
62
+ Tensors support broadcasting, reductions, views and autograd. Storage formats are
63
+ bf16, fp32, int32, bfp8 and bfp4. [NOTES.md](NOTES.md#tensors-and-kernels) explains
64
+ the layout and precision limits.
65
+
66
+ ## Models
67
+
68
+ Install NumPy for the model code and pytest for the tests:
69
+
70
+ ```sh
71
+ python3 -m pip install numpy pytest
72
+ python3 -m pytest -s test
73
+ ```
74
+
75
+ The tests include small GPT-2, Llama, mixture-of-experts and Gated DeltaNet models.
76
+ They compare results with NumPy. Some comparisons need PyTorch and transformers,
77
+ and pytest skips them if those packages aren't installed. Tests that need
78
+ checkpoint files are opt-in.
79
+
80
+ To generate text from a local SmolLM-135M checkpoint:
81
+
82
+ ```sh
83
+ python3 models/llama.py /path/to/SmolLM-135M "The meaning of life is" 6
84
+ ```
85
+
86
+ You'll need the model's config, tokenizer and safetensors weights in that folder.
87
+ The command runs baretile and NumPy side by side. It prints when they choose
88
+ different tokens, then continues with NumPy's choice.
89
+
90
+ The loader also handles Llama, Mistral, Qwen2, Qwen3, Mixtral, gpt-oss and the
91
+ Qwen3.5/3.6 text models. Most tests use small models. Full checkpoints need
92
+ separate checks. [Models and tests](NOTES.md#models-and-tests) has the commands.
93
+
94
+ To serve a model through an OpenAI-compatible HTTP API:
95
+
96
+ ```sh
97
+ python3 models/serve.py HuggingFaceTB/SmolLM-135M 8000
98
+ ```
99
+
100
+ This downloads the model if you don't already have it. You can also pass a local
101
+ folder. Requests decode together, and the server can reuse a cached prompt when
102
+ you send the next turn of a conversation. See [Serving](NOTES.md#serving) for an
103
+ example request.
104
+
105
+ ## Reading the code
106
+
107
+ Start with [hello.py](baretile/hello.py) to see a kernel launched and read back.
108
+ [pci.py](baretile/pci.py) opens the device and [chip.py](baretile/chip.py) reads and
109
+ writes its memory. [runtime.py](baretile/runtime.py) loads programs, allocates
110
+ memory and queues work.
111
+
112
+ [tensix.py](baretile/tensix.py) emits instructions.
113
+ [kernels.py](baretile/kernels.py) uses those instructions to build kernels, and
114
+ [tensor.py](baretile/tensor.py) compiles tensor expressions into launches.
115
+ Addresses and instruction fields live in [bh.py](baretile/bh.py).
116
+
117
+ [models/](models/) contains the model code and HTTP server.
118
+ [adapters/](adapters/) connects tinygrad to baretile. PyTorch uses tinygrad's torch
119
+ frontend. There are more details in [NOTES.md](NOTES.md).
120
+
121
+ Licensed under [Apache-2.0](LICENSE).
@@ -0,0 +1,39 @@
1
+ # Time a matmul with B stored by columns, as model weights are. Report throughput and the bytes read
2
+ # relative to reading each input once. Byte counts need a card or ttsim-chaos. On ttsim, timings reflect
3
+ # RISC-V work; Tensix instructions and DRAM reads take no time. Use a card for performance measurements.
4
+ # TTSIM=/path/to/libttsim.so python3 -m baretile.bench [M K N [fidelity phases [bf16|bfp8|bfp4]]]
5
+ import sys
6
+ from baretile import bh, pci, tensor
7
+ from baretile.chip import Chip
8
+ from baretile.tensix import tile_bytes
9
+ from baretile.tensor import Tensor
10
+ from baretile.runtime import Runtime
11
+
12
+ def bench(chip, m, k, n, weights="bf16", repeat=3):
13
+ """Time (m x k) @ (k x n), with B stored by columns in weights format. Return (seconds, TFLOPS, input GB/s,
14
+ read ratio, peak TFLOPS). Input GB/s assumes each input is read once. The read ratio compares counted
15
+ bytes with that minimum, or is None without NIU counters. Peak TFLOPS uses the grid workers and reported
16
+ AI clock, excluding the dispatcher and workers outside the grid. Inputs are 1.0 so the result is easy to
17
+ check."""
18
+ rt = Runtime(chip)
19
+ a, b = (Tensor.frombytes(rt, r, c, b"\x80\x3f" * (r * c)) for r, c in ((m, k), (k, n))) # bf16 1.0
20
+ b = b.cast(weights).realize().columns()
21
+ assert (a @ b).tolist()[0][0] == k # compiles it, and checks the answer
22
+ since, start = rt.launches, chip.pci.now()
23
+ for _ in range(repeat): (a @ b).realize() # queued back to back
24
+ rt.wait()
25
+ seconds, read = (chip.pci.now() - start) / repeat, sum(n for *_, n in rt.profile(since)) / repeat if rt.counts else None
26
+ once = -(-m // 32) * -(-k // 32) * tile_bytes("bf16") + -(-k // 32) * -(-n // 32) * tile_bytes(weights) # each input's tiles
27
+ peak = len(rt.grid[0]) * len(rt.grid[1]) * bh.MVMUL_FLOPS * chip.aiclk * 1e6 / tensor.FIDELITY / 1e12 # a product takes FIDELITY phases
28
+ return seconds, 2 * m * k * n / seconds / 1e12, once / seconds / 1e9, read and read / once, peak
29
+
30
+ if __name__ == "__main__":
31
+ chip = Chip(pci.open_device())
32
+ m, k, n = map(int, sys.argv[1:4]) if len(sys.argv) > 3 else (512, 768, 3072)
33
+ if len(sys.argv) > 4: tensor.FIDELITY = int(sys.argv[4])
34
+ weights = sys.argv[5] if len(sys.argv) > 5 else "bf16"
35
+ seconds, tflops, gbs, reread, peak = bench(chip, m, k, n, weights)
36
+ speed = (f"{seconds * bh.AICLK / 1e3:.1f}k ttsim cycles (RISC-V work on the critical path, not silicon time)"
37
+ if isinstance(chip.pci, pci.Sim) else
38
+ f"{seconds * 1e3:.3f} ms, {tflops:.2f} TFLOPS of {peak:.0f} peak at {tensor.FIDELITY} fidelity phases, {gbs:.0f} GB/s")
39
+ print(f"{m}x{k} @ {k}x{n} ({weights} B): {speed}" + (f"; DRAM reads {reread:.2f}x the inputs" if reread is not None else ""))