Skip to content
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

Remove blacklist prompt cache check #19

Merged
merged 18 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion prompting/baseminer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from . import blacklist
from . import config
from . import forward
from . import miner
from . import mock
from . import priority
Expand Down
9 changes: 3 additions & 6 deletions prompting/baseminer/blacklist.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import wandb
import hashlib
import bittensor as bt
from typing import Union, Tuple, Callable
from typing import Union, Tuple, Callable, List
from prompting.protocol import Prompting


Expand All @@ -37,12 +37,12 @@ def is_prompt_in_cache(self, synapse: Prompting) -> bool:
should_blacklist = True
else:
caller_hotkey = synapse.dendrite.hotkey
self.prompt_cache[prompt_key] = (caller_hotkey, current_block)
self.prompt_cache[prompt_key] = current_block
ifrit98 marked this conversation as resolved.
Show resolved Hide resolved
should_blacklist = False

# Sanitize cache by removing old entries according to block span
keys_to_remove = []
for key, (_, block) in self.prompt_cache.items():
for key, block in self.prompt_cache.items():
if block + self.config.miner.blacklist.prompt_cache_block_span < current_block:
keys_to_remove.append(key)

Expand Down Expand Up @@ -78,9 +78,6 @@ def default_blacklist(self, synapse: Prompting) -> Union[Tuple[bool, str], bool]
else:
return True, "validator permit required, but hotkey not registered"

if is_prompt_in_cache(self, synapse):
return True, "prompt already sent recently"

# request period
if synapse.dendrite.hotkey in self.request_timestamps:
period = time.time() - self.request_timestamps[synapse.dendrite.hotkey][0]
Expand Down
64 changes: 0 additions & 64 deletions prompting/baseminer/forward.py

This file was deleted.

47 changes: 41 additions & 6 deletions prompting/baseminer/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from prompting.protocol import Prompting

from prompting.baseminer.priority import priority
from prompting.baseminer.blacklist import blacklist
from prompting.baseminer.blacklist import blacklist, is_prompt_in_cache
from prompting.baseminer.run import run
from prompting.baseminer.set_weights import set_weights
from prompting.baseminer.config import check_config, get_config
Expand Down Expand Up @@ -111,7 +111,7 @@ def __init__(self, config=None, axon=None, wallet=None, subtensor=None):
# Attach determiners which functions are called when servicing a request.
bt.logging.info(f"Attaching forward function to axon.")
self.axon.attach(
forward_fn=self.prompt,
forward_fn=self._prompt,
blacklist_fn=self.blacklist,
priority_fn=self.priority,
)
Expand Down Expand Up @@ -166,15 +166,43 @@ def add_args(cls, parser: argparse.ArgumentParser):
"""
...

def _prompt(self, synapse: Prompting) -> Prompting:
"""
A wrapper method around the `prompt` method that will be defined by the subclass.

This method acts as an intermediary layer to perform pre-processing before calling the
actual `prompt` method implemented in the subclass. Specifically, it checks whether a
prompt is in cache to avoid reprocessing recent requests. If the prompt is not in the
cache, the subclass `prompt` method is called.

Args:
synapse (Prompting): The incoming request object encapsulating the details of the request.

Returns:
Prompting: The response object to be sent back in reply to the incoming request, essentially
the filled synapse request object.

Raises:
ValueError: If the prompt is found in the cache indicating it was sent recently.

Example:
This method is not meant to be called directly but is invoked internally when a request
is received, and it subsequently calls the `prompt` method of the subclass.
"""
if is_prompt_in_cache(self, synapse):
raise ValueError(
f"Blacklisted: Prompt sent recently in last {self.config.miner.blacklist.prompt_cache_block_span} blocks."
)
return self.prompt(synapse)
ifrit98 marked this conversation as resolved.
Show resolved Hide resolved

@abstractmethod
def prompt(self, synapse: Prompting) -> Prompting:
"""
Abstract method to handle and respond to incoming requests to the miner.

Subclasses should implement this method to define how the miner processes
incoming requests and what responses should be sent back. The logic can include
operations like data processing, validation, or any other computation as required
by the specific mining operation.
Subclasses should implement this method to define their custom logic for processing and
responding to requests. This method is designed to be overridden, and its behavior will
be dependent on the specific implementation provided in the subclass.

Args:
synapse (Prompting): The incoming request object encapsulating the details
Expand All @@ -183,6 +211,13 @@ def prompt(self, synapse: Prompting) -> Prompting:
Returns:
Prompting: The response object that should be sent back in reply to the
incoming request. This is essentially the filled synapse request object.

Example:
class CustomMiner(Miner):
def prompt(self, synapse: Prompting) -> Prompting:
# Custom logic to process and respond to the request.
synapse.completion = "The meaning of life is 42."
return synapse
"""
...

Expand Down