I am looking through the optimum code base and can’t help admiring the design patterns used throughout the code base.
I know some of these ideas come from HF. Still, I was wondering if there are any docs regarding the advantages of the from_pretrained factory design pattern over a simple object construction via init?
Hi @vblagoje, thanks for your admiration of optimum!
Optimum as an extension of transformers enables speedup on training and inference with technics like graph optimization, quantization, distillation, etc. Which also allows users to easily leverage different accelerator hardware.
@Jingya, thanks for your reply. However, I was referring specifically to the use of from_pretrained design patterns as an object factory rather than __init()__. Did anyone capture the rationale behind this approach?
Right, but in more abstract terms - why is this factory approach better than init? It allows more “room” to prepare various objects for init invocation. As you said, it “finds the correct base model class to initialise”, what are the other advantages on a more abstract level - unrelated to transformers/optimum?
It is not the same as init. All the models still have an init method. If you want to you can still create an empty model from scratch with a config, something like BertModel(config=config). from_pretrained is a utility function to use pre-trained checkpoints and configs. So it is NOT a replacement of an init function.