MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / test_serializer_memshare

Function test_serializer_memshare

src/serialization/test/serializer_oss.cpp:828–897  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

826}
827
828void test_serializer_memshare(GraphDumpFormat format) {
829 std::vector<uint8_t> buf;
830 HostTensorGenerator<> gen;
831 constexpr size_t SIZE = 127;
832 auto xval = gen({SIZE}, "cpu0"), bval = gen({1}, "cpu0");
833
834 auto dump = [&]() {
835 auto graph = ComputingGraph::make();
836 auto x0 = opr::SharedDeviceTensor::make(*graph, *xval).rename("x0");
837 auto x1 = opr::SharedDeviceTensor::make(*graph, *xval).rename("x1");
838 auto x2 = opr::SharedDeviceTensor::make(*graph, *xval).rename("x2");
839 auto x3 = opr::SharedDeviceTensor::make(*graph, *xval).rename("x3");
840 auto i4 = opr::ImmutableTensor::make(*graph, *xval).rename("i4");
841 auto i5 = opr::ImmutableTensor::make(*graph, *xval).rename("i5");
842 auto b = opr::SharedDeviceTensor::make(*graph, *bval).rename("b");
843 auto dumper = GraphDumper::make(OutputFile::make_vector_proxy(&buf), format);
844 dumper->dump({((x0 + x1) + b) + (x2 + x3) + i4 + i5, x0, i4});
845 };
846
847 HostTensorND expected;
848 expected.copy_from(*xval);
849 for (size_t i = 0; i < SIZE; ++i) {
850 auto&& v = expected.ptr<float>()[i];
851 v = v * 6 + bval->ptr<float>()[0];
852 }
853
854 std::vector<uint8_t> buf_al;
855 auto load = [&](bool share) {
856 std::unique_ptr<InputFile> fin;
857 if (share) {
858 buf_al.resize(buf.size());
859 memcpy(buf_al.data(), buf.data(), buf.size());
860
861 fin = InputFile::make_mem_proxy(
862 std::shared_ptr<void>{std::shared_ptr<void>{}, buf_al.data()},
863 buf.size());
864 } else {
865 fin = InputFile::make_mem_proxy(buf.data(), buf.size());
866 }
867 auto loader = GraphLoader::make(std::move(fin), format);
868 auto rst = loader->load();
869 auto x = rst.output_var_map.at("x0");
870 auto i4 = rst.output_var_map.at("i4");
871 auto&& opr = x.node()->owner_opr()->cast_final_safe<opr::SharedDeviceTensor>();
872 auto&& opr_imm =
873 i4.node()->owner_opr()->cast_final_safe<opr::ImmutableTensor>();
874 HostTensorND val;
875 auto func =
876 rst.graph_compile({make_callback_copy(rst.output_var_list[0], val)});
877 func->execute();
878 return std::make_pair(
879 val, std::vector<DeviceTensorND>{*opr.dev_data(), opr_imm.value()});
880 };
881
882 auto in_range = [](const std::vector<uint8_t>& buf, DeviceTensorND& dv) {
883 auto p0 = reinterpret_cast<uint8_t*>(dv.raw_ptr()),
884 p1 = reinterpret_cast<uint8_t*>(p0 + dv.layout().span().high_byte);
885 return buf.data() <= p0 && p1 <= buf.data() + buf.size();

Callers 1

TESTFunction · 0.85

Calls 15

make_callback_copyFunction · 0.85
renameMethod · 0.80
resizeMethod · 0.80
graph_compileMethod · 0.80
spanMethod · 0.80
genFunction · 0.50
makeFunction · 0.50
loadFunction · 0.50
dumpMethod · 0.45
copy_fromMethod · 0.45
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected