WebAssembly / wasi-nn

Neural Network proposal for WASI
458 stars 35 forks source link

Question about alignment #27

Open yamt opened 2 years ago

yamt commented 2 years ago

tensor-data is list<u8>. but i suppose that its actual contents are eg. f32. does wit/interface-types/etc provide a large enough alignment?

yamt commented 2 years ago

eg. https://github.com/bytecodealliance/wasmtime/blob/0284049e28f0655946b9f89b40f7480e60fecc45/crates/wasi-nn/examples/classification-example/src/main.rs#L42-L50

abrown commented 2 years ago

does wit/interface-types/etc provide a large enough alignment?

I guess my question would "large enough" for what? There's a couple of directions this could go but I'm not exactly sure what you mean?

yamt commented 2 years ago

does wit/interface-types/etc provide a large enough alignment?

I guess my question would "large enough" for what? There's a couple of directions this could go but I'm not exactly sure what you mean?

i meant large enough for the actual contents. eg. f32. (i expect no alignment for u8 and 4 byte alignment for f32.)

geekbeast commented 1 year ago

WITX is a view over guest slice, WIT is a copy. Alignment has the potential to be an issue, but everything will likely work fine on Linux and Windows due to the default behavior of allocator (i.e if it uses malloc).

On Ubuntu, many other linux systems malloc, and windows returns a pointer aligned to either 8 or 16 bytes:

So whether alignment will be an issue will depend on the allocator used by the wasm host and the underlying platform.

geekbeast commented 1 year ago

Additional quick follow up here is that most C compilers adhering to the standard allow casting between any valid pointer types, so everything should be fine unless there's a use case where alignment greater than 16 bytes is required (in some case 8 bytes emscripten-core/emscripten#5996 and emscripten-core/emscripten#10072).

This mainly seems to be AVX or AVX512, but if the use case does present itself it can be handled on host side.