Thank you for your reply. I have tried that before and it made no change. And after I add a line in my backward function, it seems to solve this problem.
def backward(ctx, dw):
    dw=dw.contiguous()
    # backward c++ extension