tch-rs icon indicating copy to clipboard operation
tch-rs copied to clipboard

Enable the function linalg_vector_norm

Open ramon-garcia opened this issue 1 year ago • 4 comments

This pull request is to enable the function linalg_vector_norm, disable in the original code.

I need it to compute the L2 norm of matrices and vectors.

It looks like it was disabled because of compile errors, caused by the removal of the parameter ord in the code generator, because it had a default value. After removing the default_value attribute in Declarations-v2.0.0.yaml, the code is correctly generated.

Of course, there are better solutions, but I wasn't able to make changes in the Ocaml code in gen/gen.ml.

Thank you for your time.

ramon-garcia avatar Jul 16 '23 19:07 ramon-garcia

ping

ramon-garcia avatar Aug 02 '23 11:08 ramon-garcia

Sorry for the delayed answer, making changes to the Definitions.yaml file would be a bit tricky to maintain when upgrading to new torch versions. If your goal is to compute the l2 norm, maybe you can use mse_loss or compute it directly via something like x.pow_tensor_scalar(2).sum(Kind::float).sqrt()?

LaurentMazare avatar Aug 02 '23 13:08 LaurentMazare

Thanks, I overlooked the fact that the files Declarations*.yaml are autogenerated. I am going to review the patch to change the code generator instead.

Note that in addition of the particular case of generating the norm, one needs the general case of having access to all the Torch API. So that one looks the documentation and finds the solution. Your solution would work for me, thank you.

ramon-garcia avatar Aug 02 '23 17:08 ramon-garcia

I would still encourage you to try the manual computations from my previous post if you can, it's certainly easier to get working and should cover all needs of norm computations that I can think of.

LaurentMazare avatar Aug 02 '23 18:08 LaurentMazare