-
Notifications
You must be signed in to change notification settings - Fork 218
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
[features/run] Parse primitive types #2733
Open
khalatepradnya
wants to merge
9
commits into
NVIDIA:features/run
Choose a base branch
from
khalatepradnya:recordLogDecode
base: features/run
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+238
−13
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
923771a
* Skeleton for output record logging and parsing
khalatepradnya 6843ae4
* More tests - labeled arrays, multiple shots, failing test for tuple
khalatepradnya dabeaea
* Clean-up: Removed the logger functionality since the API was implem…
khalatepradnya f807896
* Fix the format of array label - <type x size>
khalatepradnya fdec328
* Build fix - back to working stage.
khalatepradnya f6f9b72
WIP: Using the new interface for parser
khalatepradnya da44d72
* Handle primitive types
khalatepradnya bb1e6ae
* Fix test, add "qir-api" to attributes
khalatepradnya f67c3a3
Update runtime/common/RecordLogDecoder.h
schweitzpgi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,13 +8,82 @@ | |
|
||
#pragma once | ||
|
||
#include "cudaq/utils/cudaq_utils.h" | ||
#include <cstddef> | ||
#include <cstdint> | ||
#include <cstring> | ||
#include <string> | ||
#include <vector> | ||
|
||
namespace cudaq { | ||
|
||
/// QIR output schema | ||
enum struct SchemaType { LABELED, ORDERED }; | ||
enum struct RecordType { HEADER, METADATA, OUTPUT, START, END }; | ||
enum struct OutputType { RESULT, BOOL, INT, DOUBLE }; | ||
enum struct ContainerType { ARRAY, TUPLE }; | ||
|
||
/// Simple decoder for translating QIR recorded results to a C++ binary data | ||
/// structure. | ||
class RecordLogDecoder { | ||
|
||
private: | ||
std::vector<char> buffer; | ||
SchemaType schema = SchemaType::ORDERED; | ||
RecordType currentRecord; | ||
OutputType currentOutput; | ||
|
||
OutputType extractPrimitiveType(const std::string &label) { | ||
if ('i' == label[0]) { | ||
auto digits = std::stoi(label.substr(1)); | ||
if (1 == digits) | ||
return OutputType::BOOL; | ||
return OutputType::INT; | ||
} else if ('f' == label[0]) { | ||
return OutputType::DOUBLE; | ||
} | ||
throw std::runtime_error("Unknown datatype in label"); | ||
} | ||
|
||
template <typename T> | ||
void addPrimitiveRecord(T value) { | ||
/// ASKME: Is this efficient? | ||
std::size_t position = buffer.size(); | ||
buffer.resize(position + sizeof(T)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For efficiency it might be better to call |
||
std::memcpy(buffer.data() + position, &value, sizeof(T)); | ||
} | ||
|
||
void prcoessSingleRecord(const std::string &recValue, | ||
const std::string &recLabel) { | ||
if ((!recLabel.empty()) && | ||
(extractPrimitiveType(recLabel) != currentOutput)) | ||
throw std::runtime_error("Type mismatch in label"); | ||
|
||
switch (currentOutput) { | ||
case OutputType::BOOL: { | ||
bool value; | ||
if ("true" == recValue) | ||
value = true; | ||
else if ("false" == recValue) | ||
value = false; | ||
else | ||
throw std::runtime_error("Invalid boolean value"); | ||
addPrimitiveRecord<bool>(value); | ||
break; | ||
} | ||
case OutputType::INT: { | ||
addPrimitiveRecord<int>(std::stoi(recValue)); | ||
break; | ||
} | ||
case OutputType::DOUBLE: { | ||
addPrimitiveRecord<double>(std::stod(recValue)); | ||
break; | ||
} | ||
default: | ||
throw std::runtime_error("Unsupported output type"); | ||
} | ||
} | ||
|
||
public: | ||
RecordLogDecoder() = default; | ||
|
||
|
@@ -23,7 +92,89 @@ class RecordLogDecoder { | |
/// structure is created in a generic memory buffer. The buffer's address and | ||
/// length may be queried and returned as a result. | ||
void decode(const std::string &outputLog) { | ||
// NYI | ||
std::vector<std::string> lines = cudaq::split(outputLog, '\n'); | ||
if (lines.empty()) | ||
return; | ||
|
||
for (auto line : lines) { | ||
std::vector<std::string> entries = cudaq::split(line, '\t'); | ||
if (entries.empty()) | ||
continue; | ||
|
||
if ("HEADER" == entries[0]) | ||
currentRecord = RecordType::HEADER; | ||
else if ("METADATA" == entries[0]) | ||
currentRecord = RecordType::METADATA; | ||
else if ("OUTPUT" == entries[0]) | ||
currentRecord = RecordType::OUTPUT; | ||
else if ("START" == entries[0]) | ||
currentRecord = RecordType::START; | ||
else if ("END" == entries[0]) | ||
currentRecord = RecordType::END; | ||
else | ||
throw std::runtime_error("Invalid data"); | ||
|
||
switch (currentRecord) { | ||
case RecordType::HEADER: { | ||
if ("schema_name" == entries[1]) { | ||
if ("labeled" == entries[2]) | ||
schema = SchemaType::LABELED; | ||
else if ("ordered" == entries[2]) | ||
schema = SchemaType::ORDERED; | ||
else | ||
throw std::runtime_error("Unknown schema type"); | ||
} | ||
/// TODO: Check schema version | ||
break; | ||
} | ||
case RecordType::METADATA: | ||
// ignore metadata for now | ||
break; | ||
case RecordType::START: | ||
// indicates start of a shot | ||
break; | ||
case RecordType::END: { | ||
// indicates end of a shot | ||
if (entries.size() < 2) | ||
throw std::runtime_error("Missing shot status"); | ||
if ("0" != entries[1]) | ||
throw std::runtime_error("Cannot handle unsuccessful shot"); | ||
break; | ||
} | ||
case RecordType::OUTPUT: { | ||
if (entries.size() < 3) | ||
throw std::runtime_error("Insufficent data in a record"); | ||
if ((schema == SchemaType::LABELED) && (entries.size() != 4)) | ||
throw std::runtime_error( | ||
"Unexpected record size for a labeled record"); | ||
|
||
std::string recType = entries[1]; | ||
std::string recValue = entries[2]; | ||
std::string recLabel = (entries.size() == 4) ? entries[3] : ""; | ||
|
||
if ("RESULT" == recType) | ||
throw std::runtime_error("This type is not yet supported"); | ||
if ("TUPLE" == recType) | ||
throw std::runtime_error("This type is not yet supported"); | ||
if ("ARRAY" == recType) | ||
throw std::runtime_error("This type is not yet supported"); | ||
|
||
if ("BOOL" == recType) | ||
currentOutput = OutputType::BOOL; | ||
else if ("INT" == recType) | ||
currentOutput = OutputType::INT; | ||
else if ("DOUBLE" == recType) | ||
currentOutput = OutputType::DOUBLE; | ||
else | ||
throw std::runtime_error("Invalid data"); | ||
|
||
prcoessSingleRecord(recValue, recLabel); | ||
break; | ||
} | ||
default: | ||
throw std::runtime_error("Unknown record type"); | ||
} | ||
} // for line | ||
} | ||
|
||
/// Get a pointer to the data buffer. Note that the data buffer will be | ||
|
@@ -34,8 +185,5 @@ class RecordLogDecoder { | |
|
||
/// Get the size of the data buffer (in bytes). | ||
std::size_t getBufferSize() const { return buffer.size(); } | ||
|
||
private: | ||
std::vector<char> buffer; | ||
}; | ||
} // namespace cudaq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# ============================================================================ # | ||
# Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. # | ||
# All rights reserved. # | ||
# # | ||
# This source code and the accompanying materials are made available under # | ||
# the terms of the Apache License 2.0 which accompanies this distribution. # | ||
# ============================================================================ # | ||
|
||
add_executable(test_record RecordParserTester.cpp) | ||
|
||
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT APPLE) | ||
target_link_options(test_record PRIVATE -Wl,--no-as-needed) | ||
endif() | ||
target_include_directories(test_record PRIVATE ..) | ||
target_link_libraries(test_record | ||
PRIVATE | ||
fmt::fmt-header-only | ||
cudaq | ||
fmt::fmt-header-only | ||
cudaq-common | ||
gtest_main | ||
) | ||
|
||
gtest_discover_tests(test_record) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. * | ||
* All rights reserved. * | ||
* * | ||
* This source code and the accompanying materials are made available under * | ||
* the terms of the Apache License 2.0 which accompanies this distribution. * | ||
******************************************************************************/ | ||
|
||
#include "CUDAQTestUtils.h" | ||
#include "common/RecordLogDecoder.h" | ||
#include <cudaq.h> | ||
|
||
CUDAQ_TEST(ParserTester, checkSingleBoolean) { | ||
const std::string log = "OUTPUT\tBOOL\ttrue"; | ||
cudaq::RecordLogDecoder parser; | ||
parser.decode(log); | ||
auto *origBuffer = parser.getBufferPtr(); | ||
bool value; | ||
std::memcpy(&value, origBuffer, sizeof(bool)); | ||
EXPECT_EQ(true, value); | ||
} | ||
|
||
CUDAQ_TEST(ParserTester, checkIntegers) { | ||
const std::string log = "OUTPUT\tINT\t0\n" | ||
"OUTPUT\tINT\t1\n" | ||
"OUTPUT\tINT\t2\n"; | ||
cudaq::RecordLogDecoder parser; | ||
parser.decode(log); | ||
auto *origBuffer = parser.getBufferPtr(); | ||
std::size_t bufferSize = parser.getBufferSize(); | ||
EXPECT_EQ(3, bufferSize / sizeof(int)); | ||
int *buffer = static_cast<int *>(malloc(bufferSize)); | ||
std::memcpy(buffer, origBuffer, bufferSize); | ||
for (int i = 0; i < 3; ++i) | ||
EXPECT_EQ(i, buffer[i]); | ||
} | ||
|
||
CUDAQ_TEST(ParserTester, checkDoubles) { | ||
const std::string log = "START\n" | ||
"OUTPUT\tDOUBLE\t3.14\n" | ||
"OUTPUT\tDOUBLE\t2.717\n" | ||
"END\t0"; | ||
cudaq::RecordLogDecoder parser; | ||
parser.decode(log); | ||
auto *origBuffer = parser.getBufferPtr(); | ||
std::size_t bufferSize = parser.getBufferSize(); | ||
EXPECT_EQ(2, bufferSize / sizeof(double)); | ||
double *buffer = static_cast<double *>(malloc(bufferSize)); | ||
std::memcpy(buffer, origBuffer, bufferSize); | ||
EXPECT_EQ(3.14, buffer[0]); | ||
EXPECT_EQ(2.717, buffer[1]); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: class means these are already private. I prefer private data members and member functions to come last since they are not part of the interface of the class, but implementation details. Putting them up front means the reader has to wade through them to find the interface, which is where one should start reading to figure out what the class is for and does.