A segfault should not happen with that program even when the shapes do not match.
Most often segfaults happen when people use from_blob and the memory-owner goes out of scope, but this is not case here.

Does this also happen with a small / trivial module? If not, there might be a specific operation that it does not like. Ideally you would be able to isolate it.
If it does even for a trivial module, you could compile libtorch / PyTorch in Debug mode and run it with gdb -ex run --args myprogram to get a backtrace (with bt after the segfault).

Best regards
Thomas