Skip to content

Commit 8191831

Browse files
committed
Update on "[ET-VK] 7/n Split dispatches between multiple command buffers. Split execute dispatch into multiple commands based on dispatch count."
Differential Revision: [D78360039](https://our.internmc.facebook.com/intern/diff/D78360039/) [ghstack-poisoned]
2 parents dfc80d0 + 8459d87 commit 8191831

File tree

10 files changed

+574
-165
lines changed

10 files changed

+574
-165
lines changed

.ci/scripts/build-mediatek-sdk.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ build_neuron_backend() {
1414
export NEURON_BUFFER_ALLOCATOR_LIB=${MEDIATEK_SDK_ROOT}/libneuron_buffer_allocator.so
1515
export EXECUTORCH_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)"
1616

17-
1817
cd ${EXECUTORCH_ROOT}
1918
./backends/mediatek/scripts/mtk_build.sh
19+
./examples/mediatek/mtk_build_examples.sh
2020
}
2121

2222
build_neuron_backend

backends/mediatek/scripts/mtk_build.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
set -e
55

66
# Define the directory where CMakeLists.txt is located
7-
SOURCE_DIR=$(realpath "$(dirname "$0")/../../..")
7+
EXECUTORCH_ROOT=$(realpath "$(dirname "$0")/../../..")
8+
echo EXECUTORCH_ROOT=${EXECUTORCH_ROOT}
89

910
# Check if the ANDROID_NDK environment variable is set
1011
if [ -z "$ANDROID_NDK" ]; then
1112
echo "Error: ANDROID_NDK environment variable is not set." >&2
1213
exit 1
1314
fi
1415

15-
# Create and enter the build directory
16+
# Enter the build directory
17+
cd "$EXECUTORCH_ROOT"
18+
1619
# Set build directory
1720
build_dir="cmake-android-out"
18-
cd "$SOURCE_DIR"
1921
rm -rf "${build_dir}"
2022

2123
# Configure the project with CMake

docs/source/using-executorch-ios.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ let inputTensor = Tensor<Float>(&imageBuffer, shape: [1, 3, 224, 224])
246246
let outputTensor: Tensor<Float> = try module.forward(inputTensor)[0].tensor()!
247247
248248
// Copy the tensor data into logits array for easier access.
249-
let logits = try outputTensor.scalars()
249+
let logits = outputTensor.scalars()
250250
251251
// Use logits...
252252
```
@@ -444,11 +444,11 @@ Swift:
444444
let tensor = Tensor<Float>([1.0, 2.0, 3.0, 4.0], shape: [2, 2])
445445
446446
// Get data copy as a Swift array.
447-
let scalars = try tensor.scalars()
447+
let scalars = tensor.scalars()
448448
print("All scalars: \(scalars)") // [1.0, 2.0, 3.0, 4.0]
449449
450450
// Access data via a buffer pointer.
451-
try tensor.withUnsafeBytes { buffer in
451+
tensor.withUnsafeBytes { buffer in
452452
print("First float element: \(buffer.first ?? 0.0)")
453453
}
454454
@@ -482,7 +482,7 @@ Swift:
482482
let tensor = Tensor<Float>([1.0, 2.0, 3.0, 4.0], shape: [2, 2])
483483
484484
// Modify the tensor's data in place.
485-
try tensor.withUnsafeMutableBytes { buffer in
485+
tensor.withUnsafeMutableBytes { buffer in
486486
buffer[1] = 200.0
487487
}
488488
// tensor's data is now [1.0, 200.0, 3.0, 4.0]

examples/mediatek/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ examples/mediatek
2828
## Environment Setup
2929
- Follow the instructions of **Prerequisites** and **Setup** in `backends/mediatek/scripts/README.md`.
3030

31+
- Build required libraries by `backends/mediatek/scripts/mtk_build.sh` before building examples.
32+
3133
## Build MediaTek Examples
3234
1. Build the backend and the examples by exedcuting the script:
3335
```bash

examples/mediatek/mtk_build_examples.sh

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,18 @@ if [ -z "$ANDROID_NDK" ]; then
1414
fi
1515

1616
main() {
17-
# Set build directory
18-
local build_dir="cmake-android-out"
19-
20-
# Create and enter the build directory
17+
# Enter the build directory
2118
cd "$EXECUTORCH_ROOT"
22-
rm -rf "${build_dir}"
23-
24-
# Configure the project with CMake
25-
# Note: Add any additional configuration options you need here
26-
cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \
27-
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
28-
-DANDROID_ABI=arm64-v8a \
29-
-DANDROID_NATIVE_API_LEVEL=26 \
30-
-DANDROID_PLATFORM=android-26 \
31-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
32-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
33-
-DEXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR=ON \
34-
-DEXECUTORCH_BUILD_NEURON=ON \
35-
-B"${build_dir}"
3619

20+
# Set build directory
21+
local build_dir="cmake-android-out"
3722

38-
# Build the project
39-
cmake --build "${build_dir}" --target install --config Release -j5
23+
# Check if the build directory exists
24+
if [ ! -d "$EXECUTORCH_ROOT/$build_dir" ]; then
25+
echo "Error: Build directory '$build_dir' does not exist."
26+
echo "Please build MTK backend before running this script."
27+
exit 1
28+
fi
4029

4130
## Build example
4231
local example_dir=examples/mediatek

extension/apple/ExecuTorch/Exported/ExecuTorch+Module.swift

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,15 @@ public extension Module {
6363
try __executeMethod(method, withInputs: inputs.map { $0.asValue() } )
6464
}
6565

66-
/// Executes a specific method with a single input value.
67-
/// The method is loaded on demand if not already loaded.
66+
/// Executes a specific method with variadic inputs.
6867
///
6968
/// - Parameters:
7069
/// - method: The name of the method to execute.
71-
/// - input: A single `ValueConvertible` type representing the input.
70+
/// - inputs: A variadic list of `ValueConvertible` inputs.
7271
/// - Returns: An array of `Value` objects representing the outputs.
73-
/// - Throws: An error if method execution fails.
74-
func execute(_ method: String, _ input: ValueConvertible) throws -> [Value] {
75-
try __executeMethod(method, withInputs: [input.asValue()])
72+
/// - Throws: An error if loading or execution fails.
73+
func execute(_ method: String, _ inputs: ValueConvertible...) throws -> [Value] {
74+
try execute(method, inputs)
7675
}
7776

7877
/// Executes the "forward" method with the provided input values.
@@ -85,13 +84,12 @@ public extension Module {
8584
try __executeMethod("forward", withInputs: inputs.map { $0.asValue() })
8685
}
8786

88-
/// Executes the "forward" method with a single input value.
89-
/// The method is loaded on demand if not already loaded.
87+
/// Executes the "forward" method with variadic inputs.
9088
///
91-
/// - Parameter input: A single `ValueConvertible` type representing the input.
89+
/// - Parameter inputs: A variadic list of `ValueConvertible` inputs.
9290
/// - Returns: An array of `Value` objects representing the outputs.
93-
/// - Throws: An error if method execution fails.
94-
func forward(_ input: ValueConvertible) throws -> [Value] {
95-
try __executeMethod("forward", withInputs: [input.asValue()])
91+
/// - Throws: An error if loading or execution fails.
92+
func forward(_ inputs: ValueConvertible...) throws -> [Value] {
93+
try forward(inputs)
9694
}
9795
}

extension/apple/ExecuTorch/Exported/ExecuTorch+Tensor.swift

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -770,35 +770,29 @@ public final class Tensor<T: Scalar>: Equatable {
770770
/// - Parameter body: A closure that receives an `UnsafeBufferPointer<T>` bound to the tensor’s data.
771771
/// - Returns: The value returned by `body`.
772772
/// - Throws: Any error thrown by `body`.
773-
public func withUnsafeBytes<R>(_ body: (UnsafeBufferPointer<T>) throws -> R) throws -> R {
774-
var result: Result<R, Error>?
775-
anyTensor.bytes { pointer, count, _ in
776-
result = Result { try body(
777-
UnsafeBufferPointer(
778-
start: pointer.assumingMemoryBound(to: T.self),
779-
count: count
780-
)
781-
) }
773+
public func withUnsafeBytes<R>(_ body: (UnsafeBufferPointer<T>) throws -> R) rethrows -> R {
774+
try withoutActuallyEscaping(body) { body in
775+
var result: Result<R, Error>?
776+
anyTensor.bytes { pointer, count, _ in
777+
result = Result { try body(UnsafeBufferPointer(start: pointer.assumingMemoryBound(to: T.self), count: count)) }
778+
}
779+
return try result!.get()
782780
}
783-
return try result!.get()
784781
}
785782

786783
/// Calls the closure with a typed, mutable buffer pointer over the tensor’s elements.
787784
///
788785
/// - Parameter body: A closure that receives an `UnsafeMutableBufferPointer<T>` bound to the tensor’s data.
789786
/// - Returns: The value returned by `body`.
790787
/// - Throws: Any error thrown by `body`.
791-
public func withUnsafeMutableBytes<R>(_ body: (UnsafeMutableBufferPointer<T>) throws -> R) throws -> R {
792-
var result: Result<R, Error>?
793-
anyTensor.mutableBytes { pointer, count, _ in
794-
result = Result { try body(
795-
UnsafeMutableBufferPointer(
796-
start: pointer.assumingMemoryBound(to: T.self),
797-
count: count
798-
)
799-
) }
788+
public func withUnsafeMutableBytes<R>(_ body: (UnsafeMutableBufferPointer<T>) throws -> R) rethrows -> R {
789+
try withoutActuallyEscaping(body) { body in
790+
var result: Result<R, Error>?
791+
anyTensor.mutableBytes { pointer, count, _ in
792+
result = Result { try body(UnsafeMutableBufferPointer(start: pointer.assumingMemoryBound(to: T.self), count: count)) }
793+
}
794+
return try result!.get()
800795
}
801-
return try result!.get()
802796
}
803797

804798
/// Resizes the tensor to a new shape.
@@ -830,9 +824,8 @@ public extension Tensor {
830824
/// Returns the tensor's elements as an array of scalars.
831825
///
832826
/// - Returns: An array of scalars of type `T`.
833-
/// - Throws: An error if the underlying data cannot be accessed.
834-
func scalars() throws -> [T] {
835-
try withUnsafeBytes { Array($0) }
827+
func scalars() -> [T] {
828+
withUnsafeBytes { Array($0) }
836829
}
837830
}
838831

0 commit comments

Comments
 (0)