-
Notifications
You must be signed in to change notification settings - Fork 16
Adds TitanRefModel in place of HF based Reference Model #94
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
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c3f2c8c
Wrapping RefModel
Jack-Khuu 4ca0685
Merge remote-tracking branch 'origin/main' into ref-actor
Jack-Khuu 3b7ee6d
Merge remote-tracking branch 'origin/main' into ref-actor
Jack-Khuu 41bdd93
Debugging Cuda issue
Jack-Khuu 7621fe4
Still debugging
Jack-Khuu 29e74aa
Commit prior to cleanup incase rebase gets rough
Jack-Khuu 135deaf
Initial clean up
Jack-Khuu ff7c120
More cleaning before mega rebase
Jack-Khuu a33030d
Merge remote-tracking branch 'origin/main' into ref-actor
Jack-Khuu 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 hidden or 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 hidden or 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,101 @@ | ||
import asyncio | ||
|
||
from datasets import load_dataset | ||
|
||
from forge.actors.policy import Policy, PolicyConfig, SamplingOverrides, WorkerConfig | ||
from forge.actors.reference_actor import HuggingFaceRefModel, TitanRefModel | ||
|
||
from forge.controller.actor import ForgeActor | ||
from forge.controller.service import ServiceConfig, shutdown_service, spawn_service | ||
from monarch.actor import endpoint | ||
|
||
|
||
class DatasetActor(ForgeActor): | ||
"""Actor wrapper for HuggingFace dataset to provide async interface.""" | ||
|
||
def __init__( | ||
self, path: str, config_name: str, split: str, streaming: bool, **kwargs | ||
): | ||
super().__init__() | ||
|
||
def gsm8k_to_messages(sample): | ||
question = sample["question"] | ||
full_answer: str = sample["answer"] | ||
answer = full_answer.split("#### ")[1] | ||
return {"question": question, "answer": answer} | ||
|
||
ds = load_dataset(path, config_name, split=split, streaming=streaming) | ||
ds = ds.map(gsm8k_to_messages) | ||
ds = ds.shuffle() | ||
self._iterator = iter(ds) | ||
|
||
@endpoint | ||
async def __next__(self) -> dict[str, str] | None: | ||
return next(self._iterator) | ||
|
||
|
||
# Sandbox; will be removed | ||
async def main(): | ||
group_size = 1 | ||
|
||
# For Torchtitan | ||
model = "Qwen/Qwen3-1.7B" | ||
|
||
# Spawn Reference "Agents" | ||
hf_model = await spawn_service( | ||
ServiceConfig(procs_per_replica=1, num_replicas=1, with_gpus=True), | ||
HuggingFaceRefModel, | ||
model_name=model, | ||
) | ||
titan_model = await spawn_service( | ||
ServiceConfig(procs_per_replica=1, num_replicas=1, with_gpus=True), | ||
TitanRefModel, | ||
) | ||
|
||
# Spawn Policy for getting responses | ||
policy = await spawn_service( | ||
ServiceConfig(procs_per_replica=1, with_gpus=True, num_replicas=1), | ||
Policy, | ||
config=PolicyConfig( | ||
worker_params=WorkerConfig(model=model), | ||
sampling_params=SamplingOverrides(num_samples=group_size, max_tokens=16), | ||
), | ||
) | ||
|
||
# Load Dataset | ||
dataloader = await spawn_service( | ||
ServiceConfig(procs_per_replica=1, num_replicas=1), | ||
DatasetActor, | ||
path="openai/gsm8k", | ||
config_name="main", | ||
split="train", | ||
streaming=True, | ||
) | ||
sample = await dataloader.__next__.choose() | ||
prompt, target = sample["question"], sample["answer"] | ||
print("Sample: ", sample) | ||
|
||
# Generate output from policy, then pass to reference agents | ||
actions = await policy.generate.choose(prompt) | ||
for action in actions: | ||
print("Generated Action tok_ids: ", action.token_ids) | ||
|
||
print("HuggingFace Results") | ||
hf_logprobs: float = await hf_model.forward.choose(action.token_ids) | ||
print("HF logprob: ", hf_logprobs) | ||
|
||
print("Titan Results") | ||
titan_logprobs: float = await titan_model.forward.choose(action.token_ids) | ||
print("Titan logprob: ", titan_logprobs) | ||
# TODO: Update forward to convert probs (logits) to logprobs | ||
|
||
await asyncio.gather( | ||
shutdown_service(policy), | ||
shutdown_service(dataloader), | ||
shutdown_service(hf_model), | ||
shutdown_service(titan_model), | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
🙃