rorytbyrne 13 hours ago

I do neuro/ai research, so I have to instantiate a lot of Tensors from YAML definitions (that's how I define models declaratively). I built a middleware to simplify this, using intermediary pydantic models as "blueprints" for building complex objects during pydantic's build process. It lets me pass parameters (e.g. mean and standard deviation) into `thing.model_validate(...)`, and get a fully-built Tensor in the appropriate field of the model.

https://github.com/flywhl/cyantic

---

Example:

If you write a blueprint like

  @blueprint(Tensor)    
  class NormalTensor(Blueprint[Tensor]):

      mean: float  
      std: float  
      size: tuple[int, ...]  

      def build(self) -> Tensor:  
          return torch.normal(self.mean, self.std, size=self.size)  

and a data class like

  class MyModel(CyanticModel):  
      some_tensor: Tensor  

you can construct MyModel using mean and std instead of a Tensor:

  some_yaml = """common:  
      size: [3, 5]  
  some_tensor:  
      mean: 0.0  
      std: 0.1  
      size: @value:common.size  
  """

  my_model = MyModel.model_validate(yaml.safe_load(some_yaml))  

You can also pre-process the data using `@hook` references, and there's an API for defining custom hooks.

There's good test coverage and I think the library is ready for use, but would appreciate issues if you come across any bugs.

Thanks for reading