prefix module qualified names with __module__ (#23630)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/23630

This is temporary, won't be needed with the new serialization format.
But for now, since the main module gets its name from the archive name,
we need this for safety, other wise something like
`torch.jit.save("torch.pt") will break things.

Test Plan: Imported from OSS

Reviewed By: jamesr66a

Differential Revision: D16592404

Pulled By: suo

fbshipit-source-id: b538dc3438a80ea7bca14d84591ecd63f4b1289f
diff --git a/aten/src/ATen/core/qualified_name.h b/aten/src/ATen/core/qualified_name.h
index e4940dc..ede213f 100644
--- a/aten/src/ATen/core/qualified_name.h
+++ b/aten/src/ATen/core/qualified_name.h
@@ -35,7 +35,7 @@
     cacheAccessors();
   }
 
-  /* implicit */ QualifiedName(const std::vector<std::string>& atoms) {
+  explicit QualifiedName(std::vector<std::string> atoms) {
     for (const auto& atom : atoms) {
       TORCH_CHECK(!atom.empty(), "Atom cannot be empty");
       TORCH_CHECK(
diff --git a/torch/csrc/jit/import.cpp b/torch/csrc/jit/import.cpp
index 4b3f098..0914202 100644
--- a/torch/csrc/jit/import.cpp
+++ b/torch/csrc/jit/import.cpp
@@ -305,7 +305,8 @@
   for (const auto& atom : atoms) {
     moduleStack_.emplace_back(atom);
   }
-  auto module = script::Module(moduleStack_, compilation_unit_);
+  auto module =
+      script::Module(c10::QualifiedName(moduleStack_), compilation_unit_);
   for (int i = 0; i < module_def.submodules_size(); ++i) {
     const torch::ModuleDef& sub_def = module_def.submodules(i);
     auto submodule = convertModule(sub_def);
diff --git a/torch/csrc/jit/script/module.cpp b/torch/csrc/jit/script/module.cpp
index 617384a..ffc5167 100644
--- a/torch/csrc/jit/script/module.cpp
+++ b/torch/csrc/jit/script/module.cpp
@@ -16,6 +16,15 @@
     c10::QualifiedName class_name,
     std::shared_ptr<CompilationUnit> cu,
     bool shouldMangle = false) {
+  // XXX: This is a temporary hack so that module names cannot clash with
+  // builtins like `torch`. Delete this with the new serialization format.
+  std::vector<std::string> new_class_name{"__module__"};
+  new_class_name.insert(
+      new_class_name.end(),
+      class_name.atoms().begin(),
+      class_name.atoms().end());
+  class_name = c10::QualifiedName(std::move(new_class_name));
+
   if (shouldMangle && cu->get_class(class_name) != nullptr) {
     class_name = cu->mangle(class_name);
   }