Skip to content

Conversation

@MkDev11
Copy link
Contributor

@MkDev11 MkDev11 commented Dec 8, 2025

Summary

Resolves #2149

Problem

The codebase has two similar exponentiation functions in math.rs:

  1. exp_safe (line 172): Robust implementation with:

    • Input clamping to [-20, 20] range
    • Smart error handling (returns max_value for positive overflow, 0 for negative)
    • Existing test coverage in tests/math.rs
  2. safe_exp (line 1597): Simple wrapper that:

    • Has no input clamping
    • Always returns 0 on any error

Solution

  • Remove the duplicate safe_exp function from math.rs
  • Update run_epoch.rs to use exp_safe instead

Why exp_safe is better

  1. Clamps input to prevent overflow in the first place
  2. Returns max_value (not 0) when large positive inputs would overflow - more mathematically correct
  3. Has existing test coverage

Testing

  • cargo check -p pallet-subtensor passes
  • Existing exp_safe tests in tests/math.rs provide coverage

Contribution by Gittensor, learn more at https://gittensor.io/

@MkDev11
Copy link
Contributor Author

MkDev11 commented Dec 15, 2025

@gztensor @sam0x17 Hope you had great weekends. Can you please check the PR and let me know the results?

@sam0x17 sam0x17 added the skip-cargo-audit This PR fails cargo audit but needs to be merged anyway label Dec 16, 2025
@gztensor
Copy link
Contributor

@MkDev11 , thank you for your contribution. While the functions are not exactly the same, the argument range of exp_safe in alpha_sigmoid is from -5.0 to +5.0, so the safe_exp -20..+20 range fits the (only) use case. Approving.

gztensor
gztensor previously approved these changes Dec 16, 2025
@MkDev11
Copy link
Contributor Author

MkDev11 commented Dec 17, 2025

@sam0x17 Please check the PR when you are available.

@sam0x17
Copy link
Contributor

sam0x17 commented Dec 17, 2025

CI failing

Resolves opentensor#2149

The codebase had two similar exponentiation functions:
- exp_safe (line 172): Robust implementation with input clamping [-20, 20]
  and smart error handling (returns max_value for positive overflow)
- safe_exp (line 1597): Simple wrapper returning 0 on any error

This commit:
- Removes the duplicate safe_exp function from math.rs
- Updates run_epoch.rs to use exp_safe instead

exp_safe is the better choice as it:
1. Clamps input to prevent overflow in the first place
2. Returns max_value (not 0) when large positive inputs overflow
3. Has existing test coverage in tests/math.rs
@MkDev11 MkDev11 force-pushed the fix/remove-duplicate-exp-functions branch from f5033de to daf6ad8 Compare December 17, 2025 16:24
@MkDev11 MkDev11 changed the base branch from main to devnet-ready December 17, 2025 16:26
@MkDev11 MkDev11 dismissed gztensor’s stale review December 17, 2025 16:26

The base branch was changed.

@MkDev11
Copy link
Contributor Author

MkDev11 commented Dec 17, 2025

@sam0x17 Can you check again?

@MkDev11
Copy link
Contributor Author

MkDev11 commented Dec 17, 2025

@gztensor Can you approve the PR again? I changed the base branch from main to devnet-ready

@MkDev11 MkDev11 requested a review from gztensor December 18, 2025 02:06
@sam0x17 sam0x17 merged commit f7475e3 into opentensor:devnet-ready Dec 18, 2025
162 checks passed
This was referenced Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-cargo-audit This PR fails cargo audit but needs to be merged anyway

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate exp functions

3 participants