Skip to content

Commit

Permalink
support for granite-io backend in interpreter
Browse files Browse the repository at this point in the history
Signed-off-by: Mandana Vaziri <mvaziri@us.ibm.com>
  • Loading branch information
vazirim committed Feb 25, 2025
1 parent 2ffde3d commit 50cd829
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/hello/hello-graniteio.pdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
text:
- "Hello!\n"
- model: ibm-granite/granite-3.2-8b-instruct-preview
backend:
transformers: cpu
1 change: 1 addition & 0 deletions src/pdl/pdl_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ class GraniteioModelBlock(ModelBlock):
model: ExpressionType[object]
platform: Literal[ModelPlatform.GRANITEIO] = ModelPlatform.GRANITEIO
intrinsics: ExpressionType[list[GraniteioIntrinsicType]] = []
backend: ExpressionType[dict[str, Any]]


class CodeBlock(Block):
Expand Down
17 changes: 15 additions & 2 deletions src/pdl/pdl_llms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import asyncio
import os
import json
import aconfig
import threading
from concurrent.futures import Future
from typing import Any, Callable, Generator, TypeVar

from granite_io.io.granite_3_2 import Granite3Point2InputOutputProcessor
from granite_io.backend.transformers import TransformersBackend
from granite_io.io.base import ChatCompletionInputs

import httpx
import litellm
from dotenv import load_dotenv
Expand Down Expand Up @@ -141,8 +147,15 @@ async def async_generate_text(
messages: ModelInput,
) -> tuple[dict[str, Any], Any]:
try:
outputs = block.model.process(messages) # type: ignore # TODO
return outputs.response, outputs
if "transformers" in block.backend:
input_json_str = json.dumps({"messages": messages})
inputs = ChatCompletionInputs.model_validate_json(input_json_str)
io_processor = Granite3Point2InputOutputProcessor(
TransformersBackend(aconfig.Config({"model_name":block.model, "device":block.backend["transformers"]})),
)

result = io_processor.create_chat_completion(inputs)
return result.next_message.model_dump(), result.next_message.model_dump()
except Exception as exc:
message = f"Error during '{block.model}' model call: {repr(exc)}"
loc = block.location
Expand Down

0 comments on commit 50cd829

Please sign in to comment.