| 863 | } |
| 864 | |
| 865 | GraphLoader::LoadResult GraphLoaderOSS::load(const LoadConfig& config, bool rewind) { |
| 866 | mgb_assert(m_file); |
| 867 | m_cur_load_config = &config; |
| 868 | if (rewind) { |
| 869 | m_file->rewind(); |
| 870 | } |
| 871 | uint32_t magic; |
| 872 | m_file->read(&magic, sizeof(magic)); |
| 873 | mgb_throw_if( |
| 874 | (magic != MGB_MAGIC) && (magic != MAGIC_V0), SerializationError, |
| 875 | "wrong magic: wanted %#08x or %#08x, actual %#08x (not a invalid fbs " |
| 876 | "model?)", |
| 877 | MGB_MAGIC, MAGIC_V0, magic); |
| 878 | if (magic == MGB_MAGIC) { |
| 879 | // read FeatureBits |
| 880 | magic_compare = true; |
| 881 | m_file->read(&m_feature_bits, 8); |
| 882 | } else { |
| 883 | magic_compare = false; |
| 884 | } |
| 885 | m_file->skip(4); |
| 886 | |
| 887 | uint64_t offset_to_fbs; |
| 888 | m_file->read(&offset_to_fbs, sizeof(offset_to_fbs)); |
| 889 | auto tensor_begin = m_file->tell(); |
| 890 | // Skip tensor data |
| 891 | m_file->skip(offset_to_fbs); |
| 892 | |
| 893 | // Read fbs::Graph |
| 894 | uint32_t size; |
| 895 | m_file->read(&size, sizeof(size)); |
| 896 | m_file->skip(-sizeof(size)); |
| 897 | m_graph_buf = m_file->read_shared(size + sizeof(size)); |
| 898 | |
| 899 | // Rewind back to tensor data |
| 900 | m_file->rewind(); |
| 901 | m_file->skip(tensor_begin); |
| 902 | |
| 903 | { |
| 904 | flatbuffers::Verifier verifier( |
| 905 | static_cast<const uint8_t*>(m_graph_buf.data()), m_graph_buf.size()); |
| 906 | mgb_throw_if( |
| 907 | !fbs::VerifySizePrefixedGraphBuffer(verifier), SerializationError, |
| 908 | "model verification failed (invalid or corrupted model?)"); |
| 909 | } |
| 910 | |
| 911 | m_graph = fbs::GetSizePrefixedGraph(m_graph_buf.data()); |
| 912 | m_mgb_version = m_graph->mgb_version(); |
| 913 | if (m_graph->mgb_version() > MGB_VERSION) { |
| 914 | mgb_log_warn( |
| 915 | "loading model from future runtime: version=%u " |
| 916 | "model_version=%u", |
| 917 | MGB_VERSION, m_graph->mgb_version()); |
| 918 | } |
| 919 | if (!m_graph_hash) { |
| 920 | m_graph_hash = m_graph->hash(); |
| 921 | mgb_assert( |
| 922 | m_graph_hash, |
nothing calls this directly
no test coverage detected