Skip to content

Commit 1919027

Browse files
mohanchenabacus_fixer
andauthored
Update the output of OFDFT, particularly for the ML part (#7383)
* update ML KEDF output * Refactor OFDFT ML KEDF logging output to use ofs_running stream Summary of changes: 1. Modified ML_Base::set_device() to accept std::ostream& ofs_running parameter instead of using std::cout directly 2. Updated KEDF_ML::set_para() to pass ofs_running through the call chain 3. Modified KEDF_ML::init_data() to accept ofs_running parameter 4. Updated NN_OFImpl constructor to accept ofs_running parameter for logging nnode/nlayer 5. Modified Cal_MLKEDF_Descriptors::set_para() to accept ofs_running parameter for logging nkernel 6. Updated ML_EXX class methods (set_para, init_data, localTest) to use ofs_running 7. Updated all call sites to pass GlobalV::ofs_running 8. Changed 'NN' to 'Neural Network' in device initialization messages 9. Fixed 'WARNING: ML >= TF' message in KEDF_Manager::get_energy() to use ofs_running 10. Reformatted KEDF_ML::set_para() and cal_tool->set_para() calls with one parameter per line All ML KEDF related output messages now write to the running log file instead of stdout. * fix * fix * update the output formats * update KEDF * output format update * update * fix a potential bug when the net.pt model cannot be found * update kedf and exx * update --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 9c8539f commit 1919027

21 files changed

Lines changed: 648 additions & 249 deletions

File tree

source/source_estate/module_pot/pot_ml_exx.cpp

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ ML_EXX::ML_EXX()
1717

1818
ML_EXX::~ML_EXX(){}
1919

20-
void ML_EXX::set_para(const Input_para& inp, const UnitCell* ucell_in, const ModulePW::PW_Basis* rho_basis_in)
20+
void ML_EXX::set_para(const Input_para& inp, const UnitCell* ucell_in, const ModulePW::PW_Basis* rho_basis_in, std::ostream& ofs_running)
2121
{
2222
torch::set_default_dtype(caffe2::TypeMeta::fromScalarType(torch::kDouble));
2323
auto output = torch::get_default_dtype();
24-
std::cout << "Default type: " << output << std::endl;
24+
ofs_running << " Default type: " << output << std::endl;
2525

26-
this->set_device(inp.of_ml_device);
26+
this->set_device(inp.of_ml_device, ofs_running);
2727

2828
this->nx = rho_basis_in->nrxx;
2929
this->nx_tot = rho_basis_in->nrxx;
@@ -48,15 +48,23 @@ void ML_EXX::set_para(const Input_para& inp, const UnitCell* ucell_in, const Mod
4848
inp.of_ml_tanhp_nl,
4949
inp.of_ml_tanhq_nl);
5050

51-
std::cout << "ninput = " << this->ninput << std::endl;
51+
ofs_running << "ninput = " << this->ninput << std::endl;
5252

5353
if (PARAM.inp.ml_exx)
5454
{
5555
int nnode = 100;
5656
int nlayer = 3;
57-
this->nn = std::make_shared<NN_OFImpl>(this->nx, 0, this->ninput, nnode, nlayer, this->device);
58-
torch::load(this->nn, "net.pt", this->device_type);
59-
std::cout << "load net done" << std::endl;
57+
this->nn = std::make_shared<NN_OFImpl>(this->nx, 0, this->ninput, nnode, nlayer, this->device, ofs_running);
58+
try
59+
{
60+
torch::load(this->nn, "net.pt", this->device_type);
61+
}
62+
catch (const std::exception& e)
63+
{
64+
ModuleBase::WARNING_QUIT("ML_EXX::set_para",
65+
"Failed to load neural network model from net.pt: " + std::string(e.what()));
66+
}
67+
ofs_running << "load net done (ML EXX neural network functional model loaded successfully)" << std::endl;
6068
if (PARAM.inp.of_ml_feg != 0)
6169
{
6270
torch::Tensor feg_inpt = torch::zeros(this->ninput, this->device_type);
@@ -74,7 +82,7 @@ void ML_EXX::set_para(const Input_para& inp, const UnitCell* ucell_in, const Mod
7482
this->feg_net_F = this->nn->forward(feg_inpt).to(this->device_CPU).contiguous().data_ptr<double>()[0];
7583
}
7684

77-
std::cout << "feg_net_F = " << this->feg_net_F << std::endl;
85+
ofs_running << "feg_net_F = " << this->feg_net_F << std::endl;
7886
}
7987
}
8088

@@ -88,8 +96,24 @@ void ML_EXX::set_para(const Input_para& inp, const UnitCell* ucell_in, const Mod
8896
this->chi_pnl = inp.of_ml_chi_pnl;
8997
this->chi_qnl = inp.of_ml_chi_qnl;
9098

91-
this->cal_tool->set_para(this->nx, inp.nelec, inp.of_tf_weight, inp.of_vw_weight, this->chi_p, this->chi_q,
92-
this->chi_xi, this->chi_pnl, this->chi_qnl, this->nkernel, inp.of_ml_kernel, inp.of_ml_kernel_scaling, inp.of_ml_yukawa_alpha, inp.of_ml_kernel_file, this->dV * rho_basis_in->nxyz, rho_basis_in);
99+
this->cal_tool->set_para(
100+
this->nx,
101+
inp.nelec,
102+
inp.of_tf_weight,
103+
inp.of_vw_weight,
104+
this->chi_p,
105+
this->chi_q,
106+
this->chi_xi,
107+
this->chi_pnl,
108+
this->chi_qnl,
109+
this->nkernel,
110+
inp.of_ml_kernel,
111+
inp.of_ml_kernel_scaling,
112+
inp.of_ml_yukawa_alpha,
113+
inp.of_ml_kernel_file,
114+
this->dV * rho_basis_in->nxyz,
115+
rho_basis_in,
116+
ofs_running);
93117
}
94118
}
95119

@@ -112,9 +136,9 @@ void ML_EXX::ml_potential(const double * const * prho, const ModulePW::PW_Basis
112136
rho_data[ir] = std::abs(prho[0][ir]);
113137
}
114138

115-
this->updateInput(prho_mod, pw_rho);
139+
this->update_input(prho_mod, pw_rho);
116140

117-
this->NN_forward(prho_mod, pw_rho, true);
141+
this->nn_forward(prho_mod, pw_rho, true);
118142

119143
torch::Tensor enhancement_cpu_tensor = this->nn->F.to(this->device_CPU).contiguous();
120144
this->enhancement_cpu_ptr = enhancement_cpu_tensor.data_ptr<double>();
@@ -148,13 +172,13 @@ void ML_EXX::ml_potential(const double * const * prho, const ModulePW::PW_Basis
148172
* @param pw_rho PW_Basis
149173
* @param veff effective potential
150174
*/
151-
void ML_EXX::generateTrainData(const double * const *prho, const ModulePW::PW_Basis *pw_rho, const double *veff)
175+
void ML_EXX::gen_training_data(const double * const *prho, const ModulePW::PW_Basis *pw_rho, const double *veff)
152176
{
153177
if (PARAM.inp.of_kinetic == "ml")
154178
{
155-
this->updateInput(prho, pw_rho);
179+
this->update_input(prho, pw_rho);
156180

157-
this->NN_forward(prho, pw_rho, true);
181+
this->nn_forward(prho, pw_rho, true);
158182

159183
torch::Tensor enhancement_cpu_tensor = this->nn->F.to(this->device_CPU).contiguous();
160184
this->enhancement_cpu_ptr = enhancement_cpu_tensor.data_ptr<double>();
@@ -166,8 +190,8 @@ void ML_EXX::generateTrainData(const double * const *prho, const ModulePW::PW_Ba
166190

167191
this->get_potential_(prho, pw_rho, potential);
168192

169-
this->dumpTensor("enhancement.npy", enhancement);
170-
this->dumpMatrix("potential.npy", potential);
193+
this->dump_tensor("enhancement.npy", enhancement);
194+
this->dump_matrix("potential.npy", potential);
171195
}
172196
}
173197

@@ -177,28 +201,28 @@ void ML_EXX::generateTrainData(const double * const *prho, const ModulePW::PW_Ba
177201
* @param prho charge density
178202
* @param pw_rho PW_Basis
179203
*/
180-
void ML_EXX::localTest(const double * const *pprho, const ModulePW::PW_Basis *pw_rho)
204+
void ML_EXX::localTest(const double * const *pprho, const ModulePW::PW_Basis *pw_rho, std::ostream& ofs_running)
181205
{
182206
// for test =====================
183207
std::vector<long unsigned int> cshape = {(long unsigned) this->nx};
184208
bool fortran_order = false;
185209

186210
std::vector<double> temp_prho(this->nx);
187-
this->loadVector("path_to_rho_file", temp_prho);
211+
this->load_vector("path_to_rho_file", temp_prho);
188212

189213
double ** prho = new double *[1];
190214
prho[0] = new double[this->nx];
191215
for (int ir = 0; ir < this->nx; ++ir) prho[0][ir] = temp_prho[ir];
192216
for (int ir = 0; ir < this->nx; ++ir)
193217
{
194218
if (prho[0][ir] == 0.){
195-
std::cout << "WARNING: rho = 0" << std::endl;
219+
ofs_running << "WARNING: rho = 0" << std::endl;
196220
}
197221
};
198222
// ==============================
199-
this->updateInput(prho, pw_rho);
223+
this->update_input(prho, pw_rho);
200224

201-
this->NN_forward(prho, pw_rho, true);
225+
this->nn_forward(prho, pw_rho, true);
202226

203227
torch::Tensor enhancement_cpu_tensor = this->nn->F.to(this->device_CPU).contiguous();
204228
this->enhancement_cpu_ptr = enhancement_cpu_tensor.data_ptr<double>();
@@ -210,8 +234,8 @@ void ML_EXX::localTest(const double * const *pprho, const ModulePW::PW_Basis *pw
210234

211235
this->get_potential_(prho, pw_rho, potential);
212236

213-
this->dumpTensor("enhancement-abacus.npy", enhancement);
214-
this->dumpMatrix("potential-abacus.npy", potential);
237+
this->dump_tensor("enhancement-abacus.npy", enhancement);
238+
this->dump_matrix("potential-abacus.npy", potential);
215239
exit(0);
216240
}
217241

source/source_estate/module_pot/pot_ml_exx.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ class ML_EXX : public ML_Base
1616
ML_EXX();
1717
virtual ~ML_EXX();
1818

19-
void set_para(const Input_para& inp, const UnitCell* ucell_in, const ModulePW::PW_Basis* rho_basis_in);
19+
void set_para(const Input_para& inp, const UnitCell* ucell_in, const ModulePW::PW_Basis* rho_basis_in, std::ostream& ofs_running);
2020

2121
void ml_potential(const double * const * prho, const ModulePW::PW_Basis *pw_rho, ModuleBase::matrix &rpotential);
2222

23-
// output all parameters
24-
void generateTrainData(const double * const *prho, const ModulePW::PW_Basis *pw_rho, const double *veff);
25-
void localTest(const double * const *prho, const ModulePW::PW_Basis *pw_rho);
23+
void gen_training_data(const double * const *prho, const ModulePW::PW_Basis *pw_rho, const double *veff);
24+
void localTest(const double * const *prho, const ModulePW::PW_Basis *pw_rho, std::ostream& ofs_running);
2625

2726
void init_data(
2827
const int &nkernel,
@@ -56,13 +55,13 @@ class PotML_EXX : public PotBase
5655
this->dynamic_mode = true;
5756
this->fixed_mode = false;
5857

59-
this->ml_exx.set_para(PARAM.inp, ucell_in, rho_basis_in);
58+
this->ml_exx.set_para(PARAM.inp, ucell_in, rho_basis_in, GlobalV::ofs_running);
6059
}
6160
~PotML_EXX() {};
6261

6362
void cal_v_eff(const Charge*const chg, const UnitCell*const ucell, ModuleBase::matrix& v_eff) override
6463
{
65-
if (PARAM.inp.of_ml_local_test) this->ml_exx.localTest(chg->rho, this->rho_basis_);
64+
if (PARAM.inp.of_ml_local_test) this->ml_exx.localTest(chg->rho, this->rho_basis_, GlobalV::ofs_running);
6665
this->ml_exx.ml_potential(chg->rho, this->rho_basis_, v_eff);
6766
}
6867

source/source_io/module_ctrl/ctrl_output_pw.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,8 @@ void ModuleIO::ctrl_runner_pw(UnitCell& ucell,
361361
inp.of_ml_yukawa_alpha,
362362
inp.of_ml_kernel_file,
363363
ucell.omega,
364-
pw_rho);
364+
pw_rho,
365+
GlobalV::ofs_running);
365366

366367
write_mlkedf_desc.generateTrainData_KS(PARAM.globalv.global_mlkedf_descriptor_dir,
367368
stp.template get_psi_t<T, Device>(),

source/source_io/module_ml/cal_mlkedf_descriptors.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ void Cal_MLKEDF_Descriptors::set_para(
1919
const std::vector<double> &yukawa_alpha,
2020
const std::vector<std::string> &kernel_file,
2121
const double &omega,
22-
const ModulePW::PW_Basis *pw_rho
22+
const ModulePW::PW_Basis *pw_rho,
23+
std::ostream& ofs_running
2324
)
2425
{
2526
this->nx = nx;
@@ -34,7 +35,6 @@ void Cal_MLKEDF_Descriptors::set_para(
3435
this->kernel_scaling = kernel_scaling;
3536
this->yukawa_alpha = yukawa_alpha;
3637
this->kernel_file = kernel_file;
37-
std::cout << "nkernel = " << nkernel << std::endl;
3838

3939
if (PARAM.inp.of_wt_rho0 != 0)
4040
{

source/source_io/module_ml/cal_mlkedf_descriptors.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class Cal_MLKEDF_Descriptors
3838
const std::vector<double> &yukawa_alpha,
3939
const std::vector<std::string> &kernel_file,
4040
const double &omega,
41-
const ModulePW::PW_Basis *pw_rho);
41+
const ModulePW::PW_Basis *pw_rho,
42+
std::ostream& ofs_running);
4243
// get input parameters
4344
void getGamma(const double * const *prho, std::vector<double> &rgamma);
4445
void getP(const double * const *prho, const ModulePW::PW_Basis *pw_rho, std::vector<std::vector<double>> &pnablaRho, std::vector<double> &rp);

source/source_pw/module_ofdft/kedf_extwt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ void KEDF_ExtWT::tau_extwt(const double* const* prho, ModulePW::PW_Basis* pw_rho
265265
*/
266266
void KEDF_ExtWT::extwt_potential(const double* const* prho, ModulePW::PW_Basis* pw_rho, ModuleBase::matrix& rpotential)
267267
{
268+
ModuleBase::TITLE("KEDF_ExtWT", "extwt_potential");
268269
ModuleBase::timer::start("KEDF_ExtWT", "extwt_potential");
269270

270271
// 1. WT potential

source/source_pw/module_ofdft/kedf_lkt.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ void KEDF_LKT::tau_lkt(const double* const* prho, ModulePW::PW_Basis* pw_rho, do
132132
*/
133133
void KEDF_LKT::lkt_potential(const double* const* prho, ModulePW::PW_Basis* pw_rho, ModuleBase::matrix& rpotential)
134134
{
135-
ModuleBase::timer::start("KEDF_LKT", "LKT_potential");
135+
ModuleBase::TITLE("KEDF_LKT", "lkt_potential");
136+
ModuleBase::timer::start("KEDF_LKT", "lkt_potential");
136137
this->lkt_energy = 0.;
137138
double* as = new double[pw_rho->nrxx]; // a*s
138139
double** nabla_rho = new double*[3];
@@ -193,7 +194,7 @@ void KEDF_LKT::lkt_potential(const double* const* prho, ModulePW::PW_Basis* pw_r
193194
delete[] nabla_rho;
194195
delete[] nabla_term;
195196

196-
ModuleBase::timer::end("KEDF_LKT", "LKT_potential");
197+
ModuleBase::timer::end("KEDF_LKT", "lkt_potential");
197198
}
198199

199200
/**

0 commit comments

Comments
 (0)