Skip to content

[Metal] Dispatch numerically stable tanh for metal - #16438

Merged
junrushao merged 2 commits into
apache:mainfrom
CharlieFRuan:pr-0119-metal-tanh
Jan 20, 2024
Merged

junrushao merged 2 commits into
apache:mainfrom
CharlieFRuan:pr-0119-metal-tanh

Conversation

@CharlieFRuan

Copy link
Copy Markdown
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
Copy Markdown
Member Author

cc @MasterJH5574 @tqchen

@CharlieFRuan

Copy link
Copy Markdown
Member Author

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

@MasterJH5574 MasterJH5574 self-assigned this Jan 20, 2024

@junrushao junrushao left a comment

Copy link
Copy Markdown
Member

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