Yeah, I think these accumulators don’t consider the order of operations, thus they may sometimes not reflect the peak value, for big code blocks.
Perhaps the following will do it (haven’t tested it):
cuda.reset_max_memory_allocated()
m0 = cuda.memory_allocated()
f()
peak = cuda.max_memory_allocated() - m0