mirror of
https://github.com/zebrajr/pytorch.git
synced 2026-01-15 12:15:51 +00:00
This PR addresses compilation errors encountered when building PyTorch with emcc (WebAssembly).
```
/home/ice/pytorch_wasm/aten/src/ATen/core/TensorBase.h:344:31: error: use of overloaded operator '*' is ambiguous (with
operand types 'c10::SymInt' and 'size_t' (aka 'unsigned long'))
344 | return impl_->sym_numel() * impl_->itemsize();
| ~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~
```
Ambiguous SymInt Operator Overloads: On Wasm32 architecture, size_t is 32-bit. This causes ambiguous operator overload errors when performing arithmetic between c10::SymInt and size_t (e.g., impl_->itemsize()), as the compiler cannot determine the correct implicit conversion. This patch resolves the ambiguity by explicitly casting the operands to c10::SymInt.
You can use the following script to compile libtorch
```
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
cd ..
git clone https://github.com/futz12/pytorch_wasm
cd pytorch_wasm
git submodule sync && git submodule update --init --recursive
cmake -Bbuild_host \
-DBUILD_CUSTOM_PROTOBUF=ON \
-DPROTOBUF_PROTOC_EXECUTABLE="" \
-DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=""
cmake --build build_host --config Release -j4 --target protoc
sudo cp ./build_host/bin/protoc* /usr/local/bin/
rm -rf build_host
emcmake cmake -Bbuild_wasm \
-DCMAKE_BUILD_TYPE=Release \
-DUSE_KINETO=OFF \
-DUSE_CUDA=OFF \
-DUSE_MPI=OFF \
-DUSE_OPENMP=OFF \
-DUSE_MKLDNN=OFF \
-DBUILD_TEST=OFF \
-DBUILD_BINARY=OFF \
-DBUILD_SHARED_LIBS=OFF \
-DCAFFE2_CUSTOM_PROTOC_EXECUTABLE=/usr/local/bin/protoc \
-DPROTOBUF_PROTOC_EXECUTABLE=/usr/local/bin/protoc \
-DONNX_CUSTOM_PROTOC_EXECUTABLE=/usr/local/bin/protoc
cmake --build build_wasm --config Release -j4 --target torch_cpu c10
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/171464
Approved by: https://github.com/ezyang
ATen Core
ATen Core is a minimal subset of ATen which is suitable for deployment on mobile. Binary size of files in this folder is an important constraint.