cutcutcodec 1.0.post3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of cutcutcodec might be problematic. Click here for more details.
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/__init__.py +6 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/__main__.py +38 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/c_build.py +33 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/_helper_properties.py +52 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/audio/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/audio/properties/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/audio/properties/duration.py +83 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/ffprobe.py +637 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/graph/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/graph/find.py +82 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/graph/time_backprop.py +68 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/rate_audio.py +239 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/rate_video.py +97 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/shape.py +107 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/stream/time_backprop.py +183 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/duration.py +162 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/nb_frames.py +159 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/rate.py +102 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/analysis/video/properties/timestamps.py +194 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/container.py +40 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/encoder.py +152 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/filter.py +33 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/frame.py +127 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/frame_audio.py +205 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/frame_video.py +322 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/layout.py +206 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/meta_filter.py +41 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/muxer.py +184 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/node.py +164 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/stream.py +182 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/stream_audio.py +130 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/classes/stream_video.py +193 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/__init__.py +7 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/ast_to_file.py +125 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/__init__.py +6 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/compatibility.py +734 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/default.py +99 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/encodec.py +40 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/muxer.py +8 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/export/rate.py +133 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/graph_to_ast.py +477 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/graph_to_json.py +104 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/graph_to_tree.py +190 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/json_to_graph.py +114 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/parse.py +123 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/__init__.py +15 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/dynamic.py +544 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/lambdify.py +612 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/preprocess.py +698 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/sympy_to_torch/printer.py +926 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/compilation/tree_to_graph.py +168 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/decision/__init__.py +8 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/operation/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/operation/add.py +73 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/edit/operation/remove.py +476 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/exceptions.py +60 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/add.py +170 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/cat.py +60 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/cut.py +112 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/delay.py +85 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/equation.py +261 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/fir.py +231 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/identity.py +48 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/resample.py +451 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/audio/subclip.py +91 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/cut.py +74 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/mix/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/mix/audio_cast.py +208 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/mix/video_cast.py +197 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/subclip.py +53 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/add.py +331 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/cat.py +60 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/cut.py +113 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/delay.py +84 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/equation.py +265 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/identity.py +48 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/pad.py +99 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/filters/video/subclip.py +91 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/audio/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/audio/empty.py +73 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/audio/equation.py +56 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/audio/noise.py +243 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/c_fractal.cpython-312-x86_64-linux-gnu.so +0 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/empty.py +69 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/equation.py +109 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/fractal/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/fractal/fractal.c +116 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/fractal/geometry.py +100 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/fractal/mandelbrot.py +176 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/generation/video/noise.py +112 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/interfaces/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/interfaces/seedable.py +82 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/read_ffmpeg.py +904 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/read_svg.py +145 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/scheduler.py +356 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/io/write.py +340 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/basic.py +35 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/clean/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/clean/graph.py +80 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/hashes/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/hashes/graph.py +81 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cache/singleton.py +47 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/cuda/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/factor/__init__.py +5 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/factor/proba.py +189 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/generalize/__init__.py +5 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/generalize/delay_to_fir.py +88 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/graph.py +17 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/parallel/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/parallel/buffer.py +90 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/core/opti/utils.py +34 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/audio_5.1_narration.oga +0 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/intro.webm +0 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/logo.svg +13 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/project.json +226 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/examples/video.mp4 +0 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/__init__.py +8 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/__main__.py +25 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/actions.py +63 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/app/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/app/app.py +248 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/base.py +50 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/container_input_ffmpeg.py +90 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_audio_delay.py +9 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_audio_equation.py +45 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_audio_fir.py +58 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_audio_subclip.py +9 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_cut.py +82 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_delay.py +20 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_subclip.py +21 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_video_delay.py +9 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_video_equation.py +53 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/filter_video_subclip.py +9 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_audio_equation.py +14 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_audio_noise.py +19 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_video_equation.py +13 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_video_mandelbrot.py +60 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/all/generator_video_noise.py +18 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/base.py +99 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/default.py +27 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/documentation.py +47 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/general.py +71 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/interface.py +305 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/loader.py +41 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edit_node_state/main.py +62 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/edition_tabs.py +34 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry/base.py +122 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry/filters.py +42 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry/generators.py +42 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/entry_tabs.py +46 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/_helper.py +54 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/container.py +226 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/doc.py +67 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/encodec.py +289 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/export/main.py +173 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/flowchart/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/flowchart/edge_properties.py +56 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/flowchart/main.py +280 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/flowchart/node.py +44 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/main.py +151 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/menu.py +21 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/preferences/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/preferences/audio_settings.py +183 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/preferences/main.py +41 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/preferences/video_settings.py +257 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/run.py +71 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/main.py +30 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/slider.py +286 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/track.py +97 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/timeline/view.py +90 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/toolbar.py +29 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/tools.py +23 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/__init__.py +3 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/audio_player.py +103 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/control.py +96 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/frame_extractor.py +254 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/gui/video_preview/main.py +115 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/__init__.py +8 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/__main__.py +27 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/coding_style.py +49 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/generation.py +52 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/install.py +72 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/run.py +58 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/abstract.py +215 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/audio_cast.py +35 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/audio_equation.py +71 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/c_memory.py +132 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/compatibility.py +27 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/compile.py +42 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/encoder.py +23 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/filter_cut.py +107 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/filter_delay.py +71 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/filter_empty.py +40 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/fir.py +143 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/hashes.py +23 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/initialisation.py +24 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/muxer.py +22 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/nan_snapshot.py +39 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/read.py +192 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/serialization_equality.py +50 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/snapshot_shape.py +21 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/sympy_to_torch.py +135 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/thread_safe.py +77 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/time_range.py +55 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/video_equation.py +77 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/testing/tests/write.py +95 -0
- cutcutcodec-1.0.post3.data/platlib/cutcutcodec/utils.py +40 -0
- cutcutcodec-1.0.post3.dist-info/LICENSE +7 -0
- cutcutcodec-1.0.post3.dist-info/METADATA +271 -0
- cutcutcodec-1.0.post3.dist-info/RECORD +233 -0
- cutcutcodec-1.0.post3.dist-info/WHEEL +5 -0
- cutcutcodec-1.0.post3.dist-info/entry_points.txt +6 -0
- cutcutcodec-1.0.post3.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,612 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+
"""Convert a sympy expression into a torch function.
|
|
4
|
+
|
|
5
|
+
It is the main entry point.
|
|
6
|
+
The preprocessing is delegated to the
|
|
7
|
+
``cutcutcodec.core.compilation.sympy_to_torch.preprocess`` module.
|
|
8
|
+
The compilation is delegated to the
|
|
9
|
+
``cutcutcodec.core.compilation.sympy_to_torch.printer`` module.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import hashlib
|
|
13
|
+
import importlib
|
|
14
|
+
import logging
|
|
15
|
+
import numbers
|
|
16
|
+
import pathlib
|
|
17
|
+
import subprocess
|
|
18
|
+
import sys
|
|
19
|
+
import tempfile
|
|
20
|
+
import typing
|
|
21
|
+
import uuid
|
|
22
|
+
|
|
23
|
+
from sympy.core.basic import Basic
|
|
24
|
+
from sympy.core.containers import Tuple
|
|
25
|
+
from sympy.core.symbol import Symbol
|
|
26
|
+
import torch
|
|
27
|
+
|
|
28
|
+
from cutcutcodec.core.compilation.sympy_to_torch.dynamic import _dyn_eval
|
|
29
|
+
from cutcutcodec.core.compilation.sympy_to_torch.preprocess import evalf, preprocess
|
|
30
|
+
from cutcutcodec.core.compilation.sympy_to_torch.printer import _printer
|
|
31
|
+
from cutcutcodec.core.exceptions import CompilationError
|
|
32
|
+
from cutcutcodec.utils import get_compilation_rules
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _cast_lambdify_c(
|
|
36
|
+
func: callable, input_args: dict[str, typing.Union[numbers.Real, torch.Tensor]]
|
|
37
|
+
) -> typing.Union[torch.Tensor, tuple[torch.Tensor]]:
|
|
38
|
+
"""Cast the tensors into flat c continuous numpy array and cast back before return.
|
|
39
|
+
|
|
40
|
+
Parameters
|
|
41
|
+
----------
|
|
42
|
+
func : callable
|
|
43
|
+
The C func from ``cutcutcodec.core.compilation.sympy_to_torch.lamdbify._lambdify_c``.
|
|
44
|
+
input_args : dict[str, torch.Tensor]
|
|
45
|
+
The named args to give to the function.
|
|
46
|
+
|
|
47
|
+
Returns
|
|
48
|
+
-------
|
|
49
|
+
tuple
|
|
50
|
+
The ouput tensors of func, homogeneous with the input tensors.
|
|
51
|
+
|
|
52
|
+
Raises
|
|
53
|
+
------
|
|
54
|
+
cutcutcodec.core.exceptions.CompilationError
|
|
55
|
+
If func raise RuntimeError or TypeError or
|
|
56
|
+
if the arguments are not conformed.
|
|
57
|
+
|
|
58
|
+
Examples
|
|
59
|
+
--------
|
|
60
|
+
>>> import numpy as np
|
|
61
|
+
>>> import torch
|
|
62
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _cast_lambdify_c
|
|
63
|
+
>>> def func(tensor):
|
|
64
|
+
... print("c_contiguous:", tensor.flags["C_CONTIGUOUS"])
|
|
65
|
+
... print("shape:", tensor.shape)
|
|
66
|
+
... return tensor
|
|
67
|
+
...
|
|
68
|
+
>>> arg = torch.full((3, 4), .0j).real
|
|
69
|
+
>>> arg.is_contiguous()
|
|
70
|
+
False
|
|
71
|
+
>>> _cast_lambdify_c(func, {"x": arg})
|
|
72
|
+
c_contiguous: True
|
|
73
|
+
shape: (12,)
|
|
74
|
+
tensor([[0., 0., 0., 0.],
|
|
75
|
+
[0., 0., 0., 0.],
|
|
76
|
+
[0., 0., 0., 0.]])
|
|
77
|
+
>>>
|
|
78
|
+
"""
|
|
79
|
+
# basic verification, raise exception as soon as possible
|
|
80
|
+
if any(a.requires_grad for a in input_args.values() if isinstance(a, torch.Tensor)):
|
|
81
|
+
raise CompilationError("gradient is not supported by this compiled C function")
|
|
82
|
+
if any(a.device.type != "cpu" for a in input_args.values() if isinstance(a, torch.Tensor)):
|
|
83
|
+
raise CompilationError("other device than cpu is not supproted by this compiled C function")
|
|
84
|
+
if not input_args:
|
|
85
|
+
raise CompilationError("the compiled C function has to take at least one argument")
|
|
86
|
+
|
|
87
|
+
# sorted args
|
|
88
|
+
args = [input_args[k] for k in sorted(input_args)]
|
|
89
|
+
|
|
90
|
+
# converts types if necessary so that all arguments have the same dtype.
|
|
91
|
+
all_dtypes = {a.dtype for a in args if isinstance(a, torch.Tensor)}
|
|
92
|
+
all_dtypes |= {
|
|
93
|
+
torch.float32 if isinstance(a, numbers.Real) else torch.complex64
|
|
94
|
+
for a in args if not isinstance(a, torch.Tensor)
|
|
95
|
+
}
|
|
96
|
+
dtype = max(all_dtypes, key=lambda d: (d.is_complex, d.itemsize))
|
|
97
|
+
if not (dtype.is_complex or dtype.is_floating_point): # if int
|
|
98
|
+
dtype = torch.float32
|
|
99
|
+
args = [
|
|
100
|
+
a.to(dtype, copy=False) if isinstance(a, torch.Tensor) else torch.tensor(a, dtype=dtype)
|
|
101
|
+
for a in args
|
|
102
|
+
]
|
|
103
|
+
|
|
104
|
+
# broadcast shape
|
|
105
|
+
try:
|
|
106
|
+
args = torch.broadcast_tensors(*args)
|
|
107
|
+
except RuntimeError as err:
|
|
108
|
+
raise CompilationError("the input tensor shapes are incompatible") from err
|
|
109
|
+
shape = args[0].shape
|
|
110
|
+
|
|
111
|
+
# cast into numpy
|
|
112
|
+
args = [a.numpy(force=True).ravel() for a in args]
|
|
113
|
+
|
|
114
|
+
# eval function
|
|
115
|
+
try:
|
|
116
|
+
out = func(*args)
|
|
117
|
+
except (RuntimeError, TypeError) as err:
|
|
118
|
+
raise CompilationError(f"failed to excecute the c func with {input_args}") from err
|
|
119
|
+
|
|
120
|
+
# cast into torch, dtype is concervative
|
|
121
|
+
if isinstance(out, tuple):
|
|
122
|
+
return tuple(torch.from_numpy(o).reshape(shape) for o in out)
|
|
123
|
+
return torch.from_numpy(out).reshape(shape)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _lambdify_c(code: str) -> callable:
|
|
127
|
+
"""Compile the C source code and import the func.
|
|
128
|
+
|
|
129
|
+
Parameters
|
|
130
|
+
----------
|
|
131
|
+
code : str
|
|
132
|
+
The C source code, output of the
|
|
133
|
+
``cutcutcodec.core.compilation.sympy_to_torch.printer._printer`` function.
|
|
134
|
+
|
|
135
|
+
Returns
|
|
136
|
+
-------
|
|
137
|
+
func : callable
|
|
138
|
+
The python callable compiled function, corresponding to the given c code.
|
|
139
|
+
|
|
140
|
+
Raises
|
|
141
|
+
------
|
|
142
|
+
cutcutcodec.core.exceptions.CompilationError
|
|
143
|
+
If gcc failed to compile the source code.
|
|
144
|
+
|
|
145
|
+
Examples
|
|
146
|
+
--------
|
|
147
|
+
>>> from pprint import pprint
|
|
148
|
+
>>> from sympy.abc import c, x
|
|
149
|
+
>>> from sympy import Number, Tuple, sin, symbols
|
|
150
|
+
>>> import numpy as np
|
|
151
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import _lambdify_c
|
|
152
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.printer import _printer
|
|
153
|
+
>>> _, _0, _1, _2, _3, _4, _5 = symbols("_ _:6")
|
|
154
|
+
>>> tree = [(_0, c**(-2)), (_1, 1/x), (_2, _0*_1), (_3, Number(0)), (_1, sin(x)), (_1, sin(_1)),
|
|
155
|
+
... (_1, _1 + 1), (_0, c), (_4, x), (_5, _2), (_, Tuple(_3, _0, c, _4, x, _2, _5, _1))]
|
|
156
|
+
>>> alloc = {_0: {c}, _1: {c}, _2: {c}, _3: {c}, _4: {c}, _5: {c}}
|
|
157
|
+
>>> args = {c, x}
|
|
158
|
+
>>> code = _printer(tree, alloc, args)
|
|
159
|
+
>>> func = _lambdify_c(code)
|
|
160
|
+
>>> pprint(func(np.array([-1.0, 1.0]), np.array([-2.0, 2.0])))
|
|
161
|
+
(array([0., 0.]),
|
|
162
|
+
array([-1., 1.]),
|
|
163
|
+
array([-1., 1.]),
|
|
164
|
+
array([-2., 2.]),
|
|
165
|
+
array([-2., 2.]),
|
|
166
|
+
array([-0.5, 0.5]),
|
|
167
|
+
array([-0.5, 0.5]),
|
|
168
|
+
array([0.21092766, 1.78907234]))
|
|
169
|
+
>>>
|
|
170
|
+
"""
|
|
171
|
+
name = f"lambdify_{uuid.uuid4().hex}"
|
|
172
|
+
filename = pathlib.Path(tempfile.gettempdir()) / f"{name}.so"
|
|
173
|
+
|
|
174
|
+
# compilation
|
|
175
|
+
comp_rules = get_compilation_rules()
|
|
176
|
+
gcc_insructions = [
|
|
177
|
+
"gcc",
|
|
178
|
+
"-o", str(filename), # output file
|
|
179
|
+
"-xc", "-", # c language, no link, from stdin
|
|
180
|
+
"-Wall", # display all warnings
|
|
181
|
+
"-pipe", # use RAM rather than tempfile
|
|
182
|
+
"-fPIC", # emit position-independent code
|
|
183
|
+
"-shared", # produce a shared object which can then be linked with other objects
|
|
184
|
+
f"-L{sys.prefix}/lib",
|
|
185
|
+
f"-I{sys.prefix}/include/python{sys.version_info.major}.{sys.version_info.minor}",
|
|
186
|
+
*(f"-D{mac_in}={mac_out}" for mac_in, mac_out in comp_rules["define_macros"]),
|
|
187
|
+
*(f"-I{inc}" for inc in comp_rules["include_dirs"]), # extra include
|
|
188
|
+
*comp_rules["extra_compile_args"],
|
|
189
|
+
]
|
|
190
|
+
try:
|
|
191
|
+
subprocess.run(
|
|
192
|
+
gcc_insructions, input=code.encode("utf-8"), check=True, capture_output=False
|
|
193
|
+
)
|
|
194
|
+
except subprocess.CalledProcessError as err:
|
|
195
|
+
raise CompilationError("failed to compile the C code with gcc", code) from err
|
|
196
|
+
|
|
197
|
+
# import
|
|
198
|
+
spec = importlib.util.spec_from_file_location("lambdify", filename)
|
|
199
|
+
module = importlib.util.module_from_spec(spec)
|
|
200
|
+
spec.loader.exec_module(module)
|
|
201
|
+
|
|
202
|
+
# clean
|
|
203
|
+
filename.unlink()
|
|
204
|
+
|
|
205
|
+
return module.lambdify
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
class Lambdify:
|
|
209
|
+
"""Convert a sympy expression into an evaluable torch function.
|
|
210
|
+
|
|
211
|
+
Attributes
|
|
212
|
+
----------
|
|
213
|
+
args : list[str]
|
|
214
|
+
The ordered names of the input arguments of this function (readonly).
|
|
215
|
+
|
|
216
|
+
Examples
|
|
217
|
+
--------
|
|
218
|
+
>>> from sympy import I, cos, exp, im, re, sqrt, sin, symbols
|
|
219
|
+
>>> from torch import linspace, tensor
|
|
220
|
+
>>> from cutcutcodec.core.compilation.sympy_to_torch.lambdify import Lambdify
|
|
221
|
+
>>>
|
|
222
|
+
>>> # case of Faucault pendulum
|
|
223
|
+
>>> # angular earth speed, latitude, gravity, pendulum length, time
|
|
224
|
+
>>> omega, theta, g, l, t = symbols("omega theta g l t", real=True)
|
|
225
|
+
>>> z0, v0 = symbols("z_0 v_0", complex=True) # initial position and speed
|
|
226
|
+
>>> w0 = sqrt(sqrt(g/l)**2 + omega**2*sin(theta)**2)
|
|
227
|
+
>>> z = exp(-I*omega*sin(theta*t)) * (
|
|
228
|
+
... z0*(cos(w0*t) + I*(omega*sin(theta)/w0)*sin(w0*t)) + (v0/w0)*sin(w0*t)
|
|
229
|
+
... )
|
|
230
|
+
>>> func = Lambdify(
|
|
231
|
+
... [z, z.diff(t)], cst_args={omega, theta, g, l}, shapes={(omega, theta, g, l), (z0, v0)}
|
|
232
|
+
... )
|
|
233
|
+
>>> print(func)
|
|
234
|
+
def lambdify(g, l, omega, t, theta, v_0, z_0):
|
|
235
|
+
# this section is cached and not compiled
|
|
236
|
+
_cst_4 = sin(theta)
|
|
237
|
+
_cst_2 = 1/l
|
|
238
|
+
_cst_2 = _cst_2*g
|
|
239
|
+
_cst_0 = _cst_4**2
|
|
240
|
+
_cst_1 = omega**2
|
|
241
|
+
_cst_0 = _cst_0*_cst_1
|
|
242
|
+
_cst_2 = _cst_0 + _cst_2
|
|
243
|
+
_cst_0 = sqrt(_cst_2)
|
|
244
|
+
_cst_2 = _cst_2**0.5
|
|
245
|
+
_cst_1 = 1/_cst_2
|
|
246
|
+
_cst_3 = I*omega
|
|
247
|
+
_cst_4 = _cst_3*_cst_4
|
|
248
|
+
# this section is not cached and compiled in C
|
|
249
|
+
_0 = _cst_0*t
|
|
250
|
+
_1 = sin(_0)
|
|
251
|
+
_2 = _1*_cst_1
|
|
252
|
+
_0 = cos(_0)
|
|
253
|
+
_3 = t*theta
|
|
254
|
+
_4 = sin(_3)
|
|
255
|
+
_4 = -_4*_cst_3
|
|
256
|
+
_4 = exp(_4)
|
|
257
|
+
_5 = _2*v_0
|
|
258
|
+
_2 = _2*_cst_4
|
|
259
|
+
_2 = _0 + _2
|
|
260
|
+
_6 = _2*z_0
|
|
261
|
+
_6 = _5 + _6
|
|
262
|
+
_6 = _4*_6
|
|
263
|
+
_5 = _0*v_0
|
|
264
|
+
_0 = _0*_cst_4
|
|
265
|
+
_1 = -_1*_cst_2
|
|
266
|
+
_0 = _0 + _1
|
|
267
|
+
_7 = _0*z_0
|
|
268
|
+
_7 = _5 + _7
|
|
269
|
+
_7 = _4*_7
|
|
270
|
+
_0 = cos(_3)
|
|
271
|
+
_5 = -_0*_6*_cst_3*theta
|
|
272
|
+
_7 = _5 + _7
|
|
273
|
+
return [_6, _7]
|
|
274
|
+
>>>
|
|
275
|
+
>>> # parameters of the pantheon pendulum in Paris
|
|
276
|
+
>>> position, speed = func(
|
|
277
|
+
... linspace(0, (23*3600+56*60+4)/4, 1_000_000), # a tour in 23h 56min 4s
|
|
278
|
+
... g=tensor(9.81), l=tensor(67.0), omega=tensor(7.292115e-5), theta=tensor(0.8524362),
|
|
279
|
+
... v_0=tensor(1+0j), z_0=tensor(0j),
|
|
280
|
+
... )
|
|
281
|
+
>>> print(position)
|
|
282
|
+
tensor([ 0.0000+0.0000e+00j, 0.0215-2.8842e-08j, 0.0431-1.1534e-07j,
|
|
283
|
+
..., -2.1698+4.9477e-05j, -2.1583+4.6581e-05j,
|
|
284
|
+
-2.1453+4.3373e-05j])
|
|
285
|
+
>>> print(speed)
|
|
286
|
+
tensor([1.0000+0.0000e+00j, 1.0000-2.6776e-06j, 0.9999-5.3531e-06j,
|
|
287
|
+
..., 0.5574-1.4082e-04j, 0.5639-1.4032e-04j,
|
|
288
|
+
0.5711-1.3967e-04j])
|
|
289
|
+
>>>
|
|
290
|
+
"""
|
|
291
|
+
|
|
292
|
+
def __init__(self, expr: Basic, **kwargs):
|
|
293
|
+
"""Initialise and create the class.
|
|
294
|
+
|
|
295
|
+
Parameters
|
|
296
|
+
----------
|
|
297
|
+
expr : sympy.core.basic.Basic
|
|
298
|
+
The sympy expression of the function.
|
|
299
|
+
cst_args : typing.Iterable[sympy.core.symbol.Symbol], optional
|
|
300
|
+
Arguments that change infrequently enough to be cached.
|
|
301
|
+
The subexpressions computed from this parameters will be cached as well.
|
|
302
|
+
If the parameters change frequently, don't specify them in ``cst_args``,
|
|
303
|
+
This will slow down the function.
|
|
304
|
+
shapes : set[frozenset[sympy.core.symbol.Symbol]], optional
|
|
305
|
+
If some parameters have the same shape, it is possible to give this information
|
|
306
|
+
in order to find a more optimal solution for limited the allocations.
|
|
307
|
+
It variable represents the set of all tensor subsets with the same shapes.
|
|
308
|
+
For example, {frozenset({a, b, c}), frozenset({x, y})} means that
|
|
309
|
+
a, b, and c are the same shape, and x and y as well.
|
|
310
|
+
compile : boolean, default=True
|
|
311
|
+
The default behavior is to translate the expression into C,
|
|
312
|
+
compile it with gcc, import the compiled version and then use this function.
|
|
313
|
+
If any of these steps fail, the calculation is performed dynamically via pytorch only.
|
|
314
|
+
If False, the function is evaluated dynamically only. No compilation is performed.
|
|
315
|
+
It's faster to instantiate the first time but it's generally slower to evaluate.
|
|
316
|
+
safe : boolean or set[sympy.core.symbol.Symbol], default=True
|
|
317
|
+
If True, the default behavior, then the tensors provided as input are not modified.
|
|
318
|
+
This helps avoid unpleasant surprises but it is slower in certain cases.
|
|
319
|
+
If False, no preventive copy is made.
|
|
320
|
+
It is the fastest but the tensors provided as input can be modified in place.
|
|
321
|
+
It is possible to be more precise by selecting only the variables to preserve,
|
|
322
|
+
in this case, provide all the variables to be preserved.
|
|
323
|
+
"""
|
|
324
|
+
# verifications
|
|
325
|
+
assert set(kwargs).issubset({"cst_args", "shapes", "compile", "safe"}), kwargs
|
|
326
|
+
if isinstance(expr, (list, tuple, set, frozenset)):
|
|
327
|
+
assert all(isinstance(e, Basic) for e in expr), expr
|
|
328
|
+
self._cast = type(expr)
|
|
329
|
+
expr = Tuple(*expr)
|
|
330
|
+
else:
|
|
331
|
+
assert isinstance(expr, Basic), expr.__class__.__name__
|
|
332
|
+
self._cast = None
|
|
333
|
+
assert not any(str(a).startswith("_") for a in expr.free_symbols), (
|
|
334
|
+
"in order to avoid a conflict with the internal autogereted vars, "
|
|
335
|
+
f"the symbols {expr.free_symbols} must not start with '_'"
|
|
336
|
+
)
|
|
337
|
+
if kwargs.get("cst_args", None) is None:
|
|
338
|
+
kwargs["cst_args"] = set()
|
|
339
|
+
else:
|
|
340
|
+
assert isinstance(kwargs["cst_args"], typing.Iterable), \
|
|
341
|
+
kwargs["cst_args"].__class__.__name__
|
|
342
|
+
kwargs["cst_args"] = set(kwargs["cst_args"])
|
|
343
|
+
assert all(isinstance(a, Symbol) for a in kwargs["cst_args"]), kwargs["cst_args"]
|
|
344
|
+
assert kwargs["cst_args"].issubset(expr.free_symbols), (
|
|
345
|
+
expr.free_symbols, kwargs["cst_args"]
|
|
346
|
+
)
|
|
347
|
+
if kwargs.get("shapes", None) is None:
|
|
348
|
+
kwargs["shapes"] = set()
|
|
349
|
+
else:
|
|
350
|
+
assert isinstance(kwargs["shapes"], typing.Iterable), kwargs["shapes"]
|
|
351
|
+
kwargs["shapes"] = list(kwargs["shapes"])
|
|
352
|
+
assert all(isinstance(s, typing.Iterable) for s in kwargs["shapes"]), kwargs["shapes"]
|
|
353
|
+
kwargs["shapes"] = {frozenset(s) for s in kwargs["shapes"]}
|
|
354
|
+
assert all(isinstance(v, Symbol) for s in kwargs["shapes"] for v in s), kwargs["shapes"]
|
|
355
|
+
assert all(s.issubset(expr.free_symbols) for s in kwargs["shapes"]), (
|
|
356
|
+
expr.free_symbols, kwargs["shapes"]
|
|
357
|
+
)
|
|
358
|
+
if "compile" in kwargs:
|
|
359
|
+
assert isinstance(kwargs["compile"], bool), kwargs["compile"].__class__.__name__
|
|
360
|
+
if kwargs.get("safe", None) in {None, True}:
|
|
361
|
+
kwargs["safe"] = expr.free_symbols
|
|
362
|
+
elif kwargs["safe"] is False:
|
|
363
|
+
kwargs["safe"] = set()
|
|
364
|
+
else:
|
|
365
|
+
assert isinstance(kwargs["safe"], typing.Iterable), kwargs["safe"]
|
|
366
|
+
kwargs["safe"] = set(kwargs["safe"])
|
|
367
|
+
assert all(isinstance(s, Symbol) for s in kwargs["safe"]), kwargs["safe"]
|
|
368
|
+
assert kwargs["safe"].issubset(expr.free_symbols), (expr.free_symbols, kwargs["safe"])
|
|
369
|
+
|
|
370
|
+
# internal attributes
|
|
371
|
+
self._tree = {"expr": expr, "shapes": kwargs["shapes"]}
|
|
372
|
+
self._cst_cache = None
|
|
373
|
+
|
|
374
|
+
# preprocessing
|
|
375
|
+
self._tree |= preprocess(
|
|
376
|
+
evalf(expr), cst_args=kwargs["cst_args"], shapes=kwargs["shapes"], safe=kwargs["safe"]
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
# args
|
|
380
|
+
self._tree["args_str"] = set(
|
|
381
|
+
map(
|
|
382
|
+
str,
|
|
383
|
+
(
|
|
384
|
+
self._tree["cst_args"]
|
|
385
|
+
| {a for a in self._tree["dyn_args"] if not str(a).startswith("_")}
|
|
386
|
+
)
|
|
387
|
+
)
|
|
388
|
+
)
|
|
389
|
+
|
|
390
|
+
# kernel compiled function
|
|
391
|
+
self._tree["dyn_code"] = None
|
|
392
|
+
self._tree["dyn_func"] = None
|
|
393
|
+
if (
|
|
394
|
+
kwargs.get("compile", True)
|
|
395
|
+
and (
|
|
396
|
+
len(self._tree["dyn_tree"]) != 1
|
|
397
|
+
or self._tree["dyn_tree"][0][0] != self._tree["dyn_tree"][0][1]
|
|
398
|
+
)
|
|
399
|
+
and self._tree["dyn_args"]
|
|
400
|
+
): # if compilation is required
|
|
401
|
+
try:
|
|
402
|
+
self._tree["dyn_code"] = _printer(
|
|
403
|
+
self._tree["dyn_tree"], self._tree["dyn_alloc"], self._tree["dyn_args"]
|
|
404
|
+
)
|
|
405
|
+
self._tree["dyn_func"] = _lambdify_c(self._tree["dyn_code"])
|
|
406
|
+
except CompilationError as err:
|
|
407
|
+
logging.warning("failed to compile in C because %s", err)
|
|
408
|
+
|
|
409
|
+
def __str__(self):
|
|
410
|
+
"""Retunr a nice printable representation of the function."""
|
|
411
|
+
code = []
|
|
412
|
+
|
|
413
|
+
# args
|
|
414
|
+
code.append(f"def lambdify({', '.join(self.args)}):")
|
|
415
|
+
|
|
416
|
+
# cst tree
|
|
417
|
+
if self._tree["cst_tree"][-1][1]:
|
|
418
|
+
code.append(" # this section is cached and not compiled")
|
|
419
|
+
for symb, expr in self._tree["cst_tree"][:-1]:
|
|
420
|
+
if expr.is_symbol:
|
|
421
|
+
code.append(f" {symb} = {expr}.clone()")
|
|
422
|
+
else:
|
|
423
|
+
code.append(f" {symb} = {expr}")
|
|
424
|
+
|
|
425
|
+
# dyn tree
|
|
426
|
+
if self._tree["dyn_func"] is not None:
|
|
427
|
+
code.append(" # this section is not cached and compiled in C")
|
|
428
|
+
else:
|
|
429
|
+
code.append(" # this section is not cached and not compiled")
|
|
430
|
+
for symb, expr in (
|
|
431
|
+
self._tree["dyn_tree"] if self._cast is None else self._tree["dyn_tree"][:-1]
|
|
432
|
+
):
|
|
433
|
+
if expr.is_symbol:
|
|
434
|
+
code.append(f" {symb} = {expr}.clone()")
|
|
435
|
+
else:
|
|
436
|
+
code.append(f" {symb} = {expr}")
|
|
437
|
+
if self._cast is None:
|
|
438
|
+
code.append(f" return {self._tree['dyn_tree'][-1][0]}")
|
|
439
|
+
else:
|
|
440
|
+
code.append(f" return {self._cast(self._tree['dyn_tree'][-1][1])}")
|
|
441
|
+
return "\n".join(code)
|
|
442
|
+
|
|
443
|
+
def __call__(
|
|
444
|
+
self, *args: torch.Tensor, **kwargs: dict[str, torch.Tensor]
|
|
445
|
+
) -> typing.Union[torch.Tensor, typing.Iterable[torch.Tensor]]:
|
|
446
|
+
"""Evaluate the expression and return the numerical result.
|
|
447
|
+
|
|
448
|
+
Parameters
|
|
449
|
+
----------
|
|
450
|
+
*args : tuple
|
|
451
|
+
The numerical value of the symbol in the expression.
|
|
452
|
+
You can name the argument using ``**kwargs``.
|
|
453
|
+
**kwargs : dict
|
|
454
|
+
To each variable name present in the expression, associate the numerical value.
|
|
455
|
+
You don't have to name the argument, you can use ``*arg``.
|
|
456
|
+
|
|
457
|
+
Returns
|
|
458
|
+
-------
|
|
459
|
+
result
|
|
460
|
+
The numerical value of the expression evaluated with the given input parameters.
|
|
461
|
+
"""
|
|
462
|
+
# get all requiered args and named it
|
|
463
|
+
input_args = {}
|
|
464
|
+
for arg_name, value in kwargs.items():
|
|
465
|
+
if arg_name in self._tree["args_str"]:
|
|
466
|
+
input_args[arg_name] = value
|
|
467
|
+
else:
|
|
468
|
+
logging.warning("the argument %s is provided but not used", arg_name)
|
|
469
|
+
left = self._tree["args_str"] - input_args.keys()
|
|
470
|
+
if len(args) > len(left):
|
|
471
|
+
logging.warning("the %d last arguments are ignored", len(args)-len(left))
|
|
472
|
+
for i, arg_name in enumerate(sorted(left)):
|
|
473
|
+
try:
|
|
474
|
+
input_args[arg_name] = args[i]
|
|
475
|
+
except IndexError as err:
|
|
476
|
+
raise ValueError(f"the argument {arg_name} is missing") from err
|
|
477
|
+
|
|
478
|
+
# verification type
|
|
479
|
+
assert all(isinstance(a, torch.Tensor) for a in input_args.values()), \
|
|
480
|
+
f"the arguments can only be a torch tensor, given args are {input_args}"
|
|
481
|
+
|
|
482
|
+
# compute the expression
|
|
483
|
+
out = self.forward(**input_args)
|
|
484
|
+
|
|
485
|
+
# number/tensor/iterable cast
|
|
486
|
+
if self._cast is None:
|
|
487
|
+
if isinstance(out, numbers.Real):
|
|
488
|
+
return torch.tensor(out, dtype=torch.float64)
|
|
489
|
+
if isinstance(out, numbers.Complex):
|
|
490
|
+
return torch.tensor(out, dtype=torch.complex128)
|
|
491
|
+
return out
|
|
492
|
+
return self._cast(
|
|
493
|
+
o
|
|
494
|
+
if isinstance(o, torch.Tensor) else
|
|
495
|
+
(
|
|
496
|
+
torch.tensor(o, dtype=torch.float64)
|
|
497
|
+
if isinstance(o, numbers.Real) else
|
|
498
|
+
torch.tensor(o, dtype=torch.complex128)
|
|
499
|
+
)
|
|
500
|
+
for o in out
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
def _cst_tree_func(
|
|
504
|
+
self, input_args: typing.Union[numbers.Number, torch.Tensor]
|
|
505
|
+
) -> tuple[torch.Tensor]:
|
|
506
|
+
"""Dynamic evaluation of the constant tree.
|
|
507
|
+
|
|
508
|
+
This function is cached once.
|
|
509
|
+
|
|
510
|
+
Parameters
|
|
511
|
+
----------
|
|
512
|
+
input_args : dict[str, torch.Tensor]
|
|
513
|
+
For each symbol name present in the original equation, associate the numerical tensor.
|
|
514
|
+
|
|
515
|
+
Returns
|
|
516
|
+
-------
|
|
517
|
+
tuple[torch.Tensor]
|
|
518
|
+
The differents usefull constants for the compiled main function.
|
|
519
|
+
"""
|
|
520
|
+
# case no args for optimisation
|
|
521
|
+
if not self._tree["cst_args"]:
|
|
522
|
+
if self._cst_cache is None:
|
|
523
|
+
buff = {}
|
|
524
|
+
out = ()
|
|
525
|
+
for new_var, expr in self._tree["cst_tree"]:
|
|
526
|
+
out = _dyn_eval(expr, buff, str(new_var))
|
|
527
|
+
self._cst_cache = out
|
|
528
|
+
return self._cst_cache
|
|
529
|
+
|
|
530
|
+
# compute args hash
|
|
531
|
+
hash_compactor = hashlib.md5(usedforsecurity=False)
|
|
532
|
+
for arg in sorted(map(str, self._tree["cst_args"])):
|
|
533
|
+
hash_compactor.update(str(input_args[arg].dtype).encode())
|
|
534
|
+
hash_compactor.update(str(input_args[arg].shape).encode())
|
|
535
|
+
hash_compactor.update(input_args[arg].numpy(force=True))
|
|
536
|
+
args_hash = hash_compactor.digest()
|
|
537
|
+
|
|
538
|
+
# compute the constant part of the expression if it is not already done
|
|
539
|
+
if self._cst_cache is None or self._cst_cache[0] != args_hash:
|
|
540
|
+
# dynamic evaluation
|
|
541
|
+
input_args = input_args.copy() # to prevent changing hyperparameter in place
|
|
542
|
+
out = ()
|
|
543
|
+
for new_var, expr in self._tree["cst_tree"]:
|
|
544
|
+
out = _dyn_eval(expr, input_args, str(new_var))
|
|
545
|
+
self._cst_cache = (args_hash, out)
|
|
546
|
+
|
|
547
|
+
# restitution
|
|
548
|
+
return self._cst_cache[1]
|
|
549
|
+
|
|
550
|
+
def _dyn_tree_func(
|
|
551
|
+
self, dyn_args: typing.Union[numbers.Number, torch.Tensor]
|
|
552
|
+
) -> typing.Union[numbers.Number, torch.Tensor, tuple[torch.Tensor]]:
|
|
553
|
+
"""Evaluate the dynamic tree.
|
|
554
|
+
|
|
555
|
+
This function is not cached
|
|
556
|
+
|
|
557
|
+
Parameters
|
|
558
|
+
----------
|
|
559
|
+
dyn_args : dict[str, torch.Tensor]
|
|
560
|
+
For each symbol name present in the dynamic tree as argument,
|
|
561
|
+
associate the numerical tensor. Only the usefull arguments had to be present, not more.
|
|
562
|
+
|
|
563
|
+
Returns
|
|
564
|
+
-------
|
|
565
|
+
tuple[torch.Tensor]
|
|
566
|
+
The result of the dynamic tree.
|
|
567
|
+
"""
|
|
568
|
+
out = None
|
|
569
|
+
if self._tree["dyn_func"]:
|
|
570
|
+
try:
|
|
571
|
+
out = _cast_lambdify_c(self._tree["dyn_func"], dyn_args)
|
|
572
|
+
except CompilationError as err:
|
|
573
|
+
logging.warning(
|
|
574
|
+
"failed to eval the C code with the args %s because %s", dyn_args, err
|
|
575
|
+
)
|
|
576
|
+
for symb, expr in self._tree["dyn_tree"]:
|
|
577
|
+
out = _dyn_eval(expr, dyn_args, str(symb))
|
|
578
|
+
else:
|
|
579
|
+
for symb, expr in self._tree["dyn_tree"]:
|
|
580
|
+
out = _dyn_eval(expr, dyn_args, str(symb))
|
|
581
|
+
return out
|
|
582
|
+
|
|
583
|
+
@property
|
|
584
|
+
def args(self) -> list[str]:
|
|
585
|
+
"""Return the ordered names of the input arguments of this function."""
|
|
586
|
+
return sorted(self._tree["args_str"])
|
|
587
|
+
|
|
588
|
+
def forward(
|
|
589
|
+
self, **input_args: dict[str, typing.Union[numbers.Number, torch.Tensor]]
|
|
590
|
+
) -> typing.Union[numbers.Number, torch.Tensor, tuple[torch.Tensor]]:
|
|
591
|
+
"""Fast evaluation of the expression.
|
|
592
|
+
|
|
593
|
+
No casts and verifications are performed here.
|
|
594
|
+
For more flexible and safer use, please use the
|
|
595
|
+
``cutcutcodec.core.compilation.sympy_to_torch.lambdify.Lambify.__call__`` function.
|
|
596
|
+
|
|
597
|
+
Parameters
|
|
598
|
+
----------
|
|
599
|
+
**input_args : dict[str]
|
|
600
|
+
To each variable name present in the expression, associate the numerical value.
|
|
601
|
+
All arguments have to be provided.
|
|
602
|
+
|
|
603
|
+
Returns
|
|
604
|
+
-------
|
|
605
|
+
result
|
|
606
|
+
The direct result of the underground function with compute the expression.
|
|
607
|
+
"""
|
|
608
|
+
cst_out = self._cst_tree_func(input_args)
|
|
609
|
+
cst_args = dict(zip(map(str, self._tree["cst_tree"][-1][1]), cst_out))
|
|
610
|
+
dyn_args = input_args | cst_args # order cst_args takes priority
|
|
611
|
+
dyn_args = {str(s): dyn_args[str(s)] for s in self._tree["dyn_args"]}
|
|
612
|
+
return self._dyn_tree_func(dyn_args)
|