Hi, have you tried to set the internal weight Tensor to sparse directly?
If I do something like this here for example:
auto embedd = torch::nn::Embedding(3, 2);
embedd->weight = embedd->weight.to_sparse();

And then print embedd->weight before and after, it shows the following:
BEFORE:
0.1794  1.1953
1.6296  0.2926
3.0924 -1.9228
[ Variable[CPUFloatType]{3,2} ]

AFTER:
[ Variable[SparseCPUFloatType]{}
indices:
 0  0  1  1  2  2
 0  1  0  1  0  1
[ Variable[CPULongType]{2,6} ]
values:
 0.1794
 1.1953
 1.6296
 0.2926
 3.0924
-1.9228
[ Variable[CPUFloatType]{6} ]
size:
[3, 2]
]

with kind regards, Florian Korotschenko