@@ -28,6 +28,22 @@ set(LLAMA_BUILD_HTML OFF CACHE BOOL "" FORCE)
2828set (LLAMA_WASM_SINGLE_FILE OFF CACHE BOOL "" FORCE )
2929set (LLAMA_WASM_MEM64 OFF CACHE BOOL "" FORCE )
3030
31+ option (LLAMADART_WEBGPU_MEM64 "Build WebGPU bridge core with wasm64/memory64" OFF )
32+ set (LLAMADART_WEBGPU_MEM64_MAX_MEMORY "12884901888" CACHE STRING "Max wasm64 linear memory in bytes" )
33+ option (LLAMADART_WEBGPU_PTHREADS "Enable pthread support for bridge runtime" ON )
34+ set (LLAMADART_WEBGPU_PTHREAD_POOL_SIZE "2" CACHE STRING "PThread pool size for bridge runtime" )
35+
36+ if (LLAMADART_WEBGPU_PTHREADS)
37+ add_compile_options ("-pthread" )
38+ add_link_options ("-pthread" )
39+ endif ()
40+
41+ if (LLAMADART_WEBGPU_MEM64)
42+ set (LLAMA_WASM_MEM64 ON CACHE BOOL "" FORCE )
43+ add_compile_options ("-sMEMORY64=1" )
44+ add_link_options ("-sMEMORY64=1" "-sWASM_BIGINT=1" )
45+ endif ()
46+
3147set (GGML_NATIVE OFF CACHE BOOL "" FORCE )
3248set (GGML_OPENMP OFF CACHE BOOL "" FORCE )
3349set (GGML_WEBGPU ON CACHE BOOL "" FORCE )
@@ -103,19 +119,48 @@ target_include_directories(llama_webgpu_core PRIVATE
103119
104120target_link_libraries (llama_webgpu_core PRIVATE llama llamadart_mtmd )
105121
106- target_link_options ( llama_webgpu_core PRIVATE
122+ set (LLAMADART_WEBGPU_LINK_OPTIONS
107123 "-sALLOW_MEMORY_GROWTH=1"
124+ "-sWASMFS=1"
125+ "-sASYNCIFY=1"
126+ "-sFORCE_FILESYSTEM=1"
108127 "-sASSERTIONS=1"
109- "-sMIN_SAFARI_VERSION=170400"
110128 "-sMODULARIZE=1"
111129 "-sEXPORT_ES6=1"
112130 "-sEXPORT_NAME=createLlamaWebGpuCoreModule"
113131 "-sENVIRONMENT=web,worker"
114132 "-sEXPORTED_RUNTIME_METHODS=['FS','ccall','UTF8ToString']"
115- "-sEXPORTED_FUNCTIONS=['_main','_llamadart_webgpu_probe','_llamadart_webgpu_backends_json','_llamadart_webgpu_last_error','_llamadart_webgpu_set_log_level','_llamadart_webgpu_load_model','_llamadart_webgpu_mmproj_load','_llamadart_webgpu_mmproj_free','_llamadart_webgpu_mmproj_supports_vision','_llamadart_webgpu_mmproj_supports_audio','_llamadart_webgpu_media_clear_pending','_llamadart_webgpu_media_add_file','_llamadart_webgpu_media_add_encoded','_llamadart_webgpu_media_add_rgb','_llamadart_webgpu_media_add_audio_f32','_llamadart_webgpu_tokenize_to_json','_llamadart_webgpu_last_tokens_json','_llamadart_webgpu_detokenize_from_json','_llamadart_webgpu_last_detokenized','_llamadart_webgpu_generate','_llamadart_webgpu_begin_generation','_llamadart_webgpu_next_token','_llamadart_webgpu_last_piece','_llamadart_webgpu_end_generation','_llamadart_webgpu_request_cancel','_llamadart_webgpu_last_output','_llamadart_webgpu_get_context_size','_llamadart_webgpu_model_meta_json','_llamadart_webgpu_shutdown']"
133+ "-sEXPORTED_FUNCTIONS=['_main','_llamadart_webgpu_probe','_llamadart_webgpu_backends_json','_llamadart_webgpu_last_error','_llamadart_webgpu_set_log_level','_llamadart_webgpu_load_model','_llamadart_webgpu_load_model_from_url','_llamadart_webgpu_mmproj_load','_llamadart_webgpu_mmproj_free','_llamadart_webgpu_mmproj_supports_vision','_llamadart_webgpu_mmproj_supports_audio','_llamadart_webgpu_media_clear_pending','_llamadart_webgpu_media_add_file','_llamadart_webgpu_media_add_encoded','_llamadart_webgpu_media_add_rgb','_llamadart_webgpu_media_add_audio_f32','_llamadart_webgpu_tokenize_to_json','_llamadart_webgpu_last_tokens_json','_llamadart_webgpu_detokenize_from_json','_llamadart_webgpu_last_detokenized','_llamadart_webgpu_generate','_llamadart_webgpu_begin_generation','_llamadart_webgpu_next_token','_llamadart_webgpu_last_piece','_llamadart_webgpu_end_generation','_llamadart_webgpu_request_cancel','_llamadart_webgpu_last_output','_llamadart_webgpu_get_context_size','_llamadart_webgpu_model_meta_json','_llamadart_webgpu_shutdown']"
134+ "-lwasmfs_fetch.js"
116135)
117136
137+ if (LLAMADART_WEBGPU_PTHREADS)
138+ list (APPEND LLAMADART_WEBGPU_LINK_OPTIONS
139+ "-sPTHREAD_POOL_SIZE=${LLAMADART_WEBGPU_PTHREAD_POOL_SIZE} "
140+ )
141+ endif ()
142+
143+ if (LLAMADART_WEBGPU_MEM64)
144+ list (APPEND LLAMADART_WEBGPU_LINK_OPTIONS
145+ "-sMEMORY64=1"
146+ "-sWASM_BIGINT=1"
147+ "-sMAXIMUM_MEMORY=${LLAMADART_WEBGPU_MEM64_MAX_MEMORY} "
148+ )
149+ else ()
150+ list (APPEND LLAMADART_WEBGPU_LINK_OPTIONS
151+ "-sMIN_SAFARI_VERSION=170400"
152+ "-sMAXIMUM_MEMORY=4294967296"
153+ )
154+ endif ()
155+
156+ target_link_options (llama_webgpu_core PRIVATE ${LLAMADART_WEBGPU_LINK_OPTIONS} )
157+
158+ set (LLAMADART_WEBGPU_CORE_OUTPUT_NAME "llama_webgpu_core" )
159+ if (LLAMADART_WEBGPU_MEM64)
160+ set (LLAMADART_WEBGPU_CORE_OUTPUT_NAME "llama_webgpu_core_mem64" )
161+ endif ()
162+
118163set_target_properties (llama_webgpu_core PROPERTIES
119- OUTPUT_NAME "llama_webgpu_core "
164+ OUTPUT_NAME "${LLAMADART_WEBGPU_CORE_OUTPUT_NAME} "
120165 SUFFIX ".js"
121166)
0 commit comments