export-scope.cpp 1.1 KB
Newer Older
D
dolphin8 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <cstdio>
#include "export.h"

void export_scope(ProgramPtr program, ScopePtr scope, const std::string & dirname) {
  for (const auto& block: program->Blocks()) {
    for (const auto& var: block->Vars()) {
        if (var->Name() == "feed" || var->Name() == "fetch") {
          continue;
        }
        if (var->Persistable()) {
          auto* v = scope->FindVar(var->Name());
          assert(v != nullptr);
          int count = 1;
          for (auto n: var->Tensor_desc().Dims()) {
            count *= n;
          }
D
dolphin8 已提交
17 18 19 20

          auto* tensor = v->GetMutable<paddle_mobile::framework::LoDTensor>();
          const float * p = tensor->mutable_data<float>();

D
dolphin8 已提交
21 22 23 24 25
          std::string para_file_name = dirname + '/' + var->Name();
          FILE *para_file = fopen(para_file_name.c_str(), "w");
          assert(p != nullptr);
          fwrite(p, sizeof(float), count, para_file);
          fclose(para_file);
D
dolphin8 已提交
26 27 28 29 30
          // std::cout << "==> " << var->Name() << " " << count << "\n";
          // for (int i = 0; i < count; i++) {
          //     std::cout << p[i] << ", ";
          // }
          // std::cout << "\n";
D
dolphin8 已提交
31 32 33 34
        }
    }
  }
}