Skip to content

make the assert work with numpy integers #177

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
11 changes: 6 additions & 5 deletions textgrad/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from textgrad.engine import EngineLM
from typing import List, Set, Dict
import httpx
import numpy as np
from collections import defaultdict
from functools import partial
from .config import SingletonBackwardEngine
Expand All @@ -11,7 +12,7 @@
class Variable:
def __init__(
self,
value: Union[str, bytes] = "",
value: Union[str, bytes, np.integer] = "",
image_path: str = "",
predecessors: List['Variable']=None,
requires_grad: bool=True,
Expand All @@ -33,14 +34,14 @@ def __init__(

if predecessors is None:
predecessors = []

_predecessor_requires_grad = [v for v in predecessors if v.requires_grad]

if (not requires_grad) and (len(_predecessor_requires_grad) > 0):
raise Exception("If the variable does not require grad, none of its predecessors should require grad."
f"In this case, following predecessors require grad: {_predecessor_requires_grad}")
assert type(value) in [str, bytes, int], "Value must be a string, int, or image (bytes). Got: {}".format(type(value))

assert type(value) in [str, bytes, int] or np.issubdtype(value, np.integer), "Value must be a string, int, or image (bytes). Got: {}".format(type(value))
if isinstance(value, int):
value = str(value)
# We'll currently let "empty variables" slide, but we'll need to handle this better in the future.
Expand Down