This is one way to share pre-trained weights from Python to C++:

Convert Python model to TorchScript, and save the model to file using model.save(filename).
Create a C++ frontend model with the exact same model structure.
Load the parameters and buffers from TorchScript model to C++ frontend model using torch::load(cpp_module, file_path_to_torchscript_model)


This is a minimal example:
JIT model:
import torch
class Model(torch.jit.ScriptModule):
    def __init__(self):
        super(Model, self).__init__()
        self.linear = torch.nn.Linear(2, 2)

model = Model()
print(model.linear.weight)  # For comparison
model.save('model1.pt')

C++ frontend model:
struct Net : torch::nn::Module {
  Net() {
    linear = register_module("linear", torch::nn::Linear(2, 2));  // Exactly match the Python model
  }
  torch::nn::Linear linear{nullptr};
};

Load parameters of JIT model into C++ frontend model:
int main(int argc, const char* argv[]) {
    auto module = std::make_shared<Net>();
    std::cout << module->linear->named_parameters()["weight"] << "\n";  // before loading pre-trained params
    torch::load(module, argv[1]);
    std::cout << module->linear->named_parameters()["weight"] << "\n";  // after loading pre-trained params

    return 0;
}

For converting your existing Python model to TorchScript (i.e. step 1), you can also use tracing (https://pytorch.org/tutorials/advanced/cpp_export.html#converting-to-torch-script-via-tracing 18). However you still need to manually define the same model structure in C++ API before loading the parameters in C++.