Is there a way to do that in a BDSM relationship context - that would actually stand up in a judicial setting? In other words, anyone can sign any "BDSM contract" (probably the only thematically accurate BDSM related topic in the otherwise deplorable "50 shades" series); but did any such contract ever stand up in court when one of the partner was charged with perpetrating violence on another?
Not really. At best, it can discourage law enforcement or prosecutors from exercising their discretion to bring criminal charges, even though they could be brought.
Consent Toto the Conduct In Question Can't Be Given Irrevocablyconduct in question can't be given irrevocably
You can't irrevocably provide consent of this kind in a contract. Historically, you could do that through marriage, but marriage is no longer a defense to sexual assault claims, or as was the case even earlier, against almost all criminal charges involving one spouse against another.
A contract like that would provide one piece of evidence that a jury would consider in addition to testimony, possible audio or video recordings, statements made after episodes of BSDM activity, or records/testimony concerning times that "safe words" were invoked or honored.
It is particularly relevant because it goes to the mens rea (i.e. intent) of the person who is at risk of being a criminal defendant. If that person sincerely and reasonably believes that consent is present, even if it is not, that person hasn't committed a crime in many cases. Still, subsequent events can cast doubt on the reasonableness of continuing to rely on consent given in a contract in advance.
Testimony from an alleged victim under oath that something was consensual is very helpful, but even then, is not conclusive. The jury could conclude that the alleged victim is still under hidden duress of some kind.
Moreover, it is essentially impossible to protect oneself reliably from betrayal in which an episode was actually consensual, but the alleged victim falsely testifies otherwise in court.
This shouldn't be terribly surprising.
BDSM, by its very nature, simulates activity that is otherwise undertaken only in a non-consensual context, that when it is non-consensual would constitute serious crimes. The thin line between consensual BDSM and non-consensual abuse is to some extent the source of the thrill that is the whole point of BDSM for many people who engage in it.
Consent Isn't Always A Defenseisn't always a defense
Another risk is that not all forms of conduct, which is within the scope of what is called BDSM, is conduct for which consent may lawfully be given.
Generally speaking, competent adults can consent to sex (outside select special relationships like parent and child, or guard and prisoner, where consent isn't a defense).
Likewise, one usually isn't capable of "false imprisonment" if the person constrained consents to being bound or confined, unless it is in circumstances where consent can't be promptly withdrawn, or in circumstances where there is an obvious risk of harm.
But, it is not in general, in all U.S. jurisdictions (or even in most U.S. jurisdictions) permissible to consent to another person doing bodily harm to you outside of select safe harbor situations. There are safe harbor cases for certain contact sports, medical treatment, and medical research. But morethere is no similar safe harbor for BSDM conduct. More than de minimis infliction of physical pain or physical injury is often something for which the consent of the victim is not a defense at all.