Well if you run f in no_grad mode, then your loss won’t require gradients and you won’t be able to run backprop.
If you want to learn g properly, you will need to run the k and f applied after it in a differentiable manner to be able to get gradient.