| 872 | #endif |
| 873 | |
| 874 | void init_tensor(py::module m) { |
| 875 | imperative::Tensor::static_initialize(); |
| 876 | init_backtrace_tss_key(); |
| 877 | // Transformations |
| 878 | static auto& transformations = TransformationManager::get_instance(); |
| 879 | |
| 880 | using Segment = TransformationManager::Segment; |
| 881 | |
| 882 | using Channel = interpreter::Interpreter::Channel; |
| 883 | |
| 884 | auto* channel = |
| 885 | imperative::ResourceManager::create_global<std::unique_ptr<Channel>>( |
| 886 | interpreter::Interpreter::inst().create_channel()) |
| 887 | ->get(); |
| 888 | interpreter_for_py = channel; |
| 889 | MGB_MARK_USED_VAR( |
| 890 | transformations |
| 891 | .register_at<Segment::Eval>( |
| 892 | std::make_shared<InterpreterTransformation>( |
| 893 | std::shared_ptr<Channel>(channel, [](Channel*) {}))) |
| 894 | .release()); |
| 895 | MGB_MARK_USED_VAR(transformations |
| 896 | .register_at<Segment::Scalar>( |
| 897 | std::make_shared<ScalarTransformation>()) |
| 898 | .release()); |
| 899 | MGB_MARK_USED_VAR(transformations |
| 900 | .register_at<Segment::Symbol>( |
| 901 | std::make_shared<SymbolTransformation>()) |
| 902 | .release()); |
| 903 | MGB_MARK_USED_VAR(transformations |
| 904 | .register_at<Segment::DTypePromote>( |
| 905 | std::make_shared<DTypePromoteTransformation>()) |
| 906 | .release()); |
| 907 | MGB_MARK_USED_VAR(transformations |
| 908 | .register_at<Segment::DimExpansion>( |
| 909 | std::make_shared<DimExpansionTransformation>()) |
| 910 | .release()); |
| 911 | MGB_MARK_USED_VAR(transformations |
| 912 | .register_at<Segment::Complex>( |
| 913 | std::make_shared<ComplexTransformation>()) |
| 914 | .release()); |
| 915 | MGB_MARK_USED_VAR(transformations |
| 916 | .register_at<Segment::Grad>( |
| 917 | std::make_shared<GradTransformationGuard>()) |
| 918 | .release()); |
| 919 | auto format_trans = std::make_shared<FormatTransformation>(); |
| 920 | MGB_MARK_USED_VAR( |
| 921 | transformations.register_at<Segment::Format>(format_trans).release()); |
| 922 | |
| 923 | static py::exception<interpreter::AsyncError> py_async_error( |
| 924 | m, "AsyncError", PyExc_RuntimeError); |
| 925 | py::register_exception_translator([](std::exception_ptr p) { |
| 926 | try { |
| 927 | if (p) |
| 928 | std::rethrow_exception(p); |
| 929 | } catch (const interpreter::AsyncError& e) { |
| 930 | pyext17::pybind11_translate_exception(e.nested_ptr()); |
| 931 | if (PyErr_Occurred()) { |