module_util.cc 1.61 KB
Newer Older
1 2 3 4 5
/*!
 *  Copyright (c) 2017 by Contributors
 * \file module_util.cc
 * \brief Utilities for module.
 */
nhynes committed
6
#ifndef _LIBCPP_SGX_CONFIG
7
#include <dmlc/memory_io.h>
nhynes committed
8
#endif
9 10 11 12 13 14 15 16
#include <tvm/runtime/module.h>
#include <tvm/runtime/registry.h>
#include "./module_util.h"

namespace tvm {
namespace runtime {

void ImportModuleBlob(const char* mblob, std::vector<Module>* mlist) {
nhynes committed
17
#ifndef _LIBCPP_SGX_CONFIG
18 19 20 21 22 23 24
  CHECK(mblob != nullptr);
  uint64_t nbytes = 0;
  for (size_t i = 0; i < sizeof(nbytes); ++i) {
    uint64_t c = mblob[i];
    nbytes |=  (c & 0xffUL) << (i * 8);
  }
  dmlc::MemoryFixedSizeStream fs(
Tianqi Chen committed
25
      const_cast<char*>(mblob + sizeof(nbytes)), static_cast<size_t>(nbytes));
26 27 28 29 30 31 32 33 34 35 36 37 38 39
  dmlc::Stream* stream = &fs;
  uint64_t size;
  CHECK(stream->Read(&size));
  for (uint64_t i = 0; i < size; ++i) {
    std::string tkey;
    CHECK(stream->Read(&tkey));
    std::string fkey = "module.loadbinary_" + tkey;
    const PackedFunc* f = Registry::Get(fkey);
    CHECK(f != nullptr)
        << "Loader of " << tkey << "("
        << fkey << ") is not presented.";
    Module m = (*f)(static_cast<void*>(stream));
    mlist->push_back(m);
  }
nhynes committed
40 41 42
#else
  LOG(FATAL) << "SGX does not support ImportModuleBlob";
#endif
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
}

PackedFunc WrapPackedFunc(BackendPackedCFunc faddr,
                          const std::shared_ptr<ModuleNode>& sptr_to_self) {
  return PackedFunc([faddr, sptr_to_self](TVMArgs args, TVMRetValue* rv) {
      int ret = (*faddr)(
          const_cast<TVMValue*>(args.values),
          const_cast<int*>(args.type_codes),
          args.num_args);
      CHECK_EQ(ret, 0) << TVMGetLastError();
    });
}

}  // namespace runtime
}  // namespace tvm