diff options
| author | Stefan Boberg <[email protected]> | 2024-12-02 12:21:53 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-12-02 12:21:53 +0100 |
| commit | e6f44577f469e891ed8dab1492a4c53224e0b765 (patch) | |
| tree | 47334606ce62fb6bf1975cdc09276ced335599f4 /src/zencore/memory/llm.cpp | |
| parent | 5.5.15-pre0 (diff) | |
| download | zen-e6f44577f469e891ed8dab1492a4c53224e0b765.tar.xz zen-e6f44577f469e891ed8dab1492a4c53224e0b765.zip | |
added support for dynamic LLM tags (#245)
* added FLLMTag which can be used to register memory tags outside of core
* changed `UE_MEMSCOPE` -> `ZEN_MEMSCOPE` for consistency
* instrumented some subsystems with dynamic tags
Diffstat (limited to 'src/zencore/memory/llm.cpp')
| -rw-r--r-- | src/zencore/memory/llm.cpp | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/zencore/memory/llm.cpp b/src/zencore/memory/llm.cpp new file mode 100644 index 000000000..fe4853d49 --- /dev/null +++ b/src/zencore/memory/llm.cpp @@ -0,0 +1,77 @@ +// Copyright Epic Games, Inc. All Rights Reserved. + +#include <zencore/memory/llm.h> + +#include <zencore/string.h> +#include <zencore/thread.h> + +#include <atomic> + +namespace zen { + +static std::atomic<int32_t> CustomTagCounter = 257; // NOTE: hard-coded TRACE_TAG = 257 + +static const int32_t TagNamesBaseIndex = 256; +static const int32_t TrackedTagNameCount = 256; +static const char* TagNames[TrackedTagNameCount]; +static uint32_t TagNameHashes[TrackedTagNameCount]; + +static RwLock TableLock; + +FLLMTag::FLLMTag(const char* TagName) +{ + // NOTE: should add verification to prevent multiple definitions of same name? + + AssignAndAnnounceNewTag(TagName); +} + +FLLMTag::FLLMTag(const char* TagName, const FLLMTag& ParentTag) +{ + // NOTE: should add verification to prevent multiple definitions of same name? + + m_ParentTag = ParentTag.GetTag(); + + AssignAndAnnounceNewTag(TagName); +} + +void +FLLMTag::AssignAndAnnounceNewTag(const char* TagName) +{ + const uint32_t TagNameHash = HashStringDjb2(TagName); + + { + RwLock::ExclusiveLockScope _(TableLock); + + const int32_t CurrentMaxTagIndex = CustomTagCounter - TagNamesBaseIndex; + + for (int TagIndex = 0; TagIndex <= CurrentMaxTagIndex; ++TagIndex) + { + if (TagNameHashes[TagIndex] == TagNameHash) + { + m_Tag = TagIndex + TagNamesBaseIndex; + // could verify the string matches here to catch hash collisions + + // return early, no need to announce the tag as it is already known + return; + } + } + + m_Tag = ++CustomTagCounter; + + const int TagIndex = m_Tag - TagNamesBaseIndex; + + if (TagIndex < TrackedTagNameCount) + { + TagNameHashes[TagIndex] = TagNameHash; + TagNames[TagIndex] = TagName; + } + else + { + // should really let user know there's an overflow + } + } + + MemoryTrace_AnnounceCustomTag(m_Tag, m_ParentTag, TagName); +} + +} // namespace zen |