I used to work in shelter/rescue. Lot of pit bulls with issues. Your anecdote here reminds me of a tuxedo pit I was once tasked with rehabbing for a collar sensitivity: he would get happy when he saw leashes come out, but as soon as you try to clip the lead onto his collar, he'd snap defensively and try to bite, even if he was just wagging with excitement at the prospect of a walk the moment before. Similar to how yours is happy he's being masturbated or invited to mount, but then flips 180 to bite aggression when some other unknown provocation is happening.
Do you want him to mount and hump, or are you trying to discourage that behavior? If you're here at all, I'm going to assume you're on the encouragement side, that you want humping/mounting without biting. Normally, trading food treats and positive reinforcement for sexual behavior would be a slippery slope on this forum, against the rules. But since you're trying to correct a legitimate behavior problem that only manifests on that stimulus, introducing some basic operant conditioning training is warranted here, imo.
Standard desensitization, redirection techniques. Start introducing whatever you're using for obedience treats as a way to disarm him and associate reward with you diving under him. And if you haven't already started basic sit / stay/ lie down commands, you're going to need to backup a step and begin there first instead -- train the trainer, train the dog to know what a session is, then and only then do you start working on the problem behavior. Once you've got some basic obedience into him, now you're ready to revisit the problem behavior. Set up your desensitization training like any other session: closed room, 1:1 focus with no distractions, just you and him. Short sessions, 5-7 minutes a day, that end on positive notes even if they don't do well in the beginning, same as you'd teach commands. You begin by repeating whatever motion gets him reacting and biting but hold a treat to his nose this time while you're doing it. The idea is that the food distraction should stop him biting while allowing you to (slowly, gradually) progress more and more into doing the thing that causes this biting.
For example, in the case of my collar-grab sensitive dog, in the beginning it was using the treat just to be able to get my hand near his collar without a bite. I didn't try to touch the collar, just put my hand behind his ears toward the neck while using the treat as a focus. Give treat when no bite reaction is present. Repeat this several times in the session, then end the session. Do this a few days in a row. Gradually begin increasing the provocation over the coming days, while offering the treat but not allowing him to actually eat it til you can get a few repetitions in without a bite reaction. When enough day's sessions have passed with good results, go a step further. Again using mine as an example, by the second week I was able to actually get fingers into the collar, tug on the collar, even pull the dog toward me by the collar -- all with my arm right next to his mouth, and all without any more bites! None of that was possible before they brought him to me. So when we reached the point of being able to tug him by the collar, I considered his desensitization finished successfully. You should be able to modify this technique to desensitize your guy from trying to bite when you go underneath him.
Hope that helps.