Autograd only works with torch.sin / torch.cos, not math.cos. But then you don’t need to go via enumeration but can just use the pointwise function on the tensor.
You probably want to differentiate w.r.t. self.m itself and then take the index (unless self.m is prohibitively large, in which case you should assign self.m[label] to a Python variable at the very top of your function and then re-use that).

This should get you a bit further.
P.S.: With triple backticks (```) before and after your code and the code as text, it is easier to copy-paste things for rearranging.