Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ build-direct/
dist/
arm-scratch/
executorch.egg-info

# MLX delegate code generated in-tree from backends/mlx/serialization/schema.fbs
# by generate.py, which the MLX backend runs at build time.
backends/mlx/_generated_inspector.py
backends/mlx/runtime/MLXLoader.cpp
backends/mlx/runtime/MLXLoader.h
backends/mlx/runtime/schema_generated.h
backends/mlx/serialization/_generated/
backends/mlx/serialization/_generated_serializers.py
backends/mlx/serialization/mlx_graph_schema.py
pip-out/
build-profiling/
**/ddr_*_temp
Expand Down
3 changes: 3 additions & 0 deletions .lintrunner.toml
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ exclude_patterns = [
'extension/llm/tokenizers',
'extension/llm/tokenizers/**',
'examples/llm_server',
# muse_glimmer is a symlink to this directory; the linter resolves it and
# then fails on a directory.
'examples/models/muse-glimmer',
'backends/cadence/utils/FACTO',
'examples/cuda',
'examples/qualcomm',
Expand Down
15 changes: 11 additions & 4 deletions backends/cuda/runtime/cuda_mutable_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,17 @@ Error mutable_state_rebind_for_execute(CudaDelegateHandle* handle) {
return Error::Ok;
}
auto cit = m.contexts.find(hit->second);
if (cit != m.contexts.end() &&
(!cit->second.sessions.empty() ||
cit->second.rebound_handles.find(handle) !=
cit->second.rebound_handles.end())) {
if (cit != m.contexts.end()) {
const auto desc = cit->second.desc.find(handle);
const bool known_stateless =
desc != cit->second.desc.end() && desc->second.empty();
const bool requires_active_session =
(!known_stateless && !cit->second.sessions.empty()) ||
cit->second.rebound_handles.find(handle) !=
cit->second.rebound_handles.end();
if (!requires_active_session) {
return Error::Ok;
}
ET_LOG(
Error, "mutable_state: active session is required for this handle");
return Error::InvalidState;
Expand Down
9 changes: 8 additions & 1 deletion backends/cuda/runtime/test/test_cuda_mutable_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,17 @@ TEST(CudaMutableStateTest, CapturesClonesAndRebindsDeviceBuffer) {
container.fqns = {"model.state"};
container.extracted["model.state"] =
reinterpret_cast<aoti::AtenTensorHandle>(source_tensor.get());
FakeContainer stateless_container;
cu::CudaDelegateHandle handle = fake_container_handle(&container);
cu::CudaDelegateHandle stateless_handle =
fake_container_handle(&stateless_container);

cu::MutableStateContextOwner c;
c.register_fqns({"model.state"});
c.with_load_scope([&] { cu::mutable_state_note_handle(&handle); });
c.with_load_scope([&] {
cu::mutable_state_note_handle(&handle);
cu::mutable_state_note_handle(&stateless_handle);
});

ASSERT_TRUE(c.available());
EXPECT_EQ(c.bytes_per_session(), 4 * sizeof(float));
Expand All @@ -387,6 +393,7 @@ TEST(CudaMutableStateTest, CapturesClonesAndRebindsDeviceBuffer) {

auto token = c.create_session();
ASSERT_TRUE(token.ok());
EXPECT_EQ(cu::mutable_state_rebind_for_execute(&stateless_handle), Error::Ok);
EXPECT_EQ(cu::mutable_state_rebind_for_execute(&handle), Error::InvalidState);

ASSERT_EQ(
Expand Down
2 changes: 2 additions & 0 deletions examples/models/muse-glimmer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.pyc
188 changes: 188 additions & 0 deletions examples/models/muse-glimmer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

cmake_minimum_required(VERSION 3.24)
project(muse_glimmer)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if(DEFINED ENV{CONDA_PREFIX} AND EXISTS "$ENV{CONDA_PREFIX}/lib")
list(PREPEND CMAKE_BUILD_RPATH "$ENV{CONDA_PREFIX}/lib")
endif()

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)

set(_common_include_directories ${EXECUTORCH_ROOT}/..)
set(_json_include
${EXECUTORCH_ROOT}/extension/llm/tokenizers/third-party/json/single_include
)

# gflags
set(gflags_DIR ${EXECUTORCH_ROOT}/cmake-out/third-party/gflags)
find_package(gflags REQUIRED)

# executorch
list(APPEND CMAKE_FIND_ROOT_PATH ${CMAKE_CURRENT_BINARY_DIR}/../../../..)
find_package(executorch CONFIG REQUIRED FIND_ROOT_PATH_BOTH)
executorch_target_link_options_shared_lib(executorch)

set(link_libraries executorch gflags)

# CPU ops (for host-side helpers not delegated to CUDA)
list(APPEND link_libraries optimized_native_cpu_ops_lib cpublas eigen_blas)
executorch_target_link_options_shared_lib(optimized_native_cpu_ops_lib)

# Extensions
list(
APPEND
link_libraries
extension_llm_runner
extension_module
extension_data_loader
extension_tensor
extension_flat_tensor
)

# Backend: CUDA or MLX (exactly one required)
if(EXECUTORCH_BUILD_CUDA)
find_package(CUDAToolkit REQUIRED)
list(APPEND link_libraries aoti_cuda_backend)
executorch_target_link_options_shared_lib(aoti_cuda_backend)
add_compile_definitions(EXECUTORCH_BUILD_CUDA)
elseif(TARGET mlxdelegate)
list(APPEND link_libraries mlxdelegate mlx)
executorch_target_link_options_shared_lib(mlxdelegate)
add_compile_definitions(EXECUTORCH_BUILD_MLX)
else()
message(FATAL_ERROR "Set EXECUTORCH_BUILD_CUDA=ON or EXECUTORCH_BUILD_MLX=ON")
endif()

# Use a supplied or local stb checkout before fetching it. The resize header is
# under deprecated/ in the pinned revision.
if(EXECUTORCH_BUILD_CUDA OR TARGET mlxdelegate)
if(NOT MUSE_GLIMMER_STB_INCLUDE_DIR
AND EXISTS "${EXECUTORCH_ROOT}/third-party/stb/stb_image.h"
)
set(MUSE_GLIMMER_STB_INCLUDE_DIR "${EXECUTORCH_ROOT}/third-party/stb")
endif()
if(MUSE_GLIMMER_STB_INCLUDE_DIR
AND EXISTS "${MUSE_GLIMMER_STB_INCLUDE_DIR}/stb_image.h"
)
message(
STATUS "Using local stb headers from: ${MUSE_GLIMMER_STB_INCLUDE_DIR}"
)
list(APPEND _common_include_directories ${MUSE_GLIMMER_STB_INCLUDE_DIR}
${MUSE_GLIMMER_STB_INCLUDE_DIR}/deprecated
)
else()
include(FetchContent)
FetchContent_Declare(
stb
GIT_REPOSITORY https://github.com/nothings/stb.git
GIT_TAG f0569113c93ad095470c54bf34a17b36646bbbb5
)
FetchContent_MakeAvailable(stb)
list(APPEND _common_include_directories ${stb_SOURCE_DIR}
${stb_SOURCE_DIR}/deprecated
)
endif()
endif()

# Tokenizer (tiktoken via pytorch/tokenizers)
list(APPEND link_libraries tokenizers::tokenizers)

add_executable(
solo_runner runtime/runners/solo.cpp runtime/engine/muse_glimmer_engine.cpp
runtime/engine/dflash_session.cpp
)
if(EXECUTORCH_BUILD_CUDA OR TARGET mlxdelegate)
target_sources(
solo_runner PRIVATE runtime/engine/muse_glimmer_vision_runtime.cpp
)
endif()
target_include_directories(
solo_runner PUBLIC ${_common_include_directories} ${_json_include}
)
target_link_libraries(solo_runner PUBLIC ${link_libraries})

# Export symbols required by the dynamically loaded AOTI CUDA blob.
if(EXECUTORCH_BUILD_CUDA
AND NOT APPLE
AND NOT MSVC
)
target_link_options(solo_runner PRIVATE "LINKER:--export-dynamic")
endif()

add_executable(
dflash_runner
runtime/runners/dflash.cpp runtime/engine/muse_glimmer_engine.cpp
runtime/engine/dflash_session.cpp
)
if(EXECUTORCH_BUILD_CUDA OR TARGET mlxdelegate)
target_sources(
dflash_runner PRIVATE runtime/engine/muse_glimmer_vision_runtime.cpp
)
endif()
target_include_directories(
dflash_runner PUBLIC ${_common_include_directories} ${_json_include}
)
target_link_libraries(dflash_runner PUBLIC ${link_libraries})

add_executable(sampling_test tests/sampling_test.cpp)
target_include_directories(sampling_test PUBLIC ${_common_include_directories})
target_link_libraries(sampling_test PUBLIC extension_tensor executorch)

add_executable(dflash_timing_test tests/dflash_timing_test.cpp)
target_include_directories(
dflash_timing_test PUBLIC ${_common_include_directories}
)

# Worker used by the OpenAI-compatible serving harness.
add_executable(
muse_glimmer_worker
runtime/runners/muse_glimmer_worker.cpp
runtime/engine/muse_glimmer_engine.cpp runtime/engine/dflash_session.cpp
)
if(EXECUTORCH_BUILD_CUDA OR TARGET mlxdelegate)
target_sources(
muse_glimmer_worker PRIVATE runtime/engine/muse_glimmer_vision_runtime.cpp
)
endif()
target_include_directories(
muse_glimmer_worker PUBLIC ${_common_include_directories} ${_json_include}
)
target_link_libraries(muse_glimmer_worker PUBLIC ${link_libraries})

if(EXECUTORCH_BUILD_CUDA
AND NOT APPLE
AND NOT MSVC
)
target_link_options(dflash_runner PRIVATE "LINKER:--export-dynamic")
target_link_options(muse_glimmer_worker PRIVATE "LINKER:--export-dynamic")
endif()

if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
target_link_options_gc_sections(solo_runner)
target_link_options_gc_sections(dflash_runner)
target_link_options_gc_sections(muse_glimmer_worker)
if(NOT APPLE AND NOT MSVC)
# CUDA AOTI blobs resolve symbols from the host executable.
if(NOT EXECUTORCH_BUILD_CUDA)
target_link_options(solo_runner PRIVATE "LINKER:-s")
target_link_options(dflash_runner PRIVATE "LINKER:-s")
target_link_options(muse_glimmer_worker PRIVATE "LINKER:-s")
endif()
endif()
endif()

if(TARGET mlxdelegate)
executorch_target_copy_mlx_metallib(solo_runner)
executorch_target_copy_mlx_metallib(dflash_runner)
executorch_target_copy_mlx_metallib(muse_glimmer_worker)
endif()
83 changes: 83 additions & 0 deletions examples/models/muse-glimmer/CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"version": 6,
"configurePresets": [
{
"name": "muse-glimmer-base",
"hidden": true,
"binaryDir": "${sourceDir}/../../../cmake-out/examples/models/muse-glimmer",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_FIND_ROOT_PATH": "${sourceDir}/../../../cmake-out",
"CMAKE_PREFIX_PATH": "${sourceDir}/../../../cmake-out"
}
},
{
"name": "muse-glimmer-cuda",
"displayName": "Muse Glimmer runner (CUDA)",
"inherits": ["muse-glimmer-base"],
"cacheVariables": {
"EXECUTORCH_BUILD_CUDA": "ON"
},
"condition": {
"type": "inList",
"string": "${hostSystemName}",
"list": ["Linux", "Windows"]
}
},
{
"name": "muse-glimmer-mlx",
"displayName": "Muse Glimmer runner (MLX)",
"inherits": ["muse-glimmer-base"],
"cacheVariables": {},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
}
}
],
"buildPresets": [
{
"name": "muse-glimmer-cuda",
"displayName": "Build Muse Glimmer runner (CUDA)",
"configurePreset": "muse-glimmer-cuda",
"targets": ["solo_runner", "dflash_runner", "muse_glimmer_worker"]
},
{
"name": "muse-glimmer-mlx",
"displayName": "Build Muse Glimmer runner (MLX)",
"configurePreset": "muse-glimmer-mlx",
"targets": ["solo_runner", "dflash_runner", "muse_glimmer_worker"]
}
],
"workflowPresets": [
{
"name": "muse-glimmer-cuda",
"displayName": "Configure and build Muse Glimmer runner (CUDA)",
"steps": [
{
"type": "configure",
"name": "muse-glimmer-cuda"
},
{
"type": "build",
"name": "muse-glimmer-cuda"
}
]
},
{
"name": "muse-glimmer-mlx",
"displayName": "Configure and build Muse Glimmer runner (MLX)",
"steps": [
{
"type": "configure",
"name": "muse-glimmer-mlx"
},
{
"type": "build",
"name": "muse-glimmer-mlx"
}
]
}
]
}
Loading
Loading