MCPcopy Create free account
hub / github.com/Tencent/embedx / LoadPretrainParam

Method LoadPretrainParam

src/tools/model_util.cc:25–96  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23namespace embedx {
24
25bool ModelUtil::LoadPretrainParam(const std::string& param_path,
26 const deepx_core::Shard& shard,
27 deepx_core::ModelShard* model_shard) {
28 if (!io_util::ListFile(param_path, &files_)) {
29 DXERROR("Failed to init files from param_path: %s.", param_path.c_str());
30 return false;
31 }
32 DXINFO("Got %d files.", (int)files_.size());
33
34 for (const std::string& file : files_) {
35 if (!ifs_.Open(file)) {
36 DXERROR("Failed to open file: %s.", file.c_str());
37 return false;
38 }
39
40 while (GetLine(ifs_, line_)) {
41 iss_.clear();
42 iss_.str(line_);
43 if (!(iss_ >> tensor_name_ >> tensor_type_)) {
44 DXERROR("Invalid line: %s", line_.c_str());
45 return false;
46 }
47 const auto* node = graph_.find_node(tensor_name_);
48 if (node == nullptr) {
49 DXERROR("Tensor: %s doesn't exist.", tensor_name_.c_str());
50 return false;
51 }
52 if (node->tensor_type() != tensor_type_) {
53 DXERROR("Expected tensor type: %d, got: %d.", node->tensor_type(),
54 tensor_type_);
55 return false;
56 }
57
58 if (node->tensor_type() == deepx_core::TENSOR_TYPE_SRM) {
59 if (!(iss_ >> feature_id_ >> str_vals_)) {
60 DXERROR("Invalid line: %s.", line_.c_str());
61 return false;
62 }
63 if (shard.HasSRM(model_shard->shard_id(), feature_id_)) {
64 if (LoadSRMRow(model_shard, str_vals_)) {
65 valid_tensor_.emplace(tensor_name_);
66 } else {
67 return false;
68 }
69 }
70
71 } else if (node->tensor_type() == deepx_core::TENSOR_TYPE_TSR) {
72 if (!(iss_ >> str_vals_)) {
73 DXERROR("Invalid line: %s.", line_.c_str());
74 return false;
75 }
76
77 if (shard.HasTSR(model_shard->shard_id(), tensor_name_)) {
78 if (LoadTSR(model_shard, str_vals_)) {
79 valid_tensor_.emplace(tensor_name_);
80 } else {
81 return false;
82 }

Callers 1

InitMethod · 0.80

Calls 5

ListFileFunction · 0.85
shard_idMethod · 0.80
emplaceMethod · 0.80
OpenMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected