Skip to content

Commit ae9482d

Browse files
committed
[WebGPU] Add delegate header parser
Parses the VH00/VK00 FlatBuffer envelope from the Vulkan partitioner to extract the serialized graph payload.
1 parent c5e917d commit ae9482d

2 files changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#include <executorch/backends/webgpu/runtime/WebGPUDelegateHeader.h>
10+
11+
#include <cstring>
12+
13+
#include <executorch/runtime/core/error.h>
14+
#include <executorch/runtime/core/result.h>
15+
16+
namespace executorch {
17+
namespace backends {
18+
namespace webgpu {
19+
20+
using executorch::runtime::Error;
21+
using executorch::runtime::Result;
22+
23+
namespace {
24+
25+
struct ByteSlice {
26+
size_t offset;
27+
size_t size;
28+
};
29+
30+
constexpr size_t kExpectedSize = 30;
31+
constexpr char kExpectedMagic[4] = {'V', 'H', '0', '0'};
32+
33+
constexpr ByteSlice kMagic = {4, 4};
34+
constexpr ByteSlice kHeaderSize = {8, 2};
35+
constexpr ByteSlice kFlatbufferOffset = {10, 4};
36+
constexpr ByteSlice kFlatbufferSize = {14, 4};
37+
constexpr ByteSlice kBytesOffset = {18, 4};
38+
constexpr ByteSlice kBytesSize = {22, 8};
39+
40+
uint64_t getUInt64LE(const uint8_t* data) {
41+
return (uint64_t)data[0] | ((uint64_t)data[1] << 8) |
42+
((uint64_t)data[2] << 16) | ((uint64_t)data[3] << 24) |
43+
((uint64_t)data[4] << 32) | ((uint64_t)data[5] << 40) |
44+
((uint64_t)data[6] << 48) | ((uint64_t)data[7] << 56);
45+
}
46+
47+
uint32_t getUInt32LE(const uint8_t* data) {
48+
return (uint32_t)data[0] | ((uint32_t)data[1] << 8) |
49+
((uint32_t)data[2] << 16) | ((uint32_t)data[3] << 24);
50+
}
51+
52+
uint32_t getUInt16LE(const uint8_t* data) {
53+
return (uint32_t)data[0] | ((uint32_t)data[1] << 8);
54+
}
55+
56+
} // namespace
57+
58+
bool WebGPUDelegateHeader::is_valid() const {
59+
if (header_size < kExpectedSize) {
60+
return false;
61+
}
62+
if (flatbuffer_offset < header_size) {
63+
return false;
64+
}
65+
if (flatbuffer_size == 0) {
66+
return false;
67+
}
68+
if (bytes_offset < flatbuffer_offset + flatbuffer_size) {
69+
return false;
70+
}
71+
return true;
72+
}
73+
74+
Result<WebGPUDelegateHeader> WebGPUDelegateHeader::parse(const void* data) {
75+
const uint8_t* header_data = (const uint8_t*)data;
76+
77+
const uint8_t* magic_start = header_data + kMagic.offset;
78+
if (std::memcmp(magic_start, kExpectedMagic, kMagic.size) != 0) {
79+
return Error::NotFound;
80+
}
81+
82+
WebGPUDelegateHeader header = WebGPUDelegateHeader{
83+
getUInt16LE(header_data + kHeaderSize.offset),
84+
getUInt32LE(header_data + kFlatbufferOffset.offset),
85+
getUInt32LE(header_data + kFlatbufferSize.offset),
86+
getUInt32LE(header_data + kBytesOffset.offset),
87+
getUInt64LE(header_data + kBytesSize.offset),
88+
};
89+
90+
if (!header.is_valid()) {
91+
return Error::InvalidArgument;
92+
}
93+
94+
return header;
95+
}
96+
97+
} // namespace webgpu
98+
} // namespace backends
99+
} // namespace executorch
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#include <executorch/runtime/core/result.h>
12+
13+
namespace executorch {
14+
namespace backends {
15+
namespace webgpu {
16+
17+
struct WebGPUDelegateHeader {
18+
bool is_valid() const;
19+
20+
static executorch::runtime::Result<WebGPUDelegateHeader> parse(
21+
const void* data);
22+
23+
uint32_t header_size;
24+
uint32_t flatbuffer_offset;
25+
uint32_t flatbuffer_size;
26+
uint32_t bytes_offset;
27+
uint64_t bytes_size;
28+
};
29+
30+
} // namespace webgpu
31+
} // namespace backends
32+
} // namespace executorch

0 commit comments

Comments
 (0)