program.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2024, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "common.h"
20 
21 #include <raft/core/handle.hpp>
22 
23 #include <random>
24 
25 namespace cuml {
26 namespace genetic {
27 
32 struct program {
47  explicit program();
48 
54 
60  explicit program(const program& src);
61 
69  program& operator=(const program& src);
70 
73  int len;
75  int depth;
77  float raw_fitness_;
82 }; // struct program
83 
85 typedef program* program_t;
86 
97 void execute(const raft::handle_t& h,
98  const program_t& d_progs,
99  const int n_rows,
100  const int n_progs,
101  const float* data,
102  float* y_pred);
103 
117 void compute_metric(const raft::handle_t& h,
118  int n_rows,
119  int n_progs,
120  const float* y,
121  const float* y_pred,
122  const float* w,
123  float* score,
124  const param& params);
125 
138 void find_fitness(const raft::handle_t& h,
139  program_t& d_prog,
140  float* score,
141  const param& params,
142  const int n_rows,
143  const float* data,
144  const float* y,
145  const float* sample_weights);
146 
160 void find_batched_fitness(const raft::handle_t& h,
161  int n_progs,
162  program_t& d_progs,
163  float* score,
164  const param& params,
165  const int n_rows,
166  const float* data,
167  const float* y,
168  const float* sample_weights);
169 
182 void set_fitness(const raft::handle_t& h,
183  program_t& d_prog,
184  program& h_prog,
185  const param& params,
186  const int n_rows,
187  const float* data,
188  const float* y,
189  const float* sample_weights);
190 
204 void set_batched_fitness(const raft::handle_t& h,
205  int n_progs,
206  program_t& d_progs,
207  std::vector<program>& h_progs,
208  const param& params,
209  const int n_rows,
210  const float* data,
211  const float* y,
212  const float* sample_weights);
213 
222 float get_fitness(const program& prog, const param& params);
223 
230 int get_depth(const program& p_out);
231 
239 void build_program(program& p_out, const param& params, std::mt19937& rng);
240 
249 void point_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
250 
261 void crossover(const program& prog,
262  const program& donor,
263  program& p_out,
264  const param& params,
265  std::mt19937& rng);
266 
277 void subtree_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
278 
288 void hoist_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
289 } // namespace genetic
290 } // namespace cuml
RF_metrics score(const raft::handle_t &user_handle, const RandomForestClassifierF *forest, const int *ref_labels, int n_rows, const int *predictions, int verbosity=CUML_LEVEL_INFO)
void crossover(const program &prog, const program &donor, program &p_out, const param &params, std::mt19937 &rng)
Perform a 'hoisted' crossover mutation using the parent and donor programs. The donor subtree selecte...
void build_program(program &p_out, const param &params, std::mt19937 &rng)
Build a random program with depth atmost 10.
float get_fitness(const program &prog, const param &params)
Returns precomputed fitness score of program on the host, after accounting for parsimony.
void set_fitness(const raft::handle_t &h, program_t &d_prog, program &h_prog, const param &params, const int n_rows, const float *data, const float *y, const float *sample_weights)
Computes and sets the fitness scores for a single program on the given dataset.
void subtree_mutation(const program &prog, program &p_out, const param &params, std::mt19937 &rng)
Performs a crossover mutation with a randomly built new program. Since crossover is 'hoisted',...
metric_t
Definition: common.h:30
void find_fitness(const raft::handle_t &h, program_t &d_prog, float *score, const param &params, const int n_rows, const float *data, const float *y, const float *sample_weights)
Computes the fitness scores for a sngle program on the given dataset.
void find_batched_fitness(const raft::handle_t &h, int n_progs, program_t &d_progs, float *score, const param &params, const int n_rows, const float *data, const float *y, const float *sample_weights)
Computes the fitness scores for all programs on the given dataset.
void hoist_mutation(const program &prog, program &p_out, const param &params, std::mt19937 &rng)
Perform a hoist mutation on a random subtree of the given program (replace a subtree with a subtree o...
void execute(const raft::handle_t &h, const program_t &d_progs, const int n_rows, const int n_progs, const float *data, float *y_pred)
Calls the execution kernel to evaluate all programs on the given dataset.
int get_depth(const program &p_out)
Evaluates and returns the depth of the current program.
mutation_t
Definition: common.h:68
program * program_t
Definition: program.h:85
void set_batched_fitness(const raft::handle_t &h, int n_progs, program_t &d_progs, std::vector< program > &h_progs, const param &params, const int n_rows, const float *data, const float *y, const float *sample_weights)
Computes and sets the fitness scores for all programs on the given dataset.
void compute_metric(const raft::handle_t &h, int n_rows, int n_progs, const float *y, const float *y_pred, const float *w, float *score, const param &params)
Compute the loss based on the metric specified in the training hyperparameters. It performs a batched...
void point_mutation(const program &prog, program &p_out, const param &params, std::mt19937 &rng)
Perform a point mutation on the given program(AST)
Definition: common.h:26
Represents a node in the syntax tree.
Definition: node.h:38
contains all the hyper-parameters for training
Definition: common.h:94
The main data structure to store the AST that represents a program in the current generation.
Definition: program.h:32
float raw_fitness_
Definition: program.h:77
metric_t metric
Definition: program.h:79
program & operator=(const program &src)
assignment operator
node * nodes
Definition: program.h:71
~program()
Destroy the program object.
int depth
Definition: program.h:75
int len
Definition: program.h:73
program(const program &src)
Copy constructor for a new program object.
mutation_t mut_type
Definition: program.h:81