Skip to content

Commit

Permalink
Merge pull request brucefan1983#744 from brucefan1983/no_multi_layer
Browse files Browse the repository at this point in the history
remove multi-layer
  • Loading branch information
brucefan1983 authored Oct 3, 2024
2 parents a7814a1 + b609140 commit 7191884
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 273 deletions.
9 changes: 1 addition & 8 deletions src/main_nep/fitness.cu
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,7 @@ void Fitness::write_nep_txt(FILE* fid_nep, Parameters& para, float* elite)
fprintf(fid_nep, "basis_size %d %d\n", para.basis_size_radial, para.basis_size_angular);
fprintf(fid_nep, "l_max %d %d %d\n", para.L_max, para.L_max_4body, para.L_max_5body);

if (para.num_hidden_layers == 3) {
fprintf(
fid_nep, "ANN %d %d %d\n", para.num_neurons[0], para.num_neurons[1], para.num_neurons[2]);
} else if (para.num_hidden_layers == 2) {
fprintf(fid_nep, "ANN %d %d %d\n", para.num_neurons[0], para.num_neurons[1], 0);
} else if (para.num_hidden_layers == 1) {
fprintf(fid_nep, "ANN %d %d\n", para.num_neurons[0], 0);
}
fprintf(fid_nep, "ANN %d %d\n", para.num_neurons1, 0);
for (int m = 0; m < para.number_of_variables; ++m) {
fprintf(fid_nep, "%15.7e\n", elite[m]);
}
Expand Down
119 changes: 41 additions & 78 deletions src/main_nep/nep3.cu
Original file line number Diff line number Diff line change
Expand Up @@ -300,33 +300,8 @@ NEP3::NEP3(
for (int device_id = 0; device_id < deviceCount; device_id++) {
cudaSetDevice(device_id);
annmb[device_id].dim = para.dim;
annmb[device_id].num_neurons[0] = para.num_neurons[0];
annmb[device_id].num_neurons[1] = para.num_neurons[1];
annmb[device_id].num_neurons[2] = para.num_neurons[2];
annmb[device_id].num_neurons1 = para.num_neurons1;
annmb[device_id].num_para = para.number_of_variables;
annmb[device_id].num_hidden_layers = para.num_hidden_layers;
if (annmb[device_id].num_hidden_layers == 1) {
annmb[device_id].num_para_one_ann_without_bias =
(annmb[device_id].dim + 2) * annmb[device_id].num_neurons[0];
} else if (annmb[device_id].num_hidden_layers == 2) {
annmb[device_id].num_para_one_ann_without_bias =
(annmb[device_id].dim + 1) * annmb[device_id].num_neurons[0] +
(annmb[device_id].num_neurons[0] + 2) * annmb[device_id].num_neurons[1];
} else {
annmb[device_id].num_para_one_ann_without_bias =
(annmb[device_id].dim + 1) * annmb[device_id].num_neurons[0] +
(annmb[device_id].num_neurons[0] + 1) * annmb[device_id].num_neurons[1] +
(annmb[device_id].num_neurons[1] + 2) * annmb[device_id].num_neurons[2];
}
annmb[device_id].offset_w[0] = 0;
annmb[device_id].offset_b[0] = annmb[device_id].dim * annmb[device_id].num_neurons[0];
annmb[device_id].offset_w[1] = annmb[device_id].offset_b[0] + annmb[device_id].num_neurons[0];
annmb[device_id].offset_b[1] = annmb[device_id].offset_w[1] + annmb[device_id].num_neurons[0] *
annmb[device_id].num_neurons[1];
annmb[device_id].offset_w[2] = annmb[device_id].offset_b[1] + annmb[device_id].num_neurons[1];
annmb[device_id].offset_b[2] = annmb[device_id].offset_w[2] + annmb[device_id].num_neurons[1] *
annmb[device_id].num_neurons[2];
annmb[device_id].offset_w[3] = annmb[device_id].offset_b[2] + annmb[device_id].num_neurons[2];

nep_data[device_id].NN_radial.resize(N);
nep_data[device_id].NN_angular.resize(N);
Expand All @@ -350,23 +325,31 @@ void NEP3::update_potential(Parameters& para, float* parameters, ANN& ann)
float* pointer = parameters;
for (int t = 0; t < paramb.num_types; ++t) {
if (t > 0 && paramb.version != 4) { // Use the same set of NN parameters for NEP3
pointer -= ann.num_para_one_ann_without_bias;
pointer -= (ann.dim + 2) * ann.num_neurons1;
}
ann.para[t] = pointer;
pointer += ann.num_para_one_ann_without_bias;
ann.w0[t] = pointer;
pointer += ann.num_neurons1 * ann.dim;
ann.b0[t] = pointer;
pointer += ann.num_neurons1;
ann.w1[t] = pointer;
pointer += ann.num_neurons1;
}
ann.b_out = pointer;
ann.b1 = pointer;
pointer += 1;

if (para.train_mode == 2) {
for (int t = 0; t < paramb.num_types; ++t) {
if (t > 0 && paramb.version != 4) { // Use the same set of NN parameters for NEP3
pointer -= ann.num_para_one_ann_without_bias;
pointer -= (ann.dim + 2) * ann.num_neurons1;
}
ann.para_pol[t] = pointer;
pointer += ann.num_para_one_ann_without_bias;
ann.w0_pol[t] = pointer;
pointer += ann.num_neurons1 * ann.dim;
ann.b0_pol[t] = pointer;
pointer += ann.num_neurons1;
ann.w1_pol[t] = pointer;
pointer += ann.num_neurons1;
}
ann.b_out_pol = pointer;
ann.b1_pol = pointer;
pointer += 1;
}

Expand Down Expand Up @@ -433,18 +416,13 @@ static __global__ void apply_ann(
}
// get energy and energy gradient
float F = 0.0f, Fp[MAX_DIM] = {0.0f};
apply_ann_multi_layers(
apply_ann_one_layer(
annmb.dim,
annmb.num_hidden_layers,
annmb.num_neurons,
annmb.para[type] + annmb.offset_w[0],
annmb.para[type] + annmb.offset_w[1],
annmb.para[type] + annmb.offset_w[2],
annmb.para[type] + annmb.offset_b[0],
annmb.para[type] + annmb.offset_b[1],
annmb.para[type] + annmb.offset_b[2],
annmb.para[type] + annmb.offset_w[3],
annmb.b_out,
annmb.num_neurons1,
annmb.w0[type],
annmb.b0[type],
annmb.w1[type],
annmb.b1,
q,
F,
Fp);
Expand Down Expand Up @@ -478,18 +456,13 @@ static __global__ void apply_ann_pol(
float F = 0.0f, Fp[MAX_DIM] = {0.0f};

// scalar part
apply_ann_multi_layers(
apply_ann_one_layer(
annmb.dim,
annmb.num_hidden_layers,
annmb.num_neurons,
annmb.para_pol[type] + annmb.offset_w[0],
annmb.para_pol[type] + annmb.offset_w[1],
annmb.para_pol[type] + annmb.offset_w[2],
annmb.para_pol[type] + annmb.offset_b[0],
annmb.para_pol[type] + annmb.offset_b[1],
annmb.para_pol[type] + annmb.offset_b[2],
annmb.para_pol[type] + annmb.offset_w[3],
annmb.b_out_pol,
annmb.num_neurons1,
annmb.w0_pol[type],
annmb.b0_pol[type],
annmb.w1_pol[type],
annmb.b1_pol,
q,
F,
Fp);
Expand All @@ -501,18 +474,13 @@ static __global__ void apply_ann_pol(
for (int d = 0; d < annmb.dim; ++d) {
Fp[d] = 0.0f;
}
apply_ann_multi_layers(
apply_ann_one_layer(
annmb.dim,
annmb.num_hidden_layers,
annmb.num_neurons,
annmb.para[type] + annmb.offset_w[0],
annmb.para[type] + annmb.offset_w[1],
annmb.para[type] + annmb.offset_w[2],
annmb.para[type] + annmb.offset_b[0],
annmb.para[type] + annmb.offset_b[1],
annmb.para[type] + annmb.offset_b[2],
annmb.para[type] + annmb.offset_w[3],
annmb.b_out,
annmb.num_neurons1,
annmb.w0[type],
annmb.b0[type],
annmb.w1[type],
annmb.b1,
q,
F,
Fp);
Expand Down Expand Up @@ -547,18 +515,13 @@ static __global__ void apply_ann_temperature(
q[annmb.dim - 1] = temperature * g_q_scaler[annmb.dim - 1];
// get energy and energy gradient
float F = 0.0f, Fp[MAX_DIM] = {0.0f};
apply_ann_multi_layers(
apply_ann_one_layer(
annmb.dim,
annmb.num_hidden_layers,
annmb.num_neurons,
annmb.para[type] + annmb.offset_w[0],
annmb.para[type] + annmb.offset_w[1],
annmb.para[type] + annmb.offset_w[2],
annmb.para[type] + annmb.offset_b[0],
annmb.para[type] + annmb.offset_b[1],
annmb.para[type] + annmb.offset_b[2],
annmb.para[type] + annmb.offset_w[3],
annmb.b_out,
annmb.num_neurons1,
annmb.w0[type],
annmb.b0[type],
annmb.w1[type],
annmb.b1,
q,
F,
Fp);
Expand Down
19 changes: 9 additions & 10 deletions src/main_nep/nep3.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,17 @@ public:

struct ANN {
int dim = 0; // dimension of the descriptor
int num_hidden_layers = 1; // number of hidden layers (1 to 3)
int num_neurons[3] = {0, 0, 0}; // number of neurons in the hidden layer
int num_neurons1 = 0; // number of neurons in the hidden layer
int num_para = 0; // number of parameters
int num_para_one_ann_without_bias =
0; // number of parameters for one ann without the output bias
int offset_w[4];
int offset_b[3];
const float* para[NUM_ELEMENTS]; // weight and bias parameters for the hidden layers
const float* b_out; // bias for the output layer
const float* w0[NUM_ELEMENTS]; // weight from the input layer to the hidden layer
const float* b0[NUM_ELEMENTS]; // bias for the hidden layer
const float* w1[NUM_ELEMENTS]; // weight from the hidden layer to the output layer
const float* b1; // bias for the output layer
// for the scalar part of polarizability
const float* para_pol[NUM_ELEMENTS]; // weight and bias parameters for the hidden layers
const float* b_out_pol; // bias for the output layer
const float* w0_pol[10]; // weight from the input layer to the hidden layer
const float* b0_pol[10]; // bias for the hidden layer
const float* w1_pol[10]; // weight from the hidden layer to the output layer
const float* b1_pol; // bias for the output layer
// for elements in descriptor
const float* c;
};
Expand Down
81 changes: 10 additions & 71 deletions src/main_nep/parameters.cu
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ void Parameters::set_default_parameters()
L_max = 4; // the only supported value
L_max_4body = 2; // default is to include 4body
L_max_5body = 0; // default is not to include 5body
num_hidden_layers = 1; // default is to have one hidden layer
num_neurons[0] = 30; // a relatively small value to achieve high speed
num_neurons[1] = 0; // default is not to have the 2nd hidden layer
num_neurons[2] = 0; // default is not to have the 3rd hidden layer
num_neurons1 = 30; // a relatively small value to achieve high speed
lambda_1 = lambda_2 = -1.0f; // automatic regularization
lambda_e = lambda_f = 1.0f; // energy and force are more important
lambda_v = 0.1f; // virial is less important
Expand Down Expand Up @@ -187,18 +184,7 @@ void Parameters::calculate_parameters()
}
#endif

if (num_hidden_layers == 1) {
number_of_variables_ann = (dim + 2) * num_neurons[0] * (version == 4 ? num_types : 1) + 1;
} else if (num_hidden_layers == 2) {
number_of_variables_ann = ((dim + 1) * num_neurons[0] + (num_neurons[0] + 2) * num_neurons[1]) *
(version == 4 ? num_types : 1) +
1;
} else {
number_of_variables_ann = ((dim + 1) * num_neurons[0] + (num_neurons[0] + 1) * num_neurons[1] +
(num_neurons[1] + 2) * num_neurons[2]) *
(version == 4 ? num_types : 1) +
1;
}
number_of_variables_ann = (dim + 2) * num_neurons1 * (version == 4 ? num_types : 1) + 1;

number_of_variables_descriptor =
num_types * num_types *
Expand Down Expand Up @@ -358,17 +344,9 @@ void Parameters::report_inputs()
}

if (is_neuron_set) {
printf(
" (input) number of neurons = (%d, %d, %d).\n",
num_neurons[0],
num_neurons[1],
num_neurons[2]);
printf(" (input) number of neurons = %d.\n", num_neurons1);
} else {
printf(
" (default) number of neurons = (%d, %d, %d).\n",
num_neurons[0],
num_neurons[1],
num_neurons[2]);
printf(" (default) number of neurons = %d.\n", num_neurons1);
}

if (is_lambda_1_set) {
Expand Down Expand Up @@ -439,18 +417,7 @@ void Parameters::report_inputs()
printf(" number of radial descriptor components = %d.\n", dim_radial);
printf(" number of angular descriptor components = %d.\n", dim_angular);
printf(" total number of descriptor components = %d.\n", dim);
if (num_hidden_layers == 3) {
printf(
" NN architecture = %d-%d-%d-%d-1.\n",
dim,
num_neurons[0],
num_neurons[1],
num_neurons[2]);
} else if (num_hidden_layers == 2) {
printf(" NN architecture = %d-%d-%d-1.\n", dim, num_neurons[0], num_neurons[1]);
} else {
printf(" NN architecture = %d-%d-1.\n", dim, num_neurons[0]);
}
printf(" NN architecture = %d-%d-1.\n", dim, num_neurons1);
printf(
" number of NN parameters to be optimized = %d.\n",
number_of_variables_ann * (train_mode == 2 ? 2 : 1));
Expand Down Expand Up @@ -781,46 +748,18 @@ void Parameters::parse_neuron(const char** param, int num_param)
{
is_neuron_set = true;

if (num_param < 2 || num_param > 4) {
PRINT_INPUT_ERROR("neuron should have 1 to 3 parameters.\n");
if (num_param != 2) {
PRINT_INPUT_ERROR("neuron should have 1 parameter.\n");
}

num_hidden_layers = num_param - 1;

if (!is_valid_int(param[1], &num_neurons[0])) {
if (!is_valid_int(param[1], &num_neurons1)) {
PRINT_INPUT_ERROR("number of neurons should be an integer.\n");
}
if (num_neurons[0] < 1) {
if (num_neurons1 < 1) {
PRINT_INPUT_ERROR("number of neurons should >= 1.");
} else if (num_neurons[0] > 200) {
} else if (num_neurons1 > 200) {
PRINT_INPUT_ERROR("number of neurons should <= 200.");
}

if (num_param > 2) {
if (!is_valid_int(param[2], &num_neurons[1])) {
PRINT_INPUT_ERROR("number of neurons should be an integer.\n");
}
if (num_neurons[1] < 1) {
PRINT_INPUT_ERROR("number of neurons should >= 1.");
} else if (num_neurons[1] > 200) {
PRINT_INPUT_ERROR("number of neurons should <= 200.");
}
}

if (num_param > 3) {
if (!is_valid_int(param[3], &num_neurons[2])) {
PRINT_INPUT_ERROR("number of neurons should be an integer.\n");
}
if (num_neurons[2] < 1) {
PRINT_INPUT_ERROR("number of neurons should >= 1.");
} else if (num_neurons[2] > 200) {
PRINT_INPUT_ERROR("number of neurons should <= 200.");
}
}

if (num_neurons[0] + num_neurons[1] + num_neurons[2] > 200) {
PRINT_INPUT_ERROR("total number of neurons should <= 200.\n");
}
}

void Parameters::parse_lambda_1(const char** param, int num_param)
Expand Down
3 changes: 1 addition & 2 deletions src/main_nep/parameters.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public:
int num_types; // number of atom types
int population_size; // population size for SNES
int maximum_generation; // maximum number of generations for SNES;
int num_hidden_layers; // number of hidden layers
int num_neurons[3]; // number of nuerons in the three hidden layers
int num_neurons1; // number of nuerons in the 1st hidden layer (only one hidden layer)
int basis_size_radial; // for nep3
int basis_size_angular; // for nep3
int n_max_radial; // maximum order of the radial Chebyshev polynomials
Expand Down
Loading

0 comments on commit 7191884

Please sign in to comment.