2019-05-29 17:05:23 +00:00
|
|
|
#include <test/cpp/jit/test_base.h>
|
|
|
|
|
#include <test/cpp/jit/test_utils.h>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2020-02-27 20:18:24 +00:00
|
|
|
#include <torch/csrc/jit/serialization/export.h>
|
|
|
|
|
#include <torch/csrc/jit/serialization/import.h>
|
|
|
|
|
#include <torch/csrc/jit/serialization/import_source.h>
|
2019-05-29 17:05:23 +00:00
|
|
|
#include <torch/torch.h>
|
|
|
|
|
|
2020-06-24 19:39:42 +00:00
|
|
|
#include "caffe2/serialize/istream_adapter.h"
|
|
|
|
|
|
2019-05-29 17:05:23 +00:00
|
|
|
namespace torch {
|
|
|
|
|
namespace jit {
|
|
|
|
|
|
2020-06-24 19:39:42 +00:00
|
|
|
// Tests that an extra file written explicitly has precedence over
|
|
|
|
|
// extra files written by a hook
|
|
|
|
|
// TODO: test for the warning, too
|
|
|
|
|
void testExtraFilesHookPreference() {
|
|
|
|
|
const auto script = R"JIT(
|
|
|
|
|
def forward(self):
|
|
|
|
|
x = torch.rand(5, 5)
|
|
|
|
|
x = x.mm(x)
|
|
|
|
|
return x
|
|
|
|
|
)JIT";
|
|
|
|
|
|
|
|
|
|
auto module =
|
|
|
|
|
std::make_shared<Module>("Module", std::make_shared<CompilationUnit>());
|
|
|
|
|
module->define(script);
|
|
|
|
|
std::ostringstream oss;
|
|
|
|
|
std::unordered_map<std::string, std::string> extra_files;
|
|
|
|
|
extra_files["metadata.json"] = "abc";
|
|
|
|
|
SetExportModuleExtraFilesHook([](const Module&) -> ExtraFilesMap {
|
|
|
|
|
return {{"metadata.json", "def"}};
|
|
|
|
|
});
|
|
|
|
|
module->save(oss, extra_files);
|
|
|
|
|
SetExportModuleExtraFilesHook(nullptr);
|
|
|
|
|
|
|
|
|
|
std::istringstream iss(oss.str());
|
|
|
|
|
caffe2::serialize::IStreamAdapter adapter{&iss};
|
|
|
|
|
std::unordered_map<std::string, std::string> loaded_extra_files;
|
|
|
|
|
loaded_extra_files["metadata.json"] = "";
|
|
|
|
|
auto loaded_module = torch::jit::load(iss, torch::kCPU, loaded_extra_files);
|
|
|
|
|
ASSERT_EQ(loaded_extra_files["metadata.json"], "abc");
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 17:05:23 +00:00
|
|
|
void testSaveExtraFilesHook() {
|
|
|
|
|
// no secrets
|
|
|
|
|
{
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
{
|
2019-08-11 22:43:28 +00:00
|
|
|
Module m("__torch__.m");
|
2019-05-29 17:05:23 +00:00
|
|
|
ExtraFilesMap extra;
|
|
|
|
|
extra["metadata.json"] = "abc";
|
|
|
|
|
m.save(ss, extra);
|
|
|
|
|
}
|
|
|
|
|
ss.seekg(0);
|
|
|
|
|
{
|
|
|
|
|
ExtraFilesMap extra;
|
|
|
|
|
extra["metadata.json"] = "";
|
|
|
|
|
extra["secret.json"] = "";
|
|
|
|
|
jit::load(ss, c10::nullopt, extra);
|
|
|
|
|
ASSERT_EQ(extra["metadata.json"], "abc");
|
|
|
|
|
ASSERT_EQ(extra["secret.json"], "");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// some secret
|
|
|
|
|
{
|
|
|
|
|
std::stringstream ss;
|
|
|
|
|
{
|
|
|
|
|
SetExportModuleExtraFilesHook([](const Module&) -> ExtraFilesMap {
|
|
|
|
|
return {{"secret.json", "topsecret"}};
|
|
|
|
|
});
|
2019-08-11 22:43:28 +00:00
|
|
|
Module m("__torch__.m");
|
2019-05-29 17:05:23 +00:00
|
|
|
ExtraFilesMap extra;
|
|
|
|
|
extra["metadata.json"] = "abc";
|
|
|
|
|
m.save(ss, extra);
|
|
|
|
|
SetExportModuleExtraFilesHook(nullptr);
|
|
|
|
|
}
|
|
|
|
|
ss.seekg(0);
|
|
|
|
|
{
|
|
|
|
|
ExtraFilesMap extra;
|
|
|
|
|
extra["metadata.json"] = "";
|
|
|
|
|
extra["secret.json"] = "";
|
|
|
|
|
jit::load(ss, c10::nullopt, extra);
|
|
|
|
|
ASSERT_EQ(extra["metadata.json"], "abc");
|
|
|
|
|
ASSERT_EQ(extra["secret.json"], "topsecret");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 01:29:01 +00:00
|
|
|
void testTypeTags() {
|
2020-04-03 21:32:02 +00:00
|
|
|
auto list = c10::List<c10::List<int64_t>>();
|
|
|
|
|
list.push_back(c10::List<int64_t>({1, 2, 3}));
|
|
|
|
|
list.push_back(c10::List<int64_t>({4, 5, 6}));
|
|
|
|
|
auto dict = c10::Dict<std::string, at::Tensor>();
|
|
|
|
|
dict.insert("Hello", torch::ones({2, 2}));
|
|
|
|
|
auto dict_list = c10::List<c10::Dict<std::string, at::Tensor>>();
|
|
|
|
|
for (size_t i = 0; i < 5; i++) {
|
|
|
|
|
auto another_dict = c10::Dict<std::string, at::Tensor>();
|
|
|
|
|
another_dict.insert("Hello" + std::to_string(i), torch::ones({2, 2}));
|
|
|
|
|
dict_list.push_back(another_dict);
|
|
|
|
|
}
|
|
|
|
|
auto tuple = std::tuple<int, std::string>(2, "hi");
|
|
|
|
|
struct TestItem {
|
|
|
|
|
IValue value;
|
|
|
|
|
TypePtr expected_type;
|
|
|
|
|
};
|
|
|
|
|
std::vector<TestItem> items = {
|
2020-04-03 18:55:59 +00:00
|
|
|
{list, ListType::create(ListType::create(IntType::get()))},
|
|
|
|
|
{2, IntType::get()},
|
|
|
|
|
{dict, DictType::create(StringType::get(), TensorType::get())},
|
2020-04-03 21:32:02 +00:00
|
|
|
{dict_list,
|
|
|
|
|
ListType::create(
|
|
|
|
|
DictType::create(StringType::get(), TensorType::get()))},
|
|
|
|
|
{tuple, TupleType::create({IntType::get(), StringType::get()})}};
|
|
|
|
|
for (auto item : items) {
|
|
|
|
|
auto bytes = torch::pickle_save(item.value);
|
|
|
|
|
auto loaded = torch::pickle_load(bytes);
|
|
|
|
|
ASSERT_TRUE(loaded.type()->isSubtypeOf(item.expected_type));
|
|
|
|
|
ASSERT_TRUE(item.expected_type->isSubtypeOf(loaded.type()));
|
|
|
|
|
}
|
2020-03-11 01:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-29 17:05:23 +00:00
|
|
|
} // namespace jit
|
|
|
|
|
} // namespace torch
|