Skip to content

Commit 1822658

Browse files
dbortfacebook-github-bot
authored andcommitted
Migrate backends/apple/mps to the new namespace (#5883)
Summary: Move the MPS backend out of the `torch::` namespace, and update to avoid using the `torch::` or `exec_aten::` namespaces. Differential Revision: D63908530
1 parent 2726bdb commit 1822658

30 files changed

+194
-157
lines changed

backends/apple/mps/runtime/MPSBackend.mm

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@
1616
#include <string>
1717
#include <iostream>
1818

19-
namespace torch {
20-
namespace executor {
19+
namespace executorch {
20+
namespace backends {
21+
22+
using executorch::aten::Tensor;
23+
using executorch::runtime::ArrayRef;
24+
using executorch::runtime::Backend;
25+
using executorch::runtime::BackendExecutionContext;
26+
using executorch::runtime::BackendInitContext;
27+
using executorch::runtime::CompileSpec;
28+
using executorch::runtime::DelegateHandle;
29+
using executorch::runtime::EValue;
30+
using executorch::runtime::Error;
31+
using executorch::runtime::FreeableBuffer;
32+
using executorch::runtime::Result;
2133

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

125-
} // namespace executor
126-
} // namespace torch
137+
} // namespace backends
138+
} // namespace executorch

backends/apple/mps/runtime/MPSCompiler.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include <memory>
1515
#include <vector>
1616

17-
namespace torch {
18-
namespace executor {
17+
namespace executorch {
18+
namespace backends {
1919
namespace mps {
2020
namespace delegate {
2121

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

3536
} // namespace delegate
3637
} // namespace mps
37-
} // namespace executor
38-
} // namespace torch
38+
} // namespace backends
39+
} // namespace executorch

backends/apple/mps/runtime/MPSCompiler.mm

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323

2424
#define MPS_UNUSED(x) ( (void)(x) )
2525

26-
namespace torch {
27-
namespace executor {
26+
namespace executorch {
27+
namespace backends {
2828
namespace mps {
2929
namespace delegate {
3030

31+
using executorch::runtime::ArrayRef;
32+
using executorch::runtime::CompileSpec;
33+
using executorch::runtime::Error;
34+
using executorch::runtime::MemoryAllocator;
35+
3136
/*
3237
Builds the mps runtime object using the buffer pointer. The buffer pointer
3338
must be a valid pointer to the serialized mps object.
@@ -66,5 +71,5 @@
6671

6772
} // namespace delegate
6873
} // namespace mps
69-
} // namespace executor
70-
} // namespace torch
74+
} // namespace backends
75+
} // namespace executorch

backends/apple/mps/runtime/MPSDelegateHeader.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

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

10-
namespace torch {
11-
namespace executor {
10+
namespace executorch {
11+
namespace backends {
1212
namespace mps {
1313
namespace delegate {
1414

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

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

110112
} // namespace delegate
111113
} // namespace mps
112-
} // namespace executor
113-
} // namespace torch
114+
} // namespace backends
115+
} // namespace executorch

backends/apple/mps/runtime/MPSDelegateHeader.mm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
#include <executorch/runtime/core/error.h>
1111
#include <executorch/runtime/core/result.h>
1212

13-
namespace torch {
14-
namespace executor {
13+
namespace executorch {
14+
namespace backends {
1515
namespace mps {
1616
namespace delegate {
1717

18+
using executorch::runtime::Error;
19+
using executorch::runtime::Result;
20+
1821
/// Interprets the 8 bytes at `data` as a little-endian uint64_t.
1922
uint64_t getUInt64LE(const uint8_t* data) {
2023
return (uint64_t)data[0] | ((uint64_t)data[1] << 8) |
@@ -49,5 +52,5 @@ uint64_t getUInt64LE(const uint8_t* data) {
4952

5053
} // namespace delegate
5154
} // namespace mps
52-
} // namespace executor
53-
} // namespace torch
55+
} // namespace backends
56+
} // namespace executorch

backends/apple/mps/runtime/MPSDevice.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

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

23-
namespace torch {
24-
namespace executor {
23+
namespace executorch {
24+
namespace backends {
2525
namespace mps {
2626
namespace delegate {
2727

@@ -72,8 +72,10 @@ class MPSDevice {
7272
* Compile a PSO for a given library type.
7373
* Once compiled, the library and PSOs are cached.
7474
*/
75-
Error compilePSO(LibraryType libraryType, const char* kernelName);
76-
Error compileLibrary(LibraryType);
75+
executorch::runtime::Error compilePSO(
76+
LibraryType libraryType,
77+
const char* kernelName);
78+
executorch::runtime::Error compileLibrary(LibraryType);
7779

7880
private:
7981
static MPSDevice* _device;
@@ -88,5 +90,5 @@ bool is_macos_13_or_newer(
8890

8991
} // namespace delegate
9092
} // namespace mps
91-
} // namespace executor
92-
} // namespace torch
93+
} // namespace backends
94+
} // namespace executorch

backends/apple/mps/runtime/MPSDevice.mm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include <memory>
99
#include <mutex>
1010

11-
namespace torch {
12-
namespace executor {
11+
namespace executorch {
12+
namespace backends {
1313
namespace mps {
1414
namespace delegate {
1515

16+
using executorch::runtime::Error;
17+
1618
static std::unique_ptr<MPSDevice> mps_device;
1719
static std::once_flag mpsdev_init;
1820

@@ -152,5 +154,5 @@ bool is_macos_13_or_newer(MacOSVersion version) {
152154

153155
} // namespace delegate
154156
} // namespace mps
155-
} // namespace executor
156-
} // namespace torch
157+
} // namespace backends
158+
} // namespace executorch

backends/apple/mps/runtime/MPSExecutor.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include <memory>
1717
#include <vector>
1818

19-
namespace torch {
20-
namespace executor {
19+
namespace executorch {
20+
namespace backends {
2121
namespace mps {
2222
namespace delegate {
2323

@@ -73,20 +73,20 @@ class MPSExecutor {
7373
return _executable;
7474
}
7575

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

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

81-
Error initDataBuffers();
82-
Error updateDataBuffers(std::vector<const Tensor*>& inputs, std::vector<const Tensor*>& outputs);
83-
Error syncOutputBuffers(std::vector<const Tensor*>& outputs);
81+
executorch::runtime::Error initDataBuffers();
82+
executorch::runtime::Error updateDataBuffers(std::vector<const executorch::aten::Tensor*>& inputs, std::vector<const executorch::aten::Tensor*>& outputs);
83+
executorch::runtime::Error syncOutputBuffers(std::vector<const executorch::aten::Tensor*>& outputs);
8484

8585
friend class MPSCompiler;
8686
};
8787

8888
} // namespace delegate
8989
} // namespace mps
90-
} // namespace executor
91-
} // namespace torch
90+
} // namespace backends
91+
} // namespace executorch
9292
// clang-format on

backends/apple/mps/runtime/MPSExecutor.mm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@ @interface MPSGraphExecutable()
1717
@end
1818

1919

20-
namespace torch {
21-
namespace executor {
20+
namespace executorch {
21+
namespace backends {
2222
namespace mps {
2323
namespace delegate {
2424

25+
using executorch::runtime::Error;
26+
using executorch::aten::Tensor;
27+
2528
MPSExecutor::MPSExecutor() {
2629
_use_shared_mem = true;
2730
_buffers_initialized = false;
@@ -239,5 +242,5 @@ @interface MPSGraphExecutable()
239242

240243
} // namespace delegate
241244
} // namespace mps
242-
} // namespace executor
243-
} // namespace torch
245+
} // namespace backends
246+
} // namespace executorch

backends/apple/mps/runtime/MPSGraphBuilder.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
#include <unordered_map>
2525
#include <vector>
2626

27-
namespace torch {
28-
namespace executor {
27+
namespace executorch {
28+
namespace backends {
2929
namespace mps {
3030
namespace delegate {
3131

32+
using Error = executorch::runtime::Error;
3233
using DataType = mpsgraph::MPSDataType;
3334
using TensorPtr = const mpsgraph::MPSTensor *;
3435
using NodePtr = const mpsgraph::MPSNode *;
@@ -197,5 +198,5 @@ class MPSGraphBuilder {
197198

198199
} // namespace delegate
199200
} // namespace mps
200-
} // namespace executor
201-
} // namespace torch
201+
} // namespace backends
202+
} // namespace executorch

backends/apple/mps/runtime/MPSGraphBuilder.mm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
#include <executorch/backends/apple/mps/runtime/MPSDevice.h>
88
#include <executorch/backends/apple/mps/runtime/MPSDelegateHeader.h>
99

10-
namespace torch {
11-
namespace executor {
10+
namespace executorch {
11+
namespace backends {
1212
namespace mps {
1313
namespace delegate {
1414

15+
using executorch::runtime::Result;
16+
1517
MPSGraphBuilder::MPSGraphBuilder(
1618
const void* buffer_pointer,
1719
size_t num_bytes,
@@ -186,5 +188,5 @@
186188

187189
} // namespace delegate
188190
} // namespace mps
189-
} // namespace executor
190-
} // namespace torch
191+
} // namespace backends
192+
} // namespace executorch

backends/apple/mps/runtime/MPSStream.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
#include <unordered_map>
1919

20-
namespace torch {
21-
namespace executor {
20+
namespace executorch {
21+
namespace backends {
2222
namespace mps {
2323
namespace delegate {
2424

@@ -63,7 +63,7 @@ class MPSStream {
6363
MPSCommandBuffer* commandBuffer();
6464
id<MTLComputeCommandEncoder> commandEncoder();
6565
void endKernelCoalescing();
66-
ET_NODISCARD Error synchronize(SyncType syncType);
66+
ET_NODISCARD executorch::runtime::Error synchronize(SyncType syncType);
6767
bool commitAndContinueEnabled();
6868
void copy(
6969
id<MTLBuffer> srcBuffer,
@@ -135,5 +135,5 @@ class MPSStreamImpl {
135135

136136
} // namespace delegate
137137
} // namespace mps
138-
} // namespace executor
139-
} // namespace torch
138+
} // namespace backends
139+
} // namespace executorch

backends/apple/mps/runtime/MPSStream.mm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ @interface MPSGraphExecutionDescriptor ()
1111
@property (readwrite, atomic) BOOL enableCommitAndContinue;
1212
@end
1313

14-
namespace torch {
15-
namespace executor {
14+
namespace executorch {
15+
namespace backends {
1616
namespace mps {
1717
namespace delegate {
1818

19+
using executorch::runtime::Error;
20+
1921
//-----------------------------------------------------------------
2022
// MPSStream
2123
//-----------------------------------------------------------------
@@ -258,5 +260,5 @@ @interface MPSGraphExecutionDescriptor ()
258260

259261
} // namespace delegate
260262
} // namespace mps
261-
} // namespace executor
262-
} // namespace torch
263+
} // namespace backends
264+
} // namespace executorch

backends/apple/mps/runtime/operations/ActivationOps.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

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

9-
namespace torch {
10-
namespace executor {
9+
namespace executorch {
10+
namespace backends {
1111
namespace mps {
1212
namespace delegate {
1313

@@ -151,5 +151,5 @@
151151

152152
} // namespace delegate
153153
} // namespace mps
154-
} // namespace executor
155-
} // namespace torch
154+
} // namespace backends
155+
} // namespace executorch

0 commit comments

Comments
 (0)