Showing the data type of model files

I’m writing some code for InvokeAI to report whether files contain fp32 or fp16 model weights. I’ve made a bit of a mess of it, so before I get too deep I should ask: is this sort of utility already written somewhere? I haven’t stumbled across any such function in diffusers yet, but nothing about it is specific to diffusion models.

Desired features include:

  • Works without reading the entire file and without loading all the weights in to memory. This is a must.
  • Compatible with multiple serialization formats — at least safetensors and pytorch pickles.
  • Can read both single-file model distributions as well as diffusers-style model directories with their collection of component models.
    • For directories, it’d be really swell if it reported on the files we specifically expect to be loaded by the pipeline, instead of having to guess which files in the directory actually contain model weights.

It’d be great if I don’t have to write this from scratch. I guess if I do end up writing it, can you think of an existing utility library that would accept it as a contribution?