Skip to content

Commit 81c1a6d

Browse files
committed
fix: read data from buffer if data is not present
1 parent 413d754 commit 81c1a6d

File tree

85 files changed

+2835
-1629
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+2835
-1629
lines changed

cmake/src/runtime.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ iree::runtime::Device::~Device() {
2626
}
2727
}
2828

29-
iree::runtime::IREETensor::IREETensor(iree_hal_buffer_view_t *buffer_view, iree_hal_element_type_t type) : buffer_view(buffer_view), type(type) {
29+
iree::runtime::IREETensor::IREETensor(iree_hal_buffer_view_t *buffer_view, iree_hal_element_type_t type, iree_hal_device_t *device) : buffer_view(buffer_view), type(type), device(device) {
3030
size = iree_hal_buffer_view_byte_length(buffer_view);
3131
// TODO: fill in dim metadata
3232
}
@@ -81,6 +81,19 @@ std::vector<char> *iree::runtime::IREETensor::serialize() {
8181
size_t size_size = sizeof(size);
8282
buffer->insert(buffer->end(), reinterpret_cast<const char *>(&size), reinterpret_cast<const char *>(&size) + size_size);
8383

84+
if (data == nullptr) {
85+
data = std::malloc(size);
86+
87+
if (data == nullptr) {
88+
return nullptr;
89+
}
90+
91+
auto status = read_buffer(device, buffer_view, data, size);
92+
93+
if (!iree_status_is_ok(status)) {
94+
return nullptr;
95+
}
96+
}
8497
// Serialize 'data'
8598
buffer->insert(buffer->end(), reinterpret_cast<const char *>(data), reinterpret_cast<const char *>(data) + size);
8699

cmake/src/runtime.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ class IREETensor {
4141
std::vector<iree_hal_dim_t> dims;
4242
iree_hal_element_type_t type;
4343
iree_hal_buffer_view_t* buffer_view;
44+
iree_hal_device_t* device;
4445

4546
IREETensor(char* serialized_data);
46-
IREETensor(iree_hal_buffer_view_t* buffer_view, iree_hal_element_type_t type);
47+
IREETensor(iree_hal_buffer_view_t* buffer_view, iree_hal_element_type_t type, iree_hal_device_t* device);
4748
IREETensor(void* data, size_t size, std::vector<int64_t> in_dims, iree_hal_element_type_t type);
4849

4950
// Destructor

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE.xcodeproj/project.pbxproj

Lines changed: 85 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
0A6CE5F5167F6157FF061569 /* DisconnectedView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D488732A3104CEE004CB7B42 /* DisconnectedView.swift */; };
1313
125165EBA8D40490F0AABE88 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 51F3B59B97B70D891071B6C4 /* Assets.xcassets */; };
1414
137E19B83B668B9C3130A3D7 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7955FA5760A1DB1E827049B /* ContentView.swift */; };
15-
373237312C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */ = {isa = PBXBuildFile; fileRef = 373237212C7861AC009D1652 /* libnx_iree_runtime.so */; };
16-
373237322C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */ = {isa = PBXBuildFile; fileRef = 373237232C7861AC009D1652 /* libnx_iree_runtime.so */; };
17-
373237332C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */ = {isa = PBXBuildFile; fileRef = 373237252C7861AC009D1652 /* libnx_iree_runtime.so */; };
18-
373237342C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */ = {isa = PBXBuildFile; fileRef = 373237272C7861AC009D1652 /* libnx_iree_runtime.so */; };
19-
373237352C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */ = {isa = PBXBuildFile; fileRef = 373237292C7861AC009D1652 /* libnx_iree_runtime.so */; };
20-
373237362C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */ = {isa = PBXBuildFile; fileRef = 3732372B2C7861AC009D1652 /* libnx_iree_runtime.so */; };
21-
373237372C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */ = {isa = PBXBuildFile; fileRef = 3732372D2C7861AC009D1652 /* libnx_iree_runtime.so */; };
2215
373496882C7707DE00FD2E6F /* NxAddon.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373496872C7707DE00FD2E6F /* NxAddon.swift */; };
2316
3734968B2C77081100FD2E6F /* NxFunctionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3734968A2C77081100FD2E6F /* NxFunctionView.swift */; };
2417
379E276B2C76D40900624AA7 /* nx_iree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 379E276A2C76D40900624AA7 /* nx_iree.cpp */; };
@@ -395,6 +388,13 @@
395388
373237292C7861AC009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
396389
3732372B2C7861AC009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
397390
3732372D2C7861AC009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
391+
373237392C786921009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
392+
3732373B2C786921009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
393+
3732373D2C786921009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
394+
3732373F2C786921009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
395+
373237412C786921009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
396+
373237432C786921009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
397+
373237452C786921009D1652 /* libnx_iree_runtime.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; path = libnx_iree_runtime.so; sourceTree = "<group>"; };
398398
373496872C7707DE00FD2E6F /* NxAddon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NxAddon.swift; sourceTree = "<group>"; };
399399
3734968A2C77081100FD2E6F /* NxFunctionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NxFunctionView.swift; sourceTree = "<group>"; };
400400
379E27672C76D25900624AA7 /* LiveNxIREE-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "LiveNxIREE-Bridging-Header.h"; sourceTree = "<group>"; };
@@ -415,13 +415,6 @@
415415
isa = PBXFrameworksBuildPhase;
416416
buildActionMask = 2147483647;
417417
files = (
418-
373237372C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */,
419-
373237312C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */,
420-
373237362C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */,
421-
373237322C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */,
422-
373237332C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */,
423-
373237352C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */,
424-
373237342C7861AC009D1652 /* libnx_iree_runtime.so in Frameworks */,
425418
459F92DE90F9275614EDC9F7 /* LiveViewNative in Frameworks */,
426419
E4C8EE2081B99D0A468A7913 /* LiveViewNativeLiveForm in Frameworks */,
427420
);
@@ -1413,6 +1406,83 @@
14131406
path = nx_iree;
14141407
sourceTree = "<group>";
14151408
};
1409+
373237382C786921009D1652 /* Frameworks */ = {
1410+
isa = PBXGroup;
1411+
children = (
1412+
3732373C2C786921009D1652 /* host */,
1413+
373237422C786921009D1652 /* ios */,
1414+
3732373A2C786921009D1652 /* ios_simulator */,
1415+
373237462C786921009D1652 /* tvos */,
1416+
3732373E2C786921009D1652 /* tvos_simulator */,
1417+
373237442C786921009D1652 /* visionos */,
1418+
373237402C786921009D1652 /* visionos_simulator */,
1419+
);
1420+
name = Frameworks;
1421+
sourceTree = "<group>";
1422+
};
1423+
3732373A2C786921009D1652 /* ios_simulator */ = {
1424+
isa = PBXGroup;
1425+
children = (
1426+
373237392C786921009D1652 /* libnx_iree_runtime.so */,
1427+
);
1428+
name = ios_simulator;
1429+
path = LiveNxIREE/nx_iree/lib/ios_simulator;
1430+
sourceTree = "<group>";
1431+
};
1432+
3732373C2C786921009D1652 /* host */ = {
1433+
isa = PBXGroup;
1434+
children = (
1435+
3732373B2C786921009D1652 /* libnx_iree_runtime.so */,
1436+
);
1437+
name = host;
1438+
path = LiveNxIREE/nx_iree/lib/host;
1439+
sourceTree = "<group>";
1440+
};
1441+
3732373E2C786921009D1652 /* tvos_simulator */ = {
1442+
isa = PBXGroup;
1443+
children = (
1444+
3732373D2C786921009D1652 /* libnx_iree_runtime.so */,
1445+
);
1446+
name = tvos_simulator;
1447+
path = LiveNxIREE/nx_iree/lib/tvos_simulator;
1448+
sourceTree = "<group>";
1449+
};
1450+
373237402C786921009D1652 /* visionos_simulator */ = {
1451+
isa = PBXGroup;
1452+
children = (
1453+
3732373F2C786921009D1652 /* libnx_iree_runtime.so */,
1454+
);
1455+
name = visionos_simulator;
1456+
path = LiveNxIREE/nx_iree/lib/visionos_simulator;
1457+
sourceTree = "<group>";
1458+
};
1459+
373237422C786921009D1652 /* ios */ = {
1460+
isa = PBXGroup;
1461+
children = (
1462+
373237412C786921009D1652 /* libnx_iree_runtime.so */,
1463+
);
1464+
name = ios;
1465+
path = LiveNxIREE/nx_iree/lib/ios;
1466+
sourceTree = "<group>";
1467+
};
1468+
373237442C786921009D1652 /* visionos */ = {
1469+
isa = PBXGroup;
1470+
children = (
1471+
373237432C786921009D1652 /* libnx_iree_runtime.so */,
1472+
);
1473+
name = visionos;
1474+
path = LiveNxIREE/nx_iree/lib/visionos;
1475+
sourceTree = "<group>";
1476+
};
1477+
373237462C786921009D1652 /* tvos */ = {
1478+
isa = PBXGroup;
1479+
children = (
1480+
373237452C786921009D1652 /* libnx_iree_runtime.so */,
1481+
);
1482+
name = tvos;
1483+
path = LiveNxIREE/nx_iree/lib/tvos;
1484+
sourceTree = "<group>";
1485+
};
14161486
373496892C7707F700FD2E6F /* NxAddon */ = {
14171487
isa = PBXGroup;
14181488
children = (
@@ -1443,6 +1513,7 @@
14431513
children = (
14441514
2F2E8465248E555C831C9B55 /* LiveNxIREE */,
14451515
6CF8FEA143D447C31948C8CB /* Products */,
1516+
373237382C786921009D1652 /* Frameworks */,
14461517
);
14471518
sourceTree = "<group>";
14481519
};

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/c_src/nx_iree.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <string>
1212
#include <vector>
13+
#include <iostream>
1314

1415
int nx_iree_initialize(iree_vm_instance_t* vm_instance, iree_hal_driver_registry_t* driver_registry, char* error_message) {
1516
vm_instance = create_instance();
@@ -49,17 +50,22 @@ char** nx_iree_call(iree_vm_instance_t* vm_instance, iree_hal_device_t* device,
4950
// driver name is hardcoded because there is only a check for CUDA
5051
auto [status, optional_result] = call(vm_instance, device, "not_cuda", bytecode, static_cast<size_t>(bytecode_size), inputs);
5152

52-
if (!is_ok(status)) {
53+
if (!is_ok(status) || !optional_result.has_value()) {
5354
std::string msg = get_status_message(status);
5455
strncpy(error_message, msg.c_str(), msg.length());
5556
return nullptr;
5657
}
5758

5859
auto serialized_outputs = new char*[num_outputs];
5960

60-
auto result = optional_result.value();
61+
std::vector<iree::runtime::IREETensor *> result = optional_result.value();
6162
for (size_t i = 0; i < num_outputs; i++) {
62-
std::vector<char> *serialized = result[i]->serialize();
63+
iree::runtime::IREETensor *tensor = result[i];
64+
std::cout << "Tensor: " << tensor;
65+
if (tensor == nullptr) {
66+
std::cout << "Failed output allocation at index " << i << "\n";
67+
}
68+
std::vector<char> *serialized = tensor->serialize();
6369
serialized_outputs[i] = new char[serialized->size()];
6470
memcpy(serialized_outputs[i], serialized->data(), serialized->size());
6571
}

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/nx_iree/include/iree/base/internal/threading.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ void iree_thread_request_affinity(iree_thread_t* thread,
205205
// This has no effect if the thread is not suspended.
206206
void iree_thread_resume(iree_thread_t* thread);
207207

208+
// Blocks the current thread until |thread| has finished its execution.
209+
void iree_thread_join(iree_thread_t* thread);
210+
208211
void iree_thread_yield(void);
209212

210213
#ifdef __cplusplus

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/nx_iree/include/iree/base/loop_test.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,11 @@ TEST_F(LoopTest, WaitOneBlocking) {
596596
// Spin up the thread to signal the event after a short delay.
597597
// We need to do this before we issue the wait so that loops which perform the
598598
// wait inline can still make forward progress even if they block.
599+
//
600+
// Note: there is a race here that can cause flakes. If this new thread
601+
// starts running after the iree_loop_wait_* timeout below, the status check
602+
// will fail. We make the timeout there sufficiently long to give the OS
603+
// enough time to switch threads a few times.
599604
std::thread thread([&]() {
600605
IREE_TRACE_SCOPE();
601606
std::this_thread::sleep_for(std::chrono::milliseconds(50));
@@ -606,7 +611,7 @@ TEST_F(LoopTest, WaitOneBlocking) {
606611
bool did_wait_callback = false;
607612
} user_data;
608613
IREE_ASSERT_OK(iree_loop_wait_one(
609-
loop, wait_source, iree_make_timeout_ms(200),
614+
loop, wait_source, iree_make_timeout_ms(2000),
610615
+[](void* user_data_ptr, iree_loop_t loop, iree_status_t status) {
611616
IREE_TRACE_SCOPE();
612617
IREE_EXPECT_OK(status);
@@ -768,6 +773,11 @@ TEST_F(LoopTest, WaitAnyBlocking) {
768773
// Spin up the thread to signal the event after a short delay.
769774
// We need to do this before we issue the wait so that loops which perform the
770775
// wait inline can still make forward progress even if they block.
776+
//
777+
// Note: there is a race here that can cause flakes. If this new thread
778+
// starts running after the iree_loop_wait_* timeout below, the status check
779+
// will fail. We make the timeout there sufficiently long to give the OS
780+
// enough time to switch threads a few times.
771781
std::thread thread([&]() {
772782
IREE_TRACE_SCOPE();
773783
std::this_thread::sleep_for(std::chrono::milliseconds(50));
@@ -783,7 +793,7 @@ TEST_F(LoopTest, WaitAnyBlocking) {
783793
} user_data;
784794
IREE_ASSERT_OK(iree_loop_wait_any(
785795
loop, IREE_ARRAYSIZE(wait_sources), wait_sources,
786-
iree_make_timeout_ms(200),
796+
iree_make_timeout_ms(2000),
787797
+[](void* user_data_ptr, iree_loop_t loop, iree_status_t status) {
788798
IREE_TRACE_SCOPE();
789799
IREE_EXPECT_OK(status);
@@ -941,6 +951,11 @@ TEST_F(LoopTest, WaitAllBlocking) {
941951
// Spin up the thread to signal the event after a short delay.
942952
// We need to do this before we issue the wait so that loops which perform the
943953
// wait inline can still make forward progress even if they block.
954+
//
955+
// Note: there is a race here that can cause flakes. If this new thread
956+
// starts running after the iree_loop_wait_* timeout below, the status check
957+
// will fail. We make the timeout there sufficiently long to give the OS
958+
// enough time to switch threads a few times.
944959
std::thread thread([&]() {
945960
IREE_TRACE_SCOPE();
946961
std::this_thread::sleep_for(std::chrono::milliseconds(50));
@@ -956,7 +971,7 @@ TEST_F(LoopTest, WaitAllBlocking) {
956971
} user_data;
957972
IREE_ASSERT_OK(iree_loop_wait_all(
958973
loop, IREE_ARRAYSIZE(wait_sources), wait_sources,
959-
iree_make_timeout_ms(200),
974+
iree_make_timeout_ms(2000),
960975
+[](void* user_data_ptr, iree_loop_t loop, iree_status_t status) {
961976
IREE_TRACE_SCOPE();
962977
IREE_EXPECT_OK(status);

liveview_native/live_nx_iree/native/swiftui/LiveNxIREE/nx_iree/include/iree/builtins/ukernel/exported_bits.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
// query_tile_sizes
8787
//===----------------------------------------------------------------------===//
8888

89-
// OPERAND_ROLE describes the role that a tensor plays in an
89+
// OPERAND_INDEX describes the index that a tensor plays in an
9090
// operation, e.g. "left-hand-size operand" (e.g. in a matmul).
9191
#define IREE_UK_FLAG_QUERY_TILE_SIZES_OPERAND_ROLE_MASK 0xFF
9292
#define IREE_UK_FLAG_QUERY_TILE_SIZES_OPERAND_ROLE_NONE 0x00

0 commit comments

Comments
 (0)