| 1266 | } |
| 1267 | } |
| 1268 | void enter() { |
| 1269 | auto& self = *this; |
| 1270 | if (!self.trace_result) { // untraced |
| 1271 | self.tracing = std::make_shared<TracingTransformation>( |
| 1272 | self.capture_as_const, self.record_input_shapes); |
| 1273 | if (self.without_host) |
| 1274 | self.tracing->enable_record_all_shapes(); |
| 1275 | if (self.symbolic) { |
| 1276 | self.lazy_eval = |
| 1277 | std::make_shared<LazyEvalTransformation>(self.no_exec); |
| 1278 | self.options_visitor(py::cast(&self.lazy_eval->options())); |
| 1279 | } |
| 1280 | } else if (!self.compiled) { // traced but not compiled |
| 1281 | using namespace std::placeholders; |
| 1282 | self.compiled = std::make_shared<CompiledTransformation>( |
| 1283 | *self.trace_result, self.record_input_shapes, self.imperative); |
| 1284 | self.compiled->set_value_comparator( |
| 1285 | std::bind(&Trace::compare_value, this, _1, _2)); |
| 1286 | self.options_visitor(py::cast(&self.compiled->options())); |
| 1287 | try { |
| 1288 | self.compiled->compile(); |
| 1289 | } catch (const std::exception& e) { |
| 1290 | mgb_log_error("error in trace: %s", e.what()); |
| 1291 | } |
| 1292 | } |
| 1293 | // register transformations |
| 1294 | if (self.compiled) { |
| 1295 | if (self.profile) { |
| 1296 | auto& current_graph = self.compiled->graph(); |
| 1297 | if (self.profiler.first != self.compiled->graph().id()) { |
| 1298 | // graph changed |
| 1299 | self.profiler = std::make_pair( |
| 1300 | current_graph.id(), |
| 1301 | std::make_shared<GraphProfiler>(¤t_graph)); |
| 1302 | } |
| 1303 | } |
| 1304 | if (!without_host) |
| 1305 | compiled_guard = |
| 1306 | transformations.register_at<Segment::Trace>(self.compiled); |
| 1307 | else |
| 1308 | self.compiled->set_pc_to_end(); |
| 1309 | // start execute because InputCallback depends |
| 1310 | self.compiled->execute(); |
| 1311 | } else if (self.tracing) { |
| 1312 | tracing_guard = |
| 1313 | transformations.register_at<Segment::Trace>(self.tracing); |
| 1314 | if (self.lazy_eval) { |
| 1315 | lazy_eval_guard = |
| 1316 | transformations.register_at<Segment::Eval>(self.lazy_eval); |
| 1317 | } |
| 1318 | } else { |
| 1319 | mgb_throw(MegBrainError, "invalid state: neither tracing nor compiled"); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | void exit() { |
| 1324 | auto& self = *this; |
nothing calls this directly
no test coverage detected