Skip to content

Conversation

@CharlieFRuan
Copy link
Member

Prior to this PR, tanh(x)returns NaN on metal when x > 45.0.

Metal's built-in tanh is implemented as (t - 1.0) / (t + 1.0), where t = exp(2.0 * x). Hence for large x, t becomes inf, causing tanh(x) to be NaN.

A numerically stable tanh is implemented for llvm, this PR lifts it to src/target/intrin_rule.cc and apply the same rule for metal as well.

@CharlieFRuan
Copy link
Member Author

cc @MasterJH5574 @tqchen

@CharlieFRuan
Copy link
Member Author

Observed the same issue on webgpu as well; updated to fix.

@MasterJH5574 MasterJH5574 self-assigned this Jan 20, 2024
Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This looks great!

@junrushao junrushao merged commit ffa404f into apache:main Jan 20, 2024
junrushao pushed a commit to mlc-ai/mlc-llm that referenced this pull request Jan 21, 2024
This PR updates `debug_intermediate.py` that uses instrumenting to debug a compiled model library, adding some comments on the script and making it SLM-model compatible.

To run:
```
python tests/legacy-python/dump_intermediate.py --model dist/phi-2-q4f16_1-MLC --model-lib-path dist/phi-2_q4f16_1-cuda.so
```

While we can use JIT to print intermediate values for debugging, as of now it only supports cpu. Some issues are platform-specific, and this script could still come in handy (e.g. helped solve apache/tvm#16438, #1638).
smickey040404 added a commit to smickey040404/mlc-llm that referenced this pull request Feb 11, 2025
This PR updates `debug_intermediate.py` that uses instrumenting to debug a compiled model library, adding some comments on the script and making it SLM-model compatible.

To run:
```
python tests/legacy-python/dump_intermediate.py --model dist/phi-2-q4f16_1-MLC --model-lib-path dist/phi-2_q4f16_1-cuda.so
```

While we can use JIT to print intermediate values for debugging, as of now it only supports cpu. Some issues are platform-specific, and this script could still come in handy (e.g. helped solve apache/tvm#16438, mlc-ai/mlc-llm#1638).
tristankincaid added a commit to tristankincaid/mlc-llm that referenced this pull request Feb 16, 2025
This PR updates `debug_intermediate.py` that uses instrumenting to debug a compiled model library, adding some comments on the script and making it SLM-model compatible.

To run:
```
python tests/legacy-python/dump_intermediate.py --model dist/phi-2-q4f16_1-MLC --model-lib-path dist/phi-2_q4f16_1-cuda.so
```

While we can use JIT to print intermediate values for debugging, as of now it only supports cpu. Some issues are platform-specific, and this script could still come in handy (e.g. helped solve apache/tvm#16438, mlc-ai/mlc-llm#1638).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants