I'm fairly new to this, but if Retroarch is running the mame_libretro core, can you press TAB from within the NBA game, bring up the Mame menu, and check the "INPUTS FOR THIS MACHINE" mappings, to make sure they aren't overwriting the higher level ones?
If that's not the problem, then the random remapping sounds similar to an issue I had with multiple controllers using Retroarch and Windows 10. Although it would affect every game, not just the NBA one.
Windows 10 likes to swap joystick configurations over randomly when you boot up (if you Google "stable controller IDs" you'll find out about it). So even though you haven't unplugged anything, joystick 1 becomes joystick 4, and no longer works, so you can't navigate Retroarch menus etc. It was a real pain. When I tried to set up Mame mappings from within Retroarch, it wouldn't even see the joystick half the time.
If it is this with your cabinet, then it's fixable without too much hassle. But not using Retroarch, as far as I know.
In the end I used Mame itself, which allows you to map each joystick in a way that preserves the mappings even when windows 10 decides to randomly swap them all around.
This guy shows you how to do it:
https://www.youtube.com/watch?v=YmjfwLuZ_X0
Since I did that, I've had no problems at all.
There are other solutions, but they rely on the USB encoders all having different hardware IDs and these cheap Chinese encoders all have identical IDs, so it didn't work for me. But using Mame without retroarch and following the steps in that video fixed the problem. If you search for "Mame nonag" you'll find a version that doesn't pop up anything when you load a rom. I got one from this forum. And the shaders are decent enough that I don't miss the Retroarch ones.
So maybe first step is to check to see if your joysticks are being swapped over? If they are, you should be able to use the retroarch menu using whichever one of the four has taken the place of joystick 1. If so, you can go into Inputs (port bindings I think) and swap them over so Port 1 becomes joystick X - but you don't want to have to do that every time.