2024-07-27 tvmのインストールしてみる
#2024 #202407 #nix
tvmを試してみる。tvmの使い方もわかっていないがnixでインストールしていく。
してみた結果。tvmの共有ライブラリのインストールまでがされる。python環境は自分で導入していくことになるのだろう。
https://github.com/NixOS/nixpkgs/blob/nixos-24.05/pkgs/development/compilers/tvm/default.nix#L25
https://github.com/apache/tvm
https://tvm.apache.org/
nixで入れても最新リリースがインストールされそう。リポジトリ名が incubator-tvm から tvm へ変更されていることがわかる。
nix-shellで試してみる。
nix-shell -p tvm
code: shell
/nix/store/6k8b8b1rfgw7plwvy55jw5snl7zq4jv0-tvm-0.16.0/
├── include
│   └── tvm
│   └── runtime
│   ├── builtin_fp16.h
│   ├── c_backend_api.h
│   ├── c_runtime_api.h
│   ├── container
│   │   ├── adt.h
│   │   ├── array.h
│   │   ├── base.h
│   │   ├── closure.h
│   │   ├── map.h
│   │   ├── optional.h
│   │   ├── shape_tuple.h
│   │   ├── string.h
│   │   └── variant.h
│   ├── contrib
│   │   ├── libtorch_runtime.h
│   │   └── papi.h
│   ├── crt
│   │   ├── aot_executor.h
│   │   ├── aot_executor_module.h
│   │   ├── crt.h
│   │   ├── error_codes.h
│   │   ├── func_registry.h
│   │   ├── graph_executor.h
│   │   ├── graph_executor_module.h
│   │   ├── logging.h
│   │   ├── microtvm_rpc_server.h
│   │   ├── module.h
│   │   ├── packed_func.h
│   │   ├── page_allocator.h
│   │   ├── platform.h
│   │   ├── rpc_common
│   │   │   ├── frame_buffer.h
│   │   │   ├── framing.h
│   │   │   ├── session.h
│   │   │   └── write_stream.h
│   │   └── stack_allocator.h
│   ├── data_type.h
│   ├── debug.h
│   ├── device_api.h
│   ├── disco
│   │   ├── builtin.h
│   │   ├── cuda_ipc_memory.h
│   │   ├── disco_worker.h
│   │   └── session.h
│   ├── executor_info.h
│   ├── logging.h
│   ├── memory
│   │   └── memory_manager.h
│   ├── memory.h
│   ├── metadata.h
│   ├── metadata_base.h
│   ├── metadata_types.h
│   ├── micro
│   │   └── standalone
│   │   └── microtvm_runtime.h
│   ├── module.h
│   ├── name_transforms.h
│   ├── ndarray.h
│   ├── nvtx.h
│   ├── object.h
│   ├── packed_func.h
│   ├── profiling.h
│   ├── registry.h
│   ├── relax_vm
│   │   ├── builtin.h
│   │   ├── bytecode.h
│   │   ├── executable.h
│   │   ├── ndarray_cache_support.h
│   │   └── vm.h
│   ├── serializer.h
│   ├── threading_backend.h
│   └── vm
│   ├── bytecode.h
│   ├── executable.h
│   └── vm.h
└── lib
├── cmake
│   └── tvm
│   ├── tvmConfig.cmake
│   ├── tvmTargets-release.cmake
│   └── tvmTargets.cmake
├── libtvm.dylib
└── libtvm_runtime.dylib
17 directories, 70 files
dylibとヘッダーが入ることがわかる。cmakeはよくしらないので、必要になる理由がよくわかっていない。少なくともCレイヤーでのアクセスができるようになりそうである。
derivatonの確認
code: tvm.drv
{
"/nix/store/65469xixcl801x0lpddr3d9316i10zv2-tvm-0.16.0.drv": {
"args": [
"-e",
"/nix/store/v6x3cs394jgqfbi0a42pam708flxaphh-default-builder.sh"
],
"builder": "/nix/store/s30jrpgav677fpc9yvkqsib70xfmx7xi-bash-5.2p26/bin/bash",
"env": {
"__darwinAllowLocalNetworking": "",
"__impureHostDeps": "/bin/sh /usr/lib/libSystem.B.dylib /usr/lib/system/libunc.dylib /dev/zero /dev/random /dev/urandom /bin/sh",
"__propagatedImpureHostDeps": "",
"__propagatedSandboxProfile": "",
"__sandboxProfile": "",
"__structuredAttrs": "",
"buildInputs": "",
"builder": "/nix/store/s30jrpgav677fpc9yvkqsib70xfmx7xi-bash-5.2p26/bin/bash",
"cmakeFlags": "",
"configureFlags": "",
"depsBuildBuild": "",
"depsBuildBuildPropagated": "",
"depsBuildTarget": "",
"depsBuildTargetPropagated": "",
"depsHostHost": "",
"depsHostHostPropagated": "",
"depsTargetTarget": "",
"depsTargetTargetPropagated": "",
"doCheck": "",
"doInstallCheck": "",
"dontFixCmake": "1",
"mesonFlags": "",
"name": "tvm-0.16.0",
"nativeBuildInputs": "/nix/store/6n71i6is8mfi9qhy4zyxchlxf43mkhw3-cmake-3.29.3",
"out": "/nix/store/6k8b8b1rfgw7plwvy55jw5snl7zq4jv0-tvm-0.16.0",
"outputs": "out",
"patches": "",
"pname": "tvm",
"propagatedBuildInputs": "",
"propagatedNativeBuildInputs": "",
"src": "/nix/store/sx83dbqkjyprrg0ss9vyciadl6ml6aqc-source",
"stdenv": "/nix/store/ashgigyp2dkvfi1spyl9nsnirvbk67vh-stdenv-darwin",
"strictDeps": "",
"system": "aarch64-darwin",
"version": "0.16.0"
},
"inputDrvs": {
"/nix/store/69vfg4ycyakl6i031inff7g92vwanf5l-cmake-3.29.3.drv": {
"dynamicOutputs": {},
"outputs": [
"out"
]
},
"/nix/store/c9ljadwlv68drn81kk2a1wq9pdrr7xgk-bash-5.2p26.drv": {
"dynamicOutputs": {},
"outputs": [
"out"
]
},
"/nix/store/kcfl5j6vny7bj49v3mib35rs37z80n3i-stdenv-darwin.drv": {
"dynamicOutputs": {},
"outputs": [
"out"
]
},
"/nix/store/nyckb3dys5ziivyiavbk2smnzjrs21a9-source.drv": {
"dynamicOutputs": {},
"outputs": [
"out"
]
}
},
"inputSrcs": [
"/nix/store/v6x3cs394jgqfbi0a42pam708flxaphh-default-builder.sh"
],
"name": "tvm-0.16.0",
"outputs": {
"out": {
"path": "/nix/store/6k8b8b1rfgw7plwvy55jw5snl7zq4jv0-tvm-0.16.0"
}
},
"system": "aarch64-darwin"
}
}
cmakeが必要で、一般的な環境でビルドができることがおおよそわかる。
公式的にはPythonで使うのを想定していそう。
https://tvm.apache.org/docs/reference/api/python/index.html
0.16.0が最新かと思いきや先週0.17がでている
https://github.com/apache/tvm/releases/tag/v0.17.0
おそらくbuilding-the-shared-libraryまでできている状態になるので、python環境を用意すれば使えるようになりそう。
https://tvm.apache.org/docs/install/from_source.html#build-the-shared-library
pythonは3.9以降は使えない模様
nixのstableではpython3.8はすでにインストールできない。
devenvではpythonのバージョンの指定ができるのでこちらですすめる。
バージョン確認
https://www.python.org/downloads/
3.8.19が3.8の最新
languages.pythonで使えるoptionの確認
https://github.com/cachix/devenv/blob/main/src/modules/languages/python.nix#L157-L313
nixpkgs-pythonが必要そうなので、設定
code: nixenv.yaml
inputs:
nixpkgs:
url: github:cachix/devenv-nixpkgs/rolling
nixpkgs-python:
url: github:cachix/nixpkgs-python
devenv inputs add nixpkgs-python github:cachix/nixpkgs-python --follows nixpkgs としてもいいようだ。
code: sh
uv venv
uv pip install numpy decorator attrs typing-extensions psutil scipy tornado psutil 'xgboost>=1.1.0' cloudpickle
ここまできて、tvmのpythonモジュールが配布されてないことがわかる。
もうひと工夫必要そう。
code: sh
export TVM_HOME=/path/to/tvm
export PYTHONPATH=$TVM_HOME/python:${PYTHONPATH}
とすればよいようなので、pythonディレクトリをコピーしつつPATHを通せばよさそう