2023-03-31 20:05:14 +02:00
|
|
|
import typing
|
|
|
|
import enum
|
|
|
|
|
|
|
|
from tml.common.modules.embedding.config import LargeEmbeddingsConfig
|
|
|
|
from tml.core.config import base_config
|
|
|
|
from tml.optimizers.config import OptimizerConfig
|
|
|
|
|
|
|
|
import pydantic
|
|
|
|
from pydantic import validator
|
|
|
|
|
|
|
|
|
|
|
|
class TwhinEmbeddingsConfig(LargeEmbeddingsConfig):
|
2023-09-13 07:52:13 +02:00
|
|
|
"""
|
|
|
|
Configuration class for Twhin model embeddings.
|
|
|
|
|
|
|
|
This class inherits from LargeEmbeddingsConfig and ensures that the embedding dimensions and data types
|
|
|
|
for all tables in the Twhin model embeddings configuration match.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
tables (List[TableConfig]): A list of table configurations for the model's embeddings.
|
|
|
|
"""
|
2023-03-31 20:05:14 +02:00
|
|
|
@validator("tables")
|
|
|
|
def embedding_dims_match(cls, tables):
|
2023-09-13 07:52:13 +02:00
|
|
|
"""
|
|
|
|
Validate that embedding dimensions and data types match for all tables.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
tables (List[TableConfig]): List of table configurations.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List[TableConfig]: The list of validated table configurations.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: If embedding dimensions or data types do not match.
|
|
|
|
"""
|
2023-03-31 20:05:14 +02:00
|
|
|
embedding_dim = tables[0].embedding_dim
|
|
|
|
data_type = tables[0].data_type
|
|
|
|
for table in tables:
|
|
|
|
assert table.embedding_dim == embedding_dim, "Embedding dimensions for all nodes must match."
|
|
|
|
assert table.data_type == data_type, "Data types for all nodes must match."
|
|
|
|
return tables
|
|
|
|
|
|
|
|
|
|
|
|
class Operator(str, enum.Enum):
|
2023-09-13 07:52:13 +02:00
|
|
|
"""
|
|
|
|
Enumeration of operator types.
|
|
|
|
|
|
|
|
This enumeration defines different types of operators that can be applied to Twhin model relations.
|
|
|
|
"""
|
2023-03-31 20:05:14 +02:00
|
|
|
TRANSLATION = "translation"
|
|
|
|
|
|
|
|
|
|
|
|
class Relation(pydantic.BaseModel):
|
2023-09-13 07:52:13 +02:00
|
|
|
"""
|
|
|
|
Configuration class for graph relationships in the Twhin model.
|
|
|
|
|
|
|
|
This class defines properties and operators for graph relationships in the Twhin model.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
name (str): The name of the relationship.
|
|
|
|
lhs (str): The name of the entity on the left-hand side of the relation.
|
|
|
|
rhs (str): The name of the entity on the right-hand side of the relation.
|
|
|
|
operator (Operator): The transformation operator to apply to the left-hand side embedding before dot product.
|
|
|
|
"""
|
2023-03-31 20:05:14 +02:00
|
|
|
|
|
|
|
name: str = pydantic.Field(..., description="Relationship name.")
|
|
|
|
lhs: str = pydantic.Field(
|
|
|
|
...,
|
|
|
|
description="Name of the entity on the left-hand-side of this relation. Must match a table name.",
|
|
|
|
)
|
|
|
|
rhs: str = pydantic.Field(
|
|
|
|
...,
|
|
|
|
description="Name of the entity on the right-hand-side of this relation. Must match a table name.",
|
|
|
|
)
|
|
|
|
operator: Operator = pydantic.Field(
|
|
|
|
Operator.TRANSLATION, description="Transformation to apply to lhs embedding before dot product."
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TwhinModelConfig(base_config.BaseConfig):
|
2023-09-13 07:52:13 +02:00
|
|
|
"""
|
|
|
|
Configuration class for the Twhin model.
|
|
|
|
|
|
|
|
This class defines configuration options specific to the Twhin model.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
embeddings (TwhinEmbeddingsConfig): Configuration for the model's embeddings.
|
|
|
|
relations (List[Relation]): List of graph relationship configurations.
|
|
|
|
translation_optimizer (OptimizerConfig): Configuration for the optimizer used for translation.
|
|
|
|
"""
|
2023-03-31 20:05:14 +02:00
|
|
|
embeddings: TwhinEmbeddingsConfig
|
|
|
|
relations: typing.List[Relation]
|
|
|
|
translation_optimizer: OptimizerConfig
|
|
|
|
|
|
|
|
@validator("relations", each_item=True)
|
|
|
|
def valid_node_types(cls, relation, values, **kwargs):
|
2023-09-13 07:52:13 +02:00
|
|
|
"""
|
|
|
|
Validate that the specified node types in relations are valid table names in embeddings.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
relation (Relation): A single relation configuration.
|
|
|
|
values (dict): The values dictionary containing the "embeddings" configuration.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Relation: The validated relation configuration.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
AssertionError: If the specified node types are not valid table names in embeddings.
|
|
|
|
"""
|
2023-03-31 20:05:14 +02:00
|
|
|
table_names = [table.name for table in values["embeddings"].tables]
|
|
|
|
assert relation.lhs in table_names, f"Invalid lhs node type: {relation.lhs}"
|
|
|
|
assert relation.rhs in table_names, f"Invalid rhs node type: {relation.rhs}"
|
|
|
|
return relation
|