Use `isinstance(foreach_arg.type, ListType)` for correctness (#106428)
the intention here is to check the element type of `List`, not all the types with `elem` attribute such as optional.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/106428
Approved by: https://github.com/soulitzer