-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4630 from FederatedAI/feature/2.0.0-beta/params
(beta) enhance params
- Loading branch information
Showing
8 changed files
with
165 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,15 @@ | ||
from typing import Literal, Union | ||
|
||
import pydantic | ||
|
||
|
||
class PaillierCipherParam(pydantic.BaseModel): | ||
method: Literal["paillier"] = "paillier" | ||
key_length: pydantic.conint(gt=1024) = 1024 | ||
|
||
|
||
class NoopCipher(pydantic.BaseModel): | ||
method: Literal[None] | ||
|
||
|
||
CipherParamType = Union[PaillierCipherParam, NoopCipher] |
This file contains 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,96 @@ | ||
import typing | ||
from typing import Any, Optional, Type, TypeVar | ||
|
||
import pydantic | ||
|
||
|
||
class Parameter: | ||
@classmethod | ||
def parse(cls, obj: Any): | ||
return pydantic.parse_obj_as(cls, obj) | ||
|
||
@classmethod | ||
def dict(cls): | ||
raise NotImplementedError() | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
def parse(type_: Type[T], obj: Any) -> T: | ||
if not isinstance(type_, typing._GenericAlias) and issubclass(type_, Parameter): | ||
return type_.parse(obj) | ||
else: | ||
return pydantic.parse_obj_as(type_, obj) | ||
|
||
|
||
def jsonschema(type_: Type[T]): | ||
return pydantic.schema_json_of(type_, indent=2) | ||
|
||
|
||
class ConstrainedInt(pydantic.ConstrainedInt, Parameter): | ||
... | ||
|
||
|
||
def conint( | ||
*, | ||
strict: bool = False, | ||
gt: int = None, | ||
ge: int = None, | ||
lt: int = None, | ||
le: int = None, | ||
multiple_of: int = None, | ||
) -> Type[int]: | ||
namespace = dict(strict=strict, gt=gt, ge=ge, lt=lt, le=le, multiple_of=multiple_of) | ||
return type("ConstrainedIntValue", (ConstrainedInt,), namespace) | ||
|
||
|
||
class ConstrainedFloat(pydantic.ConstrainedFloat, Parameter): | ||
... | ||
|
||
|
||
def confloat( | ||
*, | ||
strict: bool = False, | ||
gt: float = None, | ||
ge: float = None, | ||
lt: float = None, | ||
le: float = None, | ||
multiple_of: float = None, | ||
allow_inf_nan: Optional[bool] = None, | ||
) -> Type[float]: | ||
namespace = dict( | ||
strict=strict, | ||
gt=gt, | ||
ge=ge, | ||
lt=lt, | ||
le=le, | ||
multiple_of=multiple_of, | ||
allow_inf_nan=allow_inf_nan, | ||
) | ||
return type("ConstrainedFloatValue", (ConstrainedFloat,), namespace) | ||
|
||
|
||
class StringChoice(str, Parameter): | ||
choice = set() | ||
lower = True | ||
|
||
@classmethod | ||
def __get_validators__(cls) -> "CallableGenerator": | ||
yield cls.string_choice_validator | ||
|
||
@classmethod | ||
def string_choice_validator(cls, v): | ||
allowed = {c.lower() for c in cls.choice} if cls.lower else cls.choice | ||
provided = v.lower() if cls.lower else v | ||
if provided in allowed: | ||
return provided | ||
raise ValueError(f"provided `{provided}` not in `{allowed}`") | ||
|
||
|
||
def string_choice(choice, lower=True) -> Type[str]: | ||
namespace = dict( | ||
choice=choice, | ||
lower=lower, | ||
) | ||
return type("StringChoice", (StringChoice,), namespace) |
This file contains 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,13 @@ | ||
from ._fields import ConstrainedFloat | ||
|
||
|
||
class LearningRate(ConstrainedFloat): | ||
gt = 0.0 | ||
|
||
@classmethod | ||
def dict(cls): | ||
return {"name": cls.__name__} | ||
|
||
|
||
def learning_rate_param(): | ||
return LearningRate |
This file contains 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,13 @@ | ||
import enum | ||
from typing import Type | ||
|
||
|
||
class Optimizer(str, enum.Enum): | ||
@classmethod | ||
def __modify_schema__(cls, field_schema: dict): | ||
field_schema["description"] = "optimizer params" | ||
|
||
|
||
def optimizer_param(rmsprop=True, sgd=True, adam=True, nesterov_momentum_sgd=True, adagrad=True) -> Type[str]: | ||
choice = dict(rmsprop=rmsprop, sgd=sgd, adam=adam, nesterov_momentum_sgd=nesterov_momentum_sgd, adagrad=adagrad) | ||
return Optimizer("OptimizerParam", {k: k for k, v in choice.items() if v}) |
This file contains 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,15 @@ | ||
from typing import Type | ||
|
||
from ._fields import StringChoice | ||
|
||
|
||
class Penalty(StringChoice): | ||
chooice = {} | ||
|
||
|
||
def penalty_param(l1=True, l2=True) -> Type[str]: | ||
choice = {"L1": l1, "L2": l2} | ||
namespace = dict( | ||
chooice={k for k, v in choice.items() if v}, | ||
) | ||
return type("PenaltyValue", (Penalty,), namespace) |