I’m not sure to understand the use case correctly.
Based on the code it seems you are creating the entire W tensor as well as the additional index tensor B and are also holding U in memory. I would guess you might save some memory before creating B and W, but would use more memory once the weight is created or where would the memory saving be coming from?