sounds like it is a turbo problem, if it was a cooler pipe it probably wouldnt hold idle, you have to have your foot on a bit to keep it alive.
start by openig the bonnet and pumping the accelerator and watch the throttle to see if its moving smoothly/correctly. since you mention it sounds like a rex, i assume its not firing properly and you are looking at either fouled/damaged plugs or inaccurate fueling. check all the wiring from your safc to the stock ecu harness, if you've damaged or d/c one its an interesting cause to the problem. could always remove the safc for troubleshooting anyway.
because it revs out, we can assume that its not in too bad a shape or it would have detonated. i would say the stock ecu is doing its best not to have the engine shit itself one way or another.
i would suggest check all the pipes tho, as its the easiest to fix, then im afraid you may have to dig your way in to the turbo and check it.