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 unused import #1823

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 0 additions & 1 deletion python/ctranslate2/converters/transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from ctranslate2.specs import (
attention_spec,
common_spec,
model_spec,
transformer_spec,
wav2vec2_spec,
wav2vec2bert_spec,
Expand Down
53 changes: 28 additions & 25 deletions python/ctranslate2/specs/model_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,40 +100,43 @@ class LayerSpec(FrozenAttr, metaclass=FrozenMeta):

def validate(self) -> None:
"""Verify that the required weights are set.

Raises:
ValueError: If a required weight is not set in the specification.
"""
unset_attributes = []

def _check(spec, name, value):
def _convert_value(value):
if value is None:
unset_attributes.append(name)
return

return None

if isinstance(value, np.ndarray):
# float64 is not a supported type.
if value.dtype == np.float64:
value = value.astype(np.float32)
elif isinstance(value, float):
value = np.dtype("float32").type(value)
value = np.float32(value)
elif isinstance(value, bool):
# Convert bool to an integer type.
value = np.dtype("int8").type(value)
elif isinstance(value, str):
if value != OPTIONAL:
value = np.frombuffer(value.encode("utf-8"), dtype=np.int8)

if isinstance(value, np.ndarray) or isinstance(value, np.generic):
value = NumpyVariable(value)
value = np.int8(value)
elif isinstance(value, str) and value != OPTIONAL:
value = np.frombuffer(value.encode("utf-8"), dtype=np.int8)

if isinstance(value, (np.ndarray, np.generic)):
return NumpyVariable(value)
elif torch_is_available and isinstance(value, torch.Tensor):
value = PyTorchVariable(value)

attr_name = _split_scope(name)[-1]
setattr(spec, attr_name, value)

return PyTorchVariable(value)

return value

def _check(spec, name, value):
converted = _convert_value(value)
if converted is None:
unset_attributes.append(name)
else:
attr_name = _split_scope(name)[-1]
setattr(spec, attr_name, converted)

self._visit(_check)

if unset_attributes:
raise ValueError(
"Some required model attributes are not set:\n\n%s"
Expand Down Expand Up @@ -746,15 +749,15 @@ def num_bytes(self) -> int:
def to_bytes(self) -> bytes:
max_size = 2**31 - 1
num_bytes = self.num_bytes()
output = b""
chunks = []
offset = 0
while num_bytes > 0:
chunk_size = max_size if num_bytes > max_size else num_bytes
chunk = ctypes.string_at(self.tensor.data_ptr() + offset, chunk_size)
output += chunk
chunks.append(chunk) # Collect chunks in a list
offset += chunk_size
num_bytes -= chunk_size
return output
return b"".join(chunks)

def _to(self, dtype: str) -> Variable:
dtype = getattr(torch, dtype)
Expand Down
Loading