superfloat 0.16.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 (92) hide show
  1. superfloat-0.16.0/LICENSE +70 -0
  2. superfloat-0.16.0/PKG-INFO +255 -0
  3. superfloat-0.16.0/README.md +158 -0
  4. superfloat-0.16.0/pyproject.toml +39 -0
  5. superfloat-0.16.0/setup.cfg +4 -0
  6. superfloat-0.16.0/src/sf/__init__.py +89 -0
  7. superfloat-0.16.0/src/sf/_accumulate.py +101 -0
  8. superfloat-0.16.0/src/sf/_array.py +800 -0
  9. superfloat-0.16.0/src/sf/_cache.py +67 -0
  10. superfloat-0.16.0/src/sf/_config.py +141 -0
  11. superfloat-0.16.0/src/sf/_device.py +205 -0
  12. superfloat-0.16.0/src/sf/_errors.py +101 -0
  13. superfloat-0.16.0/src/sf/_errstate.py +173 -0
  14. superfloat-0.16.0/src/sf/_fastmx.py +198 -0
  15. superfloat-0.16.0/src/sf/_format.py +120 -0
  16. superfloat-0.16.0/src/sf/_functions.py +365 -0
  17. superfloat-0.16.0/src/sf/_heuristic.py +169 -0
  18. superfloat-0.16.0/src/sf/_intops.py +570 -0
  19. superfloat-0.16.0/src/sf/_linalg.py +369 -0
  20. superfloat-0.16.0/src/sf/_preflight.py +220 -0
  21. superfloat-0.16.0/src/sf/_reference.py +190 -0
  22. superfloat-0.16.0/src/sf/_scalar.py +429 -0
  23. superfloat-0.16.0/src/sf/_status.py +65 -0
  24. superfloat-0.16.0/src/sf/_vectorized.py +159 -0
  25. superfloat-0.16.0/src/sf/kernels/__init__.py +8 -0
  26. superfloat-0.16.0/src/sf/kernels/_cgen.py +483 -0
  27. superfloat-0.16.0/src/sf/kernels/_interp.py +290 -0
  28. superfloat-0.16.0/src/sf/kernels/_ir.py +231 -0
  29. superfloat-0.16.0/src/sf/kernels/_lang.py +362 -0
  30. superfloat-0.16.0/src/sf/kernels/_runtime.py +521 -0
  31. superfloat-0.16.0/src/sf/kernels/_sym.py +440 -0
  32. superfloat-0.16.0/src/sf/kernels/sf16_builtins.h +130 -0
  33. superfloat-0.16.0/src/sf/torch/__init__.py +114 -0
  34. superfloat-0.16.0/src/sf/torch/_attention.py +232 -0
  35. superfloat-0.16.0/src/sf/torch/_autocast.py +123 -0
  36. superfloat-0.16.0/src/sf/torch/_backend.py +176 -0
  37. superfloat-0.16.0/src/sf/torch/_configs.py +173 -0
  38. superfloat-0.16.0/src/sf/torch/_core.py +752 -0
  39. superfloat-0.16.0/src/sf/torch/_intops.py +70 -0
  40. superfloat-0.16.0/src/sf/torch/_modules.py +544 -0
  41. superfloat-0.16.0/src/sf/torch/_mxfp4.py +181 -0
  42. superfloat-0.16.0/src/sf/torch/_ops.py +417 -0
  43. superfloat-0.16.0/src/sf/torch/_scopes.py +419 -0
  44. superfloat-0.16.0/src/sf/torch/_status.py +162 -0
  45. superfloat-0.16.0/src/sf/torch/_widths.py +314 -0
  46. superfloat-0.16.0/src/sf/torch/data/cost_table_v0.json +2180 -0
  47. superfloat-0.16.0/src/superfloat.egg-info/PKG-INFO +255 -0
  48. superfloat-0.16.0/src/superfloat.egg-info/SOURCES.txt +90 -0
  49. superfloat-0.16.0/src/superfloat.egg-info/dependency_links.txt +1 -0
  50. superfloat-0.16.0/src/superfloat.egg-info/requires.txt +18 -0
  51. superfloat-0.16.0/src/superfloat.egg-info/top_level.txt +1 -0
  52. superfloat-0.16.0/tests/test_1_0_1_types_casts.py +335 -0
  53. superfloat-0.16.0/tests/test_1_0_2_add_sub.py +159 -0
  54. superfloat-0.16.0/tests/test_1_0_3_mul_fma.py +183 -0
  55. superfloat-0.16.0/tests/test_1_0_4_div.py +167 -0
  56. superfloat-0.16.0/tests/test_1_0_5_comparisons.py +140 -0
  57. superfloat-0.16.0/tests/test_1_1_1_arrays.py +330 -0
  58. superfloat-0.16.0/tests/test_1_1_2_reductions.py +124 -0
  59. superfloat-0.16.0/tests/test_1_1_3_matmul_device.py +325 -0
  60. superfloat-0.16.0/tests/test_1_1_4_functions.py +119 -0
  61. superfloat-0.16.0/tests/test_2_0_quantize.py +135 -0
  62. superfloat-0.16.0/tests/test_2_1_convert.py +360 -0
  63. superfloat-0.16.0/tests/test_2_2_compile.py +215 -0
  64. superfloat-0.16.0/tests/test_3_kernels.py +606 -0
  65. superfloat-0.16.0/tests/test_bias_on_chip.py +144 -0
  66. superfloat-0.16.0/tests/test_bodmas.py +228 -0
  67. superfloat-0.16.0/tests/test_cache.py +109 -0
  68. superfloat-0.16.0/tests/test_configs.py +47 -0
  69. superfloat-0.16.0/tests/test_errstate.py +202 -0
  70. superfloat-0.16.0/tests/test_fastmx.py +102 -0
  71. superfloat-0.16.0/tests/test_golden_wide.py +125 -0
  72. superfloat-0.16.0/tests/test_hf_conv1d.py +101 -0
  73. superfloat-0.16.0/tests/test_intops.py +318 -0
  74. superfloat-0.16.0/tests/test_kernel_dot_wide_shift.py +186 -0
  75. superfloat-0.16.0/tests/test_lowering_spec.py +112 -0
  76. superfloat-0.16.0/tests/test_mxfp4.py +211 -0
  77. superfloat-0.16.0/tests/test_properties.py +101 -0
  78. superfloat-0.16.0/tests/test_review_findings.py +201 -0
  79. superfloat-0.16.0/tests/test_review_findings_1_1.py +261 -0
  80. superfloat-0.16.0/tests/test_review_findings_2.py +236 -0
  81. superfloat-0.16.0/tests/test_review_findings_3.py +348 -0
  82. superfloat-0.16.0/tests/test_review_findings_rtl.py +278 -0
  83. superfloat-0.16.0/tests/test_sf8_split.py +194 -0
  84. superfloat-0.16.0/tests/test_shift_arrays.py +154 -0
  85. superfloat-0.16.0/tests/test_spec_examples.py +110 -0
  86. superfloat-0.16.0/tests/test_stage2_coverage.py +514 -0
  87. superfloat-0.16.0/tests/test_torch_attention.py +312 -0
  88. superfloat-0.16.0/tests/test_torch_wide.py +266 -0
  89. superfloat-0.16.0/tests/test_v481_ops.py +116 -0
  90. superfloat-0.16.0/tests/test_weight_cache.py +127 -0
  91. superfloat-0.16.0/tests/test_wide_mode.py +529 -0
  92. superfloat-0.16.0/tests/test_widths.py +119 -0
@@ -0,0 +1,70 @@
1
+ # Superfloat SDK License Agreement
2
+
3
+ Copyright (c) 2026 Superfloat Private Limited (superfloat.si) ("Licensor"). All rights reserved, except as granted below.
4
+
5
+ By installing, copying or using the Software, you agree to this Agreement. If you do not agree, do not use it. If you accept on behalf of an organization, you confirm you have the authority to bind it.
6
+
7
+ ## 1. Definitions
8
+
9
+ - **"Software"** means the sf Python package, its kernel language, PyTorch integration, lowering specification, golden vectors, documentation and any updates Licensor provides under this Agreement.
10
+ - **"Superfloat Hardware"** means Atreides processors and any other processors designed by or for Licensor and sold or authorized by Licensor.
11
+ - **"Authorized Target"** means (a) Superfloat Hardware, (b) Licensor's simulators, emulators and RTL models of Superfloat Hardware, and (c) the Software's host reference and mock devices, when used as allowed in Section 2.
12
+ - **"Application"** means your program that uses the Software and adds substantial functionality to it.
13
+ - **"Competing Hardware"** means any processor, accelerator, FPGA bitstream or circuit design that is not Superfloat Hardware.
14
+
15
+ ## 2. License grant
16
+
17
+ Subject to this Agreement, Licensor grants you a free, worldwide, non-exclusive, non-transferable, revocable license to:
18
+
19
+ 1. **Use** the Software to develop, test, debug and run Applications on Authorized Targets.
20
+ 2. **Develop on any computer.** Run the Software on any CPU or GPU to develop, test or evaluate work intended for Superfloat Hardware, and for research and evaluation of the SF number format. This includes running the host reference.
21
+ 3. **Modify** the Software for your own use under this Agreement.
22
+ 4. **Distribute Applications**, with the parts of the Software they need, if each recipient is bound by terms at least as protective as these, and you keep all notices.
23
+ 5. **Publish results.** Publish research and benchmark results obtained with the Software, if you state the Software version and method.
24
+
25
+ ## 3. Restrictions
26
+
27
+ You may not, and may not let others:
28
+
29
+ 1. **Use the Software in production on Competing Hardware,** or to build, program or tune a product for Competing Hardware. Section 2.2 is the only exception.
30
+ 2. **Port or translate.** Create a compiler back end, translation layer, emulator or compatibility layer that makes the Software, its kernels, its lowering specification or its output run on Competing Hardware.
31
+ 3. **Design from it.** Use the lowering specification, golden vectors or arithmetic definitions to design, verify or validate Competing Hardware.
32
+ 4. **Compete with it.** Distribute the Software on its own, or offer it or a modified version as a standalone SDK or hosted service.
33
+ 5. **Remove notices,** or use the Superfloat or Atreides names and marks, except to state accurately that an Application works with them.
34
+ 6. **Fork publicly** except as needed to contribute to the Software under Section 5.
35
+
36
+ Viewing the source code is permitted. It grants no rights beyond those in this Agreement.
37
+
38
+ ## 4. Patents and hardware rights
39
+
40
+ 1. **Reservation.** No rights are granted under any patent, or in any hardware design, RTL, netlist, layout or mask work. This includes the Superfloat hardware designs, which are not part of the Software. Publishing the Software is not a license to implement its arithmetic in hardware.
41
+ 2. **Limited patent permission.** To the extent a Licensor patent would otherwise be infringed by your use of the Software as Section 2 allows, Licensor will not assert it against that use.
42
+ 3. **Defensive termination.** If you bring a patent claim against Licensor or any user of the Software over the Software or Superfloat Hardware, your rights under this Agreement end on the date you file it.
43
+
44
+ ## 5. Contributions and feedback
45
+
46
+ If you submit code, documentation or feedback, you grant Licensor a perpetual, irrevocable, worldwide, royalty-free license, with the right to sublicense, to use it for any purpose, including under other license terms, and you confirm you have the right to grant this.
47
+
48
+ ## 6. Third-party software
49
+
50
+ Third-party components, including NumPy and PyTorch, are licensed by their own owners under their own terms.
51
+
52
+ ## 7. Term and termination
53
+
54
+ This Agreement lasts until terminated. It ends automatically if you breach it and do not cure the breach within 30 days of notice, or at once for a breach of Section 3. Licensor may also end it for all users of a version on 90 days' public notice. On termination, stop using and delete the Software. Applications lawfully distributed earlier may remain with their recipients. Sections 3, 4, 8, 9 and 10 survive.
55
+
56
+ ## 8. Disclaimer of warranty
57
+
58
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IT IS NOT DESIGNED FOR SAFETY-CRITICAL USE.
59
+
60
+ ## 9. Limitation of liability
61
+
62
+ TO THE EXTENT THE LAW ALLOWS, LICENSOR IS NOT LIABLE FOR INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES, OR LOST PROFITS OR DATA. LICENSOR'S TOTAL LIABILITY IS LIMITED TO USD 100.
63
+
64
+ ## 10. General
65
+
66
+ - **Export.** You must comply with applicable export control and sanctions laws.
67
+ - **Governing law.** The laws of India, with the courts of Kerala having exclusive jurisdiction.
68
+ - **Changes.** A new version of this Agreement applies to versions of the Software released with it.
69
+ - **Entire agreement.** This is the entire agreement about the Software. If a term is unenforceable, the rest stays in force. No waiver is effective unless written.
70
+ - **Other terms.** A separate signed agreement with Licensor takes priority over this one.
@@ -0,0 +1,255 @@
1
+ Metadata-Version: 2.4
2
+ Name: superfloat
3
+ Version: 0.16.0
4
+ Summary: sf: the Python library for Superfloat numbers (sf4 to sf16 fixed point) on the Atreides chip: arrays, PyTorch integration and GPU-style kernels.
5
+ License: # Superfloat SDK License Agreement
6
+
7
+ Copyright (c) 2026 Superfloat Private Limited (superfloat.si) ("Licensor"). All rights reserved, except as granted below.
8
+
9
+ By installing, copying or using the Software, you agree to this Agreement. If you do not agree, do not use it. If you accept on behalf of an organization, you confirm you have the authority to bind it.
10
+
11
+ ## 1. Definitions
12
+
13
+ - **"Software"** means the sf Python package, its kernel language, PyTorch integration, lowering specification, golden vectors, documentation and any updates Licensor provides under this Agreement.
14
+ - **"Superfloat Hardware"** means Atreides processors and any other processors designed by or for Licensor and sold or authorized by Licensor.
15
+ - **"Authorized Target"** means (a) Superfloat Hardware, (b) Licensor's simulators, emulators and RTL models of Superfloat Hardware, and (c) the Software's host reference and mock devices, when used as allowed in Section 2.
16
+ - **"Application"** means your program that uses the Software and adds substantial functionality to it.
17
+ - **"Competing Hardware"** means any processor, accelerator, FPGA bitstream or circuit design that is not Superfloat Hardware.
18
+
19
+ ## 2. License grant
20
+
21
+ Subject to this Agreement, Licensor grants you a free, worldwide, non-exclusive, non-transferable, revocable license to:
22
+
23
+ 1. **Use** the Software to develop, test, debug and run Applications on Authorized Targets.
24
+ 2. **Develop on any computer.** Run the Software on any CPU or GPU to develop, test or evaluate work intended for Superfloat Hardware, and for research and evaluation of the SF number format. This includes running the host reference.
25
+ 3. **Modify** the Software for your own use under this Agreement.
26
+ 4. **Distribute Applications**, with the parts of the Software they need, if each recipient is bound by terms at least as protective as these, and you keep all notices.
27
+ 5. **Publish results.** Publish research and benchmark results obtained with the Software, if you state the Software version and method.
28
+
29
+ ## 3. Restrictions
30
+
31
+ You may not, and may not let others:
32
+
33
+ 1. **Use the Software in production on Competing Hardware,** or to build, program or tune a product for Competing Hardware. Section 2.2 is the only exception.
34
+ 2. **Port or translate.** Create a compiler back end, translation layer, emulator or compatibility layer that makes the Software, its kernels, its lowering specification or its output run on Competing Hardware.
35
+ 3. **Design from it.** Use the lowering specification, golden vectors or arithmetic definitions to design, verify or validate Competing Hardware.
36
+ 4. **Compete with it.** Distribute the Software on its own, or offer it or a modified version as a standalone SDK or hosted service.
37
+ 5. **Remove notices,** or use the Superfloat or Atreides names and marks, except to state accurately that an Application works with them.
38
+ 6. **Fork publicly** except as needed to contribute to the Software under Section 5.
39
+
40
+ Viewing the source code is permitted. It grants no rights beyond those in this Agreement.
41
+
42
+ ## 4. Patents and hardware rights
43
+
44
+ 1. **Reservation.** No rights are granted under any patent, or in any hardware design, RTL, netlist, layout or mask work. This includes the Superfloat hardware designs, which are not part of the Software. Publishing the Software is not a license to implement its arithmetic in hardware.
45
+ 2. **Limited patent permission.** To the extent a Licensor patent would otherwise be infringed by your use of the Software as Section 2 allows, Licensor will not assert it against that use.
46
+ 3. **Defensive termination.** If you bring a patent claim against Licensor or any user of the Software over the Software or Superfloat Hardware, your rights under this Agreement end on the date you file it.
47
+
48
+ ## 5. Contributions and feedback
49
+
50
+ If you submit code, documentation or feedback, you grant Licensor a perpetual, irrevocable, worldwide, royalty-free license, with the right to sublicense, to use it for any purpose, including under other license terms, and you confirm you have the right to grant this.
51
+
52
+ ## 6. Third-party software
53
+
54
+ Third-party components, including NumPy and PyTorch, are licensed by their own owners under their own terms.
55
+
56
+ ## 7. Term and termination
57
+
58
+ This Agreement lasts until terminated. It ends automatically if you breach it and do not cure the breach within 30 days of notice, or at once for a breach of Section 3. Licensor may also end it for all users of a version on 90 days' public notice. On termination, stop using and delete the Software. Applications lawfully distributed earlier may remain with their recipients. Sections 3, 4, 8, 9 and 10 survive.
59
+
60
+ ## 8. Disclaimer of warranty
61
+
62
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IT IS NOT DESIGNED FOR SAFETY-CRITICAL USE.
63
+
64
+ ## 9. Limitation of liability
65
+
66
+ TO THE EXTENT THE LAW ALLOWS, LICENSOR IS NOT LIABLE FOR INDIRECT, INCIDENTAL, SPECIAL, CONSEQUENTIAL OR PUNITIVE DAMAGES, OR LOST PROFITS OR DATA. LICENSOR'S TOTAL LIABILITY IS LIMITED TO USD 100.
67
+
68
+ ## 10. General
69
+
70
+ - **Export.** You must comply with applicable export control and sanctions laws.
71
+ - **Governing law.** The laws of India, with the courts of Kerala having exclusive jurisdiction.
72
+ - **Changes.** A new version of this Agreement applies to versions of the Software released with it.
73
+ - **Entire agreement.** This is the entire agreement about the Software. If a term is unenforceable, the rest stays in force. No waiver is effective unless written.
74
+ - **Other terms.** A separate signed agreement with Licensor takes priority over this one.
75
+
76
+ Project-URL: Homepage, https://superfloat.si
77
+ Project-URL: Documentation, https://docs.superfloat.si
78
+ Project-URL: Source, https://github.com/superfloat/superfloat.python
79
+ Requires-Python: >=3.10
80
+ Description-Content-Type: text/markdown
81
+ License-File: LICENSE
82
+ Requires-Dist: numpy>=1.24
83
+ Provides-Extra: torch
84
+ Requires-Dist: torch>=2.4; extra == "torch"
85
+ Provides-Extra: test
86
+ Requires-Dist: pytest>=8; extra == "test"
87
+ Requires-Dist: hypothesis>=6; extra == "test"
88
+ Requires-Dist: numpy>=1.24; extra == "test"
89
+ Requires-Dist: torch>=2.4; extra == "test"
90
+ Requires-Dist: scikit-learn>=1.3; extra == "test"
91
+ Provides-Extra: hf
92
+ Requires-Dist: transformers>=5.20; extra == "hf"
93
+ Requires-Dist: accelerate>=1.0; extra == "hf"
94
+ Provides-Extra: xdist
95
+ Requires-Dist: pytest-xdist>=3; extra == "xdist"
96
+ Dynamic: license-file
97
+
98
+ # sf: Superfloat in Python
99
+
100
+ `sf` is the Python library for the Superfloat number formats (sf4 to sf16)
101
+ used by the Atreides chip. You write normal Python, NumPy-style arrays,
102
+ PyTorch models or GPU kernels, and `sf` makes sure every number behaves
103
+ exactly as the chip would compute it, bit for bit, and tells you when
104
+ something overflowed, underflowed, divided by zero or went invalid.
105
+
106
+ ## What is Superfloat, in one paragraph
107
+
108
+ An sf16 value is a 16-bit signed fixed-point number strictly between -1 and 1.
109
+ There is no exponent, no NaN, no infinity. Adding or subtracting can push a
110
+ value past the edge: it then sticks at the largest value (saturates).
111
+ Multiplying two small values can land on 0 (underflows). Dividing `a / b`
112
+ only works when `|a| < |b|`. The chip does exactly this, and so does `sf`.
113
+
114
+ ## What you can do with it
115
+
116
+ **1. Scalars** (Stage 1.0)
117
+
118
+ ```python
119
+ import sf
120
+
121
+ a, b = sf.sf16(0.5), sf.sf16(0.25)
122
+ a + b # sf16(0.75)
123
+ a * b # sf16(0.125)
124
+ b / a # sf16(0.5)
125
+ a / b # SFOverflowError: 2.0 does not fit in sf16
126
+ a < 1 # True, comparisons are exact
127
+
128
+ with sf.errstate(over="ignore"): # or just let it saturate, like the chip
129
+ sf.sf16(0.75) + 0.5 # sf16(0.999969482421875)
130
+ ```
131
+
132
+ **2. Arrays, matmul, and the device** (Stage 1.1)
133
+
134
+ ```python
135
+ x = sf.array([0.10, -0.25, 0.50], dtype=sf.sf16) # lives on the host
136
+ h = sf.asarray(inputs, dtype=sf.sf16, device="atreides")
137
+ w = sf.asarray(weights, dtype=sf.sf16, device="atreides")
138
+ y = h @ w # runs on the device
139
+ y.status # what saturated, if anything
140
+ y.numpy() # back to floats
141
+ ```
142
+
143
+ Before anything is sent to the device, `sf` checks on the host whether the
144
+ answer is already known (a divide by zero, a result that must saturate, a
145
+ range that cannot saturate). Anything the host can decide never reaches the
146
+ chip. This is the rule the whole device path follows.
147
+
148
+ **3. PyTorch models** (Stage 2)
149
+
150
+ ```python
151
+ import sf.torch as sft
152
+
153
+ model = sft.convert(model, dtype=sf.sf16) # Linear, Conv2d and Hugging Face Conv1D now compute in sf16
154
+ fast = torch.compile(model, backend="superfloat") # sends the SF layers to the device
155
+ out = fast(x)
156
+ sft.last_status(model) # per-layer saturation report
157
+ ```
158
+
159
+ Weights and activations are scaled so the chip's -1..1 range is used well,
160
+ and by default the scale leaves enough headroom that nothing can saturate
161
+ inside a layer. Training still works (straight-through gradients).
162
+
163
+ **4. Your own GPU kernels** (Stage 3)
164
+
165
+ ```python
166
+ @sf.jit # Triton style
167
+ def axpy(a, x_ptr, y_ptr, out_ptr, n, BLOCK: sf.constexpr):
168
+ offs = sf.program_id(0) * BLOCK + sf.arange(0, BLOCK)
169
+ mask = offs < n
170
+ x = sf.load(x_ptr + offs, mask=mask)
171
+ y = sf.load(y_ptr + offs, mask=mask)
172
+ sf.store(out_ptr + offs, sf.fma(a, x, y), mask=mask)
173
+
174
+ dev = sf.device("atreides-sim")
175
+ x, y, out = dev.upload(xs), dev.upload(ys), dev.alloc(n)
176
+ r = axpy[(sf.cdiv(n, 256),)](sf.sf16(0.75), x, y, out, n, BLOCK=256)
177
+ r.status, r.cycles # exact events, simulated cycles
178
+ ```
179
+
180
+ There is also a CUDA-style `@sf.kernel` with `thread_idx`, shared memory and
181
+ barriers. Kernels run on a simulator today (`atreides-sim`) which has two
182
+ independent back ends, a Python interpreter and generated C, that must agree
183
+ bit for bit. `atreides-hw` is reserved for the real chip and waits for its
184
+ driver.
185
+
186
+ ## Install and run
187
+
188
+ ```
189
+ pip install superfloat # the library; in Python: import sf
190
+ pip install "superfloat[torch]" # with PyTorch, for sf.torch
191
+ ```
192
+
193
+ From a checkout, for development:
194
+
195
+ ```
196
+ pip install -e ".[test]" # library + test tools
197
+ pip install -e ".[torch]" # add PyTorch for sf.torch
198
+ pytest # the full per-commit suite (about 8 min)
199
+ pytest -m "not slow" # quick run while developing (about 40 s)
200
+ python examples/stage_3_tour.py # see the kernels in action
201
+ ```
202
+
203
+ Only NumPy is required. The C kernel back end needs a C compiler (`cc`).
204
+
205
+ ## Errors: what happens when a number does not fit
206
+
207
+ | Event | Meaning | Default |
208
+ |---|---|---|
209
+ | overflow | result saturated at the largest value | raise `SFOverflowError` |
210
+ | underflow | result became 0 | warn `SFUnderflowWarning` |
211
+ | divide | divided by zero | raise `SFZeroDivisionError` |
212
+ | invalid | no valid answer (sqrt of a negative) | raise `SFInvalidError` |
213
+
214
+ Change it per event with `sf.seterr(over="warn")` or inside a block with
215
+ `with sf.errstate(all="ignore"):`. The value you get back is always the
216
+ chip's value; the policy only decides whether Python also raises or warns.
217
+
218
+ ## How it is tested
219
+
220
+ Every result is compared as integer codes, never with float tolerances,
221
+ against an independent oracle written with exact fractions. 732 tests run
222
+ on every commit (Python 3.10, 3.12, 3.13) and the exhaustive sf16 sweep
223
+ runs nightly. The full matrix of what is checked, against what, at which
224
+ widths and on which target is in [docs/TESTING.md](https://github.com/superfloat/superfloat.python/blob/main/docs/TESTING.md).
225
+
226
+ ## What is done and what is not
227
+
228
+ Done: Stages 1.0, 1.1, 2 (2.0, 2.1, 2.2) and 3 (3.0 to 3.3) of the plan,
229
+ each reviewed independently with all findings fixed.
230
+
231
+ Not done:
232
+
233
+ * Running on the real chip. Everything above is proved on the host, a mock
234
+ and a simulator. The hardware gate needs the custom compiler, a driver to
235
+ attach, and answers to the open hardware questions (division and sqrt
236
+ fault values, accumulation order and width, `mat_fma` meaning, readable
237
+ flags). See `NOTES.md`.
238
+ * Stage 1.2 diagnostics (`sf.explain`) and Stage 2.3 PyTorch device tensors,
239
+ deferred on purpose.
240
+
241
+ ## Documentation
242
+
243
+ | File | What it holds |
244
+ |---|---|
245
+ | [docs/API.md](https://github.com/superfloat/superfloat.python/blob/main/docs/API.md) | Every function, decorator and object for Stages 1 to 3, with syntax and the Python semantics of SF values. The reference to use when writing tests against the simulator. |
246
+ | [docs/TESTING.md](https://github.com/superfloat/superfloat.python/blob/main/docs/TESTING.md) | The testing matrix, the test files, how to run each gate. |
247
+ | [docs/DETAILS.md](https://github.com/superfloat/superfloat.python/blob/main/docs/DETAILS.md) | Module map, exact semantics tables, every config option, design notes per stage. |
248
+ | [spec/](https://github.com/superfloat/superfloat.python/blob/main/spec/ops.py) | The lowering spec for the compiler: every IR op with operands, results, events, memory effects and legal rewrites, plus golden vectors. Generated pages under `website/compiler/`. Run `python tools/gen_spec.py` after editing `spec/ops.py`. |
249
+ | [NOTES.md](https://github.com/superfloat/superfloat.python/blob/main/NOTES.md) | Design decisions, open hardware questions, review history. |
250
+ | `examples/` | A runnable tour per stage. |
251
+ | `website/` | The developer docs site at [docs.superfloat.si](https://docs.superfloat.si) (Mintlify, hand-written). |
252
+
253
+ ## License
254
+
255
+ sf is free to use for developing and running software on Atreides hardware, under the [Superfloat SDK License Agreement](https://github.com/superfloat/superfloat.python/blob/main/LICENSE). Copyright (c) 2026 Superfloat Private Limited ([superfloat.si](https://superfloat.si)).
@@ -0,0 +1,158 @@
1
+ # sf: Superfloat in Python
2
+
3
+ `sf` is the Python library for the Superfloat number formats (sf4 to sf16)
4
+ used by the Atreides chip. You write normal Python, NumPy-style arrays,
5
+ PyTorch models or GPU kernels, and `sf` makes sure every number behaves
6
+ exactly as the chip would compute it, bit for bit, and tells you when
7
+ something overflowed, underflowed, divided by zero or went invalid.
8
+
9
+ ## What is Superfloat, in one paragraph
10
+
11
+ An sf16 value is a 16-bit signed fixed-point number strictly between -1 and 1.
12
+ There is no exponent, no NaN, no infinity. Adding or subtracting can push a
13
+ value past the edge: it then sticks at the largest value (saturates).
14
+ Multiplying two small values can land on 0 (underflows). Dividing `a / b`
15
+ only works when `|a| < |b|`. The chip does exactly this, and so does `sf`.
16
+
17
+ ## What you can do with it
18
+
19
+ **1. Scalars** (Stage 1.0)
20
+
21
+ ```python
22
+ import sf
23
+
24
+ a, b = sf.sf16(0.5), sf.sf16(0.25)
25
+ a + b # sf16(0.75)
26
+ a * b # sf16(0.125)
27
+ b / a # sf16(0.5)
28
+ a / b # SFOverflowError: 2.0 does not fit in sf16
29
+ a < 1 # True, comparisons are exact
30
+
31
+ with sf.errstate(over="ignore"): # or just let it saturate, like the chip
32
+ sf.sf16(0.75) + 0.5 # sf16(0.999969482421875)
33
+ ```
34
+
35
+ **2. Arrays, matmul, and the device** (Stage 1.1)
36
+
37
+ ```python
38
+ x = sf.array([0.10, -0.25, 0.50], dtype=sf.sf16) # lives on the host
39
+ h = sf.asarray(inputs, dtype=sf.sf16, device="atreides")
40
+ w = sf.asarray(weights, dtype=sf.sf16, device="atreides")
41
+ y = h @ w # runs on the device
42
+ y.status # what saturated, if anything
43
+ y.numpy() # back to floats
44
+ ```
45
+
46
+ Before anything is sent to the device, `sf` checks on the host whether the
47
+ answer is already known (a divide by zero, a result that must saturate, a
48
+ range that cannot saturate). Anything the host can decide never reaches the
49
+ chip. This is the rule the whole device path follows.
50
+
51
+ **3. PyTorch models** (Stage 2)
52
+
53
+ ```python
54
+ import sf.torch as sft
55
+
56
+ model = sft.convert(model, dtype=sf.sf16) # Linear, Conv2d and Hugging Face Conv1D now compute in sf16
57
+ fast = torch.compile(model, backend="superfloat") # sends the SF layers to the device
58
+ out = fast(x)
59
+ sft.last_status(model) # per-layer saturation report
60
+ ```
61
+
62
+ Weights and activations are scaled so the chip's -1..1 range is used well,
63
+ and by default the scale leaves enough headroom that nothing can saturate
64
+ inside a layer. Training still works (straight-through gradients).
65
+
66
+ **4. Your own GPU kernels** (Stage 3)
67
+
68
+ ```python
69
+ @sf.jit # Triton style
70
+ def axpy(a, x_ptr, y_ptr, out_ptr, n, BLOCK: sf.constexpr):
71
+ offs = sf.program_id(0) * BLOCK + sf.arange(0, BLOCK)
72
+ mask = offs < n
73
+ x = sf.load(x_ptr + offs, mask=mask)
74
+ y = sf.load(y_ptr + offs, mask=mask)
75
+ sf.store(out_ptr + offs, sf.fma(a, x, y), mask=mask)
76
+
77
+ dev = sf.device("atreides-sim")
78
+ x, y, out = dev.upload(xs), dev.upload(ys), dev.alloc(n)
79
+ r = axpy[(sf.cdiv(n, 256),)](sf.sf16(0.75), x, y, out, n, BLOCK=256)
80
+ r.status, r.cycles # exact events, simulated cycles
81
+ ```
82
+
83
+ There is also a CUDA-style `@sf.kernel` with `thread_idx`, shared memory and
84
+ barriers. Kernels run on a simulator today (`atreides-sim`) which has two
85
+ independent back ends, a Python interpreter and generated C, that must agree
86
+ bit for bit. `atreides-hw` is reserved for the real chip and waits for its
87
+ driver.
88
+
89
+ ## Install and run
90
+
91
+ ```
92
+ pip install superfloat # the library; in Python: import sf
93
+ pip install "superfloat[torch]" # with PyTorch, for sf.torch
94
+ ```
95
+
96
+ From a checkout, for development:
97
+
98
+ ```
99
+ pip install -e ".[test]" # library + test tools
100
+ pip install -e ".[torch]" # add PyTorch for sf.torch
101
+ pytest # the full per-commit suite (about 8 min)
102
+ pytest -m "not slow" # quick run while developing (about 40 s)
103
+ python examples/stage_3_tour.py # see the kernels in action
104
+ ```
105
+
106
+ Only NumPy is required. The C kernel back end needs a C compiler (`cc`).
107
+
108
+ ## Errors: what happens when a number does not fit
109
+
110
+ | Event | Meaning | Default |
111
+ |---|---|---|
112
+ | overflow | result saturated at the largest value | raise `SFOverflowError` |
113
+ | underflow | result became 0 | warn `SFUnderflowWarning` |
114
+ | divide | divided by zero | raise `SFZeroDivisionError` |
115
+ | invalid | no valid answer (sqrt of a negative) | raise `SFInvalidError` |
116
+
117
+ Change it per event with `sf.seterr(over="warn")` or inside a block with
118
+ `with sf.errstate(all="ignore"):`. The value you get back is always the
119
+ chip's value; the policy only decides whether Python also raises or warns.
120
+
121
+ ## How it is tested
122
+
123
+ Every result is compared as integer codes, never with float tolerances,
124
+ against an independent oracle written with exact fractions. 732 tests run
125
+ on every commit (Python 3.10, 3.12, 3.13) and the exhaustive sf16 sweep
126
+ runs nightly. The full matrix of what is checked, against what, at which
127
+ widths and on which target is in [docs/TESTING.md](https://github.com/superfloat/superfloat.python/blob/main/docs/TESTING.md).
128
+
129
+ ## What is done and what is not
130
+
131
+ Done: Stages 1.0, 1.1, 2 (2.0, 2.1, 2.2) and 3 (3.0 to 3.3) of the plan,
132
+ each reviewed independently with all findings fixed.
133
+
134
+ Not done:
135
+
136
+ * Running on the real chip. Everything above is proved on the host, a mock
137
+ and a simulator. The hardware gate needs the custom compiler, a driver to
138
+ attach, and answers to the open hardware questions (division and sqrt
139
+ fault values, accumulation order and width, `mat_fma` meaning, readable
140
+ flags). See `NOTES.md`.
141
+ * Stage 1.2 diagnostics (`sf.explain`) and Stage 2.3 PyTorch device tensors,
142
+ deferred on purpose.
143
+
144
+ ## Documentation
145
+
146
+ | File | What it holds |
147
+ |---|---|
148
+ | [docs/API.md](https://github.com/superfloat/superfloat.python/blob/main/docs/API.md) | Every function, decorator and object for Stages 1 to 3, with syntax and the Python semantics of SF values. The reference to use when writing tests against the simulator. |
149
+ | [docs/TESTING.md](https://github.com/superfloat/superfloat.python/blob/main/docs/TESTING.md) | The testing matrix, the test files, how to run each gate. |
150
+ | [docs/DETAILS.md](https://github.com/superfloat/superfloat.python/blob/main/docs/DETAILS.md) | Module map, exact semantics tables, every config option, design notes per stage. |
151
+ | [spec/](https://github.com/superfloat/superfloat.python/blob/main/spec/ops.py) | The lowering spec for the compiler: every IR op with operands, results, events, memory effects and legal rewrites, plus golden vectors. Generated pages under `website/compiler/`. Run `python tools/gen_spec.py` after editing `spec/ops.py`. |
152
+ | [NOTES.md](https://github.com/superfloat/superfloat.python/blob/main/NOTES.md) | Design decisions, open hardware questions, review history. |
153
+ | `examples/` | A runnable tour per stage. |
154
+ | `website/` | The developer docs site at [docs.superfloat.si](https://docs.superfloat.si) (Mintlify, hand-written). |
155
+
156
+ ## License
157
+
158
+ sf is free to use for developing and running software on Atreides hardware, under the [Superfloat SDK License Agreement](https://github.com/superfloat/superfloat.python/blob/main/LICENSE). Copyright (c) 2026 Superfloat Private Limited ([superfloat.si](https://superfloat.si)).
@@ -0,0 +1,39 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "superfloat"
7
+ version = "0.16.0"
8
+ description = "sf: the Python library for Superfloat numbers (sf4 to sf16 fixed point) on the Atreides chip: arrays, PyTorch integration and GPU-style kernels."
9
+ readme = "README.md"
10
+ license = {file = "LICENSE"}
11
+ requires-python = ">=3.10"
12
+ dependencies = ["numpy>=1.24"]
13
+
14
+ [project.urls]
15
+ Homepage = "https://superfloat.si"
16
+ Documentation = "https://docs.superfloat.si"
17
+ Source = "https://github.com/superfloat/superfloat.python"
18
+
19
+ [project.optional-dependencies]
20
+ torch = ["torch>=2.4"]
21
+ test = ["pytest>=8", "hypothesis>=6", "numpy>=1.24", "torch>=2.4", "scikit-learn>=1.3"]
22
+ hf = ["transformers>=5.20", "accelerate>=1.0"] # Hugging Face models; tested pins in env/dsl-test.txt
23
+ xdist = ["pytest-xdist>=3"]
24
+
25
+ [tool.setuptools.packages.find]
26
+ where = ["src"]
27
+
28
+ [tool.setuptools.package-data]
29
+ "sf.torch" = ["data/*.json"]
30
+ "sf.kernels" = ["*.h"] # the C backend compiles against it
31
+
32
+ [tool.pytest.ini_options]
33
+ pythonpath = ["src"]
34
+ testpaths = ["tests"]
35
+ markers = [
36
+ "slow: exhaustive sf9/sf10 runs (still every commit; deselect with -m 'not slow' while iterating)",
37
+ ]
38
+ filterwarnings = ["error::sf.SFWarning"]
39
+ addopts = "-p no:logging"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,89 @@
1
+ """sf: the Python DSL for Superfloat on the Atreides GPU.
2
+
3
+ Stage 1.0 (scalar arithmetic) and Stage 1.1 (arrays and linear algebra):
4
+
5
+ >>> import sf
6
+ >>> a, b = sf.sf16(0.5), sf.sf16(0.25)
7
+ >>> a + b
8
+ sf16(0.75)
9
+ >>> a < 1 # comparisons are exact
10
+ True
11
+ >>> x = sf.array([0.10, -0.25, 0.50], dtype=sf.sf16) # host array: light
12
+ >>> h = sf.asarray(np_inputs, dtype=sf.sf16, device="atreides")
13
+ >>> y = h @ w # heavy: built-in kernel
14
+ >>> y.status # SFStatus(saturated<=12, ...)
15
+ >>> y.numpy() # read back to host
16
+ """
17
+ from __future__ import annotations
18
+
19
+ from ._accumulate import accumulate, getaccumulate, setaccumulate
20
+ from ._array import SFArray, addmm, dot, matmul, sum_ as sum # noqa: A001
21
+ from ._config import config, getconfig
22
+ from ._device import Device, MockAtreides, devices, get_device, register_device
23
+ from ._errors import * # noqa: F401,F403
24
+ from ._errors import __all__ as _errors_all
25
+ from ._errstate import errstate, geterr, seterr
26
+ from ._format import FORMATS, MAX_BITS, MIN_BITS, SFFormat
27
+ from . import _intops as intops # integer exp, softmax, GELU, SiLU, reciprocal, rsqrt (bit-exact reference)
28
+ from ._functions import * # noqa: F401,F403
29
+ from ._functions import __all__ as _functions_all
30
+ from ._scalar import DTYPES, SFScalar
31
+ from ._status import SFStatus
32
+ from .kernels import (HardwareAtreides, Kernel, LaunchResult, Limits, SimulatorAtreides, # noqa: F401
33
+ arange, autotune, barrier, block_dim, block_idx, cdiv, constexpr, jit, kernel,
34
+ load, predicate, program_id, shared, store, synchronize, thread_idx)
35
+
36
+ __version__ = "0.16.0" # P·V prefix-invariant (V per key); the bias in the first POP exponent
37
+
38
+ # sf.sf4 .. sf.sf16
39
+ globals().update({cls.__name__: cls for cls in DTYPES.values()})
40
+ dtypes = tuple(DTYPES[b] for b in sorted(DTYPES))
41
+
42
+
43
+ def device(name="atreides-sim"):
44
+ """A target by name (``sf.device("atreides-sim")``); see ``sf.devices()``."""
45
+ return get_device(name)
46
+
47
+
48
+ def max(x, axis=None): # noqa: A001
49
+ """Largest element (exact selection, no events)."""
50
+ from .kernels import _lang, is_traced
51
+ if is_traced(x):
52
+ return _lang.max(x, axis)
53
+ from ._array import _minmax
54
+ import numpy as _np
55
+ return _minmax(x, axis, _np.max)
56
+
57
+
58
+ def min(x, axis=None): # noqa: A001
59
+ """Smallest element (exact selection, no events)."""
60
+ from .kernels import _lang, is_traced
61
+ if is_traced(x):
62
+ return _lang.min(x, axis)
63
+ from ._array import _minmax
64
+ import numpy as _np
65
+ return _minmax(x, axis, _np.min)
66
+
67
+
68
+ def argmax(x, axis=None):
69
+ from ._array import _as_sfarray
70
+ return _as_sfarray(x, "argmax").argmax(axis)
71
+
72
+
73
+ def argmin(x, axis=None):
74
+ from ._array import _as_sfarray
75
+ return _as_sfarray(x, "argmin").argmin(axis)
76
+
77
+
78
+ __all__ = (
79
+ ["SFScalar", "SFArray", "SFFormat", "SFStatus", "FORMATS", "MIN_BITS", "MAX_BITS", "dtypes",
80
+ "errstate", "geterr", "seterr", "config", "getconfig",
81
+ "accumulate", "getaccumulate", "setaccumulate",
82
+ "Device", "MockAtreides", "devices", "get_device", "register_device", "device",
83
+ "sum", "max", "min", "argmax", "argmin", "dot", "matmul", "addmm", "__version__",
84
+ "kernel", "jit", "autotune", "constexpr", "program_id", "block_idx", "thread_idx", "block_dim",
85
+ "arange", "load", "store", "shared", "barrier", "predicate", "cdiv", "synchronize",
86
+ "Kernel", "LaunchResult", "Limits", "SimulatorAtreides", "HardwareAtreides"]
87
+ + [cls.__name__ for cls in dtypes]
88
+ + list(_errors_all) + list(_functions_all)
89
+ )