Skip to content

Commit 203c56d

Browse files
committed
WIP - untested
1 parent f8d2f81 commit 203c56d

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

python/morpheus_llm/morpheus_llm/llm/nodes/extracter_node.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import numpy as np
1919

20+
from morpheus.messages import MessageMeta
2021
from morpheus_llm.llm import LLMContext
2122
from morpheus_llm.llm import LLMNodeBase
2223

@@ -59,7 +60,9 @@ async def execute(self, context: LLMContext) -> LLMContext: # pylint: disable=i
5960
# Get the keys from the task
6061
input_keys: list[str] = typing.cast(list[str], context.task()["input_keys"])
6162

62-
with context.message().payload().mutable_dataframe() as df:
63+
meta: MessageMeta = context.message().get_metadata("llm_message_meta")
64+
65+
with meta.mutable_dataframe() as df:
6366
input_dict: list[dict] = df[input_keys].to_dict(orient="list")
6467

6568
input_dict = _array_to_list(input_dict)
@@ -95,7 +98,8 @@ def get_input_names(self) -> list[str]:
9598
async def execute(self, context: LLMContext) -> LLMContext: # pylint: disable=invalid-overridden-method
9699

97100
# Get the data from the DataFrame
98-
with context.message().payload().mutable_dataframe() as df:
101+
meta: MessageMeta = context.message().get_metadata("llm_message_meta")
102+
with meta.mutable_dataframe() as df:
99103
input_dict: list[dict] = df[self._input_names].to_dict(orient="list")
100104

101105
input_dict = _array_to_list(input_dict)

python/morpheus_llm/morpheus_llm/llm/task_handlers/simple_task_handler.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import logging
1616

1717
from morpheus.messages import ControlMessage
18+
from morpheus.messages import MessageMeta
1819
from morpheus_llm.llm import LLMContext
1920
from morpheus_llm.llm import LLMTaskHandler
2021

@@ -48,7 +49,8 @@ async def try_handle(self, context: LLMContext) -> list[ControlMessage]:
4849

4950
input_dict = context.get_inputs()
5051

51-
with context.message().payload().mutable_dataframe() as df:
52+
meta: MessageMeta = context.message().get_metadata("llm_message_meta")
53+
with meta.mutable_dataframe() as df:
5254
# Write the values to the dataframe
5355
for key, value in input_dict.items():
5456
df[key] = value

python/morpheus_llm/morpheus_llm/stages/llm/llm_engine_stage.py

+47-9
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,66 @@ def supports_cpp_node(self) -> bool:
6868
"""Indicates whether this stage supports a C++ node."""
6969
return True
7070

71-
def _cast_control_message(self, message: ControlMessage, *, cpp_messages_lib: types.ModuleType) -> ControlMessage:
71+
def _store_payload(self, message: ControlMessage) -> ControlMessage:
72+
"""
73+
Store the MessageMeta in the ControlMessage's metadata.
74+
75+
In CPU-only allows the ControlMessage to hold an instance of a Python MessageMeta containing a pandas DataFrame.
76+
"""
77+
message.set_metadata("llm_message_meta", message.payload())
78+
return message
79+
80+
def _cast_to_cpp_control_message(self, message: ControlMessage, *,
81+
cpp_messages_lib: types.ModuleType) -> ControlMessage:
7282
"""
7383
LLMEngineStage does not contain a Python implementation, however it is capable of running in cpu-only mode.
74-
This method is needed to cast the Python ControlMessage to a C++ ControlMessage.
84+
This method is needed to create an instance of a C++ ControlMessage.
7585
7686
This is different than casting from the Python bindings for the C++ ControlMessage to a C++ ControlMessage.
7787
"""
78-
return cpp_messages_lib.ControlMessage(message, no_cast=True)
88+
cm = cpp_messages_lib.ControlMessage()
89+
metadata = message.get_metadata()
90+
for (key, value) in metadata.items():
91+
cm.set_metadata(key, value)
92+
93+
return cm
94+
95+
def _restore_payload(self, message: ControlMessage) -> ControlMessage:
96+
"""
97+
Pop llm_message_meta from the metadata and set it as the payload.
98+
99+
In CPU-only mode this has the effect of converting the C++ ControlMessage back to a Python ControlMessage.
100+
"""
101+
metadata = message.get_metadata()
102+
message_meta = metadata.pop("llm_message_meta")
103+
104+
out_message = ControlMessage()
105+
out_message.payload(message_meta)
106+
for (key, value) in metadata.items():
107+
out_message.set_metadata(key, value)
108+
109+
return out_message
79110

80111
def _build_single(self, builder: mrc.Builder, input_node: mrc.SegmentObject) -> mrc.SegmentObject:
81112
import morpheus_llm._lib.llm as _llm
113+
114+
store_payload_node = builder.make_node(f"{self.unique_name}-store-payload", ops.map(self._store_payload))
115+
builder.make_edge(input_node, store_payload_node)
116+
82117
node = _llm.LLMEngineStage(builder, self.unique_name, self._engine)
83118
node.launch_options.pe_count = 1
84119

85120
if self._config.execution_mode == ExecutionMode.CPU:
86121
import morpheus._lib.messages as _messages
87-
cast_fn = functools.partial(self._cast_control_message, cpp_messages_lib=_messages)
88-
pre_node = builder.make_node(f"{self.unique_name}-pre-cast", ops.map(cast_fn))
89-
builder.make_edge(input_node, pre_node)
122+
cast_to_cpp_fn = functools.partial(self._cast_to_cpp_control_message, cpp_messages_lib=_messages)
123+
cast_to_cpp_node = builder.make_node(f"{self.unique_name}-pre-msg-cast", ops.map(cast_to_cpp_fn))
124+
builder.make_edge(store_payload_node, cast_to_cpp_node)
125+
builder.make_edge(cast_to_cpp_node, node)
90126

91-
input_node = pre_node
127+
else:
128+
builder.make_edge(store_payload_node, node)
92129

93-
builder.make_edge(input_node, node)
130+
restore_payload_node = builder.make_node(f"{self.unique_name}-restore-payload", ops.map(self._restore_payload))
131+
builder.make_edge(node, restore_payload_node)
94132

95-
return node
133+
return restore_payload_node

0 commit comments

Comments
 (0)