Skip to content

Add Problem #41: Tanh & Soft-Capping#10

Open
ThierryHJ wants to merge 1 commit intoduoan:masterfrom
ThierryHJ:feature/add-tanh
Open

Add Problem #41: Tanh & Soft-Capping#10
ThierryHJ wants to merge 1 commit intoduoan:masterfrom
ThierryHJ:feature/add-tanh

Conversation

@ThierryHJ
Copy link
Copy Markdown
Contributor

Summary

Adds a new Easy-difficulty problem where users implement tanh activation using three parts:

  • Forward (my_tanh) β€” compute tanh from scratch using exp, numerically stable via 2/(1+exp(-2x))-1
  • Manual Backward (tanh_backward) β€” compute gradient using 1 - tanhΒ²(x), note it depends on the output not the input
  • Soft-Capping (soft_cap_logits) β€” cap Β· tanh(logits/cap), a technique used in Gemma 2 and Griffin to bound logits

Motivation

  • Tanh is a classic activation function commonly asked in ML interviews
  • Implementing it from scratch tests understanding of exponentials, numerical stability, and the chain rule
  • Logit soft-capping is a modern technique (Gemma 2, Griffin) that applies tanh in a practical setting β€” good bonus context for interviews
  • Complements existing activation exercises: ReLU (KV cache MHA inferenceΒ #1) and GELU (#19)

Files Changed

File Status Description
torch_judge/tasks/tanh.py NEW Task definition with 6 test cases
templates/41_tanh.ipynb NEW Template notebook (blank stubs for 3 functions)
solutions/41_tanh_solution.ipynb NEW Reference solution (numerically stable)
README.md Modified Added row #41 to Fundamentals table, updated badge 40β†’41

Test Cases

# Test Name What It Verifies
1 Matches torch.tanh Output matches reference with atol=1e-5
2 tanh(0) = 0 and bounded output Boundary behavior, output in (-1, 1) for Β±100
3 Shape preservation Multi-dim tensor shape unchanged
4 Gradient flow backward() succeeds, grad shape correct
5 Manual backward (tanh_backward) Manual grad matches autograd
6 Soft-capping bounds logits Output bounded within (-cap, cap), matches reference

Conventions Followed

  • Auto-discovery via _registry.py β€” no manual registration needed
  • Standard notebook format (markdown β†’ import β†’ implementation β†’ debug β†’ submit)
  • Colab badge on both template and solution notebooks
  • {fn} placeholder in all test code
  • torch.allclose() for numerical comparison with appropriate tolerances
  • Difficulty emoji consistent with existing Easy problems (🟒)

How to Test

# From repo root
python3 -c "
from torch_judge.tasks._registry import TASKS
print('tanh' in TASKS)  # True
"

Open solutions/41_tanh_solution.ipynb in JupyterLab and run all cells β€” the final check('tanh') should show 6/6 tests passed.

Add Problem #41 covering tanh forward, manual backward, and logit
soft-capping (used in Gemma 2, Griffin). Includes numerically stable
implementation using 2/(1+exp(-2x))-1 to handle large inputs.

- templates/41_tanh.ipynb: template with 3 function stubs
- solutions/41_tanh_solution.ipynb: working solution (stable formula)
- torch_judge/tasks/tanh.py: 6 judge tests (accuracy, bounds, shape,
  gradient, manual backward, soft-capping)
- README.md: add row to Fundamentals table, bump badge 40 β†’ 41

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

1 participant