export-scope.cpp 912 字节
Newer Older
D
dolphin8 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#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);
          const float * p = v->Get<float>();
          int count = 1;
          for (auto n: var->Tensor_desc().Dims()) {
            count *= n;
          }
          std::string para_file_name = dirname + '/' + var->Name();
          FILE *para_file = fopen(para_file_name.c_str(), "w");
          assert(p != nullptr);
          // std::cout << var->Name() << " " << count << "\n";
          fwrite(p, sizeof(float), count, para_file);
          fclose(para_file);
        }
    }
  }
}