Usually you would set the length in WeightedRandomSampler to the length of your dataset. However, this is not a requirement and you could also use replacement=True and double the length, if that fits your use case.
Your code looks alright. Basically each sample should get its own weight. The usual approach would be to use the corresponding class index to assign the weight to the sample. Could you please post the posts with varying implementations, as we should fix them?
The weight argument in nn.CrossEntropyLoss is using a class weight on the loss and is orthogonal to weighted sampling. In the linked post, the user wanted a sample based loss weighting in its training, which is also a special case and unrelated to random sampling. (Although all approaches might be used to counter effects of an imbalanced dataset)