Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] make webgpu build on linux and known to most tools [skip ci] #21937

Merged
merged 5 commits into from
Aug 30, 2024
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
3 changes: 2 additions & 1 deletion cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function(get_c_cxx_api_headers HEADERS_VAR)

# need to add header files for enabled EPs
foreach(f ${ONNXRUNTIME_PROVIDER_NAMES})
# The header files in include/onnxruntime/core/providers/cuda directory cannot be flattened to the same directory
# The header files in include/onnxruntime/core/providers/cuda directory cannot be flattened to the same directory
# with onnxruntime_c_api.h . Most other EPs probably also do not work in this way.
if((NOT f STREQUAL cuda) AND (NOT f STREQUAL rocm))
file(GLOB _provider_headers CONFIGURE_DEPENDS
Expand Down Expand Up @@ -200,6 +200,7 @@ set(onnxruntime_INTERNAL_LIBRARIES
${PROVIDERS_RKNPU}
${PROVIDERS_VSINPU}
${PROVIDERS_XNNPACK}
${PROVIDERS_WEBGPU}
${PROVIDERS_WEBNN}
${PROVIDERS_AZURE}
${PROVIDERS_INTERNAL_TESTING}
Expand Down
1 change: 1 addition & 0 deletions cmake/onnxruntime_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ target_link_libraries(onnxruntime_pybind11_state PRIVATE
${PROVIDERS_ACL}
${PROVIDERS_ARMNN}
${PROVIDERS_XNNPACK}
${PROVIDERS_WEBGPU}
${PROVIDERS_AZURE}
${PROVIDERS_QNN}
onnxruntime_optimizer
Expand Down
6 changes: 3 additions & 3 deletions onnxruntime/core/providers/webgpu/compute_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#include "core/framework/execution_provider.h"

#include "core/providers/webgpu/program.h"
#include "core/framework/op_kernel.h"

namespace onnxruntime {

class Tensor;
class OpKernelContext;

namespace webgpu {

Expand Down Expand Up @@ -70,7 +70,7 @@ class ComputeContext {
Tensor CreateCPUTensor(MLDataType data_type, TensorShapeType&& shape) {
AllocatorPtr allocator;
ORT_THROW_IF_ERROR(kernel_context_.GetTempSpaceCPUAllocator(&allocator));
return {data_type, std::forward<TensorShapeType>(shape)..., allocator};
return {data_type, std::forward<TensorShapeType>(shape), allocator};
}

//
Expand All @@ -80,7 +80,7 @@ class ComputeContext {
Tensor CreateGPUTensor(MLDataType data_type, TensorShapeType&& shape) {
AllocatorPtr allocator;
ORT_THROW_IF_ERROR(kernel_context_.GetTempSpaceAllocator(&allocator));
return {data_type, std::forward<TensorShapeType>(shape)..., allocator};
return {data_type, std::forward<TensorShapeType>(shape), allocator};
}

//
Expand Down
4 changes: 2 additions & 2 deletions onnxruntime/core/providers/webgpu/shader_variable.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class ShaderVariable {
void Init();
void Impl(std::ostringstream& ss);

std::string ShaderVariable::GetByOffsetImpl(const std::string& offset) const;
std::string GetByOffsetImpl(const std::string& offset) const;
std::string SetByOffsetImpl(const std::string& offset, const std::string& value) const;

std::string_view StorageType() const;
Expand All @@ -140,7 +140,7 @@ class ShaderVariable {

std::string name_;
ProgramVariableDataType type_;
int rank_;
size_t rank_;
TensorShape dims_;

mutable Usage usage_;
Expand Down
5 changes: 3 additions & 2 deletions onnxruntime/core/providers/webgpu/webgpu_provider_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ std::shared_ptr<IExecutionProviderFactory> WebGpuProviderFactoryCreator::Create(
}
LOGS_DEFAULT(VERBOSE) << "WebGPU EP graph capture enable: " << webgpu_ep_info.enable_graph_capture;

auto parse_buffer_cache_mode = [session_options](const std::string& config_entry_str, webgpu::BufferCacheMode default) -> webgpu::BufferCacheMode {
auto parse_buffer_cache_mode = [session_options](const std::string& config_entry_str,
webgpu::BufferCacheMode default_value) -> webgpu::BufferCacheMode {
std::string buffer_cache_mode_str;
if (session_options->config_options.TryGetConfigEntry(config_entry_str, buffer_cache_mode_str)) {
if (buffer_cache_mode_str == kBufferCacheMode_Disabled) {
Expand All @@ -82,7 +83,7 @@ std::shared_ptr<IExecutionProviderFactory> WebGpuProviderFactoryCreator::Create(
ORT_THROW("Invalid buffer cache mode: ", config_entry_str);
}
} else {
return default;
return default_value;
}
};

Expand Down
4 changes: 4 additions & 0 deletions onnxruntime/python/onnxruntime_pybind_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,10 @@ std::unique_ptr<IExecutionProvider> CreateExecutionProviderInstance(
return onnxruntime::XnnpackProviderFactoryCreator::Create(
cit == provider_options_map.end() ? ProviderOptions{} : cit->second, &session_options)
->CreateProvider();
#endif
} else if (type == kWebGpuExecutionProvider) {
#if defined(USE_WEBGPU)
return onnxruntime::WebGpuProviderFactoryCreator::Create(&session_options)->CreateProvider();
#endif
} else if (type == kCannExecutionProvider) {
#ifdef USE_CANN
Expand Down
6 changes: 4 additions & 2 deletions onnxruntime/test/perftest/command_args_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ namespace perftest {
"\t-A: Disable memory arena\n"
"\t-I: Generate tensor input binding (Free dimensions are treated as 1.)\n"
"\t-c [parallel runs]: Specifies the (max) number of runs to invoke simultaneously. Default:1.\n"
"\t-e [cpu|cuda|dnnl|tensorrt|openvino|dml|acl|nnapi|coreml|qnn|snpe|rocm|migraphx|xnnpack|vitisai]: Specifies the provider 'cpu','cuda','dnnl','tensorrt', "
"'openvino', 'dml', 'acl', 'nnapi', 'coreml', 'qnn', 'snpe', 'rocm', 'migraphx', 'xnnpack' or 'vitisai'. "
"\t-e [cpu|cuda|dnnl|tensorrt|openvino|dml|acl|nnapi|coreml|qnn|snpe|rocm|migraphx|xnnpack|vitisai:webgpu]: Specifies the provider 'cpu','cuda','dnnl','tensorrt', "
"'openvino', 'dml', 'acl', 'nnapi', 'coreml', 'qnn', 'snpe', 'rocm', 'migraphx', 'xnnpack', 'vitisai' or 'webgpu'. "
"Default:'cpu'.\n"
"\t-b [tf|ort]: backend to use. Default:ort\n"
"\t-r [repeated_times]: Specifies the repeated times if running in 'times' test mode.Default:1000.\n"
Expand Down Expand Up @@ -279,6 +279,8 @@ static bool ParseSessionConfigs(const std::string& configs_string,
test_config.machine_config.provider_type_name = onnxruntime::kXnnpackExecutionProvider;
} else if (!CompareCString(optarg, ORT_TSTR("vitisai"))) {
test_config.machine_config.provider_type_name = onnxruntime::kVitisAIExecutionProvider;
} else if (!CompareCString(optarg, ORT_TSTR("webgpu"))) {
test_config.machine_config.provider_type_name = onnxruntime::kWebGpuExecutionProvider;
} else {
return false;
}
Expand Down
7 changes: 7 additions & 0 deletions onnxruntime/test/perftest/ort_test_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,13 @@ select from 'TF8', 'TF16', 'UINT8', 'FLOAT', 'ITENSOR'. \n)");
"XNNPACK", {{"intra_op_num_threads", std::to_string(performance_test_config.run_config.intra_op_num_threads)}});
#else
ORT_THROW("Xnnpack is not supported in this build\n");
#endif
} else if (provider_name_ == onnxruntime::kWebGpuExecutionProvider) {
#ifdef USE_WEBGPU
session_options.AppendExecutionProvider(
"WebGPU", {{"intra_op_num_threads", std::to_string(performance_test_config.run_config.intra_op_num_threads)}});
#else
ORT_THROW("WebGpu is not supported in this build\n");
#endif
} else if (provider_name_ == onnxruntime::kVitisAIExecutionProvider) {
#ifdef USE_VITISAI
Expand Down
8 changes: 8 additions & 0 deletions onnxruntime/test/util/default_providers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ std::unique_ptr<IExecutionProvider> DefaultXnnpackExecutionProvider() {
#endif
}

std::unique_ptr<IExecutionProvider> DefaultWebGpuExecutionProvider() {
#ifdef USE_WEBGPU
return WebGpuProviderFactoryCreator::Create(nullptr)->CreateProvider();
#else
return nullptr;
#endif
}

std::unique_ptr<IExecutionProvider> DefaultCannExecutionProvider() {
#ifdef USE_CANN
OrtCANNProviderOptions provider_options{};
Expand Down
1 change: 1 addition & 0 deletions onnxruntime/test/util/include/default_providers.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ std::unique_ptr<IExecutionProvider> DefaultQnnExecutionProvider();
std::unique_ptr<IExecutionProvider> QnnExecutionProviderWithOptions(const ProviderOptions& options,
const SessionOptions* session_options = nullptr);
std::unique_ptr<IExecutionProvider> DefaultXnnpackExecutionProvider();
std::unique_ptr<IExecutionProvider> DefaultWebGpuExecutionProvider();
std::unique_ptr<IExecutionProvider> DefaultCannExecutionProvider();
std::unique_ptr<IExecutionProvider> DefaultDmlExecutionProvider();

Expand Down
1 change: 1 addition & 0 deletions tools/ci_build/gen_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def parse_arguments():
"dnnl",
"tensorrt",
"azure",
"webgpu"
):
file.write(f"#include <core/providers/{c}/{c}_provider_factory.h>\n")
file.write("void* GetFunctionEntryByName(const char* name){\n")
Expand Down
Loading