Skip to content

Migrate backends/apple to the new namespace #5883

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

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 17 additions & 5 deletions backends/apple/mps/runtime/MPSBackend.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@
#include <string>
#include <iostream>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {

using executorch::aten::Tensor;
using executorch::runtime::ArrayRef;
using executorch::runtime::Backend;
using executorch::runtime::BackendExecutionContext;
using executorch::runtime::BackendInitContext;
using executorch::runtime::CompileSpec;
using executorch::runtime::DelegateHandle;
using executorch::runtime::EValue;
using executorch::runtime::Error;
using executorch::runtime::FreeableBuffer;
using executorch::runtime::Result;

class MPSBackend final : public ::executorch::runtime::BackendInterface {
public:
Expand Down Expand Up @@ -81,7 +93,7 @@ Error execute(
output_pointers.push_back(&args[i]->toTensor());
}
} else if (args[i]->isTensorList()) {
const exec_aten::ArrayRef<exec_aten::Tensor>& tensorList = args[i]->toTensorList();
const executorch::aten::ArrayRef<executorch::aten::Tensor>& tensorList = args[i]->toTensorList();
for (auto& tensor_ : tensorList) {
if (input_pointers.size() < executor->getNumInputs()) {
input_pointers.push_back(&tensor_);
Expand Down Expand Up @@ -122,5 +134,5 @@ void destroy(DelegateHandle* handle) const override {
static auto success_with_compiler = register_backend(backend);
} // namespace

} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
15 changes: 8 additions & 7 deletions backends/apple/mps/runtime/MPSCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include <memory>
#include <vector>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

Expand All @@ -24,15 +24,16 @@ class MPSCompiler {
// Takes Flatbuffer Serialized MPS Model and rebuilds the MPSGraphExecutable
// returns an executor object that holds the MPS runtime object which we
// can then use to set inputs and run inference using the MPSGraphExecutable.
ET_NODISCARD static Error compileModel(
ET_NODISCARD static executorch::runtime::Error compileModel(
const void* buffer_pointer,
size_t num_bytes,
MPSExecutor* executor,
MemoryAllocator* runtime_allocator,
ArrayRef<CompileSpec> compile_specs);
executorch::runtime::MemoryAllocator* runtime_allocator,
executorch::runtime::ArrayRef<executorch::runtime::CompileSpec>
compile_specs);
};

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
13 changes: 9 additions & 4 deletions backends/apple/mps/runtime/MPSCompiler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@

#define MPS_UNUSED(x) ( (void)(x) )

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

using executorch::runtime::ArrayRef;
using executorch::runtime::CompileSpec;
using executorch::runtime::Error;
using executorch::runtime::MemoryAllocator;

/*
Builds the mps runtime object using the buffer pointer. The buffer pointer
must be a valid pointer to the serialized mps object.
Expand Down Expand Up @@ -66,5 +71,5 @@

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
12 changes: 7 additions & 5 deletions backends/apple/mps/runtime/MPSDelegateHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include <executorch/runtime/core/result.h>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

Expand Down Expand Up @@ -87,7 +87,9 @@ struct MPSDelegateHeader {
* error if size was too short, if the header was not found, or if the
* header appeared to be corrupt.
*/
static Result<MPSDelegateHeader> Parse(const void* data, size_t size);
static executorch::runtime::Result<MPSDelegateHeader> Parse(
const void* data,
size_t size);

/**
* The offset in bytes to the beginning of the constant data.
Expand All @@ -109,5 +111,5 @@ struct MPSDelegateHeader {

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
11 changes: 7 additions & 4 deletions backends/apple/mps/runtime/MPSDelegateHeader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
#include <executorch/runtime/core/error.h>
#include <executorch/runtime/core/result.h>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

using executorch::runtime::Error;
using executorch::runtime::Result;

/// Interprets the 8 bytes at `data` as a little-endian uint64_t.
uint64_t getUInt64LE(const uint8_t* data) {
return (uint64_t)data[0] | ((uint64_t)data[1] << 8) |
Expand Down Expand Up @@ -49,5 +52,5 @@ uint64_t getUInt64LE(const uint8_t* data) {

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
14 changes: 8 additions & 6 deletions backends/apple/mps/runtime/MPSDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#define MB(x) (x * 1048576UL)

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

Expand Down Expand Up @@ -72,8 +72,10 @@ class MPSDevice {
* Compile a PSO for a given library type.
* Once compiled, the library and PSOs are cached.
*/
Error compilePSO(LibraryType libraryType, const char* kernelName);
Error compileLibrary(LibraryType);
executorch::runtime::Error compilePSO(
LibraryType libraryType,
const char* kernelName);
executorch::runtime::Error compileLibrary(LibraryType);

private:
static MPSDevice* _device;
Expand All @@ -88,5 +90,5 @@ bool is_macos_13_or_newer(

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
10 changes: 6 additions & 4 deletions backends/apple/mps/runtime/MPSDevice.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
#include <memory>
#include <mutex>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

using executorch::runtime::Error;

static std::unique_ptr<MPSDevice> mps_device;
static std::once_flag mpsdev_init;

Expand Down Expand Up @@ -152,5 +154,5 @@ bool is_macos_13_or_newer(MacOSVersion version) {

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
20 changes: 10 additions & 10 deletions backends/apple/mps/runtime/MPSExecutor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include <memory>
#include <vector>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

Expand Down Expand Up @@ -73,20 +73,20 @@ class MPSExecutor {
return _executable;
}

ET_NODISCARD Error forward(std::vector<const Tensor*>& outputs);
ET_NODISCARD executorch::runtime::Error forward(std::vector<const executorch::aten::Tensor*>& outputs);

ET_NODISCARD Error
set_inputs_outputs(std::vector<const Tensor*>& inputs, std::vector<const Tensor*>& outputs);
ET_NODISCARD executorch::runtime::Error
set_inputs_outputs(std::vector<const executorch::aten::Tensor*>& inputs, std::vector<const executorch::aten::Tensor*>& outputs);

Error initDataBuffers();
Error updateDataBuffers(std::vector<const Tensor*>& inputs, std::vector<const Tensor*>& outputs);
Error syncOutputBuffers(std::vector<const Tensor*>& outputs);
executorch::runtime::Error initDataBuffers();
executorch::runtime::Error updateDataBuffers(std::vector<const executorch::aten::Tensor*>& inputs, std::vector<const executorch::aten::Tensor*>& outputs);
executorch::runtime::Error syncOutputBuffers(std::vector<const executorch::aten::Tensor*>& outputs);

friend class MPSCompiler;
};

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
// clang-format on
11 changes: 7 additions & 4 deletions backends/apple/mps/runtime/MPSExecutor.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ @interface MPSGraphExecutable()
@end


namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

using executorch::runtime::Error;
using executorch::aten::Tensor;

MPSExecutor::MPSExecutor() {
_use_shared_mem = true;
_buffers_initialized = false;
Expand Down Expand Up @@ -239,5 +242,5 @@ @interface MPSGraphExecutable()

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
9 changes: 5 additions & 4 deletions backends/apple/mps/runtime/MPSGraphBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
#include <unordered_map>
#include <vector>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

using Error = executorch::runtime::Error;
using DataType = mpsgraph::MPSDataType;
using TensorPtr = const mpsgraph::MPSTensor *;
using NodePtr = const mpsgraph::MPSNode *;
Expand Down Expand Up @@ -197,5 +198,5 @@ class MPSGraphBuilder {

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
10 changes: 6 additions & 4 deletions backends/apple/mps/runtime/MPSGraphBuilder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
#include <executorch/backends/apple/mps/runtime/MPSDevice.h>
#include <executorch/backends/apple/mps/runtime/MPSDelegateHeader.h>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

using executorch::runtime::Result;

MPSGraphBuilder::MPSGraphBuilder(
const void* buffer_pointer,
size_t num_bytes,
Expand Down Expand Up @@ -186,5 +188,5 @@

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
10 changes: 5 additions & 5 deletions backends/apple/mps/runtime/MPSStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

#include <unordered_map>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

Expand Down Expand Up @@ -63,7 +63,7 @@ class MPSStream {
MPSCommandBuffer* commandBuffer();
id<MTLComputeCommandEncoder> commandEncoder();
void endKernelCoalescing();
ET_NODISCARD Error synchronize(SyncType syncType);
ET_NODISCARD executorch::runtime::Error synchronize(SyncType syncType);
bool commitAndContinueEnabled();
void copy(
id<MTLBuffer> srcBuffer,
Expand Down Expand Up @@ -135,5 +135,5 @@ class MPSStreamImpl {

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
10 changes: 6 additions & 4 deletions backends/apple/mps/runtime/MPSStream.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ @interface MPSGraphExecutionDescriptor ()
@property (readwrite, atomic) BOOL enableCommitAndContinue;
@end

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

using executorch::runtime::Error;

//-----------------------------------------------------------------
// MPSStream
//-----------------------------------------------------------------
Expand Down Expand Up @@ -258,5 +260,5 @@ @interface MPSGraphExecutionDescriptor ()

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
8 changes: 4 additions & 4 deletions backends/apple/mps/runtime/operations/ActivationOps.mm
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include <executorch/backends/apple/mps/runtime/MPSGraphBuilder.h>

namespace torch {
namespace executor {
namespace executorch {
namespace backends {
namespace mps {
namespace delegate {

Expand Down Expand Up @@ -151,5 +151,5 @@

} // namespace delegate
} // namespace mps
} // namespace executor
} // namespace torch
} // namespace backends
} // namespace executorch
Loading
Loading