Skip to content

Commit bdd44d4

Browse files
authored
Fix DSP transcendentals (tinygrad#9542)
1 parent eddafb8 commit bdd44d4

File tree

4 files changed

+8
-4
lines changed

4 files changed

+8
-4
lines changed

.github/actions/setup-tinygrad/action.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ runs:
215215
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
216216
echo "deb http://apt.llvm.org/$(lsb_release -cs)/ llvm-toolchain-$(lsb_release -cs)-19 main" | sudo tee /etc/apt/sources.list.d/llvm.list
217217
sudo apt update -y || true
218-
sudo apt install -y --no-install-recommends libllvm19
218+
sudo apt install -y --no-install-recommends libllvm19 clang-19 lld-19
219219
220220
- name: Install LLVM (macOS)
221221
if: inputs.llvm == 'true' && runner.os == 'macOS'

.github/workflows/test.yml

+2
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ jobs:
498498
cache-to: type=gha,mode=min
499499
- name: Run test_tiny on DSP
500500
run: DEBUG=2 DSP=1 python test/test_tiny.py
501+
- name: Test transcendentals
502+
run: CC=clang-19 PYTHONPATH="." DEBUG=2 DSP=1 python test/test_transcendental.py TestTranscendentalVectorized
501503
- name: Test quantize onnx
502504
run: PYTHONPATH="." DEBUG=2 DSP=1 python3 test/test_quantize_onnx.py
503505
- name: Test LLVM=1 DEVECTORIZE=0

test/test_transcendental.py

+4
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,16 @@ def test_exp2_vectorized(self):
148148
def test_log2_vectorized(self):
149149
for vec_size in [1,2,3,4,5,127,128]: self._test_vectorized_op(Tensor.log2, np.log2, (0.001, 200), vec_size)
150150

151+
@unittest.skipIf(getenv("DSP"), "requires int division")
151152
def test_sin_vectorized(self):
152153
for vec_size in [1,2,3,4,5,127,128]: self._test_vectorized_op(Tensor.sin, np.sin, (-100, 100), vec_size)
153154

154155
def test_pow_vectorized(self):
155156
# np.pow returns nan for negative values raised to a non-integral power
156157
for vec_size in [1,2,3,4,5,127,128]: self._test_vectorized_op(Tensor.pow, np.pow, (0.001, 200), vec_size, param_range=(-10, 10))
157158

159+
def test_sqrt_vectorized(self):
160+
for vec_size in [1,2,3,4,5,127,128]: self._test_vectorized_op(Tensor.sqrt, np.sqrt, (0, 100), vec_size)
161+
158162
if __name__ == '__main__':
159163
unittest.main()

tinygrad/runtime/ops_dsp.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ class DSPRenderer(ClangRenderer):
4141
extra_matcher = dsp_pm_late+ClangRenderer.extra_matcher
4242
string_rewrite = dsp_string+ClangRenderer.string_rewrite
4343
type_map = { **ClangRenderer.type_map, dtypes.uint64: "unsigned long long", dtypes.int64: "long long" }
44-
code_for_op = {**ClangRenderer.code_for_op, Ops.SIN: lambda x,dtype: f"__builtin_sin({x})",
45-
Ops.LOG2: lambda x,dtype: f"__builtin_log2l({x})" if dtype == dtypes.float64 else f"__builtin_log2f({x})",
46-
Ops.EXP2: lambda x,dtype: f"__builtin_exp2l({x})" if dtype == dtypes.float64 else f"__builtin_exp2f({x})"}
44+
code_for_op = {k:v for k,v in ClangRenderer.code_for_op.items() if k != Ops.SQRT}
4745

4846
def render_kernel(self, function_name:str, kernel:list[str], bufs:list[tuple[str,tuple[DType,bool]]], uops:list[UOp], prefix=None) -> str:
4947
ret = super().render_kernel(function_name, kernel, bufs, uops, prefix)

0 commit comments

Comments
 (0)